]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/dwarf2read.c
* Makefile.in (HFILES_NO_SRCDIR): Remove link-warning.h.
[thirdparty/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"
4c2df51b 58
c906108c
SS
59#include <fcntl.h>
60#include "gdb_string.h"
4bdf3d34 61#include "gdb_assert.h"
c906108c 62#include <sys/types.h>
233a11ab
CS
63#ifdef HAVE_ZLIB_H
64#include <zlib.h>
65#endif
dce234bc
PP
66#ifdef HAVE_MMAP
67#include <sys/mman.h>
85d9bd0e
TT
68#ifndef MAP_FAILED
69#define MAP_FAILED ((void *) -1)
70#endif
dce234bc 71#endif
d8151005 72
34eaf542
TT
73typedef struct symbol *symbolp;
74DEF_VEC_P (symbolp);
75
107d2387 76#if 0
357e46e7 77/* .debug_info header for a compilation unit
c906108c
SS
78 Because of alignment constraints, this structure has padding and cannot
79 be mapped directly onto the beginning of the .debug_info section. */
80typedef struct comp_unit_header
81 {
82 unsigned int length; /* length of the .debug_info
83 contribution */
84 unsigned short version; /* version number -- 2 for DWARF
85 version 2 */
86 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
87 unsigned char addr_size; /* byte size of an address -- 4 */
88 }
89_COMP_UNIT_HEADER;
90#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 91#endif
c906108c 92
c906108c
SS
93/* .debug_line statement program prologue
94 Because of alignment constraints, this structure has padding and cannot
95 be mapped directly onto the beginning of the .debug_info section. */
96typedef struct statement_prologue
97 {
98 unsigned int total_length; /* byte length of the statement
99 information */
100 unsigned short version; /* version number -- 2 for DWARF
101 version 2 */
102 unsigned int prologue_length; /* # bytes between prologue &
103 stmt program */
104 unsigned char minimum_instruction_length; /* byte size of
105 smallest instr */
106 unsigned char default_is_stmt; /* initial value of is_stmt
107 register */
108 char line_base;
109 unsigned char line_range;
110 unsigned char opcode_base; /* number assigned to first special
111 opcode */
112 unsigned char *standard_opcode_lengths;
113 }
114_STATEMENT_PROLOGUE;
115
d97bc12b
DE
116/* When non-zero, dump DIEs after they are read in. */
117static int dwarf2_die_debug = 0;
118
dce234bc
PP
119static int pagesize;
120
df8a16a1
DJ
121/* When set, the file that we're processing is known to have debugging
122 info for C++ namespaces. GCC 3.3.x did not produce this information,
123 but later versions do. */
124
125static int processing_has_namespace_info;
126
6502dd73
DJ
127static const struct objfile_data *dwarf2_objfile_data_key;
128
dce234bc
PP
129struct dwarf2_section_info
130{
131 asection *asection;
132 gdb_byte *buffer;
133 bfd_size_type size;
134 int was_mmapped;
be391dca
TT
135 /* True if we have tried to read this section. */
136 int readin;
dce234bc
PP
137};
138
9291a0cd
TT
139/* All offsets in the index are of this type. It must be
140 architecture-independent. */
141typedef uint32_t offset_type;
142
143DEF_VEC_I (offset_type);
144
145/* A description of the mapped index. The file format is described in
146 a comment by the code that writes the index. */
147struct mapped_index
148{
149 /* The total length of the buffer. */
150 off_t total_size;
151 /* A pointer to the address table data. */
152 const gdb_byte *address_table;
153 /* Size of the address table data in bytes. */
154 offset_type address_table_size;
155 /* The hash table. */
156 const offset_type *index_table;
157 /* Size in slots, each slot is 2 offset_types. */
158 offset_type index_table_slots;
159 /* A pointer to the constant pool. */
160 const char *constant_pool;
161};
162
6502dd73
DJ
163struct dwarf2_per_objfile
164{
dce234bc
PP
165 struct dwarf2_section_info info;
166 struct dwarf2_section_info abbrev;
167 struct dwarf2_section_info line;
dce234bc
PP
168 struct dwarf2_section_info loc;
169 struct dwarf2_section_info macinfo;
170 struct dwarf2_section_info str;
171 struct dwarf2_section_info ranges;
348e048f 172 struct dwarf2_section_info types;
dce234bc
PP
173 struct dwarf2_section_info frame;
174 struct dwarf2_section_info eh_frame;
9291a0cd 175 struct dwarf2_section_info gdb_index;
ae038cb0 176
be391dca
TT
177 /* Back link. */
178 struct objfile *objfile;
179
10b3939b
DJ
180 /* A list of all the compilation units. This is used to locate
181 the target compilation unit of a particular reference. */
ae038cb0
DJ
182 struct dwarf2_per_cu_data **all_comp_units;
183
184 /* The number of compilation units in ALL_COMP_UNITS. */
185 int n_comp_units;
186
1fd400ff
TT
187 /* The number of .debug_types-related CUs. */
188 int n_type_comp_units;
189
190 /* The .debug_types-related CUs. */
191 struct dwarf2_per_cu_data **type_comp_units;
192
ae038cb0
DJ
193 /* A chain of compilation units that are currently read in, so that
194 they can be freed later. */
195 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 196
348e048f
DE
197 /* A table mapping .debug_types signatures to its signatured_type entry.
198 This is NULL if the .debug_types section hasn't been read in yet. */
199 htab_t signatured_types;
200
72dca2f5
FR
201 /* A flag indicating wether this objfile has a section loaded at a
202 VMA of 0. */
203 int has_section_at_zero;
9291a0cd
TT
204
205 /* True if we are using the mapped index. */
206 unsigned char using_index;
207
208 /* The mapped index. */
209 struct mapped_index *index_table;
6502dd73
DJ
210};
211
212static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
213
214/* names of the debugging sections */
215
233a11ab
CS
216/* Note that if the debugging section has been compressed, it might
217 have a name like .zdebug_info. */
218
219#define INFO_SECTION "debug_info"
220#define ABBREV_SECTION "debug_abbrev"
221#define LINE_SECTION "debug_line"
233a11ab
CS
222#define LOC_SECTION "debug_loc"
223#define MACINFO_SECTION "debug_macinfo"
224#define STR_SECTION "debug_str"
225#define RANGES_SECTION "debug_ranges"
348e048f 226#define TYPES_SECTION "debug_types"
233a11ab
CS
227#define FRAME_SECTION "debug_frame"
228#define EH_FRAME_SECTION "eh_frame"
9291a0cd 229#define GDB_INDEX_SECTION "gdb_index"
c906108c
SS
230
231/* local data types */
232
57349743
JB
233/* We hold several abbreviation tables in memory at the same time. */
234#ifndef ABBREV_HASH_SIZE
235#define ABBREV_HASH_SIZE 121
236#endif
237
107d2387
AC
238/* The data in a compilation unit header, after target2host
239 translation, looks like this. */
c906108c 240struct comp_unit_head
a738430d 241{
c764a876 242 unsigned int length;
a738430d 243 short version;
a738430d
MK
244 unsigned char addr_size;
245 unsigned char signed_addr_p;
9cbfa09e 246 unsigned int abbrev_offset;
57349743 247
a738430d
MK
248 /* Size of file offsets; either 4 or 8. */
249 unsigned int offset_size;
57349743 250
a738430d
MK
251 /* Size of the length field; either 4 or 12. */
252 unsigned int initial_length_size;
57349743 253
a738430d
MK
254 /* Offset to the first byte of this compilation unit header in the
255 .debug_info section, for resolving relative reference dies. */
256 unsigned int offset;
57349743 257
d00adf39
DE
258 /* Offset to first die in this cu from the start of the cu.
259 This will be the first byte following the compilation unit header. */
260 unsigned int first_die_offset;
a738430d 261};
c906108c 262
e7c27a73
DJ
263/* Internal state when decoding a particular compilation unit. */
264struct dwarf2_cu
265{
266 /* The objfile containing this compilation unit. */
267 struct objfile *objfile;
268
d00adf39 269 /* The header of the compilation unit. */
e7c27a73 270 struct comp_unit_head header;
e142c38c 271
d00adf39
DE
272 /* Base address of this compilation unit. */
273 CORE_ADDR base_address;
274
275 /* Non-zero if base_address has been set. */
276 int base_known;
277
e142c38c
DJ
278 struct function_range *first_fn, *last_fn, *cached_fn;
279
280 /* The language we are debugging. */
281 enum language language;
282 const struct language_defn *language_defn;
283
b0f35d58
DL
284 const char *producer;
285
e142c38c
DJ
286 /* The generic symbol table building routines have separate lists for
287 file scope symbols and all all other scopes (local scopes). So
288 we need to select the right one to pass to add_symbol_to_list().
289 We do it by keeping a pointer to the correct list in list_in_scope.
290
291 FIXME: The original dwarf code just treated the file scope as the
292 first local scope, and all other local scopes as nested local
293 scopes, and worked fine. Check to see if we really need to
294 distinguish these in buildsym.c. */
295 struct pending **list_in_scope;
296
f3dd6933
DJ
297 /* DWARF abbreviation table associated with this compilation unit. */
298 struct abbrev_info **dwarf2_abbrevs;
299
300 /* Storage for the abbrev table. */
301 struct obstack abbrev_obstack;
72bf9492
DJ
302
303 /* Hash table holding all the loaded partial DIEs. */
304 htab_t partial_dies;
305
306 /* Storage for things with the same lifetime as this read-in compilation
307 unit, including partial DIEs. */
308 struct obstack comp_unit_obstack;
309
ae038cb0
DJ
310 /* When multiple dwarf2_cu structures are living in memory, this field
311 chains them all together, so that they can be released efficiently.
312 We will probably also want a generation counter so that most-recently-used
313 compilation units are cached... */
314 struct dwarf2_per_cu_data *read_in_chain;
315
316 /* Backchain to our per_cu entry if the tree has been built. */
317 struct dwarf2_per_cu_data *per_cu;
318
f792889a
DJ
319 /* Pointer to the die -> type map. Although it is stored
320 permanently in per_cu, we copy it here to avoid double
321 indirection. */
322 htab_t type_hash;
323
ae038cb0
DJ
324 /* How many compilation units ago was this CU last referenced? */
325 int last_used;
326
10b3939b 327 /* A hash table of die offsets for following references. */
51545339 328 htab_t die_hash;
10b3939b
DJ
329
330 /* Full DIEs if read in. */
331 struct die_info *dies;
332
333 /* A set of pointers to dwarf2_per_cu_data objects for compilation
334 units referenced by this one. Only set during full symbol processing;
335 partial symbol tables do not have dependencies. */
336 htab_t dependencies;
337
cb1df416
DJ
338 /* Header data from the line table, during full symbol processing. */
339 struct line_header *line_header;
340
ae038cb0
DJ
341 /* Mark used when releasing cached dies. */
342 unsigned int mark : 1;
343
344 /* This flag will be set if this compilation unit might include
345 inter-compilation-unit references. */
346 unsigned int has_form_ref_addr : 1;
347
72bf9492
DJ
348 /* This flag will be set if this compilation unit includes any
349 DW_TAG_namespace DIEs. If we know that there are explicit
350 DIEs for namespaces, we don't need to try to infer them
351 from mangled names. */
352 unsigned int has_namespace_info : 1;
e7c27a73
DJ
353};
354
9291a0cd
TT
355/* When using the index (and thus not using psymtabs), each CU has an
356 object of this type. This is used to hold information needed by
357 the various "quick" methods. */
358struct dwarf2_per_cu_quick_data
359{
360 /* The line table. This can be NULL if there was no line table. */
361 struct line_header *lines;
362
363 /* The file names from the line table. */
364 const char **file_names;
365 /* The file names from the line table after being run through
366 gdb_realpath. */
367 const char **full_names;
368
369 /* The corresponding symbol table. This is NULL if symbols for this
370 CU have not yet been read. */
371 struct symtab *symtab;
372
373 /* A temporary mark bit used when iterating over all CUs in
374 expand_symtabs_matching. */
375 unsigned int mark : 1;
376
377 /* True if we've tried to read the line table. */
378 unsigned int read_lines : 1;
379};
380
10b3939b
DJ
381/* Persistent data held for a compilation unit, even when not
382 processing it. We put a pointer to this structure in the
383 read_symtab_private field of the psymtab. If we encounter
384 inter-compilation-unit references, we also maintain a sorted
385 list of all compilation units. */
386
ae038cb0
DJ
387struct dwarf2_per_cu_data
388{
348e048f 389 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 390 bytes should suffice to store the length of any compilation unit
45452591
DE
391 - if it doesn't, GDB will fall over anyway.
392 NOTE: Unlike comp_unit_head.length, this length includes
393 initial_length_size. */
c764a876 394 unsigned int offset;
348e048f 395 unsigned int length : 29;
ae038cb0
DJ
396
397 /* Flag indicating this compilation unit will be read in before
398 any of the current compilation units are processed. */
c764a876 399 unsigned int queued : 1;
ae038cb0 400
5afb4e99
DJ
401 /* This flag will be set if we need to load absolutely all DIEs
402 for this compilation unit, instead of just the ones we think
403 are interesting. It gets set if we look for a DIE in the
404 hash table and don't find it. */
405 unsigned int load_all_dies : 1;
406
348e048f
DE
407 /* Non-zero if this CU is from .debug_types.
408 Otherwise it's from .debug_info. */
409 unsigned int from_debug_types : 1;
410
17ea53c3
JK
411 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
412 of the CU cache it gets reset to NULL again. */
ae038cb0 413 struct dwarf2_cu *cu;
1c379e20
DJ
414
415 /* If full symbols for this CU have been read in, then this field
416 holds a map of DIE offsets to types. It isn't always possible
417 to reconstruct this information later, so we have to preserve
418 it. */
1c379e20 419 htab_t type_hash;
10b3939b 420
9291a0cd
TT
421 /* The corresponding objfile. */
422 struct objfile *objfile;
423
424 /* When using partial symbol tables, the 'psymtab' field is active.
425 Otherwise the 'quick' field is active. */
426 union
427 {
428 /* The partial symbol table associated with this compilation unit,
429 or NULL for partial units (which do not have an associated
430 symtab). */
431 struct partial_symtab *psymtab;
432
433 /* Data needed by the "quick" functions. */
434 struct dwarf2_per_cu_quick_data *quick;
435 } v;
ae038cb0
DJ
436};
437
348e048f
DE
438/* Entry in the signatured_types hash table. */
439
440struct signatured_type
441{
442 ULONGEST signature;
443
444 /* Offset in .debug_types of the TU (type_unit) for this type. */
445 unsigned int offset;
446
447 /* Offset in .debug_types of the type defined by this TU. */
448 unsigned int type_offset;
449
450 /* The CU(/TU) of this type. */
451 struct dwarf2_per_cu_data per_cu;
452};
453
93311388
DE
454/* Struct used to pass misc. parameters to read_die_and_children, et. al.
455 which are used for both .debug_info and .debug_types dies.
456 All parameters here are unchanging for the life of the call.
457 This struct exists to abstract away the constant parameters of
458 die reading. */
459
460struct die_reader_specs
461{
462 /* The bfd of this objfile. */
463 bfd* abfd;
464
465 /* The CU of the DIE we are parsing. */
466 struct dwarf2_cu *cu;
467
468 /* Pointer to start of section buffer.
469 This is either the start of .debug_info or .debug_types. */
470 const gdb_byte *buffer;
471};
472
debd256d
JB
473/* The line number information for a compilation unit (found in the
474 .debug_line section) begins with a "statement program header",
475 which contains the following information. */
476struct line_header
477{
478 unsigned int total_length;
479 unsigned short version;
480 unsigned int header_length;
481 unsigned char minimum_instruction_length;
2dc7f7b3 482 unsigned char maximum_ops_per_instruction;
debd256d
JB
483 unsigned char default_is_stmt;
484 int line_base;
485 unsigned char line_range;
486 unsigned char opcode_base;
487
488 /* standard_opcode_lengths[i] is the number of operands for the
489 standard opcode whose value is i. This means that
490 standard_opcode_lengths[0] is unused, and the last meaningful
491 element is standard_opcode_lengths[opcode_base - 1]. */
492 unsigned char *standard_opcode_lengths;
493
494 /* The include_directories table. NOTE! These strings are not
495 allocated with xmalloc; instead, they are pointers into
496 debug_line_buffer. If you try to free them, `free' will get
497 indigestion. */
498 unsigned int num_include_dirs, include_dirs_size;
499 char **include_dirs;
500
501 /* The file_names table. NOTE! These strings are not allocated
502 with xmalloc; instead, they are pointers into debug_line_buffer.
503 Don't try to free them directly. */
504 unsigned int num_file_names, file_names_size;
505 struct file_entry
c906108c 506 {
debd256d
JB
507 char *name;
508 unsigned int dir_index;
509 unsigned int mod_time;
510 unsigned int length;
aaa75496 511 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 512 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
513 } *file_names;
514
515 /* The start and end of the statement program following this
6502dd73 516 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 517 gdb_byte *statement_program_start, *statement_program_end;
debd256d 518};
c906108c
SS
519
520/* When we construct a partial symbol table entry we only
521 need this much information. */
522struct partial_die_info
523 {
72bf9492 524 /* Offset of this DIE. */
c906108c 525 unsigned int offset;
72bf9492
DJ
526
527 /* DWARF-2 tag for this DIE. */
528 ENUM_BITFIELD(dwarf_tag) tag : 16;
529
72bf9492
DJ
530 /* Assorted flags describing the data found in this DIE. */
531 unsigned int has_children : 1;
532 unsigned int is_external : 1;
533 unsigned int is_declaration : 1;
534 unsigned int has_type : 1;
535 unsigned int has_specification : 1;
536 unsigned int has_pc_info : 1;
537
538 /* Flag set if the SCOPE field of this structure has been
539 computed. */
540 unsigned int scope_set : 1;
541
fa4028e9
JB
542 /* Flag set if the DIE has a byte_size attribute. */
543 unsigned int has_byte_size : 1;
544
72bf9492 545 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 546 sometimes a default name for unnamed DIEs. */
c906108c 547 char *name;
72bf9492
DJ
548
549 /* The scope to prepend to our children. This is generally
550 allocated on the comp_unit_obstack, so will disappear
551 when this compilation unit leaves the cache. */
552 char *scope;
553
554 /* The location description associated with this DIE, if any. */
555 struct dwarf_block *locdesc;
556
557 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
558 CORE_ADDR lowpc;
559 CORE_ADDR highpc;
72bf9492 560
93311388 561 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 562 DW_AT_sibling, if any. */
fe1b8b76 563 gdb_byte *sibling;
72bf9492
DJ
564
565 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
566 DW_AT_specification (or DW_AT_abstract_origin or
567 DW_AT_extension). */
568 unsigned int spec_offset;
569
570 /* Pointers to this DIE's parent, first child, and next sibling,
571 if any. */
572 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
573 };
574
575/* This data structure holds the information of an abbrev. */
576struct abbrev_info
577 {
578 unsigned int number; /* number identifying abbrev */
579 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
580 unsigned short has_children; /* boolean */
581 unsigned short num_attrs; /* number of attributes */
c906108c
SS
582 struct attr_abbrev *attrs; /* an array of attribute descriptions */
583 struct abbrev_info *next; /* next in chain */
584 };
585
586struct attr_abbrev
587 {
9d25dd43
DE
588 ENUM_BITFIELD(dwarf_attribute) name : 16;
589 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
590 };
591
b60c80d6
DJ
592/* Attributes have a name and a value */
593struct attribute
594 {
9d25dd43 595 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
596 ENUM_BITFIELD(dwarf_form) form : 15;
597
598 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
599 field should be in u.str (existing only for DW_STRING) but it is kept
600 here for better struct attribute alignment. */
601 unsigned int string_is_canonical : 1;
602
b60c80d6
DJ
603 union
604 {
605 char *str;
606 struct dwarf_block *blk;
43bbcdc2
PH
607 ULONGEST unsnd;
608 LONGEST snd;
b60c80d6 609 CORE_ADDR addr;
348e048f 610 struct signatured_type *signatured_type;
b60c80d6
DJ
611 }
612 u;
613 };
614
c906108c
SS
615/* This data structure holds a complete die structure. */
616struct die_info
617 {
76815b17
DE
618 /* DWARF-2 tag for this DIE. */
619 ENUM_BITFIELD(dwarf_tag) tag : 16;
620
621 /* Number of attributes */
622 unsigned short num_attrs;
623
624 /* Abbrev number */
625 unsigned int abbrev;
626
93311388 627 /* Offset in .debug_info or .debug_types section. */
76815b17 628 unsigned int offset;
78ba4af6
JB
629
630 /* The dies in a compilation unit form an n-ary tree. PARENT
631 points to this die's parent; CHILD points to the first child of
632 this node; and all the children of a given node are chained
633 together via their SIBLING fields, terminated by a die whose
634 tag is zero. */
639d11d3
DC
635 struct die_info *child; /* Its first child, if any. */
636 struct die_info *sibling; /* Its next sibling, if any. */
637 struct die_info *parent; /* Its parent, if any. */
c906108c 638
b60c80d6
DJ
639 /* An array of attributes, with NUM_ATTRS elements. There may be
640 zero, but it's not common and zero-sized arrays are not
641 sufficiently portable C. */
642 struct attribute attrs[1];
c906108c
SS
643 };
644
5fb290d7
DJ
645struct function_range
646{
647 const char *name;
648 CORE_ADDR lowpc, highpc;
649 int seen_line;
650 struct function_range *next;
651};
652
c906108c
SS
653/* Get at parts of an attribute structure */
654
655#define DW_STRING(attr) ((attr)->u.str)
8285870a 656#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
657#define DW_UNSND(attr) ((attr)->u.unsnd)
658#define DW_BLOCK(attr) ((attr)->u.blk)
659#define DW_SND(attr) ((attr)->u.snd)
660#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 661#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c
SS
662
663/* Blocks are a bunch of untyped bytes. */
664struct dwarf_block
665 {
666 unsigned int size;
fe1b8b76 667 gdb_byte *data;
c906108c
SS
668 };
669
c906108c
SS
670#ifndef ATTR_ALLOC_CHUNK
671#define ATTR_ALLOC_CHUNK 4
672#endif
673
c906108c
SS
674/* Allocate fields for structs, unions and enums in this size. */
675#ifndef DW_FIELD_ALLOC_CHUNK
676#define DW_FIELD_ALLOC_CHUNK 4
677#endif
678
c906108c
SS
679/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
680 but this would require a corresponding change in unpack_field_as_long
681 and friends. */
682static int bits_per_byte = 8;
683
684/* The routines that read and process dies for a C struct or C++ class
685 pass lists of data member fields and lists of member function fields
686 in an instance of a field_info structure, as defined below. */
687struct field_info
c5aa993b
JM
688 {
689 /* List of data member and baseclasses fields. */
690 struct nextfield
691 {
692 struct nextfield *next;
693 int accessibility;
694 int virtuality;
695 struct field field;
696 }
7d0ccb61 697 *fields, *baseclasses;
c906108c 698
7d0ccb61 699 /* Number of fields (including baseclasses). */
c5aa993b 700 int nfields;
c906108c 701
c5aa993b
JM
702 /* Number of baseclasses. */
703 int nbaseclasses;
c906108c 704
c5aa993b
JM
705 /* Set if the accesibility of one of the fields is not public. */
706 int non_public_fields;
c906108c 707
c5aa993b
JM
708 /* Member function fields array, entries are allocated in the order they
709 are encountered in the object file. */
710 struct nextfnfield
711 {
712 struct nextfnfield *next;
713 struct fn_field fnfield;
714 }
715 *fnfields;
c906108c 716
c5aa993b
JM
717 /* Member function fieldlist array, contains name of possibly overloaded
718 member function, number of overloaded member functions and a pointer
719 to the head of the member function field chain. */
720 struct fnfieldlist
721 {
722 char *name;
723 int length;
724 struct nextfnfield *head;
725 }
726 *fnfieldlists;
c906108c 727
c5aa993b
JM
728 /* Number of entries in the fnfieldlists array. */
729 int nfnfields;
98751a41
JK
730
731 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
732 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
733 struct typedef_field_list
734 {
735 struct typedef_field field;
736 struct typedef_field_list *next;
737 }
738 *typedef_field_list;
739 unsigned typedef_field_list_count;
c5aa993b 740 };
c906108c 741
10b3939b
DJ
742/* One item on the queue of compilation units to read in full symbols
743 for. */
744struct dwarf2_queue_item
745{
746 struct dwarf2_per_cu_data *per_cu;
747 struct dwarf2_queue_item *next;
748};
749
750/* The current queue. */
751static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
752
ae038cb0
DJ
753/* Loaded secondary compilation units are kept in memory until they
754 have not been referenced for the processing of this many
755 compilation units. Set this to zero to disable caching. Cache
756 sizes of up to at least twenty will improve startup time for
757 typical inter-CU-reference binaries, at an obvious memory cost. */
758static int dwarf2_max_cache_age = 5;
920d2a44
AC
759static void
760show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
761 struct cmd_list_element *c, const char *value)
762{
763 fprintf_filtered (file, _("\
764The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
765 value);
766}
767
ae038cb0 768
c906108c
SS
769/* Various complaints about symbol reading that don't abort the process */
770
4d3c2250
KB
771static void
772dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 773{
4d3c2250 774 complaint (&symfile_complaints,
e2e0b3e5 775 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
776}
777
25e43795
DJ
778static void
779dwarf2_debug_line_missing_file_complaint (void)
780{
781 complaint (&symfile_complaints,
782 _(".debug_line section has line data without a file"));
783}
784
59205f5a
JB
785static void
786dwarf2_debug_line_missing_end_sequence_complaint (void)
787{
788 complaint (&symfile_complaints,
789 _(".debug_line section has line program sequence without an end"));
790}
791
4d3c2250
KB
792static void
793dwarf2_complex_location_expr_complaint (void)
2e276125 794{
e2e0b3e5 795 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
796}
797
4d3c2250
KB
798static void
799dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
800 int arg3)
2e276125 801{
4d3c2250 802 complaint (&symfile_complaints,
e2e0b3e5 803 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
4d3c2250
KB
804 arg2, arg3);
805}
806
807static void
808dwarf2_macros_too_long_complaint (void)
2e276125 809{
4d3c2250 810 complaint (&symfile_complaints,
e2e0b3e5 811 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
812}
813
814static void
815dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 816{
4d3c2250 817 complaint (&symfile_complaints,
e2e0b3e5 818 _("macro debug info contains a malformed macro definition:\n`%s'"),
4d3c2250
KB
819 arg1);
820}
821
822static void
823dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 824{
4d3c2250 825 complaint (&symfile_complaints,
e2e0b3e5 826 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
4d3c2250 827}
c906108c 828
c906108c
SS
829/* local function prototypes */
830
4efb68b1 831static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 832
aaa75496
JB
833static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
834 struct objfile *);
835
836static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
d85a05f0 837 struct die_info *,
aaa75496
JB
838 struct partial_symtab *);
839
c67a9c90 840static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 841
72bf9492
DJ
842static void scan_partial_symbols (struct partial_die_info *,
843 CORE_ADDR *, CORE_ADDR *,
5734ee8b 844 int, struct dwarf2_cu *);
c906108c 845
72bf9492
DJ
846static void add_partial_symbol (struct partial_die_info *,
847 struct dwarf2_cu *);
63d06c5c 848
72bf9492
DJ
849static void add_partial_namespace (struct partial_die_info *pdi,
850 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 851 int need_pc, struct dwarf2_cu *cu);
63d06c5c 852
5d7cb8df
JK
853static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
854 CORE_ADDR *highpc, int need_pc,
855 struct dwarf2_cu *cu);
856
72bf9492
DJ
857static void add_partial_enumeration (struct partial_die_info *enum_pdi,
858 struct dwarf2_cu *cu);
91c24f0a 859
bc30ff58
JB
860static void add_partial_subprogram (struct partial_die_info *pdi,
861 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 862 int need_pc, struct dwarf2_cu *cu);
bc30ff58 863
fe1b8b76 864static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
865 gdb_byte *buffer, gdb_byte *info_ptr,
866 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 867
a14ed312 868static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 869
a14ed312 870static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 871
e7c27a73 872static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 873
f3dd6933 874static void dwarf2_free_abbrev_table (void *);
c906108c 875
fe1b8b76 876static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 877 struct dwarf2_cu *);
72bf9492 878
57349743 879static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 880 struct dwarf2_cu *);
c906108c 881
93311388
DE
882static struct partial_die_info *load_partial_dies (bfd *,
883 gdb_byte *, gdb_byte *,
884 int, struct dwarf2_cu *);
72bf9492 885
fe1b8b76 886static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
887 struct abbrev_info *abbrev,
888 unsigned int, bfd *,
889 gdb_byte *, gdb_byte *,
890 struct dwarf2_cu *);
c906108c 891
c764a876 892static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 893 struct dwarf2_cu *);
72bf9492
DJ
894
895static void fixup_partial_die (struct partial_die_info *,
896 struct dwarf2_cu *);
897
fe1b8b76
JB
898static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
899 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 900
fe1b8b76
JB
901static gdb_byte *read_attribute_value (struct attribute *, unsigned,
902 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 903
fe1b8b76 904static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 905
fe1b8b76 906static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 907
fe1b8b76 908static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 909
fe1b8b76 910static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 911
93311388 912static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 913
fe1b8b76 914static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 915 unsigned int *);
c906108c 916
c764a876
DE
917static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
918
919static LONGEST read_checked_initial_length_and_offset
920 (bfd *, gdb_byte *, const struct comp_unit_head *,
921 unsigned int *, unsigned int *);
613e1657 922
fe1b8b76 923static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
924 unsigned int *);
925
926static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 927
fe1b8b76 928static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 929
9b1c24c8 930static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 931
fe1b8b76
JB
932static char *read_indirect_string (bfd *, gdb_byte *,
933 const struct comp_unit_head *,
934 unsigned int *);
4bdf3d34 935
fe1b8b76 936static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 937
fe1b8b76 938static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 939
fe1b8b76 940static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 941
e142c38c 942static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 943
e142c38c
DJ
944static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
945 struct dwarf2_cu *);
c906108c 946
348e048f
DE
947static struct attribute *dwarf2_attr_no_follow (struct die_info *,
948 unsigned int,
949 struct dwarf2_cu *);
950
05cf31d1
JB
951static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
952 struct dwarf2_cu *cu);
953
e142c38c 954static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 955
e142c38c 956static struct die_info *die_specification (struct die_info *die,
f2f0e013 957 struct dwarf2_cu **);
63d06c5c 958
debd256d
JB
959static void free_line_header (struct line_header *lh);
960
aaa75496
JB
961static void add_file_name (struct line_header *, char *, unsigned int,
962 unsigned int, unsigned int);
963
debd256d
JB
964static struct line_header *(dwarf_decode_line_header
965 (unsigned int offset,
e7c27a73 966 bfd *abfd, struct dwarf2_cu *cu));
debd256d
JB
967
968static void dwarf_decode_lines (struct line_header *, char *, bfd *,
aaa75496 969 struct dwarf2_cu *, struct partial_symtab *);
c906108c 970
4f1520fb 971static void dwarf2_start_subfile (char *, char *, char *);
c906108c 972
a14ed312 973static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 974 struct dwarf2_cu *);
c906108c 975
34eaf542
TT
976static struct symbol *new_symbol_full (struct die_info *, struct type *,
977 struct dwarf2_cu *, struct symbol *);
978
a14ed312 979static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 980 struct dwarf2_cu *);
c906108c 981
2df3850c
JM
982static void dwarf2_const_value_data (struct attribute *attr,
983 struct symbol *sym,
984 int bits);
985
e7c27a73 986static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 987
b4ba55a1
JB
988static int need_gnat_info (struct dwarf2_cu *);
989
990static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
991
992static void set_descriptive_type (struct type *, struct die_info *,
993 struct dwarf2_cu *);
994
e7c27a73
DJ
995static struct type *die_containing_type (struct die_info *,
996 struct dwarf2_cu *);
c906108c 997
e7c27a73 998static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
c906108c 999
f792889a 1000static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1001
086ed43d 1002static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1003
6e70227d 1004static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1005 const char *suffix, int physname,
1006 struct dwarf2_cu *cu);
63d06c5c 1007
e7c27a73 1008static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1009
348e048f
DE
1010static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1011
e7c27a73 1012static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1013
e7c27a73 1014static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1015
ff013f42
JK
1016static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1017 struct dwarf2_cu *, struct partial_symtab *);
1018
a14ed312 1019static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1020 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1021 struct partial_symtab *);
c906108c 1022
fae299cd
DC
1023static void get_scope_pc_bounds (struct die_info *,
1024 CORE_ADDR *, CORE_ADDR *,
1025 struct dwarf2_cu *);
1026
801e3a5b
JB
1027static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1028 CORE_ADDR, struct dwarf2_cu *);
1029
a14ed312 1030static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1031 struct dwarf2_cu *);
c906108c 1032
a14ed312 1033static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1034 struct type *, struct dwarf2_cu *);
c906108c 1035
a14ed312 1036static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1037 struct die_info *, struct type *,
e7c27a73 1038 struct dwarf2_cu *);
c906108c 1039
a14ed312 1040static void dwarf2_attach_fn_fields_to_type (struct field_info *,
e7c27a73 1041 struct type *, struct dwarf2_cu *);
c906108c 1042
134d01f1 1043static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1044
e7c27a73 1045static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1046
e7c27a73 1047static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1048
5d7cb8df
JK
1049static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1050
27aa8d6a
SW
1051static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1052
f55ee35c
JK
1053static struct type *read_module_type (struct die_info *die,
1054 struct dwarf2_cu *cu);
1055
38d518c9 1056static const char *namespace_name (struct die_info *die,
e142c38c 1057 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1058
134d01f1 1059static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1060
e7c27a73 1061static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1062
6e70227d 1063static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1064 struct dwarf2_cu *);
1065
93311388 1066static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1067
93311388
DE
1068static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1069 gdb_byte *info_ptr,
d97bc12b
DE
1070 gdb_byte **new_info_ptr,
1071 struct die_info *parent);
1072
93311388
DE
1073static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1074 gdb_byte *info_ptr,
fe1b8b76 1075 gdb_byte **new_info_ptr,
639d11d3
DC
1076 struct die_info *parent);
1077
93311388
DE
1078static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1079 gdb_byte *info_ptr,
fe1b8b76 1080 gdb_byte **new_info_ptr,
639d11d3
DC
1081 struct die_info *parent);
1082
93311388
DE
1083static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1084 struct die_info **, gdb_byte *,
1085 int *);
1086
e7c27a73 1087static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1088
71c25dea
TT
1089static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1090 struct obstack *);
1091
e142c38c 1092static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1093
e142c38c 1094static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1095 struct dwarf2_cu **);
9219021c 1096
a14ed312 1097static char *dwarf_tag_name (unsigned int);
c906108c 1098
a14ed312 1099static char *dwarf_attr_name (unsigned int);
c906108c 1100
a14ed312 1101static char *dwarf_form_name (unsigned int);
c906108c 1102
a14ed312 1103static char *dwarf_bool_name (unsigned int);
c906108c 1104
a14ed312 1105static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1106
1107#if 0
a14ed312 1108static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1109#endif
1110
f9aca02d 1111static struct die_info *sibling_die (struct die_info *);
c906108c 1112
d97bc12b
DE
1113static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1114
1115static void dump_die_for_error (struct die_info *);
1116
1117static void dump_die_1 (struct ui_file *, int level, int max_level,
1118 struct die_info *);
c906108c 1119
d97bc12b 1120/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1121
51545339 1122static void store_in_ref_table (struct die_info *,
10b3939b 1123 struct dwarf2_cu *);
c906108c 1124
93311388
DE
1125static int is_ref_attr (struct attribute *);
1126
c764a876 1127static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1128
43bbcdc2 1129static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1130
348e048f
DE
1131static struct die_info *follow_die_ref_or_sig (struct die_info *,
1132 struct attribute *,
1133 struct dwarf2_cu **);
1134
10b3939b
DJ
1135static struct die_info *follow_die_ref (struct die_info *,
1136 struct attribute *,
f2f0e013 1137 struct dwarf2_cu **);
c906108c 1138
348e048f
DE
1139static struct die_info *follow_die_sig (struct die_info *,
1140 struct attribute *,
1141 struct dwarf2_cu **);
1142
1143static void read_signatured_type_at_offset (struct objfile *objfile,
1144 unsigned int offset);
1145
1146static void read_signatured_type (struct objfile *,
1147 struct signatured_type *type_sig);
1148
c906108c
SS
1149/* memory allocation interface */
1150
7b5a2f43 1151static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1152
f3dd6933 1153static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1154
b60c80d6 1155static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1156
e142c38c 1157static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1158
e142c38c
DJ
1159static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1160 struct dwarf2_cu *);
5fb290d7 1161
2e276125 1162static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1163 char *, bfd *, struct dwarf2_cu *);
2e276125 1164
8e19ed76
PS
1165static int attr_form_is_block (struct attribute *);
1166
3690dd37
JB
1167static int attr_form_is_section_offset (struct attribute *);
1168
1169static int attr_form_is_constant (struct attribute *);
1170
93e7bd98
DJ
1171static void dwarf2_symbol_mark_computed (struct attribute *attr,
1172 struct symbol *sym,
1173 struct dwarf2_cu *cu);
4c2df51b 1174
93311388
DE
1175static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1176 struct abbrev_info *abbrev,
1177 struct dwarf2_cu *cu);
4bb7a0a7 1178
72bf9492
DJ
1179static void free_stack_comp_unit (void *);
1180
72bf9492
DJ
1181static hashval_t partial_die_hash (const void *item);
1182
1183static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1184
ae038cb0 1185static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1186 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1187
1188static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1189 (unsigned int offset, struct objfile *objfile);
ae038cb0 1190
93311388
DE
1191static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1192
ae038cb0
DJ
1193static void free_one_comp_unit (void *);
1194
1195static void free_cached_comp_units (void *);
1196
1197static void age_cached_comp_units (void);
1198
1199static void free_one_cached_comp_unit (void *);
1200
f792889a
DJ
1201static struct type *set_die_type (struct die_info *, struct type *,
1202 struct dwarf2_cu *);
1c379e20 1203
ae038cb0
DJ
1204static void create_all_comp_units (struct objfile *);
1205
1fd400ff
TT
1206static int create_debug_types_hash_table (struct objfile *objfile);
1207
93311388
DE
1208static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1209 struct objfile *);
10b3939b
DJ
1210
1211static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1212
1213static void dwarf2_add_dependence (struct dwarf2_cu *,
1214 struct dwarf2_per_cu_data *);
1215
ae038cb0
DJ
1216static void dwarf2_mark (struct dwarf2_cu *);
1217
1218static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1219
f792889a 1220static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1221
9291a0cd
TT
1222static void dwarf2_release_queue (void *dummy);
1223
1224static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1225 struct objfile *objfile);
1226
1227static void process_queue (struct objfile *objfile);
1228
1229static void find_file_and_directory (struct die_info *die,
1230 struct dwarf2_cu *cu,
1231 char **name, char **comp_dir);
1232
1233static char *file_full_name (int file, struct line_header *lh,
1234 const char *comp_dir);
1235
1236static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1237 gdb_byte *info_ptr,
1238 gdb_byte *buffer,
1239 unsigned int buffer_size,
1240 bfd *abfd);
1241
1242static void init_cu_die_reader (struct die_reader_specs *reader,
1243 struct dwarf2_cu *cu);
1244
1fd400ff
TT
1245static htab_t allocate_signatured_type_hash_table (struct objfile *objfile);
1246
9291a0cd
TT
1247#if WORDS_BIGENDIAN
1248
1249/* Convert VALUE between big- and little-endian. */
1250static offset_type
1251byte_swap (offset_type value)
1252{
1253 offset_type result;
1254
1255 result = (value & 0xff) << 24;
1256 result |= (value & 0xff00) << 8;
1257 result |= (value & 0xff0000) >> 8;
1258 result |= (value & 0xff000000) >> 24;
1259 return result;
1260}
1261
1262#define MAYBE_SWAP(V) byte_swap (V)
1263
1264#else
1265#define MAYBE_SWAP(V) (V)
1266#endif /* WORDS_BIGENDIAN */
1267
1268/* The suffix for an index file. */
1269#define INDEX_SUFFIX ".gdb-index"
1270
c906108c
SS
1271/* Try to locate the sections we need for DWARF 2 debugging
1272 information and return true if we have enough to do something. */
1273
1274int
6502dd73 1275dwarf2_has_info (struct objfile *objfile)
c906108c 1276{
be391dca
TT
1277 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1278 if (!dwarf2_per_objfile)
1279 {
1280 /* Initialize per-objfile state. */
1281 struct dwarf2_per_objfile *data
1282 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1283
be391dca
TT
1284 memset (data, 0, sizeof (*data));
1285 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1286 dwarf2_per_objfile = data;
6502dd73 1287
be391dca
TT
1288 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1289 dwarf2_per_objfile->objfile = objfile;
1290 }
1291 return (dwarf2_per_objfile->info.asection != NULL
1292 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1293}
1294
233a11ab
CS
1295/* When loading sections, we can either look for ".<name>", or for
1296 * ".z<name>", which indicates a compressed section. */
1297
1298static int
dce234bc 1299section_is_p (const char *section_name, const char *name)
233a11ab 1300{
dce234bc
PP
1301 return (section_name[0] == '.'
1302 && (strcmp (section_name + 1, name) == 0
1303 || (section_name[1] == 'z'
1304 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1305}
1306
c906108c
SS
1307/* This function is mapped across the sections and remembers the
1308 offset and size of each of the debugging sections we are interested
1309 in. */
1310
1311static void
72dca2f5 1312dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1313{
dce234bc 1314 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1315 {
dce234bc
PP
1316 dwarf2_per_objfile->info.asection = sectp;
1317 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1318 }
dce234bc 1319 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1320 {
dce234bc
PP
1321 dwarf2_per_objfile->abbrev.asection = sectp;
1322 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1323 }
dce234bc 1324 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1325 {
dce234bc
PP
1326 dwarf2_per_objfile->line.asection = sectp;
1327 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1328 }
dce234bc 1329 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1330 {
dce234bc
PP
1331 dwarf2_per_objfile->loc.asection = sectp;
1332 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1333 }
dce234bc 1334 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1335 {
dce234bc
PP
1336 dwarf2_per_objfile->macinfo.asection = sectp;
1337 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1338 }
dce234bc 1339 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1340 {
dce234bc
PP
1341 dwarf2_per_objfile->str.asection = sectp;
1342 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1343 }
dce234bc 1344 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1345 {
dce234bc
PP
1346 dwarf2_per_objfile->frame.asection = sectp;
1347 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1348 }
dce234bc 1349 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1350 {
3799ccc6 1351 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1352
3799ccc6
EZ
1353 if (aflag & SEC_HAS_CONTENTS)
1354 {
dce234bc
PP
1355 dwarf2_per_objfile->eh_frame.asection = sectp;
1356 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1357 }
b6af0555 1358 }
dce234bc 1359 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1360 {
dce234bc
PP
1361 dwarf2_per_objfile->ranges.asection = sectp;
1362 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1363 }
348e048f
DE
1364 else if (section_is_p (sectp->name, TYPES_SECTION))
1365 {
1366 dwarf2_per_objfile->types.asection = sectp;
1367 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1368 }
9291a0cd
TT
1369 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1370 {
1371 dwarf2_per_objfile->gdb_index.asection = sectp;
1372 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1373 }
dce234bc 1374
72dca2f5
FR
1375 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1376 && bfd_section_vma (abfd, sectp) == 0)
1377 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1378}
1379
dce234bc
PP
1380/* Decompress a section that was compressed using zlib. Store the
1381 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1382
1383static void
dce234bc
PP
1384zlib_decompress_section (struct objfile *objfile, asection *sectp,
1385 gdb_byte **outbuf, bfd_size_type *outsize)
1386{
1387 bfd *abfd = objfile->obfd;
1388#ifndef HAVE_ZLIB_H
1389 error (_("Support for zlib-compressed DWARF data (from '%s') "
1390 "is disabled in this copy of GDB"),
1391 bfd_get_filename (abfd));
1392#else
1393 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1394 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1395 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1396 bfd_size_type uncompressed_size;
1397 gdb_byte *uncompressed_buffer;
1398 z_stream strm;
1399 int rc;
1400 int header_size = 12;
1401
1402 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1403 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1404 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1405 bfd_get_filename (abfd));
1406
1407 /* Read the zlib header. In this case, it should be "ZLIB" followed
1408 by the uncompressed section size, 8 bytes in big-endian order. */
1409 if (compressed_size < header_size
1410 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1411 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1412 bfd_get_filename (abfd));
1413 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1414 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1415 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1416 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1417 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1418 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1419 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1420 uncompressed_size += compressed_buffer[11];
1421
1422 /* It is possible the section consists of several compressed
1423 buffers concatenated together, so we uncompress in a loop. */
1424 strm.zalloc = NULL;
1425 strm.zfree = NULL;
1426 strm.opaque = NULL;
1427 strm.avail_in = compressed_size - header_size;
1428 strm.next_in = (Bytef*) compressed_buffer + header_size;
1429 strm.avail_out = uncompressed_size;
1430 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1431 uncompressed_size);
1432 rc = inflateInit (&strm);
1433 while (strm.avail_in > 0)
1434 {
1435 if (rc != Z_OK)
1436 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1437 bfd_get_filename (abfd), rc);
1438 strm.next_out = ((Bytef*) uncompressed_buffer
1439 + (uncompressed_size - strm.avail_out));
1440 rc = inflate (&strm, Z_FINISH);
1441 if (rc != Z_STREAM_END)
1442 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1443 bfd_get_filename (abfd), rc);
1444 rc = inflateReset (&strm);
1445 }
1446 rc = inflateEnd (&strm);
1447 if (rc != Z_OK
1448 || strm.avail_out != 0)
1449 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1450 bfd_get_filename (abfd), rc);
1451
affddf13 1452 do_cleanups (cleanup);
dce234bc
PP
1453 *outbuf = uncompressed_buffer;
1454 *outsize = uncompressed_size;
1455#endif
233a11ab
CS
1456}
1457
dce234bc
PP
1458/* Read the contents of the section SECTP from object file specified by
1459 OBJFILE, store info about the section into INFO.
1460 If the section is compressed, uncompress it before returning. */
c906108c 1461
dce234bc
PP
1462static void
1463dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1464{
dce234bc
PP
1465 bfd *abfd = objfile->obfd;
1466 asection *sectp = info->asection;
1467 gdb_byte *buf, *retbuf;
1468 unsigned char header[4];
c906108c 1469
be391dca
TT
1470 if (info->readin)
1471 return;
dce234bc
PP
1472 info->buffer = NULL;
1473 info->was_mmapped = 0;
be391dca 1474 info->readin = 1;
188dd5d6 1475
dce234bc
PP
1476 if (info->asection == NULL || info->size == 0)
1477 return;
c906108c 1478
dce234bc
PP
1479 /* Check if the file has a 4-byte header indicating compression. */
1480 if (info->size > sizeof (header)
1481 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1482 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1483 {
1484 /* Upon decompression, update the buffer and its size. */
1485 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1486 {
1487 zlib_decompress_section (objfile, sectp, &info->buffer,
1488 &info->size);
1489 return;
1490 }
1491 }
4bdf3d34 1492
dce234bc
PP
1493#ifdef HAVE_MMAP
1494 if (pagesize == 0)
1495 pagesize = getpagesize ();
2e276125 1496
dce234bc
PP
1497 /* Only try to mmap sections which are large enough: we don't want to
1498 waste space due to fragmentation. Also, only try mmap for sections
1499 without relocations. */
1500
1501 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1502 {
1503 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1504 size_t map_length = info->size + sectp->filepos - pg_offset;
1505 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1506 MAP_PRIVATE, pg_offset);
1507
1508 if (retbuf != MAP_FAILED)
1509 {
1510 info->was_mmapped = 1;
1511 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1512#if HAVE_POSIX_MADVISE
1513 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1514#endif
dce234bc
PP
1515 return;
1516 }
1517 }
1518#endif
1519
1520 /* If we get here, we are a normal, not-compressed section. */
1521 info->buffer = buf
1522 = obstack_alloc (&objfile->objfile_obstack, info->size);
1523
1524 /* When debugging .o files, we may need to apply relocations; see
1525 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1526 We never compress sections in .o files, so we only need to
1527 try this when the section is not compressed. */
ac8035ab 1528 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1529 if (retbuf != NULL)
1530 {
1531 info->buffer = retbuf;
1532 return;
1533 }
1534
1535 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1536 || bfd_bread (buf, info->size, abfd) != info->size)
1537 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1538 bfd_get_filename (abfd));
1539}
1540
1541/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1542 SECTION_NAME. */
af34e669 1543
dce234bc
PP
1544void
1545dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1546 asection **sectp, gdb_byte **bufp,
1547 bfd_size_type *sizep)
1548{
1549 struct dwarf2_per_objfile *data
1550 = objfile_data (objfile, dwarf2_objfile_data_key);
1551 struct dwarf2_section_info *info;
a3b2a86b
TT
1552
1553 /* We may see an objfile without any DWARF, in which case we just
1554 return nothing. */
1555 if (data == NULL)
1556 {
1557 *sectp = NULL;
1558 *bufp = NULL;
1559 *sizep = 0;
1560 return;
1561 }
dce234bc
PP
1562 if (section_is_p (section_name, EH_FRAME_SECTION))
1563 info = &data->eh_frame;
1564 else if (section_is_p (section_name, FRAME_SECTION))
1565 info = &data->frame;
0d53c4c4 1566 else
dce234bc
PP
1567 gdb_assert (0);
1568
1569 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1570 /* We haven't read this section in yet. Do it now. */
1571 dwarf2_read_section (objfile, info);
1572
1573 *sectp = info->asection;
1574 *bufp = info->buffer;
1575 *sizep = info->size;
1576}
1577
9291a0cd
TT
1578\f
1579
1580/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1581 this CU came. */
1582static void
1583dw2_do_instantiate_symtab (struct objfile *objfile,
1584 struct dwarf2_per_cu_data *per_cu)
1585{
1586 struct cleanup *back_to;
1587
1588 back_to = make_cleanup (dwarf2_release_queue, NULL);
1589
1590 queue_comp_unit (per_cu, objfile);
1591
1592 if (per_cu->from_debug_types)
1593 read_signatured_type_at_offset (objfile, per_cu->offset);
1594 else
1595 load_full_comp_unit (per_cu, objfile);
1596
1597 process_queue (objfile);
1598
1599 /* Age the cache, releasing compilation units that have not
1600 been used recently. */
1601 age_cached_comp_units ();
1602
1603 do_cleanups (back_to);
1604}
1605
1606/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1607 the objfile from which this CU came. Returns the resulting symbol
1608 table. */
1609static struct symtab *
1610dw2_instantiate_symtab (struct objfile *objfile,
1611 struct dwarf2_per_cu_data *per_cu)
1612{
1613 if (!per_cu->v.quick->symtab)
1614 {
1615 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1616 increment_reading_symtab ();
1617 dw2_do_instantiate_symtab (objfile, per_cu);
1618 do_cleanups (back_to);
1619 }
1620 return per_cu->v.quick->symtab;
1621}
1622
1fd400ff
TT
1623/* Return the CU given its index. */
1624static struct dwarf2_per_cu_data *
1625dw2_get_cu (int index)
1626{
1627 if (index >= dwarf2_per_objfile->n_comp_units)
1628 {
1629 index -= dwarf2_per_objfile->n_comp_units;
1630 return dwarf2_per_objfile->type_comp_units[index];
1631 }
1632 return dwarf2_per_objfile->all_comp_units[index];
1633}
1634
9291a0cd
TT
1635/* A helper function that knows how to read a 64-bit value in a way
1636 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1637 otherwise. */
1638static int
1639extract_cu_value (const char *bytes, ULONGEST *result)
1640{
1641 if (sizeof (ULONGEST) < 8)
1642 {
1643 int i;
1644
1645 /* Ignore the upper 4 bytes if they are all zero. */
1646 for (i = 0; i < 4; ++i)
1647 if (bytes[i + 4] != 0)
1648 return 0;
1649
1650 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1651 }
1652 else
1653 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1654 return 1;
1655}
1656
1657/* Read the CU list from the mapped index, and use it to create all
1658 the CU objects for this objfile. Return 0 if something went wrong,
1659 1 if everything went ok. */
1660static int
1fd400ff
TT
1661create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1662 offset_type cu_list_elements)
9291a0cd
TT
1663{
1664 offset_type i;
9291a0cd
TT
1665
1666 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1667 dwarf2_per_objfile->all_comp_units
1668 = obstack_alloc (&objfile->objfile_obstack,
1669 dwarf2_per_objfile->n_comp_units
1670 * sizeof (struct dwarf2_per_cu_data *));
1671
1672 for (i = 0; i < cu_list_elements; i += 2)
1673 {
1674 struct dwarf2_per_cu_data *the_cu;
1675 ULONGEST offset, length;
1676
1677 if (!extract_cu_value (cu_list, &offset)
1678 || !extract_cu_value (cu_list + 8, &length))
1679 return 0;
1680 cu_list += 2 * 8;
1681
1682 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1683 struct dwarf2_per_cu_data);
1684 the_cu->offset = offset;
1685 the_cu->length = length;
1686 the_cu->objfile = objfile;
1687 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1688 struct dwarf2_per_cu_quick_data);
1689 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1690 }
1691
1692 return 1;
1693}
1694
1fd400ff
TT
1695/* Create the signatured type hash table from the index. */
1696static int
1697create_signatured_type_hash_from_index (struct objfile *objfile,
1698 const gdb_byte *bytes,
1699 offset_type elements)
1700{
1701 offset_type i;
1702 htab_t type_hash;
1703
1704 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1705 dwarf2_per_objfile->type_comp_units
1706 = obstack_alloc (&objfile->objfile_obstack,
1707 dwarf2_per_objfile->n_type_comp_units
1708 * sizeof (struct dwarf2_per_cu_data *));
1709
1710 type_hash = allocate_signatured_type_hash_table (objfile);
1711
1712 for (i = 0; i < elements; i += 3)
1713 {
1714 struct signatured_type *type_sig;
1715 ULONGEST offset, type_offset, signature;
1716 void **slot;
1717
1718 if (!extract_cu_value (bytes, &offset)
1719 || !extract_cu_value (bytes + 8, &type_offset))
1720 return 0;
1721 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1722 bytes += 3 * 8;
1723
1724 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1725 struct signatured_type);
1726 type_sig->signature = signature;
1727 type_sig->offset = offset;
1728 type_sig->type_offset = type_offset;
1729 type_sig->per_cu.from_debug_types = 1;
1730 type_sig->per_cu.offset = offset;
1731 type_sig->per_cu.objfile = objfile;
1732 type_sig->per_cu.v.quick
1733 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1734 struct dwarf2_per_cu_quick_data);
1735
1736 slot = htab_find_slot (type_hash, type_sig, INSERT);
1737 *slot = type_sig;
1738
1739 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1740 }
1741
1742 dwarf2_per_objfile->signatured_types = type_hash;
1743
1744 return 1;
1745}
1746
9291a0cd
TT
1747/* Read the address map data from the mapped index, and use it to
1748 populate the objfile's psymtabs_addrmap. */
1749static void
1750create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1751{
1752 const gdb_byte *iter, *end;
1753 struct obstack temp_obstack;
1754 struct addrmap *mutable_map;
1755 struct cleanup *cleanup;
1756 CORE_ADDR baseaddr;
1757
1758 obstack_init (&temp_obstack);
1759 cleanup = make_cleanup_obstack_free (&temp_obstack);
1760 mutable_map = addrmap_create_mutable (&temp_obstack);
1761
1762 iter = index->address_table;
1763 end = iter + index->address_table_size;
1764
1765 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1766
1767 while (iter < end)
1768 {
1769 ULONGEST hi, lo, cu_index;
1770 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1771 iter += 8;
1772 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1773 iter += 8;
1774 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1775 iter += 4;
1776
1777 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1778 dw2_get_cu (cu_index));
9291a0cd
TT
1779 }
1780
1781 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1782 &objfile->objfile_obstack);
1783 do_cleanups (cleanup);
1784}
1785
1786/* The hash function for strings in the mapped index. This is the
1787 same as the hashtab.c hash function, but we keep a separate copy to
1788 maintain control over the implementation. This is necessary
1789 because the hash function is tied to the format of the mapped index
1790 file. */
1791static hashval_t
1792mapped_index_string_hash (const void *p)
1793{
1794 const unsigned char *str = (const unsigned char *) p;
1795 hashval_t r = 0;
1796 unsigned char c;
1797
1798 while ((c = *str++) != 0)
1799 r = r * 67 + c - 113;
1800
1801 return r;
1802}
1803
1804/* Find a slot in the mapped index INDEX for the object named NAME.
1805 If NAME is found, set *VEC_OUT to point to the CU vector in the
1806 constant pool and return 1. If NAME cannot be found, return 0. */
1807static int
1808find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1809 offset_type **vec_out)
1810{
1811 offset_type hash = mapped_index_string_hash (name);
1812 offset_type slot, step;
1813
1814 slot = hash & (index->index_table_slots - 1);
1815 step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1816
1817 for (;;)
1818 {
1819 /* Convert a slot number to an offset into the table. */
1820 offset_type i = 2 * slot;
1821 const char *str;
1822 if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1823 return 0;
1824
1825 str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1826 if (!strcmp (name, str))
1827 {
1828 *vec_out = (offset_type *) (index->constant_pool
1829 + MAYBE_SWAP (index->index_table[i + 1]));
1830 return 1;
1831 }
1832
1833 slot = (slot + step) & (index->index_table_slots - 1);
1834 }
1835}
1836
1837/* Read the index file. If everything went ok, initialize the "quick"
1838 elements of all the CUs and return 1. Otherwise, return 0. */
1839static int
1840dwarf2_read_index (struct objfile *objfile)
1841{
9291a0cd
TT
1842 char *addr;
1843 struct mapped_index *map;
b3b272e1 1844 offset_type *metadata;
ac0b195c
KW
1845 const gdb_byte *cu_list;
1846 const gdb_byte *types_list = NULL;
1847 offset_type version, cu_list_elements;
1848 offset_type types_list_elements = 0;
1fd400ff 1849 int i;
9291a0cd
TT
1850
1851 if (dwarf2_per_objfile->gdb_index.asection == NULL
1852 || dwarf2_per_objfile->gdb_index.size == 0)
1853 return 0;
1854 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1855
1856 addr = dwarf2_per_objfile->gdb_index.buffer;
1857 /* Version check. */
1fd400ff
TT
1858 version = MAYBE_SWAP (*(offset_type *) addr);
1859 if (version == 1)
1860 {
1861 /* Index version 1 neglected to account for .debug_types. So,
1862 if we see .debug_types, we cannot use this index. */
1863 if (dwarf2_per_objfile->types.asection != NULL
1864 && dwarf2_per_objfile->types.size != 0)
1865 return 0;
1866 }
1867 else if (version != 2)
9291a0cd
TT
1868 return 0;
1869
1870 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 1871 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
1872
1873 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
1874
1875 i = 0;
1876 cu_list = addr + MAYBE_SWAP (metadata[i]);
1877 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 1878 / 8);
1fd400ff
TT
1879 ++i;
1880
1881 if (version == 2)
1882 {
1883 types_list = addr + MAYBE_SWAP (metadata[i]);
1884 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1885 - MAYBE_SWAP (metadata[i]))
1886 / 8);
1887 ++i;
1888 }
1889
1890 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1891 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1892 - MAYBE_SWAP (metadata[i]));
1893 ++i;
1894
1895 map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1896 map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1897 - MAYBE_SWAP (metadata[i]))
9291a0cd 1898 / (2 * sizeof (offset_type)));
1fd400ff 1899 ++i;
9291a0cd 1900
1fd400ff
TT
1901 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1902
1903 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1904 return 0;
1905
1906 if (version == 2
1907 && types_list_elements
1908 && !create_signatured_type_hash_from_index (objfile, types_list,
1909 types_list_elements))
9291a0cd
TT
1910 return 0;
1911
1912 create_addrmap_from_index (objfile, map);
1913
1914 dwarf2_per_objfile->index_table = map;
1915 dwarf2_per_objfile->using_index = 1;
1916
1917 return 1;
1918}
1919
1920/* A helper for the "quick" functions which sets the global
1921 dwarf2_per_objfile according to OBJFILE. */
1922static void
1923dw2_setup (struct objfile *objfile)
1924{
1925 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1926 gdb_assert (dwarf2_per_objfile);
1927}
1928
1929/* A helper for the "quick" functions which attempts to read the line
1930 table for THIS_CU. */
1931static void
1932dw2_require_line_header (struct objfile *objfile,
1933 struct dwarf2_per_cu_data *this_cu)
1934{
1935 bfd *abfd = objfile->obfd;
1936 struct line_header *lh = NULL;
1937 struct attribute *attr;
1938 struct cleanup *cleanups;
1939 struct die_info *comp_unit_die;
1940 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
1941 int has_children, i;
1942 struct dwarf2_cu cu;
1943 unsigned int bytes_read, buffer_size;
1944 struct die_reader_specs reader_specs;
1945 char *name, *comp_dir;
1946
1947 if (this_cu->v.quick->read_lines)
1948 return;
1949 this_cu->v.quick->read_lines = 1;
1950
1951 memset (&cu, 0, sizeof (cu));
1952 cu.objfile = objfile;
1953 obstack_init (&cu.comp_unit_obstack);
1954
1955 cleanups = make_cleanup (free_stack_comp_unit, &cu);
1956
1957 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
1958 buffer_size = dwarf2_per_objfile->info.size;
1959 buffer = dwarf2_per_objfile->info.buffer;
1960 info_ptr = buffer + this_cu->offset;
1961 beg_of_comp_unit = info_ptr;
1962
1963 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
1964 buffer, buffer_size,
1965 abfd);
1966
1967 /* Complete the cu_header. */
1968 cu.header.offset = beg_of_comp_unit - buffer;
1969 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
1970
1971 this_cu->cu = &cu;
1972 cu.per_cu = this_cu;
1973
1974 dwarf2_read_abbrevs (abfd, &cu);
1975 make_cleanup (dwarf2_free_abbrev_table, &cu);
1976
1977 if (this_cu->from_debug_types)
1978 info_ptr += 8 /*signature*/ + cu.header.offset_size;
1979 init_cu_die_reader (&reader_specs, &cu);
1980 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
1981 &has_children);
1982
1983 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
1984 if (attr)
1985 {
1986 unsigned int line_offset = DW_UNSND (attr);
1987 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
1988 }
1989 if (lh == NULL)
1990 {
1991 do_cleanups (cleanups);
1992 return;
1993 }
1994
1995 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
1996
1997 this_cu->v.quick->lines = lh;
1998
1999 this_cu->v.quick->file_names
2000 = obstack_alloc (&objfile->objfile_obstack,
2001 lh->num_file_names * sizeof (char *));
2002 for (i = 0; i < lh->num_file_names; ++i)
2003 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2004
2005 do_cleanups (cleanups);
2006}
2007
2008/* A helper for the "quick" functions which computes and caches the
2009 real path for a given file name from the line table.
2010 dw2_require_line_header must have been called before this is
2011 invoked. */
2012static const char *
2013dw2_require_full_path (struct objfile *objfile,
2014 struct dwarf2_per_cu_data *cu,
2015 int index)
2016{
2017 if (!cu->v.quick->full_names)
2018 cu->v.quick->full_names
2019 = OBSTACK_CALLOC (&objfile->objfile_obstack,
2020 cu->v.quick->lines->num_file_names,
2021 sizeof (char *));
2022
2023 if (!cu->v.quick->full_names[index])
2024 cu->v.quick->full_names[index]
2025 = gdb_realpath (cu->v.quick->file_names[index]);
2026
2027 return cu->v.quick->full_names[index];
2028}
2029
2030static struct symtab *
2031dw2_find_last_source_symtab (struct objfile *objfile)
2032{
2033 int index;
2034 dw2_setup (objfile);
2035 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2036 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2037}
2038
2039static void
2040dw2_forget_cached_source_info (struct objfile *objfile)
2041{
2042 int i;
2043
2044 dw2_setup (objfile);
1fd400ff
TT
2045 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2046 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2047 {
1fd400ff 2048 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2049
2050 if (cu->v.quick->full_names)
2051 {
2052 int j;
2053
2054 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2055 xfree ((void *) cu->v.quick->full_names[j]);
2056 }
2057 }
2058}
2059
2060static int
2061dw2_lookup_symtab (struct objfile *objfile, const char *name,
2062 const char *full_path, const char *real_path,
2063 struct symtab **result)
2064{
2065 int i;
2066 int check_basename = lbasename (name) == name;
2067 struct dwarf2_per_cu_data *base_cu = NULL;
2068
2069 dw2_setup (objfile);
1fd400ff
TT
2070 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2071 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2072 {
2073 int j;
1fd400ff 2074 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2075
2076 if (cu->v.quick->symtab)
2077 continue;
2078
2079 dw2_require_line_header (objfile, cu);
2080 if (!cu->v.quick->lines)
2081 continue;
2082
2083 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2084 {
2085 const char *this_name = cu->v.quick->file_names[j];
2086
2087 if (FILENAME_CMP (name, this_name) == 0)
2088 {
2089 *result = dw2_instantiate_symtab (objfile, cu);
2090 return 1;
2091 }
2092
2093 if (check_basename && ! base_cu
2094 && FILENAME_CMP (lbasename (this_name), name) == 0)
2095 base_cu = cu;
2096
2097 if (full_path != NULL)
2098 {
2099 const char *this_full_name = dw2_require_full_path (objfile,
2100 cu, j);
2101
2102 if (this_full_name
2103 && FILENAME_CMP (full_path, this_full_name) == 0)
2104 {
2105 *result = dw2_instantiate_symtab (objfile, cu);
2106 return 1;
2107 }
2108 }
2109
2110 if (real_path != NULL)
2111 {
2112 const char *this_full_name = dw2_require_full_path (objfile,
2113 cu, j);
2114
2115 if (this_full_name != NULL)
2116 {
2117 char *rp = gdb_realpath (this_full_name);
2118 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2119 {
2120 xfree (rp);
2121 *result = dw2_instantiate_symtab (objfile, cu);
2122 return 1;
2123 }
2124 xfree (rp);
2125 }
2126 }
2127 }
2128 }
2129
2130 if (base_cu)
2131 {
2132 *result = dw2_instantiate_symtab (objfile, base_cu);
2133 return 1;
2134 }
2135
2136 return 0;
2137}
2138
2139static struct symtab *
2140dw2_lookup_symbol (struct objfile *objfile, int block_index,
2141 const char *name, domain_enum domain)
2142{
2143 /* We do all the work in the pre_expand_symtabs_matching hook
2144 instead. */
2145 return NULL;
2146}
2147
2148/* A helper function that expands all symtabs that hold an object
2149 named NAME. */
2150static void
2151dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2152{
2153 dw2_setup (objfile);
2154
2155 if (dwarf2_per_objfile->index_table)
2156 {
2157 offset_type *vec;
2158
2159 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2160 name, &vec))
2161 {
2162 offset_type i, len = MAYBE_SWAP (*vec);
2163 for (i = 0; i < len; ++i)
2164 {
2165 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
1fd400ff
TT
2166 struct dwarf2_per_cu_data *cu = dw2_get_cu (cu_index);
2167
9291a0cd
TT
2168 dw2_instantiate_symtab (objfile, cu);
2169 }
2170 }
2171 }
2172}
2173
2174static void
2175dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2176 int kind, const char *name,
2177 domain_enum domain)
2178{
2179 dw2_do_expand_symtabs_matching (objfile, name);
2180}
2181
2182static void
2183dw2_print_stats (struct objfile *objfile)
2184{
2185 int i, count;
2186
2187 dw2_setup (objfile);
2188 count = 0;
1fd400ff
TT
2189 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2190 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2191 {
1fd400ff 2192 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2193
2194 if (!cu->v.quick->symtab)
2195 ++count;
2196 }
2197 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2198}
2199
2200static void
2201dw2_dump (struct objfile *objfile)
2202{
2203 /* Nothing worth printing. */
2204}
2205
2206static void
2207dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2208 struct section_offsets *delta)
2209{
2210 /* There's nothing to relocate here. */
2211}
2212
2213static void
2214dw2_expand_symtabs_for_function (struct objfile *objfile,
2215 const char *func_name)
2216{
2217 dw2_do_expand_symtabs_matching (objfile, func_name);
2218}
2219
2220static void
2221dw2_expand_all_symtabs (struct objfile *objfile)
2222{
2223 int i;
2224
2225 dw2_setup (objfile);
1fd400ff
TT
2226
2227 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2228 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2229 {
1fd400ff 2230 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2231
2232 dw2_instantiate_symtab (objfile, cu);
2233 }
2234}
2235
2236static void
2237dw2_expand_symtabs_with_filename (struct objfile *objfile,
2238 const char *filename)
2239{
2240 int i;
2241
2242 dw2_setup (objfile);
1fd400ff
TT
2243 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2244 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2245 {
2246 int j;
1fd400ff 2247 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2248
2249 if (cu->v.quick->symtab)
2250 continue;
2251
2252 dw2_require_line_header (objfile, cu);
2253 if (!cu->v.quick->lines)
2254 continue;
2255
2256 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2257 {
2258 const char *this_name = cu->v.quick->file_names[j];
2259 if (strcmp (this_name, filename) == 0)
2260 {
2261 dw2_instantiate_symtab (objfile, cu);
2262 break;
2263 }
2264 }
2265 }
2266}
2267
dd786858 2268static const char *
9291a0cd
TT
2269dw2_find_symbol_file (struct objfile *objfile, const char *name)
2270{
2271 struct dwarf2_per_cu_data *cu;
2272 offset_type *vec;
2273
2274 dw2_setup (objfile);
2275
2276 if (!dwarf2_per_objfile->index_table)
2277 return NULL;
2278
2279 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2280 name, &vec))
2281 return NULL;
2282
2283 /* Note that this just looks at the very first one named NAME -- but
2284 actually we are looking for a function. find_main_filename
2285 should be rewritten so that it doesn't require a custom hook. It
2286 could just use the ordinary symbol tables. */
2287 /* vec[0] is the length, which must always be >0. */
1fd400ff 2288 cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd
TT
2289
2290 dw2_require_line_header (objfile, cu);
2291 if (!cu->v.quick->lines)
2292 return NULL;
2293
dd786858 2294 return cu->v.quick->file_names[cu->v.quick->lines->num_file_names - 1];
9291a0cd
TT
2295}
2296
2297static void
2298dw2_map_ada_symtabs (struct objfile *objfile,
2299 int (*wild_match) (const char *, int, const char *),
2300 int (*is_name_suffix) (const char *),
2301 void (*callback) (struct objfile *,
2302 struct symtab *, void *),
2303 const char *name, int global,
2304 domain_enum namespace, int wild,
2305 void *data)
2306{
2307 /* For now, we don't support Ada, so this function can't be
2308 reached. */
2309 internal_error (__FILE__, __LINE__,
2310 _("map_ada_symtabs called via index method"));
2311}
2312
2313static void
2314dw2_expand_symtabs_matching (struct objfile *objfile,
2315 int (*file_matcher) (const char *, void *),
2316 int (*name_matcher) (const char *, void *),
2317 domain_enum kind,
2318 void *data)
2319{
2320 int i;
2321 offset_type iter;
2322
2323 dw2_setup (objfile);
2324 if (!dwarf2_per_objfile->index_table)
2325 return;
2326
1fd400ff
TT
2327 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2328 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2329 {
2330 int j;
1fd400ff 2331 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2332
2333 cu->v.quick->mark = 0;
2334 if (cu->v.quick->symtab)
2335 continue;
2336
2337 dw2_require_line_header (objfile, cu);
2338 if (!cu->v.quick->lines)
2339 continue;
2340
2341 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2342 {
2343 if (file_matcher (cu->v.quick->file_names[j], data))
2344 {
2345 cu->v.quick->mark = 1;
2346 break;
2347 }
2348 }
2349 }
2350
2351 for (iter = 0;
2352 iter < dwarf2_per_objfile->index_table->index_table_slots;
2353 ++iter)
2354 {
2355 offset_type idx = 2 * iter;
2356 const char *name;
2357 offset_type *vec, vec_len, vec_idx;
2358
2359 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2360 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2361 continue;
2362
2363 name = (dwarf2_per_objfile->index_table->constant_pool
2364 + dwarf2_per_objfile->index_table->index_table[idx]);
2365
2366 if (! (*name_matcher) (name, data))
2367 continue;
2368
2369 /* The name was matched, now expand corresponding CUs that were
2370 marked. */
2371 vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2372 + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2373 vec_len = MAYBE_SWAP (vec[0]);
2374 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2375 {
1fd400ff
TT
2376 struct dwarf2_per_cu_data *cu;
2377
2378 cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
9291a0cd
TT
2379 if (cu->v.quick->mark)
2380 dw2_instantiate_symtab (objfile, cu);
2381 }
2382 }
2383}
2384
2385static struct symtab *
2386dw2_find_pc_sect_symtab (struct objfile *objfile,
2387 struct minimal_symbol *msymbol,
2388 CORE_ADDR pc,
2389 struct obj_section *section,
2390 int warn_if_readin)
2391{
2392 struct dwarf2_per_cu_data *data;
2393
2394 dw2_setup (objfile);
2395
2396 if (!objfile->psymtabs_addrmap)
2397 return NULL;
2398
2399 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2400 if (!data)
2401 return NULL;
2402
2403 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2404 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2405 paddress (get_objfile_arch (objfile), pc));
2406
2407 return dw2_instantiate_symtab (objfile, data);
2408}
2409
2410static void
2411dw2_map_symbol_names (struct objfile *objfile,
2412 void (*fun) (const char *, void *),
2413 void *data)
2414{
2415 offset_type iter;
2416 dw2_setup (objfile);
2417
2418 if (!dwarf2_per_objfile->index_table)
2419 return;
2420
2421 for (iter = 0;
2422 iter < dwarf2_per_objfile->index_table->index_table_slots;
2423 ++iter)
2424 {
2425 offset_type idx = 2 * iter;
2426 const char *name;
2427 offset_type *vec, vec_len, vec_idx;
2428
2429 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2430 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2431 continue;
2432
2433 name = (dwarf2_per_objfile->index_table->constant_pool
2434 + dwarf2_per_objfile->index_table->index_table[idx]);
2435
2436 (*fun) (name, data);
2437 }
2438}
2439
2440static void
2441dw2_map_symbol_filenames (struct objfile *objfile,
2442 void (*fun) (const char *, const char *, void *),
2443 void *data)
2444{
2445 int i;
2446
2447 dw2_setup (objfile);
1fd400ff
TT
2448 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2449 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2450 {
2451 int j;
1fd400ff 2452 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2453
2454 if (cu->v.quick->symtab)
2455 continue;
2456
2457 dw2_require_line_header (objfile, cu);
2458 if (!cu->v.quick->lines)
2459 continue;
2460
2461 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2462 {
2463 const char *this_full_name = dw2_require_full_path (objfile, cu, j);
2464 (*fun) (cu->v.quick->file_names[j], this_full_name, data);
2465 }
2466 }
2467}
2468
2469static int
2470dw2_has_symbols (struct objfile *objfile)
2471{
2472 return 1;
2473}
2474
2475const struct quick_symbol_functions dwarf2_gdb_index_functions =
2476{
2477 dw2_has_symbols,
2478 dw2_find_last_source_symtab,
2479 dw2_forget_cached_source_info,
2480 dw2_lookup_symtab,
2481 dw2_lookup_symbol,
2482 dw2_pre_expand_symtabs_matching,
2483 dw2_print_stats,
2484 dw2_dump,
2485 dw2_relocate,
2486 dw2_expand_symtabs_for_function,
2487 dw2_expand_all_symtabs,
2488 dw2_expand_symtabs_with_filename,
2489 dw2_find_symbol_file,
2490 dw2_map_ada_symtabs,
2491 dw2_expand_symtabs_matching,
2492 dw2_find_pc_sect_symtab,
2493 dw2_map_symbol_names,
2494 dw2_map_symbol_filenames
2495};
2496
2497/* Initialize for reading DWARF for this objfile. Return 0 if this
2498 file will use psymtabs, or 1 if using the GNU index. */
2499
2500int
2501dwarf2_initialize_objfile (struct objfile *objfile)
2502{
2503 /* If we're about to read full symbols, don't bother with the
2504 indices. In this case we also don't care if some other debug
2505 format is making psymtabs, because they are all about to be
2506 expanded anyway. */
2507 if ((objfile->flags & OBJF_READNOW))
2508 {
2509 int i;
2510
2511 dwarf2_per_objfile->using_index = 1;
2512 create_all_comp_units (objfile);
1fd400ff 2513 create_debug_types_hash_table (objfile);
9291a0cd 2514
1fd400ff
TT
2515 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2516 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2517 {
1fd400ff 2518 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2519
2520 cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2521 struct dwarf2_per_cu_quick_data);
2522 }
2523
2524 /* Return 1 so that gdb sees the "quick" functions. However,
2525 these functions will be no-ops because we will have expanded
2526 all symtabs. */
2527 return 1;
2528 }
2529
2530 if (dwarf2_read_index (objfile))
2531 return 1;
2532
2533 dwarf2_build_psymtabs (objfile);
2534 return 0;
2535}
2536
2537\f
2538
dce234bc
PP
2539/* Build a partial symbol table. */
2540
2541void
f29dff0a 2542dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2543{
f29dff0a 2544 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2545 {
2546 init_psymbol_list (objfile, 1024);
2547 }
2548
d146bf1e 2549 dwarf2_build_psymtabs_hard (objfile);
c906108c 2550}
c906108c 2551
45452591
DE
2552/* Return TRUE if OFFSET is within CU_HEADER. */
2553
2554static inline int
2555offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2556{
2557 unsigned int bottom = cu_header->offset;
2558 unsigned int top = (cu_header->offset
2559 + cu_header->length
2560 + cu_header->initial_length_size);
9a619af0 2561
45452591
DE
2562 return (offset >= bottom && offset < top);
2563}
2564
93311388
DE
2565/* Read in the comp unit header information from the debug_info at info_ptr.
2566 NOTE: This leaves members offset, first_die_offset to be filled in
2567 by the caller. */
107d2387 2568
fe1b8b76 2569static gdb_byte *
107d2387 2570read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2571 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2572{
2573 int signed_addr;
891d2f0b 2574 unsigned int bytes_read;
c764a876
DE
2575
2576 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2577 cu_header->initial_length_size = bytes_read;
2578 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2579 info_ptr += bytes_read;
107d2387
AC
2580 cu_header->version = read_2_bytes (abfd, info_ptr);
2581 info_ptr += 2;
613e1657 2582 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2583 &bytes_read);
613e1657 2584 info_ptr += bytes_read;
107d2387
AC
2585 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2586 info_ptr += 1;
2587 signed_addr = bfd_get_sign_extend_vma (abfd);
2588 if (signed_addr < 0)
8e65ff28 2589 internal_error (__FILE__, __LINE__,
e2e0b3e5 2590 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2591 cu_header->signed_addr_p = signed_addr;
c764a876 2592
107d2387
AC
2593 return info_ptr;
2594}
2595
fe1b8b76
JB
2596static gdb_byte *
2597partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2598 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2599 bfd *abfd)
2600{
fe1b8b76 2601 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2602
2603 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2604
2dc7f7b3 2605 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2606 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2607 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2608 bfd_get_filename (abfd));
72bf9492 2609
dce234bc 2610 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2611 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2612 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2613 (long) header->abbrev_offset,
93311388 2614 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2615 bfd_get_filename (abfd));
2616
2617 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2618 > buffer + buffer_size)
8a3fe4f8
AC
2619 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2620 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2621 (long) header->length,
93311388 2622 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2623 bfd_get_filename (abfd));
2624
2625 return info_ptr;
2626}
2627
348e048f
DE
2628/* Read in the types comp unit header information from .debug_types entry at
2629 types_ptr. The result is a pointer to one past the end of the header. */
2630
2631static gdb_byte *
2632read_type_comp_unit_head (struct comp_unit_head *cu_header,
2633 ULONGEST *signature,
2634 gdb_byte *types_ptr, bfd *abfd)
2635{
348e048f
DE
2636 gdb_byte *initial_types_ptr = types_ptr;
2637
6e70227d 2638 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2639 &dwarf2_per_objfile->types);
348e048f
DE
2640 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2641
2642 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2643
2644 *signature = read_8_bytes (abfd, types_ptr);
2645 types_ptr += 8;
2646 types_ptr += cu_header->offset_size;
2647 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2648
2649 return types_ptr;
2650}
2651
aaa75496
JB
2652/* Allocate a new partial symtab for file named NAME and mark this new
2653 partial symtab as being an include of PST. */
2654
2655static void
2656dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2657 struct objfile *objfile)
2658{
2659 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2660
2661 subpst->section_offsets = pst->section_offsets;
2662 subpst->textlow = 0;
2663 subpst->texthigh = 0;
2664
2665 subpst->dependencies = (struct partial_symtab **)
2666 obstack_alloc (&objfile->objfile_obstack,
2667 sizeof (struct partial_symtab *));
2668 subpst->dependencies[0] = pst;
2669 subpst->number_of_dependencies = 1;
2670
2671 subpst->globals_offset = 0;
2672 subpst->n_global_syms = 0;
2673 subpst->statics_offset = 0;
2674 subpst->n_static_syms = 0;
2675 subpst->symtab = NULL;
2676 subpst->read_symtab = pst->read_symtab;
2677 subpst->readin = 0;
2678
2679 /* No private part is necessary for include psymtabs. This property
2680 can be used to differentiate between such include psymtabs and
10b3939b 2681 the regular ones. */
58a9656e 2682 subpst->read_symtab_private = NULL;
aaa75496
JB
2683}
2684
2685/* Read the Line Number Program data and extract the list of files
2686 included by the source file represented by PST. Build an include
d85a05f0 2687 partial symtab for each of these included files. */
aaa75496
JB
2688
2689static void
2690dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2691 struct die_info *die,
aaa75496
JB
2692 struct partial_symtab *pst)
2693{
2694 struct objfile *objfile = cu->objfile;
2695 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2696 struct line_header *lh = NULL;
2697 struct attribute *attr;
aaa75496 2698
d85a05f0
DJ
2699 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2700 if (attr)
2701 {
2702 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2703
d85a05f0
DJ
2704 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2705 }
aaa75496
JB
2706 if (lh == NULL)
2707 return; /* No linetable, so no includes. */
2708
2709 dwarf_decode_lines (lh, NULL, abfd, cu, pst);
2710
2711 free_line_header (lh);
2712}
2713
348e048f
DE
2714static hashval_t
2715hash_type_signature (const void *item)
2716{
2717 const struct signatured_type *type_sig = item;
9a619af0 2718
348e048f
DE
2719 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2720 return type_sig->signature;
2721}
2722
2723static int
2724eq_type_signature (const void *item_lhs, const void *item_rhs)
2725{
2726 const struct signatured_type *lhs = item_lhs;
2727 const struct signatured_type *rhs = item_rhs;
9a619af0 2728
348e048f
DE
2729 return lhs->signature == rhs->signature;
2730}
2731
1fd400ff
TT
2732/* Allocate a hash table for signatured types. */
2733
2734static htab_t
2735allocate_signatured_type_hash_table (struct objfile *objfile)
2736{
2737 return htab_create_alloc_ex (41,
2738 hash_type_signature,
2739 eq_type_signature,
2740 NULL,
2741 &objfile->objfile_obstack,
2742 hashtab_obstack_allocate,
2743 dummy_obstack_deallocate);
2744}
2745
2746/* A helper function to add a signatured type CU to a list. */
2747
2748static int
2749add_signatured_type_cu_to_list (void **slot, void *datum)
2750{
2751 struct signatured_type *sigt = *slot;
2752 struct dwarf2_per_cu_data ***datap = datum;
2753
2754 **datap = &sigt->per_cu;
2755 ++*datap;
2756
2757 return 1;
2758}
2759
348e048f
DE
2760/* Create the hash table of all entries in the .debug_types section.
2761 The result is zero if there is an error (e.g. missing .debug_types section),
2762 otherwise non-zero. */
2763
2764static int
2765create_debug_types_hash_table (struct objfile *objfile)
2766{
be391dca 2767 gdb_byte *info_ptr;
348e048f 2768 htab_t types_htab;
1fd400ff 2769 struct dwarf2_per_cu_data **iter;
348e048f 2770
be391dca
TT
2771 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2772 info_ptr = dwarf2_per_objfile->types.buffer;
2773
348e048f
DE
2774 if (info_ptr == NULL)
2775 {
2776 dwarf2_per_objfile->signatured_types = NULL;
2777 return 0;
2778 }
2779
1fd400ff 2780 types_htab = allocate_signatured_type_hash_table (objfile);
348e048f
DE
2781
2782 if (dwarf2_die_debug)
2783 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2784
2785 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2786 {
2787 unsigned int offset;
2788 unsigned int offset_size;
2789 unsigned int type_offset;
2790 unsigned int length, initial_length_size;
2791 unsigned short version;
2792 ULONGEST signature;
2793 struct signatured_type *type_sig;
2794 void **slot;
2795 gdb_byte *ptr = info_ptr;
2796
2797 offset = ptr - dwarf2_per_objfile->types.buffer;
2798
2799 /* We need to read the type's signature in order to build the hash
2800 table, but we don't need to read anything else just yet. */
2801
2802 /* Sanity check to ensure entire cu is present. */
2803 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2804 if (ptr + length + initial_length_size
2805 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2806 {
2807 complaint (&symfile_complaints,
2808 _("debug type entry runs off end of `.debug_types' section, ignored"));
2809 break;
2810 }
2811
2812 offset_size = initial_length_size == 4 ? 4 : 8;
2813 ptr += initial_length_size;
2814 version = bfd_get_16 (objfile->obfd, ptr);
2815 ptr += 2;
2816 ptr += offset_size; /* abbrev offset */
2817 ptr += 1; /* address size */
2818 signature = bfd_get_64 (objfile->obfd, ptr);
2819 ptr += 8;
2820 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2821
2822 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2823 memset (type_sig, 0, sizeof (*type_sig));
2824 type_sig->signature = signature;
2825 type_sig->offset = offset;
2826 type_sig->type_offset = type_offset;
ca1f3406 2827 type_sig->per_cu.objfile = objfile;
1fd400ff 2828 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
2829
2830 slot = htab_find_slot (types_htab, type_sig, INSERT);
2831 gdb_assert (slot != NULL);
2832 *slot = type_sig;
2833
2834 if (dwarf2_die_debug)
2835 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2836 offset, phex (signature, sizeof (signature)));
2837
2838 info_ptr = info_ptr + initial_length_size + length;
2839 }
2840
2841 dwarf2_per_objfile->signatured_types = types_htab;
2842
1fd400ff
TT
2843 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2844 dwarf2_per_objfile->type_comp_units
2845 = obstack_alloc (&objfile->objfile_obstack,
2846 dwarf2_per_objfile->n_type_comp_units
2847 * sizeof (struct dwarf2_per_cu_data *));
2848 iter = &dwarf2_per_objfile->type_comp_units[0];
2849 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2850 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2851 == dwarf2_per_objfile->n_type_comp_units);
2852
348e048f
DE
2853 return 1;
2854}
2855
2856/* Lookup a signature based type.
2857 Returns NULL if SIG is not present in the table. */
2858
2859static struct signatured_type *
2860lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2861{
2862 struct signatured_type find_entry, *entry;
2863
2864 if (dwarf2_per_objfile->signatured_types == NULL)
2865 {
2866 complaint (&symfile_complaints,
2867 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2868 return 0;
2869 }
2870
2871 find_entry.signature = sig;
2872 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2873 return entry;
2874}
2875
d85a05f0
DJ
2876/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2877
2878static void
2879init_cu_die_reader (struct die_reader_specs *reader,
2880 struct dwarf2_cu *cu)
2881{
2882 reader->abfd = cu->objfile->obfd;
2883 reader->cu = cu;
2884 if (cu->per_cu->from_debug_types)
be391dca
TT
2885 {
2886 gdb_assert (dwarf2_per_objfile->types.readin);
2887 reader->buffer = dwarf2_per_objfile->types.buffer;
2888 }
d85a05f0 2889 else
be391dca
TT
2890 {
2891 gdb_assert (dwarf2_per_objfile->info.readin);
2892 reader->buffer = dwarf2_per_objfile->info.buffer;
2893 }
d85a05f0
DJ
2894}
2895
2896/* Find the base address of the compilation unit for range lists and
2897 location lists. It will normally be specified by DW_AT_low_pc.
2898 In DWARF-3 draft 4, the base address could be overridden by
2899 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2900 compilation units with discontinuous ranges. */
2901
2902static void
2903dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2904{
2905 struct attribute *attr;
2906
2907 cu->base_known = 0;
2908 cu->base_address = 0;
2909
2910 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2911 if (attr)
2912 {
2913 cu->base_address = DW_ADDR (attr);
2914 cu->base_known = 1;
2915 }
2916 else
2917 {
2918 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2919 if (attr)
2920 {
2921 cu->base_address = DW_ADDR (attr);
2922 cu->base_known = 1;
2923 }
2924 }
2925}
2926
348e048f
DE
2927/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2928 to combine the common parts.
93311388 2929 Process a compilation unit for a psymtab.
348e048f
DE
2930 BUFFER is a pointer to the beginning of the dwarf section buffer,
2931 either .debug_info or debug_types.
93311388
DE
2932 INFO_PTR is a pointer to the start of the CU.
2933 Returns a pointer to the next CU. */
aaa75496 2934
93311388
DE
2935static gdb_byte *
2936process_psymtab_comp_unit (struct objfile *objfile,
2937 struct dwarf2_per_cu_data *this_cu,
2938 gdb_byte *buffer, gdb_byte *info_ptr,
2939 unsigned int buffer_size)
c906108c 2940{
c906108c 2941 bfd *abfd = objfile->obfd;
93311388 2942 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 2943 struct die_info *comp_unit_die;
c906108c 2944 struct partial_symtab *pst;
5734ee8b 2945 CORE_ADDR baseaddr;
93311388
DE
2946 struct cleanup *back_to_inner;
2947 struct dwarf2_cu cu;
d85a05f0
DJ
2948 int has_children, has_pc_info;
2949 struct attribute *attr;
d85a05f0
DJ
2950 CORE_ADDR best_lowpc = 0, best_highpc = 0;
2951 struct die_reader_specs reader_specs;
c906108c 2952
93311388
DE
2953 memset (&cu, 0, sizeof (cu));
2954 cu.objfile = objfile;
2955 obstack_init (&cu.comp_unit_obstack);
c906108c 2956
93311388 2957 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 2958
93311388
DE
2959 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2960 buffer, buffer_size,
2961 abfd);
10b3939b 2962
93311388
DE
2963 /* Complete the cu_header. */
2964 cu.header.offset = beg_of_comp_unit - buffer;
2965 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 2966
93311388 2967 cu.list_in_scope = &file_symbols;
af703f96 2968
328c9494
DJ
2969 /* If this compilation unit was already read in, free the
2970 cached copy in order to read it in again. This is
2971 necessary because we skipped some symbols when we first
2972 read in the compilation unit (see load_partial_dies).
2973 This problem could be avoided, but the benefit is
2974 unclear. */
2975 if (this_cu->cu != NULL)
2976 free_one_cached_comp_unit (this_cu->cu);
2977
2978 /* Note that this is a pointer to our stack frame, being
2979 added to a global data structure. It will be cleaned up
2980 in free_stack_comp_unit when we finish with this
2981 compilation unit. */
2982 this_cu->cu = &cu;
d85a05f0
DJ
2983 cu.per_cu = this_cu;
2984
93311388
DE
2985 /* Read the abbrevs for this compilation unit into a table. */
2986 dwarf2_read_abbrevs (abfd, &cu);
2987 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 2988
93311388 2989 /* Read the compilation unit die. */
348e048f
DE
2990 if (this_cu->from_debug_types)
2991 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
2992 init_cu_die_reader (&reader_specs, &cu);
2993 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2994 &has_children);
93311388 2995
348e048f
DE
2996 if (this_cu->from_debug_types)
2997 {
2998 /* offset,length haven't been set yet for type units. */
2999 this_cu->offset = cu.header.offset;
3000 this_cu->length = cu.header.length + cu.header.initial_length_size;
3001 }
d85a05f0 3002 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3003 {
93311388
DE
3004 info_ptr = (beg_of_comp_unit + cu.header.length
3005 + cu.header.initial_length_size);
3006 do_cleanups (back_to_inner);
3007 return info_ptr;
3008 }
72bf9492 3009
93311388 3010 /* Set the language we're debugging. */
d85a05f0
DJ
3011 attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3012 if (attr)
3013 set_cu_language (DW_UNSND (attr), &cu);
3014 else
3015 set_cu_language (language_minimal, &cu);
c906108c 3016
93311388 3017 /* Allocate a new partial symbol table structure. */
d85a05f0 3018 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3019 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3020 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3021 /* TEXTLOW and TEXTHIGH are set below. */
3022 0,
3023 objfile->global_psymbols.next,
3024 objfile->static_psymbols.next);
72bf9492 3025
d85a05f0
DJ
3026 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3027 if (attr != NULL)
3028 pst->dirname = DW_STRING (attr);
72bf9492 3029
e38df1d0 3030 pst->read_symtab_private = this_cu;
72bf9492 3031
93311388 3032 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3033
93311388
DE
3034 /* Store the function that reads in the rest of the symbol table */
3035 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3036
9291a0cd 3037 this_cu->v.psymtab = pst;
c906108c 3038
d85a05f0
DJ
3039 dwarf2_find_base_address (comp_unit_die, &cu);
3040
93311388
DE
3041 /* Possibly set the default values of LOWPC and HIGHPC from
3042 `DW_AT_ranges'. */
d85a05f0
DJ
3043 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3044 &best_highpc, &cu, pst);
3045 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3046 /* Store the contiguous range if it is not empty; it can be empty for
3047 CUs with no code. */
3048 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3049 best_lowpc + baseaddr,
3050 best_highpc + baseaddr - 1, pst);
93311388
DE
3051
3052 /* Check if comp unit has_children.
3053 If so, read the rest of the partial symbols from this comp unit.
3054 If not, there's no more debug_info for this comp unit. */
d85a05f0 3055 if (has_children)
93311388
DE
3056 {
3057 struct partial_die_info *first_die;
3058 CORE_ADDR lowpc, highpc;
31ffec48 3059
93311388
DE
3060 lowpc = ((CORE_ADDR) -1);
3061 highpc = ((CORE_ADDR) 0);
c906108c 3062
93311388 3063 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3064
93311388 3065 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3066 ! has_pc_info, &cu);
57c22c6c 3067
93311388
DE
3068 /* If we didn't find a lowpc, set it to highpc to avoid
3069 complaints from `maint check'. */
3070 if (lowpc == ((CORE_ADDR) -1))
3071 lowpc = highpc;
10b3939b 3072
93311388
DE
3073 /* If the compilation unit didn't have an explicit address range,
3074 then use the information extracted from its child dies. */
d85a05f0 3075 if (! has_pc_info)
93311388 3076 {
d85a05f0
DJ
3077 best_lowpc = lowpc;
3078 best_highpc = highpc;
93311388
DE
3079 }
3080 }
d85a05f0
DJ
3081 pst->textlow = best_lowpc + baseaddr;
3082 pst->texthigh = best_highpc + baseaddr;
c906108c 3083
93311388
DE
3084 pst->n_global_syms = objfile->global_psymbols.next -
3085 (objfile->global_psymbols.list + pst->globals_offset);
3086 pst->n_static_syms = objfile->static_psymbols.next -
3087 (objfile->static_psymbols.list + pst->statics_offset);
3088 sort_pst_symbols (pst);
c906108c 3089
93311388
DE
3090 info_ptr = (beg_of_comp_unit + cu.header.length
3091 + cu.header.initial_length_size);
ae038cb0 3092
348e048f
DE
3093 if (this_cu->from_debug_types)
3094 {
3095 /* It's not clear we want to do anything with stmt lists here.
3096 Waiting to see what gcc ultimately does. */
3097 }
d85a05f0 3098 else
93311388
DE
3099 {
3100 /* Get the list of files included in the current compilation unit,
3101 and build a psymtab for each of them. */
d85a05f0 3102 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3103 }
ae038cb0 3104
93311388 3105 do_cleanups (back_to_inner);
ae038cb0 3106
93311388
DE
3107 return info_ptr;
3108}
ff013f42 3109
348e048f
DE
3110/* Traversal function for htab_traverse_noresize.
3111 Process one .debug_types comp-unit. */
3112
3113static int
3114process_type_comp_unit (void **slot, void *info)
3115{
3116 struct signatured_type *entry = (struct signatured_type *) *slot;
3117 struct objfile *objfile = (struct objfile *) info;
3118 struct dwarf2_per_cu_data *this_cu;
3119
3120 this_cu = &entry->per_cu;
348e048f 3121
be391dca 3122 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3123 process_psymtab_comp_unit (objfile, this_cu,
3124 dwarf2_per_objfile->types.buffer,
3125 dwarf2_per_objfile->types.buffer + entry->offset,
3126 dwarf2_per_objfile->types.size);
3127
3128 return 1;
3129}
3130
3131/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3132 Build partial symbol tables for the .debug_types comp-units. */
3133
3134static void
3135build_type_psymtabs (struct objfile *objfile)
3136{
3137 if (! create_debug_types_hash_table (objfile))
3138 return;
3139
3140 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3141 process_type_comp_unit, objfile);
3142}
3143
60606b2c
TT
3144/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3145
3146static void
3147psymtabs_addrmap_cleanup (void *o)
3148{
3149 struct objfile *objfile = o;
ec61707d 3150
60606b2c
TT
3151 objfile->psymtabs_addrmap = NULL;
3152}
3153
93311388
DE
3154/* Build the partial symbol table by doing a quick pass through the
3155 .debug_info and .debug_abbrev sections. */
72bf9492 3156
93311388 3157static void
c67a9c90 3158dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3159{
93311388 3160 gdb_byte *info_ptr;
60606b2c
TT
3161 struct cleanup *back_to, *addrmap_cleanup;
3162 struct obstack temp_obstack;
93311388 3163
be391dca 3164 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3165 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3166
93311388
DE
3167 /* Any cached compilation units will be linked by the per-objfile
3168 read_in_chain. Make sure to free them when we're done. */
3169 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3170
348e048f
DE
3171 build_type_psymtabs (objfile);
3172
93311388 3173 create_all_comp_units (objfile);
c906108c 3174
60606b2c
TT
3175 /* Create a temporary address map on a temporary obstack. We later
3176 copy this to the final obstack. */
3177 obstack_init (&temp_obstack);
3178 make_cleanup_obstack_free (&temp_obstack);
3179 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3180 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3181
93311388
DE
3182 /* Since the objects we're extracting from .debug_info vary in
3183 length, only the individual functions to extract them (like
3184 read_comp_unit_head and load_partial_die) can really know whether
3185 the buffer is large enough to hold another complete object.
c906108c 3186
93311388
DE
3187 At the moment, they don't actually check that. If .debug_info
3188 holds just one extra byte after the last compilation unit's dies,
3189 then read_comp_unit_head will happily read off the end of the
3190 buffer. read_partial_die is similarly casual. Those functions
3191 should be fixed.
c906108c 3192
93311388
DE
3193 For this loop condition, simply checking whether there's any data
3194 left at all should be sufficient. */
c906108c 3195
93311388
DE
3196 while (info_ptr < (dwarf2_per_objfile->info.buffer
3197 + dwarf2_per_objfile->info.size))
3198 {
3199 struct dwarf2_per_cu_data *this_cu;
dd373385 3200
93311388
DE
3201 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3202 objfile);
aaa75496 3203
93311388
DE
3204 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3205 dwarf2_per_objfile->info.buffer,
3206 info_ptr,
3207 dwarf2_per_objfile->info.size);
c906108c 3208 }
ff013f42
JK
3209
3210 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3211 &objfile->objfile_obstack);
60606b2c 3212 discard_cleanups (addrmap_cleanup);
ff013f42 3213
ae038cb0
DJ
3214 do_cleanups (back_to);
3215}
3216
93311388 3217/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3218
3219static void
93311388
DE
3220load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3221 struct objfile *objfile)
ae038cb0
DJ
3222{
3223 bfd *abfd = objfile->obfd;
fe1b8b76 3224 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3225 struct die_info *comp_unit_die;
ae038cb0 3226 struct dwarf2_cu *cu;
ae038cb0 3227 struct cleanup *back_to;
d85a05f0
DJ
3228 struct attribute *attr;
3229 int has_children;
3230 struct die_reader_specs reader_specs;
ae038cb0 3231
348e048f
DE
3232 gdb_assert (! this_cu->from_debug_types);
3233
be391dca 3234 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3235 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3236 beg_of_comp_unit = info_ptr;
3237
93311388 3238 cu = alloc_one_comp_unit (objfile);
ae038cb0 3239
93311388 3240 /* ??? Missing cleanup for CU? */
ae038cb0 3241
328c9494
DJ
3242 /* Link this compilation unit into the compilation unit tree. */
3243 this_cu->cu = cu;
3244 cu->per_cu = this_cu;
3245 cu->type_hash = this_cu->type_hash;
3246
93311388
DE
3247 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3248 dwarf2_per_objfile->info.buffer,
3249 dwarf2_per_objfile->info.size,
3250 abfd);
ae038cb0
DJ
3251
3252 /* Complete the cu_header. */
93311388 3253 cu->header.offset = this_cu->offset;
d00adf39 3254 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
ae038cb0
DJ
3255
3256 /* Read the abbrevs for this compilation unit into a table. */
3257 dwarf2_read_abbrevs (abfd, cu);
3258 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
3259
3260 /* Read the compilation unit die. */
d85a05f0
DJ
3261 init_cu_die_reader (&reader_specs, cu);
3262 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3263 &has_children);
ae038cb0
DJ
3264
3265 /* Set the language we're debugging. */
d85a05f0
DJ
3266 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3267 if (attr)
3268 set_cu_language (DW_UNSND (attr), cu);
3269 else
3270 set_cu_language (language_minimal, cu);
ae038cb0 3271
ae038cb0
DJ
3272 /* Check if comp unit has_children.
3273 If so, read the rest of the partial symbols from this comp unit.
3274 If not, there's no more debug_info for this comp unit. */
d85a05f0 3275 if (has_children)
93311388 3276 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0
DJ
3277
3278 do_cleanups (back_to);
3279}
3280
3281/* Create a list of all compilation units in OBJFILE. We do this only
3282 if an inter-comp-unit reference is found; presumably if there is one,
3283 there will be many, and one will occur early in the .debug_info section.
3284 So there's no point in building this list incrementally. */
3285
3286static void
3287create_all_comp_units (struct objfile *objfile)
3288{
3289 int n_allocated;
3290 int n_comp_units;
3291 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3292 gdb_byte *info_ptr;
3293
3294 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3295 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3296
3297 n_comp_units = 0;
3298 n_allocated = 10;
3299 all_comp_units = xmalloc (n_allocated
3300 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3301
dce234bc 3302 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 3303 {
c764a876 3304 unsigned int length, initial_length_size;
ae038cb0 3305 struct dwarf2_per_cu_data *this_cu;
c764a876 3306 unsigned int offset;
ae038cb0 3307
dce234bc 3308 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3309
3310 /* Read just enough information to find out where the next
3311 compilation unit is. */
c764a876
DE
3312 length = read_initial_length (objfile->obfd, info_ptr,
3313 &initial_length_size);
ae038cb0
DJ
3314
3315 /* Save the compilation unit for later lookup. */
3316 this_cu = obstack_alloc (&objfile->objfile_obstack,
3317 sizeof (struct dwarf2_per_cu_data));
3318 memset (this_cu, 0, sizeof (*this_cu));
3319 this_cu->offset = offset;
c764a876 3320 this_cu->length = length + initial_length_size;
9291a0cd 3321 this_cu->objfile = objfile;
ae038cb0
DJ
3322
3323 if (n_comp_units == n_allocated)
3324 {
3325 n_allocated *= 2;
3326 all_comp_units = xrealloc (all_comp_units,
3327 n_allocated
3328 * sizeof (struct dwarf2_per_cu_data *));
3329 }
3330 all_comp_units[n_comp_units++] = this_cu;
3331
3332 info_ptr = info_ptr + this_cu->length;
3333 }
3334
3335 dwarf2_per_objfile->all_comp_units
3336 = obstack_alloc (&objfile->objfile_obstack,
3337 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3338 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3339 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3340 xfree (all_comp_units);
3341 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3342}
3343
5734ee8b
DJ
3344/* Process all loaded DIEs for compilation unit CU, starting at
3345 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3346 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3347 DW_AT_ranges). If NEED_PC is set, then this function will set
3348 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3349 and record the covered ranges in the addrmap. */
c906108c 3350
72bf9492
DJ
3351static void
3352scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3353 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3354{
72bf9492 3355 struct partial_die_info *pdi;
c906108c 3356
91c24f0a
DC
3357 /* Now, march along the PDI's, descending into ones which have
3358 interesting children but skipping the children of the other ones,
3359 until we reach the end of the compilation unit. */
c906108c 3360
72bf9492 3361 pdi = first_die;
91c24f0a 3362
72bf9492
DJ
3363 while (pdi != NULL)
3364 {
3365 fixup_partial_die (pdi, cu);
c906108c 3366
f55ee35c 3367 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3368 children, so we need to look at them. Ditto for anonymous
3369 enums. */
933c6fe4 3370
72bf9492 3371 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3372 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3373 {
72bf9492 3374 switch (pdi->tag)
c906108c
SS
3375 {
3376 case DW_TAG_subprogram:
5734ee8b 3377 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c
SS
3378 break;
3379 case DW_TAG_variable:
3380 case DW_TAG_typedef:
91c24f0a 3381 case DW_TAG_union_type:
72bf9492 3382 if (!pdi->is_declaration)
63d06c5c 3383 {
72bf9492 3384 add_partial_symbol (pdi, cu);
63d06c5c
DC
3385 }
3386 break;
c906108c 3387 case DW_TAG_class_type:
680b30c7 3388 case DW_TAG_interface_type:
c906108c 3389 case DW_TAG_structure_type:
72bf9492 3390 if (!pdi->is_declaration)
c906108c 3391 {
72bf9492 3392 add_partial_symbol (pdi, cu);
c906108c
SS
3393 }
3394 break;
91c24f0a 3395 case DW_TAG_enumeration_type:
72bf9492
DJ
3396 if (!pdi->is_declaration)
3397 add_partial_enumeration (pdi, cu);
c906108c
SS
3398 break;
3399 case DW_TAG_base_type:
a02abb62 3400 case DW_TAG_subrange_type:
c906108c 3401 /* File scope base type definitions are added to the partial
c5aa993b 3402 symbol table. */
72bf9492 3403 add_partial_symbol (pdi, cu);
c906108c 3404 break;
d9fa45fe 3405 case DW_TAG_namespace:
5734ee8b 3406 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3407 break;
5d7cb8df
JK
3408 case DW_TAG_module:
3409 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3410 break;
c906108c
SS
3411 default:
3412 break;
3413 }
3414 }
3415
72bf9492
DJ
3416 /* If the die has a sibling, skip to the sibling. */
3417
3418 pdi = pdi->die_sibling;
3419 }
3420}
3421
3422/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3423
72bf9492 3424 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3425 name is concatenated with "::" and the partial DIE's name. For
3426 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3427 Enumerators are an exception; they use the scope of their parent
3428 enumeration type, i.e. the name of the enumeration type is not
3429 prepended to the enumerator.
91c24f0a 3430
72bf9492
DJ
3431 There are two complexities. One is DW_AT_specification; in this
3432 case "parent" means the parent of the target of the specification,
3433 instead of the direct parent of the DIE. The other is compilers
3434 which do not emit DW_TAG_namespace; in this case we try to guess
3435 the fully qualified name of structure types from their members'
3436 linkage names. This must be done using the DIE's children rather
3437 than the children of any DW_AT_specification target. We only need
3438 to do this for structures at the top level, i.e. if the target of
3439 any DW_AT_specification (if any; otherwise the DIE itself) does not
3440 have a parent. */
3441
3442/* Compute the scope prefix associated with PDI's parent, in
3443 compilation unit CU. The result will be allocated on CU's
3444 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3445 field. NULL is returned if no prefix is necessary. */
3446static char *
3447partial_die_parent_scope (struct partial_die_info *pdi,
3448 struct dwarf2_cu *cu)
3449{
3450 char *grandparent_scope;
3451 struct partial_die_info *parent, *real_pdi;
91c24f0a 3452
72bf9492
DJ
3453 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3454 then this means the parent of the specification DIE. */
3455
3456 real_pdi = pdi;
72bf9492 3457 while (real_pdi->has_specification)
10b3939b 3458 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3459
3460 parent = real_pdi->die_parent;
3461 if (parent == NULL)
3462 return NULL;
3463
3464 if (parent->scope_set)
3465 return parent->scope;
3466
3467 fixup_partial_die (parent, cu);
3468
10b3939b 3469 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3470
acebe513
UW
3471 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3472 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3473 Work around this problem here. */
3474 if (cu->language == language_cplus
6e70227d 3475 && parent->tag == DW_TAG_namespace
acebe513
UW
3476 && strcmp (parent->name, "::") == 0
3477 && grandparent_scope == NULL)
3478 {
3479 parent->scope = NULL;
3480 parent->scope_set = 1;
3481 return NULL;
3482 }
3483
72bf9492 3484 if (parent->tag == DW_TAG_namespace
f55ee35c 3485 || parent->tag == DW_TAG_module
72bf9492
DJ
3486 || parent->tag == DW_TAG_structure_type
3487 || parent->tag == DW_TAG_class_type
680b30c7 3488 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3489 || parent->tag == DW_TAG_union_type
3490 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3491 {
3492 if (grandparent_scope == NULL)
3493 parent->scope = parent->name;
3494 else
987504bb 3495 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
f55ee35c 3496 parent->name, 0, cu);
72bf9492 3497 }
ceeb3d5a 3498 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3499 /* Enumerators should not get the name of the enumeration as a prefix. */
3500 parent->scope = grandparent_scope;
3501 else
3502 {
3503 /* FIXME drow/2004-04-01: What should we be doing with
3504 function-local names? For partial symbols, we should probably be
3505 ignoring them. */
3506 complaint (&symfile_complaints,
e2e0b3e5 3507 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3508 parent->tag, pdi->offset);
3509 parent->scope = grandparent_scope;
c906108c
SS
3510 }
3511
72bf9492
DJ
3512 parent->scope_set = 1;
3513 return parent->scope;
3514}
3515
3516/* Return the fully scoped name associated with PDI, from compilation unit
3517 CU. The result will be allocated with malloc. */
3518static char *
3519partial_die_full_name (struct partial_die_info *pdi,
3520 struct dwarf2_cu *cu)
3521{
3522 char *parent_scope;
3523
3524 parent_scope = partial_die_parent_scope (pdi, cu);
3525 if (parent_scope == NULL)
3526 return NULL;
3527 else
f55ee35c 3528 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3529}
3530
3531static void
72bf9492 3532add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3533{
e7c27a73 3534 struct objfile *objfile = cu->objfile;
c906108c 3535 CORE_ADDR addr = 0;
decbce07 3536 char *actual_name = NULL;
5c4e30ca 3537 const struct partial_symbol *psym = NULL;
e142c38c 3538 CORE_ADDR baseaddr;
72bf9492 3539 int built_actual_name = 0;
e142c38c
DJ
3540
3541 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3542
94af9270
KS
3543 actual_name = partial_die_full_name (pdi, cu);
3544 if (actual_name)
3545 built_actual_name = 1;
63d06c5c 3546
72bf9492
DJ
3547 if (actual_name == NULL)
3548 actual_name = pdi->name;
3549
c906108c
SS
3550 switch (pdi->tag)
3551 {
3552 case DW_TAG_subprogram:
2cfa0c8d 3553 if (pdi->is_external || cu->language == language_ada)
c906108c 3554 {
2cfa0c8d
JB
3555 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3556 of the global scope. But in Ada, we want to be able to access
3557 nested procedures globally. So all Ada subprograms are stored
3558 in the global scope. */
38d518c9 3559 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3560 mst_text, objfile); */
38d518c9 3561 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3562 built_actual_name,
5c4e30ca
DC
3563 VAR_DOMAIN, LOC_BLOCK,
3564 &objfile->global_psymbols,
3565 0, pdi->lowpc + baseaddr,
e142c38c 3566 cu->language, objfile);
c906108c
SS
3567 }
3568 else
3569 {
38d518c9 3570 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3571 mst_file_text, objfile); */
38d518c9 3572 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3573 built_actual_name,
5c4e30ca
DC
3574 VAR_DOMAIN, LOC_BLOCK,
3575 &objfile->static_psymbols,
3576 0, pdi->lowpc + baseaddr,
e142c38c 3577 cu->language, objfile);
c906108c
SS
3578 }
3579 break;
3580 case DW_TAG_variable:
caac4577
JG
3581 if (pdi->locdesc)
3582 addr = decode_locdesc (pdi->locdesc, cu);
3583
3584 if (pdi->locdesc
3585 && addr == 0
3586 && !dwarf2_per_objfile->has_section_at_zero)
3587 {
3588 /* A global or static variable may also have been stripped
3589 out by the linker if unused, in which case its address
3590 will be nullified; do not add such variables into partial
3591 symbol table then. */
3592 }
3593 else if (pdi->is_external)
c906108c
SS
3594 {
3595 /* Global Variable.
3596 Don't enter into the minimal symbol tables as there is
3597 a minimal symbol table entry from the ELF symbols already.
3598 Enter into partial symbol table if it has a location
3599 descriptor or a type.
3600 If the location descriptor is missing, new_symbol will create
3601 a LOC_UNRESOLVED symbol, the address of the variable will then
3602 be determined from the minimal symbol table whenever the variable
3603 is referenced.
3604 The address for the partial symbol table entry is not
3605 used by GDB, but it comes in handy for debugging partial symbol
3606 table building. */
3607
c906108c 3608 if (pdi->locdesc || pdi->has_type)
38d518c9 3609 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3610 built_actual_name,
5c4e30ca
DC
3611 VAR_DOMAIN, LOC_STATIC,
3612 &objfile->global_psymbols,
3613 0, addr + baseaddr,
e142c38c 3614 cu->language, objfile);
c906108c
SS
3615 }
3616 else
3617 {
3618 /* Static Variable. Skip symbols without location descriptors. */
3619 if (pdi->locdesc == NULL)
decbce07
MS
3620 {
3621 if (built_actual_name)
3622 xfree (actual_name);
3623 return;
3624 }
38d518c9 3625 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3626 mst_file_data, objfile); */
38d518c9 3627 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3628 built_actual_name,
5c4e30ca
DC
3629 VAR_DOMAIN, LOC_STATIC,
3630 &objfile->static_psymbols,
3631 0, addr + baseaddr,
e142c38c 3632 cu->language, objfile);
c906108c
SS
3633 }
3634 break;
3635 case DW_TAG_typedef:
3636 case DW_TAG_base_type:
a02abb62 3637 case DW_TAG_subrange_type:
38d518c9 3638 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3639 built_actual_name,
176620f1 3640 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3641 &objfile->static_psymbols,
e142c38c 3642 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3643 break;
72bf9492
DJ
3644 case DW_TAG_namespace:
3645 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3646 built_actual_name,
72bf9492
DJ
3647 VAR_DOMAIN, LOC_TYPEDEF,
3648 &objfile->global_psymbols,
3649 0, (CORE_ADDR) 0, cu->language, objfile);
3650 break;
c906108c 3651 case DW_TAG_class_type:
680b30c7 3652 case DW_TAG_interface_type:
c906108c
SS
3653 case DW_TAG_structure_type:
3654 case DW_TAG_union_type:
3655 case DW_TAG_enumeration_type:
fa4028e9
JB
3656 /* Skip external references. The DWARF standard says in the section
3657 about "Structure, Union, and Class Type Entries": "An incomplete
3658 structure, union or class type is represented by a structure,
3659 union or class entry that does not have a byte size attribute
3660 and that has a DW_AT_declaration attribute." */
3661 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3662 {
3663 if (built_actual_name)
3664 xfree (actual_name);
3665 return;
3666 }
fa4028e9 3667
63d06c5c
DC
3668 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3669 static vs. global. */
38d518c9 3670 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3671 built_actual_name,
176620f1 3672 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3673 (cu->language == language_cplus
3674 || cu->language == language_java)
63d06c5c
DC
3675 ? &objfile->global_psymbols
3676 : &objfile->static_psymbols,
e142c38c 3677 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3678
c906108c
SS
3679 break;
3680 case DW_TAG_enumerator:
38d518c9 3681 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3682 built_actual_name,
176620f1 3683 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3684 (cu->language == language_cplus
3685 || cu->language == language_java)
f6fe98ef
DJ
3686 ? &objfile->global_psymbols
3687 : &objfile->static_psymbols,
e142c38c 3688 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3689 break;
3690 default:
3691 break;
3692 }
5c4e30ca 3693
72bf9492
DJ
3694 if (built_actual_name)
3695 xfree (actual_name);
c906108c
SS
3696}
3697
5c4e30ca
DC
3698/* Read a partial die corresponding to a namespace; also, add a symbol
3699 corresponding to that namespace to the symbol table. NAMESPACE is
3700 the name of the enclosing namespace. */
91c24f0a 3701
72bf9492
DJ
3702static void
3703add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3704 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3705 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3706{
72bf9492 3707 /* Add a symbol for the namespace. */
e7c27a73 3708
72bf9492 3709 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3710
3711 /* Now scan partial symbols in that namespace. */
3712
91c24f0a 3713 if (pdi->has_children)
5734ee8b 3714 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3715}
3716
5d7cb8df
JK
3717/* Read a partial die corresponding to a Fortran module. */
3718
3719static void
3720add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3721 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3722{
f55ee35c 3723 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3724
3725 if (pdi->has_children)
3726 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3727}
3728
bc30ff58
JB
3729/* Read a partial die corresponding to a subprogram and create a partial
3730 symbol for that subprogram. When the CU language allows it, this
3731 routine also defines a partial symbol for each nested subprogram
3732 that this subprogram contains.
6e70227d 3733
bc30ff58
JB
3734 DIE my also be a lexical block, in which case we simply search
3735 recursively for suprograms defined inside that lexical block.
3736 Again, this is only performed when the CU language allows this
3737 type of definitions. */
3738
3739static void
3740add_partial_subprogram (struct partial_die_info *pdi,
3741 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3742 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
3743{
3744 if (pdi->tag == DW_TAG_subprogram)
3745 {
3746 if (pdi->has_pc_info)
3747 {
3748 if (pdi->lowpc < *lowpc)
3749 *lowpc = pdi->lowpc;
3750 if (pdi->highpc > *highpc)
3751 *highpc = pdi->highpc;
5734ee8b
DJ
3752 if (need_pc)
3753 {
3754 CORE_ADDR baseaddr;
3755 struct objfile *objfile = cu->objfile;
3756
3757 baseaddr = ANOFFSET (objfile->section_offsets,
3758 SECT_OFF_TEXT (objfile));
3759 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
3760 pdi->lowpc + baseaddr,
3761 pdi->highpc - 1 + baseaddr,
9291a0cd 3762 cu->per_cu->v.psymtab);
5734ee8b 3763 }
bc30ff58 3764 if (!pdi->is_declaration)
e8d05480
JB
3765 /* Ignore subprogram DIEs that do not have a name, they are
3766 illegal. Do not emit a complaint at this point, we will
3767 do so when we convert this psymtab into a symtab. */
3768 if (pdi->name)
3769 add_partial_symbol (pdi, cu);
bc30ff58
JB
3770 }
3771 }
6e70227d 3772
bc30ff58
JB
3773 if (! pdi->has_children)
3774 return;
3775
3776 if (cu->language == language_ada)
3777 {
3778 pdi = pdi->die_child;
3779 while (pdi != NULL)
3780 {
3781 fixup_partial_die (pdi, cu);
3782 if (pdi->tag == DW_TAG_subprogram
3783 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 3784 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
3785 pdi = pdi->die_sibling;
3786 }
3787 }
3788}
3789
72bf9492
DJ
3790/* See if we can figure out if the class lives in a namespace. We do
3791 this by looking for a member function; its demangled name will
3792 contain namespace info, if there is any. */
63d06c5c 3793
72bf9492
DJ
3794static void
3795guess_structure_name (struct partial_die_info *struct_pdi,
3796 struct dwarf2_cu *cu)
63d06c5c 3797{
987504bb
JJ
3798 if ((cu->language == language_cplus
3799 || cu->language == language_java)
72bf9492 3800 && cu->has_namespace_info == 0
63d06c5c
DC
3801 && struct_pdi->has_children)
3802 {
63d06c5c
DC
3803 /* NOTE: carlton/2003-10-07: Getting the info this way changes
3804 what template types look like, because the demangler
3805 frequently doesn't give the same name as the debug info. We
3806 could fix this by only using the demangled name to get the
134d01f1 3807 prefix (but see comment in read_structure_type). */
63d06c5c 3808
72bf9492 3809 struct partial_die_info *real_pdi;
5d51ca54 3810
72bf9492
DJ
3811 /* If this DIE (this DIE's specification, if any) has a parent, then
3812 we should not do this. We'll prepend the parent's fully qualified
3813 name when we create the partial symbol. */
5d51ca54 3814
72bf9492 3815 real_pdi = struct_pdi;
72bf9492 3816 while (real_pdi->has_specification)
10b3939b 3817 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
63d06c5c 3818
72bf9492
DJ
3819 if (real_pdi->die_parent != NULL)
3820 return;
63d06c5c 3821 }
63d06c5c
DC
3822}
3823
91c24f0a
DC
3824/* Read a partial die corresponding to an enumeration type. */
3825
72bf9492
DJ
3826static void
3827add_partial_enumeration (struct partial_die_info *enum_pdi,
3828 struct dwarf2_cu *cu)
91c24f0a 3829{
72bf9492 3830 struct partial_die_info *pdi;
91c24f0a
DC
3831
3832 if (enum_pdi->name != NULL)
72bf9492
DJ
3833 add_partial_symbol (enum_pdi, cu);
3834
3835 pdi = enum_pdi->die_child;
3836 while (pdi)
91c24f0a 3837 {
72bf9492 3838 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 3839 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 3840 else
72bf9492
DJ
3841 add_partial_symbol (pdi, cu);
3842 pdi = pdi->die_sibling;
91c24f0a 3843 }
91c24f0a
DC
3844}
3845
4bb7a0a7
DJ
3846/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3847 Return the corresponding abbrev, or NULL if the number is zero (indicating
3848 an empty DIE). In either case *BYTES_READ will be set to the length of
3849 the initial number. */
3850
3851static struct abbrev_info *
fe1b8b76 3852peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 3853 struct dwarf2_cu *cu)
4bb7a0a7
DJ
3854{
3855 bfd *abfd = cu->objfile->obfd;
3856 unsigned int abbrev_number;
3857 struct abbrev_info *abbrev;
3858
3859 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3860
3861 if (abbrev_number == 0)
3862 return NULL;
3863
3864 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3865 if (!abbrev)
3866 {
8a3fe4f8 3867 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
3868 bfd_get_filename (abfd));
3869 }
3870
3871 return abbrev;
3872}
3873
93311388
DE
3874/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3875 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
3876 DIE. Any children of the skipped DIEs will also be skipped. */
3877
fe1b8b76 3878static gdb_byte *
93311388 3879skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3880{
3881 struct abbrev_info *abbrev;
3882 unsigned int bytes_read;
3883
3884 while (1)
3885 {
3886 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3887 if (abbrev == NULL)
3888 return info_ptr + bytes_read;
3889 else
93311388 3890 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
3891 }
3892}
3893
93311388
DE
3894/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3895 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
3896 abbrev corresponding to that skipped uleb128 should be passed in
3897 ABBREV. Returns a pointer to this DIE's sibling, skipping any
3898 children. */
3899
fe1b8b76 3900static gdb_byte *
93311388
DE
3901skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
3902 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3903{
3904 unsigned int bytes_read;
3905 struct attribute attr;
3906 bfd *abfd = cu->objfile->obfd;
3907 unsigned int form, i;
3908
3909 for (i = 0; i < abbrev->num_attrs; i++)
3910 {
3911 /* The only abbrev we care about is DW_AT_sibling. */
3912 if (abbrev->attrs[i].name == DW_AT_sibling)
3913 {
3914 read_attribute (&attr, &abbrev->attrs[i],
3915 abfd, info_ptr, cu);
3916 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 3917 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 3918 else
93311388 3919 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
3920 }
3921
3922 /* If it isn't DW_AT_sibling, skip this attribute. */
3923 form = abbrev->attrs[i].form;
3924 skip_attribute:
3925 switch (form)
3926 {
4bb7a0a7 3927 case DW_FORM_ref_addr:
ae411497
TT
3928 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
3929 and later it is offset sized. */
3930 if (cu->header.version == 2)
3931 info_ptr += cu->header.addr_size;
3932 else
3933 info_ptr += cu->header.offset_size;
3934 break;
3935 case DW_FORM_addr:
4bb7a0a7
DJ
3936 info_ptr += cu->header.addr_size;
3937 break;
3938 case DW_FORM_data1:
3939 case DW_FORM_ref1:
3940 case DW_FORM_flag:
3941 info_ptr += 1;
3942 break;
2dc7f7b3
TT
3943 case DW_FORM_flag_present:
3944 break;
4bb7a0a7
DJ
3945 case DW_FORM_data2:
3946 case DW_FORM_ref2:
3947 info_ptr += 2;
3948 break;
3949 case DW_FORM_data4:
3950 case DW_FORM_ref4:
3951 info_ptr += 4;
3952 break;
3953 case DW_FORM_data8:
3954 case DW_FORM_ref8:
348e048f 3955 case DW_FORM_sig8:
4bb7a0a7
DJ
3956 info_ptr += 8;
3957 break;
3958 case DW_FORM_string:
9b1c24c8 3959 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
3960 info_ptr += bytes_read;
3961 break;
2dc7f7b3 3962 case DW_FORM_sec_offset:
4bb7a0a7
DJ
3963 case DW_FORM_strp:
3964 info_ptr += cu->header.offset_size;
3965 break;
2dc7f7b3 3966 case DW_FORM_exprloc:
4bb7a0a7
DJ
3967 case DW_FORM_block:
3968 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3969 info_ptr += bytes_read;
3970 break;
3971 case DW_FORM_block1:
3972 info_ptr += 1 + read_1_byte (abfd, info_ptr);
3973 break;
3974 case DW_FORM_block2:
3975 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
3976 break;
3977 case DW_FORM_block4:
3978 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
3979 break;
3980 case DW_FORM_sdata:
3981 case DW_FORM_udata:
3982 case DW_FORM_ref_udata:
3983 info_ptr = skip_leb128 (abfd, info_ptr);
3984 break;
3985 case DW_FORM_indirect:
3986 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3987 info_ptr += bytes_read;
3988 /* We need to continue parsing from here, so just go back to
3989 the top. */
3990 goto skip_attribute;
3991
3992 default:
8a3fe4f8 3993 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
3994 dwarf_form_name (form),
3995 bfd_get_filename (abfd));
3996 }
3997 }
3998
3999 if (abbrev->has_children)
93311388 4000 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4001 else
4002 return info_ptr;
4003}
4004
93311388
DE
4005/* Locate ORIG_PDI's sibling.
4006 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4007 in BUFFER. */
91c24f0a 4008
fe1b8b76 4009static gdb_byte *
93311388
DE
4010locate_pdi_sibling (struct partial_die_info *orig_pdi,
4011 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4012 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4013{
4014 /* Do we know the sibling already? */
72bf9492 4015
91c24f0a
DC
4016 if (orig_pdi->sibling)
4017 return orig_pdi->sibling;
4018
4019 /* Are there any children to deal with? */
4020
4021 if (!orig_pdi->has_children)
4022 return info_ptr;
4023
4bb7a0a7 4024 /* Skip the children the long way. */
91c24f0a 4025
93311388 4026 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4027}
4028
c906108c
SS
4029/* Expand this partial symbol table into a full symbol table. */
4030
4031static void
fba45db2 4032dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4033{
c906108c
SS
4034 if (pst != NULL)
4035 {
4036 if (pst->readin)
4037 {
8a3fe4f8 4038 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
4039 }
4040 else
4041 {
4042 if (info_verbose)
4043 {
a3f17187 4044 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
4045 gdb_flush (gdb_stdout);
4046 }
4047
10b3939b
DJ
4048 /* Restore our global data. */
4049 dwarf2_per_objfile = objfile_data (pst->objfile,
4050 dwarf2_objfile_data_key);
4051
b2ab525c
KB
4052 /* If this psymtab is constructed from a debug-only objfile, the
4053 has_section_at_zero flag will not necessarily be correct. We
4054 can get the correct value for this flag by looking at the data
4055 associated with the (presumably stripped) associated objfile. */
4056 if (pst->objfile->separate_debug_objfile_backlink)
4057 {
4058 struct dwarf2_per_objfile *dpo_backlink
4059 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4060 dwarf2_objfile_data_key);
9a619af0 4061
b2ab525c
KB
4062 dwarf2_per_objfile->has_section_at_zero
4063 = dpo_backlink->has_section_at_zero;
4064 }
4065
c906108c
SS
4066 psymtab_to_symtab_1 (pst);
4067
4068 /* Finish up the debug error message. */
4069 if (info_verbose)
a3f17187 4070 printf_filtered (_("done.\n"));
c906108c
SS
4071 }
4072 }
4073}
4074
10b3939b
DJ
4075/* Add PER_CU to the queue. */
4076
4077static void
03dd20cc 4078queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4079{
4080 struct dwarf2_queue_item *item;
4081
4082 per_cu->queued = 1;
4083 item = xmalloc (sizeof (*item));
4084 item->per_cu = per_cu;
4085 item->next = NULL;
4086
4087 if (dwarf2_queue == NULL)
4088 dwarf2_queue = item;
4089 else
4090 dwarf2_queue_tail->next = item;
4091
4092 dwarf2_queue_tail = item;
4093}
4094
4095/* Process the queue. */
4096
4097static void
4098process_queue (struct objfile *objfile)
4099{
4100 struct dwarf2_queue_item *item, *next_item;
4101
03dd20cc
DJ
4102 /* The queue starts out with one item, but following a DIE reference
4103 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4104 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4105 {
9291a0cd
TT
4106 if (dwarf2_per_objfile->using_index
4107 ? !item->per_cu->v.quick->symtab
4108 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4109 process_full_comp_unit (item->per_cu);
4110
4111 item->per_cu->queued = 0;
4112 next_item = item->next;
4113 xfree (item);
4114 }
4115
4116 dwarf2_queue_tail = NULL;
4117}
4118
4119/* Free all allocated queue entries. This function only releases anything if
4120 an error was thrown; if the queue was processed then it would have been
4121 freed as we went along. */
4122
4123static void
4124dwarf2_release_queue (void *dummy)
4125{
4126 struct dwarf2_queue_item *item, *last;
4127
4128 item = dwarf2_queue;
4129 while (item)
4130 {
4131 /* Anything still marked queued is likely to be in an
4132 inconsistent state, so discard it. */
4133 if (item->per_cu->queued)
4134 {
4135 if (item->per_cu->cu != NULL)
4136 free_one_cached_comp_unit (item->per_cu->cu);
4137 item->per_cu->queued = 0;
4138 }
4139
4140 last = item;
4141 item = item->next;
4142 xfree (last);
4143 }
4144
4145 dwarf2_queue = dwarf2_queue_tail = NULL;
4146}
4147
4148/* Read in full symbols for PST, and anything it depends on. */
4149
c906108c 4150static void
fba45db2 4151psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4152{
10b3939b 4153 struct dwarf2_per_cu_data *per_cu;
c906108c 4154 struct cleanup *back_to;
aaa75496
JB
4155 int i;
4156
4157 for (i = 0; i < pst->number_of_dependencies; i++)
4158 if (!pst->dependencies[i]->readin)
4159 {
4160 /* Inform about additional files that need to be read in. */
4161 if (info_verbose)
4162 {
a3f17187 4163 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4164 fputs_filtered (" ", gdb_stdout);
4165 wrap_here ("");
4166 fputs_filtered ("and ", gdb_stdout);
4167 wrap_here ("");
4168 printf_filtered ("%s...", pst->dependencies[i]->filename);
4169 wrap_here (""); /* Flush output */
4170 gdb_flush (gdb_stdout);
4171 }
4172 psymtab_to_symtab_1 (pst->dependencies[i]);
4173 }
4174
e38df1d0 4175 per_cu = pst->read_symtab_private;
10b3939b
DJ
4176
4177 if (per_cu == NULL)
aaa75496
JB
4178 {
4179 /* It's an include file, no symbols to read for it.
4180 Everything is in the parent symtab. */
4181 pst->readin = 1;
4182 return;
4183 }
c906108c 4184
9291a0cd 4185 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4186}
4187
93311388 4188/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4189
93311388 4190static void
31ffec48 4191load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 4192{
31ffec48 4193 bfd *abfd = objfile->obfd;
10b3939b 4194 struct dwarf2_cu *cu;
c764a876 4195 unsigned int offset;
93311388 4196 gdb_byte *info_ptr, *beg_of_comp_unit;
10b3939b
DJ
4197 struct cleanup *back_to, *free_cu_cleanup;
4198 struct attribute *attr;
6502dd73 4199
348e048f
DE
4200 gdb_assert (! per_cu->from_debug_types);
4201
c906108c 4202 /* Set local variables from the partial symbol table info. */
10b3939b 4203 offset = per_cu->offset;
6502dd73 4204
be391dca 4205 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4206 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4207 beg_of_comp_unit = info_ptr;
63d06c5c 4208
93311388 4209 cu = alloc_one_comp_unit (objfile);
c906108c 4210
10b3939b
DJ
4211 /* If an error occurs while loading, release our storage. */
4212 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4213
93311388 4214 /* Read in the comp_unit header. */
10b3939b 4215 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4216
93311388
DE
4217 /* Complete the cu_header. */
4218 cu->header.offset = offset;
4219 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4220
4221 /* Read the abbrevs for this compilation unit. */
10b3939b
DJ
4222 dwarf2_read_abbrevs (abfd, cu);
4223 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
4224
93311388 4225 /* Link this compilation unit into the compilation unit tree. */
10b3939b 4226 per_cu->cu = cu;
93311388 4227 cu->per_cu = per_cu;
f792889a 4228 cu->type_hash = per_cu->type_hash;
e142c38c 4229
93311388 4230 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4231
4232 /* We try not to read any attributes in this function, because not
4233 all objfiles needed for references have been loaded yet, and symbol
4234 table processing isn't initialized. But we have to set the CU language,
4235 or we won't be able to build types correctly. */
4236 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4237 if (attr)
4238 set_cu_language (DW_UNSND (attr), cu);
4239 else
4240 set_cu_language (language_minimal, cu);
4241
a6c727b2
DJ
4242 /* Similarly, if we do not read the producer, we can not apply
4243 producer-specific interpretation. */
4244 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4245 if (attr)
4246 cu->producer = DW_STRING (attr);
4247
348e048f
DE
4248 /* Link this CU into read_in_chain. */
4249 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4250 dwarf2_per_objfile->read_in_chain = per_cu;
4251
10b3939b 4252 do_cleanups (back_to);
e142c38c 4253
10b3939b
DJ
4254 /* We've successfully allocated this compilation unit. Let our caller
4255 clean it up when finished with it. */
4256 discard_cleanups (free_cu_cleanup);
10b3939b
DJ
4257}
4258
4259/* Generate full symbol information for PST and CU, whose DIEs have
4260 already been loaded into memory. */
4261
4262static void
4263process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4264{
10b3939b 4265 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4266 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4267 CORE_ADDR lowpc, highpc;
4268 struct symtab *symtab;
4269 struct cleanup *back_to;
10b3939b
DJ
4270 CORE_ADDR baseaddr;
4271
4272 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4273
10b3939b
DJ
4274 buildsym_init ();
4275 back_to = make_cleanup (really_free_pendings, NULL);
4276
4277 cu->list_in_scope = &file_symbols;
c906108c 4278
d85a05f0 4279 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4280
c906108c 4281 /* Do line number decoding in read_file_scope () */
10b3939b 4282 process_die (cu->dies, cu);
c906108c 4283
fae299cd
DC
4284 /* Some compilers don't define a DW_AT_high_pc attribute for the
4285 compilation unit. If the DW_AT_high_pc is missing, synthesize
4286 it, by scanning the DIE's below the compilation unit. */
10b3939b 4287 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4288
613e1657 4289 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4290
4291 /* Set symtab language to language from DW_AT_language.
4292 If the compilation is from a C file generated by language preprocessors,
4293 do not set the language if it was already deduced by start_subfile. */
4294 if (symtab != NULL
10b3939b 4295 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4296 {
10b3939b 4297 symtab->language = cu->language;
c906108c 4298 }
9291a0cd
TT
4299
4300 if (dwarf2_per_objfile->using_index)
4301 per_cu->v.quick->symtab = symtab;
4302 else
4303 {
4304 struct partial_symtab *pst = per_cu->v.psymtab;
4305 pst->symtab = symtab;
4306 pst->readin = 1;
4307 }
c906108c
SS
4308
4309 do_cleanups (back_to);
4310}
4311
4312/* Process a die and its children. */
4313
4314static void
e7c27a73 4315process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4316{
4317 switch (die->tag)
4318 {
4319 case DW_TAG_padding:
4320 break;
4321 case DW_TAG_compile_unit:
e7c27a73 4322 read_file_scope (die, cu);
c906108c 4323 break;
348e048f
DE
4324 case DW_TAG_type_unit:
4325 read_type_unit_scope (die, cu);
4326 break;
c906108c 4327 case DW_TAG_subprogram:
c906108c 4328 case DW_TAG_inlined_subroutine:
edb3359d 4329 read_func_scope (die, cu);
c906108c
SS
4330 break;
4331 case DW_TAG_lexical_block:
14898363
L
4332 case DW_TAG_try_block:
4333 case DW_TAG_catch_block:
e7c27a73 4334 read_lexical_block_scope (die, cu);
c906108c
SS
4335 break;
4336 case DW_TAG_class_type:
680b30c7 4337 case DW_TAG_interface_type:
c906108c
SS
4338 case DW_TAG_structure_type:
4339 case DW_TAG_union_type:
134d01f1 4340 process_structure_scope (die, cu);
c906108c
SS
4341 break;
4342 case DW_TAG_enumeration_type:
134d01f1 4343 process_enumeration_scope (die, cu);
c906108c 4344 break;
134d01f1 4345
f792889a
DJ
4346 /* These dies have a type, but processing them does not create
4347 a symbol or recurse to process the children. Therefore we can
4348 read them on-demand through read_type_die. */
c906108c 4349 case DW_TAG_subroutine_type:
72019c9c 4350 case DW_TAG_set_type:
c906108c 4351 case DW_TAG_array_type:
c906108c 4352 case DW_TAG_pointer_type:
c906108c 4353 case DW_TAG_ptr_to_member_type:
c906108c 4354 case DW_TAG_reference_type:
c906108c 4355 case DW_TAG_string_type:
c906108c 4356 break;
134d01f1 4357
c906108c 4358 case DW_TAG_base_type:
a02abb62 4359 case DW_TAG_subrange_type:
cb249c71 4360 case DW_TAG_typedef:
134d01f1
DJ
4361 /* Add a typedef symbol for the type definition, if it has a
4362 DW_AT_name. */
f792889a 4363 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4364 break;
c906108c 4365 case DW_TAG_common_block:
e7c27a73 4366 read_common_block (die, cu);
c906108c
SS
4367 break;
4368 case DW_TAG_common_inclusion:
4369 break;
d9fa45fe 4370 case DW_TAG_namespace:
63d06c5c 4371 processing_has_namespace_info = 1;
e7c27a73 4372 read_namespace (die, cu);
d9fa45fe 4373 break;
5d7cb8df 4374 case DW_TAG_module:
f55ee35c 4375 processing_has_namespace_info = 1;
5d7cb8df
JK
4376 read_module (die, cu);
4377 break;
d9fa45fe
DC
4378 case DW_TAG_imported_declaration:
4379 case DW_TAG_imported_module:
63d06c5c 4380 processing_has_namespace_info = 1;
27aa8d6a
SW
4381 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4382 || cu->language != language_fortran))
4383 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4384 dwarf_tag_name (die->tag));
4385 read_import_statement (die, cu);
d9fa45fe 4386 break;
c906108c 4387 default:
e7c27a73 4388 new_symbol (die, NULL, cu);
c906108c
SS
4389 break;
4390 }
4391}
4392
94af9270
KS
4393/* A helper function for dwarf2_compute_name which determines whether DIE
4394 needs to have the name of the scope prepended to the name listed in the
4395 die. */
4396
4397static int
4398die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4399{
1c809c68
TT
4400 struct attribute *attr;
4401
94af9270
KS
4402 switch (die->tag)
4403 {
4404 case DW_TAG_namespace:
4405 case DW_TAG_typedef:
4406 case DW_TAG_class_type:
4407 case DW_TAG_interface_type:
4408 case DW_TAG_structure_type:
4409 case DW_TAG_union_type:
4410 case DW_TAG_enumeration_type:
4411 case DW_TAG_enumerator:
4412 case DW_TAG_subprogram:
4413 case DW_TAG_member:
4414 return 1;
4415
4416 case DW_TAG_variable:
4417 /* We only need to prefix "globally" visible variables. These include
4418 any variable marked with DW_AT_external or any variable that
4419 lives in a namespace. [Variables in anonymous namespaces
4420 require prefixing, but they are not DW_AT_external.] */
4421
4422 if (dwarf2_attr (die, DW_AT_specification, cu))
4423 {
4424 struct dwarf2_cu *spec_cu = cu;
9a619af0 4425
94af9270
KS
4426 return die_needs_namespace (die_specification (die, &spec_cu),
4427 spec_cu);
4428 }
4429
1c809c68 4430 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4431 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4432 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4433 return 0;
4434 /* A variable in a lexical block of some kind does not need a
4435 namespace, even though in C++ such variables may be external
4436 and have a mangled name. */
4437 if (die->parent->tag == DW_TAG_lexical_block
4438 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4439 || die->parent->tag == DW_TAG_catch_block
4440 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4441 return 0;
4442 return 1;
94af9270
KS
4443
4444 default:
4445 return 0;
4446 }
4447}
4448
4449/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4450 compute the physname for the object, which include a method's
4451 formal parameters (C++/Java) and return type (Java).
4452
af6b7be1
JB
4453 For Ada, return the DIE's linkage name rather than the fully qualified
4454 name. PHYSNAME is ignored..
4455
94af9270
KS
4456 The result is allocated on the objfile_obstack and canonicalized. */
4457
4458static const char *
4459dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4460 int physname)
4461{
4462 if (name == NULL)
4463 name = dwarf2_name (die, cu);
4464
f55ee35c
JK
4465 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4466 compute it by typename_concat inside GDB. */
4467 if (cu->language == language_ada
4468 || (cu->language == language_fortran && physname))
4469 {
4470 /* For Ada unit, we prefer the linkage name over the name, as
4471 the former contains the exported name, which the user expects
4472 to be able to reference. Ideally, we want the user to be able
4473 to reference this entity using either natural or linkage name,
4474 but we haven't started looking at this enhancement yet. */
4475 struct attribute *attr;
4476
4477 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4478 if (attr == NULL)
4479 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4480 if (attr && DW_STRING (attr))
4481 return DW_STRING (attr);
4482 }
4483
94af9270
KS
4484 /* These are the only languages we know how to qualify names in. */
4485 if (name != NULL
f55ee35c
JK
4486 && (cu->language == language_cplus || cu->language == language_java
4487 || cu->language == language_fortran))
94af9270
KS
4488 {
4489 if (die_needs_namespace (die, cu))
4490 {
4491 long length;
4492 char *prefix;
4493 struct ui_file *buf;
4494
4495 prefix = determine_prefix (die, cu);
4496 buf = mem_fileopen ();
4497 if (*prefix != '\0')
4498 {
f55ee35c
JK
4499 char *prefixed_name = typename_concat (NULL, prefix, name,
4500 physname, cu);
9a619af0 4501
94af9270
KS
4502 fputs_unfiltered (prefixed_name, buf);
4503 xfree (prefixed_name);
4504 }
4505 else
4506 fputs_unfiltered (name ? name : "", buf);
4507
4508 /* For Java and C++ methods, append formal parameter type
4509 information, if PHYSNAME. */
6e70227d 4510
94af9270
KS
4511 if (physname && die->tag == DW_TAG_subprogram
4512 && (cu->language == language_cplus
4513 || cu->language == language_java))
4514 {
4515 struct type *type = read_type_die (die, cu);
4516
4517 c_type_print_args (type, buf, 0, cu->language);
4518
4519 if (cu->language == language_java)
4520 {
4521 /* For java, we must append the return type to method
4522 names. */
4523 if (die->tag == DW_TAG_subprogram)
4524 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4525 0, 0);
4526 }
4527 else if (cu->language == language_cplus)
4528 {
4529 if (TYPE_NFIELDS (type) > 0
4530 && TYPE_FIELD_ARTIFICIAL (type, 0)
4531 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4532 fputs_unfiltered (" const", buf);
4533 }
4534 }
4535
4536 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4537 &length);
4538 ui_file_delete (buf);
4539
4540 if (cu->language == language_cplus)
4541 {
4542 char *cname
4543 = dwarf2_canonicalize_name (name, cu,
4544 &cu->objfile->objfile_obstack);
9a619af0 4545
94af9270
KS
4546 if (cname != NULL)
4547 name = cname;
4548 }
4549 }
4550 }
4551
4552 return name;
4553}
4554
0114d602
DJ
4555/* Return the fully qualified name of DIE, based on its DW_AT_name.
4556 If scope qualifiers are appropriate they will be added. The result
4557 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4558 not have a name. NAME may either be from a previous call to
4559 dwarf2_name or NULL.
4560
4561 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4562
4563static const char *
94af9270 4564dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 4565{
94af9270
KS
4566 return dwarf2_compute_name (name, die, cu, 0);
4567}
0114d602 4568
94af9270
KS
4569/* Construct a physname for the given DIE in CU. NAME may either be
4570 from a previous call to dwarf2_name or NULL. The result will be
4571 allocated on the objfile_objstack or NULL if the DIE does not have a
4572 name.
0114d602 4573
94af9270 4574 The output string will be canonicalized (if C++/Java). */
0114d602 4575
94af9270
KS
4576static const char *
4577dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4578{
4579 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
4580}
4581
27aa8d6a
SW
4582/* Read the import statement specified by the given die and record it. */
4583
4584static void
4585read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4586{
4587 struct attribute *import_attr;
4588 struct die_info *imported_die;
de4affc9 4589 struct dwarf2_cu *imported_cu;
27aa8d6a 4590 const char *imported_name;
794684b6 4591 const char *imported_name_prefix;
13387711
SW
4592 const char *canonical_name;
4593 const char *import_alias;
4594 const char *imported_declaration = NULL;
794684b6 4595 const char *import_prefix;
13387711
SW
4596
4597 char *temp;
27aa8d6a
SW
4598
4599 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4600 if (import_attr == NULL)
4601 {
4602 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4603 dwarf_tag_name (die->tag));
4604 return;
4605 }
4606
de4affc9
CC
4607 imported_cu = cu;
4608 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4609 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
4610 if (imported_name == NULL)
4611 {
4612 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4613
4614 The import in the following code:
4615 namespace A
4616 {
4617 typedef int B;
4618 }
4619
4620 int main ()
4621 {
4622 using A::B;
4623 B b;
4624 return b;
4625 }
4626
4627 ...
4628 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4629 <52> DW_AT_decl_file : 1
4630 <53> DW_AT_decl_line : 6
4631 <54> DW_AT_import : <0x75>
4632 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4633 <59> DW_AT_name : B
4634 <5b> DW_AT_decl_file : 1
4635 <5c> DW_AT_decl_line : 2
4636 <5d> DW_AT_type : <0x6e>
4637 ...
4638 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4639 <76> DW_AT_byte_size : 4
4640 <77> DW_AT_encoding : 5 (signed)
4641
4642 imports the wrong die ( 0x75 instead of 0x58 ).
4643 This case will be ignored until the gcc bug is fixed. */
4644 return;
4645 }
4646
82856980
SW
4647 /* Figure out the local name after import. */
4648 import_alias = dwarf2_name (die, cu);
27aa8d6a 4649
794684b6
SW
4650 /* Figure out where the statement is being imported to. */
4651 import_prefix = determine_prefix (die, cu);
4652
4653 /* Figure out what the scope of the imported die is and prepend it
4654 to the name of the imported die. */
de4affc9 4655 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 4656
f55ee35c
JK
4657 if (imported_die->tag != DW_TAG_namespace
4658 && imported_die->tag != DW_TAG_module)
794684b6 4659 {
13387711
SW
4660 imported_declaration = imported_name;
4661 canonical_name = imported_name_prefix;
794684b6 4662 }
13387711 4663 else if (strlen (imported_name_prefix) > 0)
794684b6 4664 {
13387711
SW
4665 temp = alloca (strlen (imported_name_prefix)
4666 + 2 + strlen (imported_name) + 1);
4667 strcpy (temp, imported_name_prefix);
4668 strcat (temp, "::");
4669 strcat (temp, imported_name);
4670 canonical_name = temp;
794684b6 4671 }
13387711
SW
4672 else
4673 canonical_name = imported_name;
794684b6 4674
c0cc3a76
SW
4675 cp_add_using_directive (import_prefix,
4676 canonical_name,
4677 import_alias,
13387711 4678 imported_declaration,
c0cc3a76 4679 &cu->objfile->objfile_obstack);
27aa8d6a
SW
4680}
4681
5fb290d7 4682static void
e142c38c 4683initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 4684{
e142c38c 4685 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
4686}
4687
cb1df416
DJ
4688static void
4689free_cu_line_header (void *arg)
4690{
4691 struct dwarf2_cu *cu = arg;
4692
4693 free_line_header (cu->line_header);
4694 cu->line_header = NULL;
4695}
4696
9291a0cd
TT
4697static void
4698find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
4699 char **name, char **comp_dir)
4700{
4701 struct attribute *attr;
4702
4703 *name = NULL;
4704 *comp_dir = NULL;
4705
4706 /* Find the filename. Do not use dwarf2_name here, since the filename
4707 is not a source language identifier. */
4708 attr = dwarf2_attr (die, DW_AT_name, cu);
4709 if (attr)
4710 {
4711 *name = DW_STRING (attr);
4712 }
4713
4714 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
4715 if (attr)
4716 *comp_dir = DW_STRING (attr);
4717 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
4718 {
4719 *comp_dir = ldirname (*name);
4720 if (*comp_dir != NULL)
4721 make_cleanup (xfree, *comp_dir);
4722 }
4723 if (*comp_dir != NULL)
4724 {
4725 /* Irix 6.2 native cc prepends <machine>.: to the compilation
4726 directory, get rid of it. */
4727 char *cp = strchr (*comp_dir, ':');
4728
4729 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
4730 *comp_dir = cp + 1;
4731 }
4732
4733 if (*name == NULL)
4734 *name = "<unknown>";
4735}
4736
c906108c 4737static void
e7c27a73 4738read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4739{
e7c27a73 4740 struct objfile *objfile = cu->objfile;
debd256d 4741 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 4742 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
4743 CORE_ADDR highpc = ((CORE_ADDR) 0);
4744 struct attribute *attr;
e1024ff1 4745 char *name = NULL;
c906108c
SS
4746 char *comp_dir = NULL;
4747 struct die_info *child_die;
4748 bfd *abfd = objfile->obfd;
debd256d 4749 struct line_header *line_header = 0;
e142c38c 4750 CORE_ADDR baseaddr;
6e70227d 4751
e142c38c 4752 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4753
fae299cd 4754 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
4755
4756 /* If we didn't find a lowpc, set it to highpc to avoid complaints
4757 from finish_block. */
2acceee2 4758 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
4759 lowpc = highpc;
4760 lowpc += baseaddr;
4761 highpc += baseaddr;
4762
9291a0cd 4763 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 4764
e142c38c 4765 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
4766 if (attr)
4767 {
e142c38c 4768 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
4769 }
4770
b0f35d58 4771 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 4772 if (attr)
b0f35d58 4773 cu->producer = DW_STRING (attr);
303b6f5d 4774
c906108c
SS
4775 /* We assume that we're processing GCC output. */
4776 processing_gcc_compilation = 2;
c906108c 4777
df8a16a1
DJ
4778 processing_has_namespace_info = 0;
4779
c906108c
SS
4780 start_symtab (name, comp_dir, lowpc);
4781 record_debugformat ("DWARF 2");
303b6f5d 4782 record_producer (cu->producer);
c906108c 4783
e142c38c 4784 initialize_cu_func_list (cu);
c906108c 4785
cb1df416
DJ
4786 /* Decode line number information if present. We do this before
4787 processing child DIEs, so that the line header table is available
4788 for DW_AT_decl_file. */
e142c38c 4789 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
4790 if (attr)
4791 {
debd256d 4792 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 4793 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
4794 if (line_header)
4795 {
cb1df416
DJ
4796 cu->line_header = line_header;
4797 make_cleanup (free_cu_line_header, cu);
aaa75496 4798 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 4799 }
5fb290d7 4800 }
debd256d 4801
cb1df416
DJ
4802 /* Process all dies in compilation unit. */
4803 if (die->child != NULL)
4804 {
4805 child_die = die->child;
4806 while (child_die && child_die->tag)
4807 {
4808 process_die (child_die, cu);
4809 child_die = sibling_die (child_die);
4810 }
4811 }
4812
2e276125
JB
4813 /* Decode macro information, if present. Dwarf 2 macro information
4814 refers to information in the line number info statement program
4815 header, so we can only read it if we've read the header
4816 successfully. */
e142c38c 4817 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 4818 if (attr && line_header)
2e276125
JB
4819 {
4820 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 4821
2e276125 4822 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 4823 comp_dir, abfd, cu);
2e276125 4824 }
debd256d 4825 do_cleanups (back_to);
5fb290d7
DJ
4826}
4827
348e048f
DE
4828/* For TUs we want to skip the first top level sibling if it's not the
4829 actual type being defined by this TU. In this case the first top
4830 level sibling is there to provide context only. */
4831
4832static void
4833read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
4834{
4835 struct objfile *objfile = cu->objfile;
4836 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
4837 CORE_ADDR lowpc;
4838 struct attribute *attr;
4839 char *name = NULL;
4840 char *comp_dir = NULL;
4841 struct die_info *child_die;
4842 bfd *abfd = objfile->obfd;
348e048f
DE
4843
4844 /* start_symtab needs a low pc, but we don't really have one.
4845 Do what read_file_scope would do in the absence of such info. */
4846 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4847
4848 /* Find the filename. Do not use dwarf2_name here, since the filename
4849 is not a source language identifier. */
4850 attr = dwarf2_attr (die, DW_AT_name, cu);
4851 if (attr)
4852 name = DW_STRING (attr);
4853
4854 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
4855 if (attr)
4856 comp_dir = DW_STRING (attr);
4857 else if (name != NULL && IS_ABSOLUTE_PATH (name))
4858 {
4859 comp_dir = ldirname (name);
4860 if (comp_dir != NULL)
4861 make_cleanup (xfree, comp_dir);
4862 }
4863
4864 if (name == NULL)
4865 name = "<unknown>";
4866
4867 attr = dwarf2_attr (die, DW_AT_language, cu);
4868 if (attr)
4869 set_cu_language (DW_UNSND (attr), cu);
4870
4871 /* This isn't technically needed today. It is done for symmetry
4872 with read_file_scope. */
4873 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 4874 if (attr)
348e048f
DE
4875 cu->producer = DW_STRING (attr);
4876
4877 /* We assume that we're processing GCC output. */
4878 processing_gcc_compilation = 2;
4879
4880 processing_has_namespace_info = 0;
4881
4882 start_symtab (name, comp_dir, lowpc);
4883 record_debugformat ("DWARF 2");
4884 record_producer (cu->producer);
4885
4886 /* Process the dies in the type unit. */
4887 if (die->child == NULL)
4888 {
4889 dump_die_for_error (die);
4890 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
4891 bfd_get_filename (abfd));
4892 }
4893
4894 child_die = die->child;
4895
4896 while (child_die && child_die->tag)
4897 {
4898 process_die (child_die, cu);
4899
4900 child_die = sibling_die (child_die);
4901 }
4902
4903 do_cleanups (back_to);
4904}
4905
5fb290d7 4906static void
e142c38c
DJ
4907add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
4908 struct dwarf2_cu *cu)
5fb290d7
DJ
4909{
4910 struct function_range *thisfn;
4911
4912 thisfn = (struct function_range *)
7b5a2f43 4913 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
4914 thisfn->name = name;
4915 thisfn->lowpc = lowpc;
4916 thisfn->highpc = highpc;
4917 thisfn->seen_line = 0;
4918 thisfn->next = NULL;
4919
e142c38c
DJ
4920 if (cu->last_fn == NULL)
4921 cu->first_fn = thisfn;
5fb290d7 4922 else
e142c38c 4923 cu->last_fn->next = thisfn;
5fb290d7 4924
e142c38c 4925 cu->last_fn = thisfn;
c906108c
SS
4926}
4927
d389af10
JK
4928/* qsort helper for inherit_abstract_dies. */
4929
4930static int
4931unsigned_int_compar (const void *ap, const void *bp)
4932{
4933 unsigned int a = *(unsigned int *) ap;
4934 unsigned int b = *(unsigned int *) bp;
4935
4936 return (a > b) - (b > a);
4937}
4938
4939/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
4940 Inherit only the children of the DW_AT_abstract_origin DIE not being already
4941 referenced by DW_AT_abstract_origin from the children of the current DIE. */
4942
4943static void
4944inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
4945{
4946 struct die_info *child_die;
4947 unsigned die_children_count;
4948 /* CU offsets which were referenced by children of the current DIE. */
4949 unsigned *offsets;
4950 unsigned *offsets_end, *offsetp;
4951 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
4952 struct die_info *origin_die;
4953 /* Iterator of the ORIGIN_DIE children. */
4954 struct die_info *origin_child_die;
4955 struct cleanup *cleanups;
4956 struct attribute *attr;
4957
4958 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
4959 if (!attr)
4960 return;
4961
4962 origin_die = follow_die_ref (die, attr, &cu);
edb3359d
DJ
4963 if (die->tag != origin_die->tag
4964 && !(die->tag == DW_TAG_inlined_subroutine
4965 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
4966 complaint (&symfile_complaints,
4967 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
4968 die->offset, origin_die->offset);
4969
4970 child_die = die->child;
4971 die_children_count = 0;
4972 while (child_die && child_die->tag)
4973 {
4974 child_die = sibling_die (child_die);
4975 die_children_count++;
4976 }
4977 offsets = xmalloc (sizeof (*offsets) * die_children_count);
4978 cleanups = make_cleanup (xfree, offsets);
4979
4980 offsets_end = offsets;
4981 child_die = die->child;
4982 while (child_die && child_die->tag)
4983 {
c38f313d
DJ
4984 /* For each CHILD_DIE, find the corresponding child of
4985 ORIGIN_DIE. If there is more than one layer of
4986 DW_AT_abstract_origin, follow them all; there shouldn't be,
4987 but GCC versions at least through 4.4 generate this (GCC PR
4988 40573). */
4989 struct die_info *child_origin_die = child_die;
9a619af0 4990
c38f313d
DJ
4991 while (1)
4992 {
4993 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin, cu);
4994 if (attr == NULL)
4995 break;
4996 child_origin_die = follow_die_ref (child_origin_die, attr, &cu);
4997 }
4998
d389af10
JK
4999 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5000 counterpart may exist. */
c38f313d 5001 if (child_origin_die != child_die)
d389af10 5002 {
edb3359d
DJ
5003 if (child_die->tag != child_origin_die->tag
5004 && !(child_die->tag == DW_TAG_inlined_subroutine
5005 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5006 complaint (&symfile_complaints,
5007 _("Child DIE 0x%x and its abstract origin 0x%x have "
5008 "different tags"), child_die->offset,
5009 child_origin_die->offset);
c38f313d
DJ
5010 if (child_origin_die->parent != origin_die)
5011 complaint (&symfile_complaints,
5012 _("Child DIE 0x%x and its abstract origin 0x%x have "
5013 "different parents"), child_die->offset,
5014 child_origin_die->offset);
5015 else
5016 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5017 }
5018 child_die = sibling_die (child_die);
5019 }
5020 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5021 unsigned_int_compar);
5022 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5023 if (offsetp[-1] == *offsetp)
5024 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5025 "to DIE 0x%x as their abstract origin"),
5026 die->offset, *offsetp);
5027
5028 offsetp = offsets;
5029 origin_child_die = origin_die->child;
5030 while (origin_child_die && origin_child_die->tag)
5031 {
5032 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5033 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5034 offsetp++;
5035 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5036 {
5037 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5038 process_die (origin_child_die, cu);
5039 }
5040 origin_child_die = sibling_die (origin_child_die);
5041 }
5042
5043 do_cleanups (cleanups);
5044}
5045
c906108c 5046static void
e7c27a73 5047read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5048{
e7c27a73 5049 struct objfile *objfile = cu->objfile;
52f0bd74 5050 struct context_stack *new;
c906108c
SS
5051 CORE_ADDR lowpc;
5052 CORE_ADDR highpc;
5053 struct die_info *child_die;
edb3359d 5054 struct attribute *attr, *call_line, *call_file;
c906108c 5055 char *name;
e142c38c 5056 CORE_ADDR baseaddr;
801e3a5b 5057 struct block *block;
edb3359d 5058 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5059 VEC (symbolp) *template_args = NULL;
5060 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5061
5062 if (inlined_func)
5063 {
5064 /* If we do not have call site information, we can't show the
5065 caller of this inlined function. That's too confusing, so
5066 only use the scope for local variables. */
5067 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5068 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5069 if (call_line == NULL || call_file == NULL)
5070 {
5071 read_lexical_block_scope (die, cu);
5072 return;
5073 }
5074 }
c906108c 5075
e142c38c
DJ
5076 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5077
94af9270 5078 name = dwarf2_name (die, cu);
c906108c 5079
e8d05480
JB
5080 /* Ignore functions with missing or empty names. These are actually
5081 illegal according to the DWARF standard. */
5082 if (name == NULL)
5083 {
5084 complaint (&symfile_complaints,
5085 _("missing name for subprogram DIE at %d"), die->offset);
5086 return;
5087 }
5088
5089 /* Ignore functions with missing or invalid low and high pc attributes. */
5090 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5091 {
ae4d0c03
PM
5092 attr = dwarf2_attr (die, DW_AT_external, cu);
5093 if (!attr || !DW_UNSND (attr))
5094 complaint (&symfile_complaints,
5095 _("cannot get low and high bounds for subprogram DIE at %d"),
5096 die->offset);
e8d05480
JB
5097 return;
5098 }
c906108c
SS
5099
5100 lowpc += baseaddr;
5101 highpc += baseaddr;
5102
5fb290d7 5103 /* Record the function range for dwarf_decode_lines. */
e142c38c 5104 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5105
34eaf542
TT
5106 /* If we have any template arguments, then we must allocate a
5107 different sort of symbol. */
5108 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5109 {
5110 if (child_die->tag == DW_TAG_template_type_param
5111 || child_die->tag == DW_TAG_template_value_param)
5112 {
5113 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5114 struct template_symbol);
5115 templ_func->base.is_cplus_template_function = 1;
5116 break;
5117 }
5118 }
5119
c906108c 5120 new = push_context (0, lowpc);
34eaf542
TT
5121 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5122 (struct symbol *) templ_func);
4c2df51b 5123
4cecd739
DJ
5124 /* If there is a location expression for DW_AT_frame_base, record
5125 it. */
e142c38c 5126 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5127 if (attr)
c034e007
AC
5128 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5129 expression is being recorded directly in the function's symbol
5130 and not in a separate frame-base object. I guess this hack is
5131 to avoid adding some sort of frame-base adjunct/annex to the
5132 function's symbol :-(. The problem with doing this is that it
5133 results in a function symbol with a location expression that
5134 has nothing to do with the location of the function, ouch! The
5135 relationship should be: a function's symbol has-a frame base; a
5136 frame-base has-a location expression. */
e7c27a73 5137 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5138
e142c38c 5139 cu->list_in_scope = &local_symbols;
c906108c 5140
639d11d3 5141 if (die->child != NULL)
c906108c 5142 {
639d11d3 5143 child_die = die->child;
c906108c
SS
5144 while (child_die && child_die->tag)
5145 {
34eaf542
TT
5146 if (child_die->tag == DW_TAG_template_type_param
5147 || child_die->tag == DW_TAG_template_value_param)
5148 {
5149 struct symbol *arg = new_symbol (child_die, NULL, cu);
5150
5151 VEC_safe_push (symbolp, template_args, arg);
5152 }
5153 else
5154 process_die (child_die, cu);
c906108c
SS
5155 child_die = sibling_die (child_die);
5156 }
5157 }
5158
d389af10
JK
5159 inherit_abstract_dies (die, cu);
5160
4a811a97
UW
5161 /* If we have a DW_AT_specification, we might need to import using
5162 directives from the context of the specification DIE. See the
5163 comment in determine_prefix. */
5164 if (cu->language == language_cplus
5165 && dwarf2_attr (die, DW_AT_specification, cu))
5166 {
5167 struct dwarf2_cu *spec_cu = cu;
5168 struct die_info *spec_die = die_specification (die, &spec_cu);
5169
5170 while (spec_die)
5171 {
5172 child_die = spec_die->child;
5173 while (child_die && child_die->tag)
5174 {
5175 if (child_die->tag == DW_TAG_imported_module)
5176 process_die (child_die, spec_cu);
5177 child_die = sibling_die (child_die);
5178 }
5179
5180 /* In some cases, GCC generates specification DIEs that
5181 themselves contain DW_AT_specification attributes. */
5182 spec_die = die_specification (spec_die, &spec_cu);
5183 }
5184 }
5185
c906108c
SS
5186 new = pop_context ();
5187 /* Make a block for the local symbols within. */
801e3a5b
JB
5188 block = finish_block (new->name, &local_symbols, new->old_blocks,
5189 lowpc, highpc, objfile);
5190
df8a16a1 5191 /* For C++, set the block's scope. */
f55ee35c 5192 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5193 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5194 determine_prefix (die, cu),
df8a16a1
DJ
5195 processing_has_namespace_info);
5196
801e3a5b
JB
5197 /* If we have address ranges, record them. */
5198 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5199
34eaf542
TT
5200 /* Attach template arguments to function. */
5201 if (! VEC_empty (symbolp, template_args))
5202 {
5203 gdb_assert (templ_func != NULL);
5204
5205 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5206 templ_func->template_arguments
5207 = obstack_alloc (&objfile->objfile_obstack,
5208 (templ_func->n_template_arguments
5209 * sizeof (struct symbol *)));
5210 memcpy (templ_func->template_arguments,
5211 VEC_address (symbolp, template_args),
5212 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5213 VEC_free (symbolp, template_args);
5214 }
5215
208d8187
JB
5216 /* In C++, we can have functions nested inside functions (e.g., when
5217 a function declares a class that has methods). This means that
5218 when we finish processing a function scope, we may need to go
5219 back to building a containing block's symbol lists. */
5220 local_symbols = new->locals;
5221 param_symbols = new->params;
27aa8d6a 5222 using_directives = new->using_directives;
208d8187 5223
921e78cf
JB
5224 /* If we've finished processing a top-level function, subsequent
5225 symbols go in the file symbol list. */
5226 if (outermost_context_p ())
e142c38c 5227 cu->list_in_scope = &file_symbols;
c906108c
SS
5228}
5229
5230/* Process all the DIES contained within a lexical block scope. Start
5231 a new scope, process the dies, and then close the scope. */
5232
5233static void
e7c27a73 5234read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5235{
e7c27a73 5236 struct objfile *objfile = cu->objfile;
52f0bd74 5237 struct context_stack *new;
c906108c
SS
5238 CORE_ADDR lowpc, highpc;
5239 struct die_info *child_die;
e142c38c
DJ
5240 CORE_ADDR baseaddr;
5241
5242 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5243
5244 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5245 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5246 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5247 be nasty. Might be easier to properly extend generic blocks to
af34e669 5248 describe ranges. */
d85a05f0 5249 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5250 return;
5251 lowpc += baseaddr;
5252 highpc += baseaddr;
5253
5254 push_context (0, lowpc);
639d11d3 5255 if (die->child != NULL)
c906108c 5256 {
639d11d3 5257 child_die = die->child;
c906108c
SS
5258 while (child_die && child_die->tag)
5259 {
e7c27a73 5260 process_die (child_die, cu);
c906108c
SS
5261 child_die = sibling_die (child_die);
5262 }
5263 }
5264 new = pop_context ();
5265
8540c487 5266 if (local_symbols != NULL || using_directives != NULL)
c906108c 5267 {
801e3a5b
JB
5268 struct block *block
5269 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5270 highpc, objfile);
5271
5272 /* Note that recording ranges after traversing children, as we
5273 do here, means that recording a parent's ranges entails
5274 walking across all its children's ranges as they appear in
5275 the address map, which is quadratic behavior.
5276
5277 It would be nicer to record the parent's ranges before
5278 traversing its children, simply overriding whatever you find
5279 there. But since we don't even decide whether to create a
5280 block until after we've traversed its children, that's hard
5281 to do. */
5282 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5283 }
5284 local_symbols = new->locals;
27aa8d6a 5285 using_directives = new->using_directives;
c906108c
SS
5286}
5287
43039443 5288/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5289 Return 1 if the attributes are present and valid, otherwise, return 0.
5290 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5291
5292static int
5293dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5294 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5295 struct partial_symtab *ranges_pst)
43039443
JK
5296{
5297 struct objfile *objfile = cu->objfile;
5298 struct comp_unit_head *cu_header = &cu->header;
5299 bfd *obfd = objfile->obfd;
5300 unsigned int addr_size = cu_header->addr_size;
5301 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5302 /* Base address selection entry. */
5303 CORE_ADDR base;
5304 int found_base;
5305 unsigned int dummy;
5306 gdb_byte *buffer;
5307 CORE_ADDR marker;
5308 int low_set;
5309 CORE_ADDR low = 0;
5310 CORE_ADDR high = 0;
ff013f42 5311 CORE_ADDR baseaddr;
43039443 5312
d00adf39
DE
5313 found_base = cu->base_known;
5314 base = cu->base_address;
43039443 5315
be391dca 5316 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5317 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5318 {
5319 complaint (&symfile_complaints,
5320 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5321 offset);
5322 return 0;
5323 }
dce234bc 5324 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5325
5326 /* Read in the largest possible address. */
5327 marker = read_address (obfd, buffer, cu, &dummy);
5328 if ((marker & mask) == mask)
5329 {
5330 /* If we found the largest possible address, then
5331 read the base address. */
5332 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5333 buffer += 2 * addr_size;
5334 offset += 2 * addr_size;
5335 found_base = 1;
5336 }
5337
5338 low_set = 0;
5339
e7030f15 5340 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5341
43039443
JK
5342 while (1)
5343 {
5344 CORE_ADDR range_beginning, range_end;
5345
5346 range_beginning = read_address (obfd, buffer, cu, &dummy);
5347 buffer += addr_size;
5348 range_end = read_address (obfd, buffer, cu, &dummy);
5349 buffer += addr_size;
5350 offset += 2 * addr_size;
5351
5352 /* An end of list marker is a pair of zero addresses. */
5353 if (range_beginning == 0 && range_end == 0)
5354 /* Found the end of list entry. */
5355 break;
5356
5357 /* Each base address selection entry is a pair of 2 values.
5358 The first is the largest possible address, the second is
5359 the base address. Check for a base address here. */
5360 if ((range_beginning & mask) == mask)
5361 {
5362 /* If we found the largest possible address, then
5363 read the base address. */
5364 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5365 found_base = 1;
5366 continue;
5367 }
5368
5369 if (!found_base)
5370 {
5371 /* We have no valid base address for the ranges
5372 data. */
5373 complaint (&symfile_complaints,
5374 _("Invalid .debug_ranges data (no base address)"));
5375 return 0;
5376 }
5377
5378 range_beginning += base;
5379 range_end += base;
5380
ff013f42
JK
5381 if (ranges_pst != NULL && range_beginning < range_end)
5382 addrmap_set_empty (objfile->psymtabs_addrmap,
5383 range_beginning + baseaddr, range_end - 1 + baseaddr,
5384 ranges_pst);
5385
43039443
JK
5386 /* FIXME: This is recording everything as a low-high
5387 segment of consecutive addresses. We should have a
5388 data structure for discontiguous block ranges
5389 instead. */
5390 if (! low_set)
5391 {
5392 low = range_beginning;
5393 high = range_end;
5394 low_set = 1;
5395 }
5396 else
5397 {
5398 if (range_beginning < low)
5399 low = range_beginning;
5400 if (range_end > high)
5401 high = range_end;
5402 }
5403 }
5404
5405 if (! low_set)
5406 /* If the first entry is an end-of-list marker, the range
5407 describes an empty scope, i.e. no instructions. */
5408 return 0;
5409
5410 if (low_return)
5411 *low_return = low;
5412 if (high_return)
5413 *high_return = high;
5414 return 1;
5415}
5416
af34e669
DJ
5417/* Get low and high pc attributes from a die. Return 1 if the attributes
5418 are present and valid, otherwise, return 0. Return -1 if the range is
5419 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5420static int
af34e669 5421dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5422 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5423 struct partial_symtab *pst)
c906108c
SS
5424{
5425 struct attribute *attr;
af34e669
DJ
5426 CORE_ADDR low = 0;
5427 CORE_ADDR high = 0;
5428 int ret = 0;
c906108c 5429
e142c38c 5430 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5431 if (attr)
af34e669
DJ
5432 {
5433 high = DW_ADDR (attr);
e142c38c 5434 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5435 if (attr)
5436 low = DW_ADDR (attr);
5437 else
5438 /* Found high w/o low attribute. */
5439 return 0;
5440
5441 /* Found consecutive range of addresses. */
5442 ret = 1;
5443 }
c906108c 5444 else
af34e669 5445 {
e142c38c 5446 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5447 if (attr != NULL)
5448 {
af34e669 5449 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5450 .debug_ranges section. */
d85a05f0 5451 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5452 return 0;
43039443 5453 /* Found discontinuous range of addresses. */
af34e669
DJ
5454 ret = -1;
5455 }
5456 }
c906108c
SS
5457
5458 if (high < low)
5459 return 0;
5460
5461 /* When using the GNU linker, .gnu.linkonce. sections are used to
5462 eliminate duplicate copies of functions and vtables and such.
5463 The linker will arbitrarily choose one and discard the others.
5464 The AT_*_pc values for such functions refer to local labels in
5465 these sections. If the section from that file was discarded, the
5466 labels are not in the output, so the relocs get a value of 0.
5467 If this is a discarded function, mark the pc bounds as invalid,
5468 so that GDB will ignore it. */
72dca2f5 5469 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5470 return 0;
5471
5472 *lowpc = low;
5473 *highpc = high;
af34e669 5474 return ret;
c906108c
SS
5475}
5476
b084d499
JB
5477/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5478 its low and high PC addresses. Do nothing if these addresses could not
5479 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5480 and HIGHPC to the high address if greater than HIGHPC. */
5481
5482static void
5483dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5484 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5485 struct dwarf2_cu *cu)
5486{
5487 CORE_ADDR low, high;
5488 struct die_info *child = die->child;
5489
d85a05f0 5490 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5491 {
5492 *lowpc = min (*lowpc, low);
5493 *highpc = max (*highpc, high);
5494 }
5495
5496 /* If the language does not allow nested subprograms (either inside
5497 subprograms or lexical blocks), we're done. */
5498 if (cu->language != language_ada)
5499 return;
6e70227d 5500
b084d499
JB
5501 /* Check all the children of the given DIE. If it contains nested
5502 subprograms, then check their pc bounds. Likewise, we need to
5503 check lexical blocks as well, as they may also contain subprogram
5504 definitions. */
5505 while (child && child->tag)
5506 {
5507 if (child->tag == DW_TAG_subprogram
5508 || child->tag == DW_TAG_lexical_block)
5509 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5510 child = sibling_die (child);
5511 }
5512}
5513
fae299cd
DC
5514/* Get the low and high pc's represented by the scope DIE, and store
5515 them in *LOWPC and *HIGHPC. If the correct values can't be
5516 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5517
5518static void
5519get_scope_pc_bounds (struct die_info *die,
5520 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5521 struct dwarf2_cu *cu)
5522{
5523 CORE_ADDR best_low = (CORE_ADDR) -1;
5524 CORE_ADDR best_high = (CORE_ADDR) 0;
5525 CORE_ADDR current_low, current_high;
5526
d85a05f0 5527 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5528 {
5529 best_low = current_low;
5530 best_high = current_high;
5531 }
5532 else
5533 {
5534 struct die_info *child = die->child;
5535
5536 while (child && child->tag)
5537 {
5538 switch (child->tag) {
5539 case DW_TAG_subprogram:
b084d499 5540 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
5541 break;
5542 case DW_TAG_namespace:
f55ee35c 5543 case DW_TAG_module:
fae299cd
DC
5544 /* FIXME: carlton/2004-01-16: Should we do this for
5545 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5546 that current GCC's always emit the DIEs corresponding
5547 to definitions of methods of classes as children of a
5548 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5549 the DIEs giving the declarations, which could be
5550 anywhere). But I don't see any reason why the
5551 standards says that they have to be there. */
5552 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5553
5554 if (current_low != ((CORE_ADDR) -1))
5555 {
5556 best_low = min (best_low, current_low);
5557 best_high = max (best_high, current_high);
5558 }
5559 break;
5560 default:
5561 /* Ignore. */
5562 break;
5563 }
5564
5565 child = sibling_die (child);
5566 }
5567 }
5568
5569 *lowpc = best_low;
5570 *highpc = best_high;
5571}
5572
801e3a5b
JB
5573/* Record the address ranges for BLOCK, offset by BASEADDR, as given
5574 in DIE. */
5575static void
5576dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5577 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5578{
5579 struct attribute *attr;
5580
5581 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5582 if (attr)
5583 {
5584 CORE_ADDR high = DW_ADDR (attr);
9a619af0 5585
801e3a5b
JB
5586 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5587 if (attr)
5588 {
5589 CORE_ADDR low = DW_ADDR (attr);
9a619af0 5590
801e3a5b
JB
5591 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5592 }
5593 }
5594
5595 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5596 if (attr)
5597 {
5598 bfd *obfd = cu->objfile->obfd;
5599
5600 /* The value of the DW_AT_ranges attribute is the offset of the
5601 address range list in the .debug_ranges section. */
5602 unsigned long offset = DW_UNSND (attr);
dce234bc 5603 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
5604
5605 /* For some target architectures, but not others, the
5606 read_address function sign-extends the addresses it returns.
5607 To recognize base address selection entries, we need a
5608 mask. */
5609 unsigned int addr_size = cu->header.addr_size;
5610 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5611
5612 /* The base address, to which the next pair is relative. Note
5613 that this 'base' is a DWARF concept: most entries in a range
5614 list are relative, to reduce the number of relocs against the
5615 debugging information. This is separate from this function's
5616 'baseaddr' argument, which GDB uses to relocate debugging
5617 information from a shared library based on the address at
5618 which the library was loaded. */
d00adf39
DE
5619 CORE_ADDR base = cu->base_address;
5620 int base_known = cu->base_known;
801e3a5b 5621
be391dca 5622 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 5623 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
5624 {
5625 complaint (&symfile_complaints,
5626 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5627 offset);
5628 return;
5629 }
5630
5631 for (;;)
5632 {
5633 unsigned int bytes_read;
5634 CORE_ADDR start, end;
5635
5636 start = read_address (obfd, buffer, cu, &bytes_read);
5637 buffer += bytes_read;
5638 end = read_address (obfd, buffer, cu, &bytes_read);
5639 buffer += bytes_read;
5640
5641 /* Did we find the end of the range list? */
5642 if (start == 0 && end == 0)
5643 break;
5644
5645 /* Did we find a base address selection entry? */
5646 else if ((start & base_select_mask) == base_select_mask)
5647 {
5648 base = end;
5649 base_known = 1;
5650 }
5651
5652 /* We found an ordinary address range. */
5653 else
5654 {
5655 if (!base_known)
5656 {
5657 complaint (&symfile_complaints,
5658 _("Invalid .debug_ranges data (no base address)"));
5659 return;
5660 }
5661
6e70227d
DE
5662 record_block_range (block,
5663 baseaddr + base + start,
801e3a5b
JB
5664 baseaddr + base + end - 1);
5665 }
5666 }
5667 }
5668}
5669
c906108c
SS
5670/* Add an aggregate field to the field list. */
5671
5672static void
107d2387 5673dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 5674 struct dwarf2_cu *cu)
6e70227d 5675{
e7c27a73 5676 struct objfile *objfile = cu->objfile;
5e2b427d 5677 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
5678 struct nextfield *new_field;
5679 struct attribute *attr;
5680 struct field *fp;
5681 char *fieldname = "";
5682
5683 /* Allocate a new field list entry and link it in. */
5684 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 5685 make_cleanup (xfree, new_field);
c906108c 5686 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
5687
5688 if (die->tag == DW_TAG_inheritance)
5689 {
5690 new_field->next = fip->baseclasses;
5691 fip->baseclasses = new_field;
5692 }
5693 else
5694 {
5695 new_field->next = fip->fields;
5696 fip->fields = new_field;
5697 }
c906108c
SS
5698 fip->nfields++;
5699
5700 /* Handle accessibility and virtuality of field.
5701 The default accessibility for members is public, the default
5702 accessibility for inheritance is private. */
5703 if (die->tag != DW_TAG_inheritance)
5704 new_field->accessibility = DW_ACCESS_public;
5705 else
5706 new_field->accessibility = DW_ACCESS_private;
5707 new_field->virtuality = DW_VIRTUALITY_none;
5708
e142c38c 5709 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
5710 if (attr)
5711 new_field->accessibility = DW_UNSND (attr);
5712 if (new_field->accessibility != DW_ACCESS_public)
5713 fip->non_public_fields = 1;
e142c38c 5714 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
5715 if (attr)
5716 new_field->virtuality = DW_UNSND (attr);
5717
5718 fp = &new_field->field;
a9a9bd0f 5719
e142c38c 5720 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 5721 {
a9a9bd0f 5722 /* Data member other than a C++ static data member. */
6e70227d 5723
c906108c 5724 /* Get type of field. */
e7c27a73 5725 fp->type = die_type (die, cu);
c906108c 5726
d6a843b5 5727 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 5728
c906108c 5729 /* Get bit size of field (zero if none). */
e142c38c 5730 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
5731 if (attr)
5732 {
5733 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
5734 }
5735 else
5736 {
5737 FIELD_BITSIZE (*fp) = 0;
5738 }
5739
5740 /* Get bit offset of field. */
e142c38c 5741 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
5742 if (attr)
5743 {
d4b96c9a 5744 int byte_offset = 0;
c6a0999f 5745
3690dd37 5746 if (attr_form_is_section_offset (attr))
d4b96c9a 5747 dwarf2_complex_location_expr_complaint ();
3690dd37 5748 else if (attr_form_is_constant (attr))
c6a0999f 5749 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 5750 else if (attr_form_is_block (attr))
c6a0999f 5751 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
5752 else
5753 dwarf2_complex_location_expr_complaint ();
c6a0999f 5754
d6a843b5 5755 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 5756 }
e142c38c 5757 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
5758 if (attr)
5759 {
5e2b427d 5760 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
5761 {
5762 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
5763 additional bit offset from the MSB of the containing
5764 anonymous object to the MSB of the field. We don't
5765 have to do anything special since we don't need to
5766 know the size of the anonymous object. */
c906108c
SS
5767 FIELD_BITPOS (*fp) += DW_UNSND (attr);
5768 }
5769 else
5770 {
5771 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
5772 MSB of the anonymous object, subtract off the number of
5773 bits from the MSB of the field to the MSB of the
5774 object, and then subtract off the number of bits of
5775 the field itself. The result is the bit offset of
5776 the LSB of the field. */
c906108c
SS
5777 int anonymous_size;
5778 int bit_offset = DW_UNSND (attr);
5779
e142c38c 5780 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
5781 if (attr)
5782 {
5783 /* The size of the anonymous object containing
5784 the bit field is explicit, so use the
5785 indicated size (in bytes). */
5786 anonymous_size = DW_UNSND (attr);
5787 }
5788 else
5789 {
5790 /* The size of the anonymous object containing
5791 the bit field must be inferred from the type
5792 attribute of the data member containing the
5793 bit field. */
5794 anonymous_size = TYPE_LENGTH (fp->type);
5795 }
5796 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
5797 - bit_offset - FIELD_BITSIZE (*fp);
5798 }
5799 }
5800
5801 /* Get name of field. */
39cbfefa
DJ
5802 fieldname = dwarf2_name (die, cu);
5803 if (fieldname == NULL)
5804 fieldname = "";
d8151005
DJ
5805
5806 /* The name is already allocated along with this objfile, so we don't
5807 need to duplicate it for the type. */
5808 fp->name = fieldname;
c906108c
SS
5809
5810 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 5811 pointer or virtual base class pointer) to private. */
e142c38c 5812 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 5813 {
d48cc9dd 5814 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
5815 new_field->accessibility = DW_ACCESS_private;
5816 fip->non_public_fields = 1;
5817 }
5818 }
a9a9bd0f 5819 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 5820 {
a9a9bd0f
DC
5821 /* C++ static member. */
5822
5823 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
5824 is a declaration, but all versions of G++ as of this writing
5825 (so through at least 3.2.1) incorrectly generate
5826 DW_TAG_variable tags. */
6e70227d 5827
c906108c 5828 char *physname;
c906108c 5829
a9a9bd0f 5830 /* Get name of field. */
39cbfefa
DJ
5831 fieldname = dwarf2_name (die, cu);
5832 if (fieldname == NULL)
c906108c
SS
5833 return;
5834
254e6b9e 5835 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
5836 if (attr
5837 /* Only create a symbol if this is an external value.
5838 new_symbol checks this and puts the value in the global symbol
5839 table, which we want. If it is not external, new_symbol
5840 will try to put the value in cu->list_in_scope which is wrong. */
5841 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
5842 {
5843 /* A static const member, not much different than an enum as far as
5844 we're concerned, except that we can support more types. */
5845 new_symbol (die, NULL, cu);
5846 }
5847
2df3850c 5848 /* Get physical name. */
94af9270 5849 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 5850
d8151005
DJ
5851 /* The name is already allocated along with this objfile, so we don't
5852 need to duplicate it for the type. */
5853 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 5854 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 5855 FIELD_NAME (*fp) = fieldname;
c906108c
SS
5856 }
5857 else if (die->tag == DW_TAG_inheritance)
5858 {
5859 /* C++ base class field. */
e142c38c 5860 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 5861 if (attr)
d4b96c9a
JK
5862 {
5863 int byte_offset = 0;
5864
5865 if (attr_form_is_section_offset (attr))
5866 dwarf2_complex_location_expr_complaint ();
5867 else if (attr_form_is_constant (attr))
5868 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
5869 else if (attr_form_is_block (attr))
5870 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
5871 else
5872 dwarf2_complex_location_expr_complaint ();
5873
5874 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
5875 }
c906108c 5876 FIELD_BITSIZE (*fp) = 0;
e7c27a73 5877 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
5878 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
5879 fip->nbaseclasses++;
5880 }
5881}
5882
98751a41
JK
5883/* Add a typedef defined in the scope of the FIP's class. */
5884
5885static void
5886dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
5887 struct dwarf2_cu *cu)
6e70227d 5888{
98751a41
JK
5889 struct objfile *objfile = cu->objfile;
5890 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5891 struct typedef_field_list *new_field;
5892 struct attribute *attr;
5893 struct typedef_field *fp;
5894 char *fieldname = "";
5895
5896 /* Allocate a new field list entry and link it in. */
5897 new_field = xzalloc (sizeof (*new_field));
5898 make_cleanup (xfree, new_field);
5899
5900 gdb_assert (die->tag == DW_TAG_typedef);
5901
5902 fp = &new_field->field;
5903
5904 /* Get name of field. */
5905 fp->name = dwarf2_name (die, cu);
5906 if (fp->name == NULL)
5907 return;
5908
5909 fp->type = read_type_die (die, cu);
5910
5911 new_field->next = fip->typedef_field_list;
5912 fip->typedef_field_list = new_field;
5913 fip->typedef_field_list_count++;
5914}
5915
c906108c
SS
5916/* Create the vector of fields, and attach it to the type. */
5917
5918static void
fba45db2 5919dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 5920 struct dwarf2_cu *cu)
c906108c
SS
5921{
5922 int nfields = fip->nfields;
5923
5924 /* Record the field count, allocate space for the array of fields,
5925 and create blank accessibility bitfields if necessary. */
5926 TYPE_NFIELDS (type) = nfields;
5927 TYPE_FIELDS (type) = (struct field *)
5928 TYPE_ALLOC (type, sizeof (struct field) * nfields);
5929 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
5930
b4ba55a1 5931 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
5932 {
5933 ALLOCATE_CPLUS_STRUCT_TYPE (type);
5934
5935 TYPE_FIELD_PRIVATE_BITS (type) =
5936 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5937 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
5938
5939 TYPE_FIELD_PROTECTED_BITS (type) =
5940 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5941 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
5942
5943 TYPE_FIELD_IGNORE_BITS (type) =
5944 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5945 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
5946 }
5947
5948 /* If the type has baseclasses, allocate and clear a bit vector for
5949 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 5950 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
5951 {
5952 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 5953 unsigned char *pointer;
c906108c
SS
5954
5955 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
5956 pointer = TYPE_ALLOC (type, num_bytes);
5957 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
5958 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
5959 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
5960 }
5961
5962 /* Copy the saved-up fields into the field vector. Start from the head
5963 of the list, adding to the tail of the field array, so that they end
5964 up in the same order in the array in which they were added to the list. */
5965 while (nfields-- > 0)
5966 {
7d0ccb61
DJ
5967 struct nextfield *fieldp;
5968
5969 if (fip->fields)
5970 {
5971 fieldp = fip->fields;
5972 fip->fields = fieldp->next;
5973 }
5974 else
5975 {
5976 fieldp = fip->baseclasses;
5977 fip->baseclasses = fieldp->next;
5978 }
5979
5980 TYPE_FIELD (type, nfields) = fieldp->field;
5981 switch (fieldp->accessibility)
c906108c 5982 {
c5aa993b 5983 case DW_ACCESS_private:
b4ba55a1
JB
5984 if (cu->language != language_ada)
5985 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 5986 break;
c906108c 5987
c5aa993b 5988 case DW_ACCESS_protected:
b4ba55a1
JB
5989 if (cu->language != language_ada)
5990 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 5991 break;
c906108c 5992
c5aa993b
JM
5993 case DW_ACCESS_public:
5994 break;
c906108c 5995
c5aa993b
JM
5996 default:
5997 /* Unknown accessibility. Complain and treat it as public. */
5998 {
e2e0b3e5 5999 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6000 fieldp->accessibility);
c5aa993b
JM
6001 }
6002 break;
c906108c
SS
6003 }
6004 if (nfields < fip->nbaseclasses)
6005 {
7d0ccb61 6006 switch (fieldp->virtuality)
c906108c 6007 {
c5aa993b
JM
6008 case DW_VIRTUALITY_virtual:
6009 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
6010 if (cu->language == language_ada)
6011 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
6012 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6013 break;
c906108c
SS
6014 }
6015 }
c906108c
SS
6016 }
6017}
6018
c906108c
SS
6019/* Add a member function to the proper fieldlist. */
6020
6021static void
107d2387 6022dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6023 struct type *type, struct dwarf2_cu *cu)
c906108c 6024{
e7c27a73 6025 struct objfile *objfile = cu->objfile;
c906108c
SS
6026 struct attribute *attr;
6027 struct fnfieldlist *flp;
6028 int i;
6029 struct fn_field *fnp;
6030 char *fieldname;
6031 char *physname;
6032 struct nextfnfield *new_fnfield;
f792889a 6033 struct type *this_type;
c906108c 6034
b4ba55a1
JB
6035 if (cu->language == language_ada)
6036 error ("unexpected member function in Ada type");
6037
2df3850c 6038 /* Get name of member function. */
39cbfefa
DJ
6039 fieldname = dwarf2_name (die, cu);
6040 if (fieldname == NULL)
2df3850c 6041 return;
c906108c 6042
2df3850c 6043 /* Get the mangled name. */
94af9270 6044 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c
SS
6045
6046 /* Look up member function name in fieldlist. */
6047 for (i = 0; i < fip->nfnfields; i++)
6048 {
27bfe10e 6049 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6050 break;
6051 }
6052
6053 /* Create new list element if necessary. */
6054 if (i < fip->nfnfields)
6055 flp = &fip->fnfieldlists[i];
6056 else
6057 {
6058 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6059 {
6060 fip->fnfieldlists = (struct fnfieldlist *)
6061 xrealloc (fip->fnfieldlists,
6062 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6063 * sizeof (struct fnfieldlist));
c906108c 6064 if (fip->nfnfields == 0)
c13c43fd 6065 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6066 }
6067 flp = &fip->fnfieldlists[fip->nfnfields];
6068 flp->name = fieldname;
6069 flp->length = 0;
6070 flp->head = NULL;
6071 fip->nfnfields++;
6072 }
6073
6074 /* Create a new member function field and chain it to the field list
6075 entry. */
6076 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6077 make_cleanup (xfree, new_fnfield);
c906108c
SS
6078 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6079 new_fnfield->next = flp->head;
6080 flp->head = new_fnfield;
6081 flp->length++;
6082
6083 /* Fill in the member function field info. */
6084 fnp = &new_fnfield->fnfield;
d8151005
DJ
6085 /* The name is already allocated along with this objfile, so we don't
6086 need to duplicate it for the type. */
6087 fnp->physname = physname ? physname : "";
c906108c 6088 fnp->type = alloc_type (objfile);
f792889a
DJ
6089 this_type = read_type_die (die, cu);
6090 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6091 {
f792889a 6092 int nparams = TYPE_NFIELDS (this_type);
c906108c 6093
f792889a 6094 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6095 of the method itself (TYPE_CODE_METHOD). */
6096 smash_to_method_type (fnp->type, type,
f792889a
DJ
6097 TYPE_TARGET_TYPE (this_type),
6098 TYPE_FIELDS (this_type),
6099 TYPE_NFIELDS (this_type),
6100 TYPE_VARARGS (this_type));
c906108c
SS
6101
6102 /* Handle static member functions.
c5aa993b
JM
6103 Dwarf2 has no clean way to discern C++ static and non-static
6104 member functions. G++ helps GDB by marking the first
6105 parameter for non-static member functions (which is the
6106 this pointer) as artificial. We obtain this information
6107 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6108 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6109 fnp->voffset = VOFFSET_STATIC;
6110 }
6111 else
e2e0b3e5 6112 complaint (&symfile_complaints, _("member function type missing for '%s'"),
4d3c2250 6113 physname);
c906108c
SS
6114
6115 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6116 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6117 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
6118
6119 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6120 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6121
6122 /* Get accessibility. */
e142c38c 6123 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6124 if (attr)
6125 {
6126 switch (DW_UNSND (attr))
6127 {
c5aa993b
JM
6128 case DW_ACCESS_private:
6129 fnp->is_private = 1;
6130 break;
6131 case DW_ACCESS_protected:
6132 fnp->is_protected = 1;
6133 break;
c906108c
SS
6134 }
6135 }
6136
b02dede2 6137 /* Check for artificial methods. */
e142c38c 6138 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6139 if (attr && DW_UNSND (attr) != 0)
6140 fnp->is_artificial = 1;
6141
0d564a31 6142 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6143 function. For older versions of GCC, this is an offset in the
6144 appropriate virtual table, as specified by DW_AT_containing_type.
6145 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6146 to the object address. */
6147
e142c38c 6148 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6149 if (attr)
8e19ed76 6150 {
aec5aa8b 6151 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6152 {
aec5aa8b
TT
6153 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6154 {
6155 /* Old-style GCC. */
6156 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6157 }
6158 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6159 || (DW_BLOCK (attr)->size > 1
6160 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6161 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6162 {
6163 struct dwarf_block blk;
6164 int offset;
6165
6166 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6167 ? 1 : 2);
6168 blk.size = DW_BLOCK (attr)->size - offset;
6169 blk.data = DW_BLOCK (attr)->data + offset;
6170 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6171 if ((fnp->voffset % cu->header.addr_size) != 0)
6172 dwarf2_complex_location_expr_complaint ();
6173 else
6174 fnp->voffset /= cu->header.addr_size;
6175 fnp->voffset += 2;
6176 }
6177 else
6178 dwarf2_complex_location_expr_complaint ();
6179
6180 if (!fnp->fcontext)
6181 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6182 }
3690dd37 6183 else if (attr_form_is_section_offset (attr))
8e19ed76 6184 {
4d3c2250 6185 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6186 }
6187 else
6188 {
4d3c2250
KB
6189 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6190 fieldname);
8e19ed76 6191 }
0d564a31 6192 }
d48cc9dd
DJ
6193 else
6194 {
6195 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6196 if (attr && DW_UNSND (attr))
6197 {
6198 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6199 complaint (&symfile_complaints,
6200 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6201 fieldname, die->offset);
9655fd1a 6202 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6203 TYPE_CPLUS_DYNAMIC (type) = 1;
6204 }
6205 }
c906108c
SS
6206}
6207
6208/* Create the vector of member function fields, and attach it to the type. */
6209
6210static void
fba45db2 6211dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6212 struct dwarf2_cu *cu)
c906108c
SS
6213{
6214 struct fnfieldlist *flp;
6215 int total_length = 0;
6216 int i;
6217
b4ba55a1
JB
6218 if (cu->language == language_ada)
6219 error ("unexpected member functions in Ada type");
6220
c906108c
SS
6221 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6222 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6223 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6224
6225 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6226 {
6227 struct nextfnfield *nfp = flp->head;
6228 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6229 int k;
6230
6231 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6232 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6233 fn_flp->fn_fields = (struct fn_field *)
6234 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6235 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6236 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6237
6238 total_length += flp->length;
6239 }
6240
6241 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6242 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6243}
6244
1168df01
JB
6245/* Returns non-zero if NAME is the name of a vtable member in CU's
6246 language, zero otherwise. */
6247static int
6248is_vtable_name (const char *name, struct dwarf2_cu *cu)
6249{
6250 static const char vptr[] = "_vptr";
987504bb 6251 static const char vtable[] = "vtable";
1168df01 6252
987504bb
JJ
6253 /* Look for the C++ and Java forms of the vtable. */
6254 if ((cu->language == language_java
6255 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6256 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6257 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6258 return 1;
6259
6260 return 0;
6261}
6262
c0dd20ea 6263/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6264 functions, with the ABI-specified layout. If TYPE describes
6265 such a structure, smash it into a member function type.
61049d3b
DJ
6266
6267 GCC shouldn't do this; it should just output pointer to member DIEs.
6268 This is GCC PR debug/28767. */
c0dd20ea 6269
0b92b5bb
TT
6270static void
6271quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6272{
0b92b5bb 6273 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6274
6275 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6276 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6277 return;
c0dd20ea
DJ
6278
6279 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6280 if (TYPE_FIELD_NAME (type, 0) == NULL
6281 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6282 || TYPE_FIELD_NAME (type, 1) == NULL
6283 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6284 return;
c0dd20ea
DJ
6285
6286 /* Find the type of the method. */
0b92b5bb 6287 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6288 if (pfn_type == NULL
6289 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6290 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6291 return;
c0dd20ea
DJ
6292
6293 /* Look for the "this" argument. */
6294 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6295 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6296 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6297 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6298 return;
c0dd20ea
DJ
6299
6300 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6301 new_type = alloc_type (objfile);
6302 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6303 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6304 TYPE_VARARGS (pfn_type));
0b92b5bb 6305 smash_to_methodptr_type (type, new_type);
c0dd20ea 6306}
1168df01 6307
c906108c
SS
6308/* Called when we find the DIE that starts a structure or union scope
6309 (definition) to process all dies that define the members of the
6310 structure or union.
6311
6312 NOTE: we need to call struct_type regardless of whether or not the
6313 DIE has an at_name attribute, since it might be an anonymous
6314 structure or union. This gets the type entered into our set of
6315 user defined types.
6316
6317 However, if the structure is incomplete (an opaque struct/union)
6318 then suppress creating a symbol table entry for it since gdb only
6319 wants to find the one with the complete definition. Note that if
6320 it is complete, we just call new_symbol, which does it's own
6321 checking about whether the struct/union is anonymous or not (and
6322 suppresses creating a symbol table entry itself). */
6323
f792889a 6324static struct type *
134d01f1 6325read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6326{
e7c27a73 6327 struct objfile *objfile = cu->objfile;
c906108c
SS
6328 struct type *type;
6329 struct attribute *attr;
39cbfefa 6330 char *name;
d3f41bb1 6331 struct cleanup *back_to;
c906108c 6332
348e048f
DE
6333 /* If the definition of this type lives in .debug_types, read that type.
6334 Don't follow DW_AT_specification though, that will take us back up
6335 the chain and we want to go down. */
6336 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6337 if (attr)
6338 {
6339 struct dwarf2_cu *type_cu = cu;
6340 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6341
348e048f
DE
6342 /* We could just recurse on read_structure_type, but we need to call
6343 get_die_type to ensure only one type for this DIE is created.
6344 This is important, for example, because for c++ classes we need
6345 TYPE_NAME set which is only done by new_symbol. Blech. */
6346 type = read_type_die (type_die, type_cu);
6347 return set_die_type (die, type, cu);
6348 }
6349
d3f41bb1
TT
6350 back_to = make_cleanup (null_cleanup, 0);
6351
c0dd20ea 6352 type = alloc_type (objfile);
c906108c 6353 INIT_CPLUS_SPECIFIC (type);
93311388 6354
39cbfefa
DJ
6355 name = dwarf2_name (die, cu);
6356 if (name != NULL)
c906108c 6357 {
987504bb
JJ
6358 if (cu->language == language_cplus
6359 || cu->language == language_java)
63d06c5c 6360 {
94af9270
KS
6361 TYPE_TAG_NAME (type) = (char *) dwarf2_full_name (name, die, cu);
6362 if (die->tag == DW_TAG_structure_type
6363 || die->tag == DW_TAG_class_type)
6364 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6365 }
6366 else
6367 {
d8151005
DJ
6368 /* The name is already allocated along with this objfile, so
6369 we don't need to duplicate it for the type. */
94af9270
KS
6370 TYPE_TAG_NAME (type) = (char *) name;
6371 if (die->tag == DW_TAG_class_type)
6372 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6373 }
c906108c
SS
6374 }
6375
6376 if (die->tag == DW_TAG_structure_type)
6377 {
6378 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6379 }
6380 else if (die->tag == DW_TAG_union_type)
6381 {
6382 TYPE_CODE (type) = TYPE_CODE_UNION;
6383 }
6384 else
6385 {
c906108c
SS
6386 TYPE_CODE (type) = TYPE_CODE_CLASS;
6387 }
6388
0cc2414c
TT
6389 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6390 TYPE_DECLARED_CLASS (type) = 1;
6391
e142c38c 6392 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6393 if (attr)
6394 {
6395 TYPE_LENGTH (type) = DW_UNSND (attr);
6396 }
6397 else
6398 {
6399 TYPE_LENGTH (type) = 0;
6400 }
6401
876cecd0 6402 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6403 if (die_is_declaration (die, cu))
876cecd0 6404 TYPE_STUB (type) = 1;
a6c727b2
DJ
6405 else if (attr == NULL && die->child == NULL
6406 && producer_is_realview (cu->producer))
6407 /* RealView does not output the required DW_AT_declaration
6408 on incomplete types. */
6409 TYPE_STUB (type) = 1;
dc718098 6410
c906108c
SS
6411 /* We need to add the type field to the die immediately so we don't
6412 infinitely recurse when dealing with pointers to the structure
6413 type within the structure itself. */
1c379e20 6414 set_die_type (die, type, cu);
c906108c 6415
7e314c57
JK
6416 /* set_die_type should be already done. */
6417 set_descriptive_type (type, die, cu);
6418
e142c38c 6419 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6420 {
6421 struct field_info fi;
6422 struct die_info *child_die;
34eaf542 6423 VEC (symbolp) *template_args = NULL;
c906108c
SS
6424
6425 memset (&fi, 0, sizeof (struct field_info));
6426
639d11d3 6427 child_die = die->child;
c906108c
SS
6428
6429 while (child_die && child_die->tag)
6430 {
a9a9bd0f
DC
6431 if (child_die->tag == DW_TAG_member
6432 || child_die->tag == DW_TAG_variable)
c906108c 6433 {
a9a9bd0f
DC
6434 /* NOTE: carlton/2002-11-05: A C++ static data member
6435 should be a DW_TAG_member that is a declaration, but
6436 all versions of G++ as of this writing (so through at
6437 least 3.2.1) incorrectly generate DW_TAG_variable
6438 tags for them instead. */
e7c27a73 6439 dwarf2_add_field (&fi, child_die, cu);
c906108c 6440 }
8713b1b1 6441 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6442 {
6443 /* C++ member function. */
e7c27a73 6444 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6445 }
6446 else if (child_die->tag == DW_TAG_inheritance)
6447 {
6448 /* C++ base class field. */
e7c27a73 6449 dwarf2_add_field (&fi, child_die, cu);
c906108c 6450 }
98751a41
JK
6451 else if (child_die->tag == DW_TAG_typedef)
6452 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6453 else if (child_die->tag == DW_TAG_template_type_param
6454 || child_die->tag == DW_TAG_template_value_param)
6455 {
6456 struct symbol *arg = new_symbol (child_die, NULL, cu);
6457
6458 VEC_safe_push (symbolp, template_args, arg);
6459 }
6460
c906108c
SS
6461 child_die = sibling_die (child_die);
6462 }
6463
34eaf542
TT
6464 /* Attach template arguments to type. */
6465 if (! VEC_empty (symbolp, template_args))
6466 {
6467 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6468 TYPE_N_TEMPLATE_ARGUMENTS (type)
6469 = VEC_length (symbolp, template_args);
6470 TYPE_TEMPLATE_ARGUMENTS (type)
6471 = obstack_alloc (&objfile->objfile_obstack,
6472 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6473 * sizeof (struct symbol *)));
6474 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6475 VEC_address (symbolp, template_args),
6476 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6477 * sizeof (struct symbol *)));
6478 VEC_free (symbolp, template_args);
6479 }
6480
c906108c
SS
6481 /* Attach fields and member functions to the type. */
6482 if (fi.nfields)
e7c27a73 6483 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6484 if (fi.nfnfields)
6485 {
e7c27a73 6486 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6487
c5aa993b 6488 /* Get the type which refers to the base class (possibly this
c906108c 6489 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6490 class from the DW_AT_containing_type attribute. This use of
6491 DW_AT_containing_type is a GNU extension. */
c906108c 6492
e142c38c 6493 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6494 {
e7c27a73 6495 struct type *t = die_containing_type (die, cu);
c906108c
SS
6496
6497 TYPE_VPTR_BASETYPE (type) = t;
6498 if (type == t)
6499 {
c906108c
SS
6500 int i;
6501
6502 /* Our own class provides vtbl ptr. */
6503 for (i = TYPE_NFIELDS (t) - 1;
6504 i >= TYPE_N_BASECLASSES (t);
6505 --i)
6506 {
6507 char *fieldname = TYPE_FIELD_NAME (t, i);
6508
1168df01 6509 if (is_vtable_name (fieldname, cu))
c906108c
SS
6510 {
6511 TYPE_VPTR_FIELDNO (type) = i;
6512 break;
6513 }
6514 }
6515
6516 /* Complain if virtual function table field not found. */
6517 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6518 complaint (&symfile_complaints,
e2e0b3e5 6519 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6520 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6521 "");
c906108c
SS
6522 }
6523 else
6524 {
6525 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6526 }
6527 }
f6235d4c
EZ
6528 else if (cu->producer
6529 && strncmp (cu->producer,
6530 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6531 {
6532 /* The IBM XLC compiler does not provide direct indication
6533 of the containing type, but the vtable pointer is
6534 always named __vfp. */
6535
6536 int i;
6537
6538 for (i = TYPE_NFIELDS (type) - 1;
6539 i >= TYPE_N_BASECLASSES (type);
6540 --i)
6541 {
6542 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6543 {
6544 TYPE_VPTR_FIELDNO (type) = i;
6545 TYPE_VPTR_BASETYPE (type) = type;
6546 break;
6547 }
6548 }
6549 }
c906108c 6550 }
98751a41
JK
6551
6552 /* Copy fi.typedef_field_list linked list elements content into the
6553 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6554 if (fi.typedef_field_list)
6555 {
6556 int i = fi.typedef_field_list_count;
6557
a0d7a4ff 6558 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6559 TYPE_TYPEDEF_FIELD_ARRAY (type)
6560 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6561 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6562
6563 /* Reverse the list order to keep the debug info elements order. */
6564 while (--i >= 0)
6565 {
6566 struct typedef_field *dest, *src;
6e70227d 6567
98751a41
JK
6568 dest = &TYPE_TYPEDEF_FIELD (type, i);
6569 src = &fi.typedef_field_list->field;
6570 fi.typedef_field_list = fi.typedef_field_list->next;
6571 *dest = *src;
6572 }
6573 }
c906108c 6574 }
63d06c5c 6575
0b92b5bb
TT
6576 quirk_gcc_member_function_pointer (type, cu->objfile);
6577
0114d602 6578 do_cleanups (back_to);
f792889a 6579 return type;
c906108c
SS
6580}
6581
134d01f1
DJ
6582static void
6583process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6584{
90aeadfc 6585 struct die_info *child_die = die->child;
f792889a 6586 struct type *this_type;
c906108c 6587
f792889a
DJ
6588 this_type = get_die_type (die, cu);
6589 if (this_type == NULL)
6590 this_type = read_structure_type (die, cu);
c906108c 6591
90aeadfc
DC
6592 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6593 snapshots) has been known to create a die giving a declaration
6594 for a class that has, as a child, a die giving a definition for a
6595 nested class. So we have to process our children even if the
6596 current die is a declaration. Normally, of course, a declaration
6597 won't have any children at all. */
134d01f1 6598
90aeadfc
DC
6599 while (child_die != NULL && child_die->tag)
6600 {
6601 if (child_die->tag == DW_TAG_member
6602 || child_die->tag == DW_TAG_variable
34eaf542
TT
6603 || child_die->tag == DW_TAG_inheritance
6604 || child_die->tag == DW_TAG_template_value_param
6605 || child_die->tag == DW_TAG_template_type_param)
134d01f1 6606 {
90aeadfc 6607 /* Do nothing. */
134d01f1 6608 }
90aeadfc
DC
6609 else
6610 process_die (child_die, cu);
134d01f1 6611
90aeadfc 6612 child_die = sibling_die (child_die);
134d01f1
DJ
6613 }
6614
fa4028e9
JB
6615 /* Do not consider external references. According to the DWARF standard,
6616 these DIEs are identified by the fact that they have no byte_size
6617 attribute, and a declaration attribute. */
6618 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6619 || !die_is_declaration (die, cu))
f792889a 6620 new_symbol (die, this_type, cu);
134d01f1
DJ
6621}
6622
6623/* Given a DW_AT_enumeration_type die, set its type. We do not
6624 complete the type's fields yet, or create any symbols. */
c906108c 6625
f792889a 6626static struct type *
134d01f1 6627read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6628{
e7c27a73 6629 struct objfile *objfile = cu->objfile;
c906108c 6630 struct type *type;
c906108c 6631 struct attribute *attr;
0114d602 6632 const char *name;
134d01f1 6633
348e048f
DE
6634 /* If the definition of this type lives in .debug_types, read that type.
6635 Don't follow DW_AT_specification though, that will take us back up
6636 the chain and we want to go down. */
6637 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6638 if (attr)
6639 {
6640 struct dwarf2_cu *type_cu = cu;
6641 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6642
348e048f
DE
6643 type = read_type_die (type_die, type_cu);
6644 return set_die_type (die, type, cu);
6645 }
6646
c906108c
SS
6647 type = alloc_type (objfile);
6648
6649 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 6650 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 6651 if (name != NULL)
0114d602 6652 TYPE_TAG_NAME (type) = (char *) name;
c906108c 6653
e142c38c 6654 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6655 if (attr)
6656 {
6657 TYPE_LENGTH (type) = DW_UNSND (attr);
6658 }
6659 else
6660 {
6661 TYPE_LENGTH (type) = 0;
6662 }
6663
137033e9
JB
6664 /* The enumeration DIE can be incomplete. In Ada, any type can be
6665 declared as private in the package spec, and then defined only
6666 inside the package body. Such types are known as Taft Amendment
6667 Types. When another package uses such a type, an incomplete DIE
6668 may be generated by the compiler. */
02eb380e 6669 if (die_is_declaration (die, cu))
876cecd0 6670 TYPE_STUB (type) = 1;
02eb380e 6671
f792889a 6672 return set_die_type (die, type, cu);
134d01f1
DJ
6673}
6674
6675/* Given a pointer to a die which begins an enumeration, process all
6676 the dies that define the members of the enumeration, and create the
6677 symbol for the enumeration type.
6678
6679 NOTE: We reverse the order of the element list. */
6680
6681static void
6682process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
6683{
134d01f1
DJ
6684 struct die_info *child_die;
6685 struct field *fields;
134d01f1
DJ
6686 struct symbol *sym;
6687 int num_fields;
6688 int unsigned_enum = 1;
39cbfefa 6689 char *name;
f792889a 6690 struct type *this_type;
134d01f1 6691
c906108c
SS
6692 num_fields = 0;
6693 fields = NULL;
f792889a
DJ
6694 this_type = get_die_type (die, cu);
6695 if (this_type == NULL)
6696 this_type = read_enumeration_type (die, cu);
639d11d3 6697 if (die->child != NULL)
c906108c 6698 {
639d11d3 6699 child_die = die->child;
c906108c
SS
6700 while (child_die && child_die->tag)
6701 {
6702 if (child_die->tag != DW_TAG_enumerator)
6703 {
e7c27a73 6704 process_die (child_die, cu);
c906108c
SS
6705 }
6706 else
6707 {
39cbfefa
DJ
6708 name = dwarf2_name (child_die, cu);
6709 if (name)
c906108c 6710 {
f792889a 6711 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
6712 if (SYMBOL_VALUE (sym) < 0)
6713 unsigned_enum = 0;
6714
6715 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
6716 {
6717 fields = (struct field *)
6718 xrealloc (fields,
6719 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6720 * sizeof (struct field));
c906108c
SS
6721 }
6722
3567439c 6723 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 6724 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 6725 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
6726 FIELD_BITSIZE (fields[num_fields]) = 0;
6727
6728 num_fields++;
6729 }
6730 }
6731
6732 child_die = sibling_die (child_die);
6733 }
6734
6735 if (num_fields)
6736 {
f792889a
DJ
6737 TYPE_NFIELDS (this_type) = num_fields;
6738 TYPE_FIELDS (this_type) = (struct field *)
6739 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
6740 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 6741 sizeof (struct field) * num_fields);
b8c9b27d 6742 xfree (fields);
c906108c
SS
6743 }
6744 if (unsigned_enum)
876cecd0 6745 TYPE_UNSIGNED (this_type) = 1;
c906108c 6746 }
134d01f1 6747
f792889a 6748 new_symbol (die, this_type, cu);
c906108c
SS
6749}
6750
6751/* Extract all information from a DW_TAG_array_type DIE and put it in
6752 the DIE's type field. For now, this only handles one dimensional
6753 arrays. */
6754
f792889a 6755static struct type *
e7c27a73 6756read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6757{
e7c27a73 6758 struct objfile *objfile = cu->objfile;
c906108c 6759 struct die_info *child_die;
7e314c57 6760 struct type *type;
c906108c
SS
6761 struct type *element_type, *range_type, *index_type;
6762 struct type **range_types = NULL;
6763 struct attribute *attr;
6764 int ndim = 0;
6765 struct cleanup *back_to;
39cbfefa 6766 char *name;
c906108c 6767
e7c27a73 6768 element_type = die_type (die, cu);
c906108c 6769
7e314c57
JK
6770 /* The die_type call above may have already set the type for this DIE. */
6771 type = get_die_type (die, cu);
6772 if (type)
6773 return type;
6774
c906108c
SS
6775 /* Irix 6.2 native cc creates array types without children for
6776 arrays with unspecified length. */
639d11d3 6777 if (die->child == NULL)
c906108c 6778 {
46bf5051 6779 index_type = objfile_type (objfile)->builtin_int;
c906108c 6780 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
6781 type = create_array_type (NULL, element_type, range_type);
6782 return set_die_type (die, type, cu);
c906108c
SS
6783 }
6784
6785 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 6786 child_die = die->child;
c906108c
SS
6787 while (child_die && child_die->tag)
6788 {
6789 if (child_die->tag == DW_TAG_subrange_type)
6790 {
f792889a 6791 struct type *child_type = read_type_die (child_die, cu);
9a619af0 6792
f792889a 6793 if (child_type != NULL)
a02abb62
JB
6794 {
6795 /* The range type was succesfully read. Save it for
6796 the array type creation. */
6797 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
6798 {
6799 range_types = (struct type **)
6800 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
6801 * sizeof (struct type *));
6802 if (ndim == 0)
6803 make_cleanup (free_current_contents, &range_types);
6804 }
f792889a 6805 range_types[ndim++] = child_type;
a02abb62 6806 }
c906108c
SS
6807 }
6808 child_die = sibling_die (child_die);
6809 }
6810
6811 /* Dwarf2 dimensions are output from left to right, create the
6812 necessary array types in backwards order. */
7ca2d3a3 6813
c906108c 6814 type = element_type;
7ca2d3a3
DL
6815
6816 if (read_array_order (die, cu) == DW_ORD_col_major)
6817 {
6818 int i = 0;
9a619af0 6819
7ca2d3a3
DL
6820 while (i < ndim)
6821 type = create_array_type (NULL, type, range_types[i++]);
6822 }
6823 else
6824 {
6825 while (ndim-- > 0)
6826 type = create_array_type (NULL, type, range_types[ndim]);
6827 }
c906108c 6828
f5f8a009
EZ
6829 /* Understand Dwarf2 support for vector types (like they occur on
6830 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
6831 array type. This is not part of the Dwarf2/3 standard yet, but a
6832 custom vendor extension. The main difference between a regular
6833 array and the vector variant is that vectors are passed by value
6834 to functions. */
e142c38c 6835 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 6836 if (attr)
ea37ba09 6837 make_vector_type (type);
f5f8a009 6838
39cbfefa
DJ
6839 name = dwarf2_name (die, cu);
6840 if (name)
6841 TYPE_NAME (type) = name;
6e70227d 6842
7e314c57
JK
6843 /* Install the type in the die. */
6844 set_die_type (die, type, cu);
6845
6846 /* set_die_type should be already done. */
b4ba55a1
JB
6847 set_descriptive_type (type, die, cu);
6848
c906108c
SS
6849 do_cleanups (back_to);
6850
7e314c57 6851 return type;
c906108c
SS
6852}
6853
7ca2d3a3 6854static enum dwarf_array_dim_ordering
6e70227d 6855read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
6856{
6857 struct attribute *attr;
6858
6859 attr = dwarf2_attr (die, DW_AT_ordering, cu);
6860
6861 if (attr) return DW_SND (attr);
6862
6863 /*
6864 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 6865 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
6866 laid out as per normal fortran.
6867
6e70227d 6868 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
6869 version checking.
6870 */
6871
905e0470
PM
6872 if (cu->language == language_fortran
6873 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
6874 {
6875 return DW_ORD_row_major;
6876 }
6877
6e70227d 6878 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
6879 {
6880 case array_column_major:
6881 return DW_ORD_col_major;
6882 case array_row_major:
6883 default:
6884 return DW_ORD_row_major;
6885 };
6886}
6887
72019c9c
GM
6888/* Extract all information from a DW_TAG_set_type DIE and put it in
6889 the DIE's type field. */
6890
f792889a 6891static struct type *
72019c9c
GM
6892read_set_type (struct die_info *die, struct dwarf2_cu *cu)
6893{
7e314c57
JK
6894 struct type *domain_type, *set_type;
6895 struct attribute *attr;
f792889a 6896
7e314c57
JK
6897 domain_type = die_type (die, cu);
6898
6899 /* The die_type call above may have already set the type for this DIE. */
6900 set_type = get_die_type (die, cu);
6901 if (set_type)
6902 return set_type;
6903
6904 set_type = create_set_type (NULL, domain_type);
6905
6906 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
6907 if (attr)
6908 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 6909
f792889a 6910 return set_die_type (die, set_type, cu);
72019c9c 6911}
7ca2d3a3 6912
c906108c
SS
6913/* First cut: install each common block member as a global variable. */
6914
6915static void
e7c27a73 6916read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
6917{
6918 struct die_info *child_die;
6919 struct attribute *attr;
6920 struct symbol *sym;
6921 CORE_ADDR base = (CORE_ADDR) 0;
6922
e142c38c 6923 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
6924 if (attr)
6925 {
8e19ed76
PS
6926 /* Support the .debug_loc offsets */
6927 if (attr_form_is_block (attr))
6928 {
e7c27a73 6929 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 6930 }
3690dd37 6931 else if (attr_form_is_section_offset (attr))
8e19ed76 6932 {
4d3c2250 6933 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6934 }
6935 else
6936 {
4d3c2250
KB
6937 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
6938 "common block member");
8e19ed76 6939 }
c906108c 6940 }
639d11d3 6941 if (die->child != NULL)
c906108c 6942 {
639d11d3 6943 child_die = die->child;
c906108c
SS
6944 while (child_die && child_die->tag)
6945 {
e7c27a73 6946 sym = new_symbol (child_die, NULL, cu);
e142c38c 6947 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
c906108c
SS
6948 if (attr)
6949 {
d4b96c9a
JK
6950 CORE_ADDR byte_offset = 0;
6951
6952 if (attr_form_is_section_offset (attr))
6953 dwarf2_complex_location_expr_complaint ();
6954 else if (attr_form_is_constant (attr))
6955 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6956 else if (attr_form_is_block (attr))
6957 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6958 else
6959 dwarf2_complex_location_expr_complaint ();
6960
6961 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
6962 add_symbol_to_list (sym, &global_symbols);
6963 }
6964 child_die = sibling_die (child_die);
6965 }
6966 }
6967}
6968
0114d602 6969/* Create a type for a C++ namespace. */
d9fa45fe 6970
0114d602
DJ
6971static struct type *
6972read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 6973{
e7c27a73 6974 struct objfile *objfile = cu->objfile;
0114d602 6975 const char *previous_prefix, *name;
9219021c 6976 int is_anonymous;
0114d602
DJ
6977 struct type *type;
6978
6979 /* For extensions, reuse the type of the original namespace. */
6980 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
6981 {
6982 struct die_info *ext_die;
6983 struct dwarf2_cu *ext_cu = cu;
9a619af0 6984
0114d602
DJ
6985 ext_die = dwarf2_extension (die, &ext_cu);
6986 type = read_type_die (ext_die, ext_cu);
6987 return set_die_type (die, type, cu);
6988 }
9219021c 6989
e142c38c 6990 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
6991
6992 /* Now build the name of the current namespace. */
6993
0114d602
DJ
6994 previous_prefix = determine_prefix (die, cu);
6995 if (previous_prefix[0] != '\0')
6996 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 6997 previous_prefix, name, 0, cu);
0114d602
DJ
6998
6999 /* Create the type. */
7000 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7001 objfile);
7002 TYPE_NAME (type) = (char *) name;
7003 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7004
60531b24 7005 return set_die_type (die, type, cu);
0114d602
DJ
7006}
7007
7008/* Read a C++ namespace. */
7009
7010static void
7011read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7012{
7013 struct objfile *objfile = cu->objfile;
7014 const char *name;
7015 int is_anonymous;
9219021c 7016
5c4e30ca
DC
7017 /* Add a symbol associated to this if we haven't seen the namespace
7018 before. Also, add a using directive if it's an anonymous
7019 namespace. */
9219021c 7020
f2f0e013 7021 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7022 {
7023 struct type *type;
7024
0114d602 7025 type = read_type_die (die, cu);
e7c27a73 7026 new_symbol (die, type, cu);
5c4e30ca 7027
0114d602 7028 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7029 if (is_anonymous)
0114d602
DJ
7030 {
7031 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7032
c0cc3a76 7033 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7034 NULL, &objfile->objfile_obstack);
0114d602 7035 }
5c4e30ca 7036 }
9219021c 7037
639d11d3 7038 if (die->child != NULL)
d9fa45fe 7039 {
639d11d3 7040 struct die_info *child_die = die->child;
6e70227d 7041
d9fa45fe
DC
7042 while (child_die && child_die->tag)
7043 {
e7c27a73 7044 process_die (child_die, cu);
d9fa45fe
DC
7045 child_die = sibling_die (child_die);
7046 }
7047 }
38d518c9
EZ
7048}
7049
f55ee35c
JK
7050/* Read a Fortran module as type. This DIE can be only a declaration used for
7051 imported module. Still we need that type as local Fortran "use ... only"
7052 declaration imports depend on the created type in determine_prefix. */
7053
7054static struct type *
7055read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7056{
7057 struct objfile *objfile = cu->objfile;
7058 char *module_name;
7059 struct type *type;
7060
7061 module_name = dwarf2_name (die, cu);
7062 if (!module_name)
7063 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7064 die->offset);
7065 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7066
7067 /* determine_prefix uses TYPE_TAG_NAME. */
7068 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7069
7070 return set_die_type (die, type, cu);
7071}
7072
5d7cb8df
JK
7073/* Read a Fortran module. */
7074
7075static void
7076read_module (struct die_info *die, struct dwarf2_cu *cu)
7077{
7078 struct die_info *child_die = die->child;
7079
5d7cb8df
JK
7080 while (child_die && child_die->tag)
7081 {
7082 process_die (child_die, cu);
7083 child_die = sibling_die (child_die);
7084 }
7085}
7086
38d518c9
EZ
7087/* Return the name of the namespace represented by DIE. Set
7088 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7089 namespace. */
7090
7091static const char *
e142c38c 7092namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7093{
7094 struct die_info *current_die;
7095 const char *name = NULL;
7096
7097 /* Loop through the extensions until we find a name. */
7098
7099 for (current_die = die;
7100 current_die != NULL;
f2f0e013 7101 current_die = dwarf2_extension (die, &cu))
38d518c9 7102 {
e142c38c 7103 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7104 if (name != NULL)
7105 break;
7106 }
7107
7108 /* Is it an anonymous namespace? */
7109
7110 *is_anonymous = (name == NULL);
7111 if (*is_anonymous)
7112 name = "(anonymous namespace)";
7113
7114 return name;
d9fa45fe
DC
7115}
7116
c906108c
SS
7117/* Extract all information from a DW_TAG_pointer_type DIE and add to
7118 the user defined type vector. */
7119
f792889a 7120static struct type *
e7c27a73 7121read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7122{
5e2b427d 7123 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7124 struct comp_unit_head *cu_header = &cu->header;
c906108c 7125 struct type *type;
8b2dbe47
KB
7126 struct attribute *attr_byte_size;
7127 struct attribute *attr_address_class;
7128 int byte_size, addr_class;
7e314c57
JK
7129 struct type *target_type;
7130
7131 target_type = die_type (die, cu);
c906108c 7132
7e314c57
JK
7133 /* The die_type call above may have already set the type for this DIE. */
7134 type = get_die_type (die, cu);
7135 if (type)
7136 return type;
7137
7138 type = lookup_pointer_type (target_type);
8b2dbe47 7139
e142c38c 7140 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7141 if (attr_byte_size)
7142 byte_size = DW_UNSND (attr_byte_size);
c906108c 7143 else
8b2dbe47
KB
7144 byte_size = cu_header->addr_size;
7145
e142c38c 7146 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7147 if (attr_address_class)
7148 addr_class = DW_UNSND (attr_address_class);
7149 else
7150 addr_class = DW_ADDR_none;
7151
7152 /* If the pointer size or address class is different than the
7153 default, create a type variant marked as such and set the
7154 length accordingly. */
7155 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7156 {
5e2b427d 7157 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7158 {
7159 int type_flags;
7160
849957d9 7161 type_flags = gdbarch_address_class_type_flags
5e2b427d 7162 (gdbarch, byte_size, addr_class);
876cecd0
TT
7163 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7164 == 0);
8b2dbe47
KB
7165 type = make_type_with_address_space (type, type_flags);
7166 }
7167 else if (TYPE_LENGTH (type) != byte_size)
7168 {
e2e0b3e5 7169 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 7170 }
6e70227d 7171 else
9a619af0
MS
7172 {
7173 /* Should we also complain about unhandled address classes? */
7174 }
c906108c 7175 }
8b2dbe47
KB
7176
7177 TYPE_LENGTH (type) = byte_size;
f792889a 7178 return set_die_type (die, type, cu);
c906108c
SS
7179}
7180
7181/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7182 the user defined type vector. */
7183
f792889a 7184static struct type *
e7c27a73 7185read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7186{
7187 struct type *type;
7188 struct type *to_type;
7189 struct type *domain;
7190
e7c27a73
DJ
7191 to_type = die_type (die, cu);
7192 domain = die_containing_type (die, cu);
0d5de010 7193
7e314c57
JK
7194 /* The calls above may have already set the type for this DIE. */
7195 type = get_die_type (die, cu);
7196 if (type)
7197 return type;
7198
0d5de010
DJ
7199 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7200 type = lookup_methodptr_type (to_type);
7201 else
7202 type = lookup_memberptr_type (to_type, domain);
c906108c 7203
f792889a 7204 return set_die_type (die, type, cu);
c906108c
SS
7205}
7206
7207/* Extract all information from a DW_TAG_reference_type DIE and add to
7208 the user defined type vector. */
7209
f792889a 7210static struct type *
e7c27a73 7211read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7212{
e7c27a73 7213 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7214 struct type *type, *target_type;
c906108c
SS
7215 struct attribute *attr;
7216
7e314c57
JK
7217 target_type = die_type (die, cu);
7218
7219 /* The die_type call above may have already set the type for this DIE. */
7220 type = get_die_type (die, cu);
7221 if (type)
7222 return type;
7223
7224 type = lookup_reference_type (target_type);
e142c38c 7225 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7226 if (attr)
7227 {
7228 TYPE_LENGTH (type) = DW_UNSND (attr);
7229 }
7230 else
7231 {
107d2387 7232 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7233 }
f792889a 7234 return set_die_type (die, type, cu);
c906108c
SS
7235}
7236
f792889a 7237static struct type *
e7c27a73 7238read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7239{
f792889a 7240 struct type *base_type, *cv_type;
c906108c 7241
e7c27a73 7242 base_type = die_type (die, cu);
7e314c57
JK
7243
7244 /* The die_type call above may have already set the type for this DIE. */
7245 cv_type = get_die_type (die, cu);
7246 if (cv_type)
7247 return cv_type;
7248
f792889a
DJ
7249 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7250 return set_die_type (die, cv_type, cu);
c906108c
SS
7251}
7252
f792889a 7253static struct type *
e7c27a73 7254read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7255{
f792889a 7256 struct type *base_type, *cv_type;
c906108c 7257
e7c27a73 7258 base_type = die_type (die, cu);
7e314c57
JK
7259
7260 /* The die_type call above may have already set the type for this DIE. */
7261 cv_type = get_die_type (die, cu);
7262 if (cv_type)
7263 return cv_type;
7264
f792889a
DJ
7265 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7266 return set_die_type (die, cv_type, cu);
c906108c
SS
7267}
7268
7269/* Extract all information from a DW_TAG_string_type DIE and add to
7270 the user defined type vector. It isn't really a user defined type,
7271 but it behaves like one, with other DIE's using an AT_user_def_type
7272 attribute to reference it. */
7273
f792889a 7274static struct type *
e7c27a73 7275read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7276{
e7c27a73 7277 struct objfile *objfile = cu->objfile;
3b7538c0 7278 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7279 struct type *type, *range_type, *index_type, *char_type;
7280 struct attribute *attr;
7281 unsigned int length;
7282
e142c38c 7283 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7284 if (attr)
7285 {
7286 length = DW_UNSND (attr);
7287 }
7288 else
7289 {
b21b22e0 7290 /* check for the DW_AT_byte_size attribute */
e142c38c 7291 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7292 if (attr)
7293 {
7294 length = DW_UNSND (attr);
7295 }
7296 else
7297 {
7298 length = 1;
7299 }
c906108c 7300 }
6ccb9162 7301
46bf5051 7302 index_type = objfile_type (objfile)->builtin_int;
c906108c 7303 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7304 char_type = language_string_char_type (cu->language_defn, gdbarch);
7305 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7306
f792889a 7307 return set_die_type (die, type, cu);
c906108c
SS
7308}
7309
7310/* Handle DIES due to C code like:
7311
7312 struct foo
c5aa993b
JM
7313 {
7314 int (*funcp)(int a, long l);
7315 int b;
7316 };
c906108c
SS
7317
7318 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7319 */
c906108c 7320
f792889a 7321static struct type *
e7c27a73 7322read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7323{
7324 struct type *type; /* Type that this function returns */
7325 struct type *ftype; /* Function that returns above type */
7326 struct attribute *attr;
7327
e7c27a73 7328 type = die_type (die, cu);
7e314c57
JK
7329
7330 /* The die_type call above may have already set the type for this DIE. */
7331 ftype = get_die_type (die, cu);
7332 if (ftype)
7333 return ftype;
7334
0c8b41f1 7335 ftype = lookup_function_type (type);
c906108c 7336
5b8101ae 7337 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7338 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7339 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7340 || cu->language == language_cplus
5b8101ae
PM
7341 || cu->language == language_java
7342 || cu->language == language_pascal)
876cecd0 7343 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7344 else if (producer_is_realview (cu->producer))
7345 /* RealView does not emit DW_AT_prototyped. We can not
7346 distinguish prototyped and unprototyped functions; default to
7347 prototyped, since that is more common in modern code (and
7348 RealView warns about unprototyped functions). */
7349 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7350
c055b101
CV
7351 /* Store the calling convention in the type if it's available in
7352 the subroutine die. Otherwise set the calling convention to
7353 the default value DW_CC_normal. */
7354 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7355 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7356
7357 /* We need to add the subroutine type to the die immediately so
7358 we don't infinitely recurse when dealing with parameters
7359 declared as the same subroutine type. */
7360 set_die_type (die, ftype, cu);
6e70227d 7361
639d11d3 7362 if (die->child != NULL)
c906108c 7363 {
8072405b 7364 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7365 struct die_info *child_die;
8072405b 7366 int nparams, iparams;
c906108c
SS
7367
7368 /* Count the number of parameters.
7369 FIXME: GDB currently ignores vararg functions, but knows about
7370 vararg member functions. */
8072405b 7371 nparams = 0;
639d11d3 7372 child_die = die->child;
c906108c
SS
7373 while (child_die && child_die->tag)
7374 {
7375 if (child_die->tag == DW_TAG_formal_parameter)
7376 nparams++;
7377 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7378 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7379 child_die = sibling_die (child_die);
7380 }
7381
7382 /* Allocate storage for parameters and fill them in. */
7383 TYPE_NFIELDS (ftype) = nparams;
7384 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7385 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7386
8072405b
JK
7387 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7388 even if we error out during the parameters reading below. */
7389 for (iparams = 0; iparams < nparams; iparams++)
7390 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7391
7392 iparams = 0;
639d11d3 7393 child_die = die->child;
c906108c
SS
7394 while (child_die && child_die->tag)
7395 {
7396 if (child_die->tag == DW_TAG_formal_parameter)
7397 {
7398 /* Dwarf2 has no clean way to discern C++ static and non-static
c5aa993b
JM
7399 member functions. G++ helps GDB by marking the first
7400 parameter for non-static member functions (which is the
7401 this pointer) as artificial. We pass this information
7402 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL. */
e142c38c 7403 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7404 if (attr)
7405 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7406 else
418835cc
KS
7407 {
7408 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7409
7410 /* GCC/43521: In java, the formal parameter
7411 "this" is sometimes not marked with DW_AT_artificial. */
7412 if (cu->language == language_java)
7413 {
7414 const char *name = dwarf2_name (child_die, cu);
9a619af0 7415
418835cc
KS
7416 if (name && !strcmp (name, "this"))
7417 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7418 }
7419 }
e7c27a73 7420 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
c906108c
SS
7421 iparams++;
7422 }
7423 child_die = sibling_die (child_die);
7424 }
7425 }
7426
76c10ea2 7427 return ftype;
c906108c
SS
7428}
7429
f792889a 7430static struct type *
e7c27a73 7431read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7432{
e7c27a73 7433 struct objfile *objfile = cu->objfile;
0114d602 7434 const char *name = NULL;
f792889a 7435 struct type *this_type;
c906108c 7436
94af9270 7437 name = dwarf2_full_name (NULL, die, cu);
f792889a 7438 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7439 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7440 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7441 set_die_type (die, this_type, cu);
7442 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7443 return this_type;
c906108c
SS
7444}
7445
7446/* Find a representation of a given base type and install
7447 it in the TYPE field of the die. */
7448
f792889a 7449static struct type *
e7c27a73 7450read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7451{
e7c27a73 7452 struct objfile *objfile = cu->objfile;
c906108c
SS
7453 struct type *type;
7454 struct attribute *attr;
7455 int encoding = 0, size = 0;
39cbfefa 7456 char *name;
6ccb9162
UW
7457 enum type_code code = TYPE_CODE_INT;
7458 int type_flags = 0;
7459 struct type *target_type = NULL;
c906108c 7460
e142c38c 7461 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7462 if (attr)
7463 {
7464 encoding = DW_UNSND (attr);
7465 }
e142c38c 7466 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7467 if (attr)
7468 {
7469 size = DW_UNSND (attr);
7470 }
39cbfefa 7471 name = dwarf2_name (die, cu);
6ccb9162 7472 if (!name)
c906108c 7473 {
6ccb9162
UW
7474 complaint (&symfile_complaints,
7475 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7476 }
6ccb9162
UW
7477
7478 switch (encoding)
c906108c 7479 {
6ccb9162
UW
7480 case DW_ATE_address:
7481 /* Turn DW_ATE_address into a void * pointer. */
7482 code = TYPE_CODE_PTR;
7483 type_flags |= TYPE_FLAG_UNSIGNED;
7484 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7485 break;
7486 case DW_ATE_boolean:
7487 code = TYPE_CODE_BOOL;
7488 type_flags |= TYPE_FLAG_UNSIGNED;
7489 break;
7490 case DW_ATE_complex_float:
7491 code = TYPE_CODE_COMPLEX;
7492 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7493 break;
7494 case DW_ATE_decimal_float:
7495 code = TYPE_CODE_DECFLOAT;
7496 break;
7497 case DW_ATE_float:
7498 code = TYPE_CODE_FLT;
7499 break;
7500 case DW_ATE_signed:
7501 break;
7502 case DW_ATE_unsigned:
7503 type_flags |= TYPE_FLAG_UNSIGNED;
7504 break;
7505 case DW_ATE_signed_char:
6e70227d 7506 if (cu->language == language_ada || cu->language == language_m2
868a0084 7507 || cu->language == language_pascal)
6ccb9162
UW
7508 code = TYPE_CODE_CHAR;
7509 break;
7510 case DW_ATE_unsigned_char:
868a0084
PM
7511 if (cu->language == language_ada || cu->language == language_m2
7512 || cu->language == language_pascal)
6ccb9162
UW
7513 code = TYPE_CODE_CHAR;
7514 type_flags |= TYPE_FLAG_UNSIGNED;
7515 break;
75079b2b
TT
7516 case DW_ATE_UTF:
7517 /* We just treat this as an integer and then recognize the
7518 type by name elsewhere. */
7519 break;
7520
6ccb9162
UW
7521 default:
7522 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7523 dwarf_type_encoding_name (encoding));
7524 break;
c906108c 7525 }
6ccb9162 7526
0114d602
DJ
7527 type = init_type (code, size, type_flags, NULL, objfile);
7528 TYPE_NAME (type) = name;
6ccb9162
UW
7529 TYPE_TARGET_TYPE (type) = target_type;
7530
0114d602 7531 if (name && strcmp (name, "char") == 0)
876cecd0 7532 TYPE_NOSIGN (type) = 1;
0114d602 7533
f792889a 7534 return set_die_type (die, type, cu);
c906108c
SS
7535}
7536
a02abb62
JB
7537/* Read the given DW_AT_subrange DIE. */
7538
f792889a 7539static struct type *
a02abb62
JB
7540read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7541{
5e2b427d 7542 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7543 struct type *base_type;
7544 struct type *range_type;
7545 struct attribute *attr;
43bbcdc2
PH
7546 LONGEST low = 0;
7547 LONGEST high = -1;
39cbfefa 7548 char *name;
43bbcdc2 7549 LONGEST negative_mask;
e77813c8 7550
a02abb62 7551 base_type = die_type (die, cu);
953ac07e
JK
7552 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7553 check_typedef (base_type);
a02abb62 7554
7e314c57
JK
7555 /* The die_type call above may have already set the type for this DIE. */
7556 range_type = get_die_type (die, cu);
7557 if (range_type)
7558 return range_type;
7559
e142c38c 7560 if (cu->language == language_fortran)
6e70227d 7561 {
a02abb62
JB
7562 /* FORTRAN implies a lower bound of 1, if not given. */
7563 low = 1;
7564 }
7565
dd5e6932
DJ
7566 /* FIXME: For variable sized arrays either of these could be
7567 a variable rather than a constant value. We'll allow it,
7568 but we don't know how to handle it. */
e142c38c 7569 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
7570 if (attr)
7571 low = dwarf2_get_attr_constant_value (attr, 0);
7572
e142c38c 7573 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 7574 if (attr)
6e70227d 7575 {
e77813c8 7576 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
7577 {
7578 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 7579 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
7580 FIXME: GDB does not yet know how to handle dynamic
7581 arrays properly, treat them as arrays with unspecified
7582 length for now.
7583
7584 FIXME: jimb/2003-09-22: GDB does not really know
7585 how to handle arrays of unspecified length
7586 either; we just represent them as zero-length
7587 arrays. Choose an appropriate upper bound given
7588 the lower bound we've computed above. */
7589 high = low - 1;
7590 }
7591 else
7592 high = dwarf2_get_attr_constant_value (attr, 1);
7593 }
e77813c8
PM
7594 else
7595 {
7596 attr = dwarf2_attr (die, DW_AT_count, cu);
7597 if (attr)
7598 {
7599 int count = dwarf2_get_attr_constant_value (attr, 1);
7600 high = low + count - 1;
7601 }
7602 }
7603
7604 /* Dwarf-2 specifications explicitly allows to create subrange types
7605 without specifying a base type.
7606 In that case, the base type must be set to the type of
7607 the lower bound, upper bound or count, in that order, if any of these
7608 three attributes references an object that has a type.
7609 If no base type is found, the Dwarf-2 specifications say that
7610 a signed integer type of size equal to the size of an address should
7611 be used.
7612 For the following C code: `extern char gdb_int [];'
7613 GCC produces an empty range DIE.
7614 FIXME: muller/2010-05-28: Possible references to object for low bound,
7615 high bound or count are not yet handled by this code.
7616 */
7617 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
7618 {
7619 struct objfile *objfile = cu->objfile;
7620 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7621 int addr_size = gdbarch_addr_bit (gdbarch) /8;
7622 struct type *int_type = objfile_type (objfile)->builtin_int;
7623
7624 /* Test "int", "long int", and "long long int" objfile types,
7625 and select the first one having a size above or equal to the
7626 architecture address size. */
7627 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7628 base_type = int_type;
7629 else
7630 {
7631 int_type = objfile_type (objfile)->builtin_long;
7632 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7633 base_type = int_type;
7634 else
7635 {
7636 int_type = objfile_type (objfile)->builtin_long_long;
7637 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7638 base_type = int_type;
7639 }
7640 }
7641 }
a02abb62 7642
6e70227d 7643 negative_mask =
43bbcdc2
PH
7644 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
7645 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
7646 low |= negative_mask;
7647 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
7648 high |= negative_mask;
7649
a02abb62
JB
7650 range_type = create_range_type (NULL, base_type, low, high);
7651
bbb0eef6
JK
7652 /* Mark arrays with dynamic length at least as an array of unspecified
7653 length. GDB could check the boundary but before it gets implemented at
7654 least allow accessing the array elements. */
7655 if (attr && attr->form == DW_FORM_block1)
7656 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
7657
39cbfefa
DJ
7658 name = dwarf2_name (die, cu);
7659 if (name)
7660 TYPE_NAME (range_type) = name;
6e70227d 7661
e142c38c 7662 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
7663 if (attr)
7664 TYPE_LENGTH (range_type) = DW_UNSND (attr);
7665
7e314c57
JK
7666 set_die_type (die, range_type, cu);
7667
7668 /* set_die_type should be already done. */
b4ba55a1
JB
7669 set_descriptive_type (range_type, die, cu);
7670
7e314c57 7671 return range_type;
a02abb62 7672}
6e70227d 7673
f792889a 7674static struct type *
81a17f79
JB
7675read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
7676{
7677 struct type *type;
81a17f79 7678
81a17f79
JB
7679 /* For now, we only support the C meaning of an unspecified type: void. */
7680
0114d602
DJ
7681 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
7682 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 7683
f792889a 7684 return set_die_type (die, type, cu);
81a17f79 7685}
a02abb62 7686
51545339
DJ
7687/* Trivial hash function for die_info: the hash value of a DIE
7688 is its offset in .debug_info for this objfile. */
7689
7690static hashval_t
7691die_hash (const void *item)
7692{
7693 const struct die_info *die = item;
9a619af0 7694
51545339
DJ
7695 return die->offset;
7696}
7697
7698/* Trivial comparison function for die_info structures: two DIEs
7699 are equal if they have the same offset. */
7700
7701static int
7702die_eq (const void *item_lhs, const void *item_rhs)
7703{
7704 const struct die_info *die_lhs = item_lhs;
7705 const struct die_info *die_rhs = item_rhs;
9a619af0 7706
51545339
DJ
7707 return die_lhs->offset == die_rhs->offset;
7708}
7709
c906108c
SS
7710/* Read a whole compilation unit into a linked list of dies. */
7711
f9aca02d 7712static struct die_info *
93311388 7713read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 7714{
93311388
DE
7715 struct die_reader_specs reader_specs;
7716
348e048f 7717 gdb_assert (cu->die_hash == NULL);
51545339
DJ
7718 cu->die_hash
7719 = htab_create_alloc_ex (cu->header.length / 12,
7720 die_hash,
7721 die_eq,
7722 NULL,
7723 &cu->comp_unit_obstack,
7724 hashtab_obstack_allocate,
7725 dummy_obstack_deallocate);
7726
93311388
DE
7727 init_cu_die_reader (&reader_specs, cu);
7728
7729 return read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
639d11d3
DC
7730}
7731
d97bc12b
DE
7732/* Main entry point for reading a DIE and all children.
7733 Read the DIE and dump it if requested. */
7734
7735static struct die_info *
93311388
DE
7736read_die_and_children (const struct die_reader_specs *reader,
7737 gdb_byte *info_ptr,
d97bc12b
DE
7738 gdb_byte **new_info_ptr,
7739 struct die_info *parent)
7740{
93311388 7741 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
7742 new_info_ptr, parent);
7743
7744 if (dwarf2_die_debug)
7745 {
348e048f
DE
7746 fprintf_unfiltered (gdb_stdlog,
7747 "\nRead die from %s of %s:\n",
7748 reader->buffer == dwarf2_per_objfile->info.buffer
7749 ? ".debug_info"
7750 : reader->buffer == dwarf2_per_objfile->types.buffer
7751 ? ".debug_types"
7752 : "unknown section",
7753 reader->abfd->filename);
d97bc12b
DE
7754 dump_die (result, dwarf2_die_debug);
7755 }
7756
7757 return result;
7758}
7759
639d11d3
DC
7760/* Read a single die and all its descendents. Set the die's sibling
7761 field to NULL; set other fields in the die correctly, and set all
7762 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
7763 location of the info_ptr after reading all of those dies. PARENT
7764 is the parent of the die in question. */
7765
7766static struct die_info *
93311388
DE
7767read_die_and_children_1 (const struct die_reader_specs *reader,
7768 gdb_byte *info_ptr,
d97bc12b
DE
7769 gdb_byte **new_info_ptr,
7770 struct die_info *parent)
639d11d3
DC
7771{
7772 struct die_info *die;
fe1b8b76 7773 gdb_byte *cur_ptr;
639d11d3
DC
7774 int has_children;
7775
93311388 7776 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
7777 if (die == NULL)
7778 {
7779 *new_info_ptr = cur_ptr;
7780 return NULL;
7781 }
93311388 7782 store_in_ref_table (die, reader->cu);
639d11d3
DC
7783
7784 if (has_children)
348e048f 7785 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
7786 else
7787 {
7788 die->child = NULL;
7789 *new_info_ptr = cur_ptr;
7790 }
7791
7792 die->sibling = NULL;
7793 die->parent = parent;
7794 return die;
7795}
7796
7797/* Read a die, all of its descendents, and all of its siblings; set
7798 all of the fields of all of the dies correctly. Arguments are as
7799 in read_die_and_children. */
7800
7801static struct die_info *
93311388
DE
7802read_die_and_siblings (const struct die_reader_specs *reader,
7803 gdb_byte *info_ptr,
fe1b8b76 7804 gdb_byte **new_info_ptr,
639d11d3
DC
7805 struct die_info *parent)
7806{
7807 struct die_info *first_die, *last_sibling;
fe1b8b76 7808 gdb_byte *cur_ptr;
639d11d3 7809
c906108c 7810 cur_ptr = info_ptr;
639d11d3
DC
7811 first_die = last_sibling = NULL;
7812
7813 while (1)
c906108c 7814 {
639d11d3 7815 struct die_info *die
93311388 7816 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 7817
1d325ec1 7818 if (die == NULL)
c906108c 7819 {
639d11d3
DC
7820 *new_info_ptr = cur_ptr;
7821 return first_die;
c906108c 7822 }
1d325ec1
DJ
7823
7824 if (!first_die)
7825 first_die = die;
c906108c 7826 else
1d325ec1
DJ
7827 last_sibling->sibling = die;
7828
7829 last_sibling = die;
c906108c 7830 }
c906108c
SS
7831}
7832
93311388
DE
7833/* Read the die from the .debug_info section buffer. Set DIEP to
7834 point to a newly allocated die with its information, except for its
7835 child, sibling, and parent fields. Set HAS_CHILDREN to tell
7836 whether the die has children or not. */
7837
7838static gdb_byte *
7839read_full_die (const struct die_reader_specs *reader,
7840 struct die_info **diep, gdb_byte *info_ptr,
7841 int *has_children)
7842{
7843 unsigned int abbrev_number, bytes_read, i, offset;
7844 struct abbrev_info *abbrev;
7845 struct die_info *die;
7846 struct dwarf2_cu *cu = reader->cu;
7847 bfd *abfd = reader->abfd;
7848
7849 offset = info_ptr - reader->buffer;
7850 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7851 info_ptr += bytes_read;
7852 if (!abbrev_number)
7853 {
7854 *diep = NULL;
7855 *has_children = 0;
7856 return info_ptr;
7857 }
7858
7859 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
7860 if (!abbrev)
348e048f
DE
7861 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
7862 abbrev_number,
7863 bfd_get_filename (abfd));
7864
93311388
DE
7865 die = dwarf_alloc_die (cu, abbrev->num_attrs);
7866 die->offset = offset;
7867 die->tag = abbrev->tag;
7868 die->abbrev = abbrev_number;
7869
7870 die->num_attrs = abbrev->num_attrs;
7871
7872 for (i = 0; i < abbrev->num_attrs; ++i)
7873 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
7874 abfd, info_ptr, cu);
7875
7876 *diep = die;
7877 *has_children = abbrev->has_children;
7878 return info_ptr;
7879}
7880
c906108c
SS
7881/* In DWARF version 2, the description of the debugging information is
7882 stored in a separate .debug_abbrev section. Before we read any
7883 dies from a section we read in all abbreviations and install them
72bf9492
DJ
7884 in a hash table. This function also sets flags in CU describing
7885 the data found in the abbrev table. */
c906108c
SS
7886
7887static void
e7c27a73 7888dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 7889{
e7c27a73 7890 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 7891 gdb_byte *abbrev_ptr;
c906108c
SS
7892 struct abbrev_info *cur_abbrev;
7893 unsigned int abbrev_number, bytes_read, abbrev_name;
7894 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
7895 struct attr_abbrev *cur_attrs;
7896 unsigned int allocated_attrs;
c906108c 7897
57349743 7898 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
7899 obstack_init (&cu->abbrev_obstack);
7900 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
7901 (ABBREV_HASH_SIZE
7902 * sizeof (struct abbrev_info *)));
7903 memset (cu->dwarf2_abbrevs, 0,
7904 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 7905
be391dca
TT
7906 dwarf2_read_section (dwarf2_per_objfile->objfile,
7907 &dwarf2_per_objfile->abbrev);
dce234bc 7908 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
7909 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7910 abbrev_ptr += bytes_read;
7911
f3dd6933
DJ
7912 allocated_attrs = ATTR_ALLOC_CHUNK;
7913 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 7914
c906108c
SS
7915 /* loop until we reach an abbrev number of 0 */
7916 while (abbrev_number)
7917 {
f3dd6933 7918 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
7919
7920 /* read in abbrev header */
7921 cur_abbrev->number = abbrev_number;
7922 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7923 abbrev_ptr += bytes_read;
7924 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
7925 abbrev_ptr += 1;
7926
72bf9492
DJ
7927 if (cur_abbrev->tag == DW_TAG_namespace)
7928 cu->has_namespace_info = 1;
7929
c906108c
SS
7930 /* now read in declarations */
7931 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7932 abbrev_ptr += bytes_read;
7933 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7934 abbrev_ptr += bytes_read;
7935 while (abbrev_name)
7936 {
f3dd6933 7937 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 7938 {
f3dd6933
DJ
7939 allocated_attrs += ATTR_ALLOC_CHUNK;
7940 cur_attrs
7941 = xrealloc (cur_attrs, (allocated_attrs
7942 * sizeof (struct attr_abbrev)));
c906108c 7943 }
ae038cb0
DJ
7944
7945 /* Record whether this compilation unit might have
7946 inter-compilation-unit references. If we don't know what form
7947 this attribute will have, then it might potentially be a
7948 DW_FORM_ref_addr, so we conservatively expect inter-CU
7949 references. */
7950
7951 if (abbrev_form == DW_FORM_ref_addr
7952 || abbrev_form == DW_FORM_indirect)
7953 cu->has_form_ref_addr = 1;
7954
f3dd6933
DJ
7955 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
7956 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
7957 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7958 abbrev_ptr += bytes_read;
7959 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7960 abbrev_ptr += bytes_read;
7961 }
7962
f3dd6933
DJ
7963 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
7964 (cur_abbrev->num_attrs
7965 * sizeof (struct attr_abbrev)));
7966 memcpy (cur_abbrev->attrs, cur_attrs,
7967 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
7968
c906108c 7969 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
7970 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
7971 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
7972
7973 /* Get next abbreviation.
7974 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
7975 always properly terminated with an abbrev number of 0.
7976 Exit loop if we encounter an abbreviation which we have
7977 already read (which means we are about to read the abbreviations
7978 for the next compile unit) or if the end of the abbreviation
7979 table is reached. */
dce234bc
PP
7980 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
7981 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
7982 break;
7983 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7984 abbrev_ptr += bytes_read;
e7c27a73 7985 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
7986 break;
7987 }
f3dd6933
DJ
7988
7989 xfree (cur_attrs);
c906108c
SS
7990}
7991
f3dd6933 7992/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 7993
c906108c 7994static void
f3dd6933 7995dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 7996{
f3dd6933 7997 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 7998
f3dd6933
DJ
7999 obstack_free (&cu->abbrev_obstack, NULL);
8000 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8001}
8002
8003/* Lookup an abbrev_info structure in the abbrev hash table. */
8004
8005static struct abbrev_info *
e7c27a73 8006dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8007{
8008 unsigned int hash_number;
8009 struct abbrev_info *abbrev;
8010
8011 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8012 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8013
8014 while (abbrev)
8015 {
8016 if (abbrev->number == number)
8017 return abbrev;
8018 else
8019 abbrev = abbrev->next;
8020 }
8021 return NULL;
8022}
8023
72bf9492
DJ
8024/* Returns nonzero if TAG represents a type that we might generate a partial
8025 symbol for. */
8026
8027static int
8028is_type_tag_for_partial (int tag)
8029{
8030 switch (tag)
8031 {
8032#if 0
8033 /* Some types that would be reasonable to generate partial symbols for,
8034 that we don't at present. */
8035 case DW_TAG_array_type:
8036 case DW_TAG_file_type:
8037 case DW_TAG_ptr_to_member_type:
8038 case DW_TAG_set_type:
8039 case DW_TAG_string_type:
8040 case DW_TAG_subroutine_type:
8041#endif
8042 case DW_TAG_base_type:
8043 case DW_TAG_class_type:
680b30c7 8044 case DW_TAG_interface_type:
72bf9492
DJ
8045 case DW_TAG_enumeration_type:
8046 case DW_TAG_structure_type:
8047 case DW_TAG_subrange_type:
8048 case DW_TAG_typedef:
8049 case DW_TAG_union_type:
8050 return 1;
8051 default:
8052 return 0;
8053 }
8054}
8055
8056/* Load all DIEs that are interesting for partial symbols into memory. */
8057
8058static struct partial_die_info *
93311388
DE
8059load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8060 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8061{
8062 struct partial_die_info *part_die;
8063 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8064 struct abbrev_info *abbrev;
8065 unsigned int bytes_read;
5afb4e99 8066 unsigned int load_all = 0;
72bf9492
DJ
8067
8068 int nesting_level = 1;
8069
8070 parent_die = NULL;
8071 last_die = NULL;
8072
5afb4e99
DJ
8073 if (cu->per_cu && cu->per_cu->load_all_dies)
8074 load_all = 1;
8075
72bf9492
DJ
8076 cu->partial_dies
8077 = htab_create_alloc_ex (cu->header.length / 12,
8078 partial_die_hash,
8079 partial_die_eq,
8080 NULL,
8081 &cu->comp_unit_obstack,
8082 hashtab_obstack_allocate,
8083 dummy_obstack_deallocate);
8084
8085 part_die = obstack_alloc (&cu->comp_unit_obstack,
8086 sizeof (struct partial_die_info));
8087
8088 while (1)
8089 {
8090 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8091
8092 /* A NULL abbrev means the end of a series of children. */
8093 if (abbrev == NULL)
8094 {
8095 if (--nesting_level == 0)
8096 {
8097 /* PART_DIE was probably the last thing allocated on the
8098 comp_unit_obstack, so we could call obstack_free
8099 here. We don't do that because the waste is small,
8100 and will be cleaned up when we're done with this
8101 compilation unit. This way, we're also more robust
8102 against other users of the comp_unit_obstack. */
8103 return first_die;
8104 }
8105 info_ptr += bytes_read;
8106 last_die = parent_die;
8107 parent_die = parent_die->die_parent;
8108 continue;
8109 }
8110
5afb4e99
DJ
8111 /* Check whether this DIE is interesting enough to save. Normally
8112 we would not be interested in members here, but there may be
8113 later variables referencing them via DW_AT_specification (for
8114 static members). */
8115 if (!load_all
8116 && !is_type_tag_for_partial (abbrev->tag)
72bf9492
DJ
8117 && abbrev->tag != DW_TAG_enumerator
8118 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8119 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8120 && abbrev->tag != DW_TAG_variable
5afb4e99 8121 && abbrev->tag != DW_TAG_namespace
f55ee35c 8122 && abbrev->tag != DW_TAG_module
5afb4e99 8123 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8124 {
8125 /* Otherwise we skip to the next sibling, if any. */
93311388 8126 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8127 continue;
8128 }
8129
93311388
DE
8130 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8131 buffer, info_ptr, cu);
72bf9492
DJ
8132
8133 /* This two-pass algorithm for processing partial symbols has a
8134 high cost in cache pressure. Thus, handle some simple cases
8135 here which cover the majority of C partial symbols. DIEs
8136 which neither have specification tags in them, nor could have
8137 specification tags elsewhere pointing at them, can simply be
8138 processed and discarded.
8139
8140 This segment is also optional; scan_partial_symbols and
8141 add_partial_symbol will handle these DIEs if we chain
8142 them in normally. When compilers which do not emit large
8143 quantities of duplicate debug information are more common,
8144 this code can probably be removed. */
8145
8146 /* Any complete simple types at the top level (pretty much all
8147 of them, for a language without namespaces), can be processed
8148 directly. */
8149 if (parent_die == NULL
8150 && part_die->has_specification == 0
8151 && part_die->is_declaration == 0
8152 && (part_die->tag == DW_TAG_typedef
8153 || part_die->tag == DW_TAG_base_type
8154 || part_die->tag == DW_TAG_subrange_type))
8155 {
8156 if (building_psymtab && part_die->name != NULL)
04a679b8 8157 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8158 VAR_DOMAIN, LOC_TYPEDEF,
8159 &cu->objfile->static_psymbols,
8160 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8161 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8162 continue;
8163 }
8164
8165 /* If we're at the second level, and we're an enumerator, and
8166 our parent has no specification (meaning possibly lives in a
8167 namespace elsewhere), then we can add the partial symbol now
8168 instead of queueing it. */
8169 if (part_die->tag == DW_TAG_enumerator
8170 && parent_die != NULL
8171 && parent_die->die_parent == NULL
8172 && parent_die->tag == DW_TAG_enumeration_type
8173 && parent_die->has_specification == 0)
8174 {
8175 if (part_die->name == NULL)
e2e0b3e5 8176 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 8177 else if (building_psymtab)
04a679b8 8178 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8179 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8180 (cu->language == language_cplus
8181 || cu->language == language_java)
72bf9492
DJ
8182 ? &cu->objfile->global_psymbols
8183 : &cu->objfile->static_psymbols,
8184 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8185
93311388 8186 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8187 continue;
8188 }
8189
8190 /* We'll save this DIE so link it in. */
8191 part_die->die_parent = parent_die;
8192 part_die->die_sibling = NULL;
8193 part_die->die_child = NULL;
8194
8195 if (last_die && last_die == parent_die)
8196 last_die->die_child = part_die;
8197 else if (last_die)
8198 last_die->die_sibling = part_die;
8199
8200 last_die = part_die;
8201
8202 if (first_die == NULL)
8203 first_die = part_die;
8204
8205 /* Maybe add the DIE to the hash table. Not all DIEs that we
8206 find interesting need to be in the hash table, because we
8207 also have the parent/sibling/child chains; only those that we
8208 might refer to by offset later during partial symbol reading.
8209
8210 For now this means things that might have be the target of a
8211 DW_AT_specification, DW_AT_abstract_origin, or
8212 DW_AT_extension. DW_AT_extension will refer only to
8213 namespaces; DW_AT_abstract_origin refers to functions (and
8214 many things under the function DIE, but we do not recurse
8215 into function DIEs during partial symbol reading) and
8216 possibly variables as well; DW_AT_specification refers to
8217 declarations. Declarations ought to have the DW_AT_declaration
8218 flag. It happens that GCC forgets to put it in sometimes, but
8219 only for functions, not for types.
8220
8221 Adding more things than necessary to the hash table is harmless
8222 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8223 wasted time in find_partial_die, when we reread the compilation
8224 unit with load_all_dies set. */
72bf9492 8225
5afb4e99
DJ
8226 if (load_all
8227 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8228 || abbrev->tag == DW_TAG_variable
8229 || abbrev->tag == DW_TAG_namespace
8230 || part_die->is_declaration)
8231 {
8232 void **slot;
8233
8234 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8235 part_die->offset, INSERT);
8236 *slot = part_die;
8237 }
8238
8239 part_die = obstack_alloc (&cu->comp_unit_obstack,
8240 sizeof (struct partial_die_info));
8241
8242 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8243 we have no reason to follow the children of structures; for other
72bf9492 8244 languages we have to, both so that we can get at method physnames
bc30ff58
JB
8245 to infer fully qualified class names, and for DW_AT_specification.
8246
8247 For Ada, we need to scan the children of subprograms and lexical
8248 blocks as well because Ada allows the definition of nested
8249 entities that could be interesting for the debugger, such as
8250 nested subprograms for instance. */
72bf9492 8251 if (last_die->has_children
5afb4e99
DJ
8252 && (load_all
8253 || last_die->tag == DW_TAG_namespace
f55ee35c 8254 || last_die->tag == DW_TAG_module
72bf9492
DJ
8255 || last_die->tag == DW_TAG_enumeration_type
8256 || (cu->language != language_c
8257 && (last_die->tag == DW_TAG_class_type
680b30c7 8258 || last_die->tag == DW_TAG_interface_type
72bf9492 8259 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8260 || last_die->tag == DW_TAG_union_type))
8261 || (cu->language == language_ada
8262 && (last_die->tag == DW_TAG_subprogram
8263 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8264 {
8265 nesting_level++;
8266 parent_die = last_die;
8267 continue;
8268 }
8269
8270 /* Otherwise we skip to the next sibling, if any. */
93311388 8271 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8272
8273 /* Back to the top, do it again. */
8274 }
8275}
8276
c906108c
SS
8277/* Read a minimal amount of information into the minimal die structure. */
8278
fe1b8b76 8279static gdb_byte *
72bf9492
DJ
8280read_partial_die (struct partial_die_info *part_die,
8281 struct abbrev_info *abbrev,
8282 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8283 gdb_byte *buffer, gdb_byte *info_ptr,
8284 struct dwarf2_cu *cu)
c906108c 8285{
fa238c03 8286 unsigned int i;
c906108c 8287 struct attribute attr;
c5aa993b 8288 int has_low_pc_attr = 0;
c906108c
SS
8289 int has_high_pc_attr = 0;
8290
72bf9492 8291 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8292
93311388 8293 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8294
8295 info_ptr += abbrev_len;
8296
8297 if (abbrev == NULL)
8298 return info_ptr;
8299
c906108c
SS
8300 part_die->tag = abbrev->tag;
8301 part_die->has_children = abbrev->has_children;
c906108c
SS
8302
8303 for (i = 0; i < abbrev->num_attrs; ++i)
8304 {
e7c27a73 8305 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8306
8307 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8308 partial symbol table. */
c906108c
SS
8309 switch (attr.name)
8310 {
8311 case DW_AT_name:
71c25dea
TT
8312 switch (part_die->tag)
8313 {
8314 case DW_TAG_compile_unit:
348e048f 8315 case DW_TAG_type_unit:
71c25dea
TT
8316 /* Compilation units have a DW_AT_name that is a filename, not
8317 a source language identifier. */
8318 case DW_TAG_enumeration_type:
8319 case DW_TAG_enumerator:
8320 /* These tags always have simple identifiers already; no need
8321 to canonicalize them. */
8322 part_die->name = DW_STRING (&attr);
8323 break;
8324 default:
8325 part_die->name
8326 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8327 &cu->objfile->objfile_obstack);
71c25dea
TT
8328 break;
8329 }
c906108c 8330 break;
31ef98ae 8331 case DW_AT_linkage_name:
c906108c 8332 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8333 /* Note that both forms of linkage name might appear. We
8334 assume they will be the same, and we only store the last
8335 one we see. */
94af9270
KS
8336 if (cu->language == language_ada)
8337 part_die->name = DW_STRING (&attr);
c906108c
SS
8338 break;
8339 case DW_AT_low_pc:
8340 has_low_pc_attr = 1;
8341 part_die->lowpc = DW_ADDR (&attr);
8342 break;
8343 case DW_AT_high_pc:
8344 has_high_pc_attr = 1;
8345 part_die->highpc = DW_ADDR (&attr);
8346 break;
8347 case DW_AT_location:
8e19ed76
PS
8348 /* Support the .debug_loc offsets */
8349 if (attr_form_is_block (&attr))
8350 {
8351 part_die->locdesc = DW_BLOCK (&attr);
8352 }
3690dd37 8353 else if (attr_form_is_section_offset (&attr))
8e19ed76 8354 {
4d3c2250 8355 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8356 }
8357 else
8358 {
4d3c2250
KB
8359 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8360 "partial symbol information");
8e19ed76 8361 }
c906108c 8362 break;
c906108c
SS
8363 case DW_AT_external:
8364 part_die->is_external = DW_UNSND (&attr);
8365 break;
8366 case DW_AT_declaration:
8367 part_die->is_declaration = DW_UNSND (&attr);
8368 break;
8369 case DW_AT_type:
8370 part_die->has_type = 1;
8371 break;
8372 case DW_AT_abstract_origin:
8373 case DW_AT_specification:
72bf9492
DJ
8374 case DW_AT_extension:
8375 part_die->has_specification = 1;
c764a876 8376 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8377 break;
8378 case DW_AT_sibling:
8379 /* Ignore absolute siblings, they might point outside of
8380 the current compile unit. */
8381 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8382 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8383 else
93311388 8384 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8385 break;
fa4028e9
JB
8386 case DW_AT_byte_size:
8387 part_die->has_byte_size = 1;
8388 break;
68511cec
CES
8389 case DW_AT_calling_convention:
8390 /* DWARF doesn't provide a way to identify a program's source-level
8391 entry point. DW_AT_calling_convention attributes are only meant
8392 to describe functions' calling conventions.
8393
8394 However, because it's a necessary piece of information in
8395 Fortran, and because DW_CC_program is the only piece of debugging
8396 information whose definition refers to a 'main program' at all,
8397 several compilers have begun marking Fortran main programs with
8398 DW_CC_program --- even when those functions use the standard
8399 calling conventions.
8400
8401 So until DWARF specifies a way to provide this information and
8402 compilers pick up the new representation, we'll support this
8403 practice. */
8404 if (DW_UNSND (&attr) == DW_CC_program
8405 && cu->language == language_fortran)
8406 set_main_name (part_die->name);
8407 break;
c906108c
SS
8408 default:
8409 break;
8410 }
8411 }
8412
c906108c
SS
8413 /* When using the GNU linker, .gnu.linkonce. sections are used to
8414 eliminate duplicate copies of functions and vtables and such.
8415 The linker will arbitrarily choose one and discard the others.
8416 The AT_*_pc values for such functions refer to local labels in
8417 these sections. If the section from that file was discarded, the
8418 labels are not in the output, so the relocs get a value of 0.
8419 If this is a discarded function, mark the pc bounds as invalid,
8420 so that GDB will ignore it. */
8421 if (has_low_pc_attr && has_high_pc_attr
8422 && part_die->lowpc < part_die->highpc
8423 && (part_die->lowpc != 0
72dca2f5 8424 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8425 part_die->has_pc_info = 1;
85cbf3d3 8426
c906108c
SS
8427 return info_ptr;
8428}
8429
72bf9492
DJ
8430/* Find a cached partial DIE at OFFSET in CU. */
8431
8432static struct partial_die_info *
c764a876 8433find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8434{
8435 struct partial_die_info *lookup_die = NULL;
8436 struct partial_die_info part_die;
8437
8438 part_die.offset = offset;
8439 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8440
72bf9492
DJ
8441 return lookup_die;
8442}
8443
348e048f
DE
8444/* Find a partial DIE at OFFSET, which may or may not be in CU,
8445 except in the case of .debug_types DIEs which do not reference
8446 outside their CU (they do however referencing other types via
8447 DW_FORM_sig8). */
72bf9492
DJ
8448
8449static struct partial_die_info *
c764a876 8450find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8451{
5afb4e99
DJ
8452 struct dwarf2_per_cu_data *per_cu = NULL;
8453 struct partial_die_info *pd = NULL;
72bf9492 8454
348e048f
DE
8455 if (cu->per_cu->from_debug_types)
8456 {
8457 pd = find_partial_die_in_comp_unit (offset, cu);
8458 if (pd != NULL)
8459 return pd;
8460 goto not_found;
8461 }
8462
45452591 8463 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8464 {
8465 pd = find_partial_die_in_comp_unit (offset, cu);
8466 if (pd != NULL)
8467 return pd;
8468 }
72bf9492 8469
ae038cb0
DJ
8470 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8471
ae038cb0
DJ
8472 if (per_cu->cu == NULL)
8473 {
93311388 8474 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8475 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8476 dwarf2_per_objfile->read_in_chain = per_cu;
8477 }
8478
8479 per_cu->cu->last_used = 0;
5afb4e99
DJ
8480 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8481
8482 if (pd == NULL && per_cu->load_all_dies == 0)
8483 {
8484 struct cleanup *back_to;
8485 struct partial_die_info comp_unit_die;
8486 struct abbrev_info *abbrev;
8487 unsigned int bytes_read;
8488 char *info_ptr;
8489
8490 per_cu->load_all_dies = 1;
8491
8492 /* Re-read the DIEs. */
8493 back_to = make_cleanup (null_cleanup, 0);
8494 if (per_cu->cu->dwarf2_abbrevs == NULL)
8495 {
8496 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 8497 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 8498 }
dce234bc 8499 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
8500 + per_cu->cu->header.offset
8501 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
8502 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8503 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
8504 per_cu->cu->objfile->obfd,
8505 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
8506 per_cu->cu);
8507 if (comp_unit_die.has_children)
93311388
DE
8508 load_partial_dies (per_cu->cu->objfile->obfd,
8509 dwarf2_per_objfile->info.buffer, info_ptr,
8510 0, per_cu->cu);
5afb4e99
DJ
8511 do_cleanups (back_to);
8512
8513 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8514 }
8515
348e048f
DE
8516 not_found:
8517
5afb4e99
DJ
8518 if (pd == NULL)
8519 internal_error (__FILE__, __LINE__,
c764a876 8520 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
8521 offset, bfd_get_filename (cu->objfile->obfd));
8522 return pd;
72bf9492
DJ
8523}
8524
8525/* Adjust PART_DIE before generating a symbol for it. This function
8526 may set the is_external flag or change the DIE's name. */
8527
8528static void
8529fixup_partial_die (struct partial_die_info *part_die,
8530 struct dwarf2_cu *cu)
8531{
8532 /* If we found a reference attribute and the DIE has no name, try
8533 to find a name in the referred to DIE. */
8534
8535 if (part_die->name == NULL && part_die->has_specification)
8536 {
8537 struct partial_die_info *spec_die;
72bf9492 8538
10b3939b 8539 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 8540
10b3939b 8541 fixup_partial_die (spec_die, cu);
72bf9492
DJ
8542
8543 if (spec_die->name)
8544 {
8545 part_die->name = spec_die->name;
8546
8547 /* Copy DW_AT_external attribute if it is set. */
8548 if (spec_die->is_external)
8549 part_die->is_external = spec_die->is_external;
8550 }
8551 }
8552
8553 /* Set default names for some unnamed DIEs. */
8554 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
8555 || part_die->tag == DW_TAG_class_type))
8556 part_die->name = "(anonymous class)";
8557
8558 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
8559 part_die->name = "(anonymous namespace)";
8560
8561 if (part_die->tag == DW_TAG_structure_type
8562 || part_die->tag == DW_TAG_class_type
8563 || part_die->tag == DW_TAG_union_type)
8564 guess_structure_name (part_die, cu);
8565}
8566
a8329558 8567/* Read an attribute value described by an attribute form. */
c906108c 8568
fe1b8b76 8569static gdb_byte *
a8329558 8570read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 8571 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 8572 struct dwarf2_cu *cu)
c906108c 8573{
e7c27a73 8574 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
8575 unsigned int bytes_read;
8576 struct dwarf_block *blk;
8577
a8329558
KW
8578 attr->form = form;
8579 switch (form)
c906108c 8580 {
c906108c 8581 case DW_FORM_ref_addr:
ae411497
TT
8582 if (cu->header.version == 2)
8583 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
8584 else
8585 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
8586 info_ptr += bytes_read;
8587 break;
8588 case DW_FORM_addr:
e7c27a73 8589 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 8590 info_ptr += bytes_read;
c906108c
SS
8591 break;
8592 case DW_FORM_block2:
7b5a2f43 8593 blk = dwarf_alloc_block (cu);
c906108c
SS
8594 blk->size = read_2_bytes (abfd, info_ptr);
8595 info_ptr += 2;
8596 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8597 info_ptr += blk->size;
8598 DW_BLOCK (attr) = blk;
8599 break;
8600 case DW_FORM_block4:
7b5a2f43 8601 blk = dwarf_alloc_block (cu);
c906108c
SS
8602 blk->size = read_4_bytes (abfd, info_ptr);
8603 info_ptr += 4;
8604 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8605 info_ptr += blk->size;
8606 DW_BLOCK (attr) = blk;
8607 break;
8608 case DW_FORM_data2:
8609 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
8610 info_ptr += 2;
8611 break;
8612 case DW_FORM_data4:
8613 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
8614 info_ptr += 4;
8615 break;
8616 case DW_FORM_data8:
8617 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
8618 info_ptr += 8;
8619 break;
2dc7f7b3
TT
8620 case DW_FORM_sec_offset:
8621 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
8622 info_ptr += bytes_read;
8623 break;
c906108c 8624 case DW_FORM_string:
9b1c24c8 8625 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 8626 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
8627 info_ptr += bytes_read;
8628 break;
4bdf3d34
JJ
8629 case DW_FORM_strp:
8630 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
8631 &bytes_read);
8285870a 8632 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
8633 info_ptr += bytes_read;
8634 break;
2dc7f7b3 8635 case DW_FORM_exprloc:
c906108c 8636 case DW_FORM_block:
7b5a2f43 8637 blk = dwarf_alloc_block (cu);
c906108c
SS
8638 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8639 info_ptr += bytes_read;
8640 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8641 info_ptr += blk->size;
8642 DW_BLOCK (attr) = blk;
8643 break;
8644 case DW_FORM_block1:
7b5a2f43 8645 blk = dwarf_alloc_block (cu);
c906108c
SS
8646 blk->size = read_1_byte (abfd, info_ptr);
8647 info_ptr += 1;
8648 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8649 info_ptr += blk->size;
8650 DW_BLOCK (attr) = blk;
8651 break;
8652 case DW_FORM_data1:
8653 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
8654 info_ptr += 1;
8655 break;
8656 case DW_FORM_flag:
8657 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
8658 info_ptr += 1;
8659 break;
2dc7f7b3
TT
8660 case DW_FORM_flag_present:
8661 DW_UNSND (attr) = 1;
8662 break;
c906108c
SS
8663 case DW_FORM_sdata:
8664 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
8665 info_ptr += bytes_read;
8666 break;
8667 case DW_FORM_udata:
8668 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8669 info_ptr += bytes_read;
8670 break;
8671 case DW_FORM_ref1:
10b3939b 8672 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
8673 info_ptr += 1;
8674 break;
8675 case DW_FORM_ref2:
10b3939b 8676 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
8677 info_ptr += 2;
8678 break;
8679 case DW_FORM_ref4:
10b3939b 8680 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
8681 info_ptr += 4;
8682 break;
613e1657 8683 case DW_FORM_ref8:
10b3939b 8684 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
8685 info_ptr += 8;
8686 break;
348e048f
DE
8687 case DW_FORM_sig8:
8688 /* Convert the signature to something we can record in DW_UNSND
8689 for later lookup.
8690 NOTE: This is NULL if the type wasn't found. */
8691 DW_SIGNATURED_TYPE (attr) =
8692 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
8693 info_ptr += 8;
8694 break;
c906108c 8695 case DW_FORM_ref_udata:
10b3939b
DJ
8696 DW_ADDR (attr) = (cu->header.offset
8697 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
8698 info_ptr += bytes_read;
8699 break;
c906108c 8700 case DW_FORM_indirect:
a8329558
KW
8701 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8702 info_ptr += bytes_read;
e7c27a73 8703 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 8704 break;
c906108c 8705 default:
8a3fe4f8 8706 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
8707 dwarf_form_name (form),
8708 bfd_get_filename (abfd));
c906108c 8709 }
28e94949
JB
8710
8711 /* We have seen instances where the compiler tried to emit a byte
8712 size attribute of -1 which ended up being encoded as an unsigned
8713 0xffffffff. Although 0xffffffff is technically a valid size value,
8714 an object of this size seems pretty unlikely so we can relatively
8715 safely treat these cases as if the size attribute was invalid and
8716 treat them as zero by default. */
8717 if (attr->name == DW_AT_byte_size
8718 && form == DW_FORM_data4
8719 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
8720 {
8721 complaint
8722 (&symfile_complaints,
43bbcdc2
PH
8723 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
8724 hex_string (DW_UNSND (attr)));
01c66ae6
JB
8725 DW_UNSND (attr) = 0;
8726 }
28e94949 8727
c906108c
SS
8728 return info_ptr;
8729}
8730
a8329558
KW
8731/* Read an attribute described by an abbreviated attribute. */
8732
fe1b8b76 8733static gdb_byte *
a8329558 8734read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 8735 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
8736{
8737 attr->name = abbrev->name;
e7c27a73 8738 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
8739}
8740
c906108c
SS
8741/* read dwarf information from a buffer */
8742
8743static unsigned int
fe1b8b76 8744read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 8745{
fe1b8b76 8746 return bfd_get_8 (abfd, buf);
c906108c
SS
8747}
8748
8749static int
fe1b8b76 8750read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 8751{
fe1b8b76 8752 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
8753}
8754
8755static unsigned int
fe1b8b76 8756read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8757{
fe1b8b76 8758 return bfd_get_16 (abfd, buf);
c906108c
SS
8759}
8760
8761static int
fe1b8b76 8762read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8763{
fe1b8b76 8764 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
8765}
8766
8767static unsigned int
fe1b8b76 8768read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8769{
fe1b8b76 8770 return bfd_get_32 (abfd, buf);
c906108c
SS
8771}
8772
8773static int
fe1b8b76 8774read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8775{
fe1b8b76 8776 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
8777}
8778
93311388 8779static ULONGEST
fe1b8b76 8780read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8781{
fe1b8b76 8782 return bfd_get_64 (abfd, buf);
c906108c
SS
8783}
8784
8785static CORE_ADDR
fe1b8b76 8786read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 8787 unsigned int *bytes_read)
c906108c 8788{
e7c27a73 8789 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
8790 CORE_ADDR retval = 0;
8791
107d2387 8792 if (cu_header->signed_addr_p)
c906108c 8793 {
107d2387
AC
8794 switch (cu_header->addr_size)
8795 {
8796 case 2:
fe1b8b76 8797 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
8798 break;
8799 case 4:
fe1b8b76 8800 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
8801 break;
8802 case 8:
fe1b8b76 8803 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
8804 break;
8805 default:
8e65ff28 8806 internal_error (__FILE__, __LINE__,
e2e0b3e5 8807 _("read_address: bad switch, signed [in module %s]"),
659b0389 8808 bfd_get_filename (abfd));
107d2387
AC
8809 }
8810 }
8811 else
8812 {
8813 switch (cu_header->addr_size)
8814 {
8815 case 2:
fe1b8b76 8816 retval = bfd_get_16 (abfd, buf);
107d2387
AC
8817 break;
8818 case 4:
fe1b8b76 8819 retval = bfd_get_32 (abfd, buf);
107d2387
AC
8820 break;
8821 case 8:
fe1b8b76 8822 retval = bfd_get_64 (abfd, buf);
107d2387
AC
8823 break;
8824 default:
8e65ff28 8825 internal_error (__FILE__, __LINE__,
e2e0b3e5 8826 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 8827 bfd_get_filename (abfd));
107d2387 8828 }
c906108c 8829 }
64367e0a 8830
107d2387
AC
8831 *bytes_read = cu_header->addr_size;
8832 return retval;
c906108c
SS
8833}
8834
f7ef9339 8835/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
8836 specification allows the initial length to take up either 4 bytes
8837 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
8838 bytes describe the length and all offsets will be 8 bytes in length
8839 instead of 4.
8840
f7ef9339
KB
8841 An older, non-standard 64-bit format is also handled by this
8842 function. The older format in question stores the initial length
8843 as an 8-byte quantity without an escape value. Lengths greater
8844 than 2^32 aren't very common which means that the initial 4 bytes
8845 is almost always zero. Since a length value of zero doesn't make
8846 sense for the 32-bit format, this initial zero can be considered to
8847 be an escape value which indicates the presence of the older 64-bit
8848 format. As written, the code can't detect (old format) lengths
917c78fc
MK
8849 greater than 4GB. If it becomes necessary to handle lengths
8850 somewhat larger than 4GB, we could allow other small values (such
8851 as the non-sensical values of 1, 2, and 3) to also be used as
8852 escape values indicating the presence of the old format.
f7ef9339 8853
917c78fc
MK
8854 The value returned via bytes_read should be used to increment the
8855 relevant pointer after calling read_initial_length().
c764a876 8856
613e1657
KB
8857 [ Note: read_initial_length() and read_offset() are based on the
8858 document entitled "DWARF Debugging Information Format", revision
f7ef9339 8859 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
8860 from:
8861
f7ef9339 8862 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 8863
613e1657
KB
8864 This document is only a draft and is subject to change. (So beware.)
8865
f7ef9339 8866 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
8867 determined empirically by examining 64-bit ELF files produced by
8868 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
8869
8870 - Kevin, July 16, 2002
613e1657
KB
8871 ] */
8872
8873static LONGEST
c764a876 8874read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 8875{
fe1b8b76 8876 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 8877
dd373385 8878 if (length == 0xffffffff)
613e1657 8879 {
fe1b8b76 8880 length = bfd_get_64 (abfd, buf + 4);
613e1657 8881 *bytes_read = 12;
613e1657 8882 }
dd373385 8883 else if (length == 0)
f7ef9339 8884 {
dd373385 8885 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 8886 length = bfd_get_64 (abfd, buf);
f7ef9339 8887 *bytes_read = 8;
f7ef9339 8888 }
613e1657
KB
8889 else
8890 {
8891 *bytes_read = 4;
613e1657
KB
8892 }
8893
c764a876
DE
8894 return length;
8895}
dd373385 8896
c764a876
DE
8897/* Cover function for read_initial_length.
8898 Returns the length of the object at BUF, and stores the size of the
8899 initial length in *BYTES_READ and stores the size that offsets will be in
8900 *OFFSET_SIZE.
8901 If the initial length size is not equivalent to that specified in
8902 CU_HEADER then issue a complaint.
8903 This is useful when reading non-comp-unit headers. */
dd373385 8904
c764a876
DE
8905static LONGEST
8906read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
8907 const struct comp_unit_head *cu_header,
8908 unsigned int *bytes_read,
8909 unsigned int *offset_size)
8910{
8911 LONGEST length = read_initial_length (abfd, buf, bytes_read);
8912
8913 gdb_assert (cu_header->initial_length_size == 4
8914 || cu_header->initial_length_size == 8
8915 || cu_header->initial_length_size == 12);
8916
8917 if (cu_header->initial_length_size != *bytes_read)
8918 complaint (&symfile_complaints,
8919 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 8920
c764a876 8921 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 8922 return length;
613e1657
KB
8923}
8924
8925/* Read an offset from the data stream. The size of the offset is
917c78fc 8926 given by cu_header->offset_size. */
613e1657
KB
8927
8928static LONGEST
fe1b8b76 8929read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 8930 unsigned int *bytes_read)
c764a876
DE
8931{
8932 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 8933
c764a876
DE
8934 *bytes_read = cu_header->offset_size;
8935 return offset;
8936}
8937
8938/* Read an offset from the data stream. */
8939
8940static LONGEST
8941read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
8942{
8943 LONGEST retval = 0;
8944
c764a876 8945 switch (offset_size)
613e1657
KB
8946 {
8947 case 4:
fe1b8b76 8948 retval = bfd_get_32 (abfd, buf);
613e1657
KB
8949 break;
8950 case 8:
fe1b8b76 8951 retval = bfd_get_64 (abfd, buf);
613e1657
KB
8952 break;
8953 default:
8e65ff28 8954 internal_error (__FILE__, __LINE__,
c764a876 8955 _("read_offset_1: bad switch [in module %s]"),
659b0389 8956 bfd_get_filename (abfd));
613e1657
KB
8957 }
8958
917c78fc 8959 return retval;
613e1657
KB
8960}
8961
fe1b8b76
JB
8962static gdb_byte *
8963read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
8964{
8965 /* If the size of a host char is 8 bits, we can return a pointer
8966 to the buffer, otherwise we have to copy the data to a buffer
8967 allocated on the temporary obstack. */
4bdf3d34 8968 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 8969 return buf;
c906108c
SS
8970}
8971
8972static char *
9b1c24c8 8973read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
8974{
8975 /* If the size of a host char is 8 bits, we can return a pointer
8976 to the string, otherwise we have to copy the string to a buffer
8977 allocated on the temporary obstack. */
4bdf3d34 8978 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
8979 if (*buf == '\0')
8980 {
8981 *bytes_read_ptr = 1;
8982 return NULL;
8983 }
fe1b8b76
JB
8984 *bytes_read_ptr = strlen ((char *) buf) + 1;
8985 return (char *) buf;
4bdf3d34
JJ
8986}
8987
8988static char *
fe1b8b76 8989read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
8990 const struct comp_unit_head *cu_header,
8991 unsigned int *bytes_read_ptr)
8992{
c764a876 8993 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 8994
be391dca 8995 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 8996 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 8997 {
8a3fe4f8 8998 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 8999 bfd_get_filename (abfd));
4bdf3d34 9000 return NULL;
c906108c 9001 }
dce234bc 9002 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9003 {
8a3fe4f8 9004 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 9005 bfd_get_filename (abfd));
c906108c
SS
9006 return NULL;
9007 }
4bdf3d34 9008 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9009 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9010 return NULL;
dce234bc 9011 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9012}
9013
ce5d95e1 9014static unsigned long
fe1b8b76 9015read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9016{
ce5d95e1
JB
9017 unsigned long result;
9018 unsigned int num_read;
c906108c
SS
9019 int i, shift;
9020 unsigned char byte;
9021
9022 result = 0;
9023 shift = 0;
9024 num_read = 0;
9025 i = 0;
9026 while (1)
9027 {
fe1b8b76 9028 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9029 buf++;
9030 num_read++;
ce5d95e1 9031 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9032 if ((byte & 128) == 0)
9033 {
9034 break;
9035 }
9036 shift += 7;
9037 }
9038 *bytes_read_ptr = num_read;
9039 return result;
9040}
9041
ce5d95e1 9042static long
fe1b8b76 9043read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9044{
ce5d95e1 9045 long result;
77e0b926 9046 int i, shift, num_read;
c906108c
SS
9047 unsigned char byte;
9048
9049 result = 0;
9050 shift = 0;
c906108c
SS
9051 num_read = 0;
9052 i = 0;
9053 while (1)
9054 {
fe1b8b76 9055 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9056 buf++;
9057 num_read++;
ce5d95e1 9058 result |= ((long)(byte & 127) << shift);
c906108c
SS
9059 shift += 7;
9060 if ((byte & 128) == 0)
9061 {
9062 break;
9063 }
9064 }
77e0b926
DJ
9065 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9066 result |= -(((long)1) << shift);
c906108c
SS
9067 *bytes_read_ptr = num_read;
9068 return result;
9069}
9070
4bb7a0a7
DJ
9071/* Return a pointer to just past the end of an LEB128 number in BUF. */
9072
fe1b8b76
JB
9073static gdb_byte *
9074skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9075{
9076 int byte;
9077
9078 while (1)
9079 {
fe1b8b76 9080 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9081 buf++;
9082 if ((byte & 128) == 0)
9083 return buf;
9084 }
9085}
9086
c906108c 9087static void
e142c38c 9088set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9089{
9090 switch (lang)
9091 {
9092 case DW_LANG_C89:
76bee0cc 9093 case DW_LANG_C99:
c906108c 9094 case DW_LANG_C:
e142c38c 9095 cu->language = language_c;
c906108c
SS
9096 break;
9097 case DW_LANG_C_plus_plus:
e142c38c 9098 cu->language = language_cplus;
c906108c 9099 break;
6aecb9c2
JB
9100 case DW_LANG_D:
9101 cu->language = language_d;
9102 break;
c906108c
SS
9103 case DW_LANG_Fortran77:
9104 case DW_LANG_Fortran90:
b21b22e0 9105 case DW_LANG_Fortran95:
e142c38c 9106 cu->language = language_fortran;
c906108c
SS
9107 break;
9108 case DW_LANG_Mips_Assembler:
e142c38c 9109 cu->language = language_asm;
c906108c 9110 break;
bebd888e 9111 case DW_LANG_Java:
e142c38c 9112 cu->language = language_java;
bebd888e 9113 break;
c906108c 9114 case DW_LANG_Ada83:
8aaf0b47 9115 case DW_LANG_Ada95:
bc5f45f8
JB
9116 cu->language = language_ada;
9117 break;
72019c9c
GM
9118 case DW_LANG_Modula2:
9119 cu->language = language_m2;
9120 break;
fe8e67fd
PM
9121 case DW_LANG_Pascal83:
9122 cu->language = language_pascal;
9123 break;
22566fbd
DJ
9124 case DW_LANG_ObjC:
9125 cu->language = language_objc;
9126 break;
c906108c
SS
9127 case DW_LANG_Cobol74:
9128 case DW_LANG_Cobol85:
c906108c 9129 default:
e142c38c 9130 cu->language = language_minimal;
c906108c
SS
9131 break;
9132 }
e142c38c 9133 cu->language_defn = language_def (cu->language);
c906108c
SS
9134}
9135
9136/* Return the named attribute or NULL if not there. */
9137
9138static struct attribute *
e142c38c 9139dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9140{
9141 unsigned int i;
9142 struct attribute *spec = NULL;
9143
9144 for (i = 0; i < die->num_attrs; ++i)
9145 {
9146 if (die->attrs[i].name == name)
10b3939b 9147 return &die->attrs[i];
c906108c
SS
9148 if (die->attrs[i].name == DW_AT_specification
9149 || die->attrs[i].name == DW_AT_abstract_origin)
9150 spec = &die->attrs[i];
9151 }
c906108c 9152
10b3939b 9153 if (spec)
f2f0e013
DJ
9154 {
9155 die = follow_die_ref (die, spec, &cu);
9156 return dwarf2_attr (die, name, cu);
9157 }
c5aa993b 9158
c906108c
SS
9159 return NULL;
9160}
9161
348e048f
DE
9162/* Return the named attribute or NULL if not there,
9163 but do not follow DW_AT_specification, etc.
9164 This is for use in contexts where we're reading .debug_types dies.
9165 Following DW_AT_specification, DW_AT_abstract_origin will take us
9166 back up the chain, and we want to go down. */
9167
9168static struct attribute *
9169dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9170 struct dwarf2_cu *cu)
9171{
9172 unsigned int i;
9173
9174 for (i = 0; i < die->num_attrs; ++i)
9175 if (die->attrs[i].name == name)
9176 return &die->attrs[i];
9177
9178 return NULL;
9179}
9180
05cf31d1
JB
9181/* Return non-zero iff the attribute NAME is defined for the given DIE,
9182 and holds a non-zero value. This function should only be used for
2dc7f7b3 9183 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9184
9185static int
9186dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9187{
9188 struct attribute *attr = dwarf2_attr (die, name, cu);
9189
9190 return (attr && DW_UNSND (attr));
9191}
9192
3ca72b44 9193static int
e142c38c 9194die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9195{
05cf31d1
JB
9196 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9197 which value is non-zero. However, we have to be careful with
9198 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9199 (via dwarf2_flag_true_p) follows this attribute. So we may
9200 end up accidently finding a declaration attribute that belongs
9201 to a different DIE referenced by the specification attribute,
9202 even though the given DIE does not have a declaration attribute. */
9203 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9204 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9205}
9206
63d06c5c 9207/* Return the die giving the specification for DIE, if there is
f2f0e013 9208 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9209 containing the return value on output. If there is no
9210 specification, but there is an abstract origin, that is
9211 returned. */
63d06c5c
DC
9212
9213static struct die_info *
f2f0e013 9214die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9215{
f2f0e013
DJ
9216 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9217 *spec_cu);
63d06c5c 9218
edb3359d
DJ
9219 if (spec_attr == NULL)
9220 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9221
63d06c5c
DC
9222 if (spec_attr == NULL)
9223 return NULL;
9224 else
f2f0e013 9225 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9226}
c906108c 9227
debd256d
JB
9228/* Free the line_header structure *LH, and any arrays and strings it
9229 refers to. */
9230static void
9231free_line_header (struct line_header *lh)
9232{
9233 if (lh->standard_opcode_lengths)
a8bc7b56 9234 xfree (lh->standard_opcode_lengths);
debd256d
JB
9235
9236 /* Remember that all the lh->file_names[i].name pointers are
9237 pointers into debug_line_buffer, and don't need to be freed. */
9238 if (lh->file_names)
a8bc7b56 9239 xfree (lh->file_names);
debd256d
JB
9240
9241 /* Similarly for the include directory names. */
9242 if (lh->include_dirs)
a8bc7b56 9243 xfree (lh->include_dirs);
debd256d 9244
a8bc7b56 9245 xfree (lh);
debd256d
JB
9246}
9247
9248
9249/* Add an entry to LH's include directory table. */
9250static void
9251add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9252{
debd256d
JB
9253 /* Grow the array if necessary. */
9254 if (lh->include_dirs_size == 0)
c5aa993b 9255 {
debd256d
JB
9256 lh->include_dirs_size = 1; /* for testing */
9257 lh->include_dirs = xmalloc (lh->include_dirs_size
9258 * sizeof (*lh->include_dirs));
9259 }
9260 else if (lh->num_include_dirs >= lh->include_dirs_size)
9261 {
9262 lh->include_dirs_size *= 2;
9263 lh->include_dirs = xrealloc (lh->include_dirs,
9264 (lh->include_dirs_size
9265 * sizeof (*lh->include_dirs)));
c5aa993b 9266 }
c906108c 9267
debd256d
JB
9268 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9269}
6e70227d 9270
debd256d
JB
9271
9272/* Add an entry to LH's file name table. */
9273static void
9274add_file_name (struct line_header *lh,
9275 char *name,
9276 unsigned int dir_index,
9277 unsigned int mod_time,
9278 unsigned int length)
9279{
9280 struct file_entry *fe;
9281
9282 /* Grow the array if necessary. */
9283 if (lh->file_names_size == 0)
9284 {
9285 lh->file_names_size = 1; /* for testing */
9286 lh->file_names = xmalloc (lh->file_names_size
9287 * sizeof (*lh->file_names));
9288 }
9289 else if (lh->num_file_names >= lh->file_names_size)
9290 {
9291 lh->file_names_size *= 2;
9292 lh->file_names = xrealloc (lh->file_names,
9293 (lh->file_names_size
9294 * sizeof (*lh->file_names)));
9295 }
9296
9297 fe = &lh->file_names[lh->num_file_names++];
9298 fe->name = name;
9299 fe->dir_index = dir_index;
9300 fe->mod_time = mod_time;
9301 fe->length = length;
aaa75496 9302 fe->included_p = 0;
cb1df416 9303 fe->symtab = NULL;
debd256d 9304}
6e70227d 9305
debd256d
JB
9306
9307/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9308 .debug_line, according to the endianness of ABFD. Return a pointer
9309 to a struct line_header, allocated using xmalloc.
debd256d
JB
9310
9311 NOTE: the strings in the include directory and file name tables of
9312 the returned object point into debug_line_buffer, and must not be
9313 freed. */
9314static struct line_header *
9315dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9316 struct dwarf2_cu *cu)
debd256d
JB
9317{
9318 struct cleanup *back_to;
9319 struct line_header *lh;
fe1b8b76 9320 gdb_byte *line_ptr;
c764a876 9321 unsigned int bytes_read, offset_size;
debd256d
JB
9322 int i;
9323 char *cur_dir, *cur_file;
9324
be391dca 9325 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9326 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9327 {
e2e0b3e5 9328 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9329 return 0;
9330 }
9331
a738430d
MK
9332 /* Make sure that at least there's room for the total_length field.
9333 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9334 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 9335 {
4d3c2250 9336 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9337 return 0;
9338 }
9339
9340 lh = xmalloc (sizeof (*lh));
9341 memset (lh, 0, sizeof (*lh));
9342 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9343 (void *) lh);
9344
dce234bc 9345 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9346
a738430d 9347 /* Read in the header. */
6e70227d 9348 lh->total_length =
c764a876
DE
9349 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9350 &bytes_read, &offset_size);
debd256d 9351 line_ptr += bytes_read;
dce234bc
PP
9352 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9353 + dwarf2_per_objfile->line.size))
debd256d 9354 {
4d3c2250 9355 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9356 return 0;
9357 }
9358 lh->statement_program_end = line_ptr + lh->total_length;
9359 lh->version = read_2_bytes (abfd, line_ptr);
9360 line_ptr += 2;
c764a876
DE
9361 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9362 line_ptr += offset_size;
debd256d
JB
9363 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9364 line_ptr += 1;
2dc7f7b3
TT
9365 if (lh->version >= 4)
9366 {
9367 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9368 line_ptr += 1;
9369 }
9370 else
9371 lh->maximum_ops_per_instruction = 1;
9372
9373 if (lh->maximum_ops_per_instruction == 0)
9374 {
9375 lh->maximum_ops_per_instruction = 1;
9376 complaint (&symfile_complaints,
9377 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9378 }
9379
debd256d
JB
9380 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9381 line_ptr += 1;
9382 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9383 line_ptr += 1;
9384 lh->line_range = read_1_byte (abfd, line_ptr);
9385 line_ptr += 1;
9386 lh->opcode_base = read_1_byte (abfd, line_ptr);
9387 line_ptr += 1;
9388 lh->standard_opcode_lengths
fe1b8b76 9389 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9390
9391 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9392 for (i = 1; i < lh->opcode_base; ++i)
9393 {
9394 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9395 line_ptr += 1;
9396 }
9397
a738430d 9398 /* Read directory table. */
9b1c24c8 9399 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9400 {
9401 line_ptr += bytes_read;
9402 add_include_dir (lh, cur_dir);
9403 }
9404 line_ptr += bytes_read;
9405
a738430d 9406 /* Read file name table. */
9b1c24c8 9407 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9408 {
9409 unsigned int dir_index, mod_time, length;
9410
9411 line_ptr += bytes_read;
9412 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9413 line_ptr += bytes_read;
9414 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9415 line_ptr += bytes_read;
9416 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9417 line_ptr += bytes_read;
9418
9419 add_file_name (lh, cur_file, dir_index, mod_time, length);
9420 }
9421 line_ptr += bytes_read;
6e70227d 9422 lh->statement_program_start = line_ptr;
debd256d 9423
dce234bc
PP
9424 if (line_ptr > (dwarf2_per_objfile->line.buffer
9425 + dwarf2_per_objfile->line.size))
4d3c2250 9426 complaint (&symfile_complaints,
e2e0b3e5 9427 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
9428
9429 discard_cleanups (back_to);
9430 return lh;
9431}
c906108c 9432
5fb290d7
DJ
9433/* This function exists to work around a bug in certain compilers
9434 (particularly GCC 2.95), in which the first line number marker of a
9435 function does not show up until after the prologue, right before
9436 the second line number marker. This function shifts ADDRESS down
9437 to the beginning of the function if necessary, and is called on
9438 addresses passed to record_line. */
9439
9440static CORE_ADDR
e142c38c 9441check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
9442{
9443 struct function_range *fn;
9444
9445 /* Find the function_range containing address. */
e142c38c 9446 if (!cu->first_fn)
5fb290d7
DJ
9447 return address;
9448
e142c38c
DJ
9449 if (!cu->cached_fn)
9450 cu->cached_fn = cu->first_fn;
5fb290d7 9451
e142c38c 9452 fn = cu->cached_fn;
5fb290d7
DJ
9453 while (fn)
9454 if (fn->lowpc <= address && fn->highpc > address)
9455 goto found;
9456 else
9457 fn = fn->next;
9458
e142c38c
DJ
9459 fn = cu->first_fn;
9460 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
9461 if (fn->lowpc <= address && fn->highpc > address)
9462 goto found;
9463 else
9464 fn = fn->next;
9465
9466 return address;
9467
9468 found:
9469 if (fn->seen_line)
9470 return address;
9471 if (address != fn->lowpc)
4d3c2250 9472 complaint (&symfile_complaints,
e2e0b3e5 9473 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 9474 (unsigned long) address, fn->name);
5fb290d7
DJ
9475 fn->seen_line = 1;
9476 return fn->lowpc;
9477}
9478
aaa75496
JB
9479/* Decode the Line Number Program (LNP) for the given line_header
9480 structure and CU. The actual information extracted and the type
9481 of structures created from the LNP depends on the value of PST.
9482
9483 1. If PST is NULL, then this procedure uses the data from the program
9484 to create all necessary symbol tables, and their linetables.
9485 The compilation directory of the file is passed in COMP_DIR,
9486 and must not be NULL.
6e70227d 9487
aaa75496
JB
9488 2. If PST is not NULL, this procedure reads the program to determine
9489 the list of files included by the unit represented by PST, and
9490 builds all the associated partial symbol tables. In this case,
9491 the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
9492 is not used to compute the full name of the symtab, and therefore
9493 omitting it when building the partial symtab does not introduce
9494 the potential for inconsistency - a partial symtab and its associated
9495 symbtab having a different fullname -). */
debd256d 9496
c906108c 9497static void
debd256d 9498dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 9499 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 9500{
a8c50c1f 9501 gdb_byte *line_ptr, *extended_end;
fe1b8b76 9502 gdb_byte *line_end;
a8c50c1f 9503 unsigned int bytes_read, extended_len;
c906108c 9504 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
9505 CORE_ADDR baseaddr;
9506 struct objfile *objfile = cu->objfile;
fbf65064 9507 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 9508 const int decode_for_pst_p = (pst != NULL);
cb1df416 9509 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
9510
9511 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9512
debd256d
JB
9513 line_ptr = lh->statement_program_start;
9514 line_end = lh->statement_program_end;
c906108c
SS
9515
9516 /* Read the statement sequences until there's nothing left. */
9517 while (line_ptr < line_end)
9518 {
9519 /* state machine registers */
9520 CORE_ADDR address = 0;
9521 unsigned int file = 1;
9522 unsigned int line = 1;
9523 unsigned int column = 0;
debd256d 9524 int is_stmt = lh->default_is_stmt;
c906108c
SS
9525 int basic_block = 0;
9526 int end_sequence = 0;
fbf65064 9527 CORE_ADDR addr;
2dc7f7b3 9528 unsigned char op_index = 0;
c906108c 9529
aaa75496 9530 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 9531 {
aaa75496 9532 /* Start a subfile for the current file of the state machine. */
debd256d
JB
9533 /* lh->include_dirs and lh->file_names are 0-based, but the
9534 directory and file name numbers in the statement program
9535 are 1-based. */
9536 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 9537 char *dir = NULL;
a738430d 9538
debd256d
JB
9539 if (fe->dir_index)
9540 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
9541
9542 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
9543 }
9544
a738430d 9545 /* Decode the table. */
c5aa993b 9546 while (!end_sequence)
c906108c
SS
9547 {
9548 op_code = read_1_byte (abfd, line_ptr);
9549 line_ptr += 1;
59205f5a
JB
9550 if (line_ptr > line_end)
9551 {
9552 dwarf2_debug_line_missing_end_sequence_complaint ();
9553 break;
9554 }
9aa1fe7e 9555
debd256d 9556 if (op_code >= lh->opcode_base)
6e70227d 9557 {
a738430d 9558 /* Special operand. */
debd256d 9559 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
9560 address += (((op_index + (adj_opcode / lh->line_range))
9561 / lh->maximum_ops_per_instruction)
9562 * lh->minimum_instruction_length);
9563 op_index = ((op_index + (adj_opcode / lh->line_range))
9564 % lh->maximum_ops_per_instruction);
debd256d 9565 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 9566 if (lh->num_file_names < file || file == 0)
25e43795 9567 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
9568 /* For now we ignore lines not starting on an
9569 instruction boundary. */
9570 else if (op_index == 0)
25e43795
DJ
9571 {
9572 lh->file_names[file - 1].included_p = 1;
ca5f395d 9573 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
9574 {
9575 if (last_subfile != current_subfile)
9576 {
9577 addr = gdbarch_addr_bits_remove (gdbarch, address);
9578 if (last_subfile)
9579 record_line (last_subfile, 0, addr);
9580 last_subfile = current_subfile;
9581 }
25e43795 9582 /* Append row to matrix using current values. */
fbf65064
UW
9583 addr = check_cu_functions (address, cu);
9584 addr = gdbarch_addr_bits_remove (gdbarch, addr);
9585 record_line (current_subfile, line, addr);
366da635 9586 }
25e43795 9587 }
ca5f395d 9588 basic_block = 0;
9aa1fe7e
GK
9589 }
9590 else switch (op_code)
c906108c
SS
9591 {
9592 case DW_LNS_extended_op:
a8c50c1f 9593 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 9594 line_ptr += bytes_read;
a8c50c1f 9595 extended_end = line_ptr + extended_len;
c906108c
SS
9596 extended_op = read_1_byte (abfd, line_ptr);
9597 line_ptr += 1;
9598 switch (extended_op)
9599 {
9600 case DW_LNE_end_sequence:
9601 end_sequence = 1;
c906108c
SS
9602 break;
9603 case DW_LNE_set_address:
e7c27a73 9604 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 9605 op_index = 0;
107d2387
AC
9606 line_ptr += bytes_read;
9607 address += baseaddr;
c906108c
SS
9608 break;
9609 case DW_LNE_define_file:
debd256d
JB
9610 {
9611 char *cur_file;
9612 unsigned int dir_index, mod_time, length;
6e70227d 9613
9b1c24c8 9614 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
debd256d
JB
9615 line_ptr += bytes_read;
9616 dir_index =
9617 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9618 line_ptr += bytes_read;
9619 mod_time =
9620 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9621 line_ptr += bytes_read;
9622 length =
9623 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9624 line_ptr += bytes_read;
9625 add_file_name (lh, cur_file, dir_index, mod_time, length);
9626 }
c906108c 9627 break;
d0c6ba3d
CC
9628 case DW_LNE_set_discriminator:
9629 /* The discriminator is not interesting to the debugger;
9630 just ignore it. */
9631 line_ptr = extended_end;
9632 break;
c906108c 9633 default:
4d3c2250 9634 complaint (&symfile_complaints,
e2e0b3e5 9635 _("mangled .debug_line section"));
debd256d 9636 return;
c906108c 9637 }
a8c50c1f
DJ
9638 /* Make sure that we parsed the extended op correctly. If e.g.
9639 we expected a different address size than the producer used,
9640 we may have read the wrong number of bytes. */
9641 if (line_ptr != extended_end)
9642 {
9643 complaint (&symfile_complaints,
9644 _("mangled .debug_line section"));
9645 return;
9646 }
c906108c
SS
9647 break;
9648 case DW_LNS_copy:
59205f5a 9649 if (lh->num_file_names < file || file == 0)
25e43795
DJ
9650 dwarf2_debug_line_missing_file_complaint ();
9651 else
366da635 9652 {
25e43795 9653 lh->file_names[file - 1].included_p = 1;
ca5f395d 9654 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
9655 {
9656 if (last_subfile != current_subfile)
9657 {
9658 addr = gdbarch_addr_bits_remove (gdbarch, address);
9659 if (last_subfile)
9660 record_line (last_subfile, 0, addr);
9661 last_subfile = current_subfile;
9662 }
9663 addr = check_cu_functions (address, cu);
9664 addr = gdbarch_addr_bits_remove (gdbarch, addr);
9665 record_line (current_subfile, line, addr);
9666 }
366da635 9667 }
c906108c
SS
9668 basic_block = 0;
9669 break;
9670 case DW_LNS_advance_pc:
2dc7f7b3
TT
9671 {
9672 CORE_ADDR adjust
9673 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9674
9675 address += (((op_index + adjust)
9676 / lh->maximum_ops_per_instruction)
9677 * lh->minimum_instruction_length);
9678 op_index = ((op_index + adjust)
9679 % lh->maximum_ops_per_instruction);
9680 line_ptr += bytes_read;
9681 }
c906108c
SS
9682 break;
9683 case DW_LNS_advance_line:
9684 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
9685 line_ptr += bytes_read;
9686 break;
9687 case DW_LNS_set_file:
debd256d 9688 {
a738430d
MK
9689 /* The arrays lh->include_dirs and lh->file_names are
9690 0-based, but the directory and file name numbers in
9691 the statement program are 1-based. */
debd256d 9692 struct file_entry *fe;
4f1520fb 9693 char *dir = NULL;
a738430d 9694
debd256d
JB
9695 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9696 line_ptr += bytes_read;
59205f5a 9697 if (lh->num_file_names < file || file == 0)
25e43795
DJ
9698 dwarf2_debug_line_missing_file_complaint ();
9699 else
9700 {
9701 fe = &lh->file_names[file - 1];
9702 if (fe->dir_index)
9703 dir = lh->include_dirs[fe->dir_index - 1];
9704 if (!decode_for_pst_p)
9705 {
9706 last_subfile = current_subfile;
9707 dwarf2_start_subfile (fe->name, dir, comp_dir);
9708 }
9709 }
debd256d 9710 }
c906108c
SS
9711 break;
9712 case DW_LNS_set_column:
9713 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9714 line_ptr += bytes_read;
9715 break;
9716 case DW_LNS_negate_stmt:
9717 is_stmt = (!is_stmt);
9718 break;
9719 case DW_LNS_set_basic_block:
9720 basic_block = 1;
9721 break;
c2c6d25f
JM
9722 /* Add to the address register of the state machine the
9723 address increment value corresponding to special opcode
a738430d
MK
9724 255. I.e., this value is scaled by the minimum
9725 instruction length since special opcode 255 would have
9726 scaled the the increment. */
c906108c 9727 case DW_LNS_const_add_pc:
2dc7f7b3
TT
9728 {
9729 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
9730
9731 address += (((op_index + adjust)
9732 / lh->maximum_ops_per_instruction)
9733 * lh->minimum_instruction_length);
9734 op_index = ((op_index + adjust)
9735 % lh->maximum_ops_per_instruction);
9736 }
c906108c
SS
9737 break;
9738 case DW_LNS_fixed_advance_pc:
9739 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 9740 op_index = 0;
c906108c
SS
9741 line_ptr += 2;
9742 break;
9aa1fe7e 9743 default:
a738430d
MK
9744 {
9745 /* Unknown standard opcode, ignore it. */
9aa1fe7e 9746 int i;
a738430d 9747
debd256d 9748 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
9749 {
9750 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9751 line_ptr += bytes_read;
9752 }
9753 }
c906108c
SS
9754 }
9755 }
59205f5a
JB
9756 if (lh->num_file_names < file || file == 0)
9757 dwarf2_debug_line_missing_file_complaint ();
9758 else
9759 {
9760 lh->file_names[file - 1].included_p = 1;
9761 if (!decode_for_pst_p)
fbf65064
UW
9762 {
9763 addr = gdbarch_addr_bits_remove (gdbarch, address);
9764 record_line (current_subfile, 0, addr);
9765 }
59205f5a 9766 }
c906108c 9767 }
aaa75496
JB
9768
9769 if (decode_for_pst_p)
9770 {
9771 int file_index;
9772
9773 /* Now that we're done scanning the Line Header Program, we can
9774 create the psymtab of each included file. */
9775 for (file_index = 0; file_index < lh->num_file_names; file_index++)
9776 if (lh->file_names[file_index].included_p == 1)
9777 {
5b5464ad
JB
9778 const struct file_entry fe = lh->file_names [file_index];
9779 char *include_name = fe.name;
9780 char *dir_name = NULL;
9781 char *pst_filename = pst->filename;
9782
9783 if (fe.dir_index)
9784 dir_name = lh->include_dirs[fe.dir_index - 1];
9785
9786 if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
9787 {
1754f103
MK
9788 include_name = concat (dir_name, SLASH_STRING,
9789 include_name, (char *)NULL);
5b5464ad
JB
9790 make_cleanup (xfree, include_name);
9791 }
9792
9793 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
9794 {
1754f103
MK
9795 pst_filename = concat (pst->dirname, SLASH_STRING,
9796 pst_filename, (char *)NULL);
5b5464ad
JB
9797 make_cleanup (xfree, pst_filename);
9798 }
9799
9800 if (strcmp (include_name, pst_filename) != 0)
aaa75496
JB
9801 dwarf2_create_include_psymtab (include_name, pst, objfile);
9802 }
9803 }
cb1df416
DJ
9804 else
9805 {
9806 /* Make sure a symtab is created for every file, even files
9807 which contain only variables (i.e. no code with associated
9808 line numbers). */
9809
9810 int i;
9811 struct file_entry *fe;
9812
9813 for (i = 0; i < lh->num_file_names; i++)
9814 {
9815 char *dir = NULL;
9a619af0 9816
cb1df416
DJ
9817 fe = &lh->file_names[i];
9818 if (fe->dir_index)
9819 dir = lh->include_dirs[fe->dir_index - 1];
9820 dwarf2_start_subfile (fe->name, dir, comp_dir);
9821
9822 /* Skip the main file; we don't need it, and it must be
9823 allocated last, so that it will show up before the
9824 non-primary symtabs in the objfile's symtab list. */
9825 if (current_subfile == first_subfile)
9826 continue;
9827
9828 if (current_subfile->symtab == NULL)
9829 current_subfile->symtab = allocate_symtab (current_subfile->name,
9830 cu->objfile);
9831 fe->symtab = current_subfile->symtab;
9832 }
9833 }
c906108c
SS
9834}
9835
9836/* Start a subfile for DWARF. FILENAME is the name of the file and
9837 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
9838 or NULL if not known. COMP_DIR is the compilation directory for the
9839 linetable's compilation unit or NULL if not known.
c906108c
SS
9840 This routine tries to keep line numbers from identical absolute and
9841 relative file names in a common subfile.
9842
9843 Using the `list' example from the GDB testsuite, which resides in
9844 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
9845 of /srcdir/list0.c yields the following debugging information for list0.c:
9846
c5aa993b
JM
9847 DW_AT_name: /srcdir/list0.c
9848 DW_AT_comp_dir: /compdir
357e46e7 9849 files.files[0].name: list0.h
c5aa993b 9850 files.files[0].dir: /srcdir
357e46e7 9851 files.files[1].name: list0.c
c5aa993b 9852 files.files[1].dir: /srcdir
c906108c
SS
9853
9854 The line number information for list0.c has to end up in a single
4f1520fb
FR
9855 subfile, so that `break /srcdir/list0.c:1' works as expected.
9856 start_subfile will ensure that this happens provided that we pass the
9857 concatenation of files.files[1].dir and files.files[1].name as the
9858 subfile's name. */
c906108c
SS
9859
9860static void
4f1520fb 9861dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 9862{
4f1520fb
FR
9863 char *fullname;
9864
9865 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
9866 `start_symtab' will always pass the contents of DW_AT_comp_dir as
9867 second argument to start_subfile. To be consistent, we do the
9868 same here. In order not to lose the line information directory,
9869 we concatenate it to the filename when it makes sense.
9870 Note that the Dwarf3 standard says (speaking of filenames in line
9871 information): ``The directory index is ignored for file names
9872 that represent full path names''. Thus ignoring dirname in the
9873 `else' branch below isn't an issue. */
c906108c 9874
d5166ae1 9875 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
9876 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
9877 else
9878 fullname = filename;
c906108c 9879
4f1520fb
FR
9880 start_subfile (fullname, comp_dir);
9881
9882 if (fullname != filename)
9883 xfree (fullname);
c906108c
SS
9884}
9885
4c2df51b
DJ
9886static void
9887var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 9888 struct dwarf2_cu *cu)
4c2df51b 9889{
e7c27a73
DJ
9890 struct objfile *objfile = cu->objfile;
9891 struct comp_unit_head *cu_header = &cu->header;
9892
4c2df51b
DJ
9893 /* NOTE drow/2003-01-30: There used to be a comment and some special
9894 code here to turn a symbol with DW_AT_external and a
9895 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
9896 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
9897 with some versions of binutils) where shared libraries could have
9898 relocations against symbols in their debug information - the
9899 minimal symbol would have the right address, but the debug info
9900 would not. It's no longer necessary, because we will explicitly
9901 apply relocations when we read in the debug information now. */
9902
9903 /* A DW_AT_location attribute with no contents indicates that a
9904 variable has been optimized away. */
9905 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
9906 {
9907 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
9908 return;
9909 }
9910
9911 /* Handle one degenerate form of location expression specially, to
9912 preserve GDB's previous behavior when section offsets are
9913 specified. If this is just a DW_OP_addr then mark this symbol
9914 as LOC_STATIC. */
9915
9916 if (attr_form_is_block (attr)
9917 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
9918 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
9919 {
891d2f0b 9920 unsigned int dummy;
4c2df51b
DJ
9921
9922 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 9923 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 9924 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
9925 fixup_symbol_section (sym, objfile);
9926 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
9927 SYMBOL_SECTION (sym));
4c2df51b
DJ
9928 return;
9929 }
9930
9931 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
9932 expression evaluator, and use LOC_COMPUTED only when necessary
9933 (i.e. when the value of a register or memory location is
9934 referenced, or a thread-local block, etc.). Then again, it might
9935 not be worthwhile. I'm assuming that it isn't unless performance
9936 or memory numbers show me otherwise. */
9937
e7c27a73 9938 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
9939 SYMBOL_CLASS (sym) = LOC_COMPUTED;
9940}
9941
c906108c
SS
9942/* Given a pointer to a DWARF information entry, figure out if we need
9943 to make a symbol table entry for it, and if so, create a new entry
9944 and return a pointer to it.
9945 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
9946 used the passed type.
9947 If SPACE is not NULL, use it to hold the new symbol. If it is
9948 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
9949
9950static struct symbol *
34eaf542
TT
9951new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
9952 struct symbol *space)
c906108c 9953{
e7c27a73 9954 struct objfile *objfile = cu->objfile;
c906108c
SS
9955 struct symbol *sym = NULL;
9956 char *name;
9957 struct attribute *attr = NULL;
9958 struct attribute *attr2 = NULL;
e142c38c 9959 CORE_ADDR baseaddr;
edb3359d 9960 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
9961
9962 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9963
94af9270 9964 name = dwarf2_name (die, cu);
c906108c
SS
9965 if (name)
9966 {
94af9270 9967 const char *linkagename;
34eaf542 9968 int suppress_add = 0;
94af9270 9969
34eaf542
TT
9970 if (space)
9971 sym = space;
9972 else
9973 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 9974 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
9975
9976 /* Cache this symbol's name and the name's demangled form (if any). */
e142c38c 9977 SYMBOL_LANGUAGE (sym) = cu->language;
94af9270
KS
9978 linkagename = dwarf2_physname (name, die, cu);
9979 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 9980
f55ee35c
JK
9981 /* Fortran does not have mangling standard and the mangling does differ
9982 between gfortran, iFort etc. */
9983 if (cu->language == language_fortran
b250c185 9984 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
9985 symbol_set_demangled_name (&(sym->ginfo),
9986 (char *) dwarf2_full_name (name, die, cu),
9987 NULL);
f55ee35c 9988
c906108c 9989 /* Default assumptions.
c5aa993b 9990 Use the passed type or decode it from the die. */
176620f1 9991 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 9992 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
9993 if (type != NULL)
9994 SYMBOL_TYPE (sym) = type;
9995 else
e7c27a73 9996 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
9997 attr = dwarf2_attr (die,
9998 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
9999 cu);
c906108c
SS
10000 if (attr)
10001 {
10002 SYMBOL_LINE (sym) = DW_UNSND (attr);
10003 }
cb1df416 10004
edb3359d
DJ
10005 attr = dwarf2_attr (die,
10006 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10007 cu);
cb1df416
DJ
10008 if (attr)
10009 {
10010 int file_index = DW_UNSND (attr);
9a619af0 10011
cb1df416
DJ
10012 if (cu->line_header == NULL
10013 || file_index > cu->line_header->num_file_names)
10014 complaint (&symfile_complaints,
10015 _("file index out of range"));
1c3d648d 10016 else if (file_index > 0)
cb1df416
DJ
10017 {
10018 struct file_entry *fe;
9a619af0 10019
cb1df416
DJ
10020 fe = &cu->line_header->file_names[file_index - 1];
10021 SYMBOL_SYMTAB (sym) = fe->symtab;
10022 }
10023 }
10024
c906108c
SS
10025 switch (die->tag)
10026 {
10027 case DW_TAG_label:
e142c38c 10028 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10029 if (attr)
10030 {
10031 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10032 }
0f5238ed
TT
10033 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10034 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10035 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10036 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10037 break;
10038 case DW_TAG_subprogram:
10039 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10040 finish_block. */
10041 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10042 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10043 if ((attr2 && (DW_UNSND (attr2) != 0))
10044 || cu->language == language_ada)
c906108c 10045 {
2cfa0c8d
JB
10046 /* Subprograms marked external are stored as a global symbol.
10047 Ada subprograms, whether marked external or not, are always
10048 stored as a global symbol, because we want to be able to
10049 access them globally. For instance, we want to be able
10050 to break on a nested subprogram without having to
10051 specify the context. */
c906108c
SS
10052 add_symbol_to_list (sym, &global_symbols);
10053 }
10054 else
10055 {
e142c38c 10056 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10057 }
10058 break;
edb3359d
DJ
10059 case DW_TAG_inlined_subroutine:
10060 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10061 finish_block. */
10062 SYMBOL_CLASS (sym) = LOC_BLOCK;
10063 SYMBOL_INLINED (sym) = 1;
10064 /* Do not add the symbol to any lists. It will be found via
10065 BLOCK_FUNCTION from the blockvector. */
10066 break;
34eaf542
TT
10067 case DW_TAG_template_value_param:
10068 suppress_add = 1;
10069 /* Fall through. */
c906108c 10070 case DW_TAG_variable:
254e6b9e 10071 case DW_TAG_member:
c906108c
SS
10072 /* Compilation with minimal debug info may result in variables
10073 with missing type entries. Change the misleading `void' type
10074 to something sensible. */
10075 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10076 SYMBOL_TYPE (sym)
46bf5051 10077 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10078
e142c38c 10079 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10080 /* In the case of DW_TAG_member, we should only be called for
10081 static const members. */
10082 if (die->tag == DW_TAG_member)
10083 {
3863f96c
DE
10084 /* dwarf2_add_field uses die_is_declaration,
10085 so we do the same. */
254e6b9e
DE
10086 gdb_assert (die_is_declaration (die, cu));
10087 gdb_assert (attr);
10088 }
c906108c
SS
10089 if (attr)
10090 {
e7c27a73 10091 dwarf2_const_value (attr, sym, cu);
e142c38c 10092 attr2 = dwarf2_attr (die, DW_AT_external, cu);
34eaf542
TT
10093 if (suppress_add)
10094 {
10095 sym->hash_next = objfile->template_symbols;
10096 objfile->template_symbols = sym;
10097 }
c906108c 10098 else
34eaf542
TT
10099 {
10100 if (attr2 && (DW_UNSND (attr2) != 0))
10101 add_symbol_to_list (sym, &global_symbols);
10102 else
10103 add_symbol_to_list (sym, cu->list_in_scope);
10104 }
c906108c
SS
10105 break;
10106 }
e142c38c 10107 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10108 if (attr)
10109 {
e7c27a73 10110 var_decode_location (attr, sym, cu);
e142c38c 10111 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10112 if (SYMBOL_CLASS (sym) == LOC_STATIC
10113 && SYMBOL_VALUE_ADDRESS (sym) == 0
10114 && !dwarf2_per_objfile->has_section_at_zero)
10115 {
10116 /* When a static variable is eliminated by the linker,
10117 the corresponding debug information is not stripped
10118 out, but the variable address is set to null;
10119 do not add such variables into symbol table. */
10120 }
10121 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68
TT
10122 {
10123 struct pending **list_to_add;
10124
f55ee35c
JK
10125 /* Workaround gfortran PR debug/40040 - it uses
10126 DW_AT_location for variables in -fPIC libraries which may
10127 get overriden by other libraries/executable and get
10128 a different address. Resolve it by the minimal symbol
10129 which may come from inferior's executable using copy
10130 relocation. Make this workaround only for gfortran as for
10131 other compilers GDB cannot guess the minimal symbol
10132 Fortran mangling kind. */
10133 if (cu->language == language_fortran && die->parent
10134 && die->parent->tag == DW_TAG_module
10135 && cu->producer
10136 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10137 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10138
1c809c68
TT
10139 /* A variable with DW_AT_external is never static,
10140 but it may be block-scoped. */
10141 list_to_add = (cu->list_in_scope == &file_symbols
10142 ? &global_symbols : cu->list_in_scope);
10143 add_symbol_to_list (sym, list_to_add);
10144 }
c906108c 10145 else
e142c38c 10146 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10147 }
10148 else
10149 {
10150 /* We do not know the address of this symbol.
c5aa993b
JM
10151 If it is an external symbol and we have type information
10152 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10153 The address of the variable will then be determined from
10154 the minimal symbol table whenever the variable is
10155 referenced. */
e142c38c 10156 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10157 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10158 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10159 {
0fe7935b
DJ
10160 struct pending **list_to_add;
10161
10162 /* A variable with DW_AT_external is never static, but it
10163 may be block-scoped. */
10164 list_to_add = (cu->list_in_scope == &file_symbols
10165 ? &global_symbols : cu->list_in_scope);
10166
c906108c 10167 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
34eaf542
TT
10168 if (suppress_add)
10169 {
10170 sym->hash_next = objfile->template_symbols;
10171 objfile->template_symbols = sym;
10172 }
10173 else
10174 add_symbol_to_list (sym, list_to_add);
c906108c 10175 }
442ddf59
JK
10176 else if (!die_is_declaration (die, cu))
10177 {
10178 /* Use the default LOC_OPTIMIZED_OUT class. */
10179 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
34eaf542
TT
10180 if (suppress_add)
10181 {
10182 sym->hash_next = objfile->template_symbols;
10183 objfile->template_symbols = sym;
10184 }
10185 else
10186 add_symbol_to_list (sym, cu->list_in_scope);
442ddf59 10187 }
c906108c
SS
10188 }
10189 break;
10190 case DW_TAG_formal_parameter:
edb3359d
DJ
10191 /* If we are inside a function, mark this as an argument. If
10192 not, we might be looking at an argument to an inlined function
10193 when we do not have enough information to show inlined frames;
10194 pretend it's a local variable in that case so that the user can
10195 still see it. */
10196 if (context_stack_depth > 0
10197 && context_stack[context_stack_depth - 1].name != NULL)
10198 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10199 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10200 if (attr)
10201 {
e7c27a73 10202 var_decode_location (attr, sym, cu);
c906108c 10203 }
e142c38c 10204 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10205 if (attr)
10206 {
e7c27a73 10207 dwarf2_const_value (attr, sym, cu);
c906108c 10208 }
f346a30d
PM
10209 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10210 if (attr && DW_UNSND (attr))
10211 {
10212 struct type *ref_type;
10213
10214 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10215 SYMBOL_TYPE (sym) = ref_type;
10216 }
10217
e142c38c 10218 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10219 break;
10220 case DW_TAG_unspecified_parameters:
10221 /* From varargs functions; gdb doesn't seem to have any
10222 interest in this information, so just ignore it for now.
10223 (FIXME?) */
10224 break;
34eaf542
TT
10225 case DW_TAG_template_type_param:
10226 suppress_add = 1;
10227 /* Fall through. */
c906108c 10228 case DW_TAG_class_type:
680b30c7 10229 case DW_TAG_interface_type:
c906108c
SS
10230 case DW_TAG_structure_type:
10231 case DW_TAG_union_type:
72019c9c 10232 case DW_TAG_set_type:
c906108c
SS
10233 case DW_TAG_enumeration_type:
10234 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10235 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 10236
63d06c5c 10237 {
987504bb 10238 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
10239 really ever be static objects: otherwise, if you try
10240 to, say, break of a class's method and you're in a file
10241 which doesn't mention that class, it won't work unless
10242 the check for all static symbols in lookup_symbol_aux
10243 saves you. See the OtherFileClass tests in
10244 gdb.c++/namespace.exp. */
10245
34eaf542
TT
10246 if (suppress_add)
10247 {
10248 sym->hash_next = objfile->template_symbols;
10249 objfile->template_symbols = sym;
10250 }
10251 else
10252 {
10253 struct pending **list_to_add;
63d06c5c 10254
34eaf542
TT
10255 list_to_add = (cu->list_in_scope == &file_symbols
10256 && (cu->language == language_cplus
10257 || cu->language == language_java)
10258 ? &global_symbols : cu->list_in_scope);
6e70227d 10259
34eaf542
TT
10260 add_symbol_to_list (sym, list_to_add);
10261 }
63d06c5c
DC
10262
10263 /* The semantics of C++ state that "struct foo { ... }" also
987504bb 10264 defines a typedef for "foo". A Java class declaration also
5eeb2539 10265 defines a typedef for the class. */
987504bb 10266 if (cu->language == language_cplus
8c6860bb
JB
10267 || cu->language == language_java
10268 || cu->language == language_ada)
63d06c5c 10269 {
d8151005
DJ
10270 /* The symbol's name is already allocated along with
10271 this objfile, so we don't need to duplicate it for
10272 the type. */
63d06c5c 10273 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
77ef991d 10274 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
63d06c5c
DC
10275 }
10276 }
c906108c
SS
10277 break;
10278 case DW_TAG_typedef:
63d06c5c
DC
10279 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10280 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 10281 add_symbol_to_list (sym, cu->list_in_scope);
63d06c5c 10282 break;
c906108c 10283 case DW_TAG_base_type:
a02abb62 10284 case DW_TAG_subrange_type:
c906108c 10285 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10286 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 10287 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10288 break;
10289 case DW_TAG_enumerator:
e142c38c 10290 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10291 if (attr)
10292 {
e7c27a73 10293 dwarf2_const_value (attr, sym, cu);
c906108c 10294 }
63d06c5c
DC
10295 {
10296 /* NOTE: carlton/2003-11-10: See comment above in the
10297 DW_TAG_class_type, etc. block. */
10298
10299 struct pending **list_to_add;
10300
e142c38c 10301 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10302 && (cu->language == language_cplus
10303 || cu->language == language_java)
e142c38c 10304 ? &global_symbols : cu->list_in_scope);
6e70227d 10305
63d06c5c
DC
10306 add_symbol_to_list (sym, list_to_add);
10307 }
c906108c 10308 break;
5c4e30ca
DC
10309 case DW_TAG_namespace:
10310 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10311 add_symbol_to_list (sym, &global_symbols);
10312 break;
c906108c
SS
10313 default:
10314 /* Not a tag we recognize. Hopefully we aren't processing
10315 trash data, but since we must specifically ignore things
10316 we don't recognize, there is nothing else we should do at
10317 this point. */
e2e0b3e5 10318 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10319 dwarf_tag_name (die->tag));
c906108c
SS
10320 break;
10321 }
df8a16a1
DJ
10322
10323 /* For the benefit of old versions of GCC, check for anonymous
10324 namespaces based on the demangled name. */
10325 if (!processing_has_namespace_info
94af9270 10326 && cu->language == language_cplus)
df8a16a1 10327 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10328 }
10329 return (sym);
10330}
10331
34eaf542
TT
10332/* A wrapper for new_symbol_full that always allocates a new symbol. */
10333
10334static struct symbol *
10335new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10336{
10337 return new_symbol_full (die, type, cu, NULL);
10338}
10339
c906108c
SS
10340/* Copy constant value from an attribute to a symbol. */
10341
10342static void
107d2387 10343dwarf2_const_value (struct attribute *attr, struct symbol *sym,
e7c27a73 10344 struct dwarf2_cu *cu)
c906108c 10345{
e7c27a73
DJ
10346 struct objfile *objfile = cu->objfile;
10347 struct comp_unit_head *cu_header = &cu->header;
e17a4113
UW
10348 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10349 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
c906108c
SS
10350 struct dwarf_block *blk;
10351
10352 switch (attr->form)
10353 {
10354 case DW_FORM_addr:
ac56253d
TT
10355 {
10356 struct dwarf2_locexpr_baton *baton;
10357 gdb_byte *data;
10358
10359 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
10360 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
10361 cu_header->addr_size,
10362 TYPE_LENGTH (SYMBOL_TYPE
10363 (sym)));
10364 /* Symbols of this form are reasonably rare, so we just
10365 piggyback on the existing location code rather than writing
10366 a new implementation of symbol_computed_ops. */
10367 baton = obstack_alloc (&objfile->objfile_obstack,
10368 sizeof (struct dwarf2_locexpr_baton));
10369 baton->per_cu = cu->per_cu;
10370 gdb_assert (baton->per_cu);
10371
10372 baton->size = 2 + cu_header->addr_size;
10373 data = obstack_alloc (&objfile->objfile_obstack, baton->size);
10374 baton->data = data;
10375
10376 data[0] = DW_OP_addr;
10377 store_unsigned_integer (&data[1], cu_header->addr_size,
10378 byte_order, DW_ADDR (attr));
10379 data[cu_header->addr_size + 1] = DW_OP_stack_value;
10380
10381 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10382 SYMBOL_LOCATION_BATON (sym) = baton;
10383 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10384 }
c906108c 10385 break;
4ac36638 10386 case DW_FORM_string:
93b5768b
PA
10387 case DW_FORM_strp:
10388 /* DW_STRING is already allocated on the obstack, point directly
10389 to it. */
10390 SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
10391 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10392 break;
c906108c
SS
10393 case DW_FORM_block1:
10394 case DW_FORM_block2:
10395 case DW_FORM_block4:
10396 case DW_FORM_block:
2dc7f7b3 10397 case DW_FORM_exprloc:
c906108c
SS
10398 blk = DW_BLOCK (attr);
10399 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
3567439c 10400 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
4d3c2250
KB
10401 blk->size,
10402 TYPE_LENGTH (SYMBOL_TYPE
10403 (sym)));
4e38b386 10404 SYMBOL_VALUE_BYTES (sym) =
4a146b47 10405 obstack_alloc (&objfile->objfile_obstack, blk->size);
c906108c
SS
10406 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
10407 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10408 break;
2df3850c
JM
10409
10410 /* The DW_AT_const_value attributes are supposed to carry the
10411 symbol's value "represented as it would be on the target
10412 architecture." By the time we get here, it's already been
10413 converted to host endianness, so we just need to sign- or
10414 zero-extend it as appropriate. */
10415 case DW_FORM_data1:
10416 dwarf2_const_value_data (attr, sym, 8);
10417 break;
c906108c 10418 case DW_FORM_data2:
2df3850c
JM
10419 dwarf2_const_value_data (attr, sym, 16);
10420 break;
c906108c 10421 case DW_FORM_data4:
2df3850c
JM
10422 dwarf2_const_value_data (attr, sym, 32);
10423 break;
c906108c 10424 case DW_FORM_data8:
2df3850c
JM
10425 dwarf2_const_value_data (attr, sym, 64);
10426 break;
10427
c906108c 10428 case DW_FORM_sdata:
2df3850c
JM
10429 SYMBOL_VALUE (sym) = DW_SND (attr);
10430 SYMBOL_CLASS (sym) = LOC_CONST;
10431 break;
10432
c906108c
SS
10433 case DW_FORM_udata:
10434 SYMBOL_VALUE (sym) = DW_UNSND (attr);
10435 SYMBOL_CLASS (sym) = LOC_CONST;
10436 break;
2df3850c 10437
c906108c 10438 default:
4d3c2250 10439 complaint (&symfile_complaints,
e2e0b3e5 10440 _("unsupported const value attribute form: '%s'"),
4d3c2250 10441 dwarf_form_name (attr->form));
c906108c
SS
10442 SYMBOL_VALUE (sym) = 0;
10443 SYMBOL_CLASS (sym) = LOC_CONST;
10444 break;
10445 }
10446}
10447
2df3850c
JM
10448
10449/* Given an attr with a DW_FORM_dataN value in host byte order, sign-
10450 or zero-extend it as appropriate for the symbol's type. */
10451static void
10452dwarf2_const_value_data (struct attribute *attr,
10453 struct symbol *sym,
10454 int bits)
10455{
10456 LONGEST l = DW_UNSND (attr);
10457
10458 if (bits < sizeof (l) * 8)
10459 {
10460 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
10461 l &= ((LONGEST) 1 << bits) - 1;
10462 else
bf9198f1 10463 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
2df3850c
JM
10464 }
10465
10466 SYMBOL_VALUE (sym) = l;
10467 SYMBOL_CLASS (sym) = LOC_CONST;
10468}
10469
10470
c906108c
SS
10471/* Return the type of the die in question using its DW_AT_type attribute. */
10472
10473static struct type *
e7c27a73 10474die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10475{
c906108c
SS
10476 struct attribute *type_attr;
10477 struct die_info *type_die;
c906108c 10478
e142c38c 10479 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
10480 if (!type_attr)
10481 {
10482 /* A missing DW_AT_type represents a void type. */
46bf5051 10483 return objfile_type (cu->objfile)->builtin_void;
c906108c 10484 }
348e048f
DE
10485
10486 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10b3939b 10487
33ac96f0 10488 return tag_type_to_type (type_die, cu);
c906108c
SS
10489}
10490
b4ba55a1
JB
10491/* True iff CU's producer generates GNAT Ada auxiliary information
10492 that allows to find parallel types through that information instead
10493 of having to do expensive parallel lookups by type name. */
10494
10495static int
10496need_gnat_info (struct dwarf2_cu *cu)
10497{
10498 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
10499 of GNAT produces this auxiliary information, without any indication
10500 that it is produced. Part of enhancing the FSF version of GNAT
10501 to produce that information will be to put in place an indicator
10502 that we can use in order to determine whether the descriptive type
10503 info is available or not. One suggestion that has been made is
10504 to use a new attribute, attached to the CU die. For now, assume
10505 that the descriptive type info is not available. */
10506 return 0;
10507}
10508
10509
10510/* Return the auxiliary type of the die in question using its
10511 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
10512 attribute is not present. */
10513
10514static struct type *
10515die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
10516{
b4ba55a1
JB
10517 struct attribute *type_attr;
10518 struct die_info *type_die;
10519
10520 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
10521 if (!type_attr)
10522 return NULL;
10523
10524 type_die = follow_die_ref (die, type_attr, &cu);
33ac96f0 10525 return tag_type_to_type (type_die, cu);
b4ba55a1
JB
10526}
10527
10528/* If DIE has a descriptive_type attribute, then set the TYPE's
10529 descriptive type accordingly. */
10530
10531static void
10532set_descriptive_type (struct type *type, struct die_info *die,
10533 struct dwarf2_cu *cu)
10534{
10535 struct type *descriptive_type = die_descriptive_type (die, cu);
10536
10537 if (descriptive_type)
10538 {
10539 ALLOCATE_GNAT_AUX_TYPE (type);
10540 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
10541 }
10542}
10543
c906108c
SS
10544/* Return the containing type of the die in question using its
10545 DW_AT_containing_type attribute. */
10546
10547static struct type *
e7c27a73 10548die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10549{
c906108c 10550 struct attribute *type_attr;
33ac96f0 10551 struct die_info *type_die;
c906108c 10552
e142c38c 10553 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
10554 if (!type_attr)
10555 error (_("Dwarf Error: Problem turning containing type into gdb type "
10556 "[in module %s]"), cu->objfile->name);
10557
10558 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10559 return tag_type_to_type (type_die, cu);
c906108c
SS
10560}
10561
c906108c 10562static struct type *
e7c27a73 10563tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10564{
f792889a
DJ
10565 struct type *this_type;
10566
10567 this_type = read_type_die (die, cu);
10568 if (!this_type)
c906108c 10569 {
b00fdb78
TT
10570 char *message, *saved;
10571
10572 /* read_type_die already issued a complaint. */
10573 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
10574 cu->objfile->name,
10575 cu->header.offset,
10576 die->offset);
10577 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
10578 message, strlen (message));
10579 xfree (message);
10580
10581 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 10582 }
f792889a 10583 return this_type;
c906108c
SS
10584}
10585
f792889a 10586static struct type *
e7c27a73 10587read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10588{
f792889a
DJ
10589 struct type *this_type;
10590
10591 this_type = get_die_type (die, cu);
10592 if (this_type)
10593 return this_type;
10594
c906108c
SS
10595 switch (die->tag)
10596 {
10597 case DW_TAG_class_type:
680b30c7 10598 case DW_TAG_interface_type:
c906108c
SS
10599 case DW_TAG_structure_type:
10600 case DW_TAG_union_type:
f792889a 10601 this_type = read_structure_type (die, cu);
c906108c
SS
10602 break;
10603 case DW_TAG_enumeration_type:
f792889a 10604 this_type = read_enumeration_type (die, cu);
c906108c
SS
10605 break;
10606 case DW_TAG_subprogram:
10607 case DW_TAG_subroutine_type:
edb3359d 10608 case DW_TAG_inlined_subroutine:
f792889a 10609 this_type = read_subroutine_type (die, cu);
c906108c
SS
10610 break;
10611 case DW_TAG_array_type:
f792889a 10612 this_type = read_array_type (die, cu);
c906108c 10613 break;
72019c9c 10614 case DW_TAG_set_type:
f792889a 10615 this_type = read_set_type (die, cu);
72019c9c 10616 break;
c906108c 10617 case DW_TAG_pointer_type:
f792889a 10618 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
10619 break;
10620 case DW_TAG_ptr_to_member_type:
f792889a 10621 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
10622 break;
10623 case DW_TAG_reference_type:
f792889a 10624 this_type = read_tag_reference_type (die, cu);
c906108c
SS
10625 break;
10626 case DW_TAG_const_type:
f792889a 10627 this_type = read_tag_const_type (die, cu);
c906108c
SS
10628 break;
10629 case DW_TAG_volatile_type:
f792889a 10630 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
10631 break;
10632 case DW_TAG_string_type:
f792889a 10633 this_type = read_tag_string_type (die, cu);
c906108c
SS
10634 break;
10635 case DW_TAG_typedef:
f792889a 10636 this_type = read_typedef (die, cu);
c906108c 10637 break;
a02abb62 10638 case DW_TAG_subrange_type:
f792889a 10639 this_type = read_subrange_type (die, cu);
a02abb62 10640 break;
c906108c 10641 case DW_TAG_base_type:
f792889a 10642 this_type = read_base_type (die, cu);
c906108c 10643 break;
81a17f79 10644 case DW_TAG_unspecified_type:
f792889a 10645 this_type = read_unspecified_type (die, cu);
81a17f79 10646 break;
0114d602
DJ
10647 case DW_TAG_namespace:
10648 this_type = read_namespace_type (die, cu);
10649 break;
f55ee35c
JK
10650 case DW_TAG_module:
10651 this_type = read_module_type (die, cu);
10652 break;
c906108c 10653 default:
a1f5b845 10654 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 10655 dwarf_tag_name (die->tag));
c906108c
SS
10656 break;
10657 }
63d06c5c 10658
f792889a 10659 return this_type;
63d06c5c
DC
10660}
10661
fdde2d81 10662/* Return the name of the namespace/class that DIE is defined within,
0114d602 10663 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 10664
0114d602
DJ
10665 For example, if we're within the method foo() in the following
10666 code:
10667
10668 namespace N {
10669 class C {
10670 void foo () {
10671 }
10672 };
10673 }
10674
10675 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
10676
10677static char *
e142c38c 10678determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 10679{
0114d602
DJ
10680 struct die_info *parent, *spec_die;
10681 struct dwarf2_cu *spec_cu;
10682 struct type *parent_type;
63d06c5c 10683
f55ee35c
JK
10684 if (cu->language != language_cplus && cu->language != language_java
10685 && cu->language != language_fortran)
0114d602
DJ
10686 return "";
10687
10688 /* We have to be careful in the presence of DW_AT_specification.
10689 For example, with GCC 3.4, given the code
10690
10691 namespace N {
10692 void foo() {
10693 // Definition of N::foo.
10694 }
10695 }
10696
10697 then we'll have a tree of DIEs like this:
10698
10699 1: DW_TAG_compile_unit
10700 2: DW_TAG_namespace // N
10701 3: DW_TAG_subprogram // declaration of N::foo
10702 4: DW_TAG_subprogram // definition of N::foo
10703 DW_AT_specification // refers to die #3
10704
10705 Thus, when processing die #4, we have to pretend that we're in
10706 the context of its DW_AT_specification, namely the contex of die
10707 #3. */
10708 spec_cu = cu;
10709 spec_die = die_specification (die, &spec_cu);
10710 if (spec_die == NULL)
10711 parent = die->parent;
10712 else
63d06c5c 10713 {
0114d602
DJ
10714 parent = spec_die->parent;
10715 cu = spec_cu;
63d06c5c 10716 }
0114d602
DJ
10717
10718 if (parent == NULL)
10719 return "";
63d06c5c 10720 else
0114d602
DJ
10721 switch (parent->tag)
10722 {
63d06c5c 10723 case DW_TAG_namespace:
0114d602 10724 parent_type = read_type_die (parent, cu);
acebe513
UW
10725 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
10726 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
10727 Work around this problem here. */
10728 if (cu->language == language_cplus
10729 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
10730 return "";
0114d602
DJ
10731 /* We give a name to even anonymous namespaces. */
10732 return TYPE_TAG_NAME (parent_type);
63d06c5c 10733 case DW_TAG_class_type:
680b30c7 10734 case DW_TAG_interface_type:
63d06c5c 10735 case DW_TAG_structure_type:
0114d602 10736 case DW_TAG_union_type:
f55ee35c 10737 case DW_TAG_module:
0114d602
DJ
10738 parent_type = read_type_die (parent, cu);
10739 if (TYPE_TAG_NAME (parent_type) != NULL)
10740 return TYPE_TAG_NAME (parent_type);
10741 else
10742 /* An anonymous structure is only allowed non-static data
10743 members; no typedefs, no member functions, et cetera.
10744 So it does not need a prefix. */
10745 return "";
63d06c5c 10746 default:
8176b9b8 10747 return determine_prefix (parent, cu);
63d06c5c 10748 }
63d06c5c
DC
10749}
10750
987504bb
JJ
10751/* Return a newly-allocated string formed by concatenating PREFIX and
10752 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
10753 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
10754 perform an obconcat, otherwise allocate storage for the result. The CU argument
10755 is used to determine the language and hence, the appropriate separator. */
10756
f55ee35c 10757#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
10758
10759static char *
f55ee35c
JK
10760typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
10761 int physname, struct dwarf2_cu *cu)
63d06c5c 10762{
f55ee35c 10763 const char *lead = "";
5c315b68 10764 const char *sep;
63d06c5c 10765
987504bb
JJ
10766 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
10767 sep = "";
10768 else if (cu->language == language_java)
10769 sep = ".";
f55ee35c
JK
10770 else if (cu->language == language_fortran && physname)
10771 {
10772 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
10773 DW_AT_MIPS_linkage_name is preferred and used instead. */
10774
10775 lead = "__";
10776 sep = "_MOD_";
10777 }
987504bb
JJ
10778 else
10779 sep = "::";
63d06c5c 10780
6dd47d34
DE
10781 if (prefix == NULL)
10782 prefix = "";
10783 if (suffix == NULL)
10784 suffix = "";
10785
987504bb
JJ
10786 if (obs == NULL)
10787 {
10788 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 10789
f55ee35c
JK
10790 strcpy (retval, lead);
10791 strcat (retval, prefix);
6dd47d34
DE
10792 strcat (retval, sep);
10793 strcat (retval, suffix);
63d06c5c
DC
10794 return retval;
10795 }
987504bb
JJ
10796 else
10797 {
10798 /* We have an obstack. */
f55ee35c 10799 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 10800 }
63d06c5c
DC
10801}
10802
c906108c
SS
10803/* Return sibling of die, NULL if no sibling. */
10804
f9aca02d 10805static struct die_info *
fba45db2 10806sibling_die (struct die_info *die)
c906108c 10807{
639d11d3 10808 return die->sibling;
c906108c
SS
10809}
10810
71c25dea
TT
10811/* Get name of a die, return NULL if not found. */
10812
10813static char *
10814dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
10815 struct obstack *obstack)
10816{
10817 if (name && cu->language == language_cplus)
10818 {
10819 char *canon_name = cp_canonicalize_string (name);
10820
10821 if (canon_name != NULL)
10822 {
10823 if (strcmp (canon_name, name) != 0)
10824 name = obsavestring (canon_name, strlen (canon_name),
10825 obstack);
10826 xfree (canon_name);
10827 }
10828 }
10829
10830 return name;
c906108c
SS
10831}
10832
9219021c
DC
10833/* Get name of a die, return NULL if not found. */
10834
10835static char *
e142c38c 10836dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
10837{
10838 struct attribute *attr;
10839
e142c38c 10840 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
10841 if (!attr || !DW_STRING (attr))
10842 return NULL;
10843
10844 switch (die->tag)
10845 {
10846 case DW_TAG_compile_unit:
10847 /* Compilation units have a DW_AT_name that is a filename, not
10848 a source language identifier. */
10849 case DW_TAG_enumeration_type:
10850 case DW_TAG_enumerator:
10851 /* These tags always have simple identifiers already; no need
10852 to canonicalize them. */
10853 return DW_STRING (attr);
907af001 10854
418835cc
KS
10855 case DW_TAG_subprogram:
10856 /* Java constructors will all be named "<init>", so return
10857 the class name when we see this special case. */
10858 if (cu->language == language_java
10859 && DW_STRING (attr) != NULL
10860 && strcmp (DW_STRING (attr), "<init>") == 0)
10861 {
10862 struct dwarf2_cu *spec_cu = cu;
10863 struct die_info *spec_die;
10864
10865 /* GCJ will output '<init>' for Java constructor names.
10866 For this special case, return the name of the parent class. */
10867
10868 /* GCJ may output suprogram DIEs with AT_specification set.
10869 If so, use the name of the specified DIE. */
10870 spec_die = die_specification (die, &spec_cu);
10871 if (spec_die != NULL)
10872 return dwarf2_name (spec_die, spec_cu);
10873
10874 do
10875 {
10876 die = die->parent;
10877 if (die->tag == DW_TAG_class_type)
10878 return dwarf2_name (die, cu);
10879 }
10880 while (die->tag != DW_TAG_compile_unit);
10881 }
907af001
UW
10882 break;
10883
10884 case DW_TAG_class_type:
10885 case DW_TAG_interface_type:
10886 case DW_TAG_structure_type:
10887 case DW_TAG_union_type:
10888 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
10889 structures or unions. These were of the form "._%d" in GCC 4.1,
10890 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
10891 and GCC 4.4. We work around this problem by ignoring these. */
10892 if (strncmp (DW_STRING (attr), "._", 2) == 0
10893 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
10894 return NULL;
10895 break;
10896
71c25dea 10897 default:
907af001
UW
10898 break;
10899 }
10900
10901 if (!DW_STRING_IS_CANONICAL (attr))
10902 {
10903 DW_STRING (attr)
10904 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
10905 &cu->objfile->objfile_obstack);
10906 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 10907 }
907af001 10908 return DW_STRING (attr);
9219021c
DC
10909}
10910
10911/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
10912 is none. *EXT_CU is the CU containing DIE on input, and the CU
10913 containing the return value on output. */
9219021c
DC
10914
10915static struct die_info *
f2f0e013 10916dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
10917{
10918 struct attribute *attr;
9219021c 10919
f2f0e013 10920 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
10921 if (attr == NULL)
10922 return NULL;
10923
f2f0e013 10924 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
10925}
10926
c906108c
SS
10927/* Convert a DIE tag into its string name. */
10928
10929static char *
aa1ee363 10930dwarf_tag_name (unsigned tag)
c906108c
SS
10931{
10932 switch (tag)
10933 {
10934 case DW_TAG_padding:
10935 return "DW_TAG_padding";
10936 case DW_TAG_array_type:
10937 return "DW_TAG_array_type";
10938 case DW_TAG_class_type:
10939 return "DW_TAG_class_type";
10940 case DW_TAG_entry_point:
10941 return "DW_TAG_entry_point";
10942 case DW_TAG_enumeration_type:
10943 return "DW_TAG_enumeration_type";
10944 case DW_TAG_formal_parameter:
10945 return "DW_TAG_formal_parameter";
10946 case DW_TAG_imported_declaration:
10947 return "DW_TAG_imported_declaration";
10948 case DW_TAG_label:
10949 return "DW_TAG_label";
10950 case DW_TAG_lexical_block:
10951 return "DW_TAG_lexical_block";
10952 case DW_TAG_member:
10953 return "DW_TAG_member";
10954 case DW_TAG_pointer_type:
10955 return "DW_TAG_pointer_type";
10956 case DW_TAG_reference_type:
10957 return "DW_TAG_reference_type";
10958 case DW_TAG_compile_unit:
10959 return "DW_TAG_compile_unit";
10960 case DW_TAG_string_type:
10961 return "DW_TAG_string_type";
10962 case DW_TAG_structure_type:
10963 return "DW_TAG_structure_type";
10964 case DW_TAG_subroutine_type:
10965 return "DW_TAG_subroutine_type";
10966 case DW_TAG_typedef:
10967 return "DW_TAG_typedef";
10968 case DW_TAG_union_type:
10969 return "DW_TAG_union_type";
10970 case DW_TAG_unspecified_parameters:
10971 return "DW_TAG_unspecified_parameters";
10972 case DW_TAG_variant:
10973 return "DW_TAG_variant";
10974 case DW_TAG_common_block:
10975 return "DW_TAG_common_block";
10976 case DW_TAG_common_inclusion:
10977 return "DW_TAG_common_inclusion";
10978 case DW_TAG_inheritance:
10979 return "DW_TAG_inheritance";
10980 case DW_TAG_inlined_subroutine:
10981 return "DW_TAG_inlined_subroutine";
10982 case DW_TAG_module:
10983 return "DW_TAG_module";
10984 case DW_TAG_ptr_to_member_type:
10985 return "DW_TAG_ptr_to_member_type";
10986 case DW_TAG_set_type:
10987 return "DW_TAG_set_type";
10988 case DW_TAG_subrange_type:
10989 return "DW_TAG_subrange_type";
10990 case DW_TAG_with_stmt:
10991 return "DW_TAG_with_stmt";
10992 case DW_TAG_access_declaration:
10993 return "DW_TAG_access_declaration";
10994 case DW_TAG_base_type:
10995 return "DW_TAG_base_type";
10996 case DW_TAG_catch_block:
10997 return "DW_TAG_catch_block";
10998 case DW_TAG_const_type:
10999 return "DW_TAG_const_type";
11000 case DW_TAG_constant:
11001 return "DW_TAG_constant";
11002 case DW_TAG_enumerator:
11003 return "DW_TAG_enumerator";
11004 case DW_TAG_file_type:
11005 return "DW_TAG_file_type";
11006 case DW_TAG_friend:
11007 return "DW_TAG_friend";
11008 case DW_TAG_namelist:
11009 return "DW_TAG_namelist";
11010 case DW_TAG_namelist_item:
11011 return "DW_TAG_namelist_item";
11012 case DW_TAG_packed_type:
11013 return "DW_TAG_packed_type";
11014 case DW_TAG_subprogram:
11015 return "DW_TAG_subprogram";
11016 case DW_TAG_template_type_param:
11017 return "DW_TAG_template_type_param";
11018 case DW_TAG_template_value_param:
11019 return "DW_TAG_template_value_param";
11020 case DW_TAG_thrown_type:
11021 return "DW_TAG_thrown_type";
11022 case DW_TAG_try_block:
11023 return "DW_TAG_try_block";
11024 case DW_TAG_variant_part:
11025 return "DW_TAG_variant_part";
11026 case DW_TAG_variable:
11027 return "DW_TAG_variable";
11028 case DW_TAG_volatile_type:
11029 return "DW_TAG_volatile_type";
d9fa45fe
DC
11030 case DW_TAG_dwarf_procedure:
11031 return "DW_TAG_dwarf_procedure";
11032 case DW_TAG_restrict_type:
11033 return "DW_TAG_restrict_type";
11034 case DW_TAG_interface_type:
11035 return "DW_TAG_interface_type";
11036 case DW_TAG_namespace:
11037 return "DW_TAG_namespace";
11038 case DW_TAG_imported_module:
11039 return "DW_TAG_imported_module";
11040 case DW_TAG_unspecified_type:
11041 return "DW_TAG_unspecified_type";
11042 case DW_TAG_partial_unit:
11043 return "DW_TAG_partial_unit";
11044 case DW_TAG_imported_unit:
11045 return "DW_TAG_imported_unit";
b7619582
GF
11046 case DW_TAG_condition:
11047 return "DW_TAG_condition";
11048 case DW_TAG_shared_type:
11049 return "DW_TAG_shared_type";
348e048f
DE
11050 case DW_TAG_type_unit:
11051 return "DW_TAG_type_unit";
c906108c
SS
11052 case DW_TAG_MIPS_loop:
11053 return "DW_TAG_MIPS_loop";
b7619582
GF
11054 case DW_TAG_HP_array_descriptor:
11055 return "DW_TAG_HP_array_descriptor";
c906108c
SS
11056 case DW_TAG_format_label:
11057 return "DW_TAG_format_label";
11058 case DW_TAG_function_template:
11059 return "DW_TAG_function_template";
11060 case DW_TAG_class_template:
11061 return "DW_TAG_class_template";
b7619582
GF
11062 case DW_TAG_GNU_BINCL:
11063 return "DW_TAG_GNU_BINCL";
11064 case DW_TAG_GNU_EINCL:
11065 return "DW_TAG_GNU_EINCL";
11066 case DW_TAG_upc_shared_type:
11067 return "DW_TAG_upc_shared_type";
11068 case DW_TAG_upc_strict_type:
11069 return "DW_TAG_upc_strict_type";
11070 case DW_TAG_upc_relaxed_type:
11071 return "DW_TAG_upc_relaxed_type";
11072 case DW_TAG_PGI_kanji_type:
11073 return "DW_TAG_PGI_kanji_type";
11074 case DW_TAG_PGI_interface_block:
11075 return "DW_TAG_PGI_interface_block";
c906108c
SS
11076 default:
11077 return "DW_TAG_<unknown>";
11078 }
11079}
11080
11081/* Convert a DWARF attribute code into its string name. */
11082
11083static char *
aa1ee363 11084dwarf_attr_name (unsigned attr)
c906108c
SS
11085{
11086 switch (attr)
11087 {
11088 case DW_AT_sibling:
11089 return "DW_AT_sibling";
11090 case DW_AT_location:
11091 return "DW_AT_location";
11092 case DW_AT_name:
11093 return "DW_AT_name";
11094 case DW_AT_ordering:
11095 return "DW_AT_ordering";
11096 case DW_AT_subscr_data:
11097 return "DW_AT_subscr_data";
11098 case DW_AT_byte_size:
11099 return "DW_AT_byte_size";
11100 case DW_AT_bit_offset:
11101 return "DW_AT_bit_offset";
11102 case DW_AT_bit_size:
11103 return "DW_AT_bit_size";
11104 case DW_AT_element_list:
11105 return "DW_AT_element_list";
11106 case DW_AT_stmt_list:
11107 return "DW_AT_stmt_list";
11108 case DW_AT_low_pc:
11109 return "DW_AT_low_pc";
11110 case DW_AT_high_pc:
11111 return "DW_AT_high_pc";
11112 case DW_AT_language:
11113 return "DW_AT_language";
11114 case DW_AT_member:
11115 return "DW_AT_member";
11116 case DW_AT_discr:
11117 return "DW_AT_discr";
11118 case DW_AT_discr_value:
11119 return "DW_AT_discr_value";
11120 case DW_AT_visibility:
11121 return "DW_AT_visibility";
11122 case DW_AT_import:
11123 return "DW_AT_import";
11124 case DW_AT_string_length:
11125 return "DW_AT_string_length";
11126 case DW_AT_common_reference:
11127 return "DW_AT_common_reference";
11128 case DW_AT_comp_dir:
11129 return "DW_AT_comp_dir";
11130 case DW_AT_const_value:
11131 return "DW_AT_const_value";
11132 case DW_AT_containing_type:
11133 return "DW_AT_containing_type";
11134 case DW_AT_default_value:
11135 return "DW_AT_default_value";
11136 case DW_AT_inline:
11137 return "DW_AT_inline";
11138 case DW_AT_is_optional:
11139 return "DW_AT_is_optional";
11140 case DW_AT_lower_bound:
11141 return "DW_AT_lower_bound";
11142 case DW_AT_producer:
11143 return "DW_AT_producer";
11144 case DW_AT_prototyped:
11145 return "DW_AT_prototyped";
11146 case DW_AT_return_addr:
11147 return "DW_AT_return_addr";
11148 case DW_AT_start_scope:
11149 return "DW_AT_start_scope";
09fa0d7c
JK
11150 case DW_AT_bit_stride:
11151 return "DW_AT_bit_stride";
c906108c
SS
11152 case DW_AT_upper_bound:
11153 return "DW_AT_upper_bound";
11154 case DW_AT_abstract_origin:
11155 return "DW_AT_abstract_origin";
11156 case DW_AT_accessibility:
11157 return "DW_AT_accessibility";
11158 case DW_AT_address_class:
11159 return "DW_AT_address_class";
11160 case DW_AT_artificial:
11161 return "DW_AT_artificial";
11162 case DW_AT_base_types:
11163 return "DW_AT_base_types";
11164 case DW_AT_calling_convention:
11165 return "DW_AT_calling_convention";
11166 case DW_AT_count:
11167 return "DW_AT_count";
11168 case DW_AT_data_member_location:
11169 return "DW_AT_data_member_location";
11170 case DW_AT_decl_column:
11171 return "DW_AT_decl_column";
11172 case DW_AT_decl_file:
11173 return "DW_AT_decl_file";
11174 case DW_AT_decl_line:
11175 return "DW_AT_decl_line";
11176 case DW_AT_declaration:
11177 return "DW_AT_declaration";
11178 case DW_AT_discr_list:
11179 return "DW_AT_discr_list";
11180 case DW_AT_encoding:
11181 return "DW_AT_encoding";
11182 case DW_AT_external:
11183 return "DW_AT_external";
11184 case DW_AT_frame_base:
11185 return "DW_AT_frame_base";
11186 case DW_AT_friend:
11187 return "DW_AT_friend";
11188 case DW_AT_identifier_case:
11189 return "DW_AT_identifier_case";
11190 case DW_AT_macro_info:
11191 return "DW_AT_macro_info";
11192 case DW_AT_namelist_items:
11193 return "DW_AT_namelist_items";
11194 case DW_AT_priority:
11195 return "DW_AT_priority";
11196 case DW_AT_segment:
11197 return "DW_AT_segment";
11198 case DW_AT_specification:
11199 return "DW_AT_specification";
11200 case DW_AT_static_link:
11201 return "DW_AT_static_link";
11202 case DW_AT_type:
11203 return "DW_AT_type";
11204 case DW_AT_use_location:
11205 return "DW_AT_use_location";
11206 case DW_AT_variable_parameter:
11207 return "DW_AT_variable_parameter";
11208 case DW_AT_virtuality:
11209 return "DW_AT_virtuality";
11210 case DW_AT_vtable_elem_location:
11211 return "DW_AT_vtable_elem_location";
b7619582 11212 /* DWARF 3 values. */
d9fa45fe
DC
11213 case DW_AT_allocated:
11214 return "DW_AT_allocated";
11215 case DW_AT_associated:
11216 return "DW_AT_associated";
11217 case DW_AT_data_location:
11218 return "DW_AT_data_location";
09fa0d7c
JK
11219 case DW_AT_byte_stride:
11220 return "DW_AT_byte_stride";
d9fa45fe
DC
11221 case DW_AT_entry_pc:
11222 return "DW_AT_entry_pc";
11223 case DW_AT_use_UTF8:
11224 return "DW_AT_use_UTF8";
11225 case DW_AT_extension:
11226 return "DW_AT_extension";
11227 case DW_AT_ranges:
11228 return "DW_AT_ranges";
11229 case DW_AT_trampoline:
11230 return "DW_AT_trampoline";
11231 case DW_AT_call_column:
11232 return "DW_AT_call_column";
11233 case DW_AT_call_file:
11234 return "DW_AT_call_file";
11235 case DW_AT_call_line:
11236 return "DW_AT_call_line";
b7619582
GF
11237 case DW_AT_description:
11238 return "DW_AT_description";
11239 case DW_AT_binary_scale:
11240 return "DW_AT_binary_scale";
11241 case DW_AT_decimal_scale:
11242 return "DW_AT_decimal_scale";
11243 case DW_AT_small:
11244 return "DW_AT_small";
11245 case DW_AT_decimal_sign:
11246 return "DW_AT_decimal_sign";
11247 case DW_AT_digit_count:
11248 return "DW_AT_digit_count";
11249 case DW_AT_picture_string:
11250 return "DW_AT_picture_string";
11251 case DW_AT_mutable:
11252 return "DW_AT_mutable";
11253 case DW_AT_threads_scaled:
11254 return "DW_AT_threads_scaled";
11255 case DW_AT_explicit:
11256 return "DW_AT_explicit";
11257 case DW_AT_object_pointer:
11258 return "DW_AT_object_pointer";
11259 case DW_AT_endianity:
11260 return "DW_AT_endianity";
11261 case DW_AT_elemental:
11262 return "DW_AT_elemental";
11263 case DW_AT_pure:
11264 return "DW_AT_pure";
11265 case DW_AT_recursive:
11266 return "DW_AT_recursive";
348e048f
DE
11267 /* DWARF 4 values. */
11268 case DW_AT_signature:
11269 return "DW_AT_signature";
31ef98ae
TT
11270 case DW_AT_linkage_name:
11271 return "DW_AT_linkage_name";
b7619582 11272 /* SGI/MIPS extensions. */
c764a876 11273#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
11274 case DW_AT_MIPS_fde:
11275 return "DW_AT_MIPS_fde";
c764a876 11276#endif
c906108c
SS
11277 case DW_AT_MIPS_loop_begin:
11278 return "DW_AT_MIPS_loop_begin";
11279 case DW_AT_MIPS_tail_loop_begin:
11280 return "DW_AT_MIPS_tail_loop_begin";
11281 case DW_AT_MIPS_epilog_begin:
11282 return "DW_AT_MIPS_epilog_begin";
11283 case DW_AT_MIPS_loop_unroll_factor:
11284 return "DW_AT_MIPS_loop_unroll_factor";
11285 case DW_AT_MIPS_software_pipeline_depth:
11286 return "DW_AT_MIPS_software_pipeline_depth";
11287 case DW_AT_MIPS_linkage_name:
11288 return "DW_AT_MIPS_linkage_name";
b7619582
GF
11289 case DW_AT_MIPS_stride:
11290 return "DW_AT_MIPS_stride";
11291 case DW_AT_MIPS_abstract_name:
11292 return "DW_AT_MIPS_abstract_name";
11293 case DW_AT_MIPS_clone_origin:
11294 return "DW_AT_MIPS_clone_origin";
11295 case DW_AT_MIPS_has_inlines:
11296 return "DW_AT_MIPS_has_inlines";
b7619582 11297 /* HP extensions. */
c764a876 11298#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
11299 case DW_AT_HP_block_index:
11300 return "DW_AT_HP_block_index";
c764a876 11301#endif
b7619582
GF
11302 case DW_AT_HP_unmodifiable:
11303 return "DW_AT_HP_unmodifiable";
11304 case DW_AT_HP_actuals_stmt_list:
11305 return "DW_AT_HP_actuals_stmt_list";
11306 case DW_AT_HP_proc_per_section:
11307 return "DW_AT_HP_proc_per_section";
11308 case DW_AT_HP_raw_data_ptr:
11309 return "DW_AT_HP_raw_data_ptr";
11310 case DW_AT_HP_pass_by_reference:
11311 return "DW_AT_HP_pass_by_reference";
11312 case DW_AT_HP_opt_level:
11313 return "DW_AT_HP_opt_level";
11314 case DW_AT_HP_prof_version_id:
11315 return "DW_AT_HP_prof_version_id";
11316 case DW_AT_HP_opt_flags:
11317 return "DW_AT_HP_opt_flags";
11318 case DW_AT_HP_cold_region_low_pc:
11319 return "DW_AT_HP_cold_region_low_pc";
11320 case DW_AT_HP_cold_region_high_pc:
11321 return "DW_AT_HP_cold_region_high_pc";
11322 case DW_AT_HP_all_variables_modifiable:
11323 return "DW_AT_HP_all_variables_modifiable";
11324 case DW_AT_HP_linkage_name:
11325 return "DW_AT_HP_linkage_name";
11326 case DW_AT_HP_prof_flags:
11327 return "DW_AT_HP_prof_flags";
11328 /* GNU extensions. */
c906108c
SS
11329 case DW_AT_sf_names:
11330 return "DW_AT_sf_names";
11331 case DW_AT_src_info:
11332 return "DW_AT_src_info";
11333 case DW_AT_mac_info:
11334 return "DW_AT_mac_info";
11335 case DW_AT_src_coords:
11336 return "DW_AT_src_coords";
11337 case DW_AT_body_begin:
11338 return "DW_AT_body_begin";
11339 case DW_AT_body_end:
11340 return "DW_AT_body_end";
f5f8a009
EZ
11341 case DW_AT_GNU_vector:
11342 return "DW_AT_GNU_vector";
b7619582
GF
11343 /* VMS extensions. */
11344 case DW_AT_VMS_rtnbeg_pd_address:
11345 return "DW_AT_VMS_rtnbeg_pd_address";
11346 /* UPC extension. */
11347 case DW_AT_upc_threads_scaled:
11348 return "DW_AT_upc_threads_scaled";
11349 /* PGI (STMicroelectronics) extensions. */
11350 case DW_AT_PGI_lbase:
11351 return "DW_AT_PGI_lbase";
11352 case DW_AT_PGI_soffset:
11353 return "DW_AT_PGI_soffset";
11354 case DW_AT_PGI_lstride:
11355 return "DW_AT_PGI_lstride";
c906108c
SS
11356 default:
11357 return "DW_AT_<unknown>";
11358 }
11359}
11360
11361/* Convert a DWARF value form code into its string name. */
11362
11363static char *
aa1ee363 11364dwarf_form_name (unsigned form)
c906108c
SS
11365{
11366 switch (form)
11367 {
11368 case DW_FORM_addr:
11369 return "DW_FORM_addr";
11370 case DW_FORM_block2:
11371 return "DW_FORM_block2";
11372 case DW_FORM_block4:
11373 return "DW_FORM_block4";
11374 case DW_FORM_data2:
11375 return "DW_FORM_data2";
11376 case DW_FORM_data4:
11377 return "DW_FORM_data4";
11378 case DW_FORM_data8:
11379 return "DW_FORM_data8";
11380 case DW_FORM_string:
11381 return "DW_FORM_string";
11382 case DW_FORM_block:
11383 return "DW_FORM_block";
11384 case DW_FORM_block1:
11385 return "DW_FORM_block1";
11386 case DW_FORM_data1:
11387 return "DW_FORM_data1";
11388 case DW_FORM_flag:
11389 return "DW_FORM_flag";
11390 case DW_FORM_sdata:
11391 return "DW_FORM_sdata";
11392 case DW_FORM_strp:
11393 return "DW_FORM_strp";
11394 case DW_FORM_udata:
11395 return "DW_FORM_udata";
11396 case DW_FORM_ref_addr:
11397 return "DW_FORM_ref_addr";
11398 case DW_FORM_ref1:
11399 return "DW_FORM_ref1";
11400 case DW_FORM_ref2:
11401 return "DW_FORM_ref2";
11402 case DW_FORM_ref4:
11403 return "DW_FORM_ref4";
11404 case DW_FORM_ref8:
11405 return "DW_FORM_ref8";
11406 case DW_FORM_ref_udata:
11407 return "DW_FORM_ref_udata";
11408 case DW_FORM_indirect:
11409 return "DW_FORM_indirect";
348e048f
DE
11410 case DW_FORM_sec_offset:
11411 return "DW_FORM_sec_offset";
11412 case DW_FORM_exprloc:
11413 return "DW_FORM_exprloc";
11414 case DW_FORM_flag_present:
11415 return "DW_FORM_flag_present";
11416 case DW_FORM_sig8:
11417 return "DW_FORM_sig8";
c906108c
SS
11418 default:
11419 return "DW_FORM_<unknown>";
11420 }
11421}
11422
11423/* Convert a DWARF stack opcode into its string name. */
11424
9eae7c52
TT
11425const char *
11426dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
11427{
11428 switch (op)
11429 {
11430 case DW_OP_addr:
11431 return "DW_OP_addr";
11432 case DW_OP_deref:
11433 return "DW_OP_deref";
11434 case DW_OP_const1u:
11435 return "DW_OP_const1u";
11436 case DW_OP_const1s:
11437 return "DW_OP_const1s";
11438 case DW_OP_const2u:
11439 return "DW_OP_const2u";
11440 case DW_OP_const2s:
11441 return "DW_OP_const2s";
11442 case DW_OP_const4u:
11443 return "DW_OP_const4u";
11444 case DW_OP_const4s:
11445 return "DW_OP_const4s";
11446 case DW_OP_const8u:
11447 return "DW_OP_const8u";
11448 case DW_OP_const8s:
11449 return "DW_OP_const8s";
11450 case DW_OP_constu:
11451 return "DW_OP_constu";
11452 case DW_OP_consts:
11453 return "DW_OP_consts";
11454 case DW_OP_dup:
11455 return "DW_OP_dup";
11456 case DW_OP_drop:
11457 return "DW_OP_drop";
11458 case DW_OP_over:
11459 return "DW_OP_over";
11460 case DW_OP_pick:
11461 return "DW_OP_pick";
11462 case DW_OP_swap:
11463 return "DW_OP_swap";
11464 case DW_OP_rot:
11465 return "DW_OP_rot";
11466 case DW_OP_xderef:
11467 return "DW_OP_xderef";
11468 case DW_OP_abs:
11469 return "DW_OP_abs";
11470 case DW_OP_and:
11471 return "DW_OP_and";
11472 case DW_OP_div:
11473 return "DW_OP_div";
11474 case DW_OP_minus:
11475 return "DW_OP_minus";
11476 case DW_OP_mod:
11477 return "DW_OP_mod";
11478 case DW_OP_mul:
11479 return "DW_OP_mul";
11480 case DW_OP_neg:
11481 return "DW_OP_neg";
11482 case DW_OP_not:
11483 return "DW_OP_not";
11484 case DW_OP_or:
11485 return "DW_OP_or";
11486 case DW_OP_plus:
11487 return "DW_OP_plus";
11488 case DW_OP_plus_uconst:
11489 return "DW_OP_plus_uconst";
11490 case DW_OP_shl:
11491 return "DW_OP_shl";
11492 case DW_OP_shr:
11493 return "DW_OP_shr";
11494 case DW_OP_shra:
11495 return "DW_OP_shra";
11496 case DW_OP_xor:
11497 return "DW_OP_xor";
11498 case DW_OP_bra:
11499 return "DW_OP_bra";
11500 case DW_OP_eq:
11501 return "DW_OP_eq";
11502 case DW_OP_ge:
11503 return "DW_OP_ge";
11504 case DW_OP_gt:
11505 return "DW_OP_gt";
11506 case DW_OP_le:
11507 return "DW_OP_le";
11508 case DW_OP_lt:
11509 return "DW_OP_lt";
11510 case DW_OP_ne:
11511 return "DW_OP_ne";
11512 case DW_OP_skip:
11513 return "DW_OP_skip";
11514 case DW_OP_lit0:
11515 return "DW_OP_lit0";
11516 case DW_OP_lit1:
11517 return "DW_OP_lit1";
11518 case DW_OP_lit2:
11519 return "DW_OP_lit2";
11520 case DW_OP_lit3:
11521 return "DW_OP_lit3";
11522 case DW_OP_lit4:
11523 return "DW_OP_lit4";
11524 case DW_OP_lit5:
11525 return "DW_OP_lit5";
11526 case DW_OP_lit6:
11527 return "DW_OP_lit6";
11528 case DW_OP_lit7:
11529 return "DW_OP_lit7";
11530 case DW_OP_lit8:
11531 return "DW_OP_lit8";
11532 case DW_OP_lit9:
11533 return "DW_OP_lit9";
11534 case DW_OP_lit10:
11535 return "DW_OP_lit10";
11536 case DW_OP_lit11:
11537 return "DW_OP_lit11";
11538 case DW_OP_lit12:
11539 return "DW_OP_lit12";
11540 case DW_OP_lit13:
11541 return "DW_OP_lit13";
11542 case DW_OP_lit14:
11543 return "DW_OP_lit14";
11544 case DW_OP_lit15:
11545 return "DW_OP_lit15";
11546 case DW_OP_lit16:
11547 return "DW_OP_lit16";
11548 case DW_OP_lit17:
11549 return "DW_OP_lit17";
11550 case DW_OP_lit18:
11551 return "DW_OP_lit18";
11552 case DW_OP_lit19:
11553 return "DW_OP_lit19";
11554 case DW_OP_lit20:
11555 return "DW_OP_lit20";
11556 case DW_OP_lit21:
11557 return "DW_OP_lit21";
11558 case DW_OP_lit22:
11559 return "DW_OP_lit22";
11560 case DW_OP_lit23:
11561 return "DW_OP_lit23";
11562 case DW_OP_lit24:
11563 return "DW_OP_lit24";
11564 case DW_OP_lit25:
11565 return "DW_OP_lit25";
11566 case DW_OP_lit26:
11567 return "DW_OP_lit26";
11568 case DW_OP_lit27:
11569 return "DW_OP_lit27";
11570 case DW_OP_lit28:
11571 return "DW_OP_lit28";
11572 case DW_OP_lit29:
11573 return "DW_OP_lit29";
11574 case DW_OP_lit30:
11575 return "DW_OP_lit30";
11576 case DW_OP_lit31:
11577 return "DW_OP_lit31";
11578 case DW_OP_reg0:
11579 return "DW_OP_reg0";
11580 case DW_OP_reg1:
11581 return "DW_OP_reg1";
11582 case DW_OP_reg2:
11583 return "DW_OP_reg2";
11584 case DW_OP_reg3:
11585 return "DW_OP_reg3";
11586 case DW_OP_reg4:
11587 return "DW_OP_reg4";
11588 case DW_OP_reg5:
11589 return "DW_OP_reg5";
11590 case DW_OP_reg6:
11591 return "DW_OP_reg6";
11592 case DW_OP_reg7:
11593 return "DW_OP_reg7";
11594 case DW_OP_reg8:
11595 return "DW_OP_reg8";
11596 case DW_OP_reg9:
11597 return "DW_OP_reg9";
11598 case DW_OP_reg10:
11599 return "DW_OP_reg10";
11600 case DW_OP_reg11:
11601 return "DW_OP_reg11";
11602 case DW_OP_reg12:
11603 return "DW_OP_reg12";
11604 case DW_OP_reg13:
11605 return "DW_OP_reg13";
11606 case DW_OP_reg14:
11607 return "DW_OP_reg14";
11608 case DW_OP_reg15:
11609 return "DW_OP_reg15";
11610 case DW_OP_reg16:
11611 return "DW_OP_reg16";
11612 case DW_OP_reg17:
11613 return "DW_OP_reg17";
11614 case DW_OP_reg18:
11615 return "DW_OP_reg18";
11616 case DW_OP_reg19:
11617 return "DW_OP_reg19";
11618 case DW_OP_reg20:
11619 return "DW_OP_reg20";
11620 case DW_OP_reg21:
11621 return "DW_OP_reg21";
11622 case DW_OP_reg22:
11623 return "DW_OP_reg22";
11624 case DW_OP_reg23:
11625 return "DW_OP_reg23";
11626 case DW_OP_reg24:
11627 return "DW_OP_reg24";
11628 case DW_OP_reg25:
11629 return "DW_OP_reg25";
11630 case DW_OP_reg26:
11631 return "DW_OP_reg26";
11632 case DW_OP_reg27:
11633 return "DW_OP_reg27";
11634 case DW_OP_reg28:
11635 return "DW_OP_reg28";
11636 case DW_OP_reg29:
11637 return "DW_OP_reg29";
11638 case DW_OP_reg30:
11639 return "DW_OP_reg30";
11640 case DW_OP_reg31:
11641 return "DW_OP_reg31";
11642 case DW_OP_breg0:
11643 return "DW_OP_breg0";
11644 case DW_OP_breg1:
11645 return "DW_OP_breg1";
11646 case DW_OP_breg2:
11647 return "DW_OP_breg2";
11648 case DW_OP_breg3:
11649 return "DW_OP_breg3";
11650 case DW_OP_breg4:
11651 return "DW_OP_breg4";
11652 case DW_OP_breg5:
11653 return "DW_OP_breg5";
11654 case DW_OP_breg6:
11655 return "DW_OP_breg6";
11656 case DW_OP_breg7:
11657 return "DW_OP_breg7";
11658 case DW_OP_breg8:
11659 return "DW_OP_breg8";
11660 case DW_OP_breg9:
11661 return "DW_OP_breg9";
11662 case DW_OP_breg10:
11663 return "DW_OP_breg10";
11664 case DW_OP_breg11:
11665 return "DW_OP_breg11";
11666 case DW_OP_breg12:
11667 return "DW_OP_breg12";
11668 case DW_OP_breg13:
11669 return "DW_OP_breg13";
11670 case DW_OP_breg14:
11671 return "DW_OP_breg14";
11672 case DW_OP_breg15:
11673 return "DW_OP_breg15";
11674 case DW_OP_breg16:
11675 return "DW_OP_breg16";
11676 case DW_OP_breg17:
11677 return "DW_OP_breg17";
11678 case DW_OP_breg18:
11679 return "DW_OP_breg18";
11680 case DW_OP_breg19:
11681 return "DW_OP_breg19";
11682 case DW_OP_breg20:
11683 return "DW_OP_breg20";
11684 case DW_OP_breg21:
11685 return "DW_OP_breg21";
11686 case DW_OP_breg22:
11687 return "DW_OP_breg22";
11688 case DW_OP_breg23:
11689 return "DW_OP_breg23";
11690 case DW_OP_breg24:
11691 return "DW_OP_breg24";
11692 case DW_OP_breg25:
11693 return "DW_OP_breg25";
11694 case DW_OP_breg26:
11695 return "DW_OP_breg26";
11696 case DW_OP_breg27:
11697 return "DW_OP_breg27";
11698 case DW_OP_breg28:
11699 return "DW_OP_breg28";
11700 case DW_OP_breg29:
11701 return "DW_OP_breg29";
11702 case DW_OP_breg30:
11703 return "DW_OP_breg30";
11704 case DW_OP_breg31:
11705 return "DW_OP_breg31";
11706 case DW_OP_regx:
11707 return "DW_OP_regx";
11708 case DW_OP_fbreg:
11709 return "DW_OP_fbreg";
11710 case DW_OP_bregx:
11711 return "DW_OP_bregx";
11712 case DW_OP_piece:
11713 return "DW_OP_piece";
11714 case DW_OP_deref_size:
11715 return "DW_OP_deref_size";
11716 case DW_OP_xderef_size:
11717 return "DW_OP_xderef_size";
11718 case DW_OP_nop:
11719 return "DW_OP_nop";
b7619582 11720 /* DWARF 3 extensions. */
ed348acc
EZ
11721 case DW_OP_push_object_address:
11722 return "DW_OP_push_object_address";
11723 case DW_OP_call2:
11724 return "DW_OP_call2";
11725 case DW_OP_call4:
11726 return "DW_OP_call4";
11727 case DW_OP_call_ref:
11728 return "DW_OP_call_ref";
b7619582
GF
11729 case DW_OP_form_tls_address:
11730 return "DW_OP_form_tls_address";
11731 case DW_OP_call_frame_cfa:
11732 return "DW_OP_call_frame_cfa";
11733 case DW_OP_bit_piece:
11734 return "DW_OP_bit_piece";
9eae7c52
TT
11735 /* DWARF 4 extensions. */
11736 case DW_OP_implicit_value:
11737 return "DW_OP_implicit_value";
11738 case DW_OP_stack_value:
11739 return "DW_OP_stack_value";
11740 /* GNU extensions. */
ed348acc
EZ
11741 case DW_OP_GNU_push_tls_address:
11742 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
11743 case DW_OP_GNU_uninit:
11744 return "DW_OP_GNU_uninit";
c906108c 11745 default:
9eae7c52 11746 return def ? "OP_<unknown>" : NULL;
c906108c
SS
11747 }
11748}
11749
11750static char *
fba45db2 11751dwarf_bool_name (unsigned mybool)
c906108c
SS
11752{
11753 if (mybool)
11754 return "TRUE";
11755 else
11756 return "FALSE";
11757}
11758
11759/* Convert a DWARF type code into its string name. */
11760
11761static char *
aa1ee363 11762dwarf_type_encoding_name (unsigned enc)
c906108c
SS
11763{
11764 switch (enc)
11765 {
b7619582
GF
11766 case DW_ATE_void:
11767 return "DW_ATE_void";
c906108c
SS
11768 case DW_ATE_address:
11769 return "DW_ATE_address";
11770 case DW_ATE_boolean:
11771 return "DW_ATE_boolean";
11772 case DW_ATE_complex_float:
11773 return "DW_ATE_complex_float";
11774 case DW_ATE_float:
11775 return "DW_ATE_float";
11776 case DW_ATE_signed:
11777 return "DW_ATE_signed";
11778 case DW_ATE_signed_char:
11779 return "DW_ATE_signed_char";
11780 case DW_ATE_unsigned:
11781 return "DW_ATE_unsigned";
11782 case DW_ATE_unsigned_char:
11783 return "DW_ATE_unsigned_char";
b7619582 11784 /* DWARF 3. */
d9fa45fe
DC
11785 case DW_ATE_imaginary_float:
11786 return "DW_ATE_imaginary_float";
b7619582
GF
11787 case DW_ATE_packed_decimal:
11788 return "DW_ATE_packed_decimal";
11789 case DW_ATE_numeric_string:
11790 return "DW_ATE_numeric_string";
11791 case DW_ATE_edited:
11792 return "DW_ATE_edited";
11793 case DW_ATE_signed_fixed:
11794 return "DW_ATE_signed_fixed";
11795 case DW_ATE_unsigned_fixed:
11796 return "DW_ATE_unsigned_fixed";
11797 case DW_ATE_decimal_float:
11798 return "DW_ATE_decimal_float";
75079b2b
TT
11799 /* DWARF 4. */
11800 case DW_ATE_UTF:
11801 return "DW_ATE_UTF";
b7619582
GF
11802 /* HP extensions. */
11803 case DW_ATE_HP_float80:
11804 return "DW_ATE_HP_float80";
11805 case DW_ATE_HP_complex_float80:
11806 return "DW_ATE_HP_complex_float80";
11807 case DW_ATE_HP_float128:
11808 return "DW_ATE_HP_float128";
11809 case DW_ATE_HP_complex_float128:
11810 return "DW_ATE_HP_complex_float128";
11811 case DW_ATE_HP_floathpintel:
11812 return "DW_ATE_HP_floathpintel";
11813 case DW_ATE_HP_imaginary_float80:
11814 return "DW_ATE_HP_imaginary_float80";
11815 case DW_ATE_HP_imaginary_float128:
11816 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
11817 default:
11818 return "DW_ATE_<unknown>";
11819 }
11820}
11821
11822/* Convert a DWARF call frame info operation to its string name. */
11823
11824#if 0
11825static char *
aa1ee363 11826dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
11827{
11828 switch (cfi_opc)
11829 {
11830 case DW_CFA_advance_loc:
11831 return "DW_CFA_advance_loc";
11832 case DW_CFA_offset:
11833 return "DW_CFA_offset";
11834 case DW_CFA_restore:
11835 return "DW_CFA_restore";
11836 case DW_CFA_nop:
11837 return "DW_CFA_nop";
11838 case DW_CFA_set_loc:
11839 return "DW_CFA_set_loc";
11840 case DW_CFA_advance_loc1:
11841 return "DW_CFA_advance_loc1";
11842 case DW_CFA_advance_loc2:
11843 return "DW_CFA_advance_loc2";
11844 case DW_CFA_advance_loc4:
11845 return "DW_CFA_advance_loc4";
11846 case DW_CFA_offset_extended:
11847 return "DW_CFA_offset_extended";
11848 case DW_CFA_restore_extended:
11849 return "DW_CFA_restore_extended";
11850 case DW_CFA_undefined:
11851 return "DW_CFA_undefined";
11852 case DW_CFA_same_value:
11853 return "DW_CFA_same_value";
11854 case DW_CFA_register:
11855 return "DW_CFA_register";
11856 case DW_CFA_remember_state:
11857 return "DW_CFA_remember_state";
11858 case DW_CFA_restore_state:
11859 return "DW_CFA_restore_state";
11860 case DW_CFA_def_cfa:
11861 return "DW_CFA_def_cfa";
11862 case DW_CFA_def_cfa_register:
11863 return "DW_CFA_def_cfa_register";
11864 case DW_CFA_def_cfa_offset:
11865 return "DW_CFA_def_cfa_offset";
b7619582 11866 /* DWARF 3. */
985cb1a3
JM
11867 case DW_CFA_def_cfa_expression:
11868 return "DW_CFA_def_cfa_expression";
11869 case DW_CFA_expression:
11870 return "DW_CFA_expression";
11871 case DW_CFA_offset_extended_sf:
11872 return "DW_CFA_offset_extended_sf";
11873 case DW_CFA_def_cfa_sf:
11874 return "DW_CFA_def_cfa_sf";
11875 case DW_CFA_def_cfa_offset_sf:
11876 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
11877 case DW_CFA_val_offset:
11878 return "DW_CFA_val_offset";
11879 case DW_CFA_val_offset_sf:
11880 return "DW_CFA_val_offset_sf";
11881 case DW_CFA_val_expression:
11882 return "DW_CFA_val_expression";
11883 /* SGI/MIPS specific. */
c906108c
SS
11884 case DW_CFA_MIPS_advance_loc8:
11885 return "DW_CFA_MIPS_advance_loc8";
b7619582 11886 /* GNU extensions. */
985cb1a3
JM
11887 case DW_CFA_GNU_window_save:
11888 return "DW_CFA_GNU_window_save";
11889 case DW_CFA_GNU_args_size:
11890 return "DW_CFA_GNU_args_size";
11891 case DW_CFA_GNU_negative_offset_extended:
11892 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
11893 default:
11894 return "DW_CFA_<unknown>";
11895 }
11896}
11897#endif
11898
f9aca02d 11899static void
d97bc12b 11900dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
11901{
11902 unsigned int i;
11903
d97bc12b
DE
11904 print_spaces (indent, f);
11905 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 11906 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
11907
11908 if (die->parent != NULL)
11909 {
11910 print_spaces (indent, f);
11911 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
11912 die->parent->offset);
11913 }
11914
11915 print_spaces (indent, f);
11916 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 11917 dwarf_bool_name (die->child != NULL));
c906108c 11918
d97bc12b
DE
11919 print_spaces (indent, f);
11920 fprintf_unfiltered (f, " attributes:\n");
11921
c906108c
SS
11922 for (i = 0; i < die->num_attrs; ++i)
11923 {
d97bc12b
DE
11924 print_spaces (indent, f);
11925 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
11926 dwarf_attr_name (die->attrs[i].name),
11927 dwarf_form_name (die->attrs[i].form));
d97bc12b 11928
c906108c
SS
11929 switch (die->attrs[i].form)
11930 {
11931 case DW_FORM_ref_addr:
11932 case DW_FORM_addr:
d97bc12b 11933 fprintf_unfiltered (f, "address: ");
5af949e3 11934 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
11935 break;
11936 case DW_FORM_block2:
11937 case DW_FORM_block4:
11938 case DW_FORM_block:
11939 case DW_FORM_block1:
d97bc12b 11940 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 11941 break;
2dc7f7b3
TT
11942 case DW_FORM_exprloc:
11943 fprintf_unfiltered (f, "expression: size %u",
11944 DW_BLOCK (&die->attrs[i])->size);
11945 break;
10b3939b
DJ
11946 case DW_FORM_ref1:
11947 case DW_FORM_ref2:
11948 case DW_FORM_ref4:
d97bc12b 11949 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
11950 (long) (DW_ADDR (&die->attrs[i])));
11951 break;
c906108c
SS
11952 case DW_FORM_data1:
11953 case DW_FORM_data2:
11954 case DW_FORM_data4:
ce5d95e1 11955 case DW_FORM_data8:
c906108c
SS
11956 case DW_FORM_udata:
11957 case DW_FORM_sdata:
43bbcdc2
PH
11958 fprintf_unfiltered (f, "constant: %s",
11959 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 11960 break;
2dc7f7b3
TT
11961 case DW_FORM_sec_offset:
11962 fprintf_unfiltered (f, "section offset: %s",
11963 pulongest (DW_UNSND (&die->attrs[i])));
11964 break;
348e048f
DE
11965 case DW_FORM_sig8:
11966 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
11967 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
11968 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
11969 else
11970 fprintf_unfiltered (f, "signatured type, offset: unknown");
11971 break;
c906108c 11972 case DW_FORM_string:
4bdf3d34 11973 case DW_FORM_strp:
8285870a 11974 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 11975 DW_STRING (&die->attrs[i])
8285870a
JK
11976 ? DW_STRING (&die->attrs[i]) : "",
11977 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
11978 break;
11979 case DW_FORM_flag:
11980 if (DW_UNSND (&die->attrs[i]))
d97bc12b 11981 fprintf_unfiltered (f, "flag: TRUE");
c906108c 11982 else
d97bc12b 11983 fprintf_unfiltered (f, "flag: FALSE");
c906108c 11984 break;
2dc7f7b3
TT
11985 case DW_FORM_flag_present:
11986 fprintf_unfiltered (f, "flag: TRUE");
11987 break;
a8329558
KW
11988 case DW_FORM_indirect:
11989 /* the reader will have reduced the indirect form to
11990 the "base form" so this form should not occur */
d97bc12b 11991 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 11992 break;
c906108c 11993 default:
d97bc12b 11994 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 11995 die->attrs[i].form);
d97bc12b 11996 break;
c906108c 11997 }
d97bc12b 11998 fprintf_unfiltered (f, "\n");
c906108c
SS
11999 }
12000}
12001
f9aca02d 12002static void
d97bc12b 12003dump_die_for_error (struct die_info *die)
c906108c 12004{
d97bc12b
DE
12005 dump_die_shallow (gdb_stderr, 0, die);
12006}
12007
12008static void
12009dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12010{
12011 int indent = level * 4;
12012
12013 gdb_assert (die != NULL);
12014
12015 if (level >= max_level)
12016 return;
12017
12018 dump_die_shallow (f, indent, die);
12019
12020 if (die->child != NULL)
c906108c 12021 {
d97bc12b
DE
12022 print_spaces (indent, f);
12023 fprintf_unfiltered (f, " Children:");
12024 if (level + 1 < max_level)
12025 {
12026 fprintf_unfiltered (f, "\n");
12027 dump_die_1 (f, level + 1, max_level, die->child);
12028 }
12029 else
12030 {
12031 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12032 }
12033 }
12034
12035 if (die->sibling != NULL && level > 0)
12036 {
12037 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
12038 }
12039}
12040
d97bc12b
DE
12041/* This is called from the pdie macro in gdbinit.in.
12042 It's not static so gcc will keep a copy callable from gdb. */
12043
12044void
12045dump_die (struct die_info *die, int max_level)
12046{
12047 dump_die_1 (gdb_stdlog, 0, max_level, die);
12048}
12049
f9aca02d 12050static void
51545339 12051store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12052{
51545339 12053 void **slot;
c906108c 12054
51545339
DJ
12055 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12056
12057 *slot = die;
c906108c
SS
12058}
12059
93311388
DE
12060static int
12061is_ref_attr (struct attribute *attr)
c906108c 12062{
c906108c
SS
12063 switch (attr->form)
12064 {
12065 case DW_FORM_ref_addr:
c906108c
SS
12066 case DW_FORM_ref1:
12067 case DW_FORM_ref2:
12068 case DW_FORM_ref4:
613e1657 12069 case DW_FORM_ref8:
c906108c 12070 case DW_FORM_ref_udata:
93311388 12071 return 1;
c906108c 12072 default:
93311388 12073 return 0;
c906108c 12074 }
93311388
DE
12075}
12076
12077static unsigned int
12078dwarf2_get_ref_die_offset (struct attribute *attr)
12079{
12080 if (is_ref_attr (attr))
12081 return DW_ADDR (attr);
12082
12083 complaint (&symfile_complaints,
12084 _("unsupported die ref attribute form: '%s'"),
12085 dwarf_form_name (attr->form));
12086 return 0;
c906108c
SS
12087}
12088
43bbcdc2
PH
12089/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12090 * the value held by the attribute is not constant. */
a02abb62 12091
43bbcdc2 12092static LONGEST
a02abb62
JB
12093dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12094{
12095 if (attr->form == DW_FORM_sdata)
12096 return DW_SND (attr);
12097 else if (attr->form == DW_FORM_udata
12098 || attr->form == DW_FORM_data1
12099 || attr->form == DW_FORM_data2
12100 || attr->form == DW_FORM_data4
12101 || attr->form == DW_FORM_data8)
12102 return DW_UNSND (attr);
12103 else
12104 {
e2e0b3e5 12105 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
12106 dwarf_form_name (attr->form));
12107 return default_value;
12108 }
12109}
12110
03dd20cc 12111/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
12112 unit and add it to our queue.
12113 The result is non-zero if PER_CU was queued, otherwise the result is zero
12114 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 12115
348e048f 12116static int
03dd20cc
DJ
12117maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12118 struct dwarf2_per_cu_data *per_cu)
12119{
12120 /* Mark the dependence relation so that we don't flush PER_CU
12121 too early. */
12122 dwarf2_add_dependence (this_cu, per_cu);
12123
12124 /* If it's already on the queue, we have nothing to do. */
12125 if (per_cu->queued)
348e048f 12126 return 0;
03dd20cc
DJ
12127
12128 /* If the compilation unit is already loaded, just mark it as
12129 used. */
12130 if (per_cu->cu != NULL)
12131 {
12132 per_cu->cu->last_used = 0;
348e048f 12133 return 0;
03dd20cc
DJ
12134 }
12135
12136 /* Add it to the queue. */
12137 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
12138
12139 return 1;
12140}
12141
12142/* Follow reference or signature attribute ATTR of SRC_DIE.
12143 On entry *REF_CU is the CU of SRC_DIE.
12144 On exit *REF_CU is the CU of the result. */
12145
12146static struct die_info *
12147follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12148 struct dwarf2_cu **ref_cu)
12149{
12150 struct die_info *die;
12151
12152 if (is_ref_attr (attr))
12153 die = follow_die_ref (src_die, attr, ref_cu);
12154 else if (attr->form == DW_FORM_sig8)
12155 die = follow_die_sig (src_die, attr, ref_cu);
12156 else
12157 {
12158 dump_die_for_error (src_die);
12159 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12160 (*ref_cu)->objfile->name);
12161 }
12162
12163 return die;
03dd20cc
DJ
12164}
12165
5c631832
JK
12166/* Follow reference OFFSET.
12167 On entry *REF_CU is the CU of source DIE referencing OFFSET.
f504f079
DE
12168 On exit *REF_CU is the CU of the result. */
12169
f9aca02d 12170static struct die_info *
5c631832 12171follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 12172{
10b3939b 12173 struct die_info temp_die;
f2f0e013 12174 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 12175
348e048f
DE
12176 gdb_assert (cu->per_cu != NULL);
12177
348e048f
DE
12178 if (cu->per_cu->from_debug_types)
12179 {
12180 /* .debug_types CUs cannot reference anything outside their CU.
12181 If they need to, they have to reference a signatured type via
12182 DW_FORM_sig8. */
12183 if (! offset_in_cu_p (&cu->header, offset))
5c631832 12184 return NULL;
348e048f
DE
12185 target_cu = cu;
12186 }
12187 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
12188 {
12189 struct dwarf2_per_cu_data *per_cu;
9a619af0 12190
45452591 12191 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
12192
12193 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
12194 if (maybe_queue_comp_unit (cu, per_cu))
12195 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 12196
10b3939b
DJ
12197 target_cu = per_cu->cu;
12198 }
12199 else
12200 target_cu = cu;
c906108c 12201
f2f0e013 12202 *ref_cu = target_cu;
51545339 12203 temp_die.offset = offset;
5c631832
JK
12204 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12205}
10b3939b 12206
5c631832
JK
12207/* Follow reference attribute ATTR of SRC_DIE.
12208 On entry *REF_CU is the CU of SRC_DIE.
12209 On exit *REF_CU is the CU of the result. */
12210
12211static struct die_info *
12212follow_die_ref (struct die_info *src_die, struct attribute *attr,
12213 struct dwarf2_cu **ref_cu)
12214{
12215 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12216 struct dwarf2_cu *cu = *ref_cu;
12217 struct die_info *die;
12218
12219 die = follow_die_offset (offset, ref_cu);
12220 if (!die)
12221 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
12222 "at 0x%x [in module %s]"),
12223 offset, src_die->offset, cu->objfile->name);
348e048f 12224
5c631832
JK
12225 return die;
12226}
12227
12228/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
12229 value is intended for DW_OP_call*. */
12230
12231struct dwarf2_locexpr_baton
12232dwarf2_fetch_die_location_block (unsigned int offset,
12233 struct dwarf2_per_cu_data *per_cu)
12234{
12235 struct dwarf2_cu *cu = per_cu->cu;
12236 struct die_info *die;
12237 struct attribute *attr;
12238 struct dwarf2_locexpr_baton retval;
12239
12240 die = follow_die_offset (offset, &cu);
12241 if (!die)
12242 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
12243 offset, per_cu->cu->objfile->name);
12244
12245 attr = dwarf2_attr (die, DW_AT_location, cu);
12246 if (!attr)
12247 {
12248 /* DWARF: "If there is no such attribute, then there is no effect.". */
12249
12250 retval.data = NULL;
12251 retval.size = 0;
12252 }
12253 else
12254 {
12255 if (!attr_form_is_block (attr))
12256 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
12257 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
12258 offset, per_cu->cu->objfile->name);
12259
12260 retval.data = DW_BLOCK (attr)->data;
12261 retval.size = DW_BLOCK (attr)->size;
12262 }
12263 retval.per_cu = cu->per_cu;
12264 return retval;
348e048f
DE
12265}
12266
12267/* Follow the signature attribute ATTR in SRC_DIE.
12268 On entry *REF_CU is the CU of SRC_DIE.
12269 On exit *REF_CU is the CU of the result. */
12270
12271static struct die_info *
12272follow_die_sig (struct die_info *src_die, struct attribute *attr,
12273 struct dwarf2_cu **ref_cu)
12274{
12275 struct objfile *objfile = (*ref_cu)->objfile;
12276 struct die_info temp_die;
12277 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12278 struct dwarf2_cu *sig_cu;
12279 struct die_info *die;
12280
12281 /* sig_type will be NULL if the signatured type is missing from
12282 the debug info. */
12283 if (sig_type == NULL)
12284 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12285 "at 0x%x [in module %s]"),
12286 src_die->offset, objfile->name);
12287
12288 /* If necessary, add it to the queue and load its DIEs. */
12289
12290 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12291 read_signatured_type (objfile, sig_type);
12292
12293 gdb_assert (sig_type->per_cu.cu != NULL);
12294
12295 sig_cu = sig_type->per_cu.cu;
12296 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12297 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12298 if (die)
12299 {
12300 *ref_cu = sig_cu;
12301 return die;
12302 }
12303
12304 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12305 "at 0x%x [in module %s]"),
12306 sig_type->type_offset, src_die->offset, objfile->name);
12307}
12308
12309/* Given an offset of a signatured type, return its signatured_type. */
12310
12311static struct signatured_type *
12312lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12313{
12314 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12315 unsigned int length, initial_length_size;
12316 unsigned int sig_offset;
12317 struct signatured_type find_entry, *type_sig;
12318
12319 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12320 sig_offset = (initial_length_size
12321 + 2 /*version*/
12322 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12323 + 1 /*address_size*/);
12324 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12325 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12326
12327 /* This is only used to lookup previously recorded types.
12328 If we didn't find it, it's our bug. */
12329 gdb_assert (type_sig != NULL);
12330 gdb_assert (offset == type_sig->offset);
12331
12332 return type_sig;
12333}
12334
12335/* Read in signatured type at OFFSET and build its CU and die(s). */
12336
12337static void
12338read_signatured_type_at_offset (struct objfile *objfile,
12339 unsigned int offset)
12340{
12341 struct signatured_type *type_sig;
12342
be391dca
TT
12343 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12344
348e048f
DE
12345 /* We have the section offset, but we need the signature to do the
12346 hash table lookup. */
12347 type_sig = lookup_signatured_type_at_offset (objfile, offset);
12348
12349 gdb_assert (type_sig->per_cu.cu == NULL);
12350
12351 read_signatured_type (objfile, type_sig);
12352
12353 gdb_assert (type_sig->per_cu.cu != NULL);
12354}
12355
12356/* Read in a signatured type and build its CU and DIEs. */
12357
12358static void
12359read_signatured_type (struct objfile *objfile,
12360 struct signatured_type *type_sig)
12361{
1fd400ff 12362 gdb_byte *types_ptr;
348e048f
DE
12363 struct die_reader_specs reader_specs;
12364 struct dwarf2_cu *cu;
12365 ULONGEST signature;
12366 struct cleanup *back_to, *free_cu_cleanup;
12367 struct attribute *attr;
12368
1fd400ff
TT
12369 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12370 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
12371
348e048f
DE
12372 gdb_assert (type_sig->per_cu.cu == NULL);
12373
12374 cu = xmalloc (sizeof (struct dwarf2_cu));
12375 memset (cu, 0, sizeof (struct dwarf2_cu));
12376 obstack_init (&cu->comp_unit_obstack);
12377 cu->objfile = objfile;
12378 type_sig->per_cu.cu = cu;
12379 cu->per_cu = &type_sig->per_cu;
12380
12381 /* If an error occurs while loading, release our storage. */
12382 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
12383
12384 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
12385 types_ptr, objfile->obfd);
12386 gdb_assert (signature == type_sig->signature);
12387
12388 cu->die_hash
12389 = htab_create_alloc_ex (cu->header.length / 12,
12390 die_hash,
12391 die_eq,
12392 NULL,
12393 &cu->comp_unit_obstack,
12394 hashtab_obstack_allocate,
12395 dummy_obstack_deallocate);
12396
12397 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
12398 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
12399
12400 init_cu_die_reader (&reader_specs, cu);
12401
12402 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
12403 NULL /*parent*/);
12404
12405 /* We try not to read any attributes in this function, because not
12406 all objfiles needed for references have been loaded yet, and symbol
12407 table processing isn't initialized. But we have to set the CU language,
12408 or we won't be able to build types correctly. */
12409 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
12410 if (attr)
12411 set_cu_language (DW_UNSND (attr), cu);
12412 else
12413 set_cu_language (language_minimal, cu);
12414
12415 do_cleanups (back_to);
12416
12417 /* We've successfully allocated this compilation unit. Let our caller
12418 clean it up when finished with it. */
12419 discard_cleanups (free_cu_cleanup);
12420
12421 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
12422 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
12423}
12424
c906108c
SS
12425/* Decode simple location descriptions.
12426 Given a pointer to a dwarf block that defines a location, compute
12427 the location and return the value.
12428
4cecd739
DJ
12429 NOTE drow/2003-11-18: This function is called in two situations
12430 now: for the address of static or global variables (partial symbols
12431 only) and for offsets into structures which are expected to be
12432 (more or less) constant. The partial symbol case should go away,
12433 and only the constant case should remain. That will let this
12434 function complain more accurately. A few special modes are allowed
12435 without complaint for global variables (for instance, global
12436 register values and thread-local values).
c906108c
SS
12437
12438 A location description containing no operations indicates that the
4cecd739 12439 object is optimized out. The return value is 0 for that case.
6b992462
DJ
12440 FIXME drow/2003-11-16: No callers check for this case any more; soon all
12441 callers will only want a very basic result and this can become a
12442 complaint.
c906108c 12443
c906108c
SS
12444 Note that stack[0] is unused except as a default error return.
12445 Note that stack overflow is not yet handled. */
12446
12447static CORE_ADDR
e7c27a73 12448decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 12449{
e7c27a73 12450 struct objfile *objfile = cu->objfile;
c906108c
SS
12451 int i;
12452 int size = blk->size;
fe1b8b76 12453 gdb_byte *data = blk->data;
c906108c
SS
12454 CORE_ADDR stack[64];
12455 int stacki;
12456 unsigned int bytes_read, unsnd;
fe1b8b76 12457 gdb_byte op;
c906108c
SS
12458
12459 i = 0;
12460 stacki = 0;
12461 stack[stacki] = 0;
c906108c
SS
12462
12463 while (i < size)
12464 {
c906108c
SS
12465 op = data[i++];
12466 switch (op)
12467 {
f1bea926
JM
12468 case DW_OP_lit0:
12469 case DW_OP_lit1:
12470 case DW_OP_lit2:
12471 case DW_OP_lit3:
12472 case DW_OP_lit4:
12473 case DW_OP_lit5:
12474 case DW_OP_lit6:
12475 case DW_OP_lit7:
12476 case DW_OP_lit8:
12477 case DW_OP_lit9:
12478 case DW_OP_lit10:
12479 case DW_OP_lit11:
12480 case DW_OP_lit12:
12481 case DW_OP_lit13:
12482 case DW_OP_lit14:
12483 case DW_OP_lit15:
12484 case DW_OP_lit16:
12485 case DW_OP_lit17:
12486 case DW_OP_lit18:
12487 case DW_OP_lit19:
12488 case DW_OP_lit20:
12489 case DW_OP_lit21:
12490 case DW_OP_lit22:
12491 case DW_OP_lit23:
12492 case DW_OP_lit24:
12493 case DW_OP_lit25:
12494 case DW_OP_lit26:
12495 case DW_OP_lit27:
12496 case DW_OP_lit28:
12497 case DW_OP_lit29:
12498 case DW_OP_lit30:
12499 case DW_OP_lit31:
12500 stack[++stacki] = op - DW_OP_lit0;
12501 break;
12502
c906108c
SS
12503 case DW_OP_reg0:
12504 case DW_OP_reg1:
12505 case DW_OP_reg2:
12506 case DW_OP_reg3:
12507 case DW_OP_reg4:
12508 case DW_OP_reg5:
12509 case DW_OP_reg6:
12510 case DW_OP_reg7:
12511 case DW_OP_reg8:
12512 case DW_OP_reg9:
12513 case DW_OP_reg10:
12514 case DW_OP_reg11:
12515 case DW_OP_reg12:
12516 case DW_OP_reg13:
12517 case DW_OP_reg14:
12518 case DW_OP_reg15:
12519 case DW_OP_reg16:
12520 case DW_OP_reg17:
12521 case DW_OP_reg18:
12522 case DW_OP_reg19:
12523 case DW_OP_reg20:
12524 case DW_OP_reg21:
12525 case DW_OP_reg22:
12526 case DW_OP_reg23:
12527 case DW_OP_reg24:
12528 case DW_OP_reg25:
12529 case DW_OP_reg26:
12530 case DW_OP_reg27:
12531 case DW_OP_reg28:
12532 case DW_OP_reg29:
12533 case DW_OP_reg30:
12534 case DW_OP_reg31:
c906108c 12535 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
12536 if (i < size)
12537 dwarf2_complex_location_expr_complaint ();
c906108c
SS
12538 break;
12539
12540 case DW_OP_regx:
c906108c
SS
12541 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
12542 i += bytes_read;
c906108c 12543 stack[++stacki] = unsnd;
4cecd739
DJ
12544 if (i < size)
12545 dwarf2_complex_location_expr_complaint ();
c906108c
SS
12546 break;
12547
12548 case DW_OP_addr:
107d2387 12549 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 12550 cu, &bytes_read);
107d2387 12551 i += bytes_read;
c906108c
SS
12552 break;
12553
12554 case DW_OP_const1u:
12555 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
12556 i += 1;
12557 break;
12558
12559 case DW_OP_const1s:
12560 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
12561 i += 1;
12562 break;
12563
12564 case DW_OP_const2u:
12565 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
12566 i += 2;
12567 break;
12568
12569 case DW_OP_const2s:
12570 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
12571 i += 2;
12572 break;
12573
12574 case DW_OP_const4u:
12575 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
12576 i += 4;
12577 break;
12578
12579 case DW_OP_const4s:
12580 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
12581 i += 4;
12582 break;
12583
12584 case DW_OP_constu:
12585 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 12586 &bytes_read);
c906108c
SS
12587 i += bytes_read;
12588 break;
12589
12590 case DW_OP_consts:
12591 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
12592 i += bytes_read;
12593 break;
12594
f1bea926
JM
12595 case DW_OP_dup:
12596 stack[stacki + 1] = stack[stacki];
12597 stacki++;
12598 break;
12599
c906108c
SS
12600 case DW_OP_plus:
12601 stack[stacki - 1] += stack[stacki];
12602 stacki--;
12603 break;
12604
12605 case DW_OP_plus_uconst:
12606 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
12607 i += bytes_read;
12608 break;
12609
12610 case DW_OP_minus:
f1bea926 12611 stack[stacki - 1] -= stack[stacki];
c906108c
SS
12612 stacki--;
12613 break;
12614
7a292a7a 12615 case DW_OP_deref:
7a292a7a 12616 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
12617 this using GDB's address_class enum. This is valid for partial
12618 global symbols, although the variable's address will be bogus
12619 in the psymtab. */
7a292a7a 12620 if (i < size)
4d3c2250 12621 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
12622 break;
12623
9d774e44 12624 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
12625 /* The top of the stack has the offset from the beginning
12626 of the thread control block at which the variable is located. */
12627 /* Nothing should follow this operator, so the top of stack would
12628 be returned. */
4cecd739
DJ
12629 /* This is valid for partial global symbols, but the variable's
12630 address will be bogus in the psymtab. */
9d774e44 12631 if (i < size)
4d3c2250 12632 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
12633 break;
12634
42be36b3
CT
12635 case DW_OP_GNU_uninit:
12636 break;
12637
c906108c 12638 default:
e2e0b3e5 12639 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 12640 dwarf_stack_op_name (op, 1));
c906108c
SS
12641 return (stack[stacki]);
12642 }
12643 }
12644 return (stack[stacki]);
12645}
12646
12647/* memory allocation interface */
12648
c906108c 12649static struct dwarf_block *
7b5a2f43 12650dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
12651{
12652 struct dwarf_block *blk;
12653
12654 blk = (struct dwarf_block *)
7b5a2f43 12655 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
12656 return (blk);
12657}
12658
12659static struct abbrev_info *
f3dd6933 12660dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
12661{
12662 struct abbrev_info *abbrev;
12663
f3dd6933
DJ
12664 abbrev = (struct abbrev_info *)
12665 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
12666 memset (abbrev, 0, sizeof (struct abbrev_info));
12667 return (abbrev);
12668}
12669
12670static struct die_info *
b60c80d6 12671dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
12672{
12673 struct die_info *die;
b60c80d6
DJ
12674 size_t size = sizeof (struct die_info);
12675
12676 if (num_attrs > 1)
12677 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 12678
b60c80d6 12679 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
12680 memset (die, 0, sizeof (struct die_info));
12681 return (die);
12682}
2e276125
JB
12683
12684\f
12685/* Macro support. */
12686
12687
12688/* Return the full name of file number I in *LH's file name table.
12689 Use COMP_DIR as the name of the current directory of the
12690 compilation. The result is allocated using xmalloc; the caller is
12691 responsible for freeing it. */
12692static char *
12693file_full_name (int file, struct line_header *lh, const char *comp_dir)
12694{
6a83a1e6
EZ
12695 /* Is the file number a valid index into the line header's file name
12696 table? Remember that file numbers start with one, not zero. */
12697 if (1 <= file && file <= lh->num_file_names)
12698 {
12699 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 12700
6a83a1e6
EZ
12701 if (IS_ABSOLUTE_PATH (fe->name))
12702 return xstrdup (fe->name);
12703 else
12704 {
12705 const char *dir;
12706 int dir_len;
12707 char *full_name;
12708
12709 if (fe->dir_index)
12710 dir = lh->include_dirs[fe->dir_index - 1];
12711 else
12712 dir = comp_dir;
12713
12714 if (dir)
12715 {
12716 dir_len = strlen (dir);
12717 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
12718 strcpy (full_name, dir);
12719 full_name[dir_len] = '/';
12720 strcpy (full_name + dir_len + 1, fe->name);
12721 return full_name;
12722 }
12723 else
12724 return xstrdup (fe->name);
12725 }
12726 }
2e276125
JB
12727 else
12728 {
6a83a1e6
EZ
12729 /* The compiler produced a bogus file number. We can at least
12730 record the macro definitions made in the file, even if we
12731 won't be able to find the file by name. */
12732 char fake_name[80];
9a619af0 12733
6a83a1e6 12734 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 12735
6e70227d 12736 complaint (&symfile_complaints,
6a83a1e6
EZ
12737 _("bad file number in macro information (%d)"),
12738 file);
2e276125 12739
6a83a1e6 12740 return xstrdup (fake_name);
2e276125
JB
12741 }
12742}
12743
12744
12745static struct macro_source_file *
12746macro_start_file (int file, int line,
12747 struct macro_source_file *current_file,
12748 const char *comp_dir,
12749 struct line_header *lh, struct objfile *objfile)
12750{
12751 /* The full name of this source file. */
12752 char *full_name = file_full_name (file, lh, comp_dir);
12753
12754 /* We don't create a macro table for this compilation unit
12755 at all until we actually get a filename. */
12756 if (! pending_macros)
4a146b47 12757 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 12758 objfile->macro_cache);
2e276125
JB
12759
12760 if (! current_file)
12761 /* If we have no current file, then this must be the start_file
12762 directive for the compilation unit's main source file. */
12763 current_file = macro_set_main (pending_macros, full_name);
12764 else
12765 current_file = macro_include (current_file, line, full_name);
12766
12767 xfree (full_name);
6e70227d 12768
2e276125
JB
12769 return current_file;
12770}
12771
12772
12773/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
12774 followed by a null byte. */
12775static char *
12776copy_string (const char *buf, int len)
12777{
12778 char *s = xmalloc (len + 1);
9a619af0 12779
2e276125
JB
12780 memcpy (s, buf, len);
12781 s[len] = '\0';
2e276125
JB
12782 return s;
12783}
12784
12785
12786static const char *
12787consume_improper_spaces (const char *p, const char *body)
12788{
12789 if (*p == ' ')
12790 {
4d3c2250 12791 complaint (&symfile_complaints,
e2e0b3e5 12792 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 12793 body);
2e276125
JB
12794
12795 while (*p == ' ')
12796 p++;
12797 }
12798
12799 return p;
12800}
12801
12802
12803static void
12804parse_macro_definition (struct macro_source_file *file, int line,
12805 const char *body)
12806{
12807 const char *p;
12808
12809 /* The body string takes one of two forms. For object-like macro
12810 definitions, it should be:
12811
12812 <macro name> " " <definition>
12813
12814 For function-like macro definitions, it should be:
12815
12816 <macro name> "() " <definition>
12817 or
12818 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
12819
12820 Spaces may appear only where explicitly indicated, and in the
12821 <definition>.
12822
12823 The Dwarf 2 spec says that an object-like macro's name is always
12824 followed by a space, but versions of GCC around March 2002 omit
6e70227d 12825 the space when the macro's definition is the empty string.
2e276125
JB
12826
12827 The Dwarf 2 spec says that there should be no spaces between the
12828 formal arguments in a function-like macro's formal argument list,
12829 but versions of GCC around March 2002 include spaces after the
12830 commas. */
12831
12832
12833 /* Find the extent of the macro name. The macro name is terminated
12834 by either a space or null character (for an object-like macro) or
12835 an opening paren (for a function-like macro). */
12836 for (p = body; *p; p++)
12837 if (*p == ' ' || *p == '(')
12838 break;
12839
12840 if (*p == ' ' || *p == '\0')
12841 {
12842 /* It's an object-like macro. */
12843 int name_len = p - body;
12844 char *name = copy_string (body, name_len);
12845 const char *replacement;
12846
12847 if (*p == ' ')
12848 replacement = body + name_len + 1;
12849 else
12850 {
4d3c2250 12851 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12852 replacement = body + name_len;
12853 }
6e70227d 12854
2e276125
JB
12855 macro_define_object (file, line, name, replacement);
12856
12857 xfree (name);
12858 }
12859 else if (*p == '(')
12860 {
12861 /* It's a function-like macro. */
12862 char *name = copy_string (body, p - body);
12863 int argc = 0;
12864 int argv_size = 1;
12865 char **argv = xmalloc (argv_size * sizeof (*argv));
12866
12867 p++;
12868
12869 p = consume_improper_spaces (p, body);
12870
12871 /* Parse the formal argument list. */
12872 while (*p && *p != ')')
12873 {
12874 /* Find the extent of the current argument name. */
12875 const char *arg_start = p;
12876
12877 while (*p && *p != ',' && *p != ')' && *p != ' ')
12878 p++;
12879
12880 if (! *p || p == arg_start)
4d3c2250 12881 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12882 else
12883 {
12884 /* Make sure argv has room for the new argument. */
12885 if (argc >= argv_size)
12886 {
12887 argv_size *= 2;
12888 argv = xrealloc (argv, argv_size * sizeof (*argv));
12889 }
12890
12891 argv[argc++] = copy_string (arg_start, p - arg_start);
12892 }
12893
12894 p = consume_improper_spaces (p, body);
12895
12896 /* Consume the comma, if present. */
12897 if (*p == ',')
12898 {
12899 p++;
12900
12901 p = consume_improper_spaces (p, body);
12902 }
12903 }
12904
12905 if (*p == ')')
12906 {
12907 p++;
12908
12909 if (*p == ' ')
12910 /* Perfectly formed definition, no complaints. */
12911 macro_define_function (file, line, name,
6e70227d 12912 argc, (const char **) argv,
2e276125
JB
12913 p + 1);
12914 else if (*p == '\0')
12915 {
12916 /* Complain, but do define it. */
4d3c2250 12917 dwarf2_macro_malformed_definition_complaint (body);
2e276125 12918 macro_define_function (file, line, name,
6e70227d 12919 argc, (const char **) argv,
2e276125
JB
12920 p);
12921 }
12922 else
12923 /* Just complain. */
4d3c2250 12924 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12925 }
12926 else
12927 /* Just complain. */
4d3c2250 12928 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12929
12930 xfree (name);
12931 {
12932 int i;
12933
12934 for (i = 0; i < argc; i++)
12935 xfree (argv[i]);
12936 }
12937 xfree (argv);
12938 }
12939 else
4d3c2250 12940 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12941}
12942
12943
12944static void
12945dwarf_decode_macros (struct line_header *lh, unsigned int offset,
12946 char *comp_dir, bfd *abfd,
e7c27a73 12947 struct dwarf2_cu *cu)
2e276125 12948{
fe1b8b76 12949 gdb_byte *mac_ptr, *mac_end;
2e276125 12950 struct macro_source_file *current_file = 0;
757a13d0
JK
12951 enum dwarf_macinfo_record_type macinfo_type;
12952 int at_commandline;
2e276125 12953
be391dca
TT
12954 dwarf2_read_section (dwarf2_per_objfile->objfile,
12955 &dwarf2_per_objfile->macinfo);
dce234bc 12956 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 12957 {
e2e0b3e5 12958 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
12959 return;
12960 }
12961
757a13d0
JK
12962 /* First pass: Find the name of the base filename.
12963 This filename is needed in order to process all macros whose definition
12964 (or undefinition) comes from the command line. These macros are defined
12965 before the first DW_MACINFO_start_file entry, and yet still need to be
12966 associated to the base file.
12967
12968 To determine the base file name, we scan the macro definitions until we
12969 reach the first DW_MACINFO_start_file entry. We then initialize
12970 CURRENT_FILE accordingly so that any macro definition found before the
12971 first DW_MACINFO_start_file can still be associated to the base file. */
12972
dce234bc
PP
12973 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
12974 mac_end = dwarf2_per_objfile->macinfo.buffer
12975 + dwarf2_per_objfile->macinfo.size;
2e276125 12976
757a13d0 12977 do
2e276125 12978 {
2e276125
JB
12979 /* Do we at least have room for a macinfo type byte? */
12980 if (mac_ptr >= mac_end)
12981 {
757a13d0
JK
12982 /* Complaint is printed during the second pass as GDB will probably
12983 stop the first pass earlier upon finding DW_MACINFO_start_file. */
12984 break;
2e276125
JB
12985 }
12986
12987 macinfo_type = read_1_byte (abfd, mac_ptr);
12988 mac_ptr++;
12989
12990 switch (macinfo_type)
12991 {
12992 /* A zero macinfo type indicates the end of the macro
12993 information. */
12994 case 0:
757a13d0
JK
12995 break;
12996
12997 case DW_MACINFO_define:
12998 case DW_MACINFO_undef:
12999 /* Only skip the data by MAC_PTR. */
13000 {
13001 unsigned int bytes_read;
13002
13003 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13004 mac_ptr += bytes_read;
9b1c24c8 13005 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13006 mac_ptr += bytes_read;
13007 }
13008 break;
13009
13010 case DW_MACINFO_start_file:
13011 {
13012 unsigned int bytes_read;
13013 int line, file;
13014
13015 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13016 mac_ptr += bytes_read;
13017 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13018 mac_ptr += bytes_read;
13019
13020 current_file = macro_start_file (file, line, current_file, comp_dir,
13021 lh, cu->objfile);
13022 }
13023 break;
13024
13025 case DW_MACINFO_end_file:
13026 /* No data to skip by MAC_PTR. */
13027 break;
13028
13029 case DW_MACINFO_vendor_ext:
13030 /* Only skip the data by MAC_PTR. */
13031 {
13032 unsigned int bytes_read;
13033
13034 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13035 mac_ptr += bytes_read;
9b1c24c8 13036 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13037 mac_ptr += bytes_read;
13038 }
13039 break;
13040
13041 default:
13042 break;
13043 }
13044 } while (macinfo_type != 0 && current_file == NULL);
13045
13046 /* Second pass: Process all entries.
13047
13048 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13049 command-line macro definitions/undefinitions. This flag is unset when we
13050 reach the first DW_MACINFO_start_file entry. */
13051
dce234bc 13052 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
13053
13054 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13055 GDB is still reading the definitions from command line. First
13056 DW_MACINFO_start_file will need to be ignored as it was already executed
13057 to create CURRENT_FILE for the main source holding also the command line
13058 definitions. On first met DW_MACINFO_start_file this flag is reset to
13059 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13060
13061 at_commandline = 1;
13062
13063 do
13064 {
13065 /* Do we at least have room for a macinfo type byte? */
13066 if (mac_ptr >= mac_end)
13067 {
13068 dwarf2_macros_too_long_complaint ();
13069 break;
13070 }
13071
13072 macinfo_type = read_1_byte (abfd, mac_ptr);
13073 mac_ptr++;
13074
13075 switch (macinfo_type)
13076 {
13077 /* A zero macinfo type indicates the end of the macro
13078 information. */
13079 case 0:
13080 break;
2e276125
JB
13081
13082 case DW_MACINFO_define:
13083 case DW_MACINFO_undef:
13084 {
891d2f0b 13085 unsigned int bytes_read;
2e276125
JB
13086 int line;
13087 char *body;
13088
13089 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13090 mac_ptr += bytes_read;
9b1c24c8 13091 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13092 mac_ptr += bytes_read;
13093
13094 if (! current_file)
757a13d0
JK
13095 {
13096 /* DWARF violation as no main source is present. */
13097 complaint (&symfile_complaints,
13098 _("debug info with no main source gives macro %s "
13099 "on line %d: %s"),
6e70227d
DE
13100 macinfo_type == DW_MACINFO_define ?
13101 _("definition") :
905e0470
PM
13102 macinfo_type == DW_MACINFO_undef ?
13103 _("undefinition") :
13104 _("something-or-other"), line, body);
757a13d0
JK
13105 break;
13106 }
13107 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 13108 complaint (&symfile_complaints,
757a13d0
JK
13109 _("debug info gives %s macro %s with %s line %d: %s"),
13110 at_commandline ? _("command-line") : _("in-file"),
905e0470 13111 macinfo_type == DW_MACINFO_define ?
6e70227d 13112 _("definition") :
905e0470
PM
13113 macinfo_type == DW_MACINFO_undef ?
13114 _("undefinition") :
13115 _("something-or-other"),
757a13d0
JK
13116 line == 0 ? _("zero") : _("non-zero"), line, body);
13117
13118 if (macinfo_type == DW_MACINFO_define)
13119 parse_macro_definition (current_file, line, body);
13120 else if (macinfo_type == DW_MACINFO_undef)
13121 macro_undef (current_file, line, body);
2e276125
JB
13122 }
13123 break;
13124
13125 case DW_MACINFO_start_file:
13126 {
891d2f0b 13127 unsigned int bytes_read;
2e276125
JB
13128 int line, file;
13129
13130 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13131 mac_ptr += bytes_read;
13132 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13133 mac_ptr += bytes_read;
13134
757a13d0
JK
13135 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13136 complaint (&symfile_complaints,
13137 _("debug info gives source %d included "
13138 "from %s at %s line %d"),
13139 file, at_commandline ? _("command-line") : _("file"),
13140 line == 0 ? _("zero") : _("non-zero"), line);
13141
13142 if (at_commandline)
13143 {
13144 /* This DW_MACINFO_start_file was executed in the pass one. */
13145 at_commandline = 0;
13146 }
13147 else
13148 current_file = macro_start_file (file, line,
13149 current_file, comp_dir,
13150 lh, cu->objfile);
2e276125
JB
13151 }
13152 break;
13153
13154 case DW_MACINFO_end_file:
13155 if (! current_file)
4d3c2250 13156 complaint (&symfile_complaints,
e2e0b3e5 13157 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
13158 else
13159 {
13160 current_file = current_file->included_by;
13161 if (! current_file)
13162 {
13163 enum dwarf_macinfo_record_type next_type;
13164
13165 /* GCC circa March 2002 doesn't produce the zero
13166 type byte marking the end of the compilation
13167 unit. Complain if it's not there, but exit no
13168 matter what. */
13169
13170 /* Do we at least have room for a macinfo type byte? */
13171 if (mac_ptr >= mac_end)
13172 {
4d3c2250 13173 dwarf2_macros_too_long_complaint ();
2e276125
JB
13174 return;
13175 }
13176
13177 /* We don't increment mac_ptr here, so this is just
13178 a look-ahead. */
13179 next_type = read_1_byte (abfd, mac_ptr);
13180 if (next_type != 0)
4d3c2250 13181 complaint (&symfile_complaints,
e2e0b3e5 13182 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
13183
13184 return;
13185 }
13186 }
13187 break;
13188
13189 case DW_MACINFO_vendor_ext:
13190 {
891d2f0b 13191 unsigned int bytes_read;
2e276125
JB
13192 int constant;
13193 char *string;
13194
13195 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13196 mac_ptr += bytes_read;
9b1c24c8 13197 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13198 mac_ptr += bytes_read;
13199
13200 /* We don't recognize any vendor extensions. */
13201 }
13202 break;
13203 }
757a13d0 13204 } while (macinfo_type != 0);
2e276125 13205}
8e19ed76
PS
13206
13207/* Check if the attribute's form is a DW_FORM_block*
13208 if so return true else false. */
13209static int
13210attr_form_is_block (struct attribute *attr)
13211{
13212 return (attr == NULL ? 0 :
13213 attr->form == DW_FORM_block1
13214 || attr->form == DW_FORM_block2
13215 || attr->form == DW_FORM_block4
2dc7f7b3
TT
13216 || attr->form == DW_FORM_block
13217 || attr->form == DW_FORM_exprloc);
8e19ed76 13218}
4c2df51b 13219
c6a0999f
JB
13220/* Return non-zero if ATTR's value is a section offset --- classes
13221 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
13222 You may use DW_UNSND (attr) to retrieve such offsets.
13223
13224 Section 7.5.4, "Attribute Encodings", explains that no attribute
13225 may have a value that belongs to more than one of these classes; it
13226 would be ambiguous if we did, because we use the same forms for all
13227 of them. */
3690dd37
JB
13228static int
13229attr_form_is_section_offset (struct attribute *attr)
13230{
13231 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
13232 || attr->form == DW_FORM_data8
13233 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
13234}
13235
13236
13237/* Return non-zero if ATTR's value falls in the 'constant' class, or
13238 zero otherwise. When this function returns true, you can apply
13239 dwarf2_get_attr_constant_value to it.
13240
13241 However, note that for some attributes you must check
13242 attr_form_is_section_offset before using this test. DW_FORM_data4
13243 and DW_FORM_data8 are members of both the constant class, and of
13244 the classes that contain offsets into other debug sections
13245 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
13246 that, if an attribute's can be either a constant or one of the
13247 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
13248 taken as section offsets, not constants. */
13249static int
13250attr_form_is_constant (struct attribute *attr)
13251{
13252 switch (attr->form)
13253 {
13254 case DW_FORM_sdata:
13255 case DW_FORM_udata:
13256 case DW_FORM_data1:
13257 case DW_FORM_data2:
13258 case DW_FORM_data4:
13259 case DW_FORM_data8:
13260 return 1;
13261 default:
13262 return 0;
13263 }
13264}
13265
4c2df51b
DJ
13266static void
13267dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 13268 struct dwarf2_cu *cu)
4c2df51b 13269{
3690dd37 13270 if (attr_form_is_section_offset (attr)
99bcc461
DJ
13271 /* ".debug_loc" may not exist at all, or the offset may be outside
13272 the section. If so, fall through to the complaint in the
13273 other branch. */
dce234bc 13274 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 13275 {
0d53c4c4 13276 struct dwarf2_loclist_baton *baton;
4c2df51b 13277
4a146b47 13278 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13279 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
13280 baton->per_cu = cu->per_cu;
13281 gdb_assert (baton->per_cu);
4c2df51b 13282
be391dca
TT
13283 dwarf2_read_section (dwarf2_per_objfile->objfile,
13284 &dwarf2_per_objfile->loc);
13285
0d53c4c4
DJ
13286 /* We don't know how long the location list is, but make sure we
13287 don't run off the edge of the section. */
dce234bc
PP
13288 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13289 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
13290 baton->base_address = cu->base_address;
13291 if (cu->base_known == 0)
0d53c4c4 13292 complaint (&symfile_complaints,
e2e0b3e5 13293 _("Location list used without specifying the CU base address."));
4c2df51b 13294
768a979c 13295 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
13296 SYMBOL_LOCATION_BATON (sym) = baton;
13297 }
13298 else
13299 {
13300 struct dwarf2_locexpr_baton *baton;
13301
4a146b47 13302 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13303 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
13304 baton->per_cu = cu->per_cu;
13305 gdb_assert (baton->per_cu);
0d53c4c4
DJ
13306
13307 if (attr_form_is_block (attr))
13308 {
13309 /* Note that we're just copying the block's data pointer
13310 here, not the actual data. We're still pointing into the
6502dd73
DJ
13311 info_buffer for SYM's objfile; right now we never release
13312 that buffer, but when we do clean up properly this may
13313 need to change. */
0d53c4c4
DJ
13314 baton->size = DW_BLOCK (attr)->size;
13315 baton->data = DW_BLOCK (attr)->data;
13316 }
13317 else
13318 {
13319 dwarf2_invalid_attrib_class_complaint ("location description",
13320 SYMBOL_NATURAL_NAME (sym));
13321 baton->size = 0;
13322 baton->data = NULL;
13323 }
6e70227d 13324
768a979c 13325 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
13326 SYMBOL_LOCATION_BATON (sym) = baton;
13327 }
4c2df51b 13328}
6502dd73 13329
9aa1f1e3
TT
13330/* Return the OBJFILE associated with the compilation unit CU. If CU
13331 came from a separate debuginfo file, then the master objfile is
13332 returned. */
ae0d2f24
UW
13333
13334struct objfile *
13335dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13336{
9291a0cd 13337 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13338
13339 /* Return the master objfile, so that we can report and look up the
13340 correct file containing this variable. */
13341 if (objfile->separate_debug_objfile_backlink)
13342 objfile = objfile->separate_debug_objfile_backlink;
13343
13344 return objfile;
13345}
13346
13347/* Return the address size given in the compilation unit header for CU. */
13348
13349CORE_ADDR
13350dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
13351{
13352 if (per_cu->cu)
13353 return per_cu->cu->header.addr_size;
13354 else
13355 {
13356 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 13357 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13358 struct dwarf2_per_objfile *per_objfile
13359 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 13360 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 13361 struct comp_unit_head cu_header;
9a619af0 13362
ae0d2f24
UW
13363 memset (&cu_header, 0, sizeof cu_header);
13364 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13365 return cu_header.addr_size;
13366 }
13367}
13368
9eae7c52
TT
13369/* Return the offset size given in the compilation unit header for CU. */
13370
13371int
13372dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
13373{
13374 if (per_cu->cu)
13375 return per_cu->cu->header.offset_size;
13376 else
13377 {
13378 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 13379 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
13380 struct dwarf2_per_objfile *per_objfile
13381 = objfile_data (objfile, dwarf2_objfile_data_key);
13382 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13383 struct comp_unit_head cu_header;
13384
13385 memset (&cu_header, 0, sizeof cu_header);
13386 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13387 return cu_header.offset_size;
13388 }
13389}
13390
9aa1f1e3
TT
13391/* Return the text offset of the CU. The returned offset comes from
13392 this CU's objfile. If this objfile came from a separate debuginfo
13393 file, then the offset may be different from the corresponding
13394 offset in the parent objfile. */
13395
13396CORE_ADDR
13397dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
13398{
bb3fa9d0 13399 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
13400
13401 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13402}
13403
348e048f
DE
13404/* Locate the .debug_info compilation unit from CU's objfile which contains
13405 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
13406
13407static struct dwarf2_per_cu_data *
c764a876 13408dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
13409 struct objfile *objfile)
13410{
13411 struct dwarf2_per_cu_data *this_cu;
13412 int low, high;
13413
ae038cb0
DJ
13414 low = 0;
13415 high = dwarf2_per_objfile->n_comp_units - 1;
13416 while (high > low)
13417 {
13418 int mid = low + (high - low) / 2;
9a619af0 13419
ae038cb0
DJ
13420 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
13421 high = mid;
13422 else
13423 low = mid + 1;
13424 }
13425 gdb_assert (low == high);
13426 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
13427 {
10b3939b 13428 if (low == 0)
8a3fe4f8
AC
13429 error (_("Dwarf Error: could not find partial DIE containing "
13430 "offset 0x%lx [in module %s]"),
10b3939b
DJ
13431 (long) offset, bfd_get_filename (objfile->obfd));
13432
ae038cb0
DJ
13433 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
13434 return dwarf2_per_objfile->all_comp_units[low-1];
13435 }
13436 else
13437 {
13438 this_cu = dwarf2_per_objfile->all_comp_units[low];
13439 if (low == dwarf2_per_objfile->n_comp_units - 1
13440 && offset >= this_cu->offset + this_cu->length)
c764a876 13441 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
13442 gdb_assert (offset < this_cu->offset + this_cu->length);
13443 return this_cu;
13444 }
13445}
13446
10b3939b
DJ
13447/* Locate the compilation unit from OBJFILE which is located at exactly
13448 OFFSET. Raises an error on failure. */
13449
ae038cb0 13450static struct dwarf2_per_cu_data *
c764a876 13451dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
13452{
13453 struct dwarf2_per_cu_data *this_cu;
9a619af0 13454
ae038cb0
DJ
13455 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
13456 if (this_cu->offset != offset)
c764a876 13457 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
13458 return this_cu;
13459}
13460
93311388
DE
13461/* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
13462
13463static struct dwarf2_cu *
13464alloc_one_comp_unit (struct objfile *objfile)
13465{
13466 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
13467 cu->objfile = objfile;
13468 obstack_init (&cu->comp_unit_obstack);
13469 return cu;
13470}
13471
ae038cb0
DJ
13472/* Release one cached compilation unit, CU. We unlink it from the tree
13473 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
13474 the caller is responsible for that.
13475 NOTE: DATA is a void * because this function is also used as a
13476 cleanup routine. */
ae038cb0
DJ
13477
13478static void
13479free_one_comp_unit (void *data)
13480{
13481 struct dwarf2_cu *cu = data;
13482
13483 if (cu->per_cu != NULL)
13484 cu->per_cu->cu = NULL;
13485 cu->per_cu = NULL;
13486
13487 obstack_free (&cu->comp_unit_obstack, NULL);
13488
13489 xfree (cu);
13490}
13491
72bf9492 13492/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
13493 when we're finished with it. We can't free the pointer itself, but be
13494 sure to unlink it from the cache. Also release any associated storage
13495 and perform cache maintenance.
72bf9492
DJ
13496
13497 Only used during partial symbol parsing. */
13498
13499static void
13500free_stack_comp_unit (void *data)
13501{
13502 struct dwarf2_cu *cu = data;
13503
13504 obstack_free (&cu->comp_unit_obstack, NULL);
13505 cu->partial_dies = NULL;
ae038cb0
DJ
13506
13507 if (cu->per_cu != NULL)
13508 {
13509 /* This compilation unit is on the stack in our caller, so we
13510 should not xfree it. Just unlink it. */
13511 cu->per_cu->cu = NULL;
13512 cu->per_cu = NULL;
13513
13514 /* If we had a per-cu pointer, then we may have other compilation
13515 units loaded, so age them now. */
13516 age_cached_comp_units ();
13517 }
13518}
13519
13520/* Free all cached compilation units. */
13521
13522static void
13523free_cached_comp_units (void *data)
13524{
13525 struct dwarf2_per_cu_data *per_cu, **last_chain;
13526
13527 per_cu = dwarf2_per_objfile->read_in_chain;
13528 last_chain = &dwarf2_per_objfile->read_in_chain;
13529 while (per_cu != NULL)
13530 {
13531 struct dwarf2_per_cu_data *next_cu;
13532
13533 next_cu = per_cu->cu->read_in_chain;
13534
13535 free_one_comp_unit (per_cu->cu);
13536 *last_chain = next_cu;
13537
13538 per_cu = next_cu;
13539 }
13540}
13541
13542/* Increase the age counter on each cached compilation unit, and free
13543 any that are too old. */
13544
13545static void
13546age_cached_comp_units (void)
13547{
13548 struct dwarf2_per_cu_data *per_cu, **last_chain;
13549
13550 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
13551 per_cu = dwarf2_per_objfile->read_in_chain;
13552 while (per_cu != NULL)
13553 {
13554 per_cu->cu->last_used ++;
13555 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
13556 dwarf2_mark (per_cu->cu);
13557 per_cu = per_cu->cu->read_in_chain;
13558 }
13559
13560 per_cu = dwarf2_per_objfile->read_in_chain;
13561 last_chain = &dwarf2_per_objfile->read_in_chain;
13562 while (per_cu != NULL)
13563 {
13564 struct dwarf2_per_cu_data *next_cu;
13565
13566 next_cu = per_cu->cu->read_in_chain;
13567
13568 if (!per_cu->cu->mark)
13569 {
13570 free_one_comp_unit (per_cu->cu);
13571 *last_chain = next_cu;
13572 }
13573 else
13574 last_chain = &per_cu->cu->read_in_chain;
13575
13576 per_cu = next_cu;
13577 }
13578}
13579
13580/* Remove a single compilation unit from the cache. */
13581
13582static void
13583free_one_cached_comp_unit (void *target_cu)
13584{
13585 struct dwarf2_per_cu_data *per_cu, **last_chain;
13586
13587 per_cu = dwarf2_per_objfile->read_in_chain;
13588 last_chain = &dwarf2_per_objfile->read_in_chain;
13589 while (per_cu != NULL)
13590 {
13591 struct dwarf2_per_cu_data *next_cu;
13592
13593 next_cu = per_cu->cu->read_in_chain;
13594
13595 if (per_cu->cu == target_cu)
13596 {
13597 free_one_comp_unit (per_cu->cu);
13598 *last_chain = next_cu;
13599 break;
13600 }
13601 else
13602 last_chain = &per_cu->cu->read_in_chain;
13603
13604 per_cu = next_cu;
13605 }
13606}
13607
fe3e1990
DJ
13608/* Release all extra memory associated with OBJFILE. */
13609
13610void
13611dwarf2_free_objfile (struct objfile *objfile)
13612{
13613 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
13614
13615 if (dwarf2_per_objfile == NULL)
13616 return;
13617
13618 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
13619 free_cached_comp_units (NULL);
13620
9291a0cd
TT
13621 if (dwarf2_per_objfile->using_index)
13622 {
13623 int i;
13624
13625 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
13626 {
13627 int j;
13628 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
13629
13630 if (!cu->v.quick->lines)
13631 continue;
13632
13633 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
13634 {
13635 if (cu->v.quick->file_names)
13636 xfree ((void *) cu->v.quick->file_names[j]);
13637 if (cu->v.quick->full_names)
13638 xfree ((void *) cu->v.quick->full_names[j]);
13639 }
13640
13641 free_line_header (cu->v.quick->lines);
13642 }
13643 }
13644
fe3e1990
DJ
13645 /* Everything else should be on the objfile obstack. */
13646}
13647
1c379e20
DJ
13648/* A pair of DIE offset and GDB type pointer. We store these
13649 in a hash table separate from the DIEs, and preserve them
13650 when the DIEs are flushed out of cache. */
13651
13652struct dwarf2_offset_and_type
13653{
13654 unsigned int offset;
13655 struct type *type;
13656};
13657
13658/* Hash function for a dwarf2_offset_and_type. */
13659
13660static hashval_t
13661offset_and_type_hash (const void *item)
13662{
13663 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 13664
1c379e20
DJ
13665 return ofs->offset;
13666}
13667
13668/* Equality function for a dwarf2_offset_and_type. */
13669
13670static int
13671offset_and_type_eq (const void *item_lhs, const void *item_rhs)
13672{
13673 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
13674 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 13675
1c379e20
DJ
13676 return ofs_lhs->offset == ofs_rhs->offset;
13677}
13678
13679/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
13680 table if necessary. For convenience, return TYPE.
13681
13682 The DIEs reading must have careful ordering to:
13683 * Not cause infite loops trying to read in DIEs as a prerequisite for
13684 reading current DIE.
13685 * Not trying to dereference contents of still incompletely read in types
13686 while reading in other DIEs.
13687 * Enable referencing still incompletely read in types just by a pointer to
13688 the type without accessing its fields.
13689
13690 Therefore caller should follow these rules:
13691 * Try to fetch any prerequisite types we may need to build this DIE type
13692 before building the type and calling set_die_type.
13693 * After building typer call set_die_type for current DIE as soon as
13694 possible before fetching more types to complete the current type.
13695 * Make the type as complete as possible before fetching more types. */
1c379e20 13696
f792889a 13697static struct type *
1c379e20
DJ
13698set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13699{
13700 struct dwarf2_offset_and_type **slot, ofs;
13701
b4ba55a1
JB
13702 /* For Ada types, make sure that the gnat-specific data is always
13703 initialized (if not already set). There are a few types where
13704 we should not be doing so, because the type-specific area is
13705 already used to hold some other piece of info (eg: TYPE_CODE_FLT
13706 where the type-specific area is used to store the floatformat).
13707 But this is not a problem, because the gnat-specific information
13708 is actually not needed for these types. */
13709 if (need_gnat_info (cu)
13710 && TYPE_CODE (type) != TYPE_CODE_FUNC
13711 && TYPE_CODE (type) != TYPE_CODE_FLT
13712 && !HAVE_GNAT_AUX_INFO (type))
13713 INIT_GNAT_SPECIFIC (type);
13714
f792889a
DJ
13715 if (cu->type_hash == NULL)
13716 {
13717 gdb_assert (cu->per_cu != NULL);
13718 cu->per_cu->type_hash
13719 = htab_create_alloc_ex (cu->header.length / 24,
13720 offset_and_type_hash,
13721 offset_and_type_eq,
13722 NULL,
13723 &cu->objfile->objfile_obstack,
13724 hashtab_obstack_allocate,
13725 dummy_obstack_deallocate);
13726 cu->type_hash = cu->per_cu->type_hash;
13727 }
1c379e20
DJ
13728
13729 ofs.offset = die->offset;
13730 ofs.type = type;
13731 slot = (struct dwarf2_offset_and_type **)
f792889a 13732 htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
7e314c57
JK
13733 if (*slot)
13734 complaint (&symfile_complaints,
13735 _("A problem internal to GDB: DIE 0x%x has type already set"),
13736 die->offset);
1c379e20
DJ
13737 *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
13738 **slot = ofs;
f792889a 13739 return type;
1c379e20
DJ
13740}
13741
f792889a
DJ
13742/* Find the type for DIE in CU's type_hash, or return NULL if DIE does
13743 not have a saved type. */
1c379e20
DJ
13744
13745static struct type *
f792889a 13746get_die_type (struct die_info *die, struct dwarf2_cu *cu)
1c379e20
DJ
13747{
13748 struct dwarf2_offset_and_type *slot, ofs;
f792889a
DJ
13749 htab_t type_hash = cu->type_hash;
13750
13751 if (type_hash == NULL)
13752 return NULL;
1c379e20
DJ
13753
13754 ofs.offset = die->offset;
13755 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
13756 if (slot)
13757 return slot->type;
13758 else
13759 return NULL;
13760}
13761
10b3939b
DJ
13762/* Add a dependence relationship from CU to REF_PER_CU. */
13763
13764static void
13765dwarf2_add_dependence (struct dwarf2_cu *cu,
13766 struct dwarf2_per_cu_data *ref_per_cu)
13767{
13768 void **slot;
13769
13770 if (cu->dependencies == NULL)
13771 cu->dependencies
13772 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
13773 NULL, &cu->comp_unit_obstack,
13774 hashtab_obstack_allocate,
13775 dummy_obstack_deallocate);
13776
13777 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
13778 if (*slot == NULL)
13779 *slot = ref_per_cu;
13780}
1c379e20 13781
f504f079
DE
13782/* Subroutine of dwarf2_mark to pass to htab_traverse.
13783 Set the mark field in every compilation unit in the
ae038cb0
DJ
13784 cache that we must keep because we are keeping CU. */
13785
10b3939b
DJ
13786static int
13787dwarf2_mark_helper (void **slot, void *data)
13788{
13789 struct dwarf2_per_cu_data *per_cu;
13790
13791 per_cu = (struct dwarf2_per_cu_data *) *slot;
13792 if (per_cu->cu->mark)
13793 return 1;
13794 per_cu->cu->mark = 1;
13795
13796 if (per_cu->cu->dependencies != NULL)
13797 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
13798
13799 return 1;
13800}
13801
f504f079
DE
13802/* Set the mark field in CU and in every other compilation unit in the
13803 cache that we must keep because we are keeping CU. */
13804
ae038cb0
DJ
13805static void
13806dwarf2_mark (struct dwarf2_cu *cu)
13807{
13808 if (cu->mark)
13809 return;
13810 cu->mark = 1;
10b3939b
DJ
13811 if (cu->dependencies != NULL)
13812 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
13813}
13814
13815static void
13816dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
13817{
13818 while (per_cu)
13819 {
13820 per_cu->cu->mark = 0;
13821 per_cu = per_cu->cu->read_in_chain;
13822 }
72bf9492
DJ
13823}
13824
72bf9492
DJ
13825/* Trivial hash function for partial_die_info: the hash value of a DIE
13826 is its offset in .debug_info for this objfile. */
13827
13828static hashval_t
13829partial_die_hash (const void *item)
13830{
13831 const struct partial_die_info *part_die = item;
9a619af0 13832
72bf9492
DJ
13833 return part_die->offset;
13834}
13835
13836/* Trivial comparison function for partial_die_info structures: two DIEs
13837 are equal if they have the same offset. */
13838
13839static int
13840partial_die_eq (const void *item_lhs, const void *item_rhs)
13841{
13842 const struct partial_die_info *part_die_lhs = item_lhs;
13843 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 13844
72bf9492
DJ
13845 return part_die_lhs->offset == part_die_rhs->offset;
13846}
13847
ae038cb0
DJ
13848static struct cmd_list_element *set_dwarf2_cmdlist;
13849static struct cmd_list_element *show_dwarf2_cmdlist;
13850
13851static void
13852set_dwarf2_cmd (char *args, int from_tty)
13853{
13854 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
13855}
13856
13857static void
13858show_dwarf2_cmd (char *args, int from_tty)
6e70227d 13859{
ae038cb0
DJ
13860 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
13861}
13862
dce234bc
PP
13863/* If section described by INFO was mmapped, munmap it now. */
13864
13865static void
13866munmap_section_buffer (struct dwarf2_section_info *info)
13867{
13868 if (info->was_mmapped)
13869 {
13870#ifdef HAVE_MMAP
13871 intptr_t begin = (intptr_t) info->buffer;
13872 intptr_t map_begin = begin & ~(pagesize - 1);
13873 size_t map_length = info->size + begin - map_begin;
9a619af0 13874
dce234bc
PP
13875 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
13876#else
13877 /* Without HAVE_MMAP, we should never be here to begin with. */
13878 gdb_assert (0);
13879#endif
13880 }
13881}
13882
13883/* munmap debug sections for OBJFILE, if necessary. */
13884
13885static void
c1bd65d0 13886dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
13887{
13888 struct dwarf2_per_objfile *data = d;
9a619af0 13889
dce234bc
PP
13890 munmap_section_buffer (&data->info);
13891 munmap_section_buffer (&data->abbrev);
13892 munmap_section_buffer (&data->line);
13893 munmap_section_buffer (&data->str);
13894 munmap_section_buffer (&data->macinfo);
13895 munmap_section_buffer (&data->ranges);
13896 munmap_section_buffer (&data->loc);
13897 munmap_section_buffer (&data->frame);
13898 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
13899 munmap_section_buffer (&data->gdb_index);
13900}
13901
13902\f
13903
13904/* The contents of the hash table we create when building the string
13905 table. */
13906struct strtab_entry
13907{
13908 offset_type offset;
13909 const char *str;
13910};
13911
13912/* Hash function for a strtab_entry. */
13913static hashval_t
13914hash_strtab_entry (const void *e)
13915{
13916 const struct strtab_entry *entry = e;
13917 return mapped_index_string_hash (entry->str);
13918}
13919
13920/* Equality function for a strtab_entry. */
13921static int
13922eq_strtab_entry (const void *a, const void *b)
13923{
13924 const struct strtab_entry *ea = a;
13925 const struct strtab_entry *eb = b;
13926 return !strcmp (ea->str, eb->str);
13927}
13928
13929/* Create a strtab_entry hash table. */
13930static htab_t
13931create_strtab (void)
13932{
13933 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
13934 xfree, xcalloc, xfree);
13935}
13936
13937/* Add a string to the constant pool. Return the string's offset in
13938 host order. */
13939static offset_type
13940add_string (htab_t table, struct obstack *cpool, const char *str)
13941{
13942 void **slot;
13943 struct strtab_entry entry;
13944 struct strtab_entry *result;
13945
13946 entry.str = str;
13947 slot = htab_find_slot (table, &entry, INSERT);
13948 if (*slot)
13949 result = *slot;
13950 else
13951 {
13952 result = XNEW (struct strtab_entry);
13953 result->offset = obstack_object_size (cpool);
13954 result->str = str;
13955 obstack_grow_str0 (cpool, str);
13956 *slot = result;
13957 }
13958 return result->offset;
13959}
13960
13961/* An entry in the symbol table. */
13962struct symtab_index_entry
13963{
13964 /* The name of the symbol. */
13965 const char *name;
13966 /* The offset of the name in the constant pool. */
13967 offset_type index_offset;
13968 /* A sorted vector of the indices of all the CUs that hold an object
13969 of this name. */
13970 VEC (offset_type) *cu_indices;
13971};
13972
13973/* The symbol table. This is a power-of-2-sized hash table. */
13974struct mapped_symtab
13975{
13976 offset_type n_elements;
13977 offset_type size;
13978 struct symtab_index_entry **data;
13979};
13980
13981/* Hash function for a symtab_index_entry. */
13982static hashval_t
13983hash_symtab_entry (const void *e)
13984{
13985 const struct symtab_index_entry *entry = e;
13986 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
13987 sizeof (offset_type) * VEC_length (offset_type,
13988 entry->cu_indices),
13989 0);
13990}
13991
13992/* Equality function for a symtab_index_entry. */
13993static int
13994eq_symtab_entry (const void *a, const void *b)
13995{
13996 const struct symtab_index_entry *ea = a;
13997 const struct symtab_index_entry *eb = b;
13998 int len = VEC_length (offset_type, ea->cu_indices);
13999 if (len != VEC_length (offset_type, eb->cu_indices))
14000 return 0;
14001 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14002 VEC_address (offset_type, eb->cu_indices),
14003 sizeof (offset_type) * len);
14004}
14005
14006/* Destroy a symtab_index_entry. */
14007static void
14008delete_symtab_entry (void *p)
14009{
14010 struct symtab_index_entry *entry = p;
14011 VEC_free (offset_type, entry->cu_indices);
14012 xfree (entry);
14013}
14014
14015/* Create a hash table holding symtab_index_entry objects. */
14016static htab_t
14017create_index_table (void)
14018{
14019 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14020 delete_symtab_entry, xcalloc, xfree);
14021}
14022
14023/* Create a new mapped symtab object. */
14024static struct mapped_symtab *
14025create_mapped_symtab (void)
14026{
14027 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14028 symtab->n_elements = 0;
14029 symtab->size = 1024;
14030 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14031 return symtab;
14032}
14033
14034/* Destroy a mapped_symtab. */
14035static void
14036cleanup_mapped_symtab (void *p)
14037{
14038 struct mapped_symtab *symtab = p;
14039 /* The contents of the array are freed when the other hash table is
14040 destroyed. */
14041 xfree (symtab->data);
14042 xfree (symtab);
14043}
14044
14045/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14046 the slot. */
14047static struct symtab_index_entry **
14048find_slot (struct mapped_symtab *symtab, const char *name)
14049{
14050 offset_type index, step, hash = mapped_index_string_hash (name);
14051
14052 index = hash & (symtab->size - 1);
14053 step = ((hash * 17) & (symtab->size - 1)) | 1;
14054
14055 for (;;)
14056 {
14057 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14058 return &symtab->data[index];
14059 index = (index + step) & (symtab->size - 1);
14060 }
14061}
14062
14063/* Expand SYMTAB's hash table. */
14064static void
14065hash_expand (struct mapped_symtab *symtab)
14066{
14067 offset_type old_size = symtab->size;
14068 offset_type i;
14069 struct symtab_index_entry **old_entries = symtab->data;
14070
14071 symtab->size *= 2;
14072 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14073
14074 for (i = 0; i < old_size; ++i)
14075 {
14076 if (old_entries[i])
14077 {
14078 struct symtab_index_entry **slot = find_slot (symtab,
14079 old_entries[i]->name);
14080 *slot = old_entries[i];
14081 }
14082 }
14083
14084 xfree (old_entries);
14085}
14086
14087/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
14088 is the index of the CU in which the symbol appears. */
14089static void
14090add_index_entry (struct mapped_symtab *symtab, const char *name,
14091 offset_type cu_index)
14092{
14093 struct symtab_index_entry **slot;
14094
14095 ++symtab->n_elements;
14096 if (4 * symtab->n_elements / 3 >= symtab->size)
14097 hash_expand (symtab);
14098
14099 slot = find_slot (symtab, name);
14100 if (!*slot)
14101 {
14102 *slot = XNEW (struct symtab_index_entry);
14103 (*slot)->name = name;
14104 (*slot)->cu_indices = NULL;
14105 }
14106 /* Don't push an index twice. Due to how we add entries we only
14107 have to check the last one. */
14108 if (VEC_empty (offset_type, (*slot)->cu_indices)
14109 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14110 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14111}
14112
14113/* Add a vector of indices to the constant pool. */
14114static offset_type
14115add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14116 struct symtab_index_entry *entry)
14117{
14118 void **slot;
14119
14120 slot = htab_find_slot (index_table, entry, INSERT);
14121 if (!*slot)
14122 {
14123 offset_type len = VEC_length (offset_type, entry->cu_indices);
14124 offset_type val = MAYBE_SWAP (len);
14125 offset_type iter;
14126 int i;
14127
14128 *slot = entry;
14129 entry->index_offset = obstack_object_size (cpool);
14130
14131 obstack_grow (cpool, &val, sizeof (val));
14132 for (i = 0;
14133 VEC_iterate (offset_type, entry->cu_indices, i, iter);
14134 ++i)
14135 {
14136 val = MAYBE_SWAP (iter);
14137 obstack_grow (cpool, &val, sizeof (val));
14138 }
14139 }
14140 else
14141 {
14142 struct symtab_index_entry *old_entry = *slot;
14143 entry->index_offset = old_entry->index_offset;
14144 entry = old_entry;
14145 }
14146 return entry->index_offset;
14147}
14148
14149/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14150 constant pool entries going into the obstack CPOOL. */
14151static void
14152write_hash_table (struct mapped_symtab *symtab,
14153 struct obstack *output, struct obstack *cpool)
14154{
14155 offset_type i;
14156 htab_t index_table;
14157 htab_t str_table;
14158
14159 index_table = create_index_table ();
14160 str_table = create_strtab ();
14161 /* We add all the index vectors to the constant pool first, to
14162 ensure alignment is ok. */
14163 for (i = 0; i < symtab->size; ++i)
14164 {
14165 if (symtab->data[i])
14166 add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14167 }
14168
14169 /* Now write out the hash table. */
14170 for (i = 0; i < symtab->size; ++i)
14171 {
14172 offset_type str_off, vec_off;
14173
14174 if (symtab->data[i])
14175 {
14176 str_off = add_string (str_table, cpool, symtab->data[i]->name);
14177 vec_off = symtab->data[i]->index_offset;
14178 }
14179 else
14180 {
14181 /* While 0 is a valid constant pool index, it is not valid
14182 to have 0 for both offsets. */
14183 str_off = 0;
14184 vec_off = 0;
14185 }
14186
14187 str_off = MAYBE_SWAP (str_off);
14188 vec_off = MAYBE_SWAP (vec_off);
14189
14190 obstack_grow (output, &str_off, sizeof (str_off));
14191 obstack_grow (output, &vec_off, sizeof (vec_off));
14192 }
14193
14194 htab_delete (str_table);
14195 htab_delete (index_table);
14196}
14197
14198/* Write an address entry to ADDR_OBSTACK. The addresses are taken
14199 from PST; CU_INDEX is the index of the CU in the vector of all
14200 CUs. */
14201static void
14202add_address_entry (struct objfile *objfile,
14203 struct obstack *addr_obstack, struct partial_symtab *pst,
14204 unsigned int cu_index)
14205{
14206 offset_type offset;
14207 char addr[8];
14208 CORE_ADDR baseaddr;
14209
1fd400ff
TT
14210 /* Don't bother recording empty ranges. */
14211 if (pst->textlow == pst->texthigh)
14212 return;
14213
9291a0cd
TT
14214 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14215
14216 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
14217 obstack_grow (addr_obstack, addr, 8);
14218 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
14219 obstack_grow (addr_obstack, addr, 8);
14220 offset = MAYBE_SWAP (cu_index);
14221 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
14222}
14223
14224/* Add a list of partial symbols to SYMTAB. */
14225static void
14226write_psymbols (struct mapped_symtab *symtab,
14227 struct partial_symbol **psymp,
14228 int count,
14229 offset_type cu_index)
14230{
14231 for (; count-- > 0; ++psymp)
14232 {
14233 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
14234 error (_("Ada is not currently supported by the index"));
14235 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
14236 }
14237}
14238
14239/* Write the contents of an ("unfinished") obstack to FILE. Throw an
14240 exception if there is an error. */
14241static void
14242write_obstack (FILE *file, struct obstack *obstack)
14243{
14244 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
14245 file)
14246 != obstack_object_size (obstack))
14247 error (_("couldn't data write to file"));
14248}
14249
14250/* Unlink a file if the argument is not NULL. */
14251static void
14252unlink_if_set (void *p)
14253{
14254 char **filename = p;
14255 if (*filename)
14256 unlink (*filename);
14257}
14258
1fd400ff
TT
14259/* A helper struct used when iterating over debug_types. */
14260struct signatured_type_index_data
14261{
14262 struct objfile *objfile;
14263 struct mapped_symtab *symtab;
14264 struct obstack *types_list;
14265 int cu_index;
14266};
14267
14268/* A helper function that writes a single signatured_type to an
14269 obstack. */
14270static int
14271write_one_signatured_type (void **slot, void *d)
14272{
14273 struct signatured_type_index_data *info = d;
14274 struct signatured_type *entry = (struct signatured_type *) *slot;
14275 struct dwarf2_per_cu_data *cu = &entry->per_cu;
14276 struct partial_symtab *psymtab = cu->v.psymtab;
14277 gdb_byte val[8];
14278
14279 write_psymbols (info->symtab,
14280 info->objfile->global_psymbols.list + psymtab->globals_offset,
14281 psymtab->n_global_syms, info->cu_index);
14282 write_psymbols (info->symtab,
14283 info->objfile->static_psymbols.list + psymtab->statics_offset,
14284 psymtab->n_static_syms, info->cu_index);
14285
14286 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
14287 obstack_grow (info->types_list, val, 8);
14288 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
14289 obstack_grow (info->types_list, val, 8);
14290 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
14291 obstack_grow (info->types_list, val, 8);
14292
14293 ++info->cu_index;
14294
14295 return 1;
14296}
14297
9291a0cd
TT
14298/* Create an index file for OBJFILE in the directory DIR. */
14299static void
14300write_psymtabs_to_index (struct objfile *objfile, const char *dir)
14301{
14302 struct cleanup *cleanup;
14303 char *filename, *cleanup_filename;
1fd400ff
TT
14304 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
14305 struct obstack cu_list, types_cu_list;
9291a0cd
TT
14306 int i;
14307 FILE *out_file;
14308 struct mapped_symtab *symtab;
14309 offset_type val, size_of_contents, total_len;
14310 struct stat st;
14311 char buf[8];
14312
14313 if (!objfile->psymtabs)
14314 return;
14315 if (dwarf2_per_objfile->using_index)
14316 error (_("Cannot use an index to create the index"));
14317
14318 if (stat (objfile->name, &st) < 0)
14319 perror_with_name (_("Could not stat"));
14320
14321 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
14322 INDEX_SUFFIX, (char *) NULL);
14323 cleanup = make_cleanup (xfree, filename);
14324
14325 out_file = fopen (filename, "wb");
14326 if (!out_file)
14327 error (_("Can't open `%s' for writing"), filename);
14328
14329 cleanup_filename = filename;
14330 make_cleanup (unlink_if_set, &cleanup_filename);
14331
14332 symtab = create_mapped_symtab ();
14333 make_cleanup (cleanup_mapped_symtab, symtab);
14334
14335 obstack_init (&addr_obstack);
14336 make_cleanup_obstack_free (&addr_obstack);
14337
14338 obstack_init (&cu_list);
14339 make_cleanup_obstack_free (&cu_list);
14340
1fd400ff
TT
14341 obstack_init (&types_cu_list);
14342 make_cleanup_obstack_free (&types_cu_list);
14343
14344 /* The list is already sorted, so we don't need to do additional
14345 work here. Also, the debug_types entries do not appear in
14346 all_comp_units, but only in their own hash table. */
9291a0cd
TT
14347 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14348 {
14349 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14350 struct partial_symtab *psymtab = cu->v.psymtab;
14351 gdb_byte val[8];
14352
14353 write_psymbols (symtab,
14354 objfile->global_psymbols.list + psymtab->globals_offset,
14355 psymtab->n_global_syms, i);
14356 write_psymbols (symtab,
14357 objfile->static_psymbols.list + psymtab->statics_offset,
14358 psymtab->n_static_syms, i);
14359
14360 add_address_entry (objfile, &addr_obstack, psymtab, i);
14361
14362 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->offset);
14363 obstack_grow (&cu_list, val, 8);
14364 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->length);
14365 obstack_grow (&cu_list, val, 8);
14366 }
14367
1fd400ff
TT
14368 /* Write out the .debug_type entries, if any. */
14369 if (dwarf2_per_objfile->signatured_types)
14370 {
14371 struct signatured_type_index_data sig_data;
14372
14373 sig_data.objfile = objfile;
14374 sig_data.symtab = symtab;
14375 sig_data.types_list = &types_cu_list;
14376 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
14377 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
14378 write_one_signatured_type, &sig_data);
14379 }
14380
9291a0cd
TT
14381 obstack_init (&constant_pool);
14382 make_cleanup_obstack_free (&constant_pool);
14383 obstack_init (&symtab_obstack);
14384 make_cleanup_obstack_free (&symtab_obstack);
14385 write_hash_table (symtab, &symtab_obstack, &constant_pool);
14386
14387 obstack_init (&contents);
14388 make_cleanup_obstack_free (&contents);
1fd400ff 14389 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
14390 total_len = size_of_contents;
14391
14392 /* The version number. */
1fd400ff 14393 val = MAYBE_SWAP (2);
9291a0cd
TT
14394 obstack_grow (&contents, &val, sizeof (val));
14395
14396 /* The offset of the CU list from the start of the file. */
14397 val = MAYBE_SWAP (total_len);
14398 obstack_grow (&contents, &val, sizeof (val));
14399 total_len += obstack_object_size (&cu_list);
14400
1fd400ff
TT
14401 /* The offset of the types CU list from the start of the file. */
14402 val = MAYBE_SWAP (total_len);
14403 obstack_grow (&contents, &val, sizeof (val));
14404 total_len += obstack_object_size (&types_cu_list);
14405
9291a0cd
TT
14406 /* The offset of the address table from the start of the file. */
14407 val = MAYBE_SWAP (total_len);
14408 obstack_grow (&contents, &val, sizeof (val));
14409 total_len += obstack_object_size (&addr_obstack);
14410
14411 /* The offset of the symbol table from the start of the file. */
14412 val = MAYBE_SWAP (total_len);
14413 obstack_grow (&contents, &val, sizeof (val));
14414 total_len += obstack_object_size (&symtab_obstack);
14415
14416 /* The offset of the constant pool from the start of the file. */
14417 val = MAYBE_SWAP (total_len);
14418 obstack_grow (&contents, &val, sizeof (val));
14419 total_len += obstack_object_size (&constant_pool);
14420
14421 gdb_assert (obstack_object_size (&contents) == size_of_contents);
14422
14423 write_obstack (out_file, &contents);
14424 write_obstack (out_file, &cu_list);
1fd400ff 14425 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
14426 write_obstack (out_file, &addr_obstack);
14427 write_obstack (out_file, &symtab_obstack);
14428 write_obstack (out_file, &constant_pool);
14429
14430 fclose (out_file);
14431
14432 /* We want to keep the file, so we set cleanup_filename to NULL
14433 here. See unlink_if_set. */
14434 cleanup_filename = NULL;
14435
14436 do_cleanups (cleanup);
14437}
14438
14439/* The mapped index file format is designed to be directly mmap()able
14440 on any architecture. In most cases, a datum is represented using a
14441 little-endian 32-bit integer value, called an offset_type. Big
14442 endian machines must byte-swap the values before using them.
14443 Exceptions to this rule are noted. The data is laid out such that
14444 alignment is always respected.
14445
14446 A mapped index consists of several sections.
14447
14448 1. The file header. This is a sequence of values, of offset_type
14449 unless otherwise noted:
1fd400ff
TT
14450 [0] The version number. Currently 1 or 2. The differences are
14451 noted below. Version 1 did not account for .debug_types sections;
14452 the presence of a .debug_types section invalidates any version 1
14453 index that may exist.
9291a0cd 14454 [1] The offset, from the start of the file, of the CU list.
1fd400ff
TT
14455 [1.5] In version 2, the offset, from the start of the file, of the
14456 types CU list. This offset does not appear in version 1. Note
14457 that this can be empty, in which case this offset will be equal to
14458 the next offset.
9291a0cd
TT
14459 [2] The offset, from the start of the file, of the address section.
14460 [3] The offset, from the start of the file, of the symbol table.
14461 [4] The offset, from the start of the file, of the constant pool.
14462
14463 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
14464 little-endian values, sorted by the CU offset. The first element
14465 in each pair is the offset of a CU in the .debug_info section. The
14466 second element in each pair is the length of that CU. References
14467 to a CU elsewhere in the map are done using a CU index, which is
14468 just the 0-based index into this table. Note that if there are
14469 type CUs, then conceptually CUs and type CUs form a single list for
14470 the purposes of CU indices.
14471
14472 2.5 The types CU list. This does not appear in a version 1 index.
14473 This is a sequence of triplets of 64-bit little-endian values. In
14474 a triplet, the first value is the CU offset, the second value is
14475 the type offset in the CU, and the third value is the type
14476 signature. The types CU list is not sorted.
9291a0cd
TT
14477
14478 3. The address section. The address section consists of a sequence
14479 of address entries. Each address entry has three elements.
14480 [0] The low address. This is a 64-bit little-endian value.
14481 [1] The high address. This is a 64-bit little-endian value.
14482 [2] The CU index. This is an offset_type value.
14483
14484 4. The symbol table. This is a hash table. The size of the hash
14485 table is always a power of 2. The initial hash and the step are
14486 currently defined by the `find_slot' function.
14487
14488 Each slot in the hash table consists of a pair of offset_type
14489 values. The first value is the offset of the symbol's name in the
14490 constant pool. The second value is the offset of the CU vector in
14491 the constant pool.
14492
14493 If both values are 0, then this slot in the hash table is empty.
14494 This is ok because while 0 is a valid constant pool index, it
14495 cannot be a valid index for both a string and a CU vector.
14496
14497 A string in the constant pool is stored as a \0-terminated string,
14498 as you'd expect.
14499
14500 A CU vector in the constant pool is a sequence of offset_type
14501 values. The first value is the number of CU indices in the vector.
14502 Each subsequent value is the index of a CU in the CU list. This
14503 element in the hash table is used to indicate which CUs define the
14504 symbol.
14505
14506 5. The constant pool. This is simply a bunch of bytes. It is
14507 organized so that alignment is correct: CU vectors are stored
14508 first, followed by strings. */
14509static void
14510save_gdb_index_command (char *arg, int from_tty)
14511{
14512 struct objfile *objfile;
14513
14514 if (!arg || !*arg)
96d19272 14515 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
14516
14517 ALL_OBJFILES (objfile)
14518 {
14519 struct stat st;
14520
14521 /* If the objfile does not correspond to an actual file, skip it. */
14522 if (stat (objfile->name, &st) < 0)
14523 continue;
14524
14525 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14526 if (dwarf2_per_objfile)
14527 {
14528 volatile struct gdb_exception except;
14529
14530 TRY_CATCH (except, RETURN_MASK_ERROR)
14531 {
14532 write_psymtabs_to_index (objfile, arg);
14533 }
14534 if (except.reason < 0)
14535 exception_fprintf (gdb_stderr, except,
14536 _("Error while writing index for `%s': "),
14537 objfile->name);
14538 }
14539 }
dce234bc
PP
14540}
14541
9291a0cd
TT
14542\f
14543
9eae7c52
TT
14544int dwarf2_always_disassemble;
14545
14546static void
14547show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
14548 struct cmd_list_element *c, const char *value)
14549{
14550 fprintf_filtered (file, _("\
14551Whether to always disassemble DWARF expressions is %s.\n"),
14552 value);
14553}
14554
6502dd73
DJ
14555void _initialize_dwarf2_read (void);
14556
14557void
14558_initialize_dwarf2_read (void)
14559{
96d19272
JK
14560 struct cmd_list_element *c;
14561
dce234bc 14562 dwarf2_objfile_data_key
c1bd65d0 14563 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 14564
1bedd215
AC
14565 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
14566Set DWARF 2 specific variables.\n\
14567Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
14568 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
14569 0/*allow-unknown*/, &maintenance_set_cmdlist);
14570
1bedd215
AC
14571 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
14572Show DWARF 2 specific variables\n\
14573Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
14574 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
14575 0/*allow-unknown*/, &maintenance_show_cmdlist);
14576
14577 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
14578 &dwarf2_max_cache_age, _("\
14579Set the upper bound on the age of cached dwarf2 compilation units."), _("\
14580Show the upper bound on the age of cached dwarf2 compilation units."), _("\
14581A higher limit means that cached compilation units will be stored\n\
14582in memory longer, and more total memory will be used. Zero disables\n\
14583caching, which can slow down startup."),
2c5b56ce 14584 NULL,
920d2a44 14585 show_dwarf2_max_cache_age,
2c5b56ce 14586 &set_dwarf2_cmdlist,
ae038cb0 14587 &show_dwarf2_cmdlist);
d97bc12b 14588
9eae7c52
TT
14589 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
14590 &dwarf2_always_disassemble, _("\
14591Set whether `info address' always disassembles DWARF expressions."), _("\
14592Show whether `info address' always disassembles DWARF expressions."), _("\
14593When enabled, DWARF expressions are always printed in an assembly-like\n\
14594syntax. When disabled, expressions will be printed in a more\n\
14595conversational style, when possible."),
14596 NULL,
14597 show_dwarf2_always_disassemble,
14598 &set_dwarf2_cmdlist,
14599 &show_dwarf2_cmdlist);
14600
d97bc12b
DE
14601 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
14602Set debugging of the dwarf2 DIE reader."), _("\
14603Show debugging of the dwarf2 DIE reader."), _("\
14604When enabled (non-zero), DIEs are dumped after they are read in.\n\
14605The value is the maximum depth to print."),
14606 NULL,
14607 NULL,
14608 &setdebuglist, &showdebuglist);
9291a0cd 14609
96d19272
JK
14610 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
14611 _("Save a .gdb-index file"),
14612 &save_cmdlist);
14613 set_cmd_completer (c, filename_completer);
6502dd73 14614}