*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
6aba47ca 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4c38e0a4 4 2004, 2005, 2006, 2007, 2008, 2009, 2010
0fb0cc75 5 Free Software Foundation, Inc.
c906108c
SS
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 12 support.
c906108c 13
c5aa993b 14 This file is part of GDB.
c906108c 15
c5aa993b
JM
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
a9762ec7
JB
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
c906108c 20
a9762ec7
JB
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
c906108c 25
c5aa993b 26 You should have received a copy of the GNU General Public License
a9762ec7 27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
28
29#include "defs.h"
30#include "bfd.h"
c906108c
SS
31#include "symtab.h"
32#include "gdbtypes.h"
c906108c 33#include "objfiles.h"
fa8f86ff 34#include "dwarf2.h"
c906108c
SS
35#include "buildsym.h"
36#include "demangle.h"
37#include "expression.h"
d5166ae1 38#include "filenames.h" /* for DOSish file names */
2e276125 39#include "macrotab.h"
c906108c
SS
40#include "language.h"
41#include "complaints.h"
357e46e7 42#include "bcache.h"
4c2df51b
DJ
43#include "dwarf2expr.h"
44#include "dwarf2loc.h"
9219021c 45#include "cp-support.h"
72bf9492 46#include "hashtab.h"
ae038cb0
DJ
47#include "command.h"
48#include "gdbcmd.h"
edb3359d 49#include "block.h"
ff013f42 50#include "addrmap.h"
94af9270
KS
51#include "typeprint.h"
52#include "jv-lang.h"
ccefe4c4 53#include "psympriv.h"
9291a0cd
TT
54#include "exceptions.h"
55#include "gdb_stat.h"
96d19272 56#include "completer.h"
34eaf542 57#include "vec.h"
98bfdba5
PA
58#include "c-lang.h"
59#include "valprint.h"
4c2df51b 60
c906108c
SS
61#include <fcntl.h>
62#include "gdb_string.h"
4bdf3d34 63#include "gdb_assert.h"
c906108c 64#include <sys/types.h>
233a11ab
CS
65#ifdef HAVE_ZLIB_H
66#include <zlib.h>
67#endif
dce234bc
PP
68#ifdef HAVE_MMAP
69#include <sys/mman.h>
85d9bd0e
TT
70#ifndef MAP_FAILED
71#define MAP_FAILED ((void *) -1)
72#endif
dce234bc 73#endif
d8151005 74
34eaf542
TT
75typedef struct symbol *symbolp;
76DEF_VEC_P (symbolp);
77
107d2387 78#if 0
357e46e7 79/* .debug_info header for a compilation unit
c906108c
SS
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82typedef struct comp_unit_header
83 {
84 unsigned int length; /* length of the .debug_info
85 contribution */
86 unsigned short version; /* version number -- 2 for DWARF
87 version 2 */
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
90 }
91_COMP_UNIT_HEADER;
92#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 93#endif
c906108c 94
c906108c
SS
95/* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98typedef struct statement_prologue
99 {
100 unsigned int total_length; /* byte length of the statement
101 information */
102 unsigned short version; /* version number -- 2 for DWARF
103 version 2 */
104 unsigned int prologue_length; /* # bytes between prologue &
105 stmt program */
106 unsigned char minimum_instruction_length; /* byte size of
107 smallest instr */
108 unsigned char default_is_stmt; /* initial value of is_stmt
109 register */
110 char line_base;
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
113 opcode */
114 unsigned char *standard_opcode_lengths;
115 }
116_STATEMENT_PROLOGUE;
117
d97bc12b
DE
118/* When non-zero, dump DIEs after they are read in. */
119static int dwarf2_die_debug = 0;
120
dce234bc
PP
121static int pagesize;
122
df8a16a1
DJ
123/* When set, the file that we're processing is known to have debugging
124 info for C++ namespaces. GCC 3.3.x did not produce this information,
125 but later versions do. */
126
127static int processing_has_namespace_info;
128
6502dd73
DJ
129static const struct objfile_data *dwarf2_objfile_data_key;
130
dce234bc
PP
131struct dwarf2_section_info
132{
133 asection *asection;
134 gdb_byte *buffer;
135 bfd_size_type size;
136 int was_mmapped;
be391dca
TT
137 /* True if we have tried to read this section. */
138 int readin;
dce234bc
PP
139};
140
9291a0cd
TT
141/* All offsets in the index are of this type. It must be
142 architecture-independent. */
143typedef uint32_t offset_type;
144
145DEF_VEC_I (offset_type);
146
147/* A description of the mapped index. The file format is described in
148 a comment by the code that writes the index. */
149struct mapped_index
150{
151 /* The total length of the buffer. */
152 off_t total_size;
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
157 /* The hash table. */
158 const offset_type *index_table;
159 /* Size in slots, each slot is 2 offset_types. */
160 offset_type index_table_slots;
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163};
164
6502dd73
DJ
165struct dwarf2_per_objfile
166{
dce234bc
PP
167 struct dwarf2_section_info info;
168 struct dwarf2_section_info abbrev;
169 struct dwarf2_section_info line;
dce234bc
PP
170 struct dwarf2_section_info loc;
171 struct dwarf2_section_info macinfo;
172 struct dwarf2_section_info str;
173 struct dwarf2_section_info ranges;
348e048f 174 struct dwarf2_section_info types;
dce234bc
PP
175 struct dwarf2_section_info frame;
176 struct dwarf2_section_info eh_frame;
9291a0cd 177 struct dwarf2_section_info gdb_index;
ae038cb0 178
be391dca
TT
179 /* Back link. */
180 struct objfile *objfile;
181
10b3939b
DJ
182 /* A list of all the compilation units. This is used to locate
183 the target compilation unit of a particular reference. */
ae038cb0
DJ
184 struct dwarf2_per_cu_data **all_comp_units;
185
186 /* The number of compilation units in ALL_COMP_UNITS. */
187 int n_comp_units;
188
1fd400ff
TT
189 /* The number of .debug_types-related CUs. */
190 int n_type_comp_units;
191
192 /* The .debug_types-related CUs. */
193 struct dwarf2_per_cu_data **type_comp_units;
194
ae038cb0
DJ
195 /* A chain of compilation units that are currently read in, so that
196 they can be freed later. */
197 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 198
348e048f
DE
199 /* A table mapping .debug_types signatures to its signatured_type entry.
200 This is NULL if the .debug_types section hasn't been read in yet. */
201 htab_t signatured_types;
202
72dca2f5
FR
203 /* A flag indicating wether this objfile has a section loaded at a
204 VMA of 0. */
205 int has_section_at_zero;
9291a0cd
TT
206
207 /* True if we are using the mapped index. */
208 unsigned char using_index;
209
210 /* The mapped index. */
211 struct mapped_index *index_table;
98bfdba5
PA
212
213 /* Set during partial symbol reading, to prevent queueing of full
214 symbols. */
215 int reading_partial_symbols;
6502dd73
DJ
216};
217
218static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
219
220/* names of the debugging sections */
221
233a11ab
CS
222/* Note that if the debugging section has been compressed, it might
223 have a name like .zdebug_info. */
224
225#define INFO_SECTION "debug_info"
226#define ABBREV_SECTION "debug_abbrev"
227#define LINE_SECTION "debug_line"
233a11ab
CS
228#define LOC_SECTION "debug_loc"
229#define MACINFO_SECTION "debug_macinfo"
230#define STR_SECTION "debug_str"
231#define RANGES_SECTION "debug_ranges"
348e048f 232#define TYPES_SECTION "debug_types"
233a11ab
CS
233#define FRAME_SECTION "debug_frame"
234#define EH_FRAME_SECTION "eh_frame"
9291a0cd 235#define GDB_INDEX_SECTION "gdb_index"
c906108c
SS
236
237/* local data types */
238
57349743
JB
239/* We hold several abbreviation tables in memory at the same time. */
240#ifndef ABBREV_HASH_SIZE
241#define ABBREV_HASH_SIZE 121
242#endif
243
107d2387
AC
244/* The data in a compilation unit header, after target2host
245 translation, looks like this. */
c906108c 246struct comp_unit_head
a738430d 247{
c764a876 248 unsigned int length;
a738430d 249 short version;
a738430d
MK
250 unsigned char addr_size;
251 unsigned char signed_addr_p;
9cbfa09e 252 unsigned int abbrev_offset;
57349743 253
a738430d
MK
254 /* Size of file offsets; either 4 or 8. */
255 unsigned int offset_size;
57349743 256
a738430d
MK
257 /* Size of the length field; either 4 or 12. */
258 unsigned int initial_length_size;
57349743 259
a738430d
MK
260 /* Offset to the first byte of this compilation unit header in the
261 .debug_info section, for resolving relative reference dies. */
262 unsigned int offset;
57349743 263
d00adf39
DE
264 /* Offset to first die in this cu from the start of the cu.
265 This will be the first byte following the compilation unit header. */
266 unsigned int first_die_offset;
a738430d 267};
c906108c 268
3da10d80
KS
269/* Type used for delaying computation of method physnames.
270 See comments for compute_delayed_physnames. */
271struct delayed_method_info
272{
273 /* The type to which the method is attached, i.e., its parent class. */
274 struct type *type;
275
276 /* The index of the method in the type's function fieldlists. */
277 int fnfield_index;
278
279 /* The index of the method in the fieldlist. */
280 int index;
281
282 /* The name of the DIE. */
283 const char *name;
284
285 /* The DIE associated with this method. */
286 struct die_info *die;
287};
288
289typedef struct delayed_method_info delayed_method_info;
290DEF_VEC_O (delayed_method_info);
291
e7c27a73
DJ
292/* Internal state when decoding a particular compilation unit. */
293struct dwarf2_cu
294{
295 /* The objfile containing this compilation unit. */
296 struct objfile *objfile;
297
d00adf39 298 /* The header of the compilation unit. */
e7c27a73 299 struct comp_unit_head header;
e142c38c 300
d00adf39
DE
301 /* Base address of this compilation unit. */
302 CORE_ADDR base_address;
303
304 /* Non-zero if base_address has been set. */
305 int base_known;
306
e142c38c
DJ
307 struct function_range *first_fn, *last_fn, *cached_fn;
308
309 /* The language we are debugging. */
310 enum language language;
311 const struct language_defn *language_defn;
312
b0f35d58
DL
313 const char *producer;
314
e142c38c
DJ
315 /* The generic symbol table building routines have separate lists for
316 file scope symbols and all all other scopes (local scopes). So
317 we need to select the right one to pass to add_symbol_to_list().
318 We do it by keeping a pointer to the correct list in list_in_scope.
319
320 FIXME: The original dwarf code just treated the file scope as the
321 first local scope, and all other local scopes as nested local
322 scopes, and worked fine. Check to see if we really need to
323 distinguish these in buildsym.c. */
324 struct pending **list_in_scope;
325
f3dd6933
DJ
326 /* DWARF abbreviation table associated with this compilation unit. */
327 struct abbrev_info **dwarf2_abbrevs;
328
329 /* Storage for the abbrev table. */
330 struct obstack abbrev_obstack;
72bf9492
DJ
331
332 /* Hash table holding all the loaded partial DIEs. */
333 htab_t partial_dies;
334
335 /* Storage for things with the same lifetime as this read-in compilation
336 unit, including partial DIEs. */
337 struct obstack comp_unit_obstack;
338
ae038cb0
DJ
339 /* When multiple dwarf2_cu structures are living in memory, this field
340 chains them all together, so that they can be released efficiently.
341 We will probably also want a generation counter so that most-recently-used
342 compilation units are cached... */
343 struct dwarf2_per_cu_data *read_in_chain;
344
345 /* Backchain to our per_cu entry if the tree has been built. */
346 struct dwarf2_per_cu_data *per_cu;
347
f792889a
DJ
348 /* Pointer to the die -> type map. Although it is stored
349 permanently in per_cu, we copy it here to avoid double
350 indirection. */
351 htab_t type_hash;
352
ae038cb0
DJ
353 /* How many compilation units ago was this CU last referenced? */
354 int last_used;
355
10b3939b 356 /* A hash table of die offsets for following references. */
51545339 357 htab_t die_hash;
10b3939b
DJ
358
359 /* Full DIEs if read in. */
360 struct die_info *dies;
361
362 /* A set of pointers to dwarf2_per_cu_data objects for compilation
363 units referenced by this one. Only set during full symbol processing;
364 partial symbol tables do not have dependencies. */
365 htab_t dependencies;
366
cb1df416
DJ
367 /* Header data from the line table, during full symbol processing. */
368 struct line_header *line_header;
369
3da10d80
KS
370 /* A list of methods which need to have physnames computed
371 after all type information has been read. */
372 VEC (delayed_method_info) *method_list;
373
ae038cb0
DJ
374 /* Mark used when releasing cached dies. */
375 unsigned int mark : 1;
376
377 /* This flag will be set if this compilation unit might include
378 inter-compilation-unit references. */
379 unsigned int has_form_ref_addr : 1;
380
72bf9492
DJ
381 /* This flag will be set if this compilation unit includes any
382 DW_TAG_namespace DIEs. If we know that there are explicit
383 DIEs for namespaces, we don't need to try to infer them
384 from mangled names. */
385 unsigned int has_namespace_info : 1;
e7c27a73
DJ
386};
387
9291a0cd
TT
388/* When using the index (and thus not using psymtabs), each CU has an
389 object of this type. This is used to hold information needed by
390 the various "quick" methods. */
391struct dwarf2_per_cu_quick_data
392{
393 /* The line table. This can be NULL if there was no line table. */
394 struct line_header *lines;
395
396 /* The file names from the line table. */
397 const char **file_names;
398 /* The file names from the line table after being run through
399 gdb_realpath. */
400 const char **full_names;
401
402 /* The corresponding symbol table. This is NULL if symbols for this
403 CU have not yet been read. */
404 struct symtab *symtab;
405
406 /* A temporary mark bit used when iterating over all CUs in
407 expand_symtabs_matching. */
408 unsigned int mark : 1;
409
410 /* True if we've tried to read the line table. */
411 unsigned int read_lines : 1;
412};
413
10b3939b
DJ
414/* Persistent data held for a compilation unit, even when not
415 processing it. We put a pointer to this structure in the
416 read_symtab_private field of the psymtab. If we encounter
417 inter-compilation-unit references, we also maintain a sorted
418 list of all compilation units. */
419
ae038cb0
DJ
420struct dwarf2_per_cu_data
421{
348e048f 422 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 423 bytes should suffice to store the length of any compilation unit
45452591
DE
424 - if it doesn't, GDB will fall over anyway.
425 NOTE: Unlike comp_unit_head.length, this length includes
426 initial_length_size. */
c764a876 427 unsigned int offset;
348e048f 428 unsigned int length : 29;
ae038cb0
DJ
429
430 /* Flag indicating this compilation unit will be read in before
431 any of the current compilation units are processed. */
c764a876 432 unsigned int queued : 1;
ae038cb0 433
5afb4e99
DJ
434 /* This flag will be set if we need to load absolutely all DIEs
435 for this compilation unit, instead of just the ones we think
436 are interesting. It gets set if we look for a DIE in the
437 hash table and don't find it. */
438 unsigned int load_all_dies : 1;
439
348e048f
DE
440 /* Non-zero if this CU is from .debug_types.
441 Otherwise it's from .debug_info. */
442 unsigned int from_debug_types : 1;
443
17ea53c3
JK
444 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
445 of the CU cache it gets reset to NULL again. */
ae038cb0 446 struct dwarf2_cu *cu;
1c379e20
DJ
447
448 /* If full symbols for this CU have been read in, then this field
449 holds a map of DIE offsets to types. It isn't always possible
450 to reconstruct this information later, so we have to preserve
451 it. */
1c379e20 452 htab_t type_hash;
10b3939b 453
9291a0cd
TT
454 /* The corresponding objfile. */
455 struct objfile *objfile;
456
457 /* When using partial symbol tables, the 'psymtab' field is active.
458 Otherwise the 'quick' field is active. */
459 union
460 {
461 /* The partial symbol table associated with this compilation unit,
462 or NULL for partial units (which do not have an associated
463 symtab). */
464 struct partial_symtab *psymtab;
465
466 /* Data needed by the "quick" functions. */
467 struct dwarf2_per_cu_quick_data *quick;
468 } v;
ae038cb0
DJ
469};
470
348e048f
DE
471/* Entry in the signatured_types hash table. */
472
473struct signatured_type
474{
475 ULONGEST signature;
476
477 /* Offset in .debug_types of the TU (type_unit) for this type. */
478 unsigned int offset;
479
480 /* Offset in .debug_types of the type defined by this TU. */
481 unsigned int type_offset;
482
483 /* The CU(/TU) of this type. */
484 struct dwarf2_per_cu_data per_cu;
485};
486
93311388
DE
487/* Struct used to pass misc. parameters to read_die_and_children, et. al.
488 which are used for both .debug_info and .debug_types dies.
489 All parameters here are unchanging for the life of the call.
490 This struct exists to abstract away the constant parameters of
491 die reading. */
492
493struct die_reader_specs
494{
495 /* The bfd of this objfile. */
496 bfd* abfd;
497
498 /* The CU of the DIE we are parsing. */
499 struct dwarf2_cu *cu;
500
501 /* Pointer to start of section buffer.
502 This is either the start of .debug_info or .debug_types. */
503 const gdb_byte *buffer;
504};
505
debd256d
JB
506/* The line number information for a compilation unit (found in the
507 .debug_line section) begins with a "statement program header",
508 which contains the following information. */
509struct line_header
510{
511 unsigned int total_length;
512 unsigned short version;
513 unsigned int header_length;
514 unsigned char minimum_instruction_length;
2dc7f7b3 515 unsigned char maximum_ops_per_instruction;
debd256d
JB
516 unsigned char default_is_stmt;
517 int line_base;
518 unsigned char line_range;
519 unsigned char opcode_base;
520
521 /* standard_opcode_lengths[i] is the number of operands for the
522 standard opcode whose value is i. This means that
523 standard_opcode_lengths[0] is unused, and the last meaningful
524 element is standard_opcode_lengths[opcode_base - 1]. */
525 unsigned char *standard_opcode_lengths;
526
527 /* The include_directories table. NOTE! These strings are not
528 allocated with xmalloc; instead, they are pointers into
529 debug_line_buffer. If you try to free them, `free' will get
530 indigestion. */
531 unsigned int num_include_dirs, include_dirs_size;
532 char **include_dirs;
533
534 /* The file_names table. NOTE! These strings are not allocated
535 with xmalloc; instead, they are pointers into debug_line_buffer.
536 Don't try to free them directly. */
537 unsigned int num_file_names, file_names_size;
538 struct file_entry
c906108c 539 {
debd256d
JB
540 char *name;
541 unsigned int dir_index;
542 unsigned int mod_time;
543 unsigned int length;
aaa75496 544 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 545 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
546 } *file_names;
547
548 /* The start and end of the statement program following this
6502dd73 549 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 550 gdb_byte *statement_program_start, *statement_program_end;
debd256d 551};
c906108c
SS
552
553/* When we construct a partial symbol table entry we only
554 need this much information. */
555struct partial_die_info
556 {
72bf9492 557 /* Offset of this DIE. */
c906108c 558 unsigned int offset;
72bf9492
DJ
559
560 /* DWARF-2 tag for this DIE. */
561 ENUM_BITFIELD(dwarf_tag) tag : 16;
562
72bf9492
DJ
563 /* Assorted flags describing the data found in this DIE. */
564 unsigned int has_children : 1;
565 unsigned int is_external : 1;
566 unsigned int is_declaration : 1;
567 unsigned int has_type : 1;
568 unsigned int has_specification : 1;
569 unsigned int has_pc_info : 1;
570
571 /* Flag set if the SCOPE field of this structure has been
572 computed. */
573 unsigned int scope_set : 1;
574
fa4028e9
JB
575 /* Flag set if the DIE has a byte_size attribute. */
576 unsigned int has_byte_size : 1;
577
98bfdba5
PA
578 /* Flag set if any of the DIE's children are template arguments. */
579 unsigned int has_template_arguments : 1;
580
72bf9492 581 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 582 sometimes a default name for unnamed DIEs. */
c906108c 583 char *name;
72bf9492
DJ
584
585 /* The scope to prepend to our children. This is generally
586 allocated on the comp_unit_obstack, so will disappear
587 when this compilation unit leaves the cache. */
588 char *scope;
589
590 /* The location description associated with this DIE, if any. */
591 struct dwarf_block *locdesc;
592
593 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
594 CORE_ADDR lowpc;
595 CORE_ADDR highpc;
72bf9492 596
93311388 597 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 598 DW_AT_sibling, if any. */
fe1b8b76 599 gdb_byte *sibling;
72bf9492
DJ
600
601 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
602 DW_AT_specification (or DW_AT_abstract_origin or
603 DW_AT_extension). */
604 unsigned int spec_offset;
605
606 /* Pointers to this DIE's parent, first child, and next sibling,
607 if any. */
608 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
609 };
610
611/* This data structure holds the information of an abbrev. */
612struct abbrev_info
613 {
614 unsigned int number; /* number identifying abbrev */
615 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
616 unsigned short has_children; /* boolean */
617 unsigned short num_attrs; /* number of attributes */
c906108c
SS
618 struct attr_abbrev *attrs; /* an array of attribute descriptions */
619 struct abbrev_info *next; /* next in chain */
620 };
621
622struct attr_abbrev
623 {
9d25dd43
DE
624 ENUM_BITFIELD(dwarf_attribute) name : 16;
625 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
626 };
627
b60c80d6
DJ
628/* Attributes have a name and a value */
629struct attribute
630 {
9d25dd43 631 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
632 ENUM_BITFIELD(dwarf_form) form : 15;
633
634 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
635 field should be in u.str (existing only for DW_STRING) but it is kept
636 here for better struct attribute alignment. */
637 unsigned int string_is_canonical : 1;
638
b60c80d6
DJ
639 union
640 {
641 char *str;
642 struct dwarf_block *blk;
43bbcdc2
PH
643 ULONGEST unsnd;
644 LONGEST snd;
b60c80d6 645 CORE_ADDR addr;
348e048f 646 struct signatured_type *signatured_type;
b60c80d6
DJ
647 }
648 u;
649 };
650
c906108c
SS
651/* This data structure holds a complete die structure. */
652struct die_info
653 {
76815b17
DE
654 /* DWARF-2 tag for this DIE. */
655 ENUM_BITFIELD(dwarf_tag) tag : 16;
656
657 /* Number of attributes */
98bfdba5
PA
658 unsigned char num_attrs;
659
660 /* True if we're presently building the full type name for the
661 type derived from this DIE. */
662 unsigned char building_fullname : 1;
76815b17
DE
663
664 /* Abbrev number */
665 unsigned int abbrev;
666
93311388 667 /* Offset in .debug_info or .debug_types section. */
76815b17 668 unsigned int offset;
78ba4af6
JB
669
670 /* The dies in a compilation unit form an n-ary tree. PARENT
671 points to this die's parent; CHILD points to the first child of
672 this node; and all the children of a given node are chained
673 together via their SIBLING fields, terminated by a die whose
674 tag is zero. */
639d11d3
DC
675 struct die_info *child; /* Its first child, if any. */
676 struct die_info *sibling; /* Its next sibling, if any. */
677 struct die_info *parent; /* Its parent, if any. */
c906108c 678
b60c80d6
DJ
679 /* An array of attributes, with NUM_ATTRS elements. There may be
680 zero, but it's not common and zero-sized arrays are not
681 sufficiently portable C. */
682 struct attribute attrs[1];
c906108c
SS
683 };
684
5fb290d7
DJ
685struct function_range
686{
687 const char *name;
688 CORE_ADDR lowpc, highpc;
689 int seen_line;
690 struct function_range *next;
691};
692
c906108c
SS
693/* Get at parts of an attribute structure */
694
695#define DW_STRING(attr) ((attr)->u.str)
8285870a 696#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
697#define DW_UNSND(attr) ((attr)->u.unsnd)
698#define DW_BLOCK(attr) ((attr)->u.blk)
699#define DW_SND(attr) ((attr)->u.snd)
700#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 701#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c
SS
702
703/* Blocks are a bunch of untyped bytes. */
704struct dwarf_block
705 {
706 unsigned int size;
fe1b8b76 707 gdb_byte *data;
c906108c
SS
708 };
709
c906108c
SS
710#ifndef ATTR_ALLOC_CHUNK
711#define ATTR_ALLOC_CHUNK 4
712#endif
713
c906108c
SS
714/* Allocate fields for structs, unions and enums in this size. */
715#ifndef DW_FIELD_ALLOC_CHUNK
716#define DW_FIELD_ALLOC_CHUNK 4
717#endif
718
c906108c
SS
719/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
720 but this would require a corresponding change in unpack_field_as_long
721 and friends. */
722static int bits_per_byte = 8;
723
724/* The routines that read and process dies for a C struct or C++ class
725 pass lists of data member fields and lists of member function fields
726 in an instance of a field_info structure, as defined below. */
727struct field_info
c5aa993b
JM
728 {
729 /* List of data member and baseclasses fields. */
730 struct nextfield
731 {
732 struct nextfield *next;
733 int accessibility;
734 int virtuality;
735 struct field field;
736 }
7d0ccb61 737 *fields, *baseclasses;
c906108c 738
7d0ccb61 739 /* Number of fields (including baseclasses). */
c5aa993b 740 int nfields;
c906108c 741
c5aa993b
JM
742 /* Number of baseclasses. */
743 int nbaseclasses;
c906108c 744
c5aa993b
JM
745 /* Set if the accesibility of one of the fields is not public. */
746 int non_public_fields;
c906108c 747
c5aa993b
JM
748 /* Member function fields array, entries are allocated in the order they
749 are encountered in the object file. */
750 struct nextfnfield
751 {
752 struct nextfnfield *next;
753 struct fn_field fnfield;
754 }
755 *fnfields;
c906108c 756
c5aa993b
JM
757 /* Member function fieldlist array, contains name of possibly overloaded
758 member function, number of overloaded member functions and a pointer
759 to the head of the member function field chain. */
760 struct fnfieldlist
761 {
762 char *name;
763 int length;
764 struct nextfnfield *head;
765 }
766 *fnfieldlists;
c906108c 767
c5aa993b
JM
768 /* Number of entries in the fnfieldlists array. */
769 int nfnfields;
98751a41
JK
770
771 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
772 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
773 struct typedef_field_list
774 {
775 struct typedef_field field;
776 struct typedef_field_list *next;
777 }
778 *typedef_field_list;
779 unsigned typedef_field_list_count;
c5aa993b 780 };
c906108c 781
10b3939b
DJ
782/* One item on the queue of compilation units to read in full symbols
783 for. */
784struct dwarf2_queue_item
785{
786 struct dwarf2_per_cu_data *per_cu;
787 struct dwarf2_queue_item *next;
788};
789
790/* The current queue. */
791static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
792
ae038cb0
DJ
793/* Loaded secondary compilation units are kept in memory until they
794 have not been referenced for the processing of this many
795 compilation units. Set this to zero to disable caching. Cache
796 sizes of up to at least twenty will improve startup time for
797 typical inter-CU-reference binaries, at an obvious memory cost. */
798static int dwarf2_max_cache_age = 5;
920d2a44
AC
799static void
800show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
801 struct cmd_list_element *c, const char *value)
802{
803 fprintf_filtered (file, _("\
804The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
805 value);
806}
807
ae038cb0 808
c906108c
SS
809/* Various complaints about symbol reading that don't abort the process */
810
4d3c2250
KB
811static void
812dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 813{
4d3c2250 814 complaint (&symfile_complaints,
e2e0b3e5 815 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
816}
817
25e43795
DJ
818static void
819dwarf2_debug_line_missing_file_complaint (void)
820{
821 complaint (&symfile_complaints,
822 _(".debug_line section has line data without a file"));
823}
824
59205f5a
JB
825static void
826dwarf2_debug_line_missing_end_sequence_complaint (void)
827{
828 complaint (&symfile_complaints,
829 _(".debug_line section has line program sequence without an end"));
830}
831
4d3c2250
KB
832static void
833dwarf2_complex_location_expr_complaint (void)
2e276125 834{
e2e0b3e5 835 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
836}
837
4d3c2250
KB
838static void
839dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
840 int arg3)
2e276125 841{
4d3c2250 842 complaint (&symfile_complaints,
e2e0b3e5 843 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
4d3c2250
KB
844 arg2, arg3);
845}
846
847static void
848dwarf2_macros_too_long_complaint (void)
2e276125 849{
4d3c2250 850 complaint (&symfile_complaints,
e2e0b3e5 851 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
852}
853
854static void
855dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 856{
4d3c2250 857 complaint (&symfile_complaints,
e2e0b3e5 858 _("macro debug info contains a malformed macro definition:\n`%s'"),
4d3c2250
KB
859 arg1);
860}
861
862static void
863dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 864{
4d3c2250 865 complaint (&symfile_complaints,
e2e0b3e5 866 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
4d3c2250 867}
c906108c 868
c906108c
SS
869/* local function prototypes */
870
4efb68b1 871static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 872
aaa75496
JB
873static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
874 struct objfile *);
875
876static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
d85a05f0 877 struct die_info *,
aaa75496
JB
878 struct partial_symtab *);
879
c67a9c90 880static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 881
72bf9492
DJ
882static void scan_partial_symbols (struct partial_die_info *,
883 CORE_ADDR *, CORE_ADDR *,
5734ee8b 884 int, struct dwarf2_cu *);
c906108c 885
72bf9492
DJ
886static void add_partial_symbol (struct partial_die_info *,
887 struct dwarf2_cu *);
63d06c5c 888
72bf9492
DJ
889static void add_partial_namespace (struct partial_die_info *pdi,
890 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 891 int need_pc, struct dwarf2_cu *cu);
63d06c5c 892
5d7cb8df
JK
893static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
894 CORE_ADDR *highpc, int need_pc,
895 struct dwarf2_cu *cu);
896
72bf9492
DJ
897static void add_partial_enumeration (struct partial_die_info *enum_pdi,
898 struct dwarf2_cu *cu);
91c24f0a 899
bc30ff58
JB
900static void add_partial_subprogram (struct partial_die_info *pdi,
901 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 902 int need_pc, struct dwarf2_cu *cu);
bc30ff58 903
fe1b8b76 904static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
905 gdb_byte *buffer, gdb_byte *info_ptr,
906 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 907
a14ed312 908static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 909
a14ed312 910static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 911
e7c27a73 912static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 913
f3dd6933 914static void dwarf2_free_abbrev_table (void *);
c906108c 915
fe1b8b76 916static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 917 struct dwarf2_cu *);
72bf9492 918
57349743 919static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 920 struct dwarf2_cu *);
c906108c 921
93311388
DE
922static struct partial_die_info *load_partial_dies (bfd *,
923 gdb_byte *, gdb_byte *,
924 int, struct dwarf2_cu *);
72bf9492 925
fe1b8b76 926static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
927 struct abbrev_info *abbrev,
928 unsigned int, bfd *,
929 gdb_byte *, gdb_byte *,
930 struct dwarf2_cu *);
c906108c 931
c764a876 932static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 933 struct dwarf2_cu *);
72bf9492
DJ
934
935static void fixup_partial_die (struct partial_die_info *,
936 struct dwarf2_cu *);
937
fe1b8b76
JB
938static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
939 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 940
fe1b8b76
JB
941static gdb_byte *read_attribute_value (struct attribute *, unsigned,
942 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 943
fe1b8b76 944static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 945
fe1b8b76 946static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 947
fe1b8b76 948static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 949
fe1b8b76 950static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 951
93311388 952static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 953
fe1b8b76 954static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 955 unsigned int *);
c906108c 956
c764a876
DE
957static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
958
959static LONGEST read_checked_initial_length_and_offset
960 (bfd *, gdb_byte *, const struct comp_unit_head *,
961 unsigned int *, unsigned int *);
613e1657 962
fe1b8b76 963static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
964 unsigned int *);
965
966static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 967
fe1b8b76 968static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 969
9b1c24c8 970static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 971
fe1b8b76
JB
972static char *read_indirect_string (bfd *, gdb_byte *,
973 const struct comp_unit_head *,
974 unsigned int *);
4bdf3d34 975
fe1b8b76 976static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 977
fe1b8b76 978static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 979
fe1b8b76 980static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 981
e142c38c 982static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 983
e142c38c
DJ
984static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
985 struct dwarf2_cu *);
c906108c 986
348e048f
DE
987static struct attribute *dwarf2_attr_no_follow (struct die_info *,
988 unsigned int,
989 struct dwarf2_cu *);
990
05cf31d1
JB
991static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
992 struct dwarf2_cu *cu);
993
e142c38c 994static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 995
e142c38c 996static struct die_info *die_specification (struct die_info *die,
f2f0e013 997 struct dwarf2_cu **);
63d06c5c 998
debd256d
JB
999static void free_line_header (struct line_header *lh);
1000
aaa75496
JB
1001static void add_file_name (struct line_header *, char *, unsigned int,
1002 unsigned int, unsigned int);
1003
debd256d
JB
1004static struct line_header *(dwarf_decode_line_header
1005 (unsigned int offset,
e7c27a73 1006 bfd *abfd, struct dwarf2_cu *cu));
debd256d
JB
1007
1008static void dwarf_decode_lines (struct line_header *, char *, bfd *,
aaa75496 1009 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1010
4f1520fb 1011static void dwarf2_start_subfile (char *, char *, char *);
c906108c 1012
a14ed312 1013static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1014 struct dwarf2_cu *);
c906108c 1015
34eaf542
TT
1016static struct symbol *new_symbol_full (struct die_info *, struct type *,
1017 struct dwarf2_cu *, struct symbol *);
1018
a14ed312 1019static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1020 struct dwarf2_cu *);
c906108c 1021
98bfdba5
PA
1022static void dwarf2_const_value_attr (struct attribute *attr,
1023 struct type *type,
1024 const char *name,
1025 struct obstack *obstack,
1026 struct dwarf2_cu *cu, long *value,
1027 gdb_byte **bytes,
1028 struct dwarf2_locexpr_baton **baton);
2df3850c 1029
e7c27a73 1030static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1031
b4ba55a1
JB
1032static int need_gnat_info (struct dwarf2_cu *);
1033
1034static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1035
1036static void set_descriptive_type (struct type *, struct die_info *,
1037 struct dwarf2_cu *);
1038
e7c27a73
DJ
1039static struct type *die_containing_type (struct die_info *,
1040 struct dwarf2_cu *);
c906108c 1041
e7c27a73 1042static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
c906108c 1043
f792889a 1044static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1045
086ed43d 1046static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1047
6e70227d 1048static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1049 const char *suffix, int physname,
1050 struct dwarf2_cu *cu);
63d06c5c 1051
e7c27a73 1052static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1053
348e048f
DE
1054static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1055
e7c27a73 1056static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1057
e7c27a73 1058static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1059
ff013f42
JK
1060static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1061 struct dwarf2_cu *, struct partial_symtab *);
1062
a14ed312 1063static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1064 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1065 struct partial_symtab *);
c906108c 1066
fae299cd
DC
1067static void get_scope_pc_bounds (struct die_info *,
1068 CORE_ADDR *, CORE_ADDR *,
1069 struct dwarf2_cu *);
1070
801e3a5b
JB
1071static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1072 CORE_ADDR, struct dwarf2_cu *);
1073
a14ed312 1074static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1075 struct dwarf2_cu *);
c906108c 1076
a14ed312 1077static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1078 struct type *, struct dwarf2_cu *);
c906108c 1079
a14ed312 1080static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1081 struct die_info *, struct type *,
e7c27a73 1082 struct dwarf2_cu *);
c906108c 1083
a14ed312 1084static void dwarf2_attach_fn_fields_to_type (struct field_info *,
e7c27a73 1085 struct type *, struct dwarf2_cu *);
c906108c 1086
134d01f1 1087static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1088
e7c27a73 1089static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1090
e7c27a73 1091static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1092
5d7cb8df
JK
1093static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1094
27aa8d6a
SW
1095static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1096
f55ee35c
JK
1097static struct type *read_module_type (struct die_info *die,
1098 struct dwarf2_cu *cu);
1099
38d518c9 1100static const char *namespace_name (struct die_info *die,
e142c38c 1101 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1102
134d01f1 1103static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1104
e7c27a73 1105static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1106
6e70227d 1107static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1108 struct dwarf2_cu *);
1109
93311388 1110static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1111
93311388
DE
1112static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1113 gdb_byte *info_ptr,
d97bc12b
DE
1114 gdb_byte **new_info_ptr,
1115 struct die_info *parent);
1116
93311388
DE
1117static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1118 gdb_byte *info_ptr,
fe1b8b76 1119 gdb_byte **new_info_ptr,
639d11d3
DC
1120 struct die_info *parent);
1121
93311388
DE
1122static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1123 gdb_byte *info_ptr,
fe1b8b76 1124 gdb_byte **new_info_ptr,
639d11d3
DC
1125 struct die_info *parent);
1126
93311388
DE
1127static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1128 struct die_info **, gdb_byte *,
1129 int *);
1130
e7c27a73 1131static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1132
71c25dea
TT
1133static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1134 struct obstack *);
1135
e142c38c 1136static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1137
98bfdba5
PA
1138static const char *dwarf2_full_name (char *name,
1139 struct die_info *die,
1140 struct dwarf2_cu *cu);
1141
e142c38c 1142static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1143 struct dwarf2_cu **);
9219021c 1144
a14ed312 1145static char *dwarf_tag_name (unsigned int);
c906108c 1146
a14ed312 1147static char *dwarf_attr_name (unsigned int);
c906108c 1148
a14ed312 1149static char *dwarf_form_name (unsigned int);
c906108c 1150
a14ed312 1151static char *dwarf_bool_name (unsigned int);
c906108c 1152
a14ed312 1153static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1154
1155#if 0
a14ed312 1156static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1157#endif
1158
f9aca02d 1159static struct die_info *sibling_die (struct die_info *);
c906108c 1160
d97bc12b
DE
1161static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1162
1163static void dump_die_for_error (struct die_info *);
1164
1165static void dump_die_1 (struct ui_file *, int level, int max_level,
1166 struct die_info *);
c906108c 1167
d97bc12b 1168/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1169
51545339 1170static void store_in_ref_table (struct die_info *,
10b3939b 1171 struct dwarf2_cu *);
c906108c 1172
93311388
DE
1173static int is_ref_attr (struct attribute *);
1174
c764a876 1175static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1176
43bbcdc2 1177static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1178
348e048f
DE
1179static struct die_info *follow_die_ref_or_sig (struct die_info *,
1180 struct attribute *,
1181 struct dwarf2_cu **);
1182
10b3939b
DJ
1183static struct die_info *follow_die_ref (struct die_info *,
1184 struct attribute *,
f2f0e013 1185 struct dwarf2_cu **);
c906108c 1186
348e048f
DE
1187static struct die_info *follow_die_sig (struct die_info *,
1188 struct attribute *,
1189 struct dwarf2_cu **);
1190
1191static void read_signatured_type_at_offset (struct objfile *objfile,
1192 unsigned int offset);
1193
1194static void read_signatured_type (struct objfile *,
1195 struct signatured_type *type_sig);
1196
c906108c
SS
1197/* memory allocation interface */
1198
7b5a2f43 1199static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1200
f3dd6933 1201static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1202
b60c80d6 1203static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1204
e142c38c 1205static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1206
e142c38c
DJ
1207static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1208 struct dwarf2_cu *);
5fb290d7 1209
2e276125 1210static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1211 char *, bfd *, struct dwarf2_cu *);
2e276125 1212
8e19ed76
PS
1213static int attr_form_is_block (struct attribute *);
1214
3690dd37
JB
1215static int attr_form_is_section_offset (struct attribute *);
1216
1217static int attr_form_is_constant (struct attribute *);
1218
93e7bd98
DJ
1219static void dwarf2_symbol_mark_computed (struct attribute *attr,
1220 struct symbol *sym,
1221 struct dwarf2_cu *cu);
4c2df51b 1222
93311388
DE
1223static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1224 struct abbrev_info *abbrev,
1225 struct dwarf2_cu *cu);
4bb7a0a7 1226
72bf9492
DJ
1227static void free_stack_comp_unit (void *);
1228
72bf9492
DJ
1229static hashval_t partial_die_hash (const void *item);
1230
1231static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1232
ae038cb0 1233static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1234 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1235
1236static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1237 (unsigned int offset, struct objfile *objfile);
ae038cb0 1238
93311388
DE
1239static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1240
ae038cb0
DJ
1241static void free_one_comp_unit (void *);
1242
1243static void free_cached_comp_units (void *);
1244
1245static void age_cached_comp_units (void);
1246
1247static void free_one_cached_comp_unit (void *);
1248
f792889a
DJ
1249static struct type *set_die_type (struct die_info *, struct type *,
1250 struct dwarf2_cu *);
1c379e20 1251
ae038cb0
DJ
1252static void create_all_comp_units (struct objfile *);
1253
1fd400ff
TT
1254static int create_debug_types_hash_table (struct objfile *objfile);
1255
93311388
DE
1256static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1257 struct objfile *);
10b3939b
DJ
1258
1259static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1260
1261static void dwarf2_add_dependence (struct dwarf2_cu *,
1262 struct dwarf2_per_cu_data *);
1263
ae038cb0
DJ
1264static void dwarf2_mark (struct dwarf2_cu *);
1265
1266static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1267
f792889a 1268static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1269
9291a0cd
TT
1270static void dwarf2_release_queue (void *dummy);
1271
1272static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1273 struct objfile *objfile);
1274
1275static void process_queue (struct objfile *objfile);
1276
1277static void find_file_and_directory (struct die_info *die,
1278 struct dwarf2_cu *cu,
1279 char **name, char **comp_dir);
1280
1281static char *file_full_name (int file, struct line_header *lh,
1282 const char *comp_dir);
1283
1284static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1285 gdb_byte *info_ptr,
1286 gdb_byte *buffer,
1287 unsigned int buffer_size,
1288 bfd *abfd);
1289
1290static void init_cu_die_reader (struct die_reader_specs *reader,
1291 struct dwarf2_cu *cu);
1292
1fd400ff
TT
1293static htab_t allocate_signatured_type_hash_table (struct objfile *objfile);
1294
9291a0cd
TT
1295#if WORDS_BIGENDIAN
1296
1297/* Convert VALUE between big- and little-endian. */
1298static offset_type
1299byte_swap (offset_type value)
1300{
1301 offset_type result;
1302
1303 result = (value & 0xff) << 24;
1304 result |= (value & 0xff00) << 8;
1305 result |= (value & 0xff0000) >> 8;
1306 result |= (value & 0xff000000) >> 24;
1307 return result;
1308}
1309
1310#define MAYBE_SWAP(V) byte_swap (V)
1311
1312#else
1313#define MAYBE_SWAP(V) (V)
1314#endif /* WORDS_BIGENDIAN */
1315
1316/* The suffix for an index file. */
1317#define INDEX_SUFFIX ".gdb-index"
1318
3da10d80
KS
1319static const char *dwarf2_physname (char *name, struct die_info *die,
1320 struct dwarf2_cu *cu);
1321
c906108c
SS
1322/* Try to locate the sections we need for DWARF 2 debugging
1323 information and return true if we have enough to do something. */
1324
1325int
6502dd73 1326dwarf2_has_info (struct objfile *objfile)
c906108c 1327{
be391dca
TT
1328 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1329 if (!dwarf2_per_objfile)
1330 {
1331 /* Initialize per-objfile state. */
1332 struct dwarf2_per_objfile *data
1333 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1334
be391dca
TT
1335 memset (data, 0, sizeof (*data));
1336 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1337 dwarf2_per_objfile = data;
6502dd73 1338
be391dca
TT
1339 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1340 dwarf2_per_objfile->objfile = objfile;
1341 }
1342 return (dwarf2_per_objfile->info.asection != NULL
1343 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1344}
1345
233a11ab
CS
1346/* When loading sections, we can either look for ".<name>", or for
1347 * ".z<name>", which indicates a compressed section. */
1348
1349static int
dce234bc 1350section_is_p (const char *section_name, const char *name)
233a11ab 1351{
dce234bc
PP
1352 return (section_name[0] == '.'
1353 && (strcmp (section_name + 1, name) == 0
1354 || (section_name[1] == 'z'
1355 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1356}
1357
c906108c
SS
1358/* This function is mapped across the sections and remembers the
1359 offset and size of each of the debugging sections we are interested
1360 in. */
1361
1362static void
72dca2f5 1363dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1364{
dce234bc 1365 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1366 {
dce234bc
PP
1367 dwarf2_per_objfile->info.asection = sectp;
1368 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1369 }
dce234bc 1370 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1371 {
dce234bc
PP
1372 dwarf2_per_objfile->abbrev.asection = sectp;
1373 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1374 }
dce234bc 1375 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1376 {
dce234bc
PP
1377 dwarf2_per_objfile->line.asection = sectp;
1378 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1379 }
dce234bc 1380 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1381 {
dce234bc
PP
1382 dwarf2_per_objfile->loc.asection = sectp;
1383 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1384 }
dce234bc 1385 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1386 {
dce234bc
PP
1387 dwarf2_per_objfile->macinfo.asection = sectp;
1388 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1389 }
dce234bc 1390 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1391 {
dce234bc
PP
1392 dwarf2_per_objfile->str.asection = sectp;
1393 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1394 }
dce234bc 1395 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1396 {
dce234bc
PP
1397 dwarf2_per_objfile->frame.asection = sectp;
1398 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1399 }
dce234bc 1400 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1401 {
3799ccc6 1402 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1403
3799ccc6
EZ
1404 if (aflag & SEC_HAS_CONTENTS)
1405 {
dce234bc
PP
1406 dwarf2_per_objfile->eh_frame.asection = sectp;
1407 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1408 }
b6af0555 1409 }
dce234bc 1410 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1411 {
dce234bc
PP
1412 dwarf2_per_objfile->ranges.asection = sectp;
1413 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1414 }
348e048f
DE
1415 else if (section_is_p (sectp->name, TYPES_SECTION))
1416 {
1417 dwarf2_per_objfile->types.asection = sectp;
1418 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1419 }
9291a0cd
TT
1420 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1421 {
1422 dwarf2_per_objfile->gdb_index.asection = sectp;
1423 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1424 }
dce234bc 1425
72dca2f5
FR
1426 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1427 && bfd_section_vma (abfd, sectp) == 0)
1428 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1429}
1430
dce234bc
PP
1431/* Decompress a section that was compressed using zlib. Store the
1432 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1433
1434static void
dce234bc
PP
1435zlib_decompress_section (struct objfile *objfile, asection *sectp,
1436 gdb_byte **outbuf, bfd_size_type *outsize)
1437{
1438 bfd *abfd = objfile->obfd;
1439#ifndef HAVE_ZLIB_H
1440 error (_("Support for zlib-compressed DWARF data (from '%s') "
1441 "is disabled in this copy of GDB"),
1442 bfd_get_filename (abfd));
1443#else
1444 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1445 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1446 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1447 bfd_size_type uncompressed_size;
1448 gdb_byte *uncompressed_buffer;
1449 z_stream strm;
1450 int rc;
1451 int header_size = 12;
1452
1453 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1454 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1455 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1456 bfd_get_filename (abfd));
1457
1458 /* Read the zlib header. In this case, it should be "ZLIB" followed
1459 by the uncompressed section size, 8 bytes in big-endian order. */
1460 if (compressed_size < header_size
1461 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1462 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1463 bfd_get_filename (abfd));
1464 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1465 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1466 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1467 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1468 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1469 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1470 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1471 uncompressed_size += compressed_buffer[11];
1472
1473 /* It is possible the section consists of several compressed
1474 buffers concatenated together, so we uncompress in a loop. */
1475 strm.zalloc = NULL;
1476 strm.zfree = NULL;
1477 strm.opaque = NULL;
1478 strm.avail_in = compressed_size - header_size;
1479 strm.next_in = (Bytef*) compressed_buffer + header_size;
1480 strm.avail_out = uncompressed_size;
1481 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1482 uncompressed_size);
1483 rc = inflateInit (&strm);
1484 while (strm.avail_in > 0)
1485 {
1486 if (rc != Z_OK)
1487 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1488 bfd_get_filename (abfd), rc);
1489 strm.next_out = ((Bytef*) uncompressed_buffer
1490 + (uncompressed_size - strm.avail_out));
1491 rc = inflate (&strm, Z_FINISH);
1492 if (rc != Z_STREAM_END)
1493 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1494 bfd_get_filename (abfd), rc);
1495 rc = inflateReset (&strm);
1496 }
1497 rc = inflateEnd (&strm);
1498 if (rc != Z_OK
1499 || strm.avail_out != 0)
1500 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1501 bfd_get_filename (abfd), rc);
1502
affddf13 1503 do_cleanups (cleanup);
dce234bc
PP
1504 *outbuf = uncompressed_buffer;
1505 *outsize = uncompressed_size;
1506#endif
233a11ab
CS
1507}
1508
dce234bc
PP
1509/* Read the contents of the section SECTP from object file specified by
1510 OBJFILE, store info about the section into INFO.
1511 If the section is compressed, uncompress it before returning. */
c906108c 1512
dce234bc
PP
1513static void
1514dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1515{
dce234bc
PP
1516 bfd *abfd = objfile->obfd;
1517 asection *sectp = info->asection;
1518 gdb_byte *buf, *retbuf;
1519 unsigned char header[4];
c906108c 1520
be391dca
TT
1521 if (info->readin)
1522 return;
dce234bc
PP
1523 info->buffer = NULL;
1524 info->was_mmapped = 0;
be391dca 1525 info->readin = 1;
188dd5d6 1526
dce234bc
PP
1527 if (info->asection == NULL || info->size == 0)
1528 return;
c906108c 1529
dce234bc
PP
1530 /* Check if the file has a 4-byte header indicating compression. */
1531 if (info->size > sizeof (header)
1532 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1533 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1534 {
1535 /* Upon decompression, update the buffer and its size. */
1536 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1537 {
1538 zlib_decompress_section (objfile, sectp, &info->buffer,
1539 &info->size);
1540 return;
1541 }
1542 }
4bdf3d34 1543
dce234bc
PP
1544#ifdef HAVE_MMAP
1545 if (pagesize == 0)
1546 pagesize = getpagesize ();
2e276125 1547
dce234bc
PP
1548 /* Only try to mmap sections which are large enough: we don't want to
1549 waste space due to fragmentation. Also, only try mmap for sections
1550 without relocations. */
1551
1552 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1553 {
1554 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1555 size_t map_length = info->size + sectp->filepos - pg_offset;
1556 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1557 MAP_PRIVATE, pg_offset);
1558
1559 if (retbuf != MAP_FAILED)
1560 {
1561 info->was_mmapped = 1;
1562 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1563#if HAVE_POSIX_MADVISE
1564 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1565#endif
dce234bc
PP
1566 return;
1567 }
1568 }
1569#endif
1570
1571 /* If we get here, we are a normal, not-compressed section. */
1572 info->buffer = buf
1573 = obstack_alloc (&objfile->objfile_obstack, info->size);
1574
1575 /* When debugging .o files, we may need to apply relocations; see
1576 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1577 We never compress sections in .o files, so we only need to
1578 try this when the section is not compressed. */
ac8035ab 1579 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1580 if (retbuf != NULL)
1581 {
1582 info->buffer = retbuf;
1583 return;
1584 }
1585
1586 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1587 || bfd_bread (buf, info->size, abfd) != info->size)
1588 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1589 bfd_get_filename (abfd));
1590}
1591
1592/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1593 SECTION_NAME. */
af34e669 1594
dce234bc
PP
1595void
1596dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1597 asection **sectp, gdb_byte **bufp,
1598 bfd_size_type *sizep)
1599{
1600 struct dwarf2_per_objfile *data
1601 = objfile_data (objfile, dwarf2_objfile_data_key);
1602 struct dwarf2_section_info *info;
a3b2a86b
TT
1603
1604 /* We may see an objfile without any DWARF, in which case we just
1605 return nothing. */
1606 if (data == NULL)
1607 {
1608 *sectp = NULL;
1609 *bufp = NULL;
1610 *sizep = 0;
1611 return;
1612 }
dce234bc
PP
1613 if (section_is_p (section_name, EH_FRAME_SECTION))
1614 info = &data->eh_frame;
1615 else if (section_is_p (section_name, FRAME_SECTION))
1616 info = &data->frame;
0d53c4c4 1617 else
f3574227 1618 gdb_assert_not_reached ("unexpected section");
dce234bc
PP
1619
1620 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1621 /* We haven't read this section in yet. Do it now. */
1622 dwarf2_read_section (objfile, info);
1623
1624 *sectp = info->asection;
1625 *bufp = info->buffer;
1626 *sizep = info->size;
1627}
1628
9291a0cd
TT
1629\f
1630
1631/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1632 this CU came. */
1633static void
1634dw2_do_instantiate_symtab (struct objfile *objfile,
1635 struct dwarf2_per_cu_data *per_cu)
1636{
1637 struct cleanup *back_to;
1638
1639 back_to = make_cleanup (dwarf2_release_queue, NULL);
1640
1641 queue_comp_unit (per_cu, objfile);
1642
1643 if (per_cu->from_debug_types)
1644 read_signatured_type_at_offset (objfile, per_cu->offset);
1645 else
1646 load_full_comp_unit (per_cu, objfile);
1647
1648 process_queue (objfile);
1649
1650 /* Age the cache, releasing compilation units that have not
1651 been used recently. */
1652 age_cached_comp_units ();
1653
1654 do_cleanups (back_to);
1655}
1656
1657/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1658 the objfile from which this CU came. Returns the resulting symbol
1659 table. */
1660static struct symtab *
1661dw2_instantiate_symtab (struct objfile *objfile,
1662 struct dwarf2_per_cu_data *per_cu)
1663{
1664 if (!per_cu->v.quick->symtab)
1665 {
1666 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1667 increment_reading_symtab ();
1668 dw2_do_instantiate_symtab (objfile, per_cu);
1669 do_cleanups (back_to);
1670 }
1671 return per_cu->v.quick->symtab;
1672}
1673
1fd400ff
TT
1674/* Return the CU given its index. */
1675static struct dwarf2_per_cu_data *
1676dw2_get_cu (int index)
1677{
1678 if (index >= dwarf2_per_objfile->n_comp_units)
1679 {
1680 index -= dwarf2_per_objfile->n_comp_units;
1681 return dwarf2_per_objfile->type_comp_units[index];
1682 }
1683 return dwarf2_per_objfile->all_comp_units[index];
1684}
1685
9291a0cd
TT
1686/* A helper function that knows how to read a 64-bit value in a way
1687 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1688 otherwise. */
1689static int
1690extract_cu_value (const char *bytes, ULONGEST *result)
1691{
1692 if (sizeof (ULONGEST) < 8)
1693 {
1694 int i;
1695
1696 /* Ignore the upper 4 bytes if they are all zero. */
1697 for (i = 0; i < 4; ++i)
1698 if (bytes[i + 4] != 0)
1699 return 0;
1700
1701 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1702 }
1703 else
1704 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1705 return 1;
1706}
1707
1708/* Read the CU list from the mapped index, and use it to create all
1709 the CU objects for this objfile. Return 0 if something went wrong,
1710 1 if everything went ok. */
1711static int
1fd400ff
TT
1712create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1713 offset_type cu_list_elements)
9291a0cd
TT
1714{
1715 offset_type i;
9291a0cd
TT
1716
1717 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1718 dwarf2_per_objfile->all_comp_units
1719 = obstack_alloc (&objfile->objfile_obstack,
1720 dwarf2_per_objfile->n_comp_units
1721 * sizeof (struct dwarf2_per_cu_data *));
1722
1723 for (i = 0; i < cu_list_elements; i += 2)
1724 {
1725 struct dwarf2_per_cu_data *the_cu;
1726 ULONGEST offset, length;
1727
1728 if (!extract_cu_value (cu_list, &offset)
1729 || !extract_cu_value (cu_list + 8, &length))
1730 return 0;
1731 cu_list += 2 * 8;
1732
1733 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1734 struct dwarf2_per_cu_data);
1735 the_cu->offset = offset;
1736 the_cu->length = length;
1737 the_cu->objfile = objfile;
1738 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1739 struct dwarf2_per_cu_quick_data);
1740 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1741 }
1742
1743 return 1;
1744}
1745
1fd400ff
TT
1746/* Create the signatured type hash table from the index. */
1747static int
1748create_signatured_type_hash_from_index (struct objfile *objfile,
1749 const gdb_byte *bytes,
1750 offset_type elements)
1751{
1752 offset_type i;
1753 htab_t type_hash;
1754
1755 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1756 dwarf2_per_objfile->type_comp_units
1757 = obstack_alloc (&objfile->objfile_obstack,
1758 dwarf2_per_objfile->n_type_comp_units
1759 * sizeof (struct dwarf2_per_cu_data *));
1760
1761 type_hash = allocate_signatured_type_hash_table (objfile);
1762
1763 for (i = 0; i < elements; i += 3)
1764 {
1765 struct signatured_type *type_sig;
1766 ULONGEST offset, type_offset, signature;
1767 void **slot;
1768
1769 if (!extract_cu_value (bytes, &offset)
1770 || !extract_cu_value (bytes + 8, &type_offset))
1771 return 0;
1772 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1773 bytes += 3 * 8;
1774
1775 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1776 struct signatured_type);
1777 type_sig->signature = signature;
1778 type_sig->offset = offset;
1779 type_sig->type_offset = type_offset;
1780 type_sig->per_cu.from_debug_types = 1;
1781 type_sig->per_cu.offset = offset;
1782 type_sig->per_cu.objfile = objfile;
1783 type_sig->per_cu.v.quick
1784 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1785 struct dwarf2_per_cu_quick_data);
1786
1787 slot = htab_find_slot (type_hash, type_sig, INSERT);
1788 *slot = type_sig;
1789
1790 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1791 }
1792
1793 dwarf2_per_objfile->signatured_types = type_hash;
1794
1795 return 1;
1796}
1797
9291a0cd
TT
1798/* Read the address map data from the mapped index, and use it to
1799 populate the objfile's psymtabs_addrmap. */
1800static void
1801create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1802{
1803 const gdb_byte *iter, *end;
1804 struct obstack temp_obstack;
1805 struct addrmap *mutable_map;
1806 struct cleanup *cleanup;
1807 CORE_ADDR baseaddr;
1808
1809 obstack_init (&temp_obstack);
1810 cleanup = make_cleanup_obstack_free (&temp_obstack);
1811 mutable_map = addrmap_create_mutable (&temp_obstack);
1812
1813 iter = index->address_table;
1814 end = iter + index->address_table_size;
1815
1816 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1817
1818 while (iter < end)
1819 {
1820 ULONGEST hi, lo, cu_index;
1821 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1822 iter += 8;
1823 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1824 iter += 8;
1825 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1826 iter += 4;
1827
1828 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1829 dw2_get_cu (cu_index));
9291a0cd
TT
1830 }
1831
1832 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1833 &objfile->objfile_obstack);
1834 do_cleanups (cleanup);
1835}
1836
1837/* The hash function for strings in the mapped index. This is the
1838 same as the hashtab.c hash function, but we keep a separate copy to
1839 maintain control over the implementation. This is necessary
1840 because the hash function is tied to the format of the mapped index
1841 file. */
1842static hashval_t
1843mapped_index_string_hash (const void *p)
1844{
1845 const unsigned char *str = (const unsigned char *) p;
1846 hashval_t r = 0;
1847 unsigned char c;
1848
1849 while ((c = *str++) != 0)
1850 r = r * 67 + c - 113;
1851
1852 return r;
1853}
1854
1855/* Find a slot in the mapped index INDEX for the object named NAME.
1856 If NAME is found, set *VEC_OUT to point to the CU vector in the
1857 constant pool and return 1. If NAME cannot be found, return 0. */
1858static int
1859find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1860 offset_type **vec_out)
1861{
1862 offset_type hash = mapped_index_string_hash (name);
1863 offset_type slot, step;
1864
1865 slot = hash & (index->index_table_slots - 1);
1866 step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1867
1868 for (;;)
1869 {
1870 /* Convert a slot number to an offset into the table. */
1871 offset_type i = 2 * slot;
1872 const char *str;
1873 if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1874 return 0;
1875
1876 str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1877 if (!strcmp (name, str))
1878 {
1879 *vec_out = (offset_type *) (index->constant_pool
1880 + MAYBE_SWAP (index->index_table[i + 1]));
1881 return 1;
1882 }
1883
1884 slot = (slot + step) & (index->index_table_slots - 1);
1885 }
1886}
1887
1888/* Read the index file. If everything went ok, initialize the "quick"
1889 elements of all the CUs and return 1. Otherwise, return 0. */
1890static int
1891dwarf2_read_index (struct objfile *objfile)
1892{
9291a0cd
TT
1893 char *addr;
1894 struct mapped_index *map;
b3b272e1 1895 offset_type *metadata;
ac0b195c
KW
1896 const gdb_byte *cu_list;
1897 const gdb_byte *types_list = NULL;
1898 offset_type version, cu_list_elements;
1899 offset_type types_list_elements = 0;
1fd400ff 1900 int i;
9291a0cd
TT
1901
1902 if (dwarf2_per_objfile->gdb_index.asection == NULL
1903 || dwarf2_per_objfile->gdb_index.size == 0)
1904 return 0;
1905 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1906
1907 addr = dwarf2_per_objfile->gdb_index.buffer;
1908 /* Version check. */
1fd400ff
TT
1909 version = MAYBE_SWAP (*(offset_type *) addr);
1910 if (version == 1)
1911 {
1912 /* Index version 1 neglected to account for .debug_types. So,
1913 if we see .debug_types, we cannot use this index. */
1914 if (dwarf2_per_objfile->types.asection != NULL
1915 && dwarf2_per_objfile->types.size != 0)
1916 return 0;
1917 }
1918 else if (version != 2)
9291a0cd
TT
1919 return 0;
1920
1921 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 1922 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
1923
1924 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
1925
1926 i = 0;
1927 cu_list = addr + MAYBE_SWAP (metadata[i]);
1928 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 1929 / 8);
1fd400ff
TT
1930 ++i;
1931
1932 if (version == 2)
1933 {
1934 types_list = addr + MAYBE_SWAP (metadata[i]);
1935 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1936 - MAYBE_SWAP (metadata[i]))
1937 / 8);
1938 ++i;
1939 }
1940
1941 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1942 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1943 - MAYBE_SWAP (metadata[i]));
1944 ++i;
1945
1946 map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1947 map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1948 - MAYBE_SWAP (metadata[i]))
9291a0cd 1949 / (2 * sizeof (offset_type)));
1fd400ff 1950 ++i;
9291a0cd 1951
1fd400ff
TT
1952 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1953
1954 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1955 return 0;
1956
1957 if (version == 2
1958 && types_list_elements
1959 && !create_signatured_type_hash_from_index (objfile, types_list,
1960 types_list_elements))
9291a0cd
TT
1961 return 0;
1962
1963 create_addrmap_from_index (objfile, map);
1964
1965 dwarf2_per_objfile->index_table = map;
1966 dwarf2_per_objfile->using_index = 1;
1967
1968 return 1;
1969}
1970
1971/* A helper for the "quick" functions which sets the global
1972 dwarf2_per_objfile according to OBJFILE. */
1973static void
1974dw2_setup (struct objfile *objfile)
1975{
1976 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1977 gdb_assert (dwarf2_per_objfile);
1978}
1979
1980/* A helper for the "quick" functions which attempts to read the line
1981 table for THIS_CU. */
1982static void
1983dw2_require_line_header (struct objfile *objfile,
1984 struct dwarf2_per_cu_data *this_cu)
1985{
1986 bfd *abfd = objfile->obfd;
1987 struct line_header *lh = NULL;
1988 struct attribute *attr;
1989 struct cleanup *cleanups;
1990 struct die_info *comp_unit_die;
1991 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
1992 int has_children, i;
1993 struct dwarf2_cu cu;
1994 unsigned int bytes_read, buffer_size;
1995 struct die_reader_specs reader_specs;
1996 char *name, *comp_dir;
1997
1998 if (this_cu->v.quick->read_lines)
1999 return;
2000 this_cu->v.quick->read_lines = 1;
2001
2002 memset (&cu, 0, sizeof (cu));
2003 cu.objfile = objfile;
2004 obstack_init (&cu.comp_unit_obstack);
2005
2006 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2007
2008 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
2009 buffer_size = dwarf2_per_objfile->info.size;
2010 buffer = dwarf2_per_objfile->info.buffer;
2011 info_ptr = buffer + this_cu->offset;
2012 beg_of_comp_unit = info_ptr;
2013
2014 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2015 buffer, buffer_size,
2016 abfd);
2017
2018 /* Complete the cu_header. */
2019 cu.header.offset = beg_of_comp_unit - buffer;
2020 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2021
2022 this_cu->cu = &cu;
2023 cu.per_cu = this_cu;
2024
2025 dwarf2_read_abbrevs (abfd, &cu);
2026 make_cleanup (dwarf2_free_abbrev_table, &cu);
2027
2028 if (this_cu->from_debug_types)
2029 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2030 init_cu_die_reader (&reader_specs, &cu);
2031 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2032 &has_children);
2033
2034 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2035 if (attr)
2036 {
2037 unsigned int line_offset = DW_UNSND (attr);
2038 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2039 }
2040 if (lh == NULL)
2041 {
2042 do_cleanups (cleanups);
2043 return;
2044 }
2045
2046 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2047
2048 this_cu->v.quick->lines = lh;
2049
2050 this_cu->v.quick->file_names
2051 = obstack_alloc (&objfile->objfile_obstack,
2052 lh->num_file_names * sizeof (char *));
2053 for (i = 0; i < lh->num_file_names; ++i)
2054 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2055
2056 do_cleanups (cleanups);
2057}
2058
2059/* A helper for the "quick" functions which computes and caches the
2060 real path for a given file name from the line table.
2061 dw2_require_line_header must have been called before this is
2062 invoked. */
2063static const char *
2064dw2_require_full_path (struct objfile *objfile,
2065 struct dwarf2_per_cu_data *cu,
2066 int index)
2067{
2068 if (!cu->v.quick->full_names)
2069 cu->v.quick->full_names
2070 = OBSTACK_CALLOC (&objfile->objfile_obstack,
2071 cu->v.quick->lines->num_file_names,
2072 sizeof (char *));
2073
2074 if (!cu->v.quick->full_names[index])
2075 cu->v.quick->full_names[index]
2076 = gdb_realpath (cu->v.quick->file_names[index]);
2077
2078 return cu->v.quick->full_names[index];
2079}
2080
2081static struct symtab *
2082dw2_find_last_source_symtab (struct objfile *objfile)
2083{
2084 int index;
2085 dw2_setup (objfile);
2086 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2087 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2088}
2089
2090static void
2091dw2_forget_cached_source_info (struct objfile *objfile)
2092{
2093 int i;
2094
2095 dw2_setup (objfile);
1fd400ff
TT
2096 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2097 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2098 {
1fd400ff 2099 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2100
2101 if (cu->v.quick->full_names)
2102 {
2103 int j;
2104
2105 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2106 xfree ((void *) cu->v.quick->full_names[j]);
2107 }
2108 }
2109}
2110
2111static int
2112dw2_lookup_symtab (struct objfile *objfile, const char *name,
2113 const char *full_path, const char *real_path,
2114 struct symtab **result)
2115{
2116 int i;
2117 int check_basename = lbasename (name) == name;
2118 struct dwarf2_per_cu_data *base_cu = NULL;
2119
2120 dw2_setup (objfile);
1fd400ff
TT
2121 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2122 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2123 {
2124 int j;
1fd400ff 2125 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2126
2127 if (cu->v.quick->symtab)
2128 continue;
2129
2130 dw2_require_line_header (objfile, cu);
2131 if (!cu->v.quick->lines)
2132 continue;
2133
2134 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2135 {
2136 const char *this_name = cu->v.quick->file_names[j];
2137
2138 if (FILENAME_CMP (name, this_name) == 0)
2139 {
2140 *result = dw2_instantiate_symtab (objfile, cu);
2141 return 1;
2142 }
2143
2144 if (check_basename && ! base_cu
2145 && FILENAME_CMP (lbasename (this_name), name) == 0)
2146 base_cu = cu;
2147
2148 if (full_path != NULL)
2149 {
2150 const char *this_full_name = dw2_require_full_path (objfile,
2151 cu, j);
2152
2153 if (this_full_name
2154 && FILENAME_CMP (full_path, this_full_name) == 0)
2155 {
2156 *result = dw2_instantiate_symtab (objfile, cu);
2157 return 1;
2158 }
2159 }
2160
2161 if (real_path != NULL)
2162 {
2163 const char *this_full_name = dw2_require_full_path (objfile,
2164 cu, j);
2165
2166 if (this_full_name != NULL)
2167 {
2168 char *rp = gdb_realpath (this_full_name);
2169 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2170 {
2171 xfree (rp);
2172 *result = dw2_instantiate_symtab (objfile, cu);
2173 return 1;
2174 }
2175 xfree (rp);
2176 }
2177 }
2178 }
2179 }
2180
2181 if (base_cu)
2182 {
2183 *result = dw2_instantiate_symtab (objfile, base_cu);
2184 return 1;
2185 }
2186
2187 return 0;
2188}
2189
2190static struct symtab *
2191dw2_lookup_symbol (struct objfile *objfile, int block_index,
2192 const char *name, domain_enum domain)
2193{
2194 /* We do all the work in the pre_expand_symtabs_matching hook
2195 instead. */
2196 return NULL;
2197}
2198
2199/* A helper function that expands all symtabs that hold an object
2200 named NAME. */
2201static void
2202dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2203{
2204 dw2_setup (objfile);
2205
2206 if (dwarf2_per_objfile->index_table)
2207 {
2208 offset_type *vec;
2209
2210 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2211 name, &vec))
2212 {
2213 offset_type i, len = MAYBE_SWAP (*vec);
2214 for (i = 0; i < len; ++i)
2215 {
2216 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
1fd400ff
TT
2217 struct dwarf2_per_cu_data *cu = dw2_get_cu (cu_index);
2218
9291a0cd
TT
2219 dw2_instantiate_symtab (objfile, cu);
2220 }
2221 }
2222 }
2223}
2224
2225static void
2226dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2227 int kind, const char *name,
2228 domain_enum domain)
2229{
2230 dw2_do_expand_symtabs_matching (objfile, name);
2231}
2232
2233static void
2234dw2_print_stats (struct objfile *objfile)
2235{
2236 int i, count;
2237
2238 dw2_setup (objfile);
2239 count = 0;
1fd400ff
TT
2240 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2241 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2242 {
1fd400ff 2243 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2244
2245 if (!cu->v.quick->symtab)
2246 ++count;
2247 }
2248 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2249}
2250
2251static void
2252dw2_dump (struct objfile *objfile)
2253{
2254 /* Nothing worth printing. */
2255}
2256
2257static void
2258dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2259 struct section_offsets *delta)
2260{
2261 /* There's nothing to relocate here. */
2262}
2263
2264static void
2265dw2_expand_symtabs_for_function (struct objfile *objfile,
2266 const char *func_name)
2267{
2268 dw2_do_expand_symtabs_matching (objfile, func_name);
2269}
2270
2271static void
2272dw2_expand_all_symtabs (struct objfile *objfile)
2273{
2274 int i;
2275
2276 dw2_setup (objfile);
1fd400ff
TT
2277
2278 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2279 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2280 {
1fd400ff 2281 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2282
2283 dw2_instantiate_symtab (objfile, cu);
2284 }
2285}
2286
2287static void
2288dw2_expand_symtabs_with_filename (struct objfile *objfile,
2289 const char *filename)
2290{
2291 int i;
2292
2293 dw2_setup (objfile);
1fd400ff
TT
2294 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2295 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2296 {
2297 int j;
1fd400ff 2298 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2299
2300 if (cu->v.quick->symtab)
2301 continue;
2302
2303 dw2_require_line_header (objfile, cu);
2304 if (!cu->v.quick->lines)
2305 continue;
2306
2307 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2308 {
2309 const char *this_name = cu->v.quick->file_names[j];
2310 if (strcmp (this_name, filename) == 0)
2311 {
2312 dw2_instantiate_symtab (objfile, cu);
2313 break;
2314 }
2315 }
2316 }
2317}
2318
dd786858 2319static const char *
9291a0cd
TT
2320dw2_find_symbol_file (struct objfile *objfile, const char *name)
2321{
2322 struct dwarf2_per_cu_data *cu;
2323 offset_type *vec;
2324
2325 dw2_setup (objfile);
2326
2327 if (!dwarf2_per_objfile->index_table)
2328 return NULL;
2329
2330 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2331 name, &vec))
2332 return NULL;
2333
2334 /* Note that this just looks at the very first one named NAME -- but
2335 actually we are looking for a function. find_main_filename
2336 should be rewritten so that it doesn't require a custom hook. It
2337 could just use the ordinary symbol tables. */
2338 /* vec[0] is the length, which must always be >0. */
1fd400ff 2339 cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd
TT
2340
2341 dw2_require_line_header (objfile, cu);
2342 if (!cu->v.quick->lines)
2343 return NULL;
2344
dd786858 2345 return cu->v.quick->file_names[cu->v.quick->lines->num_file_names - 1];
9291a0cd
TT
2346}
2347
2348static void
2349dw2_map_ada_symtabs (struct objfile *objfile,
2350 int (*wild_match) (const char *, int, const char *),
2351 int (*is_name_suffix) (const char *),
2352 void (*callback) (struct objfile *,
2353 struct symtab *, void *),
2354 const char *name, int global,
2355 domain_enum namespace, int wild,
2356 void *data)
2357{
2358 /* For now, we don't support Ada, so this function can't be
2359 reached. */
2360 internal_error (__FILE__, __LINE__,
2361 _("map_ada_symtabs called via index method"));
2362}
2363
2364static void
2365dw2_expand_symtabs_matching (struct objfile *objfile,
2366 int (*file_matcher) (const char *, void *),
2367 int (*name_matcher) (const char *, void *),
2368 domain_enum kind,
2369 void *data)
2370{
2371 int i;
2372 offset_type iter;
2373
2374 dw2_setup (objfile);
2375 if (!dwarf2_per_objfile->index_table)
2376 return;
2377
1fd400ff
TT
2378 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2379 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2380 {
2381 int j;
1fd400ff 2382 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2383
2384 cu->v.quick->mark = 0;
2385 if (cu->v.quick->symtab)
2386 continue;
2387
2388 dw2_require_line_header (objfile, cu);
2389 if (!cu->v.quick->lines)
2390 continue;
2391
2392 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2393 {
2394 if (file_matcher (cu->v.quick->file_names[j], data))
2395 {
2396 cu->v.quick->mark = 1;
2397 break;
2398 }
2399 }
2400 }
2401
2402 for (iter = 0;
2403 iter < dwarf2_per_objfile->index_table->index_table_slots;
2404 ++iter)
2405 {
2406 offset_type idx = 2 * iter;
2407 const char *name;
2408 offset_type *vec, vec_len, vec_idx;
2409
2410 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2411 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2412 continue;
2413
2414 name = (dwarf2_per_objfile->index_table->constant_pool
2415 + dwarf2_per_objfile->index_table->index_table[idx]);
2416
2417 if (! (*name_matcher) (name, data))
2418 continue;
2419
2420 /* The name was matched, now expand corresponding CUs that were
2421 marked. */
2422 vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2423 + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2424 vec_len = MAYBE_SWAP (vec[0]);
2425 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2426 {
1fd400ff
TT
2427 struct dwarf2_per_cu_data *cu;
2428
2429 cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
9291a0cd
TT
2430 if (cu->v.quick->mark)
2431 dw2_instantiate_symtab (objfile, cu);
2432 }
2433 }
2434}
2435
2436static struct symtab *
2437dw2_find_pc_sect_symtab (struct objfile *objfile,
2438 struct minimal_symbol *msymbol,
2439 CORE_ADDR pc,
2440 struct obj_section *section,
2441 int warn_if_readin)
2442{
2443 struct dwarf2_per_cu_data *data;
2444
2445 dw2_setup (objfile);
2446
2447 if (!objfile->psymtabs_addrmap)
2448 return NULL;
2449
2450 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2451 if (!data)
2452 return NULL;
2453
2454 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2455 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2456 paddress (get_objfile_arch (objfile), pc));
2457
2458 return dw2_instantiate_symtab (objfile, data);
2459}
2460
2461static void
2462dw2_map_symbol_names (struct objfile *objfile,
2463 void (*fun) (const char *, void *),
2464 void *data)
2465{
2466 offset_type iter;
2467 dw2_setup (objfile);
2468
2469 if (!dwarf2_per_objfile->index_table)
2470 return;
2471
2472 for (iter = 0;
2473 iter < dwarf2_per_objfile->index_table->index_table_slots;
2474 ++iter)
2475 {
2476 offset_type idx = 2 * iter;
2477 const char *name;
2478 offset_type *vec, vec_len, vec_idx;
2479
2480 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2481 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2482 continue;
2483
2484 name = (dwarf2_per_objfile->index_table->constant_pool
2485 + dwarf2_per_objfile->index_table->index_table[idx]);
2486
2487 (*fun) (name, data);
2488 }
2489}
2490
2491static void
2492dw2_map_symbol_filenames (struct objfile *objfile,
2493 void (*fun) (const char *, const char *, void *),
2494 void *data)
2495{
2496 int i;
2497
2498 dw2_setup (objfile);
1fd400ff
TT
2499 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2500 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2501 {
2502 int j;
1fd400ff 2503 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2504
2505 if (cu->v.quick->symtab)
2506 continue;
2507
2508 dw2_require_line_header (objfile, cu);
2509 if (!cu->v.quick->lines)
2510 continue;
2511
2512 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2513 {
2514 const char *this_full_name = dw2_require_full_path (objfile, cu, j);
2515 (*fun) (cu->v.quick->file_names[j], this_full_name, data);
2516 }
2517 }
2518}
2519
2520static int
2521dw2_has_symbols (struct objfile *objfile)
2522{
2523 return 1;
2524}
2525
2526const struct quick_symbol_functions dwarf2_gdb_index_functions =
2527{
2528 dw2_has_symbols,
2529 dw2_find_last_source_symtab,
2530 dw2_forget_cached_source_info,
2531 dw2_lookup_symtab,
2532 dw2_lookup_symbol,
2533 dw2_pre_expand_symtabs_matching,
2534 dw2_print_stats,
2535 dw2_dump,
2536 dw2_relocate,
2537 dw2_expand_symtabs_for_function,
2538 dw2_expand_all_symtabs,
2539 dw2_expand_symtabs_with_filename,
2540 dw2_find_symbol_file,
2541 dw2_map_ada_symtabs,
2542 dw2_expand_symtabs_matching,
2543 dw2_find_pc_sect_symtab,
2544 dw2_map_symbol_names,
2545 dw2_map_symbol_filenames
2546};
2547
2548/* Initialize for reading DWARF for this objfile. Return 0 if this
2549 file will use psymtabs, or 1 if using the GNU index. */
2550
2551int
2552dwarf2_initialize_objfile (struct objfile *objfile)
2553{
2554 /* If we're about to read full symbols, don't bother with the
2555 indices. In this case we also don't care if some other debug
2556 format is making psymtabs, because they are all about to be
2557 expanded anyway. */
2558 if ((objfile->flags & OBJF_READNOW))
2559 {
2560 int i;
2561
2562 dwarf2_per_objfile->using_index = 1;
2563 create_all_comp_units (objfile);
1fd400ff 2564 create_debug_types_hash_table (objfile);
9291a0cd 2565
1fd400ff
TT
2566 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2567 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2568 {
1fd400ff 2569 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2570
2571 cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2572 struct dwarf2_per_cu_quick_data);
2573 }
2574
2575 /* Return 1 so that gdb sees the "quick" functions. However,
2576 these functions will be no-ops because we will have expanded
2577 all symtabs. */
2578 return 1;
2579 }
2580
2581 if (dwarf2_read_index (objfile))
2582 return 1;
2583
2584 dwarf2_build_psymtabs (objfile);
2585 return 0;
2586}
2587
2588\f
2589
dce234bc
PP
2590/* Build a partial symbol table. */
2591
2592void
f29dff0a 2593dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2594{
f29dff0a 2595 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2596 {
2597 init_psymbol_list (objfile, 1024);
2598 }
2599
d146bf1e 2600 dwarf2_build_psymtabs_hard (objfile);
c906108c 2601}
c906108c 2602
45452591
DE
2603/* Return TRUE if OFFSET is within CU_HEADER. */
2604
2605static inline int
2606offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2607{
2608 unsigned int bottom = cu_header->offset;
2609 unsigned int top = (cu_header->offset
2610 + cu_header->length
2611 + cu_header->initial_length_size);
9a619af0 2612
45452591
DE
2613 return (offset >= bottom && offset < top);
2614}
2615
93311388
DE
2616/* Read in the comp unit header information from the debug_info at info_ptr.
2617 NOTE: This leaves members offset, first_die_offset to be filled in
2618 by the caller. */
107d2387 2619
fe1b8b76 2620static gdb_byte *
107d2387 2621read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2622 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2623{
2624 int signed_addr;
891d2f0b 2625 unsigned int bytes_read;
c764a876
DE
2626
2627 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2628 cu_header->initial_length_size = bytes_read;
2629 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2630 info_ptr += bytes_read;
107d2387
AC
2631 cu_header->version = read_2_bytes (abfd, info_ptr);
2632 info_ptr += 2;
613e1657 2633 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2634 &bytes_read);
613e1657 2635 info_ptr += bytes_read;
107d2387
AC
2636 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2637 info_ptr += 1;
2638 signed_addr = bfd_get_sign_extend_vma (abfd);
2639 if (signed_addr < 0)
8e65ff28 2640 internal_error (__FILE__, __LINE__,
e2e0b3e5 2641 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2642 cu_header->signed_addr_p = signed_addr;
c764a876 2643
107d2387
AC
2644 return info_ptr;
2645}
2646
fe1b8b76
JB
2647static gdb_byte *
2648partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2649 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2650 bfd *abfd)
2651{
fe1b8b76 2652 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2653
2654 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2655
2dc7f7b3 2656 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2657 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2658 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2659 bfd_get_filename (abfd));
72bf9492 2660
dce234bc 2661 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2662 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2663 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2664 (long) header->abbrev_offset,
93311388 2665 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2666 bfd_get_filename (abfd));
2667
2668 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2669 > buffer + buffer_size)
8a3fe4f8
AC
2670 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2671 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2672 (long) header->length,
93311388 2673 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2674 bfd_get_filename (abfd));
2675
2676 return info_ptr;
2677}
2678
348e048f
DE
2679/* Read in the types comp unit header information from .debug_types entry at
2680 types_ptr. The result is a pointer to one past the end of the header. */
2681
2682static gdb_byte *
2683read_type_comp_unit_head (struct comp_unit_head *cu_header,
2684 ULONGEST *signature,
2685 gdb_byte *types_ptr, bfd *abfd)
2686{
348e048f
DE
2687 gdb_byte *initial_types_ptr = types_ptr;
2688
6e70227d 2689 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2690 &dwarf2_per_objfile->types);
348e048f
DE
2691 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2692
2693 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2694
2695 *signature = read_8_bytes (abfd, types_ptr);
2696 types_ptr += 8;
2697 types_ptr += cu_header->offset_size;
2698 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2699
2700 return types_ptr;
2701}
2702
aaa75496
JB
2703/* Allocate a new partial symtab for file named NAME and mark this new
2704 partial symtab as being an include of PST. */
2705
2706static void
2707dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2708 struct objfile *objfile)
2709{
2710 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2711
2712 subpst->section_offsets = pst->section_offsets;
2713 subpst->textlow = 0;
2714 subpst->texthigh = 0;
2715
2716 subpst->dependencies = (struct partial_symtab **)
2717 obstack_alloc (&objfile->objfile_obstack,
2718 sizeof (struct partial_symtab *));
2719 subpst->dependencies[0] = pst;
2720 subpst->number_of_dependencies = 1;
2721
2722 subpst->globals_offset = 0;
2723 subpst->n_global_syms = 0;
2724 subpst->statics_offset = 0;
2725 subpst->n_static_syms = 0;
2726 subpst->symtab = NULL;
2727 subpst->read_symtab = pst->read_symtab;
2728 subpst->readin = 0;
2729
2730 /* No private part is necessary for include psymtabs. This property
2731 can be used to differentiate between such include psymtabs and
10b3939b 2732 the regular ones. */
58a9656e 2733 subpst->read_symtab_private = NULL;
aaa75496
JB
2734}
2735
2736/* Read the Line Number Program data and extract the list of files
2737 included by the source file represented by PST. Build an include
d85a05f0 2738 partial symtab for each of these included files. */
aaa75496
JB
2739
2740static void
2741dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2742 struct die_info *die,
aaa75496
JB
2743 struct partial_symtab *pst)
2744{
2745 struct objfile *objfile = cu->objfile;
2746 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2747 struct line_header *lh = NULL;
2748 struct attribute *attr;
aaa75496 2749
d85a05f0
DJ
2750 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2751 if (attr)
2752 {
2753 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2754
d85a05f0
DJ
2755 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2756 }
aaa75496
JB
2757 if (lh == NULL)
2758 return; /* No linetable, so no includes. */
2759
2760 dwarf_decode_lines (lh, NULL, abfd, cu, pst);
2761
2762 free_line_header (lh);
2763}
2764
348e048f
DE
2765static hashval_t
2766hash_type_signature (const void *item)
2767{
2768 const struct signatured_type *type_sig = item;
9a619af0 2769
348e048f
DE
2770 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2771 return type_sig->signature;
2772}
2773
2774static int
2775eq_type_signature (const void *item_lhs, const void *item_rhs)
2776{
2777 const struct signatured_type *lhs = item_lhs;
2778 const struct signatured_type *rhs = item_rhs;
9a619af0 2779
348e048f
DE
2780 return lhs->signature == rhs->signature;
2781}
2782
1fd400ff
TT
2783/* Allocate a hash table for signatured types. */
2784
2785static htab_t
2786allocate_signatured_type_hash_table (struct objfile *objfile)
2787{
2788 return htab_create_alloc_ex (41,
2789 hash_type_signature,
2790 eq_type_signature,
2791 NULL,
2792 &objfile->objfile_obstack,
2793 hashtab_obstack_allocate,
2794 dummy_obstack_deallocate);
2795}
2796
2797/* A helper function to add a signatured type CU to a list. */
2798
2799static int
2800add_signatured_type_cu_to_list (void **slot, void *datum)
2801{
2802 struct signatured_type *sigt = *slot;
2803 struct dwarf2_per_cu_data ***datap = datum;
2804
2805 **datap = &sigt->per_cu;
2806 ++*datap;
2807
2808 return 1;
2809}
2810
348e048f
DE
2811/* Create the hash table of all entries in the .debug_types section.
2812 The result is zero if there is an error (e.g. missing .debug_types section),
2813 otherwise non-zero. */
2814
2815static int
2816create_debug_types_hash_table (struct objfile *objfile)
2817{
be391dca 2818 gdb_byte *info_ptr;
348e048f 2819 htab_t types_htab;
1fd400ff 2820 struct dwarf2_per_cu_data **iter;
348e048f 2821
be391dca
TT
2822 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2823 info_ptr = dwarf2_per_objfile->types.buffer;
2824
348e048f
DE
2825 if (info_ptr == NULL)
2826 {
2827 dwarf2_per_objfile->signatured_types = NULL;
2828 return 0;
2829 }
2830
1fd400ff 2831 types_htab = allocate_signatured_type_hash_table (objfile);
348e048f
DE
2832
2833 if (dwarf2_die_debug)
2834 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2835
2836 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2837 {
2838 unsigned int offset;
2839 unsigned int offset_size;
2840 unsigned int type_offset;
2841 unsigned int length, initial_length_size;
2842 unsigned short version;
2843 ULONGEST signature;
2844 struct signatured_type *type_sig;
2845 void **slot;
2846 gdb_byte *ptr = info_ptr;
2847
2848 offset = ptr - dwarf2_per_objfile->types.buffer;
2849
2850 /* We need to read the type's signature in order to build the hash
2851 table, but we don't need to read anything else just yet. */
2852
2853 /* Sanity check to ensure entire cu is present. */
2854 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2855 if (ptr + length + initial_length_size
2856 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2857 {
2858 complaint (&symfile_complaints,
2859 _("debug type entry runs off end of `.debug_types' section, ignored"));
2860 break;
2861 }
2862
2863 offset_size = initial_length_size == 4 ? 4 : 8;
2864 ptr += initial_length_size;
2865 version = bfd_get_16 (objfile->obfd, ptr);
2866 ptr += 2;
2867 ptr += offset_size; /* abbrev offset */
2868 ptr += 1; /* address size */
2869 signature = bfd_get_64 (objfile->obfd, ptr);
2870 ptr += 8;
2871 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2872
2873 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2874 memset (type_sig, 0, sizeof (*type_sig));
2875 type_sig->signature = signature;
2876 type_sig->offset = offset;
2877 type_sig->type_offset = type_offset;
ca1f3406 2878 type_sig->per_cu.objfile = objfile;
1fd400ff 2879 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
2880
2881 slot = htab_find_slot (types_htab, type_sig, INSERT);
2882 gdb_assert (slot != NULL);
2883 *slot = type_sig;
2884
2885 if (dwarf2_die_debug)
2886 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2887 offset, phex (signature, sizeof (signature)));
2888
2889 info_ptr = info_ptr + initial_length_size + length;
2890 }
2891
2892 dwarf2_per_objfile->signatured_types = types_htab;
2893
1fd400ff
TT
2894 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2895 dwarf2_per_objfile->type_comp_units
2896 = obstack_alloc (&objfile->objfile_obstack,
2897 dwarf2_per_objfile->n_type_comp_units
2898 * sizeof (struct dwarf2_per_cu_data *));
2899 iter = &dwarf2_per_objfile->type_comp_units[0];
2900 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2901 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2902 == dwarf2_per_objfile->n_type_comp_units);
2903
348e048f
DE
2904 return 1;
2905}
2906
2907/* Lookup a signature based type.
2908 Returns NULL if SIG is not present in the table. */
2909
2910static struct signatured_type *
2911lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2912{
2913 struct signatured_type find_entry, *entry;
2914
2915 if (dwarf2_per_objfile->signatured_types == NULL)
2916 {
2917 complaint (&symfile_complaints,
2918 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2919 return 0;
2920 }
2921
2922 find_entry.signature = sig;
2923 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2924 return entry;
2925}
2926
d85a05f0
DJ
2927/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2928
2929static void
2930init_cu_die_reader (struct die_reader_specs *reader,
2931 struct dwarf2_cu *cu)
2932{
2933 reader->abfd = cu->objfile->obfd;
2934 reader->cu = cu;
2935 if (cu->per_cu->from_debug_types)
be391dca
TT
2936 {
2937 gdb_assert (dwarf2_per_objfile->types.readin);
2938 reader->buffer = dwarf2_per_objfile->types.buffer;
2939 }
d85a05f0 2940 else
be391dca
TT
2941 {
2942 gdb_assert (dwarf2_per_objfile->info.readin);
2943 reader->buffer = dwarf2_per_objfile->info.buffer;
2944 }
d85a05f0
DJ
2945}
2946
2947/* Find the base address of the compilation unit for range lists and
2948 location lists. It will normally be specified by DW_AT_low_pc.
2949 In DWARF-3 draft 4, the base address could be overridden by
2950 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2951 compilation units with discontinuous ranges. */
2952
2953static void
2954dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2955{
2956 struct attribute *attr;
2957
2958 cu->base_known = 0;
2959 cu->base_address = 0;
2960
2961 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2962 if (attr)
2963 {
2964 cu->base_address = DW_ADDR (attr);
2965 cu->base_known = 1;
2966 }
2967 else
2968 {
2969 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2970 if (attr)
2971 {
2972 cu->base_address = DW_ADDR (attr);
2973 cu->base_known = 1;
2974 }
2975 }
2976}
2977
348e048f
DE
2978/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2979 to combine the common parts.
93311388 2980 Process a compilation unit for a psymtab.
348e048f
DE
2981 BUFFER is a pointer to the beginning of the dwarf section buffer,
2982 either .debug_info or debug_types.
93311388
DE
2983 INFO_PTR is a pointer to the start of the CU.
2984 Returns a pointer to the next CU. */
aaa75496 2985
93311388
DE
2986static gdb_byte *
2987process_psymtab_comp_unit (struct objfile *objfile,
2988 struct dwarf2_per_cu_data *this_cu,
2989 gdb_byte *buffer, gdb_byte *info_ptr,
2990 unsigned int buffer_size)
c906108c 2991{
c906108c 2992 bfd *abfd = objfile->obfd;
93311388 2993 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 2994 struct die_info *comp_unit_die;
c906108c 2995 struct partial_symtab *pst;
5734ee8b 2996 CORE_ADDR baseaddr;
93311388
DE
2997 struct cleanup *back_to_inner;
2998 struct dwarf2_cu cu;
d85a05f0
DJ
2999 int has_children, has_pc_info;
3000 struct attribute *attr;
d85a05f0
DJ
3001 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3002 struct die_reader_specs reader_specs;
c906108c 3003
93311388
DE
3004 memset (&cu, 0, sizeof (cu));
3005 cu.objfile = objfile;
3006 obstack_init (&cu.comp_unit_obstack);
c906108c 3007
93311388 3008 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3009
93311388
DE
3010 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3011 buffer, buffer_size,
3012 abfd);
10b3939b 3013
93311388
DE
3014 /* Complete the cu_header. */
3015 cu.header.offset = beg_of_comp_unit - buffer;
3016 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3017
93311388 3018 cu.list_in_scope = &file_symbols;
af703f96 3019
328c9494
DJ
3020 /* If this compilation unit was already read in, free the
3021 cached copy in order to read it in again. This is
3022 necessary because we skipped some symbols when we first
3023 read in the compilation unit (see load_partial_dies).
3024 This problem could be avoided, but the benefit is
3025 unclear. */
3026 if (this_cu->cu != NULL)
3027 free_one_cached_comp_unit (this_cu->cu);
3028
3029 /* Note that this is a pointer to our stack frame, being
3030 added to a global data structure. It will be cleaned up
3031 in free_stack_comp_unit when we finish with this
3032 compilation unit. */
3033 this_cu->cu = &cu;
d85a05f0
DJ
3034 cu.per_cu = this_cu;
3035
93311388
DE
3036 /* Read the abbrevs for this compilation unit into a table. */
3037 dwarf2_read_abbrevs (abfd, &cu);
3038 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3039
93311388 3040 /* Read the compilation unit die. */
348e048f
DE
3041 if (this_cu->from_debug_types)
3042 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3043 init_cu_die_reader (&reader_specs, &cu);
3044 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3045 &has_children);
93311388 3046
348e048f
DE
3047 if (this_cu->from_debug_types)
3048 {
3049 /* offset,length haven't been set yet for type units. */
3050 this_cu->offset = cu.header.offset;
3051 this_cu->length = cu.header.length + cu.header.initial_length_size;
3052 }
d85a05f0 3053 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3054 {
93311388
DE
3055 info_ptr = (beg_of_comp_unit + cu.header.length
3056 + cu.header.initial_length_size);
3057 do_cleanups (back_to_inner);
3058 return info_ptr;
3059 }
72bf9492 3060
93311388 3061 /* Set the language we're debugging. */
d85a05f0
DJ
3062 attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3063 if (attr)
3064 set_cu_language (DW_UNSND (attr), &cu);
3065 else
3066 set_cu_language (language_minimal, &cu);
c906108c 3067
93311388 3068 /* Allocate a new partial symbol table structure. */
d85a05f0 3069 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3070 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3071 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3072 /* TEXTLOW and TEXTHIGH are set below. */
3073 0,
3074 objfile->global_psymbols.next,
3075 objfile->static_psymbols.next);
72bf9492 3076
d85a05f0
DJ
3077 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3078 if (attr != NULL)
3079 pst->dirname = DW_STRING (attr);
72bf9492 3080
e38df1d0 3081 pst->read_symtab_private = this_cu;
72bf9492 3082
93311388 3083 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3084
93311388
DE
3085 /* Store the function that reads in the rest of the symbol table */
3086 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3087
9291a0cd 3088 this_cu->v.psymtab = pst;
c906108c 3089
d85a05f0
DJ
3090 dwarf2_find_base_address (comp_unit_die, &cu);
3091
93311388
DE
3092 /* Possibly set the default values of LOWPC and HIGHPC from
3093 `DW_AT_ranges'. */
d85a05f0
DJ
3094 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3095 &best_highpc, &cu, pst);
3096 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3097 /* Store the contiguous range if it is not empty; it can be empty for
3098 CUs with no code. */
3099 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3100 best_lowpc + baseaddr,
3101 best_highpc + baseaddr - 1, pst);
93311388
DE
3102
3103 /* Check if comp unit has_children.
3104 If so, read the rest of the partial symbols from this comp unit.
3105 If not, there's no more debug_info for this comp unit. */
d85a05f0 3106 if (has_children)
93311388
DE
3107 {
3108 struct partial_die_info *first_die;
3109 CORE_ADDR lowpc, highpc;
31ffec48 3110
93311388
DE
3111 lowpc = ((CORE_ADDR) -1);
3112 highpc = ((CORE_ADDR) 0);
c906108c 3113
93311388 3114 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3115
93311388 3116 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3117 ! has_pc_info, &cu);
57c22c6c 3118
93311388
DE
3119 /* If we didn't find a lowpc, set it to highpc to avoid
3120 complaints from `maint check'. */
3121 if (lowpc == ((CORE_ADDR) -1))
3122 lowpc = highpc;
10b3939b 3123
93311388
DE
3124 /* If the compilation unit didn't have an explicit address range,
3125 then use the information extracted from its child dies. */
d85a05f0 3126 if (! has_pc_info)
93311388 3127 {
d85a05f0
DJ
3128 best_lowpc = lowpc;
3129 best_highpc = highpc;
93311388
DE
3130 }
3131 }
d85a05f0
DJ
3132 pst->textlow = best_lowpc + baseaddr;
3133 pst->texthigh = best_highpc + baseaddr;
c906108c 3134
93311388
DE
3135 pst->n_global_syms = objfile->global_psymbols.next -
3136 (objfile->global_psymbols.list + pst->globals_offset);
3137 pst->n_static_syms = objfile->static_psymbols.next -
3138 (objfile->static_psymbols.list + pst->statics_offset);
3139 sort_pst_symbols (pst);
c906108c 3140
93311388
DE
3141 info_ptr = (beg_of_comp_unit + cu.header.length
3142 + cu.header.initial_length_size);
ae038cb0 3143
348e048f
DE
3144 if (this_cu->from_debug_types)
3145 {
3146 /* It's not clear we want to do anything with stmt lists here.
3147 Waiting to see what gcc ultimately does. */
3148 }
d85a05f0 3149 else
93311388
DE
3150 {
3151 /* Get the list of files included in the current compilation unit,
3152 and build a psymtab for each of them. */
d85a05f0 3153 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3154 }
ae038cb0 3155
93311388 3156 do_cleanups (back_to_inner);
ae038cb0 3157
93311388
DE
3158 return info_ptr;
3159}
ff013f42 3160
348e048f
DE
3161/* Traversal function for htab_traverse_noresize.
3162 Process one .debug_types comp-unit. */
3163
3164static int
3165process_type_comp_unit (void **slot, void *info)
3166{
3167 struct signatured_type *entry = (struct signatured_type *) *slot;
3168 struct objfile *objfile = (struct objfile *) info;
3169 struct dwarf2_per_cu_data *this_cu;
3170
3171 this_cu = &entry->per_cu;
348e048f 3172
be391dca 3173 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3174 process_psymtab_comp_unit (objfile, this_cu,
3175 dwarf2_per_objfile->types.buffer,
3176 dwarf2_per_objfile->types.buffer + entry->offset,
3177 dwarf2_per_objfile->types.size);
3178
3179 return 1;
3180}
3181
3182/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3183 Build partial symbol tables for the .debug_types comp-units. */
3184
3185static void
3186build_type_psymtabs (struct objfile *objfile)
3187{
3188 if (! create_debug_types_hash_table (objfile))
3189 return;
3190
3191 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3192 process_type_comp_unit, objfile);
3193}
3194
60606b2c
TT
3195/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3196
3197static void
3198psymtabs_addrmap_cleanup (void *o)
3199{
3200 struct objfile *objfile = o;
ec61707d 3201
60606b2c
TT
3202 objfile->psymtabs_addrmap = NULL;
3203}
3204
93311388
DE
3205/* Build the partial symbol table by doing a quick pass through the
3206 .debug_info and .debug_abbrev sections. */
72bf9492 3207
93311388 3208static void
c67a9c90 3209dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3210{
93311388 3211 gdb_byte *info_ptr;
60606b2c
TT
3212 struct cleanup *back_to, *addrmap_cleanup;
3213 struct obstack temp_obstack;
93311388 3214
98bfdba5
PA
3215 dwarf2_per_objfile->reading_partial_symbols = 1;
3216
be391dca 3217 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3218 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3219
93311388
DE
3220 /* Any cached compilation units will be linked by the per-objfile
3221 read_in_chain. Make sure to free them when we're done. */
3222 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3223
348e048f
DE
3224 build_type_psymtabs (objfile);
3225
93311388 3226 create_all_comp_units (objfile);
c906108c 3227
60606b2c
TT
3228 /* Create a temporary address map on a temporary obstack. We later
3229 copy this to the final obstack. */
3230 obstack_init (&temp_obstack);
3231 make_cleanup_obstack_free (&temp_obstack);
3232 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3233 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3234
93311388
DE
3235 /* Since the objects we're extracting from .debug_info vary in
3236 length, only the individual functions to extract them (like
3237 read_comp_unit_head and load_partial_die) can really know whether
3238 the buffer is large enough to hold another complete object.
c906108c 3239
93311388
DE
3240 At the moment, they don't actually check that. If .debug_info
3241 holds just one extra byte after the last compilation unit's dies,
3242 then read_comp_unit_head will happily read off the end of the
3243 buffer. read_partial_die is similarly casual. Those functions
3244 should be fixed.
c906108c 3245
93311388
DE
3246 For this loop condition, simply checking whether there's any data
3247 left at all should be sufficient. */
c906108c 3248
93311388
DE
3249 while (info_ptr < (dwarf2_per_objfile->info.buffer
3250 + dwarf2_per_objfile->info.size))
3251 {
3252 struct dwarf2_per_cu_data *this_cu;
dd373385 3253
93311388
DE
3254 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3255 objfile);
aaa75496 3256
93311388
DE
3257 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3258 dwarf2_per_objfile->info.buffer,
3259 info_ptr,
3260 dwarf2_per_objfile->info.size);
c906108c 3261 }
ff013f42
JK
3262
3263 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3264 &objfile->objfile_obstack);
60606b2c 3265 discard_cleanups (addrmap_cleanup);
ff013f42 3266
ae038cb0
DJ
3267 do_cleanups (back_to);
3268}
3269
93311388 3270/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3271
3272static void
93311388
DE
3273load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3274 struct objfile *objfile)
ae038cb0
DJ
3275{
3276 bfd *abfd = objfile->obfd;
fe1b8b76 3277 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3278 struct die_info *comp_unit_die;
ae038cb0 3279 struct dwarf2_cu *cu;
1d9ec526 3280 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3281 struct attribute *attr;
3282 int has_children;
3283 struct die_reader_specs reader_specs;
98bfdba5 3284 int read_cu = 0;
ae038cb0 3285
348e048f
DE
3286 gdb_assert (! this_cu->from_debug_types);
3287
be391dca 3288 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3289 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3290 beg_of_comp_unit = info_ptr;
3291
98bfdba5
PA
3292 if (this_cu->cu == NULL)
3293 {
3294 cu = alloc_one_comp_unit (objfile);
ae038cb0 3295
98bfdba5 3296 read_cu = 1;
ae038cb0 3297
98bfdba5
PA
3298 /* If an error occurs while loading, release our storage. */
3299 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3300
98bfdba5
PA
3301 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3302 dwarf2_per_objfile->info.buffer,
3303 dwarf2_per_objfile->info.size,
3304 abfd);
ae038cb0 3305
98bfdba5
PA
3306 /* Complete the cu_header. */
3307 cu->header.offset = this_cu->offset;
3308 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3309
3310 /* Link this compilation unit into the compilation unit tree. */
3311 this_cu->cu = cu;
3312 cu->per_cu = this_cu;
3313 cu->type_hash = this_cu->type_hash;
3314
3315 /* Link this CU into read_in_chain. */
3316 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3317 dwarf2_per_objfile->read_in_chain = this_cu;
3318 }
3319 else
3320 {
3321 cu = this_cu->cu;
3322 info_ptr += cu->header.first_die_offset;
3323 }
ae038cb0
DJ
3324
3325 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3326 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3327 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3328 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3329
3330 /* Read the compilation unit die. */
d85a05f0
DJ
3331 init_cu_die_reader (&reader_specs, cu);
3332 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3333 &has_children);
ae038cb0
DJ
3334
3335 /* Set the language we're debugging. */
d85a05f0
DJ
3336 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3337 if (attr)
3338 set_cu_language (DW_UNSND (attr), cu);
3339 else
3340 set_cu_language (language_minimal, cu);
ae038cb0 3341
ae038cb0
DJ
3342 /* Check if comp unit has_children.
3343 If so, read the rest of the partial symbols from this comp unit.
3344 If not, there's no more debug_info for this comp unit. */
d85a05f0 3345 if (has_children)
93311388 3346 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3347
98bfdba5
PA
3348 do_cleanups (free_abbrevs_cleanup);
3349
3350 if (read_cu)
3351 {
3352 /* We've successfully allocated this compilation unit. Let our
3353 caller clean it up when finished with it. */
3354 discard_cleanups (free_cu_cleanup);
3355 }
ae038cb0
DJ
3356}
3357
3358/* Create a list of all compilation units in OBJFILE. We do this only
3359 if an inter-comp-unit reference is found; presumably if there is one,
3360 there will be many, and one will occur early in the .debug_info section.
3361 So there's no point in building this list incrementally. */
3362
3363static void
3364create_all_comp_units (struct objfile *objfile)
3365{
3366 int n_allocated;
3367 int n_comp_units;
3368 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3369 gdb_byte *info_ptr;
3370
3371 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3372 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3373
3374 n_comp_units = 0;
3375 n_allocated = 10;
3376 all_comp_units = xmalloc (n_allocated
3377 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3378
dce234bc 3379 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 3380 {
c764a876 3381 unsigned int length, initial_length_size;
ae038cb0 3382 struct dwarf2_per_cu_data *this_cu;
c764a876 3383 unsigned int offset;
ae038cb0 3384
dce234bc 3385 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3386
3387 /* Read just enough information to find out where the next
3388 compilation unit is. */
c764a876
DE
3389 length = read_initial_length (objfile->obfd, info_ptr,
3390 &initial_length_size);
ae038cb0
DJ
3391
3392 /* Save the compilation unit for later lookup. */
3393 this_cu = obstack_alloc (&objfile->objfile_obstack,
3394 sizeof (struct dwarf2_per_cu_data));
3395 memset (this_cu, 0, sizeof (*this_cu));
3396 this_cu->offset = offset;
c764a876 3397 this_cu->length = length + initial_length_size;
9291a0cd 3398 this_cu->objfile = objfile;
ae038cb0
DJ
3399
3400 if (n_comp_units == n_allocated)
3401 {
3402 n_allocated *= 2;
3403 all_comp_units = xrealloc (all_comp_units,
3404 n_allocated
3405 * sizeof (struct dwarf2_per_cu_data *));
3406 }
3407 all_comp_units[n_comp_units++] = this_cu;
3408
3409 info_ptr = info_ptr + this_cu->length;
3410 }
3411
3412 dwarf2_per_objfile->all_comp_units
3413 = obstack_alloc (&objfile->objfile_obstack,
3414 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3415 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3416 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3417 xfree (all_comp_units);
3418 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3419}
3420
5734ee8b
DJ
3421/* Process all loaded DIEs for compilation unit CU, starting at
3422 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3423 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3424 DW_AT_ranges). If NEED_PC is set, then this function will set
3425 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3426 and record the covered ranges in the addrmap. */
c906108c 3427
72bf9492
DJ
3428static void
3429scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3430 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3431{
72bf9492 3432 struct partial_die_info *pdi;
c906108c 3433
91c24f0a
DC
3434 /* Now, march along the PDI's, descending into ones which have
3435 interesting children but skipping the children of the other ones,
3436 until we reach the end of the compilation unit. */
c906108c 3437
72bf9492 3438 pdi = first_die;
91c24f0a 3439
72bf9492
DJ
3440 while (pdi != NULL)
3441 {
3442 fixup_partial_die (pdi, cu);
c906108c 3443
f55ee35c 3444 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3445 children, so we need to look at them. Ditto for anonymous
3446 enums. */
933c6fe4 3447
72bf9492 3448 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3449 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3450 {
72bf9492 3451 switch (pdi->tag)
c906108c
SS
3452 {
3453 case DW_TAG_subprogram:
5734ee8b 3454 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c
SS
3455 break;
3456 case DW_TAG_variable:
3457 case DW_TAG_typedef:
91c24f0a 3458 case DW_TAG_union_type:
72bf9492 3459 if (!pdi->is_declaration)
63d06c5c 3460 {
72bf9492 3461 add_partial_symbol (pdi, cu);
63d06c5c
DC
3462 }
3463 break;
c906108c 3464 case DW_TAG_class_type:
680b30c7 3465 case DW_TAG_interface_type:
c906108c 3466 case DW_TAG_structure_type:
72bf9492 3467 if (!pdi->is_declaration)
c906108c 3468 {
72bf9492 3469 add_partial_symbol (pdi, cu);
c906108c
SS
3470 }
3471 break;
91c24f0a 3472 case DW_TAG_enumeration_type:
72bf9492
DJ
3473 if (!pdi->is_declaration)
3474 add_partial_enumeration (pdi, cu);
c906108c
SS
3475 break;
3476 case DW_TAG_base_type:
a02abb62 3477 case DW_TAG_subrange_type:
c906108c 3478 /* File scope base type definitions are added to the partial
c5aa993b 3479 symbol table. */
72bf9492 3480 add_partial_symbol (pdi, cu);
c906108c 3481 break;
d9fa45fe 3482 case DW_TAG_namespace:
5734ee8b 3483 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3484 break;
5d7cb8df
JK
3485 case DW_TAG_module:
3486 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3487 break;
c906108c
SS
3488 default:
3489 break;
3490 }
3491 }
3492
72bf9492
DJ
3493 /* If the die has a sibling, skip to the sibling. */
3494
3495 pdi = pdi->die_sibling;
3496 }
3497}
3498
3499/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3500
72bf9492 3501 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3502 name is concatenated with "::" and the partial DIE's name. For
3503 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3504 Enumerators are an exception; they use the scope of their parent
3505 enumeration type, i.e. the name of the enumeration type is not
3506 prepended to the enumerator.
91c24f0a 3507
72bf9492
DJ
3508 There are two complexities. One is DW_AT_specification; in this
3509 case "parent" means the parent of the target of the specification,
3510 instead of the direct parent of the DIE. The other is compilers
3511 which do not emit DW_TAG_namespace; in this case we try to guess
3512 the fully qualified name of structure types from their members'
3513 linkage names. This must be done using the DIE's children rather
3514 than the children of any DW_AT_specification target. We only need
3515 to do this for structures at the top level, i.e. if the target of
3516 any DW_AT_specification (if any; otherwise the DIE itself) does not
3517 have a parent. */
3518
3519/* Compute the scope prefix associated with PDI's parent, in
3520 compilation unit CU. The result will be allocated on CU's
3521 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3522 field. NULL is returned if no prefix is necessary. */
3523static char *
3524partial_die_parent_scope (struct partial_die_info *pdi,
3525 struct dwarf2_cu *cu)
3526{
3527 char *grandparent_scope;
3528 struct partial_die_info *parent, *real_pdi;
91c24f0a 3529
72bf9492
DJ
3530 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3531 then this means the parent of the specification DIE. */
3532
3533 real_pdi = pdi;
72bf9492 3534 while (real_pdi->has_specification)
10b3939b 3535 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3536
3537 parent = real_pdi->die_parent;
3538 if (parent == NULL)
3539 return NULL;
3540
3541 if (parent->scope_set)
3542 return parent->scope;
3543
3544 fixup_partial_die (parent, cu);
3545
10b3939b 3546 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3547
acebe513
UW
3548 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3549 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3550 Work around this problem here. */
3551 if (cu->language == language_cplus
6e70227d 3552 && parent->tag == DW_TAG_namespace
acebe513
UW
3553 && strcmp (parent->name, "::") == 0
3554 && grandparent_scope == NULL)
3555 {
3556 parent->scope = NULL;
3557 parent->scope_set = 1;
3558 return NULL;
3559 }
3560
72bf9492 3561 if (parent->tag == DW_TAG_namespace
f55ee35c 3562 || parent->tag == DW_TAG_module
72bf9492
DJ
3563 || parent->tag == DW_TAG_structure_type
3564 || parent->tag == DW_TAG_class_type
680b30c7 3565 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3566 || parent->tag == DW_TAG_union_type
3567 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3568 {
3569 if (grandparent_scope == NULL)
3570 parent->scope = parent->name;
3571 else
987504bb 3572 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
f55ee35c 3573 parent->name, 0, cu);
72bf9492 3574 }
ceeb3d5a 3575 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3576 /* Enumerators should not get the name of the enumeration as a prefix. */
3577 parent->scope = grandparent_scope;
3578 else
3579 {
3580 /* FIXME drow/2004-04-01: What should we be doing with
3581 function-local names? For partial symbols, we should probably be
3582 ignoring them. */
3583 complaint (&symfile_complaints,
e2e0b3e5 3584 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3585 parent->tag, pdi->offset);
3586 parent->scope = grandparent_scope;
c906108c
SS
3587 }
3588
72bf9492
DJ
3589 parent->scope_set = 1;
3590 return parent->scope;
3591}
3592
3593/* Return the fully scoped name associated with PDI, from compilation unit
3594 CU. The result will be allocated with malloc. */
3595static char *
3596partial_die_full_name (struct partial_die_info *pdi,
3597 struct dwarf2_cu *cu)
3598{
3599 char *parent_scope;
3600
98bfdba5
PA
3601 /* If this is a template instantiation, we can not work out the
3602 template arguments from partial DIEs. So, unfortunately, we have
3603 to go through the full DIEs. At least any work we do building
3604 types here will be reused if full symbols are loaded later. */
3605 if (pdi->has_template_arguments)
3606 {
3607 fixup_partial_die (pdi, cu);
3608
3609 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3610 {
3611 struct die_info *die;
3612 struct attribute attr;
3613 struct dwarf2_cu *ref_cu = cu;
3614
3615 attr.name = 0;
3616 attr.form = DW_FORM_ref_addr;
3617 attr.u.addr = pdi->offset;
3618 die = follow_die_ref (NULL, &attr, &ref_cu);
3619
3620 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3621 }
3622 }
3623
72bf9492
DJ
3624 parent_scope = partial_die_parent_scope (pdi, cu);
3625 if (parent_scope == NULL)
3626 return NULL;
3627 else
f55ee35c 3628 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3629}
3630
3631static void
72bf9492 3632add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3633{
e7c27a73 3634 struct objfile *objfile = cu->objfile;
c906108c 3635 CORE_ADDR addr = 0;
decbce07 3636 char *actual_name = NULL;
5c4e30ca 3637 const struct partial_symbol *psym = NULL;
e142c38c 3638 CORE_ADDR baseaddr;
72bf9492 3639 int built_actual_name = 0;
e142c38c
DJ
3640
3641 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3642
94af9270
KS
3643 actual_name = partial_die_full_name (pdi, cu);
3644 if (actual_name)
3645 built_actual_name = 1;
63d06c5c 3646
72bf9492
DJ
3647 if (actual_name == NULL)
3648 actual_name = pdi->name;
3649
c906108c
SS
3650 switch (pdi->tag)
3651 {
3652 case DW_TAG_subprogram:
2cfa0c8d 3653 if (pdi->is_external || cu->language == language_ada)
c906108c 3654 {
2cfa0c8d
JB
3655 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3656 of the global scope. But in Ada, we want to be able to access
3657 nested procedures globally. So all Ada subprograms are stored
3658 in the global scope. */
38d518c9 3659 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3660 mst_text, objfile); */
38d518c9 3661 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3662 built_actual_name,
5c4e30ca
DC
3663 VAR_DOMAIN, LOC_BLOCK,
3664 &objfile->global_psymbols,
3665 0, pdi->lowpc + baseaddr,
e142c38c 3666 cu->language, objfile);
c906108c
SS
3667 }
3668 else
3669 {
38d518c9 3670 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3671 mst_file_text, objfile); */
38d518c9 3672 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3673 built_actual_name,
5c4e30ca
DC
3674 VAR_DOMAIN, LOC_BLOCK,
3675 &objfile->static_psymbols,
3676 0, pdi->lowpc + baseaddr,
e142c38c 3677 cu->language, objfile);
c906108c
SS
3678 }
3679 break;
3680 case DW_TAG_variable:
caac4577
JG
3681 if (pdi->locdesc)
3682 addr = decode_locdesc (pdi->locdesc, cu);
3683
3684 if (pdi->locdesc
3685 && addr == 0
3686 && !dwarf2_per_objfile->has_section_at_zero)
3687 {
3688 /* A global or static variable may also have been stripped
3689 out by the linker if unused, in which case its address
3690 will be nullified; do not add such variables into partial
3691 symbol table then. */
3692 }
3693 else if (pdi->is_external)
c906108c
SS
3694 {
3695 /* Global Variable.
3696 Don't enter into the minimal symbol tables as there is
3697 a minimal symbol table entry from the ELF symbols already.
3698 Enter into partial symbol table if it has a location
3699 descriptor or a type.
3700 If the location descriptor is missing, new_symbol will create
3701 a LOC_UNRESOLVED symbol, the address of the variable will then
3702 be determined from the minimal symbol table whenever the variable
3703 is referenced.
3704 The address for the partial symbol table entry is not
3705 used by GDB, but it comes in handy for debugging partial symbol
3706 table building. */
3707
c906108c 3708 if (pdi->locdesc || pdi->has_type)
38d518c9 3709 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3710 built_actual_name,
5c4e30ca
DC
3711 VAR_DOMAIN, LOC_STATIC,
3712 &objfile->global_psymbols,
3713 0, addr + baseaddr,
e142c38c 3714 cu->language, objfile);
c906108c
SS
3715 }
3716 else
3717 {
3718 /* Static Variable. Skip symbols without location descriptors. */
3719 if (pdi->locdesc == NULL)
decbce07
MS
3720 {
3721 if (built_actual_name)
3722 xfree (actual_name);
3723 return;
3724 }
38d518c9 3725 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3726 mst_file_data, objfile); */
38d518c9 3727 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3728 built_actual_name,
5c4e30ca
DC
3729 VAR_DOMAIN, LOC_STATIC,
3730 &objfile->static_psymbols,
3731 0, addr + baseaddr,
e142c38c 3732 cu->language, objfile);
c906108c
SS
3733 }
3734 break;
3735 case DW_TAG_typedef:
3736 case DW_TAG_base_type:
a02abb62 3737 case DW_TAG_subrange_type:
38d518c9 3738 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3739 built_actual_name,
176620f1 3740 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3741 &objfile->static_psymbols,
e142c38c 3742 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3743 break;
72bf9492
DJ
3744 case DW_TAG_namespace:
3745 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3746 built_actual_name,
72bf9492
DJ
3747 VAR_DOMAIN, LOC_TYPEDEF,
3748 &objfile->global_psymbols,
3749 0, (CORE_ADDR) 0, cu->language, objfile);
3750 break;
c906108c 3751 case DW_TAG_class_type:
680b30c7 3752 case DW_TAG_interface_type:
c906108c
SS
3753 case DW_TAG_structure_type:
3754 case DW_TAG_union_type:
3755 case DW_TAG_enumeration_type:
fa4028e9
JB
3756 /* Skip external references. The DWARF standard says in the section
3757 about "Structure, Union, and Class Type Entries": "An incomplete
3758 structure, union or class type is represented by a structure,
3759 union or class entry that does not have a byte size attribute
3760 and that has a DW_AT_declaration attribute." */
3761 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3762 {
3763 if (built_actual_name)
3764 xfree (actual_name);
3765 return;
3766 }
fa4028e9 3767
63d06c5c
DC
3768 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3769 static vs. global. */
38d518c9 3770 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3771 built_actual_name,
176620f1 3772 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3773 (cu->language == language_cplus
3774 || cu->language == language_java)
63d06c5c
DC
3775 ? &objfile->global_psymbols
3776 : &objfile->static_psymbols,
e142c38c 3777 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3778
c906108c
SS
3779 break;
3780 case DW_TAG_enumerator:
38d518c9 3781 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3782 built_actual_name,
176620f1 3783 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3784 (cu->language == language_cplus
3785 || cu->language == language_java)
f6fe98ef
DJ
3786 ? &objfile->global_psymbols
3787 : &objfile->static_psymbols,
e142c38c 3788 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3789 break;
3790 default:
3791 break;
3792 }
5c4e30ca 3793
72bf9492
DJ
3794 if (built_actual_name)
3795 xfree (actual_name);
c906108c
SS
3796}
3797
5c4e30ca
DC
3798/* Read a partial die corresponding to a namespace; also, add a symbol
3799 corresponding to that namespace to the symbol table. NAMESPACE is
3800 the name of the enclosing namespace. */
91c24f0a 3801
72bf9492
DJ
3802static void
3803add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3804 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3805 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3806{
72bf9492 3807 /* Add a symbol for the namespace. */
e7c27a73 3808
72bf9492 3809 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3810
3811 /* Now scan partial symbols in that namespace. */
3812
91c24f0a 3813 if (pdi->has_children)
5734ee8b 3814 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3815}
3816
5d7cb8df
JK
3817/* Read a partial die corresponding to a Fortran module. */
3818
3819static void
3820add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3821 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3822{
f55ee35c 3823 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3824
3825 if (pdi->has_children)
3826 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3827}
3828
bc30ff58
JB
3829/* Read a partial die corresponding to a subprogram and create a partial
3830 symbol for that subprogram. When the CU language allows it, this
3831 routine also defines a partial symbol for each nested subprogram
3832 that this subprogram contains.
6e70227d 3833
bc30ff58
JB
3834 DIE my also be a lexical block, in which case we simply search
3835 recursively for suprograms defined inside that lexical block.
3836 Again, this is only performed when the CU language allows this
3837 type of definitions. */
3838
3839static void
3840add_partial_subprogram (struct partial_die_info *pdi,
3841 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3842 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
3843{
3844 if (pdi->tag == DW_TAG_subprogram)
3845 {
3846 if (pdi->has_pc_info)
3847 {
3848 if (pdi->lowpc < *lowpc)
3849 *lowpc = pdi->lowpc;
3850 if (pdi->highpc > *highpc)
3851 *highpc = pdi->highpc;
5734ee8b
DJ
3852 if (need_pc)
3853 {
3854 CORE_ADDR baseaddr;
3855 struct objfile *objfile = cu->objfile;
3856
3857 baseaddr = ANOFFSET (objfile->section_offsets,
3858 SECT_OFF_TEXT (objfile));
3859 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
3860 pdi->lowpc + baseaddr,
3861 pdi->highpc - 1 + baseaddr,
9291a0cd 3862 cu->per_cu->v.psymtab);
5734ee8b 3863 }
bc30ff58 3864 if (!pdi->is_declaration)
e8d05480
JB
3865 /* Ignore subprogram DIEs that do not have a name, they are
3866 illegal. Do not emit a complaint at this point, we will
3867 do so when we convert this psymtab into a symtab. */
3868 if (pdi->name)
3869 add_partial_symbol (pdi, cu);
bc30ff58
JB
3870 }
3871 }
6e70227d 3872
bc30ff58
JB
3873 if (! pdi->has_children)
3874 return;
3875
3876 if (cu->language == language_ada)
3877 {
3878 pdi = pdi->die_child;
3879 while (pdi != NULL)
3880 {
3881 fixup_partial_die (pdi, cu);
3882 if (pdi->tag == DW_TAG_subprogram
3883 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 3884 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
3885 pdi = pdi->die_sibling;
3886 }
3887 }
3888}
3889
72bf9492
DJ
3890/* See if we can figure out if the class lives in a namespace. We do
3891 this by looking for a member function; its demangled name will
3892 contain namespace info, if there is any. */
63d06c5c 3893
72bf9492
DJ
3894static void
3895guess_structure_name (struct partial_die_info *struct_pdi,
3896 struct dwarf2_cu *cu)
63d06c5c 3897{
987504bb
JJ
3898 if ((cu->language == language_cplus
3899 || cu->language == language_java)
72bf9492 3900 && cu->has_namespace_info == 0
63d06c5c
DC
3901 && struct_pdi->has_children)
3902 {
63d06c5c
DC
3903 /* NOTE: carlton/2003-10-07: Getting the info this way changes
3904 what template types look like, because the demangler
3905 frequently doesn't give the same name as the debug info. We
3906 could fix this by only using the demangled name to get the
134d01f1 3907 prefix (but see comment in read_structure_type). */
63d06c5c 3908
72bf9492 3909 struct partial_die_info *real_pdi;
5d51ca54 3910
72bf9492
DJ
3911 /* If this DIE (this DIE's specification, if any) has a parent, then
3912 we should not do this. We'll prepend the parent's fully qualified
3913 name when we create the partial symbol. */
5d51ca54 3914
72bf9492 3915 real_pdi = struct_pdi;
72bf9492 3916 while (real_pdi->has_specification)
10b3939b 3917 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
63d06c5c 3918
72bf9492
DJ
3919 if (real_pdi->die_parent != NULL)
3920 return;
63d06c5c 3921 }
63d06c5c
DC
3922}
3923
91c24f0a
DC
3924/* Read a partial die corresponding to an enumeration type. */
3925
72bf9492
DJ
3926static void
3927add_partial_enumeration (struct partial_die_info *enum_pdi,
3928 struct dwarf2_cu *cu)
91c24f0a 3929{
72bf9492 3930 struct partial_die_info *pdi;
91c24f0a
DC
3931
3932 if (enum_pdi->name != NULL)
72bf9492
DJ
3933 add_partial_symbol (enum_pdi, cu);
3934
3935 pdi = enum_pdi->die_child;
3936 while (pdi)
91c24f0a 3937 {
72bf9492 3938 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 3939 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 3940 else
72bf9492
DJ
3941 add_partial_symbol (pdi, cu);
3942 pdi = pdi->die_sibling;
91c24f0a 3943 }
91c24f0a
DC
3944}
3945
4bb7a0a7
DJ
3946/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3947 Return the corresponding abbrev, or NULL if the number is zero (indicating
3948 an empty DIE). In either case *BYTES_READ will be set to the length of
3949 the initial number. */
3950
3951static struct abbrev_info *
fe1b8b76 3952peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 3953 struct dwarf2_cu *cu)
4bb7a0a7
DJ
3954{
3955 bfd *abfd = cu->objfile->obfd;
3956 unsigned int abbrev_number;
3957 struct abbrev_info *abbrev;
3958
3959 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3960
3961 if (abbrev_number == 0)
3962 return NULL;
3963
3964 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3965 if (!abbrev)
3966 {
8a3fe4f8 3967 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
3968 bfd_get_filename (abfd));
3969 }
3970
3971 return abbrev;
3972}
3973
93311388
DE
3974/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3975 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
3976 DIE. Any children of the skipped DIEs will also be skipped. */
3977
fe1b8b76 3978static gdb_byte *
93311388 3979skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3980{
3981 struct abbrev_info *abbrev;
3982 unsigned int bytes_read;
3983
3984 while (1)
3985 {
3986 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3987 if (abbrev == NULL)
3988 return info_ptr + bytes_read;
3989 else
93311388 3990 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
3991 }
3992}
3993
93311388
DE
3994/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3995 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
3996 abbrev corresponding to that skipped uleb128 should be passed in
3997 ABBREV. Returns a pointer to this DIE's sibling, skipping any
3998 children. */
3999
fe1b8b76 4000static gdb_byte *
93311388
DE
4001skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4002 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4003{
4004 unsigned int bytes_read;
4005 struct attribute attr;
4006 bfd *abfd = cu->objfile->obfd;
4007 unsigned int form, i;
4008
4009 for (i = 0; i < abbrev->num_attrs; i++)
4010 {
4011 /* The only abbrev we care about is DW_AT_sibling. */
4012 if (abbrev->attrs[i].name == DW_AT_sibling)
4013 {
4014 read_attribute (&attr, &abbrev->attrs[i],
4015 abfd, info_ptr, cu);
4016 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 4017 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4018 else
93311388 4019 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4020 }
4021
4022 /* If it isn't DW_AT_sibling, skip this attribute. */
4023 form = abbrev->attrs[i].form;
4024 skip_attribute:
4025 switch (form)
4026 {
4bb7a0a7 4027 case DW_FORM_ref_addr:
ae411497
TT
4028 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4029 and later it is offset sized. */
4030 if (cu->header.version == 2)
4031 info_ptr += cu->header.addr_size;
4032 else
4033 info_ptr += cu->header.offset_size;
4034 break;
4035 case DW_FORM_addr:
4bb7a0a7
DJ
4036 info_ptr += cu->header.addr_size;
4037 break;
4038 case DW_FORM_data1:
4039 case DW_FORM_ref1:
4040 case DW_FORM_flag:
4041 info_ptr += 1;
4042 break;
2dc7f7b3
TT
4043 case DW_FORM_flag_present:
4044 break;
4bb7a0a7
DJ
4045 case DW_FORM_data2:
4046 case DW_FORM_ref2:
4047 info_ptr += 2;
4048 break;
4049 case DW_FORM_data4:
4050 case DW_FORM_ref4:
4051 info_ptr += 4;
4052 break;
4053 case DW_FORM_data8:
4054 case DW_FORM_ref8:
348e048f 4055 case DW_FORM_sig8:
4bb7a0a7
DJ
4056 info_ptr += 8;
4057 break;
4058 case DW_FORM_string:
9b1c24c8 4059 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4060 info_ptr += bytes_read;
4061 break;
2dc7f7b3 4062 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4063 case DW_FORM_strp:
4064 info_ptr += cu->header.offset_size;
4065 break;
2dc7f7b3 4066 case DW_FORM_exprloc:
4bb7a0a7
DJ
4067 case DW_FORM_block:
4068 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4069 info_ptr += bytes_read;
4070 break;
4071 case DW_FORM_block1:
4072 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4073 break;
4074 case DW_FORM_block2:
4075 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4076 break;
4077 case DW_FORM_block4:
4078 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4079 break;
4080 case DW_FORM_sdata:
4081 case DW_FORM_udata:
4082 case DW_FORM_ref_udata:
4083 info_ptr = skip_leb128 (abfd, info_ptr);
4084 break;
4085 case DW_FORM_indirect:
4086 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4087 info_ptr += bytes_read;
4088 /* We need to continue parsing from here, so just go back to
4089 the top. */
4090 goto skip_attribute;
4091
4092 default:
8a3fe4f8 4093 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4094 dwarf_form_name (form),
4095 bfd_get_filename (abfd));
4096 }
4097 }
4098
4099 if (abbrev->has_children)
93311388 4100 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4101 else
4102 return info_ptr;
4103}
4104
93311388
DE
4105/* Locate ORIG_PDI's sibling.
4106 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4107 in BUFFER. */
91c24f0a 4108
fe1b8b76 4109static gdb_byte *
93311388
DE
4110locate_pdi_sibling (struct partial_die_info *orig_pdi,
4111 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4112 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4113{
4114 /* Do we know the sibling already? */
72bf9492 4115
91c24f0a
DC
4116 if (orig_pdi->sibling)
4117 return orig_pdi->sibling;
4118
4119 /* Are there any children to deal with? */
4120
4121 if (!orig_pdi->has_children)
4122 return info_ptr;
4123
4bb7a0a7 4124 /* Skip the children the long way. */
91c24f0a 4125
93311388 4126 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4127}
4128
c906108c
SS
4129/* Expand this partial symbol table into a full symbol table. */
4130
4131static void
fba45db2 4132dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4133{
c906108c
SS
4134 if (pst != NULL)
4135 {
4136 if (pst->readin)
4137 {
8a3fe4f8 4138 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
4139 }
4140 else
4141 {
4142 if (info_verbose)
4143 {
a3f17187 4144 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
4145 gdb_flush (gdb_stdout);
4146 }
4147
10b3939b
DJ
4148 /* Restore our global data. */
4149 dwarf2_per_objfile = objfile_data (pst->objfile,
4150 dwarf2_objfile_data_key);
4151
b2ab525c
KB
4152 /* If this psymtab is constructed from a debug-only objfile, the
4153 has_section_at_zero flag will not necessarily be correct. We
4154 can get the correct value for this flag by looking at the data
4155 associated with the (presumably stripped) associated objfile. */
4156 if (pst->objfile->separate_debug_objfile_backlink)
4157 {
4158 struct dwarf2_per_objfile *dpo_backlink
4159 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4160 dwarf2_objfile_data_key);
9a619af0 4161
b2ab525c
KB
4162 dwarf2_per_objfile->has_section_at_zero
4163 = dpo_backlink->has_section_at_zero;
4164 }
4165
98bfdba5
PA
4166 dwarf2_per_objfile->reading_partial_symbols = 0;
4167
c906108c
SS
4168 psymtab_to_symtab_1 (pst);
4169
4170 /* Finish up the debug error message. */
4171 if (info_verbose)
a3f17187 4172 printf_filtered (_("done.\n"));
c906108c
SS
4173 }
4174 }
4175}
4176
10b3939b
DJ
4177/* Add PER_CU to the queue. */
4178
4179static void
03dd20cc 4180queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4181{
4182 struct dwarf2_queue_item *item;
4183
4184 per_cu->queued = 1;
4185 item = xmalloc (sizeof (*item));
4186 item->per_cu = per_cu;
4187 item->next = NULL;
4188
4189 if (dwarf2_queue == NULL)
4190 dwarf2_queue = item;
4191 else
4192 dwarf2_queue_tail->next = item;
4193
4194 dwarf2_queue_tail = item;
4195}
4196
4197/* Process the queue. */
4198
4199static void
4200process_queue (struct objfile *objfile)
4201{
4202 struct dwarf2_queue_item *item, *next_item;
4203
03dd20cc
DJ
4204 /* The queue starts out with one item, but following a DIE reference
4205 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4206 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4207 {
9291a0cd
TT
4208 if (dwarf2_per_objfile->using_index
4209 ? !item->per_cu->v.quick->symtab
4210 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4211 process_full_comp_unit (item->per_cu);
4212
4213 item->per_cu->queued = 0;
4214 next_item = item->next;
4215 xfree (item);
4216 }
4217
4218 dwarf2_queue_tail = NULL;
4219}
4220
4221/* Free all allocated queue entries. This function only releases anything if
4222 an error was thrown; if the queue was processed then it would have been
4223 freed as we went along. */
4224
4225static void
4226dwarf2_release_queue (void *dummy)
4227{
4228 struct dwarf2_queue_item *item, *last;
4229
4230 item = dwarf2_queue;
4231 while (item)
4232 {
4233 /* Anything still marked queued is likely to be in an
4234 inconsistent state, so discard it. */
4235 if (item->per_cu->queued)
4236 {
4237 if (item->per_cu->cu != NULL)
4238 free_one_cached_comp_unit (item->per_cu->cu);
4239 item->per_cu->queued = 0;
4240 }
4241
4242 last = item;
4243 item = item->next;
4244 xfree (last);
4245 }
4246
4247 dwarf2_queue = dwarf2_queue_tail = NULL;
4248}
4249
4250/* Read in full symbols for PST, and anything it depends on. */
4251
c906108c 4252static void
fba45db2 4253psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4254{
10b3939b 4255 struct dwarf2_per_cu_data *per_cu;
c906108c 4256 struct cleanup *back_to;
aaa75496
JB
4257 int i;
4258
4259 for (i = 0; i < pst->number_of_dependencies; i++)
4260 if (!pst->dependencies[i]->readin)
4261 {
4262 /* Inform about additional files that need to be read in. */
4263 if (info_verbose)
4264 {
a3f17187 4265 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4266 fputs_filtered (" ", gdb_stdout);
4267 wrap_here ("");
4268 fputs_filtered ("and ", gdb_stdout);
4269 wrap_here ("");
4270 printf_filtered ("%s...", pst->dependencies[i]->filename);
4271 wrap_here (""); /* Flush output */
4272 gdb_flush (gdb_stdout);
4273 }
4274 psymtab_to_symtab_1 (pst->dependencies[i]);
4275 }
4276
e38df1d0 4277 per_cu = pst->read_symtab_private;
10b3939b
DJ
4278
4279 if (per_cu == NULL)
aaa75496
JB
4280 {
4281 /* It's an include file, no symbols to read for it.
4282 Everything is in the parent symtab. */
4283 pst->readin = 1;
4284 return;
4285 }
c906108c 4286
9291a0cd 4287 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4288}
4289
93311388 4290/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4291
93311388 4292static void
31ffec48 4293load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 4294{
31ffec48 4295 bfd *abfd = objfile->obfd;
10b3939b 4296 struct dwarf2_cu *cu;
c764a876 4297 unsigned int offset;
93311388 4298 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4299 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4300 struct attribute *attr;
98bfdba5 4301 int read_cu = 0;
6502dd73 4302
348e048f
DE
4303 gdb_assert (! per_cu->from_debug_types);
4304
c906108c 4305 /* Set local variables from the partial symbol table info. */
10b3939b 4306 offset = per_cu->offset;
6502dd73 4307
be391dca 4308 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4309 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4310 beg_of_comp_unit = info_ptr;
63d06c5c 4311
98bfdba5
PA
4312 if (per_cu->cu == NULL)
4313 {
4314 cu = alloc_one_comp_unit (objfile);
4315
4316 read_cu = 1;
c906108c 4317
98bfdba5
PA
4318 /* If an error occurs while loading, release our storage. */
4319 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4320
98bfdba5
PA
4321 /* Read in the comp_unit header. */
4322 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4323
98bfdba5
PA
4324 /* Complete the cu_header. */
4325 cu->header.offset = offset;
4326 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4327
98bfdba5
PA
4328 /* Read the abbrevs for this compilation unit. */
4329 dwarf2_read_abbrevs (abfd, cu);
4330 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4331
98bfdba5
PA
4332 /* Link this compilation unit into the compilation unit tree. */
4333 per_cu->cu = cu;
4334 cu->per_cu = per_cu;
4335 cu->type_hash = per_cu->type_hash;
4336
4337 /* Link this CU into read_in_chain. */
4338 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4339 dwarf2_per_objfile->read_in_chain = per_cu;
4340 }
4341 else
4342 {
4343 cu = per_cu->cu;
4344 info_ptr += cu->header.first_die_offset;
4345 }
e142c38c 4346
93311388 4347 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4348
4349 /* We try not to read any attributes in this function, because not
4350 all objfiles needed for references have been loaded yet, and symbol
4351 table processing isn't initialized. But we have to set the CU language,
4352 or we won't be able to build types correctly. */
4353 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4354 if (attr)
4355 set_cu_language (DW_UNSND (attr), cu);
4356 else
4357 set_cu_language (language_minimal, cu);
4358
a6c727b2
DJ
4359 /* Similarly, if we do not read the producer, we can not apply
4360 producer-specific interpretation. */
4361 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4362 if (attr)
4363 cu->producer = DW_STRING (attr);
4364
98bfdba5
PA
4365 if (read_cu)
4366 {
4367 do_cleanups (free_abbrevs_cleanup);
e142c38c 4368
98bfdba5
PA
4369 /* We've successfully allocated this compilation unit. Let our
4370 caller clean it up when finished with it. */
4371 discard_cleanups (free_cu_cleanup);
4372 }
10b3939b
DJ
4373}
4374
3da10d80
KS
4375/* Add a DIE to the delayed physname list. */
4376
4377static void
4378add_to_method_list (struct type *type, int fnfield_index, int index,
4379 const char *name, struct die_info *die,
4380 struct dwarf2_cu *cu)
4381{
4382 struct delayed_method_info mi;
4383 mi.type = type;
4384 mi.fnfield_index = fnfield_index;
4385 mi.index = index;
4386 mi.name = name;
4387 mi.die = die;
4388 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4389}
4390
4391/* A cleanup for freeing the delayed method list. */
4392
4393static void
4394free_delayed_list (void *ptr)
4395{
4396 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4397 if (cu->method_list != NULL)
4398 {
4399 VEC_free (delayed_method_info, cu->method_list);
4400 cu->method_list = NULL;
4401 }
4402}
4403
4404/* Compute the physnames of any methods on the CU's method list.
4405
4406 The computation of method physnames is delayed in order to avoid the
4407 (bad) condition that one of the method's formal parameters is of an as yet
4408 incomplete type. */
4409
4410static void
4411compute_delayed_physnames (struct dwarf2_cu *cu)
4412{
4413 int i;
4414 struct delayed_method_info *mi;
4415 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4416 {
4417 char *physname;
4418 struct fn_fieldlist *fn_flp
4419 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4420 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4421 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4422 }
4423}
4424
10b3939b
DJ
4425/* Generate full symbol information for PST and CU, whose DIEs have
4426 already been loaded into memory. */
4427
4428static void
4429process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4430{
10b3939b 4431 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4432 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4433 CORE_ADDR lowpc, highpc;
4434 struct symtab *symtab;
3da10d80 4435 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4436 CORE_ADDR baseaddr;
4437
4438 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4439
10b3939b
DJ
4440 buildsym_init ();
4441 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4442 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4443
4444 cu->list_in_scope = &file_symbols;
c906108c 4445
d85a05f0 4446 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4447
c906108c 4448 /* Do line number decoding in read_file_scope () */
10b3939b 4449 process_die (cu->dies, cu);
c906108c 4450
3da10d80
KS
4451 /* Now that we have processed all the DIEs in the CU, all the types
4452 should be complete, and it should now be safe to compute all of the
4453 physnames. */
4454 compute_delayed_physnames (cu);
4455 do_cleanups (delayed_list_cleanup);
4456
fae299cd
DC
4457 /* Some compilers don't define a DW_AT_high_pc attribute for the
4458 compilation unit. If the DW_AT_high_pc is missing, synthesize
4459 it, by scanning the DIE's below the compilation unit. */
10b3939b 4460 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4461
613e1657 4462 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4463
4464 /* Set symtab language to language from DW_AT_language.
4465 If the compilation is from a C file generated by language preprocessors,
4466 do not set the language if it was already deduced by start_subfile. */
4467 if (symtab != NULL
10b3939b 4468 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4469 {
10b3939b 4470 symtab->language = cu->language;
c906108c 4471 }
9291a0cd
TT
4472
4473 if (dwarf2_per_objfile->using_index)
4474 per_cu->v.quick->symtab = symtab;
4475 else
4476 {
4477 struct partial_symtab *pst = per_cu->v.psymtab;
4478 pst->symtab = symtab;
4479 pst->readin = 1;
4480 }
c906108c
SS
4481
4482 do_cleanups (back_to);
4483}
4484
4485/* Process a die and its children. */
4486
4487static void
e7c27a73 4488process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4489{
4490 switch (die->tag)
4491 {
4492 case DW_TAG_padding:
4493 break;
4494 case DW_TAG_compile_unit:
e7c27a73 4495 read_file_scope (die, cu);
c906108c 4496 break;
348e048f
DE
4497 case DW_TAG_type_unit:
4498 read_type_unit_scope (die, cu);
4499 break;
c906108c 4500 case DW_TAG_subprogram:
c906108c 4501 case DW_TAG_inlined_subroutine:
edb3359d 4502 read_func_scope (die, cu);
c906108c
SS
4503 break;
4504 case DW_TAG_lexical_block:
14898363
L
4505 case DW_TAG_try_block:
4506 case DW_TAG_catch_block:
e7c27a73 4507 read_lexical_block_scope (die, cu);
c906108c
SS
4508 break;
4509 case DW_TAG_class_type:
680b30c7 4510 case DW_TAG_interface_type:
c906108c
SS
4511 case DW_TAG_structure_type:
4512 case DW_TAG_union_type:
134d01f1 4513 process_structure_scope (die, cu);
c906108c
SS
4514 break;
4515 case DW_TAG_enumeration_type:
134d01f1 4516 process_enumeration_scope (die, cu);
c906108c 4517 break;
134d01f1 4518
f792889a
DJ
4519 /* These dies have a type, but processing them does not create
4520 a symbol or recurse to process the children. Therefore we can
4521 read them on-demand through read_type_die. */
c906108c 4522 case DW_TAG_subroutine_type:
72019c9c 4523 case DW_TAG_set_type:
c906108c 4524 case DW_TAG_array_type:
c906108c 4525 case DW_TAG_pointer_type:
c906108c 4526 case DW_TAG_ptr_to_member_type:
c906108c 4527 case DW_TAG_reference_type:
c906108c 4528 case DW_TAG_string_type:
c906108c 4529 break;
134d01f1 4530
c906108c 4531 case DW_TAG_base_type:
a02abb62 4532 case DW_TAG_subrange_type:
cb249c71 4533 case DW_TAG_typedef:
134d01f1
DJ
4534 /* Add a typedef symbol for the type definition, if it has a
4535 DW_AT_name. */
f792889a 4536 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4537 break;
c906108c 4538 case DW_TAG_common_block:
e7c27a73 4539 read_common_block (die, cu);
c906108c
SS
4540 break;
4541 case DW_TAG_common_inclusion:
4542 break;
d9fa45fe 4543 case DW_TAG_namespace:
63d06c5c 4544 processing_has_namespace_info = 1;
e7c27a73 4545 read_namespace (die, cu);
d9fa45fe 4546 break;
5d7cb8df 4547 case DW_TAG_module:
f55ee35c 4548 processing_has_namespace_info = 1;
5d7cb8df
JK
4549 read_module (die, cu);
4550 break;
d9fa45fe
DC
4551 case DW_TAG_imported_declaration:
4552 case DW_TAG_imported_module:
63d06c5c 4553 processing_has_namespace_info = 1;
27aa8d6a
SW
4554 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4555 || cu->language != language_fortran))
4556 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4557 dwarf_tag_name (die->tag));
4558 read_import_statement (die, cu);
d9fa45fe 4559 break;
c906108c 4560 default:
e7c27a73 4561 new_symbol (die, NULL, cu);
c906108c
SS
4562 break;
4563 }
4564}
4565
94af9270
KS
4566/* A helper function for dwarf2_compute_name which determines whether DIE
4567 needs to have the name of the scope prepended to the name listed in the
4568 die. */
4569
4570static int
4571die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4572{
1c809c68
TT
4573 struct attribute *attr;
4574
94af9270
KS
4575 switch (die->tag)
4576 {
4577 case DW_TAG_namespace:
4578 case DW_TAG_typedef:
4579 case DW_TAG_class_type:
4580 case DW_TAG_interface_type:
4581 case DW_TAG_structure_type:
4582 case DW_TAG_union_type:
4583 case DW_TAG_enumeration_type:
4584 case DW_TAG_enumerator:
4585 case DW_TAG_subprogram:
4586 case DW_TAG_member:
4587 return 1;
4588
4589 case DW_TAG_variable:
4590 /* We only need to prefix "globally" visible variables. These include
4591 any variable marked with DW_AT_external or any variable that
4592 lives in a namespace. [Variables in anonymous namespaces
4593 require prefixing, but they are not DW_AT_external.] */
4594
4595 if (dwarf2_attr (die, DW_AT_specification, cu))
4596 {
4597 struct dwarf2_cu *spec_cu = cu;
9a619af0 4598
94af9270
KS
4599 return die_needs_namespace (die_specification (die, &spec_cu),
4600 spec_cu);
4601 }
4602
1c809c68 4603 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4604 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4605 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4606 return 0;
4607 /* A variable in a lexical block of some kind does not need a
4608 namespace, even though in C++ such variables may be external
4609 and have a mangled name. */
4610 if (die->parent->tag == DW_TAG_lexical_block
4611 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4612 || die->parent->tag == DW_TAG_catch_block
4613 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4614 return 0;
4615 return 1;
94af9270
KS
4616
4617 default:
4618 return 0;
4619 }
4620}
4621
98bfdba5
PA
4622/* Retrieve the last character from a mem_file. */
4623
4624static void
4625do_ui_file_peek_last (void *object, const char *buffer, long length)
4626{
4627 char *last_char_p = (char *) object;
4628
4629 if (length > 0)
4630 *last_char_p = buffer[length - 1];
4631}
4632
94af9270
KS
4633/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4634 compute the physname for the object, which include a method's
4635 formal parameters (C++/Java) and return type (Java).
4636
af6b7be1
JB
4637 For Ada, return the DIE's linkage name rather than the fully qualified
4638 name. PHYSNAME is ignored..
4639
94af9270
KS
4640 The result is allocated on the objfile_obstack and canonicalized. */
4641
4642static const char *
4643dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4644 int physname)
4645{
4646 if (name == NULL)
4647 name = dwarf2_name (die, cu);
4648
f55ee35c
JK
4649 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4650 compute it by typename_concat inside GDB. */
4651 if (cu->language == language_ada
4652 || (cu->language == language_fortran && physname))
4653 {
4654 /* For Ada unit, we prefer the linkage name over the name, as
4655 the former contains the exported name, which the user expects
4656 to be able to reference. Ideally, we want the user to be able
4657 to reference this entity using either natural or linkage name,
4658 but we haven't started looking at this enhancement yet. */
4659 struct attribute *attr;
4660
4661 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4662 if (attr == NULL)
4663 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4664 if (attr && DW_STRING (attr))
4665 return DW_STRING (attr);
4666 }
4667
94af9270
KS
4668 /* These are the only languages we know how to qualify names in. */
4669 if (name != NULL
f55ee35c
JK
4670 && (cu->language == language_cplus || cu->language == language_java
4671 || cu->language == language_fortran))
94af9270
KS
4672 {
4673 if (die_needs_namespace (die, cu))
4674 {
4675 long length;
4676 char *prefix;
4677 struct ui_file *buf;
4678
4679 prefix = determine_prefix (die, cu);
4680 buf = mem_fileopen ();
4681 if (*prefix != '\0')
4682 {
f55ee35c
JK
4683 char *prefixed_name = typename_concat (NULL, prefix, name,
4684 physname, cu);
9a619af0 4685
94af9270
KS
4686 fputs_unfiltered (prefixed_name, buf);
4687 xfree (prefixed_name);
4688 }
4689 else
4690 fputs_unfiltered (name ? name : "", buf);
4691
98bfdba5
PA
4692 /* Template parameters may be specified in the DIE's DW_AT_name, or
4693 as children with DW_TAG_template_type_param or
4694 DW_TAG_value_type_param. If the latter, add them to the name
4695 here. If the name already has template parameters, then
4696 skip this step; some versions of GCC emit both, and
4697 it is more efficient to use the pre-computed name.
4698
4699 Something to keep in mind about this process: it is very
4700 unlikely, or in some cases downright impossible, to produce
4701 something that will match the mangled name of a function.
4702 If the definition of the function has the same debug info,
4703 we should be able to match up with it anyway. But fallbacks
4704 using the minimal symbol, for instance to find a method
4705 implemented in a stripped copy of libstdc++, will not work.
4706 If we do not have debug info for the definition, we will have to
4707 match them up some other way.
4708
4709 When we do name matching there is a related problem with function
4710 templates; two instantiated function templates are allowed to
4711 differ only by their return types, which we do not add here. */
4712
4713 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4714 {
4715 struct attribute *attr;
4716 struct die_info *child;
4717 int first = 1;
4718
4719 die->building_fullname = 1;
4720
4721 for (child = die->child; child != NULL; child = child->sibling)
4722 {
4723 struct type *type;
4724 long value;
4725 gdb_byte *bytes;
4726 struct dwarf2_locexpr_baton *baton;
4727 struct value *v;
4728
4729 if (child->tag != DW_TAG_template_type_param
4730 && child->tag != DW_TAG_template_value_param)
4731 continue;
4732
4733 if (first)
4734 {
4735 fputs_unfiltered ("<", buf);
4736 first = 0;
4737 }
4738 else
4739 fputs_unfiltered (", ", buf);
4740
4741 attr = dwarf2_attr (child, DW_AT_type, cu);
4742 if (attr == NULL)
4743 {
4744 complaint (&symfile_complaints,
4745 _("template parameter missing DW_AT_type"));
4746 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4747 continue;
4748 }
4749 type = die_type (child, cu);
4750
4751 if (child->tag == DW_TAG_template_type_param)
4752 {
4753 c_print_type (type, "", buf, -1, 0);
4754 continue;
4755 }
4756
4757 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4758 if (attr == NULL)
4759 {
4760 complaint (&symfile_complaints,
4761 _("template parameter missing DW_AT_const_value"));
4762 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4763 continue;
4764 }
4765
4766 dwarf2_const_value_attr (attr, type, name,
4767 &cu->comp_unit_obstack, cu,
4768 &value, &bytes, &baton);
4769
4770 if (TYPE_NOSIGN (type))
4771 /* GDB prints characters as NUMBER 'CHAR'. If that's
4772 changed, this can use value_print instead. */
4773 c_printchar (value, type, buf);
4774 else
4775 {
4776 struct value_print_options opts;
4777
4778 if (baton != NULL)
4779 v = dwarf2_evaluate_loc_desc (type, NULL,
4780 baton->data,
4781 baton->size,
4782 baton->per_cu);
4783 else if (bytes != NULL)
4784 {
4785 v = allocate_value (type);
4786 memcpy (value_contents_writeable (v), bytes,
4787 TYPE_LENGTH (type));
4788 }
4789 else
4790 v = value_from_longest (type, value);
4791
4792 /* Specify decimal so that we do not depend on the radix. */
4793 get_formatted_print_options (&opts, 'd');
4794 opts.raw = 1;
4795 value_print (v, buf, &opts);
4796 release_value (v);
4797 value_free (v);
4798 }
4799 }
4800
4801 die->building_fullname = 0;
4802
4803 if (!first)
4804 {
4805 /* Close the argument list, with a space if necessary
4806 (nested templates). */
4807 char last_char = '\0';
4808 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4809 if (last_char == '>')
4810 fputs_unfiltered (" >", buf);
4811 else
4812 fputs_unfiltered (">", buf);
4813 }
4814 }
4815
94af9270
KS
4816 /* For Java and C++ methods, append formal parameter type
4817 information, if PHYSNAME. */
6e70227d 4818
94af9270
KS
4819 if (physname && die->tag == DW_TAG_subprogram
4820 && (cu->language == language_cplus
4821 || cu->language == language_java))
4822 {
4823 struct type *type = read_type_die (die, cu);
4824
4825 c_type_print_args (type, buf, 0, cu->language);
4826
4827 if (cu->language == language_java)
4828 {
4829 /* For java, we must append the return type to method
4830 names. */
4831 if (die->tag == DW_TAG_subprogram)
4832 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4833 0, 0);
4834 }
4835 else if (cu->language == language_cplus)
4836 {
4837 if (TYPE_NFIELDS (type) > 0
4838 && TYPE_FIELD_ARTIFICIAL (type, 0)
4839 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4840 fputs_unfiltered (" const", buf);
4841 }
4842 }
4843
4844 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4845 &length);
4846 ui_file_delete (buf);
4847
4848 if (cu->language == language_cplus)
4849 {
4850 char *cname
4851 = dwarf2_canonicalize_name (name, cu,
4852 &cu->objfile->objfile_obstack);
9a619af0 4853
94af9270
KS
4854 if (cname != NULL)
4855 name = cname;
4856 }
4857 }
4858 }
4859
4860 return name;
4861}
4862
0114d602
DJ
4863/* Return the fully qualified name of DIE, based on its DW_AT_name.
4864 If scope qualifiers are appropriate they will be added. The result
4865 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4866 not have a name. NAME may either be from a previous call to
4867 dwarf2_name or NULL.
4868
4869 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4870
4871static const char *
94af9270 4872dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 4873{
94af9270
KS
4874 return dwarf2_compute_name (name, die, cu, 0);
4875}
0114d602 4876
94af9270
KS
4877/* Construct a physname for the given DIE in CU. NAME may either be
4878 from a previous call to dwarf2_name or NULL. The result will be
4879 allocated on the objfile_objstack or NULL if the DIE does not have a
4880 name.
0114d602 4881
94af9270 4882 The output string will be canonicalized (if C++/Java). */
0114d602 4883
94af9270
KS
4884static const char *
4885dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4886{
4887 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
4888}
4889
27aa8d6a
SW
4890/* Read the import statement specified by the given die and record it. */
4891
4892static void
4893read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4894{
4895 struct attribute *import_attr;
4896 struct die_info *imported_die;
de4affc9 4897 struct dwarf2_cu *imported_cu;
27aa8d6a 4898 const char *imported_name;
794684b6 4899 const char *imported_name_prefix;
13387711
SW
4900 const char *canonical_name;
4901 const char *import_alias;
4902 const char *imported_declaration = NULL;
794684b6 4903 const char *import_prefix;
13387711
SW
4904
4905 char *temp;
27aa8d6a
SW
4906
4907 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4908 if (import_attr == NULL)
4909 {
4910 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4911 dwarf_tag_name (die->tag));
4912 return;
4913 }
4914
de4affc9
CC
4915 imported_cu = cu;
4916 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4917 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
4918 if (imported_name == NULL)
4919 {
4920 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4921
4922 The import in the following code:
4923 namespace A
4924 {
4925 typedef int B;
4926 }
4927
4928 int main ()
4929 {
4930 using A::B;
4931 B b;
4932 return b;
4933 }
4934
4935 ...
4936 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4937 <52> DW_AT_decl_file : 1
4938 <53> DW_AT_decl_line : 6
4939 <54> DW_AT_import : <0x75>
4940 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4941 <59> DW_AT_name : B
4942 <5b> DW_AT_decl_file : 1
4943 <5c> DW_AT_decl_line : 2
4944 <5d> DW_AT_type : <0x6e>
4945 ...
4946 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4947 <76> DW_AT_byte_size : 4
4948 <77> DW_AT_encoding : 5 (signed)
4949
4950 imports the wrong die ( 0x75 instead of 0x58 ).
4951 This case will be ignored until the gcc bug is fixed. */
4952 return;
4953 }
4954
82856980
SW
4955 /* Figure out the local name after import. */
4956 import_alias = dwarf2_name (die, cu);
27aa8d6a 4957
794684b6
SW
4958 /* Figure out where the statement is being imported to. */
4959 import_prefix = determine_prefix (die, cu);
4960
4961 /* Figure out what the scope of the imported die is and prepend it
4962 to the name of the imported die. */
de4affc9 4963 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 4964
f55ee35c
JK
4965 if (imported_die->tag != DW_TAG_namespace
4966 && imported_die->tag != DW_TAG_module)
794684b6 4967 {
13387711
SW
4968 imported_declaration = imported_name;
4969 canonical_name = imported_name_prefix;
794684b6 4970 }
13387711 4971 else if (strlen (imported_name_prefix) > 0)
794684b6 4972 {
13387711
SW
4973 temp = alloca (strlen (imported_name_prefix)
4974 + 2 + strlen (imported_name) + 1);
4975 strcpy (temp, imported_name_prefix);
4976 strcat (temp, "::");
4977 strcat (temp, imported_name);
4978 canonical_name = temp;
794684b6 4979 }
13387711
SW
4980 else
4981 canonical_name = imported_name;
794684b6 4982
c0cc3a76
SW
4983 cp_add_using_directive (import_prefix,
4984 canonical_name,
4985 import_alias,
13387711 4986 imported_declaration,
c0cc3a76 4987 &cu->objfile->objfile_obstack);
27aa8d6a
SW
4988}
4989
5fb290d7 4990static void
e142c38c 4991initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 4992{
e142c38c 4993 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
4994}
4995
cb1df416
DJ
4996static void
4997free_cu_line_header (void *arg)
4998{
4999 struct dwarf2_cu *cu = arg;
5000
5001 free_line_header (cu->line_header);
5002 cu->line_header = NULL;
5003}
5004
9291a0cd
TT
5005static void
5006find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5007 char **name, char **comp_dir)
5008{
5009 struct attribute *attr;
5010
5011 *name = NULL;
5012 *comp_dir = NULL;
5013
5014 /* Find the filename. Do not use dwarf2_name here, since the filename
5015 is not a source language identifier. */
5016 attr = dwarf2_attr (die, DW_AT_name, cu);
5017 if (attr)
5018 {
5019 *name = DW_STRING (attr);
5020 }
5021
5022 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5023 if (attr)
5024 *comp_dir = DW_STRING (attr);
5025 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5026 {
5027 *comp_dir = ldirname (*name);
5028 if (*comp_dir != NULL)
5029 make_cleanup (xfree, *comp_dir);
5030 }
5031 if (*comp_dir != NULL)
5032 {
5033 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5034 directory, get rid of it. */
5035 char *cp = strchr (*comp_dir, ':');
5036
5037 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5038 *comp_dir = cp + 1;
5039 }
5040
5041 if (*name == NULL)
5042 *name = "<unknown>";
5043}
5044
c906108c 5045static void
e7c27a73 5046read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5047{
e7c27a73 5048 struct objfile *objfile = cu->objfile;
debd256d 5049 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5050 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5051 CORE_ADDR highpc = ((CORE_ADDR) 0);
5052 struct attribute *attr;
e1024ff1 5053 char *name = NULL;
c906108c
SS
5054 char *comp_dir = NULL;
5055 struct die_info *child_die;
5056 bfd *abfd = objfile->obfd;
debd256d 5057 struct line_header *line_header = 0;
e142c38c 5058 CORE_ADDR baseaddr;
6e70227d 5059
e142c38c 5060 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5061
fae299cd 5062 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5063
5064 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5065 from finish_block. */
2acceee2 5066 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5067 lowpc = highpc;
5068 lowpc += baseaddr;
5069 highpc += baseaddr;
5070
9291a0cd 5071 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5072
e142c38c 5073 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5074 if (attr)
5075 {
e142c38c 5076 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5077 }
5078
b0f35d58 5079 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5080 if (attr)
b0f35d58 5081 cu->producer = DW_STRING (attr);
303b6f5d 5082
c906108c
SS
5083 /* We assume that we're processing GCC output. */
5084 processing_gcc_compilation = 2;
c906108c 5085
df8a16a1
DJ
5086 processing_has_namespace_info = 0;
5087
c906108c
SS
5088 start_symtab (name, comp_dir, lowpc);
5089 record_debugformat ("DWARF 2");
303b6f5d 5090 record_producer (cu->producer);
c906108c 5091
e142c38c 5092 initialize_cu_func_list (cu);
c906108c 5093
cb1df416
DJ
5094 /* Decode line number information if present. We do this before
5095 processing child DIEs, so that the line header table is available
5096 for DW_AT_decl_file. */
e142c38c 5097 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5098 if (attr)
5099 {
debd256d 5100 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5101 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5102 if (line_header)
5103 {
cb1df416
DJ
5104 cu->line_header = line_header;
5105 make_cleanup (free_cu_line_header, cu);
aaa75496 5106 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5107 }
5fb290d7 5108 }
debd256d 5109
cb1df416
DJ
5110 /* Process all dies in compilation unit. */
5111 if (die->child != NULL)
5112 {
5113 child_die = die->child;
5114 while (child_die && child_die->tag)
5115 {
5116 process_die (child_die, cu);
5117 child_die = sibling_die (child_die);
5118 }
5119 }
5120
2e276125
JB
5121 /* Decode macro information, if present. Dwarf 2 macro information
5122 refers to information in the line number info statement program
5123 header, so we can only read it if we've read the header
5124 successfully. */
e142c38c 5125 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5126 if (attr && line_header)
2e276125
JB
5127 {
5128 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5129
2e276125 5130 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5131 comp_dir, abfd, cu);
2e276125 5132 }
debd256d 5133 do_cleanups (back_to);
5fb290d7
DJ
5134}
5135
348e048f
DE
5136/* For TUs we want to skip the first top level sibling if it's not the
5137 actual type being defined by this TU. In this case the first top
5138 level sibling is there to provide context only. */
5139
5140static void
5141read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5142{
5143 struct objfile *objfile = cu->objfile;
5144 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5145 CORE_ADDR lowpc;
5146 struct attribute *attr;
5147 char *name = NULL;
5148 char *comp_dir = NULL;
5149 struct die_info *child_die;
5150 bfd *abfd = objfile->obfd;
348e048f
DE
5151
5152 /* start_symtab needs a low pc, but we don't really have one.
5153 Do what read_file_scope would do in the absence of such info. */
5154 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5155
5156 /* Find the filename. Do not use dwarf2_name here, since the filename
5157 is not a source language identifier. */
5158 attr = dwarf2_attr (die, DW_AT_name, cu);
5159 if (attr)
5160 name = DW_STRING (attr);
5161
5162 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5163 if (attr)
5164 comp_dir = DW_STRING (attr);
5165 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5166 {
5167 comp_dir = ldirname (name);
5168 if (comp_dir != NULL)
5169 make_cleanup (xfree, comp_dir);
5170 }
5171
5172 if (name == NULL)
5173 name = "<unknown>";
5174
5175 attr = dwarf2_attr (die, DW_AT_language, cu);
5176 if (attr)
5177 set_cu_language (DW_UNSND (attr), cu);
5178
5179 /* This isn't technically needed today. It is done for symmetry
5180 with read_file_scope. */
5181 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5182 if (attr)
348e048f
DE
5183 cu->producer = DW_STRING (attr);
5184
5185 /* We assume that we're processing GCC output. */
5186 processing_gcc_compilation = 2;
5187
5188 processing_has_namespace_info = 0;
5189
5190 start_symtab (name, comp_dir, lowpc);
5191 record_debugformat ("DWARF 2");
5192 record_producer (cu->producer);
5193
5194 /* Process the dies in the type unit. */
5195 if (die->child == NULL)
5196 {
5197 dump_die_for_error (die);
5198 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5199 bfd_get_filename (abfd));
5200 }
5201
5202 child_die = die->child;
5203
5204 while (child_die && child_die->tag)
5205 {
5206 process_die (child_die, cu);
5207
5208 child_die = sibling_die (child_die);
5209 }
5210
5211 do_cleanups (back_to);
5212}
5213
5fb290d7 5214static void
e142c38c
DJ
5215add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5216 struct dwarf2_cu *cu)
5fb290d7
DJ
5217{
5218 struct function_range *thisfn;
5219
5220 thisfn = (struct function_range *)
7b5a2f43 5221 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5222 thisfn->name = name;
5223 thisfn->lowpc = lowpc;
5224 thisfn->highpc = highpc;
5225 thisfn->seen_line = 0;
5226 thisfn->next = NULL;
5227
e142c38c
DJ
5228 if (cu->last_fn == NULL)
5229 cu->first_fn = thisfn;
5fb290d7 5230 else
e142c38c 5231 cu->last_fn->next = thisfn;
5fb290d7 5232
e142c38c 5233 cu->last_fn = thisfn;
c906108c
SS
5234}
5235
d389af10
JK
5236/* qsort helper for inherit_abstract_dies. */
5237
5238static int
5239unsigned_int_compar (const void *ap, const void *bp)
5240{
5241 unsigned int a = *(unsigned int *) ap;
5242 unsigned int b = *(unsigned int *) bp;
5243
5244 return (a > b) - (b > a);
5245}
5246
5247/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5248 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5249 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5250
5251static void
5252inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5253{
5254 struct die_info *child_die;
5255 unsigned die_children_count;
5256 /* CU offsets which were referenced by children of the current DIE. */
5257 unsigned *offsets;
5258 unsigned *offsets_end, *offsetp;
5259 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5260 struct die_info *origin_die;
5261 /* Iterator of the ORIGIN_DIE children. */
5262 struct die_info *origin_child_die;
5263 struct cleanup *cleanups;
5264 struct attribute *attr;
5265
5266 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5267 if (!attr)
5268 return;
5269
5270 origin_die = follow_die_ref (die, attr, &cu);
edb3359d
DJ
5271 if (die->tag != origin_die->tag
5272 && !(die->tag == DW_TAG_inlined_subroutine
5273 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5274 complaint (&symfile_complaints,
5275 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5276 die->offset, origin_die->offset);
5277
5278 child_die = die->child;
5279 die_children_count = 0;
5280 while (child_die && child_die->tag)
5281 {
5282 child_die = sibling_die (child_die);
5283 die_children_count++;
5284 }
5285 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5286 cleanups = make_cleanup (xfree, offsets);
5287
5288 offsets_end = offsets;
5289 child_die = die->child;
5290 while (child_die && child_die->tag)
5291 {
c38f313d
DJ
5292 /* For each CHILD_DIE, find the corresponding child of
5293 ORIGIN_DIE. If there is more than one layer of
5294 DW_AT_abstract_origin, follow them all; there shouldn't be,
5295 but GCC versions at least through 4.4 generate this (GCC PR
5296 40573). */
5297 struct die_info *child_origin_die = child_die;
9a619af0 5298
c38f313d
DJ
5299 while (1)
5300 {
5301 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin, cu);
5302 if (attr == NULL)
5303 break;
5304 child_origin_die = follow_die_ref (child_origin_die, attr, &cu);
5305 }
5306
d389af10
JK
5307 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5308 counterpart may exist. */
c38f313d 5309 if (child_origin_die != child_die)
d389af10 5310 {
edb3359d
DJ
5311 if (child_die->tag != child_origin_die->tag
5312 && !(child_die->tag == DW_TAG_inlined_subroutine
5313 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5314 complaint (&symfile_complaints,
5315 _("Child DIE 0x%x and its abstract origin 0x%x have "
5316 "different tags"), child_die->offset,
5317 child_origin_die->offset);
c38f313d
DJ
5318 if (child_origin_die->parent != origin_die)
5319 complaint (&symfile_complaints,
5320 _("Child DIE 0x%x and its abstract origin 0x%x have "
5321 "different parents"), child_die->offset,
5322 child_origin_die->offset);
5323 else
5324 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5325 }
5326 child_die = sibling_die (child_die);
5327 }
5328 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5329 unsigned_int_compar);
5330 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5331 if (offsetp[-1] == *offsetp)
5332 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5333 "to DIE 0x%x as their abstract origin"),
5334 die->offset, *offsetp);
5335
5336 offsetp = offsets;
5337 origin_child_die = origin_die->child;
5338 while (origin_child_die && origin_child_die->tag)
5339 {
5340 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5341 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5342 offsetp++;
5343 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5344 {
5345 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5346 process_die (origin_child_die, cu);
5347 }
5348 origin_child_die = sibling_die (origin_child_die);
5349 }
5350
5351 do_cleanups (cleanups);
5352}
5353
c906108c 5354static void
e7c27a73 5355read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5356{
e7c27a73 5357 struct objfile *objfile = cu->objfile;
52f0bd74 5358 struct context_stack *new;
c906108c
SS
5359 CORE_ADDR lowpc;
5360 CORE_ADDR highpc;
5361 struct die_info *child_die;
edb3359d 5362 struct attribute *attr, *call_line, *call_file;
c906108c 5363 char *name;
e142c38c 5364 CORE_ADDR baseaddr;
801e3a5b 5365 struct block *block;
edb3359d 5366 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5367 VEC (symbolp) *template_args = NULL;
5368 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5369
5370 if (inlined_func)
5371 {
5372 /* If we do not have call site information, we can't show the
5373 caller of this inlined function. That's too confusing, so
5374 only use the scope for local variables. */
5375 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5376 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5377 if (call_line == NULL || call_file == NULL)
5378 {
5379 read_lexical_block_scope (die, cu);
5380 return;
5381 }
5382 }
c906108c 5383
e142c38c
DJ
5384 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5385
94af9270 5386 name = dwarf2_name (die, cu);
c906108c 5387
e8d05480
JB
5388 /* Ignore functions with missing or empty names. These are actually
5389 illegal according to the DWARF standard. */
5390 if (name == NULL)
5391 {
5392 complaint (&symfile_complaints,
5393 _("missing name for subprogram DIE at %d"), die->offset);
5394 return;
5395 }
5396
5397 /* Ignore functions with missing or invalid low and high pc attributes. */
5398 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5399 {
ae4d0c03
PM
5400 attr = dwarf2_attr (die, DW_AT_external, cu);
5401 if (!attr || !DW_UNSND (attr))
5402 complaint (&symfile_complaints,
5403 _("cannot get low and high bounds for subprogram DIE at %d"),
5404 die->offset);
e8d05480
JB
5405 return;
5406 }
c906108c
SS
5407
5408 lowpc += baseaddr;
5409 highpc += baseaddr;
5410
5fb290d7 5411 /* Record the function range for dwarf_decode_lines. */
e142c38c 5412 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5413
34eaf542
TT
5414 /* If we have any template arguments, then we must allocate a
5415 different sort of symbol. */
5416 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5417 {
5418 if (child_die->tag == DW_TAG_template_type_param
5419 || child_die->tag == DW_TAG_template_value_param)
5420 {
5421 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5422 struct template_symbol);
5423 templ_func->base.is_cplus_template_function = 1;
5424 break;
5425 }
5426 }
5427
c906108c 5428 new = push_context (0, lowpc);
34eaf542
TT
5429 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5430 (struct symbol *) templ_func);
4c2df51b 5431
4cecd739
DJ
5432 /* If there is a location expression for DW_AT_frame_base, record
5433 it. */
e142c38c 5434 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5435 if (attr)
c034e007
AC
5436 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5437 expression is being recorded directly in the function's symbol
5438 and not in a separate frame-base object. I guess this hack is
5439 to avoid adding some sort of frame-base adjunct/annex to the
5440 function's symbol :-(. The problem with doing this is that it
5441 results in a function symbol with a location expression that
5442 has nothing to do with the location of the function, ouch! The
5443 relationship should be: a function's symbol has-a frame base; a
5444 frame-base has-a location expression. */
e7c27a73 5445 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5446
e142c38c 5447 cu->list_in_scope = &local_symbols;
c906108c 5448
639d11d3 5449 if (die->child != NULL)
c906108c 5450 {
639d11d3 5451 child_die = die->child;
c906108c
SS
5452 while (child_die && child_die->tag)
5453 {
34eaf542
TT
5454 if (child_die->tag == DW_TAG_template_type_param
5455 || child_die->tag == DW_TAG_template_value_param)
5456 {
5457 struct symbol *arg = new_symbol (child_die, NULL, cu);
5458
5459 VEC_safe_push (symbolp, template_args, arg);
5460 }
5461 else
5462 process_die (child_die, cu);
c906108c
SS
5463 child_die = sibling_die (child_die);
5464 }
5465 }
5466
d389af10
JK
5467 inherit_abstract_dies (die, cu);
5468
4a811a97
UW
5469 /* If we have a DW_AT_specification, we might need to import using
5470 directives from the context of the specification DIE. See the
5471 comment in determine_prefix. */
5472 if (cu->language == language_cplus
5473 && dwarf2_attr (die, DW_AT_specification, cu))
5474 {
5475 struct dwarf2_cu *spec_cu = cu;
5476 struct die_info *spec_die = die_specification (die, &spec_cu);
5477
5478 while (spec_die)
5479 {
5480 child_die = spec_die->child;
5481 while (child_die && child_die->tag)
5482 {
5483 if (child_die->tag == DW_TAG_imported_module)
5484 process_die (child_die, spec_cu);
5485 child_die = sibling_die (child_die);
5486 }
5487
5488 /* In some cases, GCC generates specification DIEs that
5489 themselves contain DW_AT_specification attributes. */
5490 spec_die = die_specification (spec_die, &spec_cu);
5491 }
5492 }
5493
c906108c
SS
5494 new = pop_context ();
5495 /* Make a block for the local symbols within. */
801e3a5b
JB
5496 block = finish_block (new->name, &local_symbols, new->old_blocks,
5497 lowpc, highpc, objfile);
5498
df8a16a1 5499 /* For C++, set the block's scope. */
f55ee35c 5500 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5501 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5502 determine_prefix (die, cu),
df8a16a1
DJ
5503 processing_has_namespace_info);
5504
801e3a5b
JB
5505 /* If we have address ranges, record them. */
5506 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5507
34eaf542
TT
5508 /* Attach template arguments to function. */
5509 if (! VEC_empty (symbolp, template_args))
5510 {
5511 gdb_assert (templ_func != NULL);
5512
5513 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5514 templ_func->template_arguments
5515 = obstack_alloc (&objfile->objfile_obstack,
5516 (templ_func->n_template_arguments
5517 * sizeof (struct symbol *)));
5518 memcpy (templ_func->template_arguments,
5519 VEC_address (symbolp, template_args),
5520 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5521 VEC_free (symbolp, template_args);
5522 }
5523
208d8187
JB
5524 /* In C++, we can have functions nested inside functions (e.g., when
5525 a function declares a class that has methods). This means that
5526 when we finish processing a function scope, we may need to go
5527 back to building a containing block's symbol lists. */
5528 local_symbols = new->locals;
5529 param_symbols = new->params;
27aa8d6a 5530 using_directives = new->using_directives;
208d8187 5531
921e78cf
JB
5532 /* If we've finished processing a top-level function, subsequent
5533 symbols go in the file symbol list. */
5534 if (outermost_context_p ())
e142c38c 5535 cu->list_in_scope = &file_symbols;
c906108c
SS
5536}
5537
5538/* Process all the DIES contained within a lexical block scope. Start
5539 a new scope, process the dies, and then close the scope. */
5540
5541static void
e7c27a73 5542read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5543{
e7c27a73 5544 struct objfile *objfile = cu->objfile;
52f0bd74 5545 struct context_stack *new;
c906108c
SS
5546 CORE_ADDR lowpc, highpc;
5547 struct die_info *child_die;
e142c38c
DJ
5548 CORE_ADDR baseaddr;
5549
5550 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5551
5552 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5553 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5554 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5555 be nasty. Might be easier to properly extend generic blocks to
af34e669 5556 describe ranges. */
d85a05f0 5557 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5558 return;
5559 lowpc += baseaddr;
5560 highpc += baseaddr;
5561
5562 push_context (0, lowpc);
639d11d3 5563 if (die->child != NULL)
c906108c 5564 {
639d11d3 5565 child_die = die->child;
c906108c
SS
5566 while (child_die && child_die->tag)
5567 {
e7c27a73 5568 process_die (child_die, cu);
c906108c
SS
5569 child_die = sibling_die (child_die);
5570 }
5571 }
5572 new = pop_context ();
5573
8540c487 5574 if (local_symbols != NULL || using_directives != NULL)
c906108c 5575 {
801e3a5b
JB
5576 struct block *block
5577 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5578 highpc, objfile);
5579
5580 /* Note that recording ranges after traversing children, as we
5581 do here, means that recording a parent's ranges entails
5582 walking across all its children's ranges as they appear in
5583 the address map, which is quadratic behavior.
5584
5585 It would be nicer to record the parent's ranges before
5586 traversing its children, simply overriding whatever you find
5587 there. But since we don't even decide whether to create a
5588 block until after we've traversed its children, that's hard
5589 to do. */
5590 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5591 }
5592 local_symbols = new->locals;
27aa8d6a 5593 using_directives = new->using_directives;
c906108c
SS
5594}
5595
43039443 5596/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5597 Return 1 if the attributes are present and valid, otherwise, return 0.
5598 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5599
5600static int
5601dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5602 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5603 struct partial_symtab *ranges_pst)
43039443
JK
5604{
5605 struct objfile *objfile = cu->objfile;
5606 struct comp_unit_head *cu_header = &cu->header;
5607 bfd *obfd = objfile->obfd;
5608 unsigned int addr_size = cu_header->addr_size;
5609 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5610 /* Base address selection entry. */
5611 CORE_ADDR base;
5612 int found_base;
5613 unsigned int dummy;
5614 gdb_byte *buffer;
5615 CORE_ADDR marker;
5616 int low_set;
5617 CORE_ADDR low = 0;
5618 CORE_ADDR high = 0;
ff013f42 5619 CORE_ADDR baseaddr;
43039443 5620
d00adf39
DE
5621 found_base = cu->base_known;
5622 base = cu->base_address;
43039443 5623
be391dca 5624 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5625 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5626 {
5627 complaint (&symfile_complaints,
5628 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5629 offset);
5630 return 0;
5631 }
dce234bc 5632 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5633
5634 /* Read in the largest possible address. */
5635 marker = read_address (obfd, buffer, cu, &dummy);
5636 if ((marker & mask) == mask)
5637 {
5638 /* If we found the largest possible address, then
5639 read the base address. */
5640 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5641 buffer += 2 * addr_size;
5642 offset += 2 * addr_size;
5643 found_base = 1;
5644 }
5645
5646 low_set = 0;
5647
e7030f15 5648 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5649
43039443
JK
5650 while (1)
5651 {
5652 CORE_ADDR range_beginning, range_end;
5653
5654 range_beginning = read_address (obfd, buffer, cu, &dummy);
5655 buffer += addr_size;
5656 range_end = read_address (obfd, buffer, cu, &dummy);
5657 buffer += addr_size;
5658 offset += 2 * addr_size;
5659
5660 /* An end of list marker is a pair of zero addresses. */
5661 if (range_beginning == 0 && range_end == 0)
5662 /* Found the end of list entry. */
5663 break;
5664
5665 /* Each base address selection entry is a pair of 2 values.
5666 The first is the largest possible address, the second is
5667 the base address. Check for a base address here. */
5668 if ((range_beginning & mask) == mask)
5669 {
5670 /* If we found the largest possible address, then
5671 read the base address. */
5672 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5673 found_base = 1;
5674 continue;
5675 }
5676
5677 if (!found_base)
5678 {
5679 /* We have no valid base address for the ranges
5680 data. */
5681 complaint (&symfile_complaints,
5682 _("Invalid .debug_ranges data (no base address)"));
5683 return 0;
5684 }
5685
5686 range_beginning += base;
5687 range_end += base;
5688
ff013f42
JK
5689 if (ranges_pst != NULL && range_beginning < range_end)
5690 addrmap_set_empty (objfile->psymtabs_addrmap,
5691 range_beginning + baseaddr, range_end - 1 + baseaddr,
5692 ranges_pst);
5693
43039443
JK
5694 /* FIXME: This is recording everything as a low-high
5695 segment of consecutive addresses. We should have a
5696 data structure for discontiguous block ranges
5697 instead. */
5698 if (! low_set)
5699 {
5700 low = range_beginning;
5701 high = range_end;
5702 low_set = 1;
5703 }
5704 else
5705 {
5706 if (range_beginning < low)
5707 low = range_beginning;
5708 if (range_end > high)
5709 high = range_end;
5710 }
5711 }
5712
5713 if (! low_set)
5714 /* If the first entry is an end-of-list marker, the range
5715 describes an empty scope, i.e. no instructions. */
5716 return 0;
5717
5718 if (low_return)
5719 *low_return = low;
5720 if (high_return)
5721 *high_return = high;
5722 return 1;
5723}
5724
af34e669
DJ
5725/* Get low and high pc attributes from a die. Return 1 if the attributes
5726 are present and valid, otherwise, return 0. Return -1 if the range is
5727 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5728static int
af34e669 5729dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5730 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5731 struct partial_symtab *pst)
c906108c
SS
5732{
5733 struct attribute *attr;
af34e669
DJ
5734 CORE_ADDR low = 0;
5735 CORE_ADDR high = 0;
5736 int ret = 0;
c906108c 5737
e142c38c 5738 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5739 if (attr)
af34e669
DJ
5740 {
5741 high = DW_ADDR (attr);
e142c38c 5742 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5743 if (attr)
5744 low = DW_ADDR (attr);
5745 else
5746 /* Found high w/o low attribute. */
5747 return 0;
5748
5749 /* Found consecutive range of addresses. */
5750 ret = 1;
5751 }
c906108c 5752 else
af34e669 5753 {
e142c38c 5754 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5755 if (attr != NULL)
5756 {
af34e669 5757 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5758 .debug_ranges section. */
d85a05f0 5759 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5760 return 0;
43039443 5761 /* Found discontinuous range of addresses. */
af34e669
DJ
5762 ret = -1;
5763 }
5764 }
c906108c
SS
5765
5766 if (high < low)
5767 return 0;
5768
5769 /* When using the GNU linker, .gnu.linkonce. sections are used to
5770 eliminate duplicate copies of functions and vtables and such.
5771 The linker will arbitrarily choose one and discard the others.
5772 The AT_*_pc values for such functions refer to local labels in
5773 these sections. If the section from that file was discarded, the
5774 labels are not in the output, so the relocs get a value of 0.
5775 If this is a discarded function, mark the pc bounds as invalid,
5776 so that GDB will ignore it. */
72dca2f5 5777 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5778 return 0;
5779
5780 *lowpc = low;
5781 *highpc = high;
af34e669 5782 return ret;
c906108c
SS
5783}
5784
b084d499
JB
5785/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5786 its low and high PC addresses. Do nothing if these addresses could not
5787 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5788 and HIGHPC to the high address if greater than HIGHPC. */
5789
5790static void
5791dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5792 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5793 struct dwarf2_cu *cu)
5794{
5795 CORE_ADDR low, high;
5796 struct die_info *child = die->child;
5797
d85a05f0 5798 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5799 {
5800 *lowpc = min (*lowpc, low);
5801 *highpc = max (*highpc, high);
5802 }
5803
5804 /* If the language does not allow nested subprograms (either inside
5805 subprograms or lexical blocks), we're done. */
5806 if (cu->language != language_ada)
5807 return;
6e70227d 5808
b084d499
JB
5809 /* Check all the children of the given DIE. If it contains nested
5810 subprograms, then check their pc bounds. Likewise, we need to
5811 check lexical blocks as well, as they may also contain subprogram
5812 definitions. */
5813 while (child && child->tag)
5814 {
5815 if (child->tag == DW_TAG_subprogram
5816 || child->tag == DW_TAG_lexical_block)
5817 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5818 child = sibling_die (child);
5819 }
5820}
5821
fae299cd
DC
5822/* Get the low and high pc's represented by the scope DIE, and store
5823 them in *LOWPC and *HIGHPC. If the correct values can't be
5824 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5825
5826static void
5827get_scope_pc_bounds (struct die_info *die,
5828 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5829 struct dwarf2_cu *cu)
5830{
5831 CORE_ADDR best_low = (CORE_ADDR) -1;
5832 CORE_ADDR best_high = (CORE_ADDR) 0;
5833 CORE_ADDR current_low, current_high;
5834
d85a05f0 5835 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5836 {
5837 best_low = current_low;
5838 best_high = current_high;
5839 }
5840 else
5841 {
5842 struct die_info *child = die->child;
5843
5844 while (child && child->tag)
5845 {
5846 switch (child->tag) {
5847 case DW_TAG_subprogram:
b084d499 5848 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
5849 break;
5850 case DW_TAG_namespace:
f55ee35c 5851 case DW_TAG_module:
fae299cd
DC
5852 /* FIXME: carlton/2004-01-16: Should we do this for
5853 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5854 that current GCC's always emit the DIEs corresponding
5855 to definitions of methods of classes as children of a
5856 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5857 the DIEs giving the declarations, which could be
5858 anywhere). But I don't see any reason why the
5859 standards says that they have to be there. */
5860 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5861
5862 if (current_low != ((CORE_ADDR) -1))
5863 {
5864 best_low = min (best_low, current_low);
5865 best_high = max (best_high, current_high);
5866 }
5867 break;
5868 default:
5869 /* Ignore. */
5870 break;
5871 }
5872
5873 child = sibling_die (child);
5874 }
5875 }
5876
5877 *lowpc = best_low;
5878 *highpc = best_high;
5879}
5880
801e3a5b
JB
5881/* Record the address ranges for BLOCK, offset by BASEADDR, as given
5882 in DIE. */
5883static void
5884dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5885 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5886{
5887 struct attribute *attr;
5888
5889 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5890 if (attr)
5891 {
5892 CORE_ADDR high = DW_ADDR (attr);
9a619af0 5893
801e3a5b
JB
5894 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5895 if (attr)
5896 {
5897 CORE_ADDR low = DW_ADDR (attr);
9a619af0 5898
801e3a5b
JB
5899 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5900 }
5901 }
5902
5903 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5904 if (attr)
5905 {
5906 bfd *obfd = cu->objfile->obfd;
5907
5908 /* The value of the DW_AT_ranges attribute is the offset of the
5909 address range list in the .debug_ranges section. */
5910 unsigned long offset = DW_UNSND (attr);
dce234bc 5911 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
5912
5913 /* For some target architectures, but not others, the
5914 read_address function sign-extends the addresses it returns.
5915 To recognize base address selection entries, we need a
5916 mask. */
5917 unsigned int addr_size = cu->header.addr_size;
5918 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5919
5920 /* The base address, to which the next pair is relative. Note
5921 that this 'base' is a DWARF concept: most entries in a range
5922 list are relative, to reduce the number of relocs against the
5923 debugging information. This is separate from this function's
5924 'baseaddr' argument, which GDB uses to relocate debugging
5925 information from a shared library based on the address at
5926 which the library was loaded. */
d00adf39
DE
5927 CORE_ADDR base = cu->base_address;
5928 int base_known = cu->base_known;
801e3a5b 5929
be391dca 5930 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 5931 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
5932 {
5933 complaint (&symfile_complaints,
5934 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5935 offset);
5936 return;
5937 }
5938
5939 for (;;)
5940 {
5941 unsigned int bytes_read;
5942 CORE_ADDR start, end;
5943
5944 start = read_address (obfd, buffer, cu, &bytes_read);
5945 buffer += bytes_read;
5946 end = read_address (obfd, buffer, cu, &bytes_read);
5947 buffer += bytes_read;
5948
5949 /* Did we find the end of the range list? */
5950 if (start == 0 && end == 0)
5951 break;
5952
5953 /* Did we find a base address selection entry? */
5954 else if ((start & base_select_mask) == base_select_mask)
5955 {
5956 base = end;
5957 base_known = 1;
5958 }
5959
5960 /* We found an ordinary address range. */
5961 else
5962 {
5963 if (!base_known)
5964 {
5965 complaint (&symfile_complaints,
5966 _("Invalid .debug_ranges data (no base address)"));
5967 return;
5968 }
5969
6e70227d
DE
5970 record_block_range (block,
5971 baseaddr + base + start,
801e3a5b
JB
5972 baseaddr + base + end - 1);
5973 }
5974 }
5975 }
5976}
5977
c906108c
SS
5978/* Add an aggregate field to the field list. */
5979
5980static void
107d2387 5981dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 5982 struct dwarf2_cu *cu)
6e70227d 5983{
e7c27a73 5984 struct objfile *objfile = cu->objfile;
5e2b427d 5985 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
5986 struct nextfield *new_field;
5987 struct attribute *attr;
5988 struct field *fp;
5989 char *fieldname = "";
5990
5991 /* Allocate a new field list entry and link it in. */
5992 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 5993 make_cleanup (xfree, new_field);
c906108c 5994 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
5995
5996 if (die->tag == DW_TAG_inheritance)
5997 {
5998 new_field->next = fip->baseclasses;
5999 fip->baseclasses = new_field;
6000 }
6001 else
6002 {
6003 new_field->next = fip->fields;
6004 fip->fields = new_field;
6005 }
c906108c
SS
6006 fip->nfields++;
6007
6008 /* Handle accessibility and virtuality of field.
6009 The default accessibility for members is public, the default
6010 accessibility for inheritance is private. */
6011 if (die->tag != DW_TAG_inheritance)
6012 new_field->accessibility = DW_ACCESS_public;
6013 else
6014 new_field->accessibility = DW_ACCESS_private;
6015 new_field->virtuality = DW_VIRTUALITY_none;
6016
e142c38c 6017 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6018 if (attr)
6019 new_field->accessibility = DW_UNSND (attr);
6020 if (new_field->accessibility != DW_ACCESS_public)
6021 fip->non_public_fields = 1;
e142c38c 6022 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6023 if (attr)
6024 new_field->virtuality = DW_UNSND (attr);
6025
6026 fp = &new_field->field;
a9a9bd0f 6027
e142c38c 6028 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6029 {
a9a9bd0f 6030 /* Data member other than a C++ static data member. */
6e70227d 6031
c906108c 6032 /* Get type of field. */
e7c27a73 6033 fp->type = die_type (die, cu);
c906108c 6034
d6a843b5 6035 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6036
c906108c 6037 /* Get bit size of field (zero if none). */
e142c38c 6038 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6039 if (attr)
6040 {
6041 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6042 }
6043 else
6044 {
6045 FIELD_BITSIZE (*fp) = 0;
6046 }
6047
6048 /* Get bit offset of field. */
e142c38c 6049 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6050 if (attr)
6051 {
d4b96c9a 6052 int byte_offset = 0;
c6a0999f 6053
3690dd37 6054 if (attr_form_is_section_offset (attr))
d4b96c9a 6055 dwarf2_complex_location_expr_complaint ();
3690dd37 6056 else if (attr_form_is_constant (attr))
c6a0999f 6057 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6058 else if (attr_form_is_block (attr))
c6a0999f 6059 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6060 else
6061 dwarf2_complex_location_expr_complaint ();
c6a0999f 6062
d6a843b5 6063 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6064 }
e142c38c 6065 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6066 if (attr)
6067 {
5e2b427d 6068 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6069 {
6070 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6071 additional bit offset from the MSB of the containing
6072 anonymous object to the MSB of the field. We don't
6073 have to do anything special since we don't need to
6074 know the size of the anonymous object. */
c906108c
SS
6075 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6076 }
6077 else
6078 {
6079 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6080 MSB of the anonymous object, subtract off the number of
6081 bits from the MSB of the field to the MSB of the
6082 object, and then subtract off the number of bits of
6083 the field itself. The result is the bit offset of
6084 the LSB of the field. */
c906108c
SS
6085 int anonymous_size;
6086 int bit_offset = DW_UNSND (attr);
6087
e142c38c 6088 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6089 if (attr)
6090 {
6091 /* The size of the anonymous object containing
6092 the bit field is explicit, so use the
6093 indicated size (in bytes). */
6094 anonymous_size = DW_UNSND (attr);
6095 }
6096 else
6097 {
6098 /* The size of the anonymous object containing
6099 the bit field must be inferred from the type
6100 attribute of the data member containing the
6101 bit field. */
6102 anonymous_size = TYPE_LENGTH (fp->type);
6103 }
6104 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6105 - bit_offset - FIELD_BITSIZE (*fp);
6106 }
6107 }
6108
6109 /* Get name of field. */
39cbfefa
DJ
6110 fieldname = dwarf2_name (die, cu);
6111 if (fieldname == NULL)
6112 fieldname = "";
d8151005
DJ
6113
6114 /* The name is already allocated along with this objfile, so we don't
6115 need to duplicate it for the type. */
6116 fp->name = fieldname;
c906108c
SS
6117
6118 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6119 pointer or virtual base class pointer) to private. */
e142c38c 6120 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6121 {
d48cc9dd 6122 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6123 new_field->accessibility = DW_ACCESS_private;
6124 fip->non_public_fields = 1;
6125 }
6126 }
a9a9bd0f 6127 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6128 {
a9a9bd0f
DC
6129 /* C++ static member. */
6130
6131 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6132 is a declaration, but all versions of G++ as of this writing
6133 (so through at least 3.2.1) incorrectly generate
6134 DW_TAG_variable tags. */
6e70227d 6135
c906108c 6136 char *physname;
c906108c 6137
a9a9bd0f 6138 /* Get name of field. */
39cbfefa
DJ
6139 fieldname = dwarf2_name (die, cu);
6140 if (fieldname == NULL)
c906108c
SS
6141 return;
6142
254e6b9e 6143 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6144 if (attr
6145 /* Only create a symbol if this is an external value.
6146 new_symbol checks this and puts the value in the global symbol
6147 table, which we want. If it is not external, new_symbol
6148 will try to put the value in cu->list_in_scope which is wrong. */
6149 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6150 {
6151 /* A static const member, not much different than an enum as far as
6152 we're concerned, except that we can support more types. */
6153 new_symbol (die, NULL, cu);
6154 }
6155
2df3850c 6156 /* Get physical name. */
94af9270 6157 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6158
d8151005
DJ
6159 /* The name is already allocated along with this objfile, so we don't
6160 need to duplicate it for the type. */
6161 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6162 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6163 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6164 }
6165 else if (die->tag == DW_TAG_inheritance)
6166 {
6167 /* C++ base class field. */
e142c38c 6168 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6169 if (attr)
d4b96c9a
JK
6170 {
6171 int byte_offset = 0;
6172
6173 if (attr_form_is_section_offset (attr))
6174 dwarf2_complex_location_expr_complaint ();
6175 else if (attr_form_is_constant (attr))
6176 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6177 else if (attr_form_is_block (attr))
6178 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6179 else
6180 dwarf2_complex_location_expr_complaint ();
6181
6182 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6183 }
c906108c 6184 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6185 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6186 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6187 fip->nbaseclasses++;
6188 }
6189}
6190
98751a41
JK
6191/* Add a typedef defined in the scope of the FIP's class. */
6192
6193static void
6194dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6195 struct dwarf2_cu *cu)
6e70227d 6196{
98751a41
JK
6197 struct objfile *objfile = cu->objfile;
6198 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6199 struct typedef_field_list *new_field;
6200 struct attribute *attr;
6201 struct typedef_field *fp;
6202 char *fieldname = "";
6203
6204 /* Allocate a new field list entry and link it in. */
6205 new_field = xzalloc (sizeof (*new_field));
6206 make_cleanup (xfree, new_field);
6207
6208 gdb_assert (die->tag == DW_TAG_typedef);
6209
6210 fp = &new_field->field;
6211
6212 /* Get name of field. */
6213 fp->name = dwarf2_name (die, cu);
6214 if (fp->name == NULL)
6215 return;
6216
6217 fp->type = read_type_die (die, cu);
6218
6219 new_field->next = fip->typedef_field_list;
6220 fip->typedef_field_list = new_field;
6221 fip->typedef_field_list_count++;
6222}
6223
c906108c
SS
6224/* Create the vector of fields, and attach it to the type. */
6225
6226static void
fba45db2 6227dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6228 struct dwarf2_cu *cu)
c906108c
SS
6229{
6230 int nfields = fip->nfields;
6231
6232 /* Record the field count, allocate space for the array of fields,
6233 and create blank accessibility bitfields if necessary. */
6234 TYPE_NFIELDS (type) = nfields;
6235 TYPE_FIELDS (type) = (struct field *)
6236 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6237 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6238
b4ba55a1 6239 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6240 {
6241 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6242
6243 TYPE_FIELD_PRIVATE_BITS (type) =
6244 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6245 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6246
6247 TYPE_FIELD_PROTECTED_BITS (type) =
6248 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6249 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6250
6251 TYPE_FIELD_IGNORE_BITS (type) =
6252 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6253 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6254 }
6255
6256 /* If the type has baseclasses, allocate and clear a bit vector for
6257 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6258 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6259 {
6260 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6261 unsigned char *pointer;
c906108c
SS
6262
6263 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6264 pointer = TYPE_ALLOC (type, num_bytes);
6265 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6266 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6267 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6268 }
6269
6270 /* Copy the saved-up fields into the field vector. Start from the head
6271 of the list, adding to the tail of the field array, so that they end
6272 up in the same order in the array in which they were added to the list. */
6273 while (nfields-- > 0)
6274 {
7d0ccb61
DJ
6275 struct nextfield *fieldp;
6276
6277 if (fip->fields)
6278 {
6279 fieldp = fip->fields;
6280 fip->fields = fieldp->next;
6281 }
6282 else
6283 {
6284 fieldp = fip->baseclasses;
6285 fip->baseclasses = fieldp->next;
6286 }
6287
6288 TYPE_FIELD (type, nfields) = fieldp->field;
6289 switch (fieldp->accessibility)
c906108c 6290 {
c5aa993b 6291 case DW_ACCESS_private:
b4ba55a1
JB
6292 if (cu->language != language_ada)
6293 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6294 break;
c906108c 6295
c5aa993b 6296 case DW_ACCESS_protected:
b4ba55a1
JB
6297 if (cu->language != language_ada)
6298 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6299 break;
c906108c 6300
c5aa993b
JM
6301 case DW_ACCESS_public:
6302 break;
c906108c 6303
c5aa993b
JM
6304 default:
6305 /* Unknown accessibility. Complain and treat it as public. */
6306 {
e2e0b3e5 6307 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6308 fieldp->accessibility);
c5aa993b
JM
6309 }
6310 break;
c906108c
SS
6311 }
6312 if (nfields < fip->nbaseclasses)
6313 {
7d0ccb61 6314 switch (fieldp->virtuality)
c906108c 6315 {
c5aa993b
JM
6316 case DW_VIRTUALITY_virtual:
6317 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
6318 if (cu->language == language_ada)
6319 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
6320 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6321 break;
c906108c
SS
6322 }
6323 }
c906108c
SS
6324 }
6325}
6326
c906108c
SS
6327/* Add a member function to the proper fieldlist. */
6328
6329static void
107d2387 6330dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6331 struct type *type, struct dwarf2_cu *cu)
c906108c 6332{
e7c27a73 6333 struct objfile *objfile = cu->objfile;
c906108c
SS
6334 struct attribute *attr;
6335 struct fnfieldlist *flp;
6336 int i;
6337 struct fn_field *fnp;
6338 char *fieldname;
c906108c 6339 struct nextfnfield *new_fnfield;
f792889a 6340 struct type *this_type;
c906108c 6341
b4ba55a1
JB
6342 if (cu->language == language_ada)
6343 error ("unexpected member function in Ada type");
6344
2df3850c 6345 /* Get name of member function. */
39cbfefa
DJ
6346 fieldname = dwarf2_name (die, cu);
6347 if (fieldname == NULL)
2df3850c 6348 return;
c906108c 6349
c906108c
SS
6350 /* Look up member function name in fieldlist. */
6351 for (i = 0; i < fip->nfnfields; i++)
6352 {
27bfe10e 6353 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6354 break;
6355 }
6356
6357 /* Create new list element if necessary. */
6358 if (i < fip->nfnfields)
6359 flp = &fip->fnfieldlists[i];
6360 else
6361 {
6362 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6363 {
6364 fip->fnfieldlists = (struct fnfieldlist *)
6365 xrealloc (fip->fnfieldlists,
6366 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6367 * sizeof (struct fnfieldlist));
c906108c 6368 if (fip->nfnfields == 0)
c13c43fd 6369 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6370 }
6371 flp = &fip->fnfieldlists[fip->nfnfields];
6372 flp->name = fieldname;
6373 flp->length = 0;
6374 flp->head = NULL;
3da10d80 6375 i = fip->nfnfields++;
c906108c
SS
6376 }
6377
6378 /* Create a new member function field and chain it to the field list
6379 entry. */
6380 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6381 make_cleanup (xfree, new_fnfield);
c906108c
SS
6382 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6383 new_fnfield->next = flp->head;
6384 flp->head = new_fnfield;
6385 flp->length++;
6386
6387 /* Fill in the member function field info. */
6388 fnp = &new_fnfield->fnfield;
3da10d80
KS
6389
6390 /* Delay processing of the physname until later. */
6391 if (cu->language == language_cplus || cu->language == language_java)
6392 {
6393 add_to_method_list (type, i, flp->length - 1, fieldname,
6394 die, cu);
6395 }
6396 else
6397 {
6398 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6399 fnp->physname = physname ? physname : "";
6400 }
6401
c906108c 6402 fnp->type = alloc_type (objfile);
f792889a
DJ
6403 this_type = read_type_die (die, cu);
6404 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6405 {
f792889a 6406 int nparams = TYPE_NFIELDS (this_type);
c906108c 6407
f792889a 6408 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6409 of the method itself (TYPE_CODE_METHOD). */
6410 smash_to_method_type (fnp->type, type,
f792889a
DJ
6411 TYPE_TARGET_TYPE (this_type),
6412 TYPE_FIELDS (this_type),
6413 TYPE_NFIELDS (this_type),
6414 TYPE_VARARGS (this_type));
c906108c
SS
6415
6416 /* Handle static member functions.
c5aa993b
JM
6417 Dwarf2 has no clean way to discern C++ static and non-static
6418 member functions. G++ helps GDB by marking the first
6419 parameter for non-static member functions (which is the
6420 this pointer) as artificial. We obtain this information
6421 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6422 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6423 fnp->voffset = VOFFSET_STATIC;
6424 }
6425 else
e2e0b3e5 6426 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6427 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6428
6429 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6430 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6431 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
6432
6433 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6434 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6435
6436 /* Get accessibility. */
e142c38c 6437 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6438 if (attr)
6439 {
6440 switch (DW_UNSND (attr))
6441 {
c5aa993b
JM
6442 case DW_ACCESS_private:
6443 fnp->is_private = 1;
6444 break;
6445 case DW_ACCESS_protected:
6446 fnp->is_protected = 1;
6447 break;
c906108c
SS
6448 }
6449 }
6450
b02dede2 6451 /* Check for artificial methods. */
e142c38c 6452 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6453 if (attr && DW_UNSND (attr) != 0)
6454 fnp->is_artificial = 1;
6455
0d564a31 6456 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6457 function. For older versions of GCC, this is an offset in the
6458 appropriate virtual table, as specified by DW_AT_containing_type.
6459 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6460 to the object address. */
6461
e142c38c 6462 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6463 if (attr)
8e19ed76 6464 {
aec5aa8b 6465 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6466 {
aec5aa8b
TT
6467 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6468 {
6469 /* Old-style GCC. */
6470 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6471 }
6472 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6473 || (DW_BLOCK (attr)->size > 1
6474 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6475 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6476 {
6477 struct dwarf_block blk;
6478 int offset;
6479
6480 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6481 ? 1 : 2);
6482 blk.size = DW_BLOCK (attr)->size - offset;
6483 blk.data = DW_BLOCK (attr)->data + offset;
6484 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6485 if ((fnp->voffset % cu->header.addr_size) != 0)
6486 dwarf2_complex_location_expr_complaint ();
6487 else
6488 fnp->voffset /= cu->header.addr_size;
6489 fnp->voffset += 2;
6490 }
6491 else
6492 dwarf2_complex_location_expr_complaint ();
6493
6494 if (!fnp->fcontext)
6495 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6496 }
3690dd37 6497 else if (attr_form_is_section_offset (attr))
8e19ed76 6498 {
4d3c2250 6499 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6500 }
6501 else
6502 {
4d3c2250
KB
6503 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6504 fieldname);
8e19ed76 6505 }
0d564a31 6506 }
d48cc9dd
DJ
6507 else
6508 {
6509 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6510 if (attr && DW_UNSND (attr))
6511 {
6512 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6513 complaint (&symfile_complaints,
6514 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6515 fieldname, die->offset);
9655fd1a 6516 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6517 TYPE_CPLUS_DYNAMIC (type) = 1;
6518 }
6519 }
c906108c
SS
6520}
6521
6522/* Create the vector of member function fields, and attach it to the type. */
6523
6524static void
fba45db2 6525dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6526 struct dwarf2_cu *cu)
c906108c
SS
6527{
6528 struct fnfieldlist *flp;
6529 int total_length = 0;
6530 int i;
6531
b4ba55a1
JB
6532 if (cu->language == language_ada)
6533 error ("unexpected member functions in Ada type");
6534
c906108c
SS
6535 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6536 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6537 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6538
6539 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6540 {
6541 struct nextfnfield *nfp = flp->head;
6542 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6543 int k;
6544
6545 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6546 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6547 fn_flp->fn_fields = (struct fn_field *)
6548 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6549 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6550 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6551
6552 total_length += flp->length;
6553 }
6554
6555 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6556 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6557}
6558
1168df01
JB
6559/* Returns non-zero if NAME is the name of a vtable member in CU's
6560 language, zero otherwise. */
6561static int
6562is_vtable_name (const char *name, struct dwarf2_cu *cu)
6563{
6564 static const char vptr[] = "_vptr";
987504bb 6565 static const char vtable[] = "vtable";
1168df01 6566
987504bb
JJ
6567 /* Look for the C++ and Java forms of the vtable. */
6568 if ((cu->language == language_java
6569 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6570 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6571 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6572 return 1;
6573
6574 return 0;
6575}
6576
c0dd20ea 6577/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6578 functions, with the ABI-specified layout. If TYPE describes
6579 such a structure, smash it into a member function type.
61049d3b
DJ
6580
6581 GCC shouldn't do this; it should just output pointer to member DIEs.
6582 This is GCC PR debug/28767. */
c0dd20ea 6583
0b92b5bb
TT
6584static void
6585quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6586{
0b92b5bb 6587 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6588
6589 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6590 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6591 return;
c0dd20ea
DJ
6592
6593 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6594 if (TYPE_FIELD_NAME (type, 0) == NULL
6595 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6596 || TYPE_FIELD_NAME (type, 1) == NULL
6597 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6598 return;
c0dd20ea
DJ
6599
6600 /* Find the type of the method. */
0b92b5bb 6601 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6602 if (pfn_type == NULL
6603 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6604 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6605 return;
c0dd20ea
DJ
6606
6607 /* Look for the "this" argument. */
6608 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6609 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6610 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6611 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6612 return;
c0dd20ea
DJ
6613
6614 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6615 new_type = alloc_type (objfile);
6616 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6617 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6618 TYPE_VARARGS (pfn_type));
0b92b5bb 6619 smash_to_methodptr_type (type, new_type);
c0dd20ea 6620}
1168df01 6621
c906108c
SS
6622/* Called when we find the DIE that starts a structure or union scope
6623 (definition) to process all dies that define the members of the
6624 structure or union.
6625
6626 NOTE: we need to call struct_type regardless of whether or not the
6627 DIE has an at_name attribute, since it might be an anonymous
6628 structure or union. This gets the type entered into our set of
6629 user defined types.
6630
6631 However, if the structure is incomplete (an opaque struct/union)
6632 then suppress creating a symbol table entry for it since gdb only
6633 wants to find the one with the complete definition. Note that if
6634 it is complete, we just call new_symbol, which does it's own
6635 checking about whether the struct/union is anonymous or not (and
6636 suppresses creating a symbol table entry itself). */
6637
f792889a 6638static struct type *
134d01f1 6639read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6640{
e7c27a73 6641 struct objfile *objfile = cu->objfile;
c906108c
SS
6642 struct type *type;
6643 struct attribute *attr;
39cbfefa 6644 char *name;
d3f41bb1 6645 struct cleanup *back_to;
c906108c 6646
348e048f
DE
6647 /* If the definition of this type lives in .debug_types, read that type.
6648 Don't follow DW_AT_specification though, that will take us back up
6649 the chain and we want to go down. */
6650 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6651 if (attr)
6652 {
6653 struct dwarf2_cu *type_cu = cu;
6654 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6655
348e048f
DE
6656 /* We could just recurse on read_structure_type, but we need to call
6657 get_die_type to ensure only one type for this DIE is created.
6658 This is important, for example, because for c++ classes we need
6659 TYPE_NAME set which is only done by new_symbol. Blech. */
6660 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6661
6662 /* TYPE_CU may not be the same as CU.
6663 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6664 return set_die_type (die, type, cu);
6665 }
6666
d3f41bb1
TT
6667 back_to = make_cleanup (null_cleanup, 0);
6668
c0dd20ea 6669 type = alloc_type (objfile);
c906108c 6670 INIT_CPLUS_SPECIFIC (type);
93311388 6671
39cbfefa
DJ
6672 name = dwarf2_name (die, cu);
6673 if (name != NULL)
c906108c 6674 {
987504bb
JJ
6675 if (cu->language == language_cplus
6676 || cu->language == language_java)
63d06c5c 6677 {
3da10d80
KS
6678 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6679
6680 /* dwarf2_full_name might have already finished building the DIE's
6681 type. If so, there is no need to continue. */
6682 if (get_die_type (die, cu) != NULL)
6683 return get_die_type (die, cu);
6684
6685 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6686 if (die->tag == DW_TAG_structure_type
6687 || die->tag == DW_TAG_class_type)
6688 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6689 }
6690 else
6691 {
d8151005
DJ
6692 /* The name is already allocated along with this objfile, so
6693 we don't need to duplicate it for the type. */
94af9270
KS
6694 TYPE_TAG_NAME (type) = (char *) name;
6695 if (die->tag == DW_TAG_class_type)
6696 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6697 }
c906108c
SS
6698 }
6699
6700 if (die->tag == DW_TAG_structure_type)
6701 {
6702 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6703 }
6704 else if (die->tag == DW_TAG_union_type)
6705 {
6706 TYPE_CODE (type) = TYPE_CODE_UNION;
6707 }
6708 else
6709 {
c906108c
SS
6710 TYPE_CODE (type) = TYPE_CODE_CLASS;
6711 }
6712
0cc2414c
TT
6713 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6714 TYPE_DECLARED_CLASS (type) = 1;
6715
e142c38c 6716 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6717 if (attr)
6718 {
6719 TYPE_LENGTH (type) = DW_UNSND (attr);
6720 }
6721 else
6722 {
6723 TYPE_LENGTH (type) = 0;
6724 }
6725
876cecd0 6726 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6727 if (die_is_declaration (die, cu))
876cecd0 6728 TYPE_STUB (type) = 1;
a6c727b2
DJ
6729 else if (attr == NULL && die->child == NULL
6730 && producer_is_realview (cu->producer))
6731 /* RealView does not output the required DW_AT_declaration
6732 on incomplete types. */
6733 TYPE_STUB (type) = 1;
dc718098 6734
c906108c
SS
6735 /* We need to add the type field to the die immediately so we don't
6736 infinitely recurse when dealing with pointers to the structure
6737 type within the structure itself. */
1c379e20 6738 set_die_type (die, type, cu);
c906108c 6739
7e314c57
JK
6740 /* set_die_type should be already done. */
6741 set_descriptive_type (type, die, cu);
6742
e142c38c 6743 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6744 {
6745 struct field_info fi;
6746 struct die_info *child_die;
34eaf542 6747 VEC (symbolp) *template_args = NULL;
c906108c
SS
6748
6749 memset (&fi, 0, sizeof (struct field_info));
6750
639d11d3 6751 child_die = die->child;
c906108c
SS
6752
6753 while (child_die && child_die->tag)
6754 {
a9a9bd0f
DC
6755 if (child_die->tag == DW_TAG_member
6756 || child_die->tag == DW_TAG_variable)
c906108c 6757 {
a9a9bd0f
DC
6758 /* NOTE: carlton/2002-11-05: A C++ static data member
6759 should be a DW_TAG_member that is a declaration, but
6760 all versions of G++ as of this writing (so through at
6761 least 3.2.1) incorrectly generate DW_TAG_variable
6762 tags for them instead. */
e7c27a73 6763 dwarf2_add_field (&fi, child_die, cu);
c906108c 6764 }
8713b1b1 6765 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6766 {
6767 /* C++ member function. */
e7c27a73 6768 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6769 }
6770 else if (child_die->tag == DW_TAG_inheritance)
6771 {
6772 /* C++ base class field. */
e7c27a73 6773 dwarf2_add_field (&fi, child_die, cu);
c906108c 6774 }
98751a41
JK
6775 else if (child_die->tag == DW_TAG_typedef)
6776 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6777 else if (child_die->tag == DW_TAG_template_type_param
6778 || child_die->tag == DW_TAG_template_value_param)
6779 {
6780 struct symbol *arg = new_symbol (child_die, NULL, cu);
6781
6782 VEC_safe_push (symbolp, template_args, arg);
6783 }
6784
c906108c
SS
6785 child_die = sibling_die (child_die);
6786 }
6787
34eaf542
TT
6788 /* Attach template arguments to type. */
6789 if (! VEC_empty (symbolp, template_args))
6790 {
6791 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6792 TYPE_N_TEMPLATE_ARGUMENTS (type)
6793 = VEC_length (symbolp, template_args);
6794 TYPE_TEMPLATE_ARGUMENTS (type)
6795 = obstack_alloc (&objfile->objfile_obstack,
6796 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6797 * sizeof (struct symbol *)));
6798 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6799 VEC_address (symbolp, template_args),
6800 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6801 * sizeof (struct symbol *)));
6802 VEC_free (symbolp, template_args);
6803 }
6804
c906108c
SS
6805 /* Attach fields and member functions to the type. */
6806 if (fi.nfields)
e7c27a73 6807 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6808 if (fi.nfnfields)
6809 {
e7c27a73 6810 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6811
c5aa993b 6812 /* Get the type which refers to the base class (possibly this
c906108c 6813 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6814 class from the DW_AT_containing_type attribute. This use of
6815 DW_AT_containing_type is a GNU extension. */
c906108c 6816
e142c38c 6817 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6818 {
e7c27a73 6819 struct type *t = die_containing_type (die, cu);
c906108c
SS
6820
6821 TYPE_VPTR_BASETYPE (type) = t;
6822 if (type == t)
6823 {
c906108c
SS
6824 int i;
6825
6826 /* Our own class provides vtbl ptr. */
6827 for (i = TYPE_NFIELDS (t) - 1;
6828 i >= TYPE_N_BASECLASSES (t);
6829 --i)
6830 {
6831 char *fieldname = TYPE_FIELD_NAME (t, i);
6832
1168df01 6833 if (is_vtable_name (fieldname, cu))
c906108c
SS
6834 {
6835 TYPE_VPTR_FIELDNO (type) = i;
6836 break;
6837 }
6838 }
6839
6840 /* Complain if virtual function table field not found. */
6841 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6842 complaint (&symfile_complaints,
e2e0b3e5 6843 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6844 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6845 "");
c906108c
SS
6846 }
6847 else
6848 {
6849 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6850 }
6851 }
f6235d4c
EZ
6852 else if (cu->producer
6853 && strncmp (cu->producer,
6854 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6855 {
6856 /* The IBM XLC compiler does not provide direct indication
6857 of the containing type, but the vtable pointer is
6858 always named __vfp. */
6859
6860 int i;
6861
6862 for (i = TYPE_NFIELDS (type) - 1;
6863 i >= TYPE_N_BASECLASSES (type);
6864 --i)
6865 {
6866 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6867 {
6868 TYPE_VPTR_FIELDNO (type) = i;
6869 TYPE_VPTR_BASETYPE (type) = type;
6870 break;
6871 }
6872 }
6873 }
c906108c 6874 }
98751a41
JK
6875
6876 /* Copy fi.typedef_field_list linked list elements content into the
6877 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6878 if (fi.typedef_field_list)
6879 {
6880 int i = fi.typedef_field_list_count;
6881
a0d7a4ff 6882 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6883 TYPE_TYPEDEF_FIELD_ARRAY (type)
6884 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6885 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6886
6887 /* Reverse the list order to keep the debug info elements order. */
6888 while (--i >= 0)
6889 {
6890 struct typedef_field *dest, *src;
6e70227d 6891
98751a41
JK
6892 dest = &TYPE_TYPEDEF_FIELD (type, i);
6893 src = &fi.typedef_field_list->field;
6894 fi.typedef_field_list = fi.typedef_field_list->next;
6895 *dest = *src;
6896 }
6897 }
c906108c 6898 }
63d06c5c 6899
0b92b5bb
TT
6900 quirk_gcc_member_function_pointer (type, cu->objfile);
6901
0114d602 6902 do_cleanups (back_to);
f792889a 6903 return type;
c906108c
SS
6904}
6905
134d01f1
DJ
6906static void
6907process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6908{
90aeadfc 6909 struct die_info *child_die = die->child;
f792889a 6910 struct type *this_type;
c906108c 6911
f792889a
DJ
6912 this_type = get_die_type (die, cu);
6913 if (this_type == NULL)
6914 this_type = read_structure_type (die, cu);
c906108c 6915
90aeadfc
DC
6916 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6917 snapshots) has been known to create a die giving a declaration
6918 for a class that has, as a child, a die giving a definition for a
6919 nested class. So we have to process our children even if the
6920 current die is a declaration. Normally, of course, a declaration
6921 won't have any children at all. */
134d01f1 6922
90aeadfc
DC
6923 while (child_die != NULL && child_die->tag)
6924 {
6925 if (child_die->tag == DW_TAG_member
6926 || child_die->tag == DW_TAG_variable
34eaf542
TT
6927 || child_die->tag == DW_TAG_inheritance
6928 || child_die->tag == DW_TAG_template_value_param
6929 || child_die->tag == DW_TAG_template_type_param)
134d01f1 6930 {
90aeadfc 6931 /* Do nothing. */
134d01f1 6932 }
90aeadfc
DC
6933 else
6934 process_die (child_die, cu);
134d01f1 6935
90aeadfc 6936 child_die = sibling_die (child_die);
134d01f1
DJ
6937 }
6938
fa4028e9
JB
6939 /* Do not consider external references. According to the DWARF standard,
6940 these DIEs are identified by the fact that they have no byte_size
6941 attribute, and a declaration attribute. */
6942 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6943 || !die_is_declaration (die, cu))
f792889a 6944 new_symbol (die, this_type, cu);
134d01f1
DJ
6945}
6946
6947/* Given a DW_AT_enumeration_type die, set its type. We do not
6948 complete the type's fields yet, or create any symbols. */
c906108c 6949
f792889a 6950static struct type *
134d01f1 6951read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6952{
e7c27a73 6953 struct objfile *objfile = cu->objfile;
c906108c 6954 struct type *type;
c906108c 6955 struct attribute *attr;
0114d602 6956 const char *name;
134d01f1 6957
348e048f
DE
6958 /* If the definition of this type lives in .debug_types, read that type.
6959 Don't follow DW_AT_specification though, that will take us back up
6960 the chain and we want to go down. */
6961 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6962 if (attr)
6963 {
6964 struct dwarf2_cu *type_cu = cu;
6965 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6966
348e048f 6967 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6968
6969 /* TYPE_CU may not be the same as CU.
6970 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6971 return set_die_type (die, type, cu);
6972 }
6973
c906108c
SS
6974 type = alloc_type (objfile);
6975
6976 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 6977 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 6978 if (name != NULL)
0114d602 6979 TYPE_TAG_NAME (type) = (char *) name;
c906108c 6980
e142c38c 6981 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6982 if (attr)
6983 {
6984 TYPE_LENGTH (type) = DW_UNSND (attr);
6985 }
6986 else
6987 {
6988 TYPE_LENGTH (type) = 0;
6989 }
6990
137033e9
JB
6991 /* The enumeration DIE can be incomplete. In Ada, any type can be
6992 declared as private in the package spec, and then defined only
6993 inside the package body. Such types are known as Taft Amendment
6994 Types. When another package uses such a type, an incomplete DIE
6995 may be generated by the compiler. */
02eb380e 6996 if (die_is_declaration (die, cu))
876cecd0 6997 TYPE_STUB (type) = 1;
02eb380e 6998
f792889a 6999 return set_die_type (die, type, cu);
134d01f1
DJ
7000}
7001
7002/* Given a pointer to a die which begins an enumeration, process all
7003 the dies that define the members of the enumeration, and create the
7004 symbol for the enumeration type.
7005
7006 NOTE: We reverse the order of the element list. */
7007
7008static void
7009process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7010{
f792889a 7011 struct type *this_type;
134d01f1 7012
f792889a
DJ
7013 this_type = get_die_type (die, cu);
7014 if (this_type == NULL)
7015 this_type = read_enumeration_type (die, cu);
9dc481d3 7016
639d11d3 7017 if (die->child != NULL)
c906108c 7018 {
9dc481d3
DE
7019 struct die_info *child_die;
7020 struct symbol *sym;
7021 struct field *fields = NULL;
7022 int num_fields = 0;
7023 int unsigned_enum = 1;
7024 char *name;
7025
639d11d3 7026 child_die = die->child;
c906108c
SS
7027 while (child_die && child_die->tag)
7028 {
7029 if (child_die->tag != DW_TAG_enumerator)
7030 {
e7c27a73 7031 process_die (child_die, cu);
c906108c
SS
7032 }
7033 else
7034 {
39cbfefa
DJ
7035 name = dwarf2_name (child_die, cu);
7036 if (name)
c906108c 7037 {
f792889a 7038 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7039 if (SYMBOL_VALUE (sym) < 0)
7040 unsigned_enum = 0;
7041
7042 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7043 {
7044 fields = (struct field *)
7045 xrealloc (fields,
7046 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7047 * sizeof (struct field));
c906108c
SS
7048 }
7049
3567439c 7050 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7051 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7052 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7053 FIELD_BITSIZE (fields[num_fields]) = 0;
7054
7055 num_fields++;
7056 }
7057 }
7058
7059 child_die = sibling_die (child_die);
7060 }
7061
7062 if (num_fields)
7063 {
f792889a
DJ
7064 TYPE_NFIELDS (this_type) = num_fields;
7065 TYPE_FIELDS (this_type) = (struct field *)
7066 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7067 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7068 sizeof (struct field) * num_fields);
b8c9b27d 7069 xfree (fields);
c906108c
SS
7070 }
7071 if (unsigned_enum)
876cecd0 7072 TYPE_UNSIGNED (this_type) = 1;
c906108c 7073 }
134d01f1 7074
f792889a 7075 new_symbol (die, this_type, cu);
c906108c
SS
7076}
7077
7078/* Extract all information from a DW_TAG_array_type DIE and put it in
7079 the DIE's type field. For now, this only handles one dimensional
7080 arrays. */
7081
f792889a 7082static struct type *
e7c27a73 7083read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7084{
e7c27a73 7085 struct objfile *objfile = cu->objfile;
c906108c 7086 struct die_info *child_die;
7e314c57 7087 struct type *type;
c906108c
SS
7088 struct type *element_type, *range_type, *index_type;
7089 struct type **range_types = NULL;
7090 struct attribute *attr;
7091 int ndim = 0;
7092 struct cleanup *back_to;
39cbfefa 7093 char *name;
c906108c 7094
e7c27a73 7095 element_type = die_type (die, cu);
c906108c 7096
7e314c57
JK
7097 /* The die_type call above may have already set the type for this DIE. */
7098 type = get_die_type (die, cu);
7099 if (type)
7100 return type;
7101
c906108c
SS
7102 /* Irix 6.2 native cc creates array types without children for
7103 arrays with unspecified length. */
639d11d3 7104 if (die->child == NULL)
c906108c 7105 {
46bf5051 7106 index_type = objfile_type (objfile)->builtin_int;
c906108c 7107 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7108 type = create_array_type (NULL, element_type, range_type);
7109 return set_die_type (die, type, cu);
c906108c
SS
7110 }
7111
7112 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7113 child_die = die->child;
c906108c
SS
7114 while (child_die && child_die->tag)
7115 {
7116 if (child_die->tag == DW_TAG_subrange_type)
7117 {
f792889a 7118 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7119
f792889a 7120 if (child_type != NULL)
a02abb62
JB
7121 {
7122 /* The range type was succesfully read. Save it for
7123 the array type creation. */
7124 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7125 {
7126 range_types = (struct type **)
7127 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7128 * sizeof (struct type *));
7129 if (ndim == 0)
7130 make_cleanup (free_current_contents, &range_types);
7131 }
f792889a 7132 range_types[ndim++] = child_type;
a02abb62 7133 }
c906108c
SS
7134 }
7135 child_die = sibling_die (child_die);
7136 }
7137
7138 /* Dwarf2 dimensions are output from left to right, create the
7139 necessary array types in backwards order. */
7ca2d3a3 7140
c906108c 7141 type = element_type;
7ca2d3a3
DL
7142
7143 if (read_array_order (die, cu) == DW_ORD_col_major)
7144 {
7145 int i = 0;
9a619af0 7146
7ca2d3a3
DL
7147 while (i < ndim)
7148 type = create_array_type (NULL, type, range_types[i++]);
7149 }
7150 else
7151 {
7152 while (ndim-- > 0)
7153 type = create_array_type (NULL, type, range_types[ndim]);
7154 }
c906108c 7155
f5f8a009
EZ
7156 /* Understand Dwarf2 support for vector types (like they occur on
7157 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7158 array type. This is not part of the Dwarf2/3 standard yet, but a
7159 custom vendor extension. The main difference between a regular
7160 array and the vector variant is that vectors are passed by value
7161 to functions. */
e142c38c 7162 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7163 if (attr)
ea37ba09 7164 make_vector_type (type);
f5f8a009 7165
39cbfefa
DJ
7166 name = dwarf2_name (die, cu);
7167 if (name)
7168 TYPE_NAME (type) = name;
6e70227d 7169
7e314c57
JK
7170 /* Install the type in the die. */
7171 set_die_type (die, type, cu);
7172
7173 /* set_die_type should be already done. */
b4ba55a1
JB
7174 set_descriptive_type (type, die, cu);
7175
c906108c
SS
7176 do_cleanups (back_to);
7177
7e314c57 7178 return type;
c906108c
SS
7179}
7180
7ca2d3a3 7181static enum dwarf_array_dim_ordering
6e70227d 7182read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7183{
7184 struct attribute *attr;
7185
7186 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7187
7188 if (attr) return DW_SND (attr);
7189
7190 /*
7191 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 7192 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
7193 laid out as per normal fortran.
7194
6e70227d 7195 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
7196 version checking.
7197 */
7198
905e0470
PM
7199 if (cu->language == language_fortran
7200 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7201 {
7202 return DW_ORD_row_major;
7203 }
7204
6e70227d 7205 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7206 {
7207 case array_column_major:
7208 return DW_ORD_col_major;
7209 case array_row_major:
7210 default:
7211 return DW_ORD_row_major;
7212 };
7213}
7214
72019c9c
GM
7215/* Extract all information from a DW_TAG_set_type DIE and put it in
7216 the DIE's type field. */
7217
f792889a 7218static struct type *
72019c9c
GM
7219read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7220{
7e314c57
JK
7221 struct type *domain_type, *set_type;
7222 struct attribute *attr;
f792889a 7223
7e314c57
JK
7224 domain_type = die_type (die, cu);
7225
7226 /* The die_type call above may have already set the type for this DIE. */
7227 set_type = get_die_type (die, cu);
7228 if (set_type)
7229 return set_type;
7230
7231 set_type = create_set_type (NULL, domain_type);
7232
7233 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7234 if (attr)
7235 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7236
f792889a 7237 return set_die_type (die, set_type, cu);
72019c9c 7238}
7ca2d3a3 7239
c906108c
SS
7240/* First cut: install each common block member as a global variable. */
7241
7242static void
e7c27a73 7243read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7244{
7245 struct die_info *child_die;
7246 struct attribute *attr;
7247 struct symbol *sym;
7248 CORE_ADDR base = (CORE_ADDR) 0;
7249
e142c38c 7250 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7251 if (attr)
7252 {
8e19ed76
PS
7253 /* Support the .debug_loc offsets */
7254 if (attr_form_is_block (attr))
7255 {
e7c27a73 7256 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7257 }
3690dd37 7258 else if (attr_form_is_section_offset (attr))
8e19ed76 7259 {
4d3c2250 7260 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7261 }
7262 else
7263 {
4d3c2250
KB
7264 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7265 "common block member");
8e19ed76 7266 }
c906108c 7267 }
639d11d3 7268 if (die->child != NULL)
c906108c 7269 {
639d11d3 7270 child_die = die->child;
c906108c
SS
7271 while (child_die && child_die->tag)
7272 {
e7c27a73 7273 sym = new_symbol (child_die, NULL, cu);
e142c38c 7274 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
c906108c
SS
7275 if (attr)
7276 {
d4b96c9a
JK
7277 CORE_ADDR byte_offset = 0;
7278
7279 if (attr_form_is_section_offset (attr))
7280 dwarf2_complex_location_expr_complaint ();
7281 else if (attr_form_is_constant (attr))
7282 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7283 else if (attr_form_is_block (attr))
7284 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7285 else
7286 dwarf2_complex_location_expr_complaint ();
7287
7288 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7289 add_symbol_to_list (sym, &global_symbols);
7290 }
7291 child_die = sibling_die (child_die);
7292 }
7293 }
7294}
7295
0114d602 7296/* Create a type for a C++ namespace. */
d9fa45fe 7297
0114d602
DJ
7298static struct type *
7299read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7300{
e7c27a73 7301 struct objfile *objfile = cu->objfile;
0114d602 7302 const char *previous_prefix, *name;
9219021c 7303 int is_anonymous;
0114d602
DJ
7304 struct type *type;
7305
7306 /* For extensions, reuse the type of the original namespace. */
7307 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7308 {
7309 struct die_info *ext_die;
7310 struct dwarf2_cu *ext_cu = cu;
9a619af0 7311
0114d602
DJ
7312 ext_die = dwarf2_extension (die, &ext_cu);
7313 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7314
7315 /* EXT_CU may not be the same as CU.
7316 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7317 return set_die_type (die, type, cu);
7318 }
9219021c 7319
e142c38c 7320 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7321
7322 /* Now build the name of the current namespace. */
7323
0114d602
DJ
7324 previous_prefix = determine_prefix (die, cu);
7325 if (previous_prefix[0] != '\0')
7326 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7327 previous_prefix, name, 0, cu);
0114d602
DJ
7328
7329 /* Create the type. */
7330 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7331 objfile);
7332 TYPE_NAME (type) = (char *) name;
7333 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7334
60531b24 7335 return set_die_type (die, type, cu);
0114d602
DJ
7336}
7337
7338/* Read a C++ namespace. */
7339
7340static void
7341read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7342{
7343 struct objfile *objfile = cu->objfile;
7344 const char *name;
7345 int is_anonymous;
9219021c 7346
5c4e30ca
DC
7347 /* Add a symbol associated to this if we haven't seen the namespace
7348 before. Also, add a using directive if it's an anonymous
7349 namespace. */
9219021c 7350
f2f0e013 7351 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7352 {
7353 struct type *type;
7354
0114d602 7355 type = read_type_die (die, cu);
e7c27a73 7356 new_symbol (die, type, cu);
5c4e30ca 7357
0114d602 7358 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7359 if (is_anonymous)
0114d602
DJ
7360 {
7361 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7362
c0cc3a76 7363 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7364 NULL, &objfile->objfile_obstack);
0114d602 7365 }
5c4e30ca 7366 }
9219021c 7367
639d11d3 7368 if (die->child != NULL)
d9fa45fe 7369 {
639d11d3 7370 struct die_info *child_die = die->child;
6e70227d 7371
d9fa45fe
DC
7372 while (child_die && child_die->tag)
7373 {
e7c27a73 7374 process_die (child_die, cu);
d9fa45fe
DC
7375 child_die = sibling_die (child_die);
7376 }
7377 }
38d518c9
EZ
7378}
7379
f55ee35c
JK
7380/* Read a Fortran module as type. This DIE can be only a declaration used for
7381 imported module. Still we need that type as local Fortran "use ... only"
7382 declaration imports depend on the created type in determine_prefix. */
7383
7384static struct type *
7385read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7386{
7387 struct objfile *objfile = cu->objfile;
7388 char *module_name;
7389 struct type *type;
7390
7391 module_name = dwarf2_name (die, cu);
7392 if (!module_name)
7393 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7394 die->offset);
7395 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7396
7397 /* determine_prefix uses TYPE_TAG_NAME. */
7398 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7399
7400 return set_die_type (die, type, cu);
7401}
7402
5d7cb8df
JK
7403/* Read a Fortran module. */
7404
7405static void
7406read_module (struct die_info *die, struct dwarf2_cu *cu)
7407{
7408 struct die_info *child_die = die->child;
7409
5d7cb8df
JK
7410 while (child_die && child_die->tag)
7411 {
7412 process_die (child_die, cu);
7413 child_die = sibling_die (child_die);
7414 }
7415}
7416
38d518c9
EZ
7417/* Return the name of the namespace represented by DIE. Set
7418 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7419 namespace. */
7420
7421static const char *
e142c38c 7422namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7423{
7424 struct die_info *current_die;
7425 const char *name = NULL;
7426
7427 /* Loop through the extensions until we find a name. */
7428
7429 for (current_die = die;
7430 current_die != NULL;
f2f0e013 7431 current_die = dwarf2_extension (die, &cu))
38d518c9 7432 {
e142c38c 7433 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7434 if (name != NULL)
7435 break;
7436 }
7437
7438 /* Is it an anonymous namespace? */
7439
7440 *is_anonymous = (name == NULL);
7441 if (*is_anonymous)
7442 name = "(anonymous namespace)";
7443
7444 return name;
d9fa45fe
DC
7445}
7446
c906108c
SS
7447/* Extract all information from a DW_TAG_pointer_type DIE and add to
7448 the user defined type vector. */
7449
f792889a 7450static struct type *
e7c27a73 7451read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7452{
5e2b427d 7453 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7454 struct comp_unit_head *cu_header = &cu->header;
c906108c 7455 struct type *type;
8b2dbe47
KB
7456 struct attribute *attr_byte_size;
7457 struct attribute *attr_address_class;
7458 int byte_size, addr_class;
7e314c57
JK
7459 struct type *target_type;
7460
7461 target_type = die_type (die, cu);
c906108c 7462
7e314c57
JK
7463 /* The die_type call above may have already set the type for this DIE. */
7464 type = get_die_type (die, cu);
7465 if (type)
7466 return type;
7467
7468 type = lookup_pointer_type (target_type);
8b2dbe47 7469
e142c38c 7470 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7471 if (attr_byte_size)
7472 byte_size = DW_UNSND (attr_byte_size);
c906108c 7473 else
8b2dbe47
KB
7474 byte_size = cu_header->addr_size;
7475
e142c38c 7476 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7477 if (attr_address_class)
7478 addr_class = DW_UNSND (attr_address_class);
7479 else
7480 addr_class = DW_ADDR_none;
7481
7482 /* If the pointer size or address class is different than the
7483 default, create a type variant marked as such and set the
7484 length accordingly. */
7485 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7486 {
5e2b427d 7487 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7488 {
7489 int type_flags;
7490
849957d9 7491 type_flags = gdbarch_address_class_type_flags
5e2b427d 7492 (gdbarch, byte_size, addr_class);
876cecd0
TT
7493 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7494 == 0);
8b2dbe47
KB
7495 type = make_type_with_address_space (type, type_flags);
7496 }
7497 else if (TYPE_LENGTH (type) != byte_size)
7498 {
e2e0b3e5 7499 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 7500 }
6e70227d 7501 else
9a619af0
MS
7502 {
7503 /* Should we also complain about unhandled address classes? */
7504 }
c906108c 7505 }
8b2dbe47
KB
7506
7507 TYPE_LENGTH (type) = byte_size;
f792889a 7508 return set_die_type (die, type, cu);
c906108c
SS
7509}
7510
7511/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7512 the user defined type vector. */
7513
f792889a 7514static struct type *
e7c27a73 7515read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7516{
7517 struct type *type;
7518 struct type *to_type;
7519 struct type *domain;
7520
e7c27a73
DJ
7521 to_type = die_type (die, cu);
7522 domain = die_containing_type (die, cu);
0d5de010 7523
7e314c57
JK
7524 /* The calls above may have already set the type for this DIE. */
7525 type = get_die_type (die, cu);
7526 if (type)
7527 return type;
7528
0d5de010
DJ
7529 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7530 type = lookup_methodptr_type (to_type);
7531 else
7532 type = lookup_memberptr_type (to_type, domain);
c906108c 7533
f792889a 7534 return set_die_type (die, type, cu);
c906108c
SS
7535}
7536
7537/* Extract all information from a DW_TAG_reference_type DIE and add to
7538 the user defined type vector. */
7539
f792889a 7540static struct type *
e7c27a73 7541read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7542{
e7c27a73 7543 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7544 struct type *type, *target_type;
c906108c
SS
7545 struct attribute *attr;
7546
7e314c57
JK
7547 target_type = die_type (die, cu);
7548
7549 /* The die_type call above may have already set the type for this DIE. */
7550 type = get_die_type (die, cu);
7551 if (type)
7552 return type;
7553
7554 type = lookup_reference_type (target_type);
e142c38c 7555 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7556 if (attr)
7557 {
7558 TYPE_LENGTH (type) = DW_UNSND (attr);
7559 }
7560 else
7561 {
107d2387 7562 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7563 }
f792889a 7564 return set_die_type (die, type, cu);
c906108c
SS
7565}
7566
f792889a 7567static struct type *
e7c27a73 7568read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7569{
f792889a 7570 struct type *base_type, *cv_type;
c906108c 7571
e7c27a73 7572 base_type = die_type (die, cu);
7e314c57
JK
7573
7574 /* The die_type call above may have already set the type for this DIE. */
7575 cv_type = get_die_type (die, cu);
7576 if (cv_type)
7577 return cv_type;
7578
f792889a
DJ
7579 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7580 return set_die_type (die, cv_type, cu);
c906108c
SS
7581}
7582
f792889a 7583static struct type *
e7c27a73 7584read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7585{
f792889a 7586 struct type *base_type, *cv_type;
c906108c 7587
e7c27a73 7588 base_type = die_type (die, cu);
7e314c57
JK
7589
7590 /* The die_type call above may have already set the type for this DIE. */
7591 cv_type = get_die_type (die, cu);
7592 if (cv_type)
7593 return cv_type;
7594
f792889a
DJ
7595 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7596 return set_die_type (die, cv_type, cu);
c906108c
SS
7597}
7598
7599/* Extract all information from a DW_TAG_string_type DIE and add to
7600 the user defined type vector. It isn't really a user defined type,
7601 but it behaves like one, with other DIE's using an AT_user_def_type
7602 attribute to reference it. */
7603
f792889a 7604static struct type *
e7c27a73 7605read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7606{
e7c27a73 7607 struct objfile *objfile = cu->objfile;
3b7538c0 7608 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7609 struct type *type, *range_type, *index_type, *char_type;
7610 struct attribute *attr;
7611 unsigned int length;
7612
e142c38c 7613 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7614 if (attr)
7615 {
7616 length = DW_UNSND (attr);
7617 }
7618 else
7619 {
b21b22e0 7620 /* check for the DW_AT_byte_size attribute */
e142c38c 7621 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7622 if (attr)
7623 {
7624 length = DW_UNSND (attr);
7625 }
7626 else
7627 {
7628 length = 1;
7629 }
c906108c 7630 }
6ccb9162 7631
46bf5051 7632 index_type = objfile_type (objfile)->builtin_int;
c906108c 7633 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7634 char_type = language_string_char_type (cu->language_defn, gdbarch);
7635 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7636
f792889a 7637 return set_die_type (die, type, cu);
c906108c
SS
7638}
7639
7640/* Handle DIES due to C code like:
7641
7642 struct foo
c5aa993b
JM
7643 {
7644 int (*funcp)(int a, long l);
7645 int b;
7646 };
c906108c
SS
7647
7648 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7649 */
c906108c 7650
f792889a 7651static struct type *
e7c27a73 7652read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7653{
7654 struct type *type; /* Type that this function returns */
7655 struct type *ftype; /* Function that returns above type */
7656 struct attribute *attr;
7657
e7c27a73 7658 type = die_type (die, cu);
7e314c57
JK
7659
7660 /* The die_type call above may have already set the type for this DIE. */
7661 ftype = get_die_type (die, cu);
7662 if (ftype)
7663 return ftype;
7664
0c8b41f1 7665 ftype = lookup_function_type (type);
c906108c 7666
5b8101ae 7667 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7668 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7669 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7670 || cu->language == language_cplus
5b8101ae
PM
7671 || cu->language == language_java
7672 || cu->language == language_pascal)
876cecd0 7673 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7674 else if (producer_is_realview (cu->producer))
7675 /* RealView does not emit DW_AT_prototyped. We can not
7676 distinguish prototyped and unprototyped functions; default to
7677 prototyped, since that is more common in modern code (and
7678 RealView warns about unprototyped functions). */
7679 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7680
c055b101
CV
7681 /* Store the calling convention in the type if it's available in
7682 the subroutine die. Otherwise set the calling convention to
7683 the default value DW_CC_normal. */
7684 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7685 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7686
7687 /* We need to add the subroutine type to the die immediately so
7688 we don't infinitely recurse when dealing with parameters
7689 declared as the same subroutine type. */
7690 set_die_type (die, ftype, cu);
6e70227d 7691
639d11d3 7692 if (die->child != NULL)
c906108c 7693 {
8072405b 7694 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7695 struct die_info *child_die;
8072405b 7696 int nparams, iparams;
c906108c
SS
7697
7698 /* Count the number of parameters.
7699 FIXME: GDB currently ignores vararg functions, but knows about
7700 vararg member functions. */
8072405b 7701 nparams = 0;
639d11d3 7702 child_die = die->child;
c906108c
SS
7703 while (child_die && child_die->tag)
7704 {
7705 if (child_die->tag == DW_TAG_formal_parameter)
7706 nparams++;
7707 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7708 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7709 child_die = sibling_die (child_die);
7710 }
7711
7712 /* Allocate storage for parameters and fill them in. */
7713 TYPE_NFIELDS (ftype) = nparams;
7714 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7715 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7716
8072405b
JK
7717 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7718 even if we error out during the parameters reading below. */
7719 for (iparams = 0; iparams < nparams; iparams++)
7720 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7721
7722 iparams = 0;
639d11d3 7723 child_die = die->child;
c906108c
SS
7724 while (child_die && child_die->tag)
7725 {
7726 if (child_die->tag == DW_TAG_formal_parameter)
7727 {
3ce3b1ba
PA
7728 struct type *arg_type;
7729
7730 /* DWARF version 2 has no clean way to discern C++
7731 static and non-static member functions. G++ helps
7732 GDB by marking the first parameter for non-static
7733 member functions (which is the this pointer) as
7734 artificial. We pass this information to
7735 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7736
7737 DWARF version 3 added DW_AT_object_pointer, which GCC
7738 4.5 does not yet generate. */
e142c38c 7739 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7740 if (attr)
7741 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7742 else
418835cc
KS
7743 {
7744 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7745
7746 /* GCC/43521: In java, the formal parameter
7747 "this" is sometimes not marked with DW_AT_artificial. */
7748 if (cu->language == language_java)
7749 {
7750 const char *name = dwarf2_name (child_die, cu);
9a619af0 7751
418835cc
KS
7752 if (name && !strcmp (name, "this"))
7753 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7754 }
7755 }
3ce3b1ba
PA
7756 arg_type = die_type (child_die, cu);
7757
7758 /* RealView does not mark THIS as const, which the testsuite
7759 expects. GCC marks THIS as const in method definitions,
7760 but not in the class specifications (GCC PR 43053). */
7761 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7762 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7763 {
7764 int is_this = 0;
7765 struct dwarf2_cu *arg_cu = cu;
7766 const char *name = dwarf2_name (child_die, cu);
7767
7768 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7769 if (attr)
7770 {
7771 /* If the compiler emits this, use it. */
7772 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7773 is_this = 1;
7774 }
7775 else if (name && strcmp (name, "this") == 0)
7776 /* Function definitions will have the argument names. */
7777 is_this = 1;
7778 else if (name == NULL && iparams == 0)
7779 /* Declarations may not have the names, so like
7780 elsewhere in GDB, assume an artificial first
7781 argument is "this". */
7782 is_this = 1;
7783
7784 if (is_this)
7785 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7786 arg_type, 0);
7787 }
7788
7789 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
7790 iparams++;
7791 }
7792 child_die = sibling_die (child_die);
7793 }
7794 }
7795
76c10ea2 7796 return ftype;
c906108c
SS
7797}
7798
f792889a 7799static struct type *
e7c27a73 7800read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7801{
e7c27a73 7802 struct objfile *objfile = cu->objfile;
0114d602 7803 const char *name = NULL;
f792889a 7804 struct type *this_type;
c906108c 7805
94af9270 7806 name = dwarf2_full_name (NULL, die, cu);
f792889a 7807 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7808 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7809 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7810 set_die_type (die, this_type, cu);
7811 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7812 return this_type;
c906108c
SS
7813}
7814
7815/* Find a representation of a given base type and install
7816 it in the TYPE field of the die. */
7817
f792889a 7818static struct type *
e7c27a73 7819read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7820{
e7c27a73 7821 struct objfile *objfile = cu->objfile;
c906108c
SS
7822 struct type *type;
7823 struct attribute *attr;
7824 int encoding = 0, size = 0;
39cbfefa 7825 char *name;
6ccb9162
UW
7826 enum type_code code = TYPE_CODE_INT;
7827 int type_flags = 0;
7828 struct type *target_type = NULL;
c906108c 7829
e142c38c 7830 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7831 if (attr)
7832 {
7833 encoding = DW_UNSND (attr);
7834 }
e142c38c 7835 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7836 if (attr)
7837 {
7838 size = DW_UNSND (attr);
7839 }
39cbfefa 7840 name = dwarf2_name (die, cu);
6ccb9162 7841 if (!name)
c906108c 7842 {
6ccb9162
UW
7843 complaint (&symfile_complaints,
7844 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7845 }
6ccb9162
UW
7846
7847 switch (encoding)
c906108c 7848 {
6ccb9162
UW
7849 case DW_ATE_address:
7850 /* Turn DW_ATE_address into a void * pointer. */
7851 code = TYPE_CODE_PTR;
7852 type_flags |= TYPE_FLAG_UNSIGNED;
7853 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7854 break;
7855 case DW_ATE_boolean:
7856 code = TYPE_CODE_BOOL;
7857 type_flags |= TYPE_FLAG_UNSIGNED;
7858 break;
7859 case DW_ATE_complex_float:
7860 code = TYPE_CODE_COMPLEX;
7861 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7862 break;
7863 case DW_ATE_decimal_float:
7864 code = TYPE_CODE_DECFLOAT;
7865 break;
7866 case DW_ATE_float:
7867 code = TYPE_CODE_FLT;
7868 break;
7869 case DW_ATE_signed:
7870 break;
7871 case DW_ATE_unsigned:
7872 type_flags |= TYPE_FLAG_UNSIGNED;
7873 break;
7874 case DW_ATE_signed_char:
6e70227d 7875 if (cu->language == language_ada || cu->language == language_m2
868a0084 7876 || cu->language == language_pascal)
6ccb9162
UW
7877 code = TYPE_CODE_CHAR;
7878 break;
7879 case DW_ATE_unsigned_char:
868a0084
PM
7880 if (cu->language == language_ada || cu->language == language_m2
7881 || cu->language == language_pascal)
6ccb9162
UW
7882 code = TYPE_CODE_CHAR;
7883 type_flags |= TYPE_FLAG_UNSIGNED;
7884 break;
75079b2b
TT
7885 case DW_ATE_UTF:
7886 /* We just treat this as an integer and then recognize the
7887 type by name elsewhere. */
7888 break;
7889
6ccb9162
UW
7890 default:
7891 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7892 dwarf_type_encoding_name (encoding));
7893 break;
c906108c 7894 }
6ccb9162 7895
0114d602
DJ
7896 type = init_type (code, size, type_flags, NULL, objfile);
7897 TYPE_NAME (type) = name;
6ccb9162
UW
7898 TYPE_TARGET_TYPE (type) = target_type;
7899
0114d602 7900 if (name && strcmp (name, "char") == 0)
876cecd0 7901 TYPE_NOSIGN (type) = 1;
0114d602 7902
f792889a 7903 return set_die_type (die, type, cu);
c906108c
SS
7904}
7905
a02abb62
JB
7906/* Read the given DW_AT_subrange DIE. */
7907
f792889a 7908static struct type *
a02abb62
JB
7909read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7910{
5e2b427d 7911 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7912 struct type *base_type;
7913 struct type *range_type;
7914 struct attribute *attr;
43bbcdc2
PH
7915 LONGEST low = 0;
7916 LONGEST high = -1;
39cbfefa 7917 char *name;
43bbcdc2 7918 LONGEST negative_mask;
e77813c8 7919
a02abb62 7920 base_type = die_type (die, cu);
953ac07e
JK
7921 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7922 check_typedef (base_type);
a02abb62 7923
7e314c57
JK
7924 /* The die_type call above may have already set the type for this DIE. */
7925 range_type = get_die_type (die, cu);
7926 if (range_type)
7927 return range_type;
7928
e142c38c 7929 if (cu->language == language_fortran)
6e70227d 7930 {
a02abb62
JB
7931 /* FORTRAN implies a lower bound of 1, if not given. */
7932 low = 1;
7933 }
7934
dd5e6932
DJ
7935 /* FIXME: For variable sized arrays either of these could be
7936 a variable rather than a constant value. We'll allow it,
7937 but we don't know how to handle it. */
e142c38c 7938 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
7939 if (attr)
7940 low = dwarf2_get_attr_constant_value (attr, 0);
7941
e142c38c 7942 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 7943 if (attr)
6e70227d 7944 {
e77813c8 7945 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
7946 {
7947 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 7948 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
7949 FIXME: GDB does not yet know how to handle dynamic
7950 arrays properly, treat them as arrays with unspecified
7951 length for now.
7952
7953 FIXME: jimb/2003-09-22: GDB does not really know
7954 how to handle arrays of unspecified length
7955 either; we just represent them as zero-length
7956 arrays. Choose an appropriate upper bound given
7957 the lower bound we've computed above. */
7958 high = low - 1;
7959 }
7960 else
7961 high = dwarf2_get_attr_constant_value (attr, 1);
7962 }
e77813c8
PM
7963 else
7964 {
7965 attr = dwarf2_attr (die, DW_AT_count, cu);
7966 if (attr)
7967 {
7968 int count = dwarf2_get_attr_constant_value (attr, 1);
7969 high = low + count - 1;
7970 }
7971 }
7972
7973 /* Dwarf-2 specifications explicitly allows to create subrange types
7974 without specifying a base type.
7975 In that case, the base type must be set to the type of
7976 the lower bound, upper bound or count, in that order, if any of these
7977 three attributes references an object that has a type.
7978 If no base type is found, the Dwarf-2 specifications say that
7979 a signed integer type of size equal to the size of an address should
7980 be used.
7981 For the following C code: `extern char gdb_int [];'
7982 GCC produces an empty range DIE.
7983 FIXME: muller/2010-05-28: Possible references to object for low bound,
7984 high bound or count are not yet handled by this code.
7985 */
7986 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
7987 {
7988 struct objfile *objfile = cu->objfile;
7989 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7990 int addr_size = gdbarch_addr_bit (gdbarch) /8;
7991 struct type *int_type = objfile_type (objfile)->builtin_int;
7992
7993 /* Test "int", "long int", and "long long int" objfile types,
7994 and select the first one having a size above or equal to the
7995 architecture address size. */
7996 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7997 base_type = int_type;
7998 else
7999 {
8000 int_type = objfile_type (objfile)->builtin_long;
8001 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8002 base_type = int_type;
8003 else
8004 {
8005 int_type = objfile_type (objfile)->builtin_long_long;
8006 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8007 base_type = int_type;
8008 }
8009 }
8010 }
a02abb62 8011
6e70227d 8012 negative_mask =
43bbcdc2
PH
8013 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8014 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8015 low |= negative_mask;
8016 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8017 high |= negative_mask;
8018
a02abb62
JB
8019 range_type = create_range_type (NULL, base_type, low, high);
8020
bbb0eef6
JK
8021 /* Mark arrays with dynamic length at least as an array of unspecified
8022 length. GDB could check the boundary but before it gets implemented at
8023 least allow accessing the array elements. */
8024 if (attr && attr->form == DW_FORM_block1)
8025 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8026
39cbfefa
DJ
8027 name = dwarf2_name (die, cu);
8028 if (name)
8029 TYPE_NAME (range_type) = name;
6e70227d 8030
e142c38c 8031 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8032 if (attr)
8033 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8034
7e314c57
JK
8035 set_die_type (die, range_type, cu);
8036
8037 /* set_die_type should be already done. */
b4ba55a1
JB
8038 set_descriptive_type (range_type, die, cu);
8039
7e314c57 8040 return range_type;
a02abb62 8041}
6e70227d 8042
f792889a 8043static struct type *
81a17f79
JB
8044read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8045{
8046 struct type *type;
81a17f79 8047
81a17f79
JB
8048 /* For now, we only support the C meaning of an unspecified type: void. */
8049
0114d602
DJ
8050 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8051 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8052
f792889a 8053 return set_die_type (die, type, cu);
81a17f79 8054}
a02abb62 8055
51545339
DJ
8056/* Trivial hash function for die_info: the hash value of a DIE
8057 is its offset in .debug_info for this objfile. */
8058
8059static hashval_t
8060die_hash (const void *item)
8061{
8062 const struct die_info *die = item;
9a619af0 8063
51545339
DJ
8064 return die->offset;
8065}
8066
8067/* Trivial comparison function for die_info structures: two DIEs
8068 are equal if they have the same offset. */
8069
8070static int
8071die_eq (const void *item_lhs, const void *item_rhs)
8072{
8073 const struct die_info *die_lhs = item_lhs;
8074 const struct die_info *die_rhs = item_rhs;
9a619af0 8075
51545339
DJ
8076 return die_lhs->offset == die_rhs->offset;
8077}
8078
c906108c
SS
8079/* Read a whole compilation unit into a linked list of dies. */
8080
f9aca02d 8081static struct die_info *
93311388 8082read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8083{
93311388 8084 struct die_reader_specs reader_specs;
98bfdba5 8085 int read_abbrevs = 0;
1d9ec526 8086 struct cleanup *back_to = NULL;
98bfdba5
PA
8087 struct die_info *die;
8088
8089 if (cu->dwarf2_abbrevs == NULL)
8090 {
8091 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8092 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8093 read_abbrevs = 1;
8094 }
93311388 8095
348e048f 8096 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8097 cu->die_hash
8098 = htab_create_alloc_ex (cu->header.length / 12,
8099 die_hash,
8100 die_eq,
8101 NULL,
8102 &cu->comp_unit_obstack,
8103 hashtab_obstack_allocate,
8104 dummy_obstack_deallocate);
8105
93311388
DE
8106 init_cu_die_reader (&reader_specs, cu);
8107
98bfdba5
PA
8108 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8109
8110 if (read_abbrevs)
8111 do_cleanups (back_to);
8112
8113 return die;
639d11d3
DC
8114}
8115
d97bc12b
DE
8116/* Main entry point for reading a DIE and all children.
8117 Read the DIE and dump it if requested. */
8118
8119static struct die_info *
93311388
DE
8120read_die_and_children (const struct die_reader_specs *reader,
8121 gdb_byte *info_ptr,
d97bc12b
DE
8122 gdb_byte **new_info_ptr,
8123 struct die_info *parent)
8124{
93311388 8125 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8126 new_info_ptr, parent);
8127
8128 if (dwarf2_die_debug)
8129 {
348e048f
DE
8130 fprintf_unfiltered (gdb_stdlog,
8131 "\nRead die from %s of %s:\n",
8132 reader->buffer == dwarf2_per_objfile->info.buffer
8133 ? ".debug_info"
8134 : reader->buffer == dwarf2_per_objfile->types.buffer
8135 ? ".debug_types"
8136 : "unknown section",
8137 reader->abfd->filename);
d97bc12b
DE
8138 dump_die (result, dwarf2_die_debug);
8139 }
8140
8141 return result;
8142}
8143
639d11d3
DC
8144/* Read a single die and all its descendents. Set the die's sibling
8145 field to NULL; set other fields in the die correctly, and set all
8146 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8147 location of the info_ptr after reading all of those dies. PARENT
8148 is the parent of the die in question. */
8149
8150static struct die_info *
93311388
DE
8151read_die_and_children_1 (const struct die_reader_specs *reader,
8152 gdb_byte *info_ptr,
d97bc12b
DE
8153 gdb_byte **new_info_ptr,
8154 struct die_info *parent)
639d11d3
DC
8155{
8156 struct die_info *die;
fe1b8b76 8157 gdb_byte *cur_ptr;
639d11d3
DC
8158 int has_children;
8159
93311388 8160 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8161 if (die == NULL)
8162 {
8163 *new_info_ptr = cur_ptr;
8164 return NULL;
8165 }
93311388 8166 store_in_ref_table (die, reader->cu);
639d11d3
DC
8167
8168 if (has_children)
348e048f 8169 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8170 else
8171 {
8172 die->child = NULL;
8173 *new_info_ptr = cur_ptr;
8174 }
8175
8176 die->sibling = NULL;
8177 die->parent = parent;
8178 return die;
8179}
8180
8181/* Read a die, all of its descendents, and all of its siblings; set
8182 all of the fields of all of the dies correctly. Arguments are as
8183 in read_die_and_children. */
8184
8185static struct die_info *
93311388
DE
8186read_die_and_siblings (const struct die_reader_specs *reader,
8187 gdb_byte *info_ptr,
fe1b8b76 8188 gdb_byte **new_info_ptr,
639d11d3
DC
8189 struct die_info *parent)
8190{
8191 struct die_info *first_die, *last_sibling;
fe1b8b76 8192 gdb_byte *cur_ptr;
639d11d3 8193
c906108c 8194 cur_ptr = info_ptr;
639d11d3
DC
8195 first_die = last_sibling = NULL;
8196
8197 while (1)
c906108c 8198 {
639d11d3 8199 struct die_info *die
93311388 8200 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8201
1d325ec1 8202 if (die == NULL)
c906108c 8203 {
639d11d3
DC
8204 *new_info_ptr = cur_ptr;
8205 return first_die;
c906108c 8206 }
1d325ec1
DJ
8207
8208 if (!first_die)
8209 first_die = die;
c906108c 8210 else
1d325ec1
DJ
8211 last_sibling->sibling = die;
8212
8213 last_sibling = die;
c906108c 8214 }
c906108c
SS
8215}
8216
93311388
DE
8217/* Read the die from the .debug_info section buffer. Set DIEP to
8218 point to a newly allocated die with its information, except for its
8219 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8220 whether the die has children or not. */
8221
8222static gdb_byte *
8223read_full_die (const struct die_reader_specs *reader,
8224 struct die_info **diep, gdb_byte *info_ptr,
8225 int *has_children)
8226{
8227 unsigned int abbrev_number, bytes_read, i, offset;
8228 struct abbrev_info *abbrev;
8229 struct die_info *die;
8230 struct dwarf2_cu *cu = reader->cu;
8231 bfd *abfd = reader->abfd;
8232
8233 offset = info_ptr - reader->buffer;
8234 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8235 info_ptr += bytes_read;
8236 if (!abbrev_number)
8237 {
8238 *diep = NULL;
8239 *has_children = 0;
8240 return info_ptr;
8241 }
8242
8243 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8244 if (!abbrev)
348e048f
DE
8245 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8246 abbrev_number,
8247 bfd_get_filename (abfd));
8248
93311388
DE
8249 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8250 die->offset = offset;
8251 die->tag = abbrev->tag;
8252 die->abbrev = abbrev_number;
8253
8254 die->num_attrs = abbrev->num_attrs;
8255
8256 for (i = 0; i < abbrev->num_attrs; ++i)
8257 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8258 abfd, info_ptr, cu);
8259
8260 *diep = die;
8261 *has_children = abbrev->has_children;
8262 return info_ptr;
8263}
8264
c906108c
SS
8265/* In DWARF version 2, the description of the debugging information is
8266 stored in a separate .debug_abbrev section. Before we read any
8267 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8268 in a hash table. This function also sets flags in CU describing
8269 the data found in the abbrev table. */
c906108c
SS
8270
8271static void
e7c27a73 8272dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8273{
e7c27a73 8274 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8275 gdb_byte *abbrev_ptr;
c906108c
SS
8276 struct abbrev_info *cur_abbrev;
8277 unsigned int abbrev_number, bytes_read, abbrev_name;
8278 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8279 struct attr_abbrev *cur_attrs;
8280 unsigned int allocated_attrs;
c906108c 8281
57349743 8282 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
8283 obstack_init (&cu->abbrev_obstack);
8284 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8285 (ABBREV_HASH_SIZE
8286 * sizeof (struct abbrev_info *)));
8287 memset (cu->dwarf2_abbrevs, 0,
8288 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8289
be391dca
TT
8290 dwarf2_read_section (dwarf2_per_objfile->objfile,
8291 &dwarf2_per_objfile->abbrev);
dce234bc 8292 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8293 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8294 abbrev_ptr += bytes_read;
8295
f3dd6933
DJ
8296 allocated_attrs = ATTR_ALLOC_CHUNK;
8297 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8298
c906108c
SS
8299 /* loop until we reach an abbrev number of 0 */
8300 while (abbrev_number)
8301 {
f3dd6933 8302 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8303
8304 /* read in abbrev header */
8305 cur_abbrev->number = abbrev_number;
8306 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8307 abbrev_ptr += bytes_read;
8308 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8309 abbrev_ptr += 1;
8310
72bf9492
DJ
8311 if (cur_abbrev->tag == DW_TAG_namespace)
8312 cu->has_namespace_info = 1;
8313
c906108c
SS
8314 /* now read in declarations */
8315 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8316 abbrev_ptr += bytes_read;
8317 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8318 abbrev_ptr += bytes_read;
8319 while (abbrev_name)
8320 {
f3dd6933 8321 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8322 {
f3dd6933
DJ
8323 allocated_attrs += ATTR_ALLOC_CHUNK;
8324 cur_attrs
8325 = xrealloc (cur_attrs, (allocated_attrs
8326 * sizeof (struct attr_abbrev)));
c906108c 8327 }
ae038cb0
DJ
8328
8329 /* Record whether this compilation unit might have
8330 inter-compilation-unit references. If we don't know what form
8331 this attribute will have, then it might potentially be a
8332 DW_FORM_ref_addr, so we conservatively expect inter-CU
8333 references. */
8334
8335 if (abbrev_form == DW_FORM_ref_addr
8336 || abbrev_form == DW_FORM_indirect)
8337 cu->has_form_ref_addr = 1;
8338
f3dd6933
DJ
8339 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8340 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8341 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8342 abbrev_ptr += bytes_read;
8343 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8344 abbrev_ptr += bytes_read;
8345 }
8346
f3dd6933
DJ
8347 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8348 (cur_abbrev->num_attrs
8349 * sizeof (struct attr_abbrev)));
8350 memcpy (cur_abbrev->attrs, cur_attrs,
8351 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8352
c906108c 8353 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8354 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8355 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8356
8357 /* Get next abbreviation.
8358 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8359 always properly terminated with an abbrev number of 0.
8360 Exit loop if we encounter an abbreviation which we have
8361 already read (which means we are about to read the abbreviations
8362 for the next compile unit) or if the end of the abbreviation
8363 table is reached. */
dce234bc
PP
8364 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8365 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8366 break;
8367 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8368 abbrev_ptr += bytes_read;
e7c27a73 8369 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8370 break;
8371 }
f3dd6933
DJ
8372
8373 xfree (cur_attrs);
c906108c
SS
8374}
8375
f3dd6933 8376/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8377
c906108c 8378static void
f3dd6933 8379dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8380{
f3dd6933 8381 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8382
f3dd6933
DJ
8383 obstack_free (&cu->abbrev_obstack, NULL);
8384 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8385}
8386
8387/* Lookup an abbrev_info structure in the abbrev hash table. */
8388
8389static struct abbrev_info *
e7c27a73 8390dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8391{
8392 unsigned int hash_number;
8393 struct abbrev_info *abbrev;
8394
8395 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8396 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8397
8398 while (abbrev)
8399 {
8400 if (abbrev->number == number)
8401 return abbrev;
8402 else
8403 abbrev = abbrev->next;
8404 }
8405 return NULL;
8406}
8407
72bf9492
DJ
8408/* Returns nonzero if TAG represents a type that we might generate a partial
8409 symbol for. */
8410
8411static int
8412is_type_tag_for_partial (int tag)
8413{
8414 switch (tag)
8415 {
8416#if 0
8417 /* Some types that would be reasonable to generate partial symbols for,
8418 that we don't at present. */
8419 case DW_TAG_array_type:
8420 case DW_TAG_file_type:
8421 case DW_TAG_ptr_to_member_type:
8422 case DW_TAG_set_type:
8423 case DW_TAG_string_type:
8424 case DW_TAG_subroutine_type:
8425#endif
8426 case DW_TAG_base_type:
8427 case DW_TAG_class_type:
680b30c7 8428 case DW_TAG_interface_type:
72bf9492
DJ
8429 case DW_TAG_enumeration_type:
8430 case DW_TAG_structure_type:
8431 case DW_TAG_subrange_type:
8432 case DW_TAG_typedef:
8433 case DW_TAG_union_type:
8434 return 1;
8435 default:
8436 return 0;
8437 }
8438}
8439
8440/* Load all DIEs that are interesting for partial symbols into memory. */
8441
8442static struct partial_die_info *
93311388
DE
8443load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8444 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8445{
8446 struct partial_die_info *part_die;
8447 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8448 struct abbrev_info *abbrev;
8449 unsigned int bytes_read;
5afb4e99 8450 unsigned int load_all = 0;
72bf9492
DJ
8451
8452 int nesting_level = 1;
8453
8454 parent_die = NULL;
8455 last_die = NULL;
8456
5afb4e99
DJ
8457 if (cu->per_cu && cu->per_cu->load_all_dies)
8458 load_all = 1;
8459
72bf9492
DJ
8460 cu->partial_dies
8461 = htab_create_alloc_ex (cu->header.length / 12,
8462 partial_die_hash,
8463 partial_die_eq,
8464 NULL,
8465 &cu->comp_unit_obstack,
8466 hashtab_obstack_allocate,
8467 dummy_obstack_deallocate);
8468
8469 part_die = obstack_alloc (&cu->comp_unit_obstack,
8470 sizeof (struct partial_die_info));
8471
8472 while (1)
8473 {
8474 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8475
8476 /* A NULL abbrev means the end of a series of children. */
8477 if (abbrev == NULL)
8478 {
8479 if (--nesting_level == 0)
8480 {
8481 /* PART_DIE was probably the last thing allocated on the
8482 comp_unit_obstack, so we could call obstack_free
8483 here. We don't do that because the waste is small,
8484 and will be cleaned up when we're done with this
8485 compilation unit. This way, we're also more robust
8486 against other users of the comp_unit_obstack. */
8487 return first_die;
8488 }
8489 info_ptr += bytes_read;
8490 last_die = parent_die;
8491 parent_die = parent_die->die_parent;
8492 continue;
8493 }
8494
98bfdba5
PA
8495 /* Check for template arguments. We never save these; if
8496 they're seen, we just mark the parent, and go on our way. */
8497 if (parent_die != NULL
8498 && cu->language == language_cplus
8499 && (abbrev->tag == DW_TAG_template_type_param
8500 || abbrev->tag == DW_TAG_template_value_param))
8501 {
8502 parent_die->has_template_arguments = 1;
8503
8504 if (!load_all)
8505 {
8506 /* We don't need a partial DIE for the template argument. */
8507 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8508 cu);
8509 continue;
8510 }
8511 }
8512
8513 /* We only recurse into subprograms looking for template arguments.
8514 Skip their other children. */
8515 if (!load_all
8516 && cu->language == language_cplus
8517 && parent_die != NULL
8518 && parent_die->tag == DW_TAG_subprogram)
8519 {
8520 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8521 continue;
8522 }
8523
5afb4e99
DJ
8524 /* Check whether this DIE is interesting enough to save. Normally
8525 we would not be interested in members here, but there may be
8526 later variables referencing them via DW_AT_specification (for
8527 static members). */
8528 if (!load_all
8529 && !is_type_tag_for_partial (abbrev->tag)
72bf9492
DJ
8530 && abbrev->tag != DW_TAG_enumerator
8531 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8532 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8533 && abbrev->tag != DW_TAG_variable
5afb4e99 8534 && abbrev->tag != DW_TAG_namespace
f55ee35c 8535 && abbrev->tag != DW_TAG_module
5afb4e99 8536 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8537 {
8538 /* Otherwise we skip to the next sibling, if any. */
93311388 8539 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8540 continue;
8541 }
8542
93311388
DE
8543 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8544 buffer, info_ptr, cu);
72bf9492
DJ
8545
8546 /* This two-pass algorithm for processing partial symbols has a
8547 high cost in cache pressure. Thus, handle some simple cases
8548 here which cover the majority of C partial symbols. DIEs
8549 which neither have specification tags in them, nor could have
8550 specification tags elsewhere pointing at them, can simply be
8551 processed and discarded.
8552
8553 This segment is also optional; scan_partial_symbols and
8554 add_partial_symbol will handle these DIEs if we chain
8555 them in normally. When compilers which do not emit large
8556 quantities of duplicate debug information are more common,
8557 this code can probably be removed. */
8558
8559 /* Any complete simple types at the top level (pretty much all
8560 of them, for a language without namespaces), can be processed
8561 directly. */
8562 if (parent_die == NULL
8563 && part_die->has_specification == 0
8564 && part_die->is_declaration == 0
8565 && (part_die->tag == DW_TAG_typedef
8566 || part_die->tag == DW_TAG_base_type
8567 || part_die->tag == DW_TAG_subrange_type))
8568 {
8569 if (building_psymtab && part_die->name != NULL)
04a679b8 8570 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8571 VAR_DOMAIN, LOC_TYPEDEF,
8572 &cu->objfile->static_psymbols,
8573 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8574 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8575 continue;
8576 }
8577
8578 /* If we're at the second level, and we're an enumerator, and
8579 our parent has no specification (meaning possibly lives in a
8580 namespace elsewhere), then we can add the partial symbol now
8581 instead of queueing it. */
8582 if (part_die->tag == DW_TAG_enumerator
8583 && parent_die != NULL
8584 && parent_die->die_parent == NULL
8585 && parent_die->tag == DW_TAG_enumeration_type
8586 && parent_die->has_specification == 0)
8587 {
8588 if (part_die->name == NULL)
e2e0b3e5 8589 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 8590 else if (building_psymtab)
04a679b8 8591 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8592 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8593 (cu->language == language_cplus
8594 || cu->language == language_java)
72bf9492
DJ
8595 ? &cu->objfile->global_psymbols
8596 : &cu->objfile->static_psymbols,
8597 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8598
93311388 8599 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8600 continue;
8601 }
8602
8603 /* We'll save this DIE so link it in. */
8604 part_die->die_parent = parent_die;
8605 part_die->die_sibling = NULL;
8606 part_die->die_child = NULL;
8607
8608 if (last_die && last_die == parent_die)
8609 last_die->die_child = part_die;
8610 else if (last_die)
8611 last_die->die_sibling = part_die;
8612
8613 last_die = part_die;
8614
8615 if (first_die == NULL)
8616 first_die = part_die;
8617
8618 /* Maybe add the DIE to the hash table. Not all DIEs that we
8619 find interesting need to be in the hash table, because we
8620 also have the parent/sibling/child chains; only those that we
8621 might refer to by offset later during partial symbol reading.
8622
8623 For now this means things that might have be the target of a
8624 DW_AT_specification, DW_AT_abstract_origin, or
8625 DW_AT_extension. DW_AT_extension will refer only to
8626 namespaces; DW_AT_abstract_origin refers to functions (and
8627 many things under the function DIE, but we do not recurse
8628 into function DIEs during partial symbol reading) and
8629 possibly variables as well; DW_AT_specification refers to
8630 declarations. Declarations ought to have the DW_AT_declaration
8631 flag. It happens that GCC forgets to put it in sometimes, but
8632 only for functions, not for types.
8633
8634 Adding more things than necessary to the hash table is harmless
8635 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8636 wasted time in find_partial_die, when we reread the compilation
8637 unit with load_all_dies set. */
72bf9492 8638
5afb4e99
DJ
8639 if (load_all
8640 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8641 || abbrev->tag == DW_TAG_variable
8642 || abbrev->tag == DW_TAG_namespace
8643 || part_die->is_declaration)
8644 {
8645 void **slot;
8646
8647 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8648 part_die->offset, INSERT);
8649 *slot = part_die;
8650 }
8651
8652 part_die = obstack_alloc (&cu->comp_unit_obstack,
8653 sizeof (struct partial_die_info));
8654
8655 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8656 we have no reason to follow the children of structures; for other
98bfdba5
PA
8657 languages we have to, so that we can get at method physnames
8658 to infer fully qualified class names, for DW_AT_specification,
8659 and for C++ template arguments. For C++, we also look one level
8660 inside functions to find template arguments (if the name of the
8661 function does not already contain the template arguments).
bc30ff58
JB
8662
8663 For Ada, we need to scan the children of subprograms and lexical
8664 blocks as well because Ada allows the definition of nested
8665 entities that could be interesting for the debugger, such as
8666 nested subprograms for instance. */
72bf9492 8667 if (last_die->has_children
5afb4e99
DJ
8668 && (load_all
8669 || last_die->tag == DW_TAG_namespace
f55ee35c 8670 || last_die->tag == DW_TAG_module
72bf9492 8671 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8672 || (cu->language == language_cplus
8673 && last_die->tag == DW_TAG_subprogram
8674 && (last_die->name == NULL
8675 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8676 || (cu->language != language_c
8677 && (last_die->tag == DW_TAG_class_type
680b30c7 8678 || last_die->tag == DW_TAG_interface_type
72bf9492 8679 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8680 || last_die->tag == DW_TAG_union_type))
8681 || (cu->language == language_ada
8682 && (last_die->tag == DW_TAG_subprogram
8683 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8684 {
8685 nesting_level++;
8686 parent_die = last_die;
8687 continue;
8688 }
8689
8690 /* Otherwise we skip to the next sibling, if any. */
93311388 8691 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8692
8693 /* Back to the top, do it again. */
8694 }
8695}
8696
c906108c
SS
8697/* Read a minimal amount of information into the minimal die structure. */
8698
fe1b8b76 8699static gdb_byte *
72bf9492
DJ
8700read_partial_die (struct partial_die_info *part_die,
8701 struct abbrev_info *abbrev,
8702 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8703 gdb_byte *buffer, gdb_byte *info_ptr,
8704 struct dwarf2_cu *cu)
c906108c 8705{
fa238c03 8706 unsigned int i;
c906108c 8707 struct attribute attr;
c5aa993b 8708 int has_low_pc_attr = 0;
c906108c
SS
8709 int has_high_pc_attr = 0;
8710
72bf9492 8711 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8712
93311388 8713 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8714
8715 info_ptr += abbrev_len;
8716
8717 if (abbrev == NULL)
8718 return info_ptr;
8719
c906108c
SS
8720 part_die->tag = abbrev->tag;
8721 part_die->has_children = abbrev->has_children;
c906108c
SS
8722
8723 for (i = 0; i < abbrev->num_attrs; ++i)
8724 {
e7c27a73 8725 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8726
8727 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8728 partial symbol table. */
c906108c
SS
8729 switch (attr.name)
8730 {
8731 case DW_AT_name:
71c25dea
TT
8732 switch (part_die->tag)
8733 {
8734 case DW_TAG_compile_unit:
348e048f 8735 case DW_TAG_type_unit:
71c25dea
TT
8736 /* Compilation units have a DW_AT_name that is a filename, not
8737 a source language identifier. */
8738 case DW_TAG_enumeration_type:
8739 case DW_TAG_enumerator:
8740 /* These tags always have simple identifiers already; no need
8741 to canonicalize them. */
8742 part_die->name = DW_STRING (&attr);
8743 break;
8744 default:
8745 part_die->name
8746 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8747 &cu->objfile->objfile_obstack);
71c25dea
TT
8748 break;
8749 }
c906108c 8750 break;
31ef98ae 8751 case DW_AT_linkage_name:
c906108c 8752 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8753 /* Note that both forms of linkage name might appear. We
8754 assume they will be the same, and we only store the last
8755 one we see. */
94af9270
KS
8756 if (cu->language == language_ada)
8757 part_die->name = DW_STRING (&attr);
c906108c
SS
8758 break;
8759 case DW_AT_low_pc:
8760 has_low_pc_attr = 1;
8761 part_die->lowpc = DW_ADDR (&attr);
8762 break;
8763 case DW_AT_high_pc:
8764 has_high_pc_attr = 1;
8765 part_die->highpc = DW_ADDR (&attr);
8766 break;
8767 case DW_AT_location:
8e19ed76
PS
8768 /* Support the .debug_loc offsets */
8769 if (attr_form_is_block (&attr))
8770 {
8771 part_die->locdesc = DW_BLOCK (&attr);
8772 }
3690dd37 8773 else if (attr_form_is_section_offset (&attr))
8e19ed76 8774 {
4d3c2250 8775 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8776 }
8777 else
8778 {
4d3c2250
KB
8779 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8780 "partial symbol information");
8e19ed76 8781 }
c906108c 8782 break;
c906108c
SS
8783 case DW_AT_external:
8784 part_die->is_external = DW_UNSND (&attr);
8785 break;
8786 case DW_AT_declaration:
8787 part_die->is_declaration = DW_UNSND (&attr);
8788 break;
8789 case DW_AT_type:
8790 part_die->has_type = 1;
8791 break;
8792 case DW_AT_abstract_origin:
8793 case DW_AT_specification:
72bf9492
DJ
8794 case DW_AT_extension:
8795 part_die->has_specification = 1;
c764a876 8796 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8797 break;
8798 case DW_AT_sibling:
8799 /* Ignore absolute siblings, they might point outside of
8800 the current compile unit. */
8801 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8802 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8803 else
93311388 8804 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8805 break;
fa4028e9
JB
8806 case DW_AT_byte_size:
8807 part_die->has_byte_size = 1;
8808 break;
68511cec
CES
8809 case DW_AT_calling_convention:
8810 /* DWARF doesn't provide a way to identify a program's source-level
8811 entry point. DW_AT_calling_convention attributes are only meant
8812 to describe functions' calling conventions.
8813
8814 However, because it's a necessary piece of information in
8815 Fortran, and because DW_CC_program is the only piece of debugging
8816 information whose definition refers to a 'main program' at all,
8817 several compilers have begun marking Fortran main programs with
8818 DW_CC_program --- even when those functions use the standard
8819 calling conventions.
8820
8821 So until DWARF specifies a way to provide this information and
8822 compilers pick up the new representation, we'll support this
8823 practice. */
8824 if (DW_UNSND (&attr) == DW_CC_program
8825 && cu->language == language_fortran)
8826 set_main_name (part_die->name);
8827 break;
c906108c
SS
8828 default:
8829 break;
8830 }
8831 }
8832
c906108c
SS
8833 /* When using the GNU linker, .gnu.linkonce. sections are used to
8834 eliminate duplicate copies of functions and vtables and such.
8835 The linker will arbitrarily choose one and discard the others.
8836 The AT_*_pc values for such functions refer to local labels in
8837 these sections. If the section from that file was discarded, the
8838 labels are not in the output, so the relocs get a value of 0.
8839 If this is a discarded function, mark the pc bounds as invalid,
8840 so that GDB will ignore it. */
8841 if (has_low_pc_attr && has_high_pc_attr
8842 && part_die->lowpc < part_die->highpc
8843 && (part_die->lowpc != 0
72dca2f5 8844 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8845 part_die->has_pc_info = 1;
85cbf3d3 8846
c906108c
SS
8847 return info_ptr;
8848}
8849
72bf9492
DJ
8850/* Find a cached partial DIE at OFFSET in CU. */
8851
8852static struct partial_die_info *
c764a876 8853find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8854{
8855 struct partial_die_info *lookup_die = NULL;
8856 struct partial_die_info part_die;
8857
8858 part_die.offset = offset;
8859 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8860
72bf9492
DJ
8861 return lookup_die;
8862}
8863
348e048f
DE
8864/* Find a partial DIE at OFFSET, which may or may not be in CU,
8865 except in the case of .debug_types DIEs which do not reference
8866 outside their CU (they do however referencing other types via
8867 DW_FORM_sig8). */
72bf9492
DJ
8868
8869static struct partial_die_info *
c764a876 8870find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8871{
5afb4e99
DJ
8872 struct dwarf2_per_cu_data *per_cu = NULL;
8873 struct partial_die_info *pd = NULL;
72bf9492 8874
348e048f
DE
8875 if (cu->per_cu->from_debug_types)
8876 {
8877 pd = find_partial_die_in_comp_unit (offset, cu);
8878 if (pd != NULL)
8879 return pd;
8880 goto not_found;
8881 }
8882
45452591 8883 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8884 {
8885 pd = find_partial_die_in_comp_unit (offset, cu);
8886 if (pd != NULL)
8887 return pd;
8888 }
72bf9492 8889
ae038cb0
DJ
8890 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8891
98bfdba5
PA
8892 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8893 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8894
8895 per_cu->cu->last_used = 0;
5afb4e99
DJ
8896 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8897
8898 if (pd == NULL && per_cu->load_all_dies == 0)
8899 {
8900 struct cleanup *back_to;
8901 struct partial_die_info comp_unit_die;
8902 struct abbrev_info *abbrev;
8903 unsigned int bytes_read;
8904 char *info_ptr;
8905
8906 per_cu->load_all_dies = 1;
8907
8908 /* Re-read the DIEs. */
8909 back_to = make_cleanup (null_cleanup, 0);
8910 if (per_cu->cu->dwarf2_abbrevs == NULL)
8911 {
8912 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 8913 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 8914 }
dce234bc 8915 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
8916 + per_cu->cu->header.offset
8917 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
8918 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8919 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
8920 per_cu->cu->objfile->obfd,
8921 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
8922 per_cu->cu);
8923 if (comp_unit_die.has_children)
93311388
DE
8924 load_partial_dies (per_cu->cu->objfile->obfd,
8925 dwarf2_per_objfile->info.buffer, info_ptr,
8926 0, per_cu->cu);
5afb4e99
DJ
8927 do_cleanups (back_to);
8928
8929 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8930 }
8931
348e048f
DE
8932 not_found:
8933
5afb4e99
DJ
8934 if (pd == NULL)
8935 internal_error (__FILE__, __LINE__,
c764a876 8936 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
8937 offset, bfd_get_filename (cu->objfile->obfd));
8938 return pd;
72bf9492
DJ
8939}
8940
8941/* Adjust PART_DIE before generating a symbol for it. This function
8942 may set the is_external flag or change the DIE's name. */
8943
8944static void
8945fixup_partial_die (struct partial_die_info *part_die,
8946 struct dwarf2_cu *cu)
8947{
8948 /* If we found a reference attribute and the DIE has no name, try
8949 to find a name in the referred to DIE. */
8950
8951 if (part_die->name == NULL && part_die->has_specification)
8952 {
8953 struct partial_die_info *spec_die;
72bf9492 8954
10b3939b 8955 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 8956
10b3939b 8957 fixup_partial_die (spec_die, cu);
72bf9492
DJ
8958
8959 if (spec_die->name)
8960 {
8961 part_die->name = spec_die->name;
8962
8963 /* Copy DW_AT_external attribute if it is set. */
8964 if (spec_die->is_external)
8965 part_die->is_external = spec_die->is_external;
8966 }
8967 }
8968
8969 /* Set default names for some unnamed DIEs. */
8970 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
8971 || part_die->tag == DW_TAG_class_type))
8972 part_die->name = "(anonymous class)";
8973
8974 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
8975 part_die->name = "(anonymous namespace)";
8976
8977 if (part_die->tag == DW_TAG_structure_type
8978 || part_die->tag == DW_TAG_class_type
8979 || part_die->tag == DW_TAG_union_type)
8980 guess_structure_name (part_die, cu);
8981}
8982
a8329558 8983/* Read an attribute value described by an attribute form. */
c906108c 8984
fe1b8b76 8985static gdb_byte *
a8329558 8986read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 8987 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 8988 struct dwarf2_cu *cu)
c906108c 8989{
e7c27a73 8990 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
8991 unsigned int bytes_read;
8992 struct dwarf_block *blk;
8993
a8329558
KW
8994 attr->form = form;
8995 switch (form)
c906108c 8996 {
c906108c 8997 case DW_FORM_ref_addr:
ae411497
TT
8998 if (cu->header.version == 2)
8999 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9000 else
9001 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9002 info_ptr += bytes_read;
9003 break;
9004 case DW_FORM_addr:
e7c27a73 9005 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9006 info_ptr += bytes_read;
c906108c
SS
9007 break;
9008 case DW_FORM_block2:
7b5a2f43 9009 blk = dwarf_alloc_block (cu);
c906108c
SS
9010 blk->size = read_2_bytes (abfd, info_ptr);
9011 info_ptr += 2;
9012 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9013 info_ptr += blk->size;
9014 DW_BLOCK (attr) = blk;
9015 break;
9016 case DW_FORM_block4:
7b5a2f43 9017 blk = dwarf_alloc_block (cu);
c906108c
SS
9018 blk->size = read_4_bytes (abfd, info_ptr);
9019 info_ptr += 4;
9020 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9021 info_ptr += blk->size;
9022 DW_BLOCK (attr) = blk;
9023 break;
9024 case DW_FORM_data2:
9025 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9026 info_ptr += 2;
9027 break;
9028 case DW_FORM_data4:
9029 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9030 info_ptr += 4;
9031 break;
9032 case DW_FORM_data8:
9033 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9034 info_ptr += 8;
9035 break;
2dc7f7b3
TT
9036 case DW_FORM_sec_offset:
9037 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9038 info_ptr += bytes_read;
9039 break;
c906108c 9040 case DW_FORM_string:
9b1c24c8 9041 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9042 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9043 info_ptr += bytes_read;
9044 break;
4bdf3d34
JJ
9045 case DW_FORM_strp:
9046 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9047 &bytes_read);
8285870a 9048 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9049 info_ptr += bytes_read;
9050 break;
2dc7f7b3 9051 case DW_FORM_exprloc:
c906108c 9052 case DW_FORM_block:
7b5a2f43 9053 blk = dwarf_alloc_block (cu);
c906108c
SS
9054 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9055 info_ptr += bytes_read;
9056 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9057 info_ptr += blk->size;
9058 DW_BLOCK (attr) = blk;
9059 break;
9060 case DW_FORM_block1:
7b5a2f43 9061 blk = dwarf_alloc_block (cu);
c906108c
SS
9062 blk->size = read_1_byte (abfd, info_ptr);
9063 info_ptr += 1;
9064 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9065 info_ptr += blk->size;
9066 DW_BLOCK (attr) = blk;
9067 break;
9068 case DW_FORM_data1:
9069 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9070 info_ptr += 1;
9071 break;
9072 case DW_FORM_flag:
9073 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9074 info_ptr += 1;
9075 break;
2dc7f7b3
TT
9076 case DW_FORM_flag_present:
9077 DW_UNSND (attr) = 1;
9078 break;
c906108c
SS
9079 case DW_FORM_sdata:
9080 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9081 info_ptr += bytes_read;
9082 break;
9083 case DW_FORM_udata:
9084 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9085 info_ptr += bytes_read;
9086 break;
9087 case DW_FORM_ref1:
10b3939b 9088 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9089 info_ptr += 1;
9090 break;
9091 case DW_FORM_ref2:
10b3939b 9092 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9093 info_ptr += 2;
9094 break;
9095 case DW_FORM_ref4:
10b3939b 9096 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9097 info_ptr += 4;
9098 break;
613e1657 9099 case DW_FORM_ref8:
10b3939b 9100 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9101 info_ptr += 8;
9102 break;
348e048f
DE
9103 case DW_FORM_sig8:
9104 /* Convert the signature to something we can record in DW_UNSND
9105 for later lookup.
9106 NOTE: This is NULL if the type wasn't found. */
9107 DW_SIGNATURED_TYPE (attr) =
9108 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9109 info_ptr += 8;
9110 break;
c906108c 9111 case DW_FORM_ref_udata:
10b3939b
DJ
9112 DW_ADDR (attr) = (cu->header.offset
9113 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9114 info_ptr += bytes_read;
9115 break;
c906108c 9116 case DW_FORM_indirect:
a8329558
KW
9117 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9118 info_ptr += bytes_read;
e7c27a73 9119 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9120 break;
c906108c 9121 default:
8a3fe4f8 9122 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9123 dwarf_form_name (form),
9124 bfd_get_filename (abfd));
c906108c 9125 }
28e94949
JB
9126
9127 /* We have seen instances where the compiler tried to emit a byte
9128 size attribute of -1 which ended up being encoded as an unsigned
9129 0xffffffff. Although 0xffffffff is technically a valid size value,
9130 an object of this size seems pretty unlikely so we can relatively
9131 safely treat these cases as if the size attribute was invalid and
9132 treat them as zero by default. */
9133 if (attr->name == DW_AT_byte_size
9134 && form == DW_FORM_data4
9135 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9136 {
9137 complaint
9138 (&symfile_complaints,
43bbcdc2
PH
9139 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9140 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9141 DW_UNSND (attr) = 0;
9142 }
28e94949 9143
c906108c
SS
9144 return info_ptr;
9145}
9146
a8329558
KW
9147/* Read an attribute described by an abbreviated attribute. */
9148
fe1b8b76 9149static gdb_byte *
a8329558 9150read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9151 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9152{
9153 attr->name = abbrev->name;
e7c27a73 9154 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9155}
9156
c906108c
SS
9157/* read dwarf information from a buffer */
9158
9159static unsigned int
fe1b8b76 9160read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9161{
fe1b8b76 9162 return bfd_get_8 (abfd, buf);
c906108c
SS
9163}
9164
9165static int
fe1b8b76 9166read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9167{
fe1b8b76 9168 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9169}
9170
9171static unsigned int
fe1b8b76 9172read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9173{
fe1b8b76 9174 return bfd_get_16 (abfd, buf);
c906108c
SS
9175}
9176
9177static int
fe1b8b76 9178read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9179{
fe1b8b76 9180 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9181}
9182
9183static unsigned int
fe1b8b76 9184read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9185{
fe1b8b76 9186 return bfd_get_32 (abfd, buf);
c906108c
SS
9187}
9188
9189static int
fe1b8b76 9190read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9191{
fe1b8b76 9192 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9193}
9194
93311388 9195static ULONGEST
fe1b8b76 9196read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9197{
fe1b8b76 9198 return bfd_get_64 (abfd, buf);
c906108c
SS
9199}
9200
9201static CORE_ADDR
fe1b8b76 9202read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9203 unsigned int *bytes_read)
c906108c 9204{
e7c27a73 9205 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9206 CORE_ADDR retval = 0;
9207
107d2387 9208 if (cu_header->signed_addr_p)
c906108c 9209 {
107d2387
AC
9210 switch (cu_header->addr_size)
9211 {
9212 case 2:
fe1b8b76 9213 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9214 break;
9215 case 4:
fe1b8b76 9216 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9217 break;
9218 case 8:
fe1b8b76 9219 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9220 break;
9221 default:
8e65ff28 9222 internal_error (__FILE__, __LINE__,
e2e0b3e5 9223 _("read_address: bad switch, signed [in module %s]"),
659b0389 9224 bfd_get_filename (abfd));
107d2387
AC
9225 }
9226 }
9227 else
9228 {
9229 switch (cu_header->addr_size)
9230 {
9231 case 2:
fe1b8b76 9232 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9233 break;
9234 case 4:
fe1b8b76 9235 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9236 break;
9237 case 8:
fe1b8b76 9238 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9239 break;
9240 default:
8e65ff28 9241 internal_error (__FILE__, __LINE__,
e2e0b3e5 9242 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 9243 bfd_get_filename (abfd));
107d2387 9244 }
c906108c 9245 }
64367e0a 9246
107d2387
AC
9247 *bytes_read = cu_header->addr_size;
9248 return retval;
c906108c
SS
9249}
9250
f7ef9339 9251/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9252 specification allows the initial length to take up either 4 bytes
9253 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9254 bytes describe the length and all offsets will be 8 bytes in length
9255 instead of 4.
9256
f7ef9339
KB
9257 An older, non-standard 64-bit format is also handled by this
9258 function. The older format in question stores the initial length
9259 as an 8-byte quantity without an escape value. Lengths greater
9260 than 2^32 aren't very common which means that the initial 4 bytes
9261 is almost always zero. Since a length value of zero doesn't make
9262 sense for the 32-bit format, this initial zero can be considered to
9263 be an escape value which indicates the presence of the older 64-bit
9264 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9265 greater than 4GB. If it becomes necessary to handle lengths
9266 somewhat larger than 4GB, we could allow other small values (such
9267 as the non-sensical values of 1, 2, and 3) to also be used as
9268 escape values indicating the presence of the old format.
f7ef9339 9269
917c78fc
MK
9270 The value returned via bytes_read should be used to increment the
9271 relevant pointer after calling read_initial_length().
c764a876 9272
613e1657
KB
9273 [ Note: read_initial_length() and read_offset() are based on the
9274 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9275 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9276 from:
9277
f7ef9339 9278 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9279
613e1657
KB
9280 This document is only a draft and is subject to change. (So beware.)
9281
f7ef9339 9282 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9283 determined empirically by examining 64-bit ELF files produced by
9284 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9285
9286 - Kevin, July 16, 2002
613e1657
KB
9287 ] */
9288
9289static LONGEST
c764a876 9290read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9291{
fe1b8b76 9292 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9293
dd373385 9294 if (length == 0xffffffff)
613e1657 9295 {
fe1b8b76 9296 length = bfd_get_64 (abfd, buf + 4);
613e1657 9297 *bytes_read = 12;
613e1657 9298 }
dd373385 9299 else if (length == 0)
f7ef9339 9300 {
dd373385 9301 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9302 length = bfd_get_64 (abfd, buf);
f7ef9339 9303 *bytes_read = 8;
f7ef9339 9304 }
613e1657
KB
9305 else
9306 {
9307 *bytes_read = 4;
613e1657
KB
9308 }
9309
c764a876
DE
9310 return length;
9311}
dd373385 9312
c764a876
DE
9313/* Cover function for read_initial_length.
9314 Returns the length of the object at BUF, and stores the size of the
9315 initial length in *BYTES_READ and stores the size that offsets will be in
9316 *OFFSET_SIZE.
9317 If the initial length size is not equivalent to that specified in
9318 CU_HEADER then issue a complaint.
9319 This is useful when reading non-comp-unit headers. */
dd373385 9320
c764a876
DE
9321static LONGEST
9322read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9323 const struct comp_unit_head *cu_header,
9324 unsigned int *bytes_read,
9325 unsigned int *offset_size)
9326{
9327 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9328
9329 gdb_assert (cu_header->initial_length_size == 4
9330 || cu_header->initial_length_size == 8
9331 || cu_header->initial_length_size == 12);
9332
9333 if (cu_header->initial_length_size != *bytes_read)
9334 complaint (&symfile_complaints,
9335 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9336
c764a876 9337 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9338 return length;
613e1657
KB
9339}
9340
9341/* Read an offset from the data stream. The size of the offset is
917c78fc 9342 given by cu_header->offset_size. */
613e1657
KB
9343
9344static LONGEST
fe1b8b76 9345read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9346 unsigned int *bytes_read)
c764a876
DE
9347{
9348 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9349
c764a876
DE
9350 *bytes_read = cu_header->offset_size;
9351 return offset;
9352}
9353
9354/* Read an offset from the data stream. */
9355
9356static LONGEST
9357read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9358{
9359 LONGEST retval = 0;
9360
c764a876 9361 switch (offset_size)
613e1657
KB
9362 {
9363 case 4:
fe1b8b76 9364 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9365 break;
9366 case 8:
fe1b8b76 9367 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9368 break;
9369 default:
8e65ff28 9370 internal_error (__FILE__, __LINE__,
c764a876 9371 _("read_offset_1: bad switch [in module %s]"),
659b0389 9372 bfd_get_filename (abfd));
613e1657
KB
9373 }
9374
917c78fc 9375 return retval;
613e1657
KB
9376}
9377
fe1b8b76
JB
9378static gdb_byte *
9379read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9380{
9381 /* If the size of a host char is 8 bits, we can return a pointer
9382 to the buffer, otherwise we have to copy the data to a buffer
9383 allocated on the temporary obstack. */
4bdf3d34 9384 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9385 return buf;
c906108c
SS
9386}
9387
9388static char *
9b1c24c8 9389read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9390{
9391 /* If the size of a host char is 8 bits, we can return a pointer
9392 to the string, otherwise we have to copy the string to a buffer
9393 allocated on the temporary obstack. */
4bdf3d34 9394 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9395 if (*buf == '\0')
9396 {
9397 *bytes_read_ptr = 1;
9398 return NULL;
9399 }
fe1b8b76
JB
9400 *bytes_read_ptr = strlen ((char *) buf) + 1;
9401 return (char *) buf;
4bdf3d34
JJ
9402}
9403
9404static char *
fe1b8b76 9405read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9406 const struct comp_unit_head *cu_header,
9407 unsigned int *bytes_read_ptr)
9408{
c764a876 9409 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9410
be391dca 9411 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9412 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9413 {
8a3fe4f8 9414 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9415 bfd_get_filename (abfd));
4bdf3d34 9416 return NULL;
c906108c 9417 }
dce234bc 9418 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9419 {
8a3fe4f8 9420 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 9421 bfd_get_filename (abfd));
c906108c
SS
9422 return NULL;
9423 }
4bdf3d34 9424 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9425 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9426 return NULL;
dce234bc 9427 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9428}
9429
ce5d95e1 9430static unsigned long
fe1b8b76 9431read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9432{
ce5d95e1
JB
9433 unsigned long result;
9434 unsigned int num_read;
c906108c
SS
9435 int i, shift;
9436 unsigned char byte;
9437
9438 result = 0;
9439 shift = 0;
9440 num_read = 0;
9441 i = 0;
9442 while (1)
9443 {
fe1b8b76 9444 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9445 buf++;
9446 num_read++;
ce5d95e1 9447 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9448 if ((byte & 128) == 0)
9449 {
9450 break;
9451 }
9452 shift += 7;
9453 }
9454 *bytes_read_ptr = num_read;
9455 return result;
9456}
9457
ce5d95e1 9458static long
fe1b8b76 9459read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9460{
ce5d95e1 9461 long result;
77e0b926 9462 int i, shift, num_read;
c906108c
SS
9463 unsigned char byte;
9464
9465 result = 0;
9466 shift = 0;
c906108c
SS
9467 num_read = 0;
9468 i = 0;
9469 while (1)
9470 {
fe1b8b76 9471 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9472 buf++;
9473 num_read++;
ce5d95e1 9474 result |= ((long)(byte & 127) << shift);
c906108c
SS
9475 shift += 7;
9476 if ((byte & 128) == 0)
9477 {
9478 break;
9479 }
9480 }
77e0b926
DJ
9481 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9482 result |= -(((long)1) << shift);
c906108c
SS
9483 *bytes_read_ptr = num_read;
9484 return result;
9485}
9486
4bb7a0a7
DJ
9487/* Return a pointer to just past the end of an LEB128 number in BUF. */
9488
fe1b8b76
JB
9489static gdb_byte *
9490skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9491{
9492 int byte;
9493
9494 while (1)
9495 {
fe1b8b76 9496 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9497 buf++;
9498 if ((byte & 128) == 0)
9499 return buf;
9500 }
9501}
9502
c906108c 9503static void
e142c38c 9504set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9505{
9506 switch (lang)
9507 {
9508 case DW_LANG_C89:
76bee0cc 9509 case DW_LANG_C99:
c906108c 9510 case DW_LANG_C:
e142c38c 9511 cu->language = language_c;
c906108c
SS
9512 break;
9513 case DW_LANG_C_plus_plus:
e142c38c 9514 cu->language = language_cplus;
c906108c 9515 break;
6aecb9c2
JB
9516 case DW_LANG_D:
9517 cu->language = language_d;
9518 break;
c906108c
SS
9519 case DW_LANG_Fortran77:
9520 case DW_LANG_Fortran90:
b21b22e0 9521 case DW_LANG_Fortran95:
e142c38c 9522 cu->language = language_fortran;
c906108c
SS
9523 break;
9524 case DW_LANG_Mips_Assembler:
e142c38c 9525 cu->language = language_asm;
c906108c 9526 break;
bebd888e 9527 case DW_LANG_Java:
e142c38c 9528 cu->language = language_java;
bebd888e 9529 break;
c906108c 9530 case DW_LANG_Ada83:
8aaf0b47 9531 case DW_LANG_Ada95:
bc5f45f8
JB
9532 cu->language = language_ada;
9533 break;
72019c9c
GM
9534 case DW_LANG_Modula2:
9535 cu->language = language_m2;
9536 break;
fe8e67fd
PM
9537 case DW_LANG_Pascal83:
9538 cu->language = language_pascal;
9539 break;
22566fbd
DJ
9540 case DW_LANG_ObjC:
9541 cu->language = language_objc;
9542 break;
c906108c
SS
9543 case DW_LANG_Cobol74:
9544 case DW_LANG_Cobol85:
c906108c 9545 default:
e142c38c 9546 cu->language = language_minimal;
c906108c
SS
9547 break;
9548 }
e142c38c 9549 cu->language_defn = language_def (cu->language);
c906108c
SS
9550}
9551
9552/* Return the named attribute or NULL if not there. */
9553
9554static struct attribute *
e142c38c 9555dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9556{
9557 unsigned int i;
9558 struct attribute *spec = NULL;
9559
9560 for (i = 0; i < die->num_attrs; ++i)
9561 {
9562 if (die->attrs[i].name == name)
10b3939b 9563 return &die->attrs[i];
c906108c
SS
9564 if (die->attrs[i].name == DW_AT_specification
9565 || die->attrs[i].name == DW_AT_abstract_origin)
9566 spec = &die->attrs[i];
9567 }
c906108c 9568
10b3939b 9569 if (spec)
f2f0e013
DJ
9570 {
9571 die = follow_die_ref (die, spec, &cu);
9572 return dwarf2_attr (die, name, cu);
9573 }
c5aa993b 9574
c906108c
SS
9575 return NULL;
9576}
9577
348e048f
DE
9578/* Return the named attribute or NULL if not there,
9579 but do not follow DW_AT_specification, etc.
9580 This is for use in contexts where we're reading .debug_types dies.
9581 Following DW_AT_specification, DW_AT_abstract_origin will take us
9582 back up the chain, and we want to go down. */
9583
9584static struct attribute *
9585dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9586 struct dwarf2_cu *cu)
9587{
9588 unsigned int i;
9589
9590 for (i = 0; i < die->num_attrs; ++i)
9591 if (die->attrs[i].name == name)
9592 return &die->attrs[i];
9593
9594 return NULL;
9595}
9596
05cf31d1
JB
9597/* Return non-zero iff the attribute NAME is defined for the given DIE,
9598 and holds a non-zero value. This function should only be used for
2dc7f7b3 9599 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9600
9601static int
9602dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9603{
9604 struct attribute *attr = dwarf2_attr (die, name, cu);
9605
9606 return (attr && DW_UNSND (attr));
9607}
9608
3ca72b44 9609static int
e142c38c 9610die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9611{
05cf31d1
JB
9612 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9613 which value is non-zero. However, we have to be careful with
9614 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9615 (via dwarf2_flag_true_p) follows this attribute. So we may
9616 end up accidently finding a declaration attribute that belongs
9617 to a different DIE referenced by the specification attribute,
9618 even though the given DIE does not have a declaration attribute. */
9619 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9620 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9621}
9622
63d06c5c 9623/* Return the die giving the specification for DIE, if there is
f2f0e013 9624 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9625 containing the return value on output. If there is no
9626 specification, but there is an abstract origin, that is
9627 returned. */
63d06c5c
DC
9628
9629static struct die_info *
f2f0e013 9630die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9631{
f2f0e013
DJ
9632 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9633 *spec_cu);
63d06c5c 9634
edb3359d
DJ
9635 if (spec_attr == NULL)
9636 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9637
63d06c5c
DC
9638 if (spec_attr == NULL)
9639 return NULL;
9640 else
f2f0e013 9641 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9642}
c906108c 9643
debd256d
JB
9644/* Free the line_header structure *LH, and any arrays and strings it
9645 refers to. */
9646static void
9647free_line_header (struct line_header *lh)
9648{
9649 if (lh->standard_opcode_lengths)
a8bc7b56 9650 xfree (lh->standard_opcode_lengths);
debd256d
JB
9651
9652 /* Remember that all the lh->file_names[i].name pointers are
9653 pointers into debug_line_buffer, and don't need to be freed. */
9654 if (lh->file_names)
a8bc7b56 9655 xfree (lh->file_names);
debd256d
JB
9656
9657 /* Similarly for the include directory names. */
9658 if (lh->include_dirs)
a8bc7b56 9659 xfree (lh->include_dirs);
debd256d 9660
a8bc7b56 9661 xfree (lh);
debd256d
JB
9662}
9663
9664
9665/* Add an entry to LH's include directory table. */
9666static void
9667add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9668{
debd256d
JB
9669 /* Grow the array if necessary. */
9670 if (lh->include_dirs_size == 0)
c5aa993b 9671 {
debd256d
JB
9672 lh->include_dirs_size = 1; /* for testing */
9673 lh->include_dirs = xmalloc (lh->include_dirs_size
9674 * sizeof (*lh->include_dirs));
9675 }
9676 else if (lh->num_include_dirs >= lh->include_dirs_size)
9677 {
9678 lh->include_dirs_size *= 2;
9679 lh->include_dirs = xrealloc (lh->include_dirs,
9680 (lh->include_dirs_size
9681 * sizeof (*lh->include_dirs)));
c5aa993b 9682 }
c906108c 9683
debd256d
JB
9684 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9685}
6e70227d 9686
debd256d
JB
9687
9688/* Add an entry to LH's file name table. */
9689static void
9690add_file_name (struct line_header *lh,
9691 char *name,
9692 unsigned int dir_index,
9693 unsigned int mod_time,
9694 unsigned int length)
9695{
9696 struct file_entry *fe;
9697
9698 /* Grow the array if necessary. */
9699 if (lh->file_names_size == 0)
9700 {
9701 lh->file_names_size = 1; /* for testing */
9702 lh->file_names = xmalloc (lh->file_names_size
9703 * sizeof (*lh->file_names));
9704 }
9705 else if (lh->num_file_names >= lh->file_names_size)
9706 {
9707 lh->file_names_size *= 2;
9708 lh->file_names = xrealloc (lh->file_names,
9709 (lh->file_names_size
9710 * sizeof (*lh->file_names)));
9711 }
9712
9713 fe = &lh->file_names[lh->num_file_names++];
9714 fe->name = name;
9715 fe->dir_index = dir_index;
9716 fe->mod_time = mod_time;
9717 fe->length = length;
aaa75496 9718 fe->included_p = 0;
cb1df416 9719 fe->symtab = NULL;
debd256d 9720}
6e70227d 9721
debd256d
JB
9722
9723/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9724 .debug_line, according to the endianness of ABFD. Return a pointer
9725 to a struct line_header, allocated using xmalloc.
debd256d
JB
9726
9727 NOTE: the strings in the include directory and file name tables of
9728 the returned object point into debug_line_buffer, and must not be
9729 freed. */
9730static struct line_header *
9731dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9732 struct dwarf2_cu *cu)
debd256d
JB
9733{
9734 struct cleanup *back_to;
9735 struct line_header *lh;
fe1b8b76 9736 gdb_byte *line_ptr;
c764a876 9737 unsigned int bytes_read, offset_size;
debd256d
JB
9738 int i;
9739 char *cur_dir, *cur_file;
9740
be391dca 9741 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9742 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9743 {
e2e0b3e5 9744 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9745 return 0;
9746 }
9747
a738430d
MK
9748 /* Make sure that at least there's room for the total_length field.
9749 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9750 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 9751 {
4d3c2250 9752 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9753 return 0;
9754 }
9755
9756 lh = xmalloc (sizeof (*lh));
9757 memset (lh, 0, sizeof (*lh));
9758 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9759 (void *) lh);
9760
dce234bc 9761 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9762
a738430d 9763 /* Read in the header. */
6e70227d 9764 lh->total_length =
c764a876
DE
9765 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9766 &bytes_read, &offset_size);
debd256d 9767 line_ptr += bytes_read;
dce234bc
PP
9768 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9769 + dwarf2_per_objfile->line.size))
debd256d 9770 {
4d3c2250 9771 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9772 return 0;
9773 }
9774 lh->statement_program_end = line_ptr + lh->total_length;
9775 lh->version = read_2_bytes (abfd, line_ptr);
9776 line_ptr += 2;
c764a876
DE
9777 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9778 line_ptr += offset_size;
debd256d
JB
9779 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9780 line_ptr += 1;
2dc7f7b3
TT
9781 if (lh->version >= 4)
9782 {
9783 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9784 line_ptr += 1;
9785 }
9786 else
9787 lh->maximum_ops_per_instruction = 1;
9788
9789 if (lh->maximum_ops_per_instruction == 0)
9790 {
9791 lh->maximum_ops_per_instruction = 1;
9792 complaint (&symfile_complaints,
9793 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9794 }
9795
debd256d
JB
9796 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9797 line_ptr += 1;
9798 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9799 line_ptr += 1;
9800 lh->line_range = read_1_byte (abfd, line_ptr);
9801 line_ptr += 1;
9802 lh->opcode_base = read_1_byte (abfd, line_ptr);
9803 line_ptr += 1;
9804 lh->standard_opcode_lengths
fe1b8b76 9805 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9806
9807 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9808 for (i = 1; i < lh->opcode_base; ++i)
9809 {
9810 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9811 line_ptr += 1;
9812 }
9813
a738430d 9814 /* Read directory table. */
9b1c24c8 9815 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9816 {
9817 line_ptr += bytes_read;
9818 add_include_dir (lh, cur_dir);
9819 }
9820 line_ptr += bytes_read;
9821
a738430d 9822 /* Read file name table. */
9b1c24c8 9823 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9824 {
9825 unsigned int dir_index, mod_time, length;
9826
9827 line_ptr += bytes_read;
9828 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9829 line_ptr += bytes_read;
9830 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9831 line_ptr += bytes_read;
9832 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9833 line_ptr += bytes_read;
9834
9835 add_file_name (lh, cur_file, dir_index, mod_time, length);
9836 }
9837 line_ptr += bytes_read;
6e70227d 9838 lh->statement_program_start = line_ptr;
debd256d 9839
dce234bc
PP
9840 if (line_ptr > (dwarf2_per_objfile->line.buffer
9841 + dwarf2_per_objfile->line.size))
4d3c2250 9842 complaint (&symfile_complaints,
e2e0b3e5 9843 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
9844
9845 discard_cleanups (back_to);
9846 return lh;
9847}
c906108c 9848
5fb290d7
DJ
9849/* This function exists to work around a bug in certain compilers
9850 (particularly GCC 2.95), in which the first line number marker of a
9851 function does not show up until after the prologue, right before
9852 the second line number marker. This function shifts ADDRESS down
9853 to the beginning of the function if necessary, and is called on
9854 addresses passed to record_line. */
9855
9856static CORE_ADDR
e142c38c 9857check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
9858{
9859 struct function_range *fn;
9860
9861 /* Find the function_range containing address. */
e142c38c 9862 if (!cu->first_fn)
5fb290d7
DJ
9863 return address;
9864
e142c38c
DJ
9865 if (!cu->cached_fn)
9866 cu->cached_fn = cu->first_fn;
5fb290d7 9867
e142c38c 9868 fn = cu->cached_fn;
5fb290d7
DJ
9869 while (fn)
9870 if (fn->lowpc <= address && fn->highpc > address)
9871 goto found;
9872 else
9873 fn = fn->next;
9874
e142c38c
DJ
9875 fn = cu->first_fn;
9876 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
9877 if (fn->lowpc <= address && fn->highpc > address)
9878 goto found;
9879 else
9880 fn = fn->next;
9881
9882 return address;
9883
9884 found:
9885 if (fn->seen_line)
9886 return address;
9887 if (address != fn->lowpc)
4d3c2250 9888 complaint (&symfile_complaints,
e2e0b3e5 9889 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 9890 (unsigned long) address, fn->name);
5fb290d7
DJ
9891 fn->seen_line = 1;
9892 return fn->lowpc;
9893}
9894
aaa75496
JB
9895/* Decode the Line Number Program (LNP) for the given line_header
9896 structure and CU. The actual information extracted and the type
9897 of structures created from the LNP depends on the value of PST.
9898
9899 1. If PST is NULL, then this procedure uses the data from the program
9900 to create all necessary symbol tables, and their linetables.
9901 The compilation directory of the file is passed in COMP_DIR,
9902 and must not be NULL.
6e70227d 9903
aaa75496
JB
9904 2. If PST is not NULL, this procedure reads the program to determine
9905 the list of files included by the unit represented by PST, and
9906 builds all the associated partial symbol tables. In this case,
9907 the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
9908 is not used to compute the full name of the symtab, and therefore
9909 omitting it when building the partial symtab does not introduce
9910 the potential for inconsistency - a partial symtab and its associated
9911 symbtab having a different fullname -). */
debd256d 9912
c906108c 9913static void
debd256d 9914dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 9915 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 9916{
a8c50c1f 9917 gdb_byte *line_ptr, *extended_end;
fe1b8b76 9918 gdb_byte *line_end;
a8c50c1f 9919 unsigned int bytes_read, extended_len;
c906108c 9920 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
9921 CORE_ADDR baseaddr;
9922 struct objfile *objfile = cu->objfile;
fbf65064 9923 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 9924 const int decode_for_pst_p = (pst != NULL);
cb1df416 9925 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
9926
9927 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9928
debd256d
JB
9929 line_ptr = lh->statement_program_start;
9930 line_end = lh->statement_program_end;
c906108c
SS
9931
9932 /* Read the statement sequences until there's nothing left. */
9933 while (line_ptr < line_end)
9934 {
9935 /* state machine registers */
9936 CORE_ADDR address = 0;
9937 unsigned int file = 1;
9938 unsigned int line = 1;
9939 unsigned int column = 0;
debd256d 9940 int is_stmt = lh->default_is_stmt;
c906108c
SS
9941 int basic_block = 0;
9942 int end_sequence = 0;
fbf65064 9943 CORE_ADDR addr;
2dc7f7b3 9944 unsigned char op_index = 0;
c906108c 9945
aaa75496 9946 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 9947 {
aaa75496 9948 /* Start a subfile for the current file of the state machine. */
debd256d
JB
9949 /* lh->include_dirs and lh->file_names are 0-based, but the
9950 directory and file name numbers in the statement program
9951 are 1-based. */
9952 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 9953 char *dir = NULL;
a738430d 9954
debd256d
JB
9955 if (fe->dir_index)
9956 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
9957
9958 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
9959 }
9960
a738430d 9961 /* Decode the table. */
c5aa993b 9962 while (!end_sequence)
c906108c
SS
9963 {
9964 op_code = read_1_byte (abfd, line_ptr);
9965 line_ptr += 1;
59205f5a
JB
9966 if (line_ptr > line_end)
9967 {
9968 dwarf2_debug_line_missing_end_sequence_complaint ();
9969 break;
9970 }
9aa1fe7e 9971
debd256d 9972 if (op_code >= lh->opcode_base)
6e70227d 9973 {
a738430d 9974 /* Special operand. */
debd256d 9975 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
9976 address += (((op_index + (adj_opcode / lh->line_range))
9977 / lh->maximum_ops_per_instruction)
9978 * lh->minimum_instruction_length);
9979 op_index = ((op_index + (adj_opcode / lh->line_range))
9980 % lh->maximum_ops_per_instruction);
debd256d 9981 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 9982 if (lh->num_file_names < file || file == 0)
25e43795 9983 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
9984 /* For now we ignore lines not starting on an
9985 instruction boundary. */
9986 else if (op_index == 0)
25e43795
DJ
9987 {
9988 lh->file_names[file - 1].included_p = 1;
ca5f395d 9989 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
9990 {
9991 if (last_subfile != current_subfile)
9992 {
9993 addr = gdbarch_addr_bits_remove (gdbarch, address);
9994 if (last_subfile)
9995 record_line (last_subfile, 0, addr);
9996 last_subfile = current_subfile;
9997 }
25e43795 9998 /* Append row to matrix using current values. */
fbf65064
UW
9999 addr = check_cu_functions (address, cu);
10000 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10001 record_line (current_subfile, line, addr);
366da635 10002 }
25e43795 10003 }
ca5f395d 10004 basic_block = 0;
9aa1fe7e
GK
10005 }
10006 else switch (op_code)
c906108c
SS
10007 {
10008 case DW_LNS_extended_op:
a8c50c1f 10009 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 10010 line_ptr += bytes_read;
a8c50c1f 10011 extended_end = line_ptr + extended_len;
c906108c
SS
10012 extended_op = read_1_byte (abfd, line_ptr);
10013 line_ptr += 1;
10014 switch (extended_op)
10015 {
10016 case DW_LNE_end_sequence:
10017 end_sequence = 1;
c906108c
SS
10018 break;
10019 case DW_LNE_set_address:
e7c27a73 10020 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10021 op_index = 0;
107d2387
AC
10022 line_ptr += bytes_read;
10023 address += baseaddr;
c906108c
SS
10024 break;
10025 case DW_LNE_define_file:
debd256d
JB
10026 {
10027 char *cur_file;
10028 unsigned int dir_index, mod_time, length;
6e70227d 10029
9b1c24c8 10030 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
debd256d
JB
10031 line_ptr += bytes_read;
10032 dir_index =
10033 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10034 line_ptr += bytes_read;
10035 mod_time =
10036 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10037 line_ptr += bytes_read;
10038 length =
10039 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10040 line_ptr += bytes_read;
10041 add_file_name (lh, cur_file, dir_index, mod_time, length);
10042 }
c906108c 10043 break;
d0c6ba3d
CC
10044 case DW_LNE_set_discriminator:
10045 /* The discriminator is not interesting to the debugger;
10046 just ignore it. */
10047 line_ptr = extended_end;
10048 break;
c906108c 10049 default:
4d3c2250 10050 complaint (&symfile_complaints,
e2e0b3e5 10051 _("mangled .debug_line section"));
debd256d 10052 return;
c906108c 10053 }
a8c50c1f
DJ
10054 /* Make sure that we parsed the extended op correctly. If e.g.
10055 we expected a different address size than the producer used,
10056 we may have read the wrong number of bytes. */
10057 if (line_ptr != extended_end)
10058 {
10059 complaint (&symfile_complaints,
10060 _("mangled .debug_line section"));
10061 return;
10062 }
c906108c
SS
10063 break;
10064 case DW_LNS_copy:
59205f5a 10065 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10066 dwarf2_debug_line_missing_file_complaint ();
10067 else
366da635 10068 {
25e43795 10069 lh->file_names[file - 1].included_p = 1;
ca5f395d 10070 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10071 {
10072 if (last_subfile != current_subfile)
10073 {
10074 addr = gdbarch_addr_bits_remove (gdbarch, address);
10075 if (last_subfile)
10076 record_line (last_subfile, 0, addr);
10077 last_subfile = current_subfile;
10078 }
10079 addr = check_cu_functions (address, cu);
10080 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10081 record_line (current_subfile, line, addr);
10082 }
366da635 10083 }
c906108c
SS
10084 basic_block = 0;
10085 break;
10086 case DW_LNS_advance_pc:
2dc7f7b3
TT
10087 {
10088 CORE_ADDR adjust
10089 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10090
10091 address += (((op_index + adjust)
10092 / lh->maximum_ops_per_instruction)
10093 * lh->minimum_instruction_length);
10094 op_index = ((op_index + adjust)
10095 % lh->maximum_ops_per_instruction);
10096 line_ptr += bytes_read;
10097 }
c906108c
SS
10098 break;
10099 case DW_LNS_advance_line:
10100 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10101 line_ptr += bytes_read;
10102 break;
10103 case DW_LNS_set_file:
debd256d 10104 {
a738430d
MK
10105 /* The arrays lh->include_dirs and lh->file_names are
10106 0-based, but the directory and file name numbers in
10107 the statement program are 1-based. */
debd256d 10108 struct file_entry *fe;
4f1520fb 10109 char *dir = NULL;
a738430d 10110
debd256d
JB
10111 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10112 line_ptr += bytes_read;
59205f5a 10113 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10114 dwarf2_debug_line_missing_file_complaint ();
10115 else
10116 {
10117 fe = &lh->file_names[file - 1];
10118 if (fe->dir_index)
10119 dir = lh->include_dirs[fe->dir_index - 1];
10120 if (!decode_for_pst_p)
10121 {
10122 last_subfile = current_subfile;
10123 dwarf2_start_subfile (fe->name, dir, comp_dir);
10124 }
10125 }
debd256d 10126 }
c906108c
SS
10127 break;
10128 case DW_LNS_set_column:
10129 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10130 line_ptr += bytes_read;
10131 break;
10132 case DW_LNS_negate_stmt:
10133 is_stmt = (!is_stmt);
10134 break;
10135 case DW_LNS_set_basic_block:
10136 basic_block = 1;
10137 break;
c2c6d25f
JM
10138 /* Add to the address register of the state machine the
10139 address increment value corresponding to special opcode
a738430d
MK
10140 255. I.e., this value is scaled by the minimum
10141 instruction length since special opcode 255 would have
10142 scaled the the increment. */
c906108c 10143 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10144 {
10145 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10146
10147 address += (((op_index + adjust)
10148 / lh->maximum_ops_per_instruction)
10149 * lh->minimum_instruction_length);
10150 op_index = ((op_index + adjust)
10151 % lh->maximum_ops_per_instruction);
10152 }
c906108c
SS
10153 break;
10154 case DW_LNS_fixed_advance_pc:
10155 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10156 op_index = 0;
c906108c
SS
10157 line_ptr += 2;
10158 break;
9aa1fe7e 10159 default:
a738430d
MK
10160 {
10161 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10162 int i;
a738430d 10163
debd256d 10164 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10165 {
10166 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10167 line_ptr += bytes_read;
10168 }
10169 }
c906108c
SS
10170 }
10171 }
59205f5a
JB
10172 if (lh->num_file_names < file || file == 0)
10173 dwarf2_debug_line_missing_file_complaint ();
10174 else
10175 {
10176 lh->file_names[file - 1].included_p = 1;
10177 if (!decode_for_pst_p)
fbf65064
UW
10178 {
10179 addr = gdbarch_addr_bits_remove (gdbarch, address);
10180 record_line (current_subfile, 0, addr);
10181 }
59205f5a 10182 }
c906108c 10183 }
aaa75496
JB
10184
10185 if (decode_for_pst_p)
10186 {
10187 int file_index;
10188
10189 /* Now that we're done scanning the Line Header Program, we can
10190 create the psymtab of each included file. */
10191 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10192 if (lh->file_names[file_index].included_p == 1)
10193 {
5b5464ad
JB
10194 const struct file_entry fe = lh->file_names [file_index];
10195 char *include_name = fe.name;
10196 char *dir_name = NULL;
10197 char *pst_filename = pst->filename;
10198
10199 if (fe.dir_index)
10200 dir_name = lh->include_dirs[fe.dir_index - 1];
10201
10202 if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
10203 {
1754f103
MK
10204 include_name = concat (dir_name, SLASH_STRING,
10205 include_name, (char *)NULL);
5b5464ad
JB
10206 make_cleanup (xfree, include_name);
10207 }
10208
10209 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10210 {
1754f103
MK
10211 pst_filename = concat (pst->dirname, SLASH_STRING,
10212 pst_filename, (char *)NULL);
5b5464ad
JB
10213 make_cleanup (xfree, pst_filename);
10214 }
10215
10216 if (strcmp (include_name, pst_filename) != 0)
aaa75496
JB
10217 dwarf2_create_include_psymtab (include_name, pst, objfile);
10218 }
10219 }
cb1df416
DJ
10220 else
10221 {
10222 /* Make sure a symtab is created for every file, even files
10223 which contain only variables (i.e. no code with associated
10224 line numbers). */
10225
10226 int i;
10227 struct file_entry *fe;
10228
10229 for (i = 0; i < lh->num_file_names; i++)
10230 {
10231 char *dir = NULL;
9a619af0 10232
cb1df416
DJ
10233 fe = &lh->file_names[i];
10234 if (fe->dir_index)
10235 dir = lh->include_dirs[fe->dir_index - 1];
10236 dwarf2_start_subfile (fe->name, dir, comp_dir);
10237
10238 /* Skip the main file; we don't need it, and it must be
10239 allocated last, so that it will show up before the
10240 non-primary symtabs in the objfile's symtab list. */
10241 if (current_subfile == first_subfile)
10242 continue;
10243
10244 if (current_subfile->symtab == NULL)
10245 current_subfile->symtab = allocate_symtab (current_subfile->name,
10246 cu->objfile);
10247 fe->symtab = current_subfile->symtab;
10248 }
10249 }
c906108c
SS
10250}
10251
10252/* Start a subfile for DWARF. FILENAME is the name of the file and
10253 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10254 or NULL if not known. COMP_DIR is the compilation directory for the
10255 linetable's compilation unit or NULL if not known.
c906108c
SS
10256 This routine tries to keep line numbers from identical absolute and
10257 relative file names in a common subfile.
10258
10259 Using the `list' example from the GDB testsuite, which resides in
10260 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10261 of /srcdir/list0.c yields the following debugging information for list0.c:
10262
c5aa993b
JM
10263 DW_AT_name: /srcdir/list0.c
10264 DW_AT_comp_dir: /compdir
357e46e7 10265 files.files[0].name: list0.h
c5aa993b 10266 files.files[0].dir: /srcdir
357e46e7 10267 files.files[1].name: list0.c
c5aa993b 10268 files.files[1].dir: /srcdir
c906108c
SS
10269
10270 The line number information for list0.c has to end up in a single
4f1520fb
FR
10271 subfile, so that `break /srcdir/list0.c:1' works as expected.
10272 start_subfile will ensure that this happens provided that we pass the
10273 concatenation of files.files[1].dir and files.files[1].name as the
10274 subfile's name. */
c906108c
SS
10275
10276static void
4f1520fb 10277dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 10278{
4f1520fb
FR
10279 char *fullname;
10280
10281 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10282 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10283 second argument to start_subfile. To be consistent, we do the
10284 same here. In order not to lose the line information directory,
10285 we concatenate it to the filename when it makes sense.
10286 Note that the Dwarf3 standard says (speaking of filenames in line
10287 information): ``The directory index is ignored for file names
10288 that represent full path names''. Thus ignoring dirname in the
10289 `else' branch below isn't an issue. */
c906108c 10290
d5166ae1 10291 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10292 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10293 else
10294 fullname = filename;
c906108c 10295
4f1520fb
FR
10296 start_subfile (fullname, comp_dir);
10297
10298 if (fullname != filename)
10299 xfree (fullname);
c906108c
SS
10300}
10301
4c2df51b
DJ
10302static void
10303var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10304 struct dwarf2_cu *cu)
4c2df51b 10305{
e7c27a73
DJ
10306 struct objfile *objfile = cu->objfile;
10307 struct comp_unit_head *cu_header = &cu->header;
10308
4c2df51b
DJ
10309 /* NOTE drow/2003-01-30: There used to be a comment and some special
10310 code here to turn a symbol with DW_AT_external and a
10311 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10312 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10313 with some versions of binutils) where shared libraries could have
10314 relocations against symbols in their debug information - the
10315 minimal symbol would have the right address, but the debug info
10316 would not. It's no longer necessary, because we will explicitly
10317 apply relocations when we read in the debug information now. */
10318
10319 /* A DW_AT_location attribute with no contents indicates that a
10320 variable has been optimized away. */
10321 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10322 {
10323 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10324 return;
10325 }
10326
10327 /* Handle one degenerate form of location expression specially, to
10328 preserve GDB's previous behavior when section offsets are
10329 specified. If this is just a DW_OP_addr then mark this symbol
10330 as LOC_STATIC. */
10331
10332 if (attr_form_is_block (attr)
10333 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10334 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10335 {
891d2f0b 10336 unsigned int dummy;
4c2df51b
DJ
10337
10338 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10339 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10340 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10341 fixup_symbol_section (sym, objfile);
10342 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10343 SYMBOL_SECTION (sym));
4c2df51b
DJ
10344 return;
10345 }
10346
10347 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10348 expression evaluator, and use LOC_COMPUTED only when necessary
10349 (i.e. when the value of a register or memory location is
10350 referenced, or a thread-local block, etc.). Then again, it might
10351 not be worthwhile. I'm assuming that it isn't unless performance
10352 or memory numbers show me otherwise. */
10353
e7c27a73 10354 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10355 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10356}
10357
c906108c
SS
10358/* Given a pointer to a DWARF information entry, figure out if we need
10359 to make a symbol table entry for it, and if so, create a new entry
10360 and return a pointer to it.
10361 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10362 used the passed type.
10363 If SPACE is not NULL, use it to hold the new symbol. If it is
10364 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10365
10366static struct symbol *
34eaf542
TT
10367new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10368 struct symbol *space)
c906108c 10369{
e7c27a73 10370 struct objfile *objfile = cu->objfile;
c906108c
SS
10371 struct symbol *sym = NULL;
10372 char *name;
10373 struct attribute *attr = NULL;
10374 struct attribute *attr2 = NULL;
e142c38c 10375 CORE_ADDR baseaddr;
e37fd15a
SW
10376 struct pending **list_to_add = NULL;
10377
edb3359d 10378 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10379
10380 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10381
94af9270 10382 name = dwarf2_name (die, cu);
c906108c
SS
10383 if (name)
10384 {
94af9270 10385 const char *linkagename;
34eaf542 10386 int suppress_add = 0;
94af9270 10387
34eaf542
TT
10388 if (space)
10389 sym = space;
10390 else
10391 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10392 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10393
10394 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10395 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10396 linkagename = dwarf2_physname (name, die, cu);
10397 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10398
f55ee35c
JK
10399 /* Fortran does not have mangling standard and the mangling does differ
10400 between gfortran, iFort etc. */
10401 if (cu->language == language_fortran
b250c185 10402 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10403 symbol_set_demangled_name (&(sym->ginfo),
10404 (char *) dwarf2_full_name (name, die, cu),
10405 NULL);
f55ee35c 10406
c906108c 10407 /* Default assumptions.
c5aa993b 10408 Use the passed type or decode it from the die. */
176620f1 10409 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10410 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10411 if (type != NULL)
10412 SYMBOL_TYPE (sym) = type;
10413 else
e7c27a73 10414 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10415 attr = dwarf2_attr (die,
10416 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10417 cu);
c906108c
SS
10418 if (attr)
10419 {
10420 SYMBOL_LINE (sym) = DW_UNSND (attr);
10421 }
cb1df416 10422
edb3359d
DJ
10423 attr = dwarf2_attr (die,
10424 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10425 cu);
cb1df416
DJ
10426 if (attr)
10427 {
10428 int file_index = DW_UNSND (attr);
9a619af0 10429
cb1df416
DJ
10430 if (cu->line_header == NULL
10431 || file_index > cu->line_header->num_file_names)
10432 complaint (&symfile_complaints,
10433 _("file index out of range"));
1c3d648d 10434 else if (file_index > 0)
cb1df416
DJ
10435 {
10436 struct file_entry *fe;
9a619af0 10437
cb1df416
DJ
10438 fe = &cu->line_header->file_names[file_index - 1];
10439 SYMBOL_SYMTAB (sym) = fe->symtab;
10440 }
10441 }
10442
c906108c
SS
10443 switch (die->tag)
10444 {
10445 case DW_TAG_label:
e142c38c 10446 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10447 if (attr)
10448 {
10449 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10450 }
0f5238ed
TT
10451 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10452 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10453 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10454 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10455 break;
10456 case DW_TAG_subprogram:
10457 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10458 finish_block. */
10459 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10460 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10461 if ((attr2 && (DW_UNSND (attr2) != 0))
10462 || cu->language == language_ada)
c906108c 10463 {
2cfa0c8d
JB
10464 /* Subprograms marked external are stored as a global symbol.
10465 Ada subprograms, whether marked external or not, are always
10466 stored as a global symbol, because we want to be able to
10467 access them globally. For instance, we want to be able
10468 to break on a nested subprogram without having to
10469 specify the context. */
e37fd15a 10470 list_to_add = &global_symbols;
c906108c
SS
10471 }
10472 else
10473 {
e37fd15a 10474 list_to_add = cu->list_in_scope;
c906108c
SS
10475 }
10476 break;
edb3359d
DJ
10477 case DW_TAG_inlined_subroutine:
10478 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10479 finish_block. */
10480 SYMBOL_CLASS (sym) = LOC_BLOCK;
10481 SYMBOL_INLINED (sym) = 1;
10482 /* Do not add the symbol to any lists. It will be found via
10483 BLOCK_FUNCTION from the blockvector. */
10484 break;
34eaf542
TT
10485 case DW_TAG_template_value_param:
10486 suppress_add = 1;
10487 /* Fall through. */
c906108c 10488 case DW_TAG_variable:
254e6b9e 10489 case DW_TAG_member:
c906108c
SS
10490 /* Compilation with minimal debug info may result in variables
10491 with missing type entries. Change the misleading `void' type
10492 to something sensible. */
10493 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10494 SYMBOL_TYPE (sym)
46bf5051 10495 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10496
e142c38c 10497 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10498 /* In the case of DW_TAG_member, we should only be called for
10499 static const members. */
10500 if (die->tag == DW_TAG_member)
10501 {
3863f96c
DE
10502 /* dwarf2_add_field uses die_is_declaration,
10503 so we do the same. */
254e6b9e
DE
10504 gdb_assert (die_is_declaration (die, cu));
10505 gdb_assert (attr);
10506 }
c906108c
SS
10507 if (attr)
10508 {
e7c27a73 10509 dwarf2_const_value (attr, sym, cu);
e142c38c 10510 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10511 if (!suppress_add)
34eaf542
TT
10512 {
10513 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10514 list_to_add = &global_symbols;
34eaf542 10515 else
e37fd15a 10516 list_to_add = cu->list_in_scope;
34eaf542 10517 }
c906108c
SS
10518 break;
10519 }
e142c38c 10520 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10521 if (attr)
10522 {
e7c27a73 10523 var_decode_location (attr, sym, cu);
e142c38c 10524 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10525 if (SYMBOL_CLASS (sym) == LOC_STATIC
10526 && SYMBOL_VALUE_ADDRESS (sym) == 0
10527 && !dwarf2_per_objfile->has_section_at_zero)
10528 {
10529 /* When a static variable is eliminated by the linker,
10530 the corresponding debug information is not stripped
10531 out, but the variable address is set to null;
10532 do not add such variables into symbol table. */
10533 }
10534 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10535 {
f55ee35c
JK
10536 /* Workaround gfortran PR debug/40040 - it uses
10537 DW_AT_location for variables in -fPIC libraries which may
10538 get overriden by other libraries/executable and get
10539 a different address. Resolve it by the minimal symbol
10540 which may come from inferior's executable using copy
10541 relocation. Make this workaround only for gfortran as for
10542 other compilers GDB cannot guess the minimal symbol
10543 Fortran mangling kind. */
10544 if (cu->language == language_fortran && die->parent
10545 && die->parent->tag == DW_TAG_module
10546 && cu->producer
10547 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10548 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10549
1c809c68
TT
10550 /* A variable with DW_AT_external is never static,
10551 but it may be block-scoped. */
10552 list_to_add = (cu->list_in_scope == &file_symbols
10553 ? &global_symbols : cu->list_in_scope);
1c809c68 10554 }
c906108c 10555 else
e37fd15a 10556 list_to_add = cu->list_in_scope;
c906108c
SS
10557 }
10558 else
10559 {
10560 /* We do not know the address of this symbol.
c5aa993b
JM
10561 If it is an external symbol and we have type information
10562 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10563 The address of the variable will then be determined from
10564 the minimal symbol table whenever the variable is
10565 referenced. */
e142c38c 10566 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10567 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10568 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10569 {
0fe7935b
DJ
10570 /* A variable with DW_AT_external is never static, but it
10571 may be block-scoped. */
10572 list_to_add = (cu->list_in_scope == &file_symbols
10573 ? &global_symbols : cu->list_in_scope);
10574
c906108c 10575 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10576 }
442ddf59
JK
10577 else if (!die_is_declaration (die, cu))
10578 {
10579 /* Use the default LOC_OPTIMIZED_OUT class. */
10580 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10581 if (!suppress_add)
10582 list_to_add = cu->list_in_scope;
442ddf59 10583 }
c906108c
SS
10584 }
10585 break;
10586 case DW_TAG_formal_parameter:
edb3359d
DJ
10587 /* If we are inside a function, mark this as an argument. If
10588 not, we might be looking at an argument to an inlined function
10589 when we do not have enough information to show inlined frames;
10590 pretend it's a local variable in that case so that the user can
10591 still see it. */
10592 if (context_stack_depth > 0
10593 && context_stack[context_stack_depth - 1].name != NULL)
10594 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10595 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10596 if (attr)
10597 {
e7c27a73 10598 var_decode_location (attr, sym, cu);
c906108c 10599 }
e142c38c 10600 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10601 if (attr)
10602 {
e7c27a73 10603 dwarf2_const_value (attr, sym, cu);
c906108c 10604 }
f346a30d
PM
10605 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10606 if (attr && DW_UNSND (attr))
10607 {
10608 struct type *ref_type;
10609
10610 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10611 SYMBOL_TYPE (sym) = ref_type;
10612 }
10613
e37fd15a 10614 list_to_add = cu->list_in_scope;
c906108c
SS
10615 break;
10616 case DW_TAG_unspecified_parameters:
10617 /* From varargs functions; gdb doesn't seem to have any
10618 interest in this information, so just ignore it for now.
10619 (FIXME?) */
10620 break;
34eaf542
TT
10621 case DW_TAG_template_type_param:
10622 suppress_add = 1;
10623 /* Fall through. */
c906108c 10624 case DW_TAG_class_type:
680b30c7 10625 case DW_TAG_interface_type:
c906108c
SS
10626 case DW_TAG_structure_type:
10627 case DW_TAG_union_type:
72019c9c 10628 case DW_TAG_set_type:
c906108c
SS
10629 case DW_TAG_enumeration_type:
10630 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10631 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 10632
63d06c5c 10633 {
987504bb 10634 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
10635 really ever be static objects: otherwise, if you try
10636 to, say, break of a class's method and you're in a file
10637 which doesn't mention that class, it won't work unless
10638 the check for all static symbols in lookup_symbol_aux
10639 saves you. See the OtherFileClass tests in
10640 gdb.c++/namespace.exp. */
10641
e37fd15a 10642 if (!suppress_add)
34eaf542 10643 {
34eaf542
TT
10644 list_to_add = (cu->list_in_scope == &file_symbols
10645 && (cu->language == language_cplus
10646 || cu->language == language_java)
10647 ? &global_symbols : cu->list_in_scope);
34eaf542 10648 }
63d06c5c
DC
10649
10650 /* The semantics of C++ state that "struct foo { ... }" also
987504bb 10651 defines a typedef for "foo". A Java class declaration also
5eeb2539 10652 defines a typedef for the class. */
987504bb 10653 if (cu->language == language_cplus
8c6860bb
JB
10654 || cu->language == language_java
10655 || cu->language == language_ada)
63d06c5c 10656 {
d8151005
DJ
10657 /* The symbol's name is already allocated along with
10658 this objfile, so we don't need to duplicate it for
10659 the type. */
63d06c5c 10660 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
77ef991d 10661 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
63d06c5c
DC
10662 }
10663 }
c906108c
SS
10664 break;
10665 case DW_TAG_typedef:
63d06c5c
DC
10666 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10667 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10668 list_to_add = cu->list_in_scope;
63d06c5c 10669 break;
c906108c 10670 case DW_TAG_base_type:
a02abb62 10671 case DW_TAG_subrange_type:
c906108c 10672 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10673 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10674 list_to_add = cu->list_in_scope;
c906108c
SS
10675 break;
10676 case DW_TAG_enumerator:
e142c38c 10677 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10678 if (attr)
10679 {
e7c27a73 10680 dwarf2_const_value (attr, sym, cu);
c906108c 10681 }
63d06c5c
DC
10682 {
10683 /* NOTE: carlton/2003-11-10: See comment above in the
10684 DW_TAG_class_type, etc. block. */
10685
e142c38c 10686 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10687 && (cu->language == language_cplus
10688 || cu->language == language_java)
e142c38c 10689 ? &global_symbols : cu->list_in_scope);
63d06c5c 10690 }
c906108c 10691 break;
5c4e30ca
DC
10692 case DW_TAG_namespace:
10693 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 10694 list_to_add = &global_symbols;
5c4e30ca 10695 break;
c906108c
SS
10696 default:
10697 /* Not a tag we recognize. Hopefully we aren't processing
10698 trash data, but since we must specifically ignore things
10699 we don't recognize, there is nothing else we should do at
10700 this point. */
e2e0b3e5 10701 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10702 dwarf_tag_name (die->tag));
c906108c
SS
10703 break;
10704 }
df8a16a1 10705
e37fd15a
SW
10706 if (suppress_add)
10707 {
10708 sym->hash_next = objfile->template_symbols;
10709 objfile->template_symbols = sym;
10710 list_to_add = NULL;
10711 }
10712
10713 if (list_to_add != NULL)
10714 add_symbol_to_list (sym, list_to_add);
10715
df8a16a1
DJ
10716 /* For the benefit of old versions of GCC, check for anonymous
10717 namespaces based on the demangled name. */
10718 if (!processing_has_namespace_info
94af9270 10719 && cu->language == language_cplus)
df8a16a1 10720 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10721 }
10722 return (sym);
10723}
10724
34eaf542
TT
10725/* A wrapper for new_symbol_full that always allocates a new symbol. */
10726
10727static struct symbol *
10728new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10729{
10730 return new_symbol_full (die, type, cu, NULL);
10731}
10732
98bfdba5
PA
10733/* Given an attr with a DW_FORM_dataN value in host byte order,
10734 zero-extend it as appropriate for the symbol's type. The DWARF
10735 standard (v4) is not entirely clear about the meaning of using
10736 DW_FORM_dataN for a constant with a signed type, where the type is
10737 wider than the data. The conclusion of a discussion on the DWARF
10738 list was that this is unspecified. We choose to always zero-extend
10739 because that is the interpretation long in use by GCC. */
c906108c 10740
98bfdba5
PA
10741static gdb_byte *
10742dwarf2_const_value_data (struct attribute *attr, struct type *type,
10743 const char *name, struct obstack *obstack,
10744 struct dwarf2_cu *cu, long *value, int bits)
c906108c 10745{
e7c27a73 10746 struct objfile *objfile = cu->objfile;
e17a4113
UW
10747 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10748 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
10749 LONGEST l = DW_UNSND (attr);
10750
10751 if (bits < sizeof (*value) * 8)
10752 {
10753 l &= ((LONGEST) 1 << bits) - 1;
10754 *value = l;
10755 }
10756 else if (bits == sizeof (*value) * 8)
10757 *value = l;
10758 else
10759 {
10760 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10761 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10762 return bytes;
10763 }
10764
10765 return NULL;
10766}
10767
10768/* Read a constant value from an attribute. Either set *VALUE, or if
10769 the value does not fit in *VALUE, set *BYTES - either already
10770 allocated on the objfile obstack, or newly allocated on OBSTACK,
10771 or, set *BATON, if we translated the constant to a location
10772 expression. */
10773
10774static void
10775dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10776 const char *name, struct obstack *obstack,
10777 struct dwarf2_cu *cu,
10778 long *value, gdb_byte **bytes,
10779 struct dwarf2_locexpr_baton **baton)
10780{
10781 struct objfile *objfile = cu->objfile;
10782 struct comp_unit_head *cu_header = &cu->header;
c906108c 10783 struct dwarf_block *blk;
98bfdba5
PA
10784 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10785 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10786
10787 *value = 0;
10788 *bytes = NULL;
10789 *baton = NULL;
c906108c
SS
10790
10791 switch (attr->form)
10792 {
10793 case DW_FORM_addr:
ac56253d 10794 {
ac56253d
TT
10795 gdb_byte *data;
10796
98bfdba5
PA
10797 if (TYPE_LENGTH (type) != cu_header->addr_size)
10798 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 10799 cu_header->addr_size,
98bfdba5 10800 TYPE_LENGTH (type));
ac56253d
TT
10801 /* Symbols of this form are reasonably rare, so we just
10802 piggyback on the existing location code rather than writing
10803 a new implementation of symbol_computed_ops. */
98bfdba5
PA
10804 *baton = obstack_alloc (&objfile->objfile_obstack,
10805 sizeof (struct dwarf2_locexpr_baton));
10806 (*baton)->per_cu = cu->per_cu;
10807 gdb_assert ((*baton)->per_cu);
ac56253d 10808
98bfdba5
PA
10809 (*baton)->size = 2 + cu_header->addr_size;
10810 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
10811 (*baton)->data = data;
ac56253d
TT
10812
10813 data[0] = DW_OP_addr;
10814 store_unsigned_integer (&data[1], cu_header->addr_size,
10815 byte_order, DW_ADDR (attr));
10816 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 10817 }
c906108c 10818 break;
4ac36638 10819 case DW_FORM_string:
93b5768b 10820 case DW_FORM_strp:
98bfdba5
PA
10821 /* DW_STRING is already allocated on the objfile obstack, point
10822 directly to it. */
10823 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 10824 break;
c906108c
SS
10825 case DW_FORM_block1:
10826 case DW_FORM_block2:
10827 case DW_FORM_block4:
10828 case DW_FORM_block:
2dc7f7b3 10829 case DW_FORM_exprloc:
c906108c 10830 blk = DW_BLOCK (attr);
98bfdba5
PA
10831 if (TYPE_LENGTH (type) != blk->size)
10832 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
10833 TYPE_LENGTH (type));
10834 *bytes = blk->data;
c906108c 10835 break;
2df3850c
JM
10836
10837 /* The DW_AT_const_value attributes are supposed to carry the
10838 symbol's value "represented as it would be on the target
10839 architecture." By the time we get here, it's already been
10840 converted to host endianness, so we just need to sign- or
10841 zero-extend it as appropriate. */
10842 case DW_FORM_data1:
98bfdba5 10843 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
2df3850c 10844 break;
c906108c 10845 case DW_FORM_data2:
98bfdba5 10846 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
2df3850c 10847 break;
c906108c 10848 case DW_FORM_data4:
98bfdba5 10849 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
2df3850c 10850 break;
c906108c 10851 case DW_FORM_data8:
98bfdba5 10852 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
2df3850c
JM
10853 break;
10854
c906108c 10855 case DW_FORM_sdata:
98bfdba5 10856 *value = DW_SND (attr);
2df3850c
JM
10857 break;
10858
c906108c 10859 case DW_FORM_udata:
98bfdba5 10860 *value = DW_UNSND (attr);
c906108c 10861 break;
2df3850c 10862
c906108c 10863 default:
4d3c2250 10864 complaint (&symfile_complaints,
e2e0b3e5 10865 _("unsupported const value attribute form: '%s'"),
4d3c2250 10866 dwarf_form_name (attr->form));
98bfdba5 10867 *value = 0;
c906108c
SS
10868 break;
10869 }
10870}
10871
2df3850c 10872
98bfdba5
PA
10873/* Copy constant value from an attribute to a symbol. */
10874
2df3850c 10875static void
98bfdba5
PA
10876dwarf2_const_value (struct attribute *attr, struct symbol *sym,
10877 struct dwarf2_cu *cu)
2df3850c 10878{
98bfdba5
PA
10879 struct objfile *objfile = cu->objfile;
10880 struct comp_unit_head *cu_header = &cu->header;
10881 long value;
10882 gdb_byte *bytes;
10883 struct dwarf2_locexpr_baton *baton;
2df3850c 10884
98bfdba5
PA
10885 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
10886 SYMBOL_PRINT_NAME (sym),
10887 &objfile->objfile_obstack, cu,
10888 &value, &bytes, &baton);
2df3850c 10889
98bfdba5
PA
10890 if (baton != NULL)
10891 {
10892 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10893 SYMBOL_LOCATION_BATON (sym) = baton;
10894 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10895 }
10896 else if (bytes != NULL)
10897 {
10898 SYMBOL_VALUE_BYTES (sym) = bytes;
10899 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10900 }
10901 else
10902 {
10903 SYMBOL_VALUE (sym) = value;
10904 SYMBOL_CLASS (sym) = LOC_CONST;
10905 }
2df3850c
JM
10906}
10907
10908
c906108c
SS
10909/* Return the type of the die in question using its DW_AT_type attribute. */
10910
10911static struct type *
e7c27a73 10912die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10913{
c906108c
SS
10914 struct attribute *type_attr;
10915 struct die_info *type_die;
c906108c 10916
e142c38c 10917 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
10918 if (!type_attr)
10919 {
10920 /* A missing DW_AT_type represents a void type. */
46bf5051 10921 return objfile_type (cu->objfile)->builtin_void;
c906108c 10922 }
348e048f
DE
10923
10924 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10b3939b 10925
33ac96f0 10926 return tag_type_to_type (type_die, cu);
c906108c
SS
10927}
10928
b4ba55a1
JB
10929/* True iff CU's producer generates GNAT Ada auxiliary information
10930 that allows to find parallel types through that information instead
10931 of having to do expensive parallel lookups by type name. */
10932
10933static int
10934need_gnat_info (struct dwarf2_cu *cu)
10935{
10936 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
10937 of GNAT produces this auxiliary information, without any indication
10938 that it is produced. Part of enhancing the FSF version of GNAT
10939 to produce that information will be to put in place an indicator
10940 that we can use in order to determine whether the descriptive type
10941 info is available or not. One suggestion that has been made is
10942 to use a new attribute, attached to the CU die. For now, assume
10943 that the descriptive type info is not available. */
10944 return 0;
10945}
10946
10947
10948/* Return the auxiliary type of the die in question using its
10949 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
10950 attribute is not present. */
10951
10952static struct type *
10953die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
10954{
b4ba55a1
JB
10955 struct attribute *type_attr;
10956 struct die_info *type_die;
10957
10958 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
10959 if (!type_attr)
10960 return NULL;
10961
10962 type_die = follow_die_ref (die, type_attr, &cu);
33ac96f0 10963 return tag_type_to_type (type_die, cu);
b4ba55a1
JB
10964}
10965
10966/* If DIE has a descriptive_type attribute, then set the TYPE's
10967 descriptive type accordingly. */
10968
10969static void
10970set_descriptive_type (struct type *type, struct die_info *die,
10971 struct dwarf2_cu *cu)
10972{
10973 struct type *descriptive_type = die_descriptive_type (die, cu);
10974
10975 if (descriptive_type)
10976 {
10977 ALLOCATE_GNAT_AUX_TYPE (type);
10978 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
10979 }
10980}
10981
c906108c
SS
10982/* Return the containing type of the die in question using its
10983 DW_AT_containing_type attribute. */
10984
10985static struct type *
e7c27a73 10986die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10987{
c906108c 10988 struct attribute *type_attr;
33ac96f0 10989 struct die_info *type_die;
c906108c 10990
e142c38c 10991 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
10992 if (!type_attr)
10993 error (_("Dwarf Error: Problem turning containing type into gdb type "
10994 "[in module %s]"), cu->objfile->name);
10995
10996 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10997 return tag_type_to_type (type_die, cu);
c906108c
SS
10998}
10999
c906108c 11000static struct type *
e7c27a73 11001tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11002{
f792889a
DJ
11003 struct type *this_type;
11004
11005 this_type = read_type_die (die, cu);
11006 if (!this_type)
c906108c 11007 {
b00fdb78
TT
11008 char *message, *saved;
11009
11010 /* read_type_die already issued a complaint. */
11011 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11012 cu->objfile->name,
11013 cu->header.offset,
11014 die->offset);
11015 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11016 message, strlen (message));
11017 xfree (message);
11018
11019 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11020 }
f792889a 11021 return this_type;
c906108c
SS
11022}
11023
f792889a 11024static struct type *
e7c27a73 11025read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11026{
f792889a
DJ
11027 struct type *this_type;
11028
11029 this_type = get_die_type (die, cu);
11030 if (this_type)
11031 return this_type;
11032
c906108c
SS
11033 switch (die->tag)
11034 {
11035 case DW_TAG_class_type:
680b30c7 11036 case DW_TAG_interface_type:
c906108c
SS
11037 case DW_TAG_structure_type:
11038 case DW_TAG_union_type:
f792889a 11039 this_type = read_structure_type (die, cu);
c906108c
SS
11040 break;
11041 case DW_TAG_enumeration_type:
f792889a 11042 this_type = read_enumeration_type (die, cu);
c906108c
SS
11043 break;
11044 case DW_TAG_subprogram:
11045 case DW_TAG_subroutine_type:
edb3359d 11046 case DW_TAG_inlined_subroutine:
f792889a 11047 this_type = read_subroutine_type (die, cu);
c906108c
SS
11048 break;
11049 case DW_TAG_array_type:
f792889a 11050 this_type = read_array_type (die, cu);
c906108c 11051 break;
72019c9c 11052 case DW_TAG_set_type:
f792889a 11053 this_type = read_set_type (die, cu);
72019c9c 11054 break;
c906108c 11055 case DW_TAG_pointer_type:
f792889a 11056 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11057 break;
11058 case DW_TAG_ptr_to_member_type:
f792889a 11059 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11060 break;
11061 case DW_TAG_reference_type:
f792889a 11062 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11063 break;
11064 case DW_TAG_const_type:
f792889a 11065 this_type = read_tag_const_type (die, cu);
c906108c
SS
11066 break;
11067 case DW_TAG_volatile_type:
f792889a 11068 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11069 break;
11070 case DW_TAG_string_type:
f792889a 11071 this_type = read_tag_string_type (die, cu);
c906108c
SS
11072 break;
11073 case DW_TAG_typedef:
f792889a 11074 this_type = read_typedef (die, cu);
c906108c 11075 break;
a02abb62 11076 case DW_TAG_subrange_type:
f792889a 11077 this_type = read_subrange_type (die, cu);
a02abb62 11078 break;
c906108c 11079 case DW_TAG_base_type:
f792889a 11080 this_type = read_base_type (die, cu);
c906108c 11081 break;
81a17f79 11082 case DW_TAG_unspecified_type:
f792889a 11083 this_type = read_unspecified_type (die, cu);
81a17f79 11084 break;
0114d602
DJ
11085 case DW_TAG_namespace:
11086 this_type = read_namespace_type (die, cu);
11087 break;
f55ee35c
JK
11088 case DW_TAG_module:
11089 this_type = read_module_type (die, cu);
11090 break;
c906108c 11091 default:
a1f5b845 11092 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11093 dwarf_tag_name (die->tag));
c906108c
SS
11094 break;
11095 }
63d06c5c 11096
f792889a 11097 return this_type;
63d06c5c
DC
11098}
11099
fdde2d81 11100/* Return the name of the namespace/class that DIE is defined within,
0114d602 11101 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11102
0114d602
DJ
11103 For example, if we're within the method foo() in the following
11104 code:
11105
11106 namespace N {
11107 class C {
11108 void foo () {
11109 }
11110 };
11111 }
11112
11113 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11114
11115static char *
e142c38c 11116determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11117{
0114d602
DJ
11118 struct die_info *parent, *spec_die;
11119 struct dwarf2_cu *spec_cu;
11120 struct type *parent_type;
63d06c5c 11121
f55ee35c
JK
11122 if (cu->language != language_cplus && cu->language != language_java
11123 && cu->language != language_fortran)
0114d602
DJ
11124 return "";
11125
11126 /* We have to be careful in the presence of DW_AT_specification.
11127 For example, with GCC 3.4, given the code
11128
11129 namespace N {
11130 void foo() {
11131 // Definition of N::foo.
11132 }
11133 }
11134
11135 then we'll have a tree of DIEs like this:
11136
11137 1: DW_TAG_compile_unit
11138 2: DW_TAG_namespace // N
11139 3: DW_TAG_subprogram // declaration of N::foo
11140 4: DW_TAG_subprogram // definition of N::foo
11141 DW_AT_specification // refers to die #3
11142
11143 Thus, when processing die #4, we have to pretend that we're in
11144 the context of its DW_AT_specification, namely the contex of die
11145 #3. */
11146 spec_cu = cu;
11147 spec_die = die_specification (die, &spec_cu);
11148 if (spec_die == NULL)
11149 parent = die->parent;
11150 else
63d06c5c 11151 {
0114d602
DJ
11152 parent = spec_die->parent;
11153 cu = spec_cu;
63d06c5c 11154 }
0114d602
DJ
11155
11156 if (parent == NULL)
11157 return "";
98bfdba5
PA
11158 else if (parent->building_fullname)
11159 {
11160 const char *name;
11161 const char *parent_name;
11162
11163 /* It has been seen on RealView 2.2 built binaries,
11164 DW_TAG_template_type_param types actually _defined_ as
11165 children of the parent class:
11166
11167 enum E {};
11168 template class <class Enum> Class{};
11169 Class<enum E> class_e;
11170
11171 1: DW_TAG_class_type (Class)
11172 2: DW_TAG_enumeration_type (E)
11173 3: DW_TAG_enumerator (enum1:0)
11174 3: DW_TAG_enumerator (enum2:1)
11175 ...
11176 2: DW_TAG_template_type_param
11177 DW_AT_type DW_FORM_ref_udata (E)
11178
11179 Besides being broken debug info, it can put GDB into an
11180 infinite loop. Consider:
11181
11182 When we're building the full name for Class<E>, we'll start
11183 at Class, and go look over its template type parameters,
11184 finding E. We'll then try to build the full name of E, and
11185 reach here. We're now trying to build the full name of E,
11186 and look over the parent DIE for containing scope. In the
11187 broken case, if we followed the parent DIE of E, we'd again
11188 find Class, and once again go look at its template type
11189 arguments, etc., etc. Simply don't consider such parent die
11190 as source-level parent of this die (it can't be, the language
11191 doesn't allow it), and break the loop here. */
11192 name = dwarf2_name (die, cu);
11193 parent_name = dwarf2_name (parent, cu);
11194 complaint (&symfile_complaints,
11195 _("template param type '%s' defined within parent '%s'"),
11196 name ? name : "<unknown>",
11197 parent_name ? parent_name : "<unknown>");
11198 return "";
11199 }
63d06c5c 11200 else
0114d602
DJ
11201 switch (parent->tag)
11202 {
63d06c5c 11203 case DW_TAG_namespace:
0114d602 11204 parent_type = read_type_die (parent, cu);
acebe513
UW
11205 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11206 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11207 Work around this problem here. */
11208 if (cu->language == language_cplus
11209 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11210 return "";
0114d602
DJ
11211 /* We give a name to even anonymous namespaces. */
11212 return TYPE_TAG_NAME (parent_type);
63d06c5c 11213 case DW_TAG_class_type:
680b30c7 11214 case DW_TAG_interface_type:
63d06c5c 11215 case DW_TAG_structure_type:
0114d602 11216 case DW_TAG_union_type:
f55ee35c 11217 case DW_TAG_module:
0114d602
DJ
11218 parent_type = read_type_die (parent, cu);
11219 if (TYPE_TAG_NAME (parent_type) != NULL)
11220 return TYPE_TAG_NAME (parent_type);
11221 else
11222 /* An anonymous structure is only allowed non-static data
11223 members; no typedefs, no member functions, et cetera.
11224 So it does not need a prefix. */
11225 return "";
63d06c5c 11226 default:
8176b9b8 11227 return determine_prefix (parent, cu);
63d06c5c 11228 }
63d06c5c
DC
11229}
11230
987504bb
JJ
11231/* Return a newly-allocated string formed by concatenating PREFIX and
11232 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11233 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11234 perform an obconcat, otherwise allocate storage for the result. The CU argument
11235 is used to determine the language and hence, the appropriate separator. */
11236
f55ee35c 11237#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11238
11239static char *
f55ee35c
JK
11240typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11241 int physname, struct dwarf2_cu *cu)
63d06c5c 11242{
f55ee35c 11243 const char *lead = "";
5c315b68 11244 const char *sep;
63d06c5c 11245
987504bb
JJ
11246 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11247 sep = "";
11248 else if (cu->language == language_java)
11249 sep = ".";
f55ee35c
JK
11250 else if (cu->language == language_fortran && physname)
11251 {
11252 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11253 DW_AT_MIPS_linkage_name is preferred and used instead. */
11254
11255 lead = "__";
11256 sep = "_MOD_";
11257 }
987504bb
JJ
11258 else
11259 sep = "::";
63d06c5c 11260
6dd47d34
DE
11261 if (prefix == NULL)
11262 prefix = "";
11263 if (suffix == NULL)
11264 suffix = "";
11265
987504bb
JJ
11266 if (obs == NULL)
11267 {
11268 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11269
f55ee35c
JK
11270 strcpy (retval, lead);
11271 strcat (retval, prefix);
6dd47d34
DE
11272 strcat (retval, sep);
11273 strcat (retval, suffix);
63d06c5c
DC
11274 return retval;
11275 }
987504bb
JJ
11276 else
11277 {
11278 /* We have an obstack. */
f55ee35c 11279 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11280 }
63d06c5c
DC
11281}
11282
c906108c
SS
11283/* Return sibling of die, NULL if no sibling. */
11284
f9aca02d 11285static struct die_info *
fba45db2 11286sibling_die (struct die_info *die)
c906108c 11287{
639d11d3 11288 return die->sibling;
c906108c
SS
11289}
11290
71c25dea
TT
11291/* Get name of a die, return NULL if not found. */
11292
11293static char *
11294dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11295 struct obstack *obstack)
11296{
11297 if (name && cu->language == language_cplus)
11298 {
11299 char *canon_name = cp_canonicalize_string (name);
11300
11301 if (canon_name != NULL)
11302 {
11303 if (strcmp (canon_name, name) != 0)
11304 name = obsavestring (canon_name, strlen (canon_name),
11305 obstack);
11306 xfree (canon_name);
11307 }
11308 }
11309
11310 return name;
c906108c
SS
11311}
11312
9219021c
DC
11313/* Get name of a die, return NULL if not found. */
11314
11315static char *
e142c38c 11316dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11317{
11318 struct attribute *attr;
11319
e142c38c 11320 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11321 if (!attr || !DW_STRING (attr))
11322 return NULL;
11323
11324 switch (die->tag)
11325 {
11326 case DW_TAG_compile_unit:
11327 /* Compilation units have a DW_AT_name that is a filename, not
11328 a source language identifier. */
11329 case DW_TAG_enumeration_type:
11330 case DW_TAG_enumerator:
11331 /* These tags always have simple identifiers already; no need
11332 to canonicalize them. */
11333 return DW_STRING (attr);
907af001 11334
418835cc
KS
11335 case DW_TAG_subprogram:
11336 /* Java constructors will all be named "<init>", so return
11337 the class name when we see this special case. */
11338 if (cu->language == language_java
11339 && DW_STRING (attr) != NULL
11340 && strcmp (DW_STRING (attr), "<init>") == 0)
11341 {
11342 struct dwarf2_cu *spec_cu = cu;
11343 struct die_info *spec_die;
11344
11345 /* GCJ will output '<init>' for Java constructor names.
11346 For this special case, return the name of the parent class. */
11347
11348 /* GCJ may output suprogram DIEs with AT_specification set.
11349 If so, use the name of the specified DIE. */
11350 spec_die = die_specification (die, &spec_cu);
11351 if (spec_die != NULL)
11352 return dwarf2_name (spec_die, spec_cu);
11353
11354 do
11355 {
11356 die = die->parent;
11357 if (die->tag == DW_TAG_class_type)
11358 return dwarf2_name (die, cu);
11359 }
11360 while (die->tag != DW_TAG_compile_unit);
11361 }
907af001
UW
11362 break;
11363
11364 case DW_TAG_class_type:
11365 case DW_TAG_interface_type:
11366 case DW_TAG_structure_type:
11367 case DW_TAG_union_type:
11368 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11369 structures or unions. These were of the form "._%d" in GCC 4.1,
11370 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11371 and GCC 4.4. We work around this problem by ignoring these. */
11372 if (strncmp (DW_STRING (attr), "._", 2) == 0
11373 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11374 return NULL;
11375 break;
11376
71c25dea 11377 default:
907af001
UW
11378 break;
11379 }
11380
11381 if (!DW_STRING_IS_CANONICAL (attr))
11382 {
11383 DW_STRING (attr)
11384 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11385 &cu->objfile->objfile_obstack);
11386 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11387 }
907af001 11388 return DW_STRING (attr);
9219021c
DC
11389}
11390
11391/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11392 is none. *EXT_CU is the CU containing DIE on input, and the CU
11393 containing the return value on output. */
9219021c
DC
11394
11395static struct die_info *
f2f0e013 11396dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11397{
11398 struct attribute *attr;
9219021c 11399
f2f0e013 11400 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11401 if (attr == NULL)
11402 return NULL;
11403
f2f0e013 11404 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11405}
11406
c906108c
SS
11407/* Convert a DIE tag into its string name. */
11408
11409static char *
aa1ee363 11410dwarf_tag_name (unsigned tag)
c906108c
SS
11411{
11412 switch (tag)
11413 {
11414 case DW_TAG_padding:
11415 return "DW_TAG_padding";
11416 case DW_TAG_array_type:
11417 return "DW_TAG_array_type";
11418 case DW_TAG_class_type:
11419 return "DW_TAG_class_type";
11420 case DW_TAG_entry_point:
11421 return "DW_TAG_entry_point";
11422 case DW_TAG_enumeration_type:
11423 return "DW_TAG_enumeration_type";
11424 case DW_TAG_formal_parameter:
11425 return "DW_TAG_formal_parameter";
11426 case DW_TAG_imported_declaration:
11427 return "DW_TAG_imported_declaration";
11428 case DW_TAG_label:
11429 return "DW_TAG_label";
11430 case DW_TAG_lexical_block:
11431 return "DW_TAG_lexical_block";
11432 case DW_TAG_member:
11433 return "DW_TAG_member";
11434 case DW_TAG_pointer_type:
11435 return "DW_TAG_pointer_type";
11436 case DW_TAG_reference_type:
11437 return "DW_TAG_reference_type";
11438 case DW_TAG_compile_unit:
11439 return "DW_TAG_compile_unit";
11440 case DW_TAG_string_type:
11441 return "DW_TAG_string_type";
11442 case DW_TAG_structure_type:
11443 return "DW_TAG_structure_type";
11444 case DW_TAG_subroutine_type:
11445 return "DW_TAG_subroutine_type";
11446 case DW_TAG_typedef:
11447 return "DW_TAG_typedef";
11448 case DW_TAG_union_type:
11449 return "DW_TAG_union_type";
11450 case DW_TAG_unspecified_parameters:
11451 return "DW_TAG_unspecified_parameters";
11452 case DW_TAG_variant:
11453 return "DW_TAG_variant";
11454 case DW_TAG_common_block:
11455 return "DW_TAG_common_block";
11456 case DW_TAG_common_inclusion:
11457 return "DW_TAG_common_inclusion";
11458 case DW_TAG_inheritance:
11459 return "DW_TAG_inheritance";
11460 case DW_TAG_inlined_subroutine:
11461 return "DW_TAG_inlined_subroutine";
11462 case DW_TAG_module:
11463 return "DW_TAG_module";
11464 case DW_TAG_ptr_to_member_type:
11465 return "DW_TAG_ptr_to_member_type";
11466 case DW_TAG_set_type:
11467 return "DW_TAG_set_type";
11468 case DW_TAG_subrange_type:
11469 return "DW_TAG_subrange_type";
11470 case DW_TAG_with_stmt:
11471 return "DW_TAG_with_stmt";
11472 case DW_TAG_access_declaration:
11473 return "DW_TAG_access_declaration";
11474 case DW_TAG_base_type:
11475 return "DW_TAG_base_type";
11476 case DW_TAG_catch_block:
11477 return "DW_TAG_catch_block";
11478 case DW_TAG_const_type:
11479 return "DW_TAG_const_type";
11480 case DW_TAG_constant:
11481 return "DW_TAG_constant";
11482 case DW_TAG_enumerator:
11483 return "DW_TAG_enumerator";
11484 case DW_TAG_file_type:
11485 return "DW_TAG_file_type";
11486 case DW_TAG_friend:
11487 return "DW_TAG_friend";
11488 case DW_TAG_namelist:
11489 return "DW_TAG_namelist";
11490 case DW_TAG_namelist_item:
11491 return "DW_TAG_namelist_item";
11492 case DW_TAG_packed_type:
11493 return "DW_TAG_packed_type";
11494 case DW_TAG_subprogram:
11495 return "DW_TAG_subprogram";
11496 case DW_TAG_template_type_param:
11497 return "DW_TAG_template_type_param";
11498 case DW_TAG_template_value_param:
11499 return "DW_TAG_template_value_param";
11500 case DW_TAG_thrown_type:
11501 return "DW_TAG_thrown_type";
11502 case DW_TAG_try_block:
11503 return "DW_TAG_try_block";
11504 case DW_TAG_variant_part:
11505 return "DW_TAG_variant_part";
11506 case DW_TAG_variable:
11507 return "DW_TAG_variable";
11508 case DW_TAG_volatile_type:
11509 return "DW_TAG_volatile_type";
d9fa45fe
DC
11510 case DW_TAG_dwarf_procedure:
11511 return "DW_TAG_dwarf_procedure";
11512 case DW_TAG_restrict_type:
11513 return "DW_TAG_restrict_type";
11514 case DW_TAG_interface_type:
11515 return "DW_TAG_interface_type";
11516 case DW_TAG_namespace:
11517 return "DW_TAG_namespace";
11518 case DW_TAG_imported_module:
11519 return "DW_TAG_imported_module";
11520 case DW_TAG_unspecified_type:
11521 return "DW_TAG_unspecified_type";
11522 case DW_TAG_partial_unit:
11523 return "DW_TAG_partial_unit";
11524 case DW_TAG_imported_unit:
11525 return "DW_TAG_imported_unit";
b7619582
GF
11526 case DW_TAG_condition:
11527 return "DW_TAG_condition";
11528 case DW_TAG_shared_type:
11529 return "DW_TAG_shared_type";
348e048f
DE
11530 case DW_TAG_type_unit:
11531 return "DW_TAG_type_unit";
c906108c
SS
11532 case DW_TAG_MIPS_loop:
11533 return "DW_TAG_MIPS_loop";
b7619582
GF
11534 case DW_TAG_HP_array_descriptor:
11535 return "DW_TAG_HP_array_descriptor";
c906108c
SS
11536 case DW_TAG_format_label:
11537 return "DW_TAG_format_label";
11538 case DW_TAG_function_template:
11539 return "DW_TAG_function_template";
11540 case DW_TAG_class_template:
11541 return "DW_TAG_class_template";
b7619582
GF
11542 case DW_TAG_GNU_BINCL:
11543 return "DW_TAG_GNU_BINCL";
11544 case DW_TAG_GNU_EINCL:
11545 return "DW_TAG_GNU_EINCL";
11546 case DW_TAG_upc_shared_type:
11547 return "DW_TAG_upc_shared_type";
11548 case DW_TAG_upc_strict_type:
11549 return "DW_TAG_upc_strict_type";
11550 case DW_TAG_upc_relaxed_type:
11551 return "DW_TAG_upc_relaxed_type";
11552 case DW_TAG_PGI_kanji_type:
11553 return "DW_TAG_PGI_kanji_type";
11554 case DW_TAG_PGI_interface_block:
11555 return "DW_TAG_PGI_interface_block";
c906108c
SS
11556 default:
11557 return "DW_TAG_<unknown>";
11558 }
11559}
11560
11561/* Convert a DWARF attribute code into its string name. */
11562
11563static char *
aa1ee363 11564dwarf_attr_name (unsigned attr)
c906108c
SS
11565{
11566 switch (attr)
11567 {
11568 case DW_AT_sibling:
11569 return "DW_AT_sibling";
11570 case DW_AT_location:
11571 return "DW_AT_location";
11572 case DW_AT_name:
11573 return "DW_AT_name";
11574 case DW_AT_ordering:
11575 return "DW_AT_ordering";
11576 case DW_AT_subscr_data:
11577 return "DW_AT_subscr_data";
11578 case DW_AT_byte_size:
11579 return "DW_AT_byte_size";
11580 case DW_AT_bit_offset:
11581 return "DW_AT_bit_offset";
11582 case DW_AT_bit_size:
11583 return "DW_AT_bit_size";
11584 case DW_AT_element_list:
11585 return "DW_AT_element_list";
11586 case DW_AT_stmt_list:
11587 return "DW_AT_stmt_list";
11588 case DW_AT_low_pc:
11589 return "DW_AT_low_pc";
11590 case DW_AT_high_pc:
11591 return "DW_AT_high_pc";
11592 case DW_AT_language:
11593 return "DW_AT_language";
11594 case DW_AT_member:
11595 return "DW_AT_member";
11596 case DW_AT_discr:
11597 return "DW_AT_discr";
11598 case DW_AT_discr_value:
11599 return "DW_AT_discr_value";
11600 case DW_AT_visibility:
11601 return "DW_AT_visibility";
11602 case DW_AT_import:
11603 return "DW_AT_import";
11604 case DW_AT_string_length:
11605 return "DW_AT_string_length";
11606 case DW_AT_common_reference:
11607 return "DW_AT_common_reference";
11608 case DW_AT_comp_dir:
11609 return "DW_AT_comp_dir";
11610 case DW_AT_const_value:
11611 return "DW_AT_const_value";
11612 case DW_AT_containing_type:
11613 return "DW_AT_containing_type";
11614 case DW_AT_default_value:
11615 return "DW_AT_default_value";
11616 case DW_AT_inline:
11617 return "DW_AT_inline";
11618 case DW_AT_is_optional:
11619 return "DW_AT_is_optional";
11620 case DW_AT_lower_bound:
11621 return "DW_AT_lower_bound";
11622 case DW_AT_producer:
11623 return "DW_AT_producer";
11624 case DW_AT_prototyped:
11625 return "DW_AT_prototyped";
11626 case DW_AT_return_addr:
11627 return "DW_AT_return_addr";
11628 case DW_AT_start_scope:
11629 return "DW_AT_start_scope";
09fa0d7c
JK
11630 case DW_AT_bit_stride:
11631 return "DW_AT_bit_stride";
c906108c
SS
11632 case DW_AT_upper_bound:
11633 return "DW_AT_upper_bound";
11634 case DW_AT_abstract_origin:
11635 return "DW_AT_abstract_origin";
11636 case DW_AT_accessibility:
11637 return "DW_AT_accessibility";
11638 case DW_AT_address_class:
11639 return "DW_AT_address_class";
11640 case DW_AT_artificial:
11641 return "DW_AT_artificial";
11642 case DW_AT_base_types:
11643 return "DW_AT_base_types";
11644 case DW_AT_calling_convention:
11645 return "DW_AT_calling_convention";
11646 case DW_AT_count:
11647 return "DW_AT_count";
11648 case DW_AT_data_member_location:
11649 return "DW_AT_data_member_location";
11650 case DW_AT_decl_column:
11651 return "DW_AT_decl_column";
11652 case DW_AT_decl_file:
11653 return "DW_AT_decl_file";
11654 case DW_AT_decl_line:
11655 return "DW_AT_decl_line";
11656 case DW_AT_declaration:
11657 return "DW_AT_declaration";
11658 case DW_AT_discr_list:
11659 return "DW_AT_discr_list";
11660 case DW_AT_encoding:
11661 return "DW_AT_encoding";
11662 case DW_AT_external:
11663 return "DW_AT_external";
11664 case DW_AT_frame_base:
11665 return "DW_AT_frame_base";
11666 case DW_AT_friend:
11667 return "DW_AT_friend";
11668 case DW_AT_identifier_case:
11669 return "DW_AT_identifier_case";
11670 case DW_AT_macro_info:
11671 return "DW_AT_macro_info";
11672 case DW_AT_namelist_items:
11673 return "DW_AT_namelist_items";
11674 case DW_AT_priority:
11675 return "DW_AT_priority";
11676 case DW_AT_segment:
11677 return "DW_AT_segment";
11678 case DW_AT_specification:
11679 return "DW_AT_specification";
11680 case DW_AT_static_link:
11681 return "DW_AT_static_link";
11682 case DW_AT_type:
11683 return "DW_AT_type";
11684 case DW_AT_use_location:
11685 return "DW_AT_use_location";
11686 case DW_AT_variable_parameter:
11687 return "DW_AT_variable_parameter";
11688 case DW_AT_virtuality:
11689 return "DW_AT_virtuality";
11690 case DW_AT_vtable_elem_location:
11691 return "DW_AT_vtable_elem_location";
b7619582 11692 /* DWARF 3 values. */
d9fa45fe
DC
11693 case DW_AT_allocated:
11694 return "DW_AT_allocated";
11695 case DW_AT_associated:
11696 return "DW_AT_associated";
11697 case DW_AT_data_location:
11698 return "DW_AT_data_location";
09fa0d7c
JK
11699 case DW_AT_byte_stride:
11700 return "DW_AT_byte_stride";
d9fa45fe
DC
11701 case DW_AT_entry_pc:
11702 return "DW_AT_entry_pc";
11703 case DW_AT_use_UTF8:
11704 return "DW_AT_use_UTF8";
11705 case DW_AT_extension:
11706 return "DW_AT_extension";
11707 case DW_AT_ranges:
11708 return "DW_AT_ranges";
11709 case DW_AT_trampoline:
11710 return "DW_AT_trampoline";
11711 case DW_AT_call_column:
11712 return "DW_AT_call_column";
11713 case DW_AT_call_file:
11714 return "DW_AT_call_file";
11715 case DW_AT_call_line:
11716 return "DW_AT_call_line";
b7619582
GF
11717 case DW_AT_description:
11718 return "DW_AT_description";
11719 case DW_AT_binary_scale:
11720 return "DW_AT_binary_scale";
11721 case DW_AT_decimal_scale:
11722 return "DW_AT_decimal_scale";
11723 case DW_AT_small:
11724 return "DW_AT_small";
11725 case DW_AT_decimal_sign:
11726 return "DW_AT_decimal_sign";
11727 case DW_AT_digit_count:
11728 return "DW_AT_digit_count";
11729 case DW_AT_picture_string:
11730 return "DW_AT_picture_string";
11731 case DW_AT_mutable:
11732 return "DW_AT_mutable";
11733 case DW_AT_threads_scaled:
11734 return "DW_AT_threads_scaled";
11735 case DW_AT_explicit:
11736 return "DW_AT_explicit";
11737 case DW_AT_object_pointer:
11738 return "DW_AT_object_pointer";
11739 case DW_AT_endianity:
11740 return "DW_AT_endianity";
11741 case DW_AT_elemental:
11742 return "DW_AT_elemental";
11743 case DW_AT_pure:
11744 return "DW_AT_pure";
11745 case DW_AT_recursive:
11746 return "DW_AT_recursive";
348e048f
DE
11747 /* DWARF 4 values. */
11748 case DW_AT_signature:
11749 return "DW_AT_signature";
31ef98ae
TT
11750 case DW_AT_linkage_name:
11751 return "DW_AT_linkage_name";
b7619582 11752 /* SGI/MIPS extensions. */
c764a876 11753#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
11754 case DW_AT_MIPS_fde:
11755 return "DW_AT_MIPS_fde";
c764a876 11756#endif
c906108c
SS
11757 case DW_AT_MIPS_loop_begin:
11758 return "DW_AT_MIPS_loop_begin";
11759 case DW_AT_MIPS_tail_loop_begin:
11760 return "DW_AT_MIPS_tail_loop_begin";
11761 case DW_AT_MIPS_epilog_begin:
11762 return "DW_AT_MIPS_epilog_begin";
11763 case DW_AT_MIPS_loop_unroll_factor:
11764 return "DW_AT_MIPS_loop_unroll_factor";
11765 case DW_AT_MIPS_software_pipeline_depth:
11766 return "DW_AT_MIPS_software_pipeline_depth";
11767 case DW_AT_MIPS_linkage_name:
11768 return "DW_AT_MIPS_linkage_name";
b7619582
GF
11769 case DW_AT_MIPS_stride:
11770 return "DW_AT_MIPS_stride";
11771 case DW_AT_MIPS_abstract_name:
11772 return "DW_AT_MIPS_abstract_name";
11773 case DW_AT_MIPS_clone_origin:
11774 return "DW_AT_MIPS_clone_origin";
11775 case DW_AT_MIPS_has_inlines:
11776 return "DW_AT_MIPS_has_inlines";
b7619582 11777 /* HP extensions. */
c764a876 11778#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
11779 case DW_AT_HP_block_index:
11780 return "DW_AT_HP_block_index";
c764a876 11781#endif
b7619582
GF
11782 case DW_AT_HP_unmodifiable:
11783 return "DW_AT_HP_unmodifiable";
11784 case DW_AT_HP_actuals_stmt_list:
11785 return "DW_AT_HP_actuals_stmt_list";
11786 case DW_AT_HP_proc_per_section:
11787 return "DW_AT_HP_proc_per_section";
11788 case DW_AT_HP_raw_data_ptr:
11789 return "DW_AT_HP_raw_data_ptr";
11790 case DW_AT_HP_pass_by_reference:
11791 return "DW_AT_HP_pass_by_reference";
11792 case DW_AT_HP_opt_level:
11793 return "DW_AT_HP_opt_level";
11794 case DW_AT_HP_prof_version_id:
11795 return "DW_AT_HP_prof_version_id";
11796 case DW_AT_HP_opt_flags:
11797 return "DW_AT_HP_opt_flags";
11798 case DW_AT_HP_cold_region_low_pc:
11799 return "DW_AT_HP_cold_region_low_pc";
11800 case DW_AT_HP_cold_region_high_pc:
11801 return "DW_AT_HP_cold_region_high_pc";
11802 case DW_AT_HP_all_variables_modifiable:
11803 return "DW_AT_HP_all_variables_modifiable";
11804 case DW_AT_HP_linkage_name:
11805 return "DW_AT_HP_linkage_name";
11806 case DW_AT_HP_prof_flags:
11807 return "DW_AT_HP_prof_flags";
11808 /* GNU extensions. */
c906108c
SS
11809 case DW_AT_sf_names:
11810 return "DW_AT_sf_names";
11811 case DW_AT_src_info:
11812 return "DW_AT_src_info";
11813 case DW_AT_mac_info:
11814 return "DW_AT_mac_info";
11815 case DW_AT_src_coords:
11816 return "DW_AT_src_coords";
11817 case DW_AT_body_begin:
11818 return "DW_AT_body_begin";
11819 case DW_AT_body_end:
11820 return "DW_AT_body_end";
f5f8a009
EZ
11821 case DW_AT_GNU_vector:
11822 return "DW_AT_GNU_vector";
2de00c64
DE
11823 case DW_AT_GNU_odr_signature:
11824 return "DW_AT_GNU_odr_signature";
b7619582
GF
11825 /* VMS extensions. */
11826 case DW_AT_VMS_rtnbeg_pd_address:
11827 return "DW_AT_VMS_rtnbeg_pd_address";
11828 /* UPC extension. */
11829 case DW_AT_upc_threads_scaled:
11830 return "DW_AT_upc_threads_scaled";
11831 /* PGI (STMicroelectronics) extensions. */
11832 case DW_AT_PGI_lbase:
11833 return "DW_AT_PGI_lbase";
11834 case DW_AT_PGI_soffset:
11835 return "DW_AT_PGI_soffset";
11836 case DW_AT_PGI_lstride:
11837 return "DW_AT_PGI_lstride";
c906108c
SS
11838 default:
11839 return "DW_AT_<unknown>";
11840 }
11841}
11842
11843/* Convert a DWARF value form code into its string name. */
11844
11845static char *
aa1ee363 11846dwarf_form_name (unsigned form)
c906108c
SS
11847{
11848 switch (form)
11849 {
11850 case DW_FORM_addr:
11851 return "DW_FORM_addr";
11852 case DW_FORM_block2:
11853 return "DW_FORM_block2";
11854 case DW_FORM_block4:
11855 return "DW_FORM_block4";
11856 case DW_FORM_data2:
11857 return "DW_FORM_data2";
11858 case DW_FORM_data4:
11859 return "DW_FORM_data4";
11860 case DW_FORM_data8:
11861 return "DW_FORM_data8";
11862 case DW_FORM_string:
11863 return "DW_FORM_string";
11864 case DW_FORM_block:
11865 return "DW_FORM_block";
11866 case DW_FORM_block1:
11867 return "DW_FORM_block1";
11868 case DW_FORM_data1:
11869 return "DW_FORM_data1";
11870 case DW_FORM_flag:
11871 return "DW_FORM_flag";
11872 case DW_FORM_sdata:
11873 return "DW_FORM_sdata";
11874 case DW_FORM_strp:
11875 return "DW_FORM_strp";
11876 case DW_FORM_udata:
11877 return "DW_FORM_udata";
11878 case DW_FORM_ref_addr:
11879 return "DW_FORM_ref_addr";
11880 case DW_FORM_ref1:
11881 return "DW_FORM_ref1";
11882 case DW_FORM_ref2:
11883 return "DW_FORM_ref2";
11884 case DW_FORM_ref4:
11885 return "DW_FORM_ref4";
11886 case DW_FORM_ref8:
11887 return "DW_FORM_ref8";
11888 case DW_FORM_ref_udata:
11889 return "DW_FORM_ref_udata";
11890 case DW_FORM_indirect:
11891 return "DW_FORM_indirect";
348e048f
DE
11892 case DW_FORM_sec_offset:
11893 return "DW_FORM_sec_offset";
11894 case DW_FORM_exprloc:
11895 return "DW_FORM_exprloc";
11896 case DW_FORM_flag_present:
11897 return "DW_FORM_flag_present";
11898 case DW_FORM_sig8:
11899 return "DW_FORM_sig8";
c906108c
SS
11900 default:
11901 return "DW_FORM_<unknown>";
11902 }
11903}
11904
11905/* Convert a DWARF stack opcode into its string name. */
11906
9eae7c52
TT
11907const char *
11908dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
11909{
11910 switch (op)
11911 {
11912 case DW_OP_addr:
11913 return "DW_OP_addr";
11914 case DW_OP_deref:
11915 return "DW_OP_deref";
11916 case DW_OP_const1u:
11917 return "DW_OP_const1u";
11918 case DW_OP_const1s:
11919 return "DW_OP_const1s";
11920 case DW_OP_const2u:
11921 return "DW_OP_const2u";
11922 case DW_OP_const2s:
11923 return "DW_OP_const2s";
11924 case DW_OP_const4u:
11925 return "DW_OP_const4u";
11926 case DW_OP_const4s:
11927 return "DW_OP_const4s";
11928 case DW_OP_const8u:
11929 return "DW_OP_const8u";
11930 case DW_OP_const8s:
11931 return "DW_OP_const8s";
11932 case DW_OP_constu:
11933 return "DW_OP_constu";
11934 case DW_OP_consts:
11935 return "DW_OP_consts";
11936 case DW_OP_dup:
11937 return "DW_OP_dup";
11938 case DW_OP_drop:
11939 return "DW_OP_drop";
11940 case DW_OP_over:
11941 return "DW_OP_over";
11942 case DW_OP_pick:
11943 return "DW_OP_pick";
11944 case DW_OP_swap:
11945 return "DW_OP_swap";
11946 case DW_OP_rot:
11947 return "DW_OP_rot";
11948 case DW_OP_xderef:
11949 return "DW_OP_xderef";
11950 case DW_OP_abs:
11951 return "DW_OP_abs";
11952 case DW_OP_and:
11953 return "DW_OP_and";
11954 case DW_OP_div:
11955 return "DW_OP_div";
11956 case DW_OP_minus:
11957 return "DW_OP_minus";
11958 case DW_OP_mod:
11959 return "DW_OP_mod";
11960 case DW_OP_mul:
11961 return "DW_OP_mul";
11962 case DW_OP_neg:
11963 return "DW_OP_neg";
11964 case DW_OP_not:
11965 return "DW_OP_not";
11966 case DW_OP_or:
11967 return "DW_OP_or";
11968 case DW_OP_plus:
11969 return "DW_OP_plus";
11970 case DW_OP_plus_uconst:
11971 return "DW_OP_plus_uconst";
11972 case DW_OP_shl:
11973 return "DW_OP_shl";
11974 case DW_OP_shr:
11975 return "DW_OP_shr";
11976 case DW_OP_shra:
11977 return "DW_OP_shra";
11978 case DW_OP_xor:
11979 return "DW_OP_xor";
11980 case DW_OP_bra:
11981 return "DW_OP_bra";
11982 case DW_OP_eq:
11983 return "DW_OP_eq";
11984 case DW_OP_ge:
11985 return "DW_OP_ge";
11986 case DW_OP_gt:
11987 return "DW_OP_gt";
11988 case DW_OP_le:
11989 return "DW_OP_le";
11990 case DW_OP_lt:
11991 return "DW_OP_lt";
11992 case DW_OP_ne:
11993 return "DW_OP_ne";
11994 case DW_OP_skip:
11995 return "DW_OP_skip";
11996 case DW_OP_lit0:
11997 return "DW_OP_lit0";
11998 case DW_OP_lit1:
11999 return "DW_OP_lit1";
12000 case DW_OP_lit2:
12001 return "DW_OP_lit2";
12002 case DW_OP_lit3:
12003 return "DW_OP_lit3";
12004 case DW_OP_lit4:
12005 return "DW_OP_lit4";
12006 case DW_OP_lit5:
12007 return "DW_OP_lit5";
12008 case DW_OP_lit6:
12009 return "DW_OP_lit6";
12010 case DW_OP_lit7:
12011 return "DW_OP_lit7";
12012 case DW_OP_lit8:
12013 return "DW_OP_lit8";
12014 case DW_OP_lit9:
12015 return "DW_OP_lit9";
12016 case DW_OP_lit10:
12017 return "DW_OP_lit10";
12018 case DW_OP_lit11:
12019 return "DW_OP_lit11";
12020 case DW_OP_lit12:
12021 return "DW_OP_lit12";
12022 case DW_OP_lit13:
12023 return "DW_OP_lit13";
12024 case DW_OP_lit14:
12025 return "DW_OP_lit14";
12026 case DW_OP_lit15:
12027 return "DW_OP_lit15";
12028 case DW_OP_lit16:
12029 return "DW_OP_lit16";
12030 case DW_OP_lit17:
12031 return "DW_OP_lit17";
12032 case DW_OP_lit18:
12033 return "DW_OP_lit18";
12034 case DW_OP_lit19:
12035 return "DW_OP_lit19";
12036 case DW_OP_lit20:
12037 return "DW_OP_lit20";
12038 case DW_OP_lit21:
12039 return "DW_OP_lit21";
12040 case DW_OP_lit22:
12041 return "DW_OP_lit22";
12042 case DW_OP_lit23:
12043 return "DW_OP_lit23";
12044 case DW_OP_lit24:
12045 return "DW_OP_lit24";
12046 case DW_OP_lit25:
12047 return "DW_OP_lit25";
12048 case DW_OP_lit26:
12049 return "DW_OP_lit26";
12050 case DW_OP_lit27:
12051 return "DW_OP_lit27";
12052 case DW_OP_lit28:
12053 return "DW_OP_lit28";
12054 case DW_OP_lit29:
12055 return "DW_OP_lit29";
12056 case DW_OP_lit30:
12057 return "DW_OP_lit30";
12058 case DW_OP_lit31:
12059 return "DW_OP_lit31";
12060 case DW_OP_reg0:
12061 return "DW_OP_reg0";
12062 case DW_OP_reg1:
12063 return "DW_OP_reg1";
12064 case DW_OP_reg2:
12065 return "DW_OP_reg2";
12066 case DW_OP_reg3:
12067 return "DW_OP_reg3";
12068 case DW_OP_reg4:
12069 return "DW_OP_reg4";
12070 case DW_OP_reg5:
12071 return "DW_OP_reg5";
12072 case DW_OP_reg6:
12073 return "DW_OP_reg6";
12074 case DW_OP_reg7:
12075 return "DW_OP_reg7";
12076 case DW_OP_reg8:
12077 return "DW_OP_reg8";
12078 case DW_OP_reg9:
12079 return "DW_OP_reg9";
12080 case DW_OP_reg10:
12081 return "DW_OP_reg10";
12082 case DW_OP_reg11:
12083 return "DW_OP_reg11";
12084 case DW_OP_reg12:
12085 return "DW_OP_reg12";
12086 case DW_OP_reg13:
12087 return "DW_OP_reg13";
12088 case DW_OP_reg14:
12089 return "DW_OP_reg14";
12090 case DW_OP_reg15:
12091 return "DW_OP_reg15";
12092 case DW_OP_reg16:
12093 return "DW_OP_reg16";
12094 case DW_OP_reg17:
12095 return "DW_OP_reg17";
12096 case DW_OP_reg18:
12097 return "DW_OP_reg18";
12098 case DW_OP_reg19:
12099 return "DW_OP_reg19";
12100 case DW_OP_reg20:
12101 return "DW_OP_reg20";
12102 case DW_OP_reg21:
12103 return "DW_OP_reg21";
12104 case DW_OP_reg22:
12105 return "DW_OP_reg22";
12106 case DW_OP_reg23:
12107 return "DW_OP_reg23";
12108 case DW_OP_reg24:
12109 return "DW_OP_reg24";
12110 case DW_OP_reg25:
12111 return "DW_OP_reg25";
12112 case DW_OP_reg26:
12113 return "DW_OP_reg26";
12114 case DW_OP_reg27:
12115 return "DW_OP_reg27";
12116 case DW_OP_reg28:
12117 return "DW_OP_reg28";
12118 case DW_OP_reg29:
12119 return "DW_OP_reg29";
12120 case DW_OP_reg30:
12121 return "DW_OP_reg30";
12122 case DW_OP_reg31:
12123 return "DW_OP_reg31";
12124 case DW_OP_breg0:
12125 return "DW_OP_breg0";
12126 case DW_OP_breg1:
12127 return "DW_OP_breg1";
12128 case DW_OP_breg2:
12129 return "DW_OP_breg2";
12130 case DW_OP_breg3:
12131 return "DW_OP_breg3";
12132 case DW_OP_breg4:
12133 return "DW_OP_breg4";
12134 case DW_OP_breg5:
12135 return "DW_OP_breg5";
12136 case DW_OP_breg6:
12137 return "DW_OP_breg6";
12138 case DW_OP_breg7:
12139 return "DW_OP_breg7";
12140 case DW_OP_breg8:
12141 return "DW_OP_breg8";
12142 case DW_OP_breg9:
12143 return "DW_OP_breg9";
12144 case DW_OP_breg10:
12145 return "DW_OP_breg10";
12146 case DW_OP_breg11:
12147 return "DW_OP_breg11";
12148 case DW_OP_breg12:
12149 return "DW_OP_breg12";
12150 case DW_OP_breg13:
12151 return "DW_OP_breg13";
12152 case DW_OP_breg14:
12153 return "DW_OP_breg14";
12154 case DW_OP_breg15:
12155 return "DW_OP_breg15";
12156 case DW_OP_breg16:
12157 return "DW_OP_breg16";
12158 case DW_OP_breg17:
12159 return "DW_OP_breg17";
12160 case DW_OP_breg18:
12161 return "DW_OP_breg18";
12162 case DW_OP_breg19:
12163 return "DW_OP_breg19";
12164 case DW_OP_breg20:
12165 return "DW_OP_breg20";
12166 case DW_OP_breg21:
12167 return "DW_OP_breg21";
12168 case DW_OP_breg22:
12169 return "DW_OP_breg22";
12170 case DW_OP_breg23:
12171 return "DW_OP_breg23";
12172 case DW_OP_breg24:
12173 return "DW_OP_breg24";
12174 case DW_OP_breg25:
12175 return "DW_OP_breg25";
12176 case DW_OP_breg26:
12177 return "DW_OP_breg26";
12178 case DW_OP_breg27:
12179 return "DW_OP_breg27";
12180 case DW_OP_breg28:
12181 return "DW_OP_breg28";
12182 case DW_OP_breg29:
12183 return "DW_OP_breg29";
12184 case DW_OP_breg30:
12185 return "DW_OP_breg30";
12186 case DW_OP_breg31:
12187 return "DW_OP_breg31";
12188 case DW_OP_regx:
12189 return "DW_OP_regx";
12190 case DW_OP_fbreg:
12191 return "DW_OP_fbreg";
12192 case DW_OP_bregx:
12193 return "DW_OP_bregx";
12194 case DW_OP_piece:
12195 return "DW_OP_piece";
12196 case DW_OP_deref_size:
12197 return "DW_OP_deref_size";
12198 case DW_OP_xderef_size:
12199 return "DW_OP_xderef_size";
12200 case DW_OP_nop:
12201 return "DW_OP_nop";
b7619582 12202 /* DWARF 3 extensions. */
ed348acc
EZ
12203 case DW_OP_push_object_address:
12204 return "DW_OP_push_object_address";
12205 case DW_OP_call2:
12206 return "DW_OP_call2";
12207 case DW_OP_call4:
12208 return "DW_OP_call4";
12209 case DW_OP_call_ref:
12210 return "DW_OP_call_ref";
b7619582
GF
12211 case DW_OP_form_tls_address:
12212 return "DW_OP_form_tls_address";
12213 case DW_OP_call_frame_cfa:
12214 return "DW_OP_call_frame_cfa";
12215 case DW_OP_bit_piece:
12216 return "DW_OP_bit_piece";
9eae7c52
TT
12217 /* DWARF 4 extensions. */
12218 case DW_OP_implicit_value:
12219 return "DW_OP_implicit_value";
12220 case DW_OP_stack_value:
12221 return "DW_OP_stack_value";
12222 /* GNU extensions. */
ed348acc
EZ
12223 case DW_OP_GNU_push_tls_address:
12224 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12225 case DW_OP_GNU_uninit:
12226 return "DW_OP_GNU_uninit";
c906108c 12227 default:
9eae7c52 12228 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12229 }
12230}
12231
12232static char *
fba45db2 12233dwarf_bool_name (unsigned mybool)
c906108c
SS
12234{
12235 if (mybool)
12236 return "TRUE";
12237 else
12238 return "FALSE";
12239}
12240
12241/* Convert a DWARF type code into its string name. */
12242
12243static char *
aa1ee363 12244dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12245{
12246 switch (enc)
12247 {
b7619582
GF
12248 case DW_ATE_void:
12249 return "DW_ATE_void";
c906108c
SS
12250 case DW_ATE_address:
12251 return "DW_ATE_address";
12252 case DW_ATE_boolean:
12253 return "DW_ATE_boolean";
12254 case DW_ATE_complex_float:
12255 return "DW_ATE_complex_float";
12256 case DW_ATE_float:
12257 return "DW_ATE_float";
12258 case DW_ATE_signed:
12259 return "DW_ATE_signed";
12260 case DW_ATE_signed_char:
12261 return "DW_ATE_signed_char";
12262 case DW_ATE_unsigned:
12263 return "DW_ATE_unsigned";
12264 case DW_ATE_unsigned_char:
12265 return "DW_ATE_unsigned_char";
b7619582 12266 /* DWARF 3. */
d9fa45fe
DC
12267 case DW_ATE_imaginary_float:
12268 return "DW_ATE_imaginary_float";
b7619582
GF
12269 case DW_ATE_packed_decimal:
12270 return "DW_ATE_packed_decimal";
12271 case DW_ATE_numeric_string:
12272 return "DW_ATE_numeric_string";
12273 case DW_ATE_edited:
12274 return "DW_ATE_edited";
12275 case DW_ATE_signed_fixed:
12276 return "DW_ATE_signed_fixed";
12277 case DW_ATE_unsigned_fixed:
12278 return "DW_ATE_unsigned_fixed";
12279 case DW_ATE_decimal_float:
12280 return "DW_ATE_decimal_float";
75079b2b
TT
12281 /* DWARF 4. */
12282 case DW_ATE_UTF:
12283 return "DW_ATE_UTF";
b7619582
GF
12284 /* HP extensions. */
12285 case DW_ATE_HP_float80:
12286 return "DW_ATE_HP_float80";
12287 case DW_ATE_HP_complex_float80:
12288 return "DW_ATE_HP_complex_float80";
12289 case DW_ATE_HP_float128:
12290 return "DW_ATE_HP_float128";
12291 case DW_ATE_HP_complex_float128:
12292 return "DW_ATE_HP_complex_float128";
12293 case DW_ATE_HP_floathpintel:
12294 return "DW_ATE_HP_floathpintel";
12295 case DW_ATE_HP_imaginary_float80:
12296 return "DW_ATE_HP_imaginary_float80";
12297 case DW_ATE_HP_imaginary_float128:
12298 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12299 default:
12300 return "DW_ATE_<unknown>";
12301 }
12302}
12303
12304/* Convert a DWARF call frame info operation to its string name. */
12305
12306#if 0
12307static char *
aa1ee363 12308dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12309{
12310 switch (cfi_opc)
12311 {
12312 case DW_CFA_advance_loc:
12313 return "DW_CFA_advance_loc";
12314 case DW_CFA_offset:
12315 return "DW_CFA_offset";
12316 case DW_CFA_restore:
12317 return "DW_CFA_restore";
12318 case DW_CFA_nop:
12319 return "DW_CFA_nop";
12320 case DW_CFA_set_loc:
12321 return "DW_CFA_set_loc";
12322 case DW_CFA_advance_loc1:
12323 return "DW_CFA_advance_loc1";
12324 case DW_CFA_advance_loc2:
12325 return "DW_CFA_advance_loc2";
12326 case DW_CFA_advance_loc4:
12327 return "DW_CFA_advance_loc4";
12328 case DW_CFA_offset_extended:
12329 return "DW_CFA_offset_extended";
12330 case DW_CFA_restore_extended:
12331 return "DW_CFA_restore_extended";
12332 case DW_CFA_undefined:
12333 return "DW_CFA_undefined";
12334 case DW_CFA_same_value:
12335 return "DW_CFA_same_value";
12336 case DW_CFA_register:
12337 return "DW_CFA_register";
12338 case DW_CFA_remember_state:
12339 return "DW_CFA_remember_state";
12340 case DW_CFA_restore_state:
12341 return "DW_CFA_restore_state";
12342 case DW_CFA_def_cfa:
12343 return "DW_CFA_def_cfa";
12344 case DW_CFA_def_cfa_register:
12345 return "DW_CFA_def_cfa_register";
12346 case DW_CFA_def_cfa_offset:
12347 return "DW_CFA_def_cfa_offset";
b7619582 12348 /* DWARF 3. */
985cb1a3
JM
12349 case DW_CFA_def_cfa_expression:
12350 return "DW_CFA_def_cfa_expression";
12351 case DW_CFA_expression:
12352 return "DW_CFA_expression";
12353 case DW_CFA_offset_extended_sf:
12354 return "DW_CFA_offset_extended_sf";
12355 case DW_CFA_def_cfa_sf:
12356 return "DW_CFA_def_cfa_sf";
12357 case DW_CFA_def_cfa_offset_sf:
12358 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12359 case DW_CFA_val_offset:
12360 return "DW_CFA_val_offset";
12361 case DW_CFA_val_offset_sf:
12362 return "DW_CFA_val_offset_sf";
12363 case DW_CFA_val_expression:
12364 return "DW_CFA_val_expression";
12365 /* SGI/MIPS specific. */
c906108c
SS
12366 case DW_CFA_MIPS_advance_loc8:
12367 return "DW_CFA_MIPS_advance_loc8";
b7619582 12368 /* GNU extensions. */
985cb1a3
JM
12369 case DW_CFA_GNU_window_save:
12370 return "DW_CFA_GNU_window_save";
12371 case DW_CFA_GNU_args_size:
12372 return "DW_CFA_GNU_args_size";
12373 case DW_CFA_GNU_negative_offset_extended:
12374 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12375 default:
12376 return "DW_CFA_<unknown>";
12377 }
12378}
12379#endif
12380
f9aca02d 12381static void
d97bc12b 12382dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12383{
12384 unsigned int i;
12385
d97bc12b
DE
12386 print_spaces (indent, f);
12387 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12388 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12389
12390 if (die->parent != NULL)
12391 {
12392 print_spaces (indent, f);
12393 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12394 die->parent->offset);
12395 }
12396
12397 print_spaces (indent, f);
12398 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12399 dwarf_bool_name (die->child != NULL));
c906108c 12400
d97bc12b
DE
12401 print_spaces (indent, f);
12402 fprintf_unfiltered (f, " attributes:\n");
12403
c906108c
SS
12404 for (i = 0; i < die->num_attrs; ++i)
12405 {
d97bc12b
DE
12406 print_spaces (indent, f);
12407 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12408 dwarf_attr_name (die->attrs[i].name),
12409 dwarf_form_name (die->attrs[i].form));
d97bc12b 12410
c906108c
SS
12411 switch (die->attrs[i].form)
12412 {
12413 case DW_FORM_ref_addr:
12414 case DW_FORM_addr:
d97bc12b 12415 fprintf_unfiltered (f, "address: ");
5af949e3 12416 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12417 break;
12418 case DW_FORM_block2:
12419 case DW_FORM_block4:
12420 case DW_FORM_block:
12421 case DW_FORM_block1:
d97bc12b 12422 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 12423 break;
2dc7f7b3
TT
12424 case DW_FORM_exprloc:
12425 fprintf_unfiltered (f, "expression: size %u",
12426 DW_BLOCK (&die->attrs[i])->size);
12427 break;
10b3939b
DJ
12428 case DW_FORM_ref1:
12429 case DW_FORM_ref2:
12430 case DW_FORM_ref4:
d97bc12b 12431 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12432 (long) (DW_ADDR (&die->attrs[i])));
12433 break;
c906108c
SS
12434 case DW_FORM_data1:
12435 case DW_FORM_data2:
12436 case DW_FORM_data4:
ce5d95e1 12437 case DW_FORM_data8:
c906108c
SS
12438 case DW_FORM_udata:
12439 case DW_FORM_sdata:
43bbcdc2
PH
12440 fprintf_unfiltered (f, "constant: %s",
12441 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 12442 break;
2dc7f7b3
TT
12443 case DW_FORM_sec_offset:
12444 fprintf_unfiltered (f, "section offset: %s",
12445 pulongest (DW_UNSND (&die->attrs[i])));
12446 break;
348e048f
DE
12447 case DW_FORM_sig8:
12448 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12449 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12450 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12451 else
12452 fprintf_unfiltered (f, "signatured type, offset: unknown");
12453 break;
c906108c 12454 case DW_FORM_string:
4bdf3d34 12455 case DW_FORM_strp:
8285870a 12456 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 12457 DW_STRING (&die->attrs[i])
8285870a
JK
12458 ? DW_STRING (&die->attrs[i]) : "",
12459 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
12460 break;
12461 case DW_FORM_flag:
12462 if (DW_UNSND (&die->attrs[i]))
d97bc12b 12463 fprintf_unfiltered (f, "flag: TRUE");
c906108c 12464 else
d97bc12b 12465 fprintf_unfiltered (f, "flag: FALSE");
c906108c 12466 break;
2dc7f7b3
TT
12467 case DW_FORM_flag_present:
12468 fprintf_unfiltered (f, "flag: TRUE");
12469 break;
a8329558
KW
12470 case DW_FORM_indirect:
12471 /* the reader will have reduced the indirect form to
12472 the "base form" so this form should not occur */
d97bc12b 12473 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 12474 break;
c906108c 12475 default:
d97bc12b 12476 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 12477 die->attrs[i].form);
d97bc12b 12478 break;
c906108c 12479 }
d97bc12b 12480 fprintf_unfiltered (f, "\n");
c906108c
SS
12481 }
12482}
12483
f9aca02d 12484static void
d97bc12b 12485dump_die_for_error (struct die_info *die)
c906108c 12486{
d97bc12b
DE
12487 dump_die_shallow (gdb_stderr, 0, die);
12488}
12489
12490static void
12491dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12492{
12493 int indent = level * 4;
12494
12495 gdb_assert (die != NULL);
12496
12497 if (level >= max_level)
12498 return;
12499
12500 dump_die_shallow (f, indent, die);
12501
12502 if (die->child != NULL)
c906108c 12503 {
d97bc12b
DE
12504 print_spaces (indent, f);
12505 fprintf_unfiltered (f, " Children:");
12506 if (level + 1 < max_level)
12507 {
12508 fprintf_unfiltered (f, "\n");
12509 dump_die_1 (f, level + 1, max_level, die->child);
12510 }
12511 else
12512 {
12513 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12514 }
12515 }
12516
12517 if (die->sibling != NULL && level > 0)
12518 {
12519 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
12520 }
12521}
12522
d97bc12b
DE
12523/* This is called from the pdie macro in gdbinit.in.
12524 It's not static so gcc will keep a copy callable from gdb. */
12525
12526void
12527dump_die (struct die_info *die, int max_level)
12528{
12529 dump_die_1 (gdb_stdlog, 0, max_level, die);
12530}
12531
f9aca02d 12532static void
51545339 12533store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12534{
51545339 12535 void **slot;
c906108c 12536
51545339
DJ
12537 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12538
12539 *slot = die;
c906108c
SS
12540}
12541
93311388
DE
12542static int
12543is_ref_attr (struct attribute *attr)
c906108c 12544{
c906108c
SS
12545 switch (attr->form)
12546 {
12547 case DW_FORM_ref_addr:
c906108c
SS
12548 case DW_FORM_ref1:
12549 case DW_FORM_ref2:
12550 case DW_FORM_ref4:
613e1657 12551 case DW_FORM_ref8:
c906108c 12552 case DW_FORM_ref_udata:
93311388 12553 return 1;
c906108c 12554 default:
93311388 12555 return 0;
c906108c 12556 }
93311388
DE
12557}
12558
12559static unsigned int
12560dwarf2_get_ref_die_offset (struct attribute *attr)
12561{
12562 if (is_ref_attr (attr))
12563 return DW_ADDR (attr);
12564
12565 complaint (&symfile_complaints,
12566 _("unsupported die ref attribute form: '%s'"),
12567 dwarf_form_name (attr->form));
12568 return 0;
c906108c
SS
12569}
12570
43bbcdc2
PH
12571/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12572 * the value held by the attribute is not constant. */
a02abb62 12573
43bbcdc2 12574static LONGEST
a02abb62
JB
12575dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12576{
12577 if (attr->form == DW_FORM_sdata)
12578 return DW_SND (attr);
12579 else if (attr->form == DW_FORM_udata
12580 || attr->form == DW_FORM_data1
12581 || attr->form == DW_FORM_data2
12582 || attr->form == DW_FORM_data4
12583 || attr->form == DW_FORM_data8)
12584 return DW_UNSND (attr);
12585 else
12586 {
e2e0b3e5 12587 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
12588 dwarf_form_name (attr->form));
12589 return default_value;
12590 }
12591}
12592
03dd20cc 12593/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
12594 unit and add it to our queue.
12595 The result is non-zero if PER_CU was queued, otherwise the result is zero
12596 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 12597
348e048f 12598static int
03dd20cc
DJ
12599maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12600 struct dwarf2_per_cu_data *per_cu)
12601{
98bfdba5
PA
12602 /* We may arrive here during partial symbol reading, if we need full
12603 DIEs to process an unusual case (e.g. template arguments). Do
12604 not queue PER_CU, just tell our caller to load its DIEs. */
12605 if (dwarf2_per_objfile->reading_partial_symbols)
12606 {
12607 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12608 return 1;
12609 return 0;
12610 }
12611
03dd20cc
DJ
12612 /* Mark the dependence relation so that we don't flush PER_CU
12613 too early. */
12614 dwarf2_add_dependence (this_cu, per_cu);
12615
12616 /* If it's already on the queue, we have nothing to do. */
12617 if (per_cu->queued)
348e048f 12618 return 0;
03dd20cc
DJ
12619
12620 /* If the compilation unit is already loaded, just mark it as
12621 used. */
12622 if (per_cu->cu != NULL)
12623 {
12624 per_cu->cu->last_used = 0;
348e048f 12625 return 0;
03dd20cc
DJ
12626 }
12627
12628 /* Add it to the queue. */
12629 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
12630
12631 return 1;
12632}
12633
12634/* Follow reference or signature attribute ATTR of SRC_DIE.
12635 On entry *REF_CU is the CU of SRC_DIE.
12636 On exit *REF_CU is the CU of the result. */
12637
12638static struct die_info *
12639follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12640 struct dwarf2_cu **ref_cu)
12641{
12642 struct die_info *die;
12643
12644 if (is_ref_attr (attr))
12645 die = follow_die_ref (src_die, attr, ref_cu);
12646 else if (attr->form == DW_FORM_sig8)
12647 die = follow_die_sig (src_die, attr, ref_cu);
12648 else
12649 {
12650 dump_die_for_error (src_die);
12651 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12652 (*ref_cu)->objfile->name);
12653 }
12654
12655 return die;
03dd20cc
DJ
12656}
12657
5c631832
JK
12658/* Follow reference OFFSET.
12659 On entry *REF_CU is the CU of source DIE referencing OFFSET.
f504f079
DE
12660 On exit *REF_CU is the CU of the result. */
12661
f9aca02d 12662static struct die_info *
5c631832 12663follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 12664{
10b3939b 12665 struct die_info temp_die;
f2f0e013 12666 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 12667
348e048f
DE
12668 gdb_assert (cu->per_cu != NULL);
12669
98bfdba5
PA
12670 target_cu = cu;
12671
348e048f
DE
12672 if (cu->per_cu->from_debug_types)
12673 {
12674 /* .debug_types CUs cannot reference anything outside their CU.
12675 If they need to, they have to reference a signatured type via
12676 DW_FORM_sig8. */
12677 if (! offset_in_cu_p (&cu->header, offset))
5c631832 12678 return NULL;
348e048f
DE
12679 }
12680 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
12681 {
12682 struct dwarf2_per_cu_data *per_cu;
9a619af0 12683
45452591 12684 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
12685
12686 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
12687 if (maybe_queue_comp_unit (cu, per_cu))
12688 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 12689
10b3939b
DJ
12690 target_cu = per_cu->cu;
12691 }
98bfdba5
PA
12692 else if (cu->dies == NULL)
12693 {
12694 /* We're loading full DIEs during partial symbol reading. */
12695 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
12696 load_full_comp_unit (cu->per_cu, cu->objfile);
12697 }
c906108c 12698
f2f0e013 12699 *ref_cu = target_cu;
51545339 12700 temp_die.offset = offset;
5c631832
JK
12701 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12702}
10b3939b 12703
5c631832
JK
12704/* Follow reference attribute ATTR of SRC_DIE.
12705 On entry *REF_CU is the CU of SRC_DIE.
12706 On exit *REF_CU is the CU of the result. */
12707
12708static struct die_info *
12709follow_die_ref (struct die_info *src_die, struct attribute *attr,
12710 struct dwarf2_cu **ref_cu)
12711{
12712 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12713 struct dwarf2_cu *cu = *ref_cu;
12714 struct die_info *die;
12715
12716 die = follow_die_offset (offset, ref_cu);
12717 if (!die)
12718 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
12719 "at 0x%x [in module %s]"),
12720 offset, src_die->offset, cu->objfile->name);
348e048f 12721
5c631832
JK
12722 return die;
12723}
12724
12725/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
12726 value is intended for DW_OP_call*. */
12727
12728struct dwarf2_locexpr_baton
12729dwarf2_fetch_die_location_block (unsigned int offset,
12730 struct dwarf2_per_cu_data *per_cu)
12731{
12732 struct dwarf2_cu *cu = per_cu->cu;
12733 struct die_info *die;
12734 struct attribute *attr;
12735 struct dwarf2_locexpr_baton retval;
12736
12737 die = follow_die_offset (offset, &cu);
12738 if (!die)
12739 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
12740 offset, per_cu->cu->objfile->name);
12741
12742 attr = dwarf2_attr (die, DW_AT_location, cu);
12743 if (!attr)
12744 {
12745 /* DWARF: "If there is no such attribute, then there is no effect.". */
12746
12747 retval.data = NULL;
12748 retval.size = 0;
12749 }
12750 else
12751 {
12752 if (!attr_form_is_block (attr))
12753 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
12754 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
12755 offset, per_cu->cu->objfile->name);
12756
12757 retval.data = DW_BLOCK (attr)->data;
12758 retval.size = DW_BLOCK (attr)->size;
12759 }
12760 retval.per_cu = cu->per_cu;
12761 return retval;
348e048f
DE
12762}
12763
12764/* Follow the signature attribute ATTR in SRC_DIE.
12765 On entry *REF_CU is the CU of SRC_DIE.
12766 On exit *REF_CU is the CU of the result. */
12767
12768static struct die_info *
12769follow_die_sig (struct die_info *src_die, struct attribute *attr,
12770 struct dwarf2_cu **ref_cu)
12771{
12772 struct objfile *objfile = (*ref_cu)->objfile;
12773 struct die_info temp_die;
12774 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12775 struct dwarf2_cu *sig_cu;
12776 struct die_info *die;
12777
12778 /* sig_type will be NULL if the signatured type is missing from
12779 the debug info. */
12780 if (sig_type == NULL)
12781 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12782 "at 0x%x [in module %s]"),
12783 src_die->offset, objfile->name);
12784
12785 /* If necessary, add it to the queue and load its DIEs. */
12786
12787 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12788 read_signatured_type (objfile, sig_type);
12789
12790 gdb_assert (sig_type->per_cu.cu != NULL);
12791
12792 sig_cu = sig_type->per_cu.cu;
12793 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12794 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12795 if (die)
12796 {
12797 *ref_cu = sig_cu;
12798 return die;
12799 }
12800
12801 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12802 "at 0x%x [in module %s]"),
12803 sig_type->type_offset, src_die->offset, objfile->name);
12804}
12805
12806/* Given an offset of a signatured type, return its signatured_type. */
12807
12808static struct signatured_type *
12809lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12810{
12811 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12812 unsigned int length, initial_length_size;
12813 unsigned int sig_offset;
12814 struct signatured_type find_entry, *type_sig;
12815
12816 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12817 sig_offset = (initial_length_size
12818 + 2 /*version*/
12819 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12820 + 1 /*address_size*/);
12821 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12822 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12823
12824 /* This is only used to lookup previously recorded types.
12825 If we didn't find it, it's our bug. */
12826 gdb_assert (type_sig != NULL);
12827 gdb_assert (offset == type_sig->offset);
12828
12829 return type_sig;
12830}
12831
12832/* Read in signatured type at OFFSET and build its CU and die(s). */
12833
12834static void
12835read_signatured_type_at_offset (struct objfile *objfile,
12836 unsigned int offset)
12837{
12838 struct signatured_type *type_sig;
12839
be391dca
TT
12840 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12841
348e048f
DE
12842 /* We have the section offset, but we need the signature to do the
12843 hash table lookup. */
12844 type_sig = lookup_signatured_type_at_offset (objfile, offset);
12845
12846 gdb_assert (type_sig->per_cu.cu == NULL);
12847
12848 read_signatured_type (objfile, type_sig);
12849
12850 gdb_assert (type_sig->per_cu.cu != NULL);
12851}
12852
12853/* Read in a signatured type and build its CU and DIEs. */
12854
12855static void
12856read_signatured_type (struct objfile *objfile,
12857 struct signatured_type *type_sig)
12858{
1fd400ff 12859 gdb_byte *types_ptr;
348e048f
DE
12860 struct die_reader_specs reader_specs;
12861 struct dwarf2_cu *cu;
12862 ULONGEST signature;
12863 struct cleanup *back_to, *free_cu_cleanup;
12864 struct attribute *attr;
12865
1fd400ff
TT
12866 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12867 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
12868
348e048f
DE
12869 gdb_assert (type_sig->per_cu.cu == NULL);
12870
12871 cu = xmalloc (sizeof (struct dwarf2_cu));
12872 memset (cu, 0, sizeof (struct dwarf2_cu));
12873 obstack_init (&cu->comp_unit_obstack);
12874 cu->objfile = objfile;
12875 type_sig->per_cu.cu = cu;
12876 cu->per_cu = &type_sig->per_cu;
12877
12878 /* If an error occurs while loading, release our storage. */
12879 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
12880
12881 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
12882 types_ptr, objfile->obfd);
12883 gdb_assert (signature == type_sig->signature);
12884
12885 cu->die_hash
12886 = htab_create_alloc_ex (cu->header.length / 12,
12887 die_hash,
12888 die_eq,
12889 NULL,
12890 &cu->comp_unit_obstack,
12891 hashtab_obstack_allocate,
12892 dummy_obstack_deallocate);
12893
12894 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
12895 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
12896
12897 init_cu_die_reader (&reader_specs, cu);
12898
12899 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
12900 NULL /*parent*/);
12901
12902 /* We try not to read any attributes in this function, because not
12903 all objfiles needed for references have been loaded yet, and symbol
12904 table processing isn't initialized. But we have to set the CU language,
12905 or we won't be able to build types correctly. */
12906 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
12907 if (attr)
12908 set_cu_language (DW_UNSND (attr), cu);
12909 else
12910 set_cu_language (language_minimal, cu);
12911
12912 do_cleanups (back_to);
12913
12914 /* We've successfully allocated this compilation unit. Let our caller
12915 clean it up when finished with it. */
12916 discard_cleanups (free_cu_cleanup);
12917
12918 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
12919 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
12920}
12921
c906108c
SS
12922/* Decode simple location descriptions.
12923 Given a pointer to a dwarf block that defines a location, compute
12924 the location and return the value.
12925
4cecd739
DJ
12926 NOTE drow/2003-11-18: This function is called in two situations
12927 now: for the address of static or global variables (partial symbols
12928 only) and for offsets into structures which are expected to be
12929 (more or less) constant. The partial symbol case should go away,
12930 and only the constant case should remain. That will let this
12931 function complain more accurately. A few special modes are allowed
12932 without complaint for global variables (for instance, global
12933 register values and thread-local values).
c906108c
SS
12934
12935 A location description containing no operations indicates that the
4cecd739 12936 object is optimized out. The return value is 0 for that case.
6b992462
DJ
12937 FIXME drow/2003-11-16: No callers check for this case any more; soon all
12938 callers will only want a very basic result and this can become a
12939 complaint.
c906108c 12940
c906108c
SS
12941 Note that stack[0] is unused except as a default error return.
12942 Note that stack overflow is not yet handled. */
12943
12944static CORE_ADDR
e7c27a73 12945decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 12946{
e7c27a73 12947 struct objfile *objfile = cu->objfile;
c906108c
SS
12948 int i;
12949 int size = blk->size;
fe1b8b76 12950 gdb_byte *data = blk->data;
c906108c
SS
12951 CORE_ADDR stack[64];
12952 int stacki;
12953 unsigned int bytes_read, unsnd;
fe1b8b76 12954 gdb_byte op;
c906108c
SS
12955
12956 i = 0;
12957 stacki = 0;
12958 stack[stacki] = 0;
c906108c
SS
12959
12960 while (i < size)
12961 {
c906108c
SS
12962 op = data[i++];
12963 switch (op)
12964 {
f1bea926
JM
12965 case DW_OP_lit0:
12966 case DW_OP_lit1:
12967 case DW_OP_lit2:
12968 case DW_OP_lit3:
12969 case DW_OP_lit4:
12970 case DW_OP_lit5:
12971 case DW_OP_lit6:
12972 case DW_OP_lit7:
12973 case DW_OP_lit8:
12974 case DW_OP_lit9:
12975 case DW_OP_lit10:
12976 case DW_OP_lit11:
12977 case DW_OP_lit12:
12978 case DW_OP_lit13:
12979 case DW_OP_lit14:
12980 case DW_OP_lit15:
12981 case DW_OP_lit16:
12982 case DW_OP_lit17:
12983 case DW_OP_lit18:
12984 case DW_OP_lit19:
12985 case DW_OP_lit20:
12986 case DW_OP_lit21:
12987 case DW_OP_lit22:
12988 case DW_OP_lit23:
12989 case DW_OP_lit24:
12990 case DW_OP_lit25:
12991 case DW_OP_lit26:
12992 case DW_OP_lit27:
12993 case DW_OP_lit28:
12994 case DW_OP_lit29:
12995 case DW_OP_lit30:
12996 case DW_OP_lit31:
12997 stack[++stacki] = op - DW_OP_lit0;
12998 break;
12999
c906108c
SS
13000 case DW_OP_reg0:
13001 case DW_OP_reg1:
13002 case DW_OP_reg2:
13003 case DW_OP_reg3:
13004 case DW_OP_reg4:
13005 case DW_OP_reg5:
13006 case DW_OP_reg6:
13007 case DW_OP_reg7:
13008 case DW_OP_reg8:
13009 case DW_OP_reg9:
13010 case DW_OP_reg10:
13011 case DW_OP_reg11:
13012 case DW_OP_reg12:
13013 case DW_OP_reg13:
13014 case DW_OP_reg14:
13015 case DW_OP_reg15:
13016 case DW_OP_reg16:
13017 case DW_OP_reg17:
13018 case DW_OP_reg18:
13019 case DW_OP_reg19:
13020 case DW_OP_reg20:
13021 case DW_OP_reg21:
13022 case DW_OP_reg22:
13023 case DW_OP_reg23:
13024 case DW_OP_reg24:
13025 case DW_OP_reg25:
13026 case DW_OP_reg26:
13027 case DW_OP_reg27:
13028 case DW_OP_reg28:
13029 case DW_OP_reg29:
13030 case DW_OP_reg30:
13031 case DW_OP_reg31:
c906108c 13032 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13033 if (i < size)
13034 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13035 break;
13036
13037 case DW_OP_regx:
c906108c
SS
13038 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13039 i += bytes_read;
c906108c 13040 stack[++stacki] = unsnd;
4cecd739
DJ
13041 if (i < size)
13042 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13043 break;
13044
13045 case DW_OP_addr:
107d2387 13046 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13047 cu, &bytes_read);
107d2387 13048 i += bytes_read;
c906108c
SS
13049 break;
13050
13051 case DW_OP_const1u:
13052 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13053 i += 1;
13054 break;
13055
13056 case DW_OP_const1s:
13057 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13058 i += 1;
13059 break;
13060
13061 case DW_OP_const2u:
13062 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13063 i += 2;
13064 break;
13065
13066 case DW_OP_const2s:
13067 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13068 i += 2;
13069 break;
13070
13071 case DW_OP_const4u:
13072 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13073 i += 4;
13074 break;
13075
13076 case DW_OP_const4s:
13077 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13078 i += 4;
13079 break;
13080
13081 case DW_OP_constu:
13082 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13083 &bytes_read);
c906108c
SS
13084 i += bytes_read;
13085 break;
13086
13087 case DW_OP_consts:
13088 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13089 i += bytes_read;
13090 break;
13091
f1bea926
JM
13092 case DW_OP_dup:
13093 stack[stacki + 1] = stack[stacki];
13094 stacki++;
13095 break;
13096
c906108c
SS
13097 case DW_OP_plus:
13098 stack[stacki - 1] += stack[stacki];
13099 stacki--;
13100 break;
13101
13102 case DW_OP_plus_uconst:
13103 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13104 i += bytes_read;
13105 break;
13106
13107 case DW_OP_minus:
f1bea926 13108 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13109 stacki--;
13110 break;
13111
7a292a7a 13112 case DW_OP_deref:
7a292a7a 13113 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13114 this using GDB's address_class enum. This is valid for partial
13115 global symbols, although the variable's address will be bogus
13116 in the psymtab. */
7a292a7a 13117 if (i < size)
4d3c2250 13118 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13119 break;
13120
9d774e44 13121 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13122 /* The top of the stack has the offset from the beginning
13123 of the thread control block at which the variable is located. */
13124 /* Nothing should follow this operator, so the top of stack would
13125 be returned. */
4cecd739
DJ
13126 /* This is valid for partial global symbols, but the variable's
13127 address will be bogus in the psymtab. */
9d774e44 13128 if (i < size)
4d3c2250 13129 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13130 break;
13131
42be36b3
CT
13132 case DW_OP_GNU_uninit:
13133 break;
13134
c906108c 13135 default:
e2e0b3e5 13136 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13137 dwarf_stack_op_name (op, 1));
c906108c
SS
13138 return (stack[stacki]);
13139 }
13140 }
13141 return (stack[stacki]);
13142}
13143
13144/* memory allocation interface */
13145
c906108c 13146static struct dwarf_block *
7b5a2f43 13147dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13148{
13149 struct dwarf_block *blk;
13150
13151 blk = (struct dwarf_block *)
7b5a2f43 13152 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13153 return (blk);
13154}
13155
13156static struct abbrev_info *
f3dd6933 13157dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13158{
13159 struct abbrev_info *abbrev;
13160
f3dd6933
DJ
13161 abbrev = (struct abbrev_info *)
13162 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13163 memset (abbrev, 0, sizeof (struct abbrev_info));
13164 return (abbrev);
13165}
13166
13167static struct die_info *
b60c80d6 13168dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13169{
13170 struct die_info *die;
b60c80d6
DJ
13171 size_t size = sizeof (struct die_info);
13172
13173 if (num_attrs > 1)
13174 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13175
b60c80d6 13176 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13177 memset (die, 0, sizeof (struct die_info));
13178 return (die);
13179}
2e276125
JB
13180
13181\f
13182/* Macro support. */
13183
13184
13185/* Return the full name of file number I in *LH's file name table.
13186 Use COMP_DIR as the name of the current directory of the
13187 compilation. The result is allocated using xmalloc; the caller is
13188 responsible for freeing it. */
13189static char *
13190file_full_name (int file, struct line_header *lh, const char *comp_dir)
13191{
6a83a1e6
EZ
13192 /* Is the file number a valid index into the line header's file name
13193 table? Remember that file numbers start with one, not zero. */
13194 if (1 <= file && file <= lh->num_file_names)
13195 {
13196 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13197
6a83a1e6
EZ
13198 if (IS_ABSOLUTE_PATH (fe->name))
13199 return xstrdup (fe->name);
13200 else
13201 {
13202 const char *dir;
13203 int dir_len;
13204 char *full_name;
13205
13206 if (fe->dir_index)
13207 dir = lh->include_dirs[fe->dir_index - 1];
13208 else
13209 dir = comp_dir;
13210
13211 if (dir)
13212 {
13213 dir_len = strlen (dir);
13214 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13215 strcpy (full_name, dir);
13216 full_name[dir_len] = '/';
13217 strcpy (full_name + dir_len + 1, fe->name);
13218 return full_name;
13219 }
13220 else
13221 return xstrdup (fe->name);
13222 }
13223 }
2e276125
JB
13224 else
13225 {
6a83a1e6
EZ
13226 /* The compiler produced a bogus file number. We can at least
13227 record the macro definitions made in the file, even if we
13228 won't be able to find the file by name. */
13229 char fake_name[80];
9a619af0 13230
6a83a1e6 13231 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13232
6e70227d 13233 complaint (&symfile_complaints,
6a83a1e6
EZ
13234 _("bad file number in macro information (%d)"),
13235 file);
2e276125 13236
6a83a1e6 13237 return xstrdup (fake_name);
2e276125
JB
13238 }
13239}
13240
13241
13242static struct macro_source_file *
13243macro_start_file (int file, int line,
13244 struct macro_source_file *current_file,
13245 const char *comp_dir,
13246 struct line_header *lh, struct objfile *objfile)
13247{
13248 /* The full name of this source file. */
13249 char *full_name = file_full_name (file, lh, comp_dir);
13250
13251 /* We don't create a macro table for this compilation unit
13252 at all until we actually get a filename. */
13253 if (! pending_macros)
4a146b47 13254 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13255 objfile->macro_cache);
2e276125
JB
13256
13257 if (! current_file)
13258 /* If we have no current file, then this must be the start_file
13259 directive for the compilation unit's main source file. */
13260 current_file = macro_set_main (pending_macros, full_name);
13261 else
13262 current_file = macro_include (current_file, line, full_name);
13263
13264 xfree (full_name);
6e70227d 13265
2e276125
JB
13266 return current_file;
13267}
13268
13269
13270/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13271 followed by a null byte. */
13272static char *
13273copy_string (const char *buf, int len)
13274{
13275 char *s = xmalloc (len + 1);
9a619af0 13276
2e276125
JB
13277 memcpy (s, buf, len);
13278 s[len] = '\0';
2e276125
JB
13279 return s;
13280}
13281
13282
13283static const char *
13284consume_improper_spaces (const char *p, const char *body)
13285{
13286 if (*p == ' ')
13287 {
4d3c2250 13288 complaint (&symfile_complaints,
e2e0b3e5 13289 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 13290 body);
2e276125
JB
13291
13292 while (*p == ' ')
13293 p++;
13294 }
13295
13296 return p;
13297}
13298
13299
13300static void
13301parse_macro_definition (struct macro_source_file *file, int line,
13302 const char *body)
13303{
13304 const char *p;
13305
13306 /* The body string takes one of two forms. For object-like macro
13307 definitions, it should be:
13308
13309 <macro name> " " <definition>
13310
13311 For function-like macro definitions, it should be:
13312
13313 <macro name> "() " <definition>
13314 or
13315 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13316
13317 Spaces may appear only where explicitly indicated, and in the
13318 <definition>.
13319
13320 The Dwarf 2 spec says that an object-like macro's name is always
13321 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13322 the space when the macro's definition is the empty string.
2e276125
JB
13323
13324 The Dwarf 2 spec says that there should be no spaces between the
13325 formal arguments in a function-like macro's formal argument list,
13326 but versions of GCC around March 2002 include spaces after the
13327 commas. */
13328
13329
13330 /* Find the extent of the macro name. The macro name is terminated
13331 by either a space or null character (for an object-like macro) or
13332 an opening paren (for a function-like macro). */
13333 for (p = body; *p; p++)
13334 if (*p == ' ' || *p == '(')
13335 break;
13336
13337 if (*p == ' ' || *p == '\0')
13338 {
13339 /* It's an object-like macro. */
13340 int name_len = p - body;
13341 char *name = copy_string (body, name_len);
13342 const char *replacement;
13343
13344 if (*p == ' ')
13345 replacement = body + name_len + 1;
13346 else
13347 {
4d3c2250 13348 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13349 replacement = body + name_len;
13350 }
6e70227d 13351
2e276125
JB
13352 macro_define_object (file, line, name, replacement);
13353
13354 xfree (name);
13355 }
13356 else if (*p == '(')
13357 {
13358 /* It's a function-like macro. */
13359 char *name = copy_string (body, p - body);
13360 int argc = 0;
13361 int argv_size = 1;
13362 char **argv = xmalloc (argv_size * sizeof (*argv));
13363
13364 p++;
13365
13366 p = consume_improper_spaces (p, body);
13367
13368 /* Parse the formal argument list. */
13369 while (*p && *p != ')')
13370 {
13371 /* Find the extent of the current argument name. */
13372 const char *arg_start = p;
13373
13374 while (*p && *p != ',' && *p != ')' && *p != ' ')
13375 p++;
13376
13377 if (! *p || p == arg_start)
4d3c2250 13378 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13379 else
13380 {
13381 /* Make sure argv has room for the new argument. */
13382 if (argc >= argv_size)
13383 {
13384 argv_size *= 2;
13385 argv = xrealloc (argv, argv_size * sizeof (*argv));
13386 }
13387
13388 argv[argc++] = copy_string (arg_start, p - arg_start);
13389 }
13390
13391 p = consume_improper_spaces (p, body);
13392
13393 /* Consume the comma, if present. */
13394 if (*p == ',')
13395 {
13396 p++;
13397
13398 p = consume_improper_spaces (p, body);
13399 }
13400 }
13401
13402 if (*p == ')')
13403 {
13404 p++;
13405
13406 if (*p == ' ')
13407 /* Perfectly formed definition, no complaints. */
13408 macro_define_function (file, line, name,
6e70227d 13409 argc, (const char **) argv,
2e276125
JB
13410 p + 1);
13411 else if (*p == '\0')
13412 {
13413 /* Complain, but do define it. */
4d3c2250 13414 dwarf2_macro_malformed_definition_complaint (body);
2e276125 13415 macro_define_function (file, line, name,
6e70227d 13416 argc, (const char **) argv,
2e276125
JB
13417 p);
13418 }
13419 else
13420 /* Just complain. */
4d3c2250 13421 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13422 }
13423 else
13424 /* Just complain. */
4d3c2250 13425 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13426
13427 xfree (name);
13428 {
13429 int i;
13430
13431 for (i = 0; i < argc; i++)
13432 xfree (argv[i]);
13433 }
13434 xfree (argv);
13435 }
13436 else
4d3c2250 13437 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13438}
13439
13440
13441static void
13442dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13443 char *comp_dir, bfd *abfd,
e7c27a73 13444 struct dwarf2_cu *cu)
2e276125 13445{
fe1b8b76 13446 gdb_byte *mac_ptr, *mac_end;
2e276125 13447 struct macro_source_file *current_file = 0;
757a13d0
JK
13448 enum dwarf_macinfo_record_type macinfo_type;
13449 int at_commandline;
2e276125 13450
be391dca
TT
13451 dwarf2_read_section (dwarf2_per_objfile->objfile,
13452 &dwarf2_per_objfile->macinfo);
dce234bc 13453 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 13454 {
e2e0b3e5 13455 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
13456 return;
13457 }
13458
757a13d0
JK
13459 /* First pass: Find the name of the base filename.
13460 This filename is needed in order to process all macros whose definition
13461 (or undefinition) comes from the command line. These macros are defined
13462 before the first DW_MACINFO_start_file entry, and yet still need to be
13463 associated to the base file.
13464
13465 To determine the base file name, we scan the macro definitions until we
13466 reach the first DW_MACINFO_start_file entry. We then initialize
13467 CURRENT_FILE accordingly so that any macro definition found before the
13468 first DW_MACINFO_start_file can still be associated to the base file. */
13469
dce234bc
PP
13470 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13471 mac_end = dwarf2_per_objfile->macinfo.buffer
13472 + dwarf2_per_objfile->macinfo.size;
2e276125 13473
757a13d0 13474 do
2e276125 13475 {
2e276125
JB
13476 /* Do we at least have room for a macinfo type byte? */
13477 if (mac_ptr >= mac_end)
13478 {
757a13d0
JK
13479 /* Complaint is printed during the second pass as GDB will probably
13480 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13481 break;
2e276125
JB
13482 }
13483
13484 macinfo_type = read_1_byte (abfd, mac_ptr);
13485 mac_ptr++;
13486
13487 switch (macinfo_type)
13488 {
13489 /* A zero macinfo type indicates the end of the macro
13490 information. */
13491 case 0:
757a13d0
JK
13492 break;
13493
13494 case DW_MACINFO_define:
13495 case DW_MACINFO_undef:
13496 /* Only skip the data by MAC_PTR. */
13497 {
13498 unsigned int bytes_read;
13499
13500 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13501 mac_ptr += bytes_read;
9b1c24c8 13502 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13503 mac_ptr += bytes_read;
13504 }
13505 break;
13506
13507 case DW_MACINFO_start_file:
13508 {
13509 unsigned int bytes_read;
13510 int line, file;
13511
13512 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13513 mac_ptr += bytes_read;
13514 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13515 mac_ptr += bytes_read;
13516
13517 current_file = macro_start_file (file, line, current_file, comp_dir,
13518 lh, cu->objfile);
13519 }
13520 break;
13521
13522 case DW_MACINFO_end_file:
13523 /* No data to skip by MAC_PTR. */
13524 break;
13525
13526 case DW_MACINFO_vendor_ext:
13527 /* Only skip the data by MAC_PTR. */
13528 {
13529 unsigned int bytes_read;
13530
13531 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13532 mac_ptr += bytes_read;
9b1c24c8 13533 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13534 mac_ptr += bytes_read;
13535 }
13536 break;
13537
13538 default:
13539 break;
13540 }
13541 } while (macinfo_type != 0 && current_file == NULL);
13542
13543 /* Second pass: Process all entries.
13544
13545 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13546 command-line macro definitions/undefinitions. This flag is unset when we
13547 reach the first DW_MACINFO_start_file entry. */
13548
dce234bc 13549 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
13550
13551 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13552 GDB is still reading the definitions from command line. First
13553 DW_MACINFO_start_file will need to be ignored as it was already executed
13554 to create CURRENT_FILE for the main source holding also the command line
13555 definitions. On first met DW_MACINFO_start_file this flag is reset to
13556 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13557
13558 at_commandline = 1;
13559
13560 do
13561 {
13562 /* Do we at least have room for a macinfo type byte? */
13563 if (mac_ptr >= mac_end)
13564 {
13565 dwarf2_macros_too_long_complaint ();
13566 break;
13567 }
13568
13569 macinfo_type = read_1_byte (abfd, mac_ptr);
13570 mac_ptr++;
13571
13572 switch (macinfo_type)
13573 {
13574 /* A zero macinfo type indicates the end of the macro
13575 information. */
13576 case 0:
13577 break;
2e276125
JB
13578
13579 case DW_MACINFO_define:
13580 case DW_MACINFO_undef:
13581 {
891d2f0b 13582 unsigned int bytes_read;
2e276125
JB
13583 int line;
13584 char *body;
13585
13586 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13587 mac_ptr += bytes_read;
9b1c24c8 13588 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13589 mac_ptr += bytes_read;
13590
13591 if (! current_file)
757a13d0
JK
13592 {
13593 /* DWARF violation as no main source is present. */
13594 complaint (&symfile_complaints,
13595 _("debug info with no main source gives macro %s "
13596 "on line %d: %s"),
6e70227d
DE
13597 macinfo_type == DW_MACINFO_define ?
13598 _("definition") :
905e0470
PM
13599 macinfo_type == DW_MACINFO_undef ?
13600 _("undefinition") :
13601 _("something-or-other"), line, body);
757a13d0
JK
13602 break;
13603 }
13604 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 13605 complaint (&symfile_complaints,
757a13d0
JK
13606 _("debug info gives %s macro %s with %s line %d: %s"),
13607 at_commandline ? _("command-line") : _("in-file"),
905e0470 13608 macinfo_type == DW_MACINFO_define ?
6e70227d 13609 _("definition") :
905e0470
PM
13610 macinfo_type == DW_MACINFO_undef ?
13611 _("undefinition") :
13612 _("something-or-other"),
757a13d0
JK
13613 line == 0 ? _("zero") : _("non-zero"), line, body);
13614
13615 if (macinfo_type == DW_MACINFO_define)
13616 parse_macro_definition (current_file, line, body);
13617 else if (macinfo_type == DW_MACINFO_undef)
13618 macro_undef (current_file, line, body);
2e276125
JB
13619 }
13620 break;
13621
13622 case DW_MACINFO_start_file:
13623 {
891d2f0b 13624 unsigned int bytes_read;
2e276125
JB
13625 int line, file;
13626
13627 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13628 mac_ptr += bytes_read;
13629 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13630 mac_ptr += bytes_read;
13631
757a13d0
JK
13632 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13633 complaint (&symfile_complaints,
13634 _("debug info gives source %d included "
13635 "from %s at %s line %d"),
13636 file, at_commandline ? _("command-line") : _("file"),
13637 line == 0 ? _("zero") : _("non-zero"), line);
13638
13639 if (at_commandline)
13640 {
13641 /* This DW_MACINFO_start_file was executed in the pass one. */
13642 at_commandline = 0;
13643 }
13644 else
13645 current_file = macro_start_file (file, line,
13646 current_file, comp_dir,
13647 lh, cu->objfile);
2e276125
JB
13648 }
13649 break;
13650
13651 case DW_MACINFO_end_file:
13652 if (! current_file)
4d3c2250 13653 complaint (&symfile_complaints,
e2e0b3e5 13654 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
13655 else
13656 {
13657 current_file = current_file->included_by;
13658 if (! current_file)
13659 {
13660 enum dwarf_macinfo_record_type next_type;
13661
13662 /* GCC circa March 2002 doesn't produce the zero
13663 type byte marking the end of the compilation
13664 unit. Complain if it's not there, but exit no
13665 matter what. */
13666
13667 /* Do we at least have room for a macinfo type byte? */
13668 if (mac_ptr >= mac_end)
13669 {
4d3c2250 13670 dwarf2_macros_too_long_complaint ();
2e276125
JB
13671 return;
13672 }
13673
13674 /* We don't increment mac_ptr here, so this is just
13675 a look-ahead. */
13676 next_type = read_1_byte (abfd, mac_ptr);
13677 if (next_type != 0)
4d3c2250 13678 complaint (&symfile_complaints,
e2e0b3e5 13679 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
13680
13681 return;
13682 }
13683 }
13684 break;
13685
13686 case DW_MACINFO_vendor_ext:
13687 {
891d2f0b 13688 unsigned int bytes_read;
2e276125
JB
13689 int constant;
13690 char *string;
13691
13692 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13693 mac_ptr += bytes_read;
9b1c24c8 13694 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13695 mac_ptr += bytes_read;
13696
13697 /* We don't recognize any vendor extensions. */
13698 }
13699 break;
13700 }
757a13d0 13701 } while (macinfo_type != 0);
2e276125 13702}
8e19ed76
PS
13703
13704/* Check if the attribute's form is a DW_FORM_block*
13705 if so return true else false. */
13706static int
13707attr_form_is_block (struct attribute *attr)
13708{
13709 return (attr == NULL ? 0 :
13710 attr->form == DW_FORM_block1
13711 || attr->form == DW_FORM_block2
13712 || attr->form == DW_FORM_block4
2dc7f7b3
TT
13713 || attr->form == DW_FORM_block
13714 || attr->form == DW_FORM_exprloc);
8e19ed76 13715}
4c2df51b 13716
c6a0999f
JB
13717/* Return non-zero if ATTR's value is a section offset --- classes
13718 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
13719 You may use DW_UNSND (attr) to retrieve such offsets.
13720
13721 Section 7.5.4, "Attribute Encodings", explains that no attribute
13722 may have a value that belongs to more than one of these classes; it
13723 would be ambiguous if we did, because we use the same forms for all
13724 of them. */
3690dd37
JB
13725static int
13726attr_form_is_section_offset (struct attribute *attr)
13727{
13728 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
13729 || attr->form == DW_FORM_data8
13730 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
13731}
13732
13733
13734/* Return non-zero if ATTR's value falls in the 'constant' class, or
13735 zero otherwise. When this function returns true, you can apply
13736 dwarf2_get_attr_constant_value to it.
13737
13738 However, note that for some attributes you must check
13739 attr_form_is_section_offset before using this test. DW_FORM_data4
13740 and DW_FORM_data8 are members of both the constant class, and of
13741 the classes that contain offsets into other debug sections
13742 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
13743 that, if an attribute's can be either a constant or one of the
13744 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
13745 taken as section offsets, not constants. */
13746static int
13747attr_form_is_constant (struct attribute *attr)
13748{
13749 switch (attr->form)
13750 {
13751 case DW_FORM_sdata:
13752 case DW_FORM_udata:
13753 case DW_FORM_data1:
13754 case DW_FORM_data2:
13755 case DW_FORM_data4:
13756 case DW_FORM_data8:
13757 return 1;
13758 default:
13759 return 0;
13760 }
13761}
13762
4c2df51b
DJ
13763static void
13764dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 13765 struct dwarf2_cu *cu)
4c2df51b 13766{
3690dd37 13767 if (attr_form_is_section_offset (attr)
99bcc461
DJ
13768 /* ".debug_loc" may not exist at all, or the offset may be outside
13769 the section. If so, fall through to the complaint in the
13770 other branch. */
dce234bc 13771 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 13772 {
0d53c4c4 13773 struct dwarf2_loclist_baton *baton;
4c2df51b 13774
4a146b47 13775 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13776 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
13777 baton->per_cu = cu->per_cu;
13778 gdb_assert (baton->per_cu);
4c2df51b 13779
be391dca
TT
13780 dwarf2_read_section (dwarf2_per_objfile->objfile,
13781 &dwarf2_per_objfile->loc);
13782
0d53c4c4
DJ
13783 /* We don't know how long the location list is, but make sure we
13784 don't run off the edge of the section. */
dce234bc
PP
13785 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13786 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
13787 baton->base_address = cu->base_address;
13788 if (cu->base_known == 0)
0d53c4c4 13789 complaint (&symfile_complaints,
e2e0b3e5 13790 _("Location list used without specifying the CU base address."));
4c2df51b 13791
768a979c 13792 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
13793 SYMBOL_LOCATION_BATON (sym) = baton;
13794 }
13795 else
13796 {
13797 struct dwarf2_locexpr_baton *baton;
13798
4a146b47 13799 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13800 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
13801 baton->per_cu = cu->per_cu;
13802 gdb_assert (baton->per_cu);
0d53c4c4
DJ
13803
13804 if (attr_form_is_block (attr))
13805 {
13806 /* Note that we're just copying the block's data pointer
13807 here, not the actual data. We're still pointing into the
6502dd73
DJ
13808 info_buffer for SYM's objfile; right now we never release
13809 that buffer, but when we do clean up properly this may
13810 need to change. */
0d53c4c4
DJ
13811 baton->size = DW_BLOCK (attr)->size;
13812 baton->data = DW_BLOCK (attr)->data;
13813 }
13814 else
13815 {
13816 dwarf2_invalid_attrib_class_complaint ("location description",
13817 SYMBOL_NATURAL_NAME (sym));
13818 baton->size = 0;
13819 baton->data = NULL;
13820 }
6e70227d 13821
768a979c 13822 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
13823 SYMBOL_LOCATION_BATON (sym) = baton;
13824 }
4c2df51b 13825}
6502dd73 13826
9aa1f1e3
TT
13827/* Return the OBJFILE associated with the compilation unit CU. If CU
13828 came from a separate debuginfo file, then the master objfile is
13829 returned. */
ae0d2f24
UW
13830
13831struct objfile *
13832dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13833{
9291a0cd 13834 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13835
13836 /* Return the master objfile, so that we can report and look up the
13837 correct file containing this variable. */
13838 if (objfile->separate_debug_objfile_backlink)
13839 objfile = objfile->separate_debug_objfile_backlink;
13840
13841 return objfile;
13842}
13843
13844/* Return the address size given in the compilation unit header for CU. */
13845
13846CORE_ADDR
13847dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
13848{
13849 if (per_cu->cu)
13850 return per_cu->cu->header.addr_size;
13851 else
13852 {
13853 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 13854 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13855 struct dwarf2_per_objfile *per_objfile
13856 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 13857 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 13858 struct comp_unit_head cu_header;
9a619af0 13859
ae0d2f24
UW
13860 memset (&cu_header, 0, sizeof cu_header);
13861 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13862 return cu_header.addr_size;
13863 }
13864}
13865
9eae7c52
TT
13866/* Return the offset size given in the compilation unit header for CU. */
13867
13868int
13869dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
13870{
13871 if (per_cu->cu)
13872 return per_cu->cu->header.offset_size;
13873 else
13874 {
13875 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 13876 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
13877 struct dwarf2_per_objfile *per_objfile
13878 = objfile_data (objfile, dwarf2_objfile_data_key);
13879 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13880 struct comp_unit_head cu_header;
13881
13882 memset (&cu_header, 0, sizeof cu_header);
13883 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13884 return cu_header.offset_size;
13885 }
13886}
13887
9aa1f1e3
TT
13888/* Return the text offset of the CU. The returned offset comes from
13889 this CU's objfile. If this objfile came from a separate debuginfo
13890 file, then the offset may be different from the corresponding
13891 offset in the parent objfile. */
13892
13893CORE_ADDR
13894dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
13895{
bb3fa9d0 13896 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
13897
13898 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13899}
13900
348e048f
DE
13901/* Locate the .debug_info compilation unit from CU's objfile which contains
13902 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
13903
13904static struct dwarf2_per_cu_data *
c764a876 13905dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
13906 struct objfile *objfile)
13907{
13908 struct dwarf2_per_cu_data *this_cu;
13909 int low, high;
13910
ae038cb0
DJ
13911 low = 0;
13912 high = dwarf2_per_objfile->n_comp_units - 1;
13913 while (high > low)
13914 {
13915 int mid = low + (high - low) / 2;
9a619af0 13916
ae038cb0
DJ
13917 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
13918 high = mid;
13919 else
13920 low = mid + 1;
13921 }
13922 gdb_assert (low == high);
13923 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
13924 {
10b3939b 13925 if (low == 0)
8a3fe4f8
AC
13926 error (_("Dwarf Error: could not find partial DIE containing "
13927 "offset 0x%lx [in module %s]"),
10b3939b
DJ
13928 (long) offset, bfd_get_filename (objfile->obfd));
13929
ae038cb0
DJ
13930 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
13931 return dwarf2_per_objfile->all_comp_units[low-1];
13932 }
13933 else
13934 {
13935 this_cu = dwarf2_per_objfile->all_comp_units[low];
13936 if (low == dwarf2_per_objfile->n_comp_units - 1
13937 && offset >= this_cu->offset + this_cu->length)
c764a876 13938 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
13939 gdb_assert (offset < this_cu->offset + this_cu->length);
13940 return this_cu;
13941 }
13942}
13943
10b3939b
DJ
13944/* Locate the compilation unit from OBJFILE which is located at exactly
13945 OFFSET. Raises an error on failure. */
13946
ae038cb0 13947static struct dwarf2_per_cu_data *
c764a876 13948dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
13949{
13950 struct dwarf2_per_cu_data *this_cu;
9a619af0 13951
ae038cb0
DJ
13952 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
13953 if (this_cu->offset != offset)
c764a876 13954 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
13955 return this_cu;
13956}
13957
93311388
DE
13958/* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
13959
13960static struct dwarf2_cu *
13961alloc_one_comp_unit (struct objfile *objfile)
13962{
13963 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
13964 cu->objfile = objfile;
13965 obstack_init (&cu->comp_unit_obstack);
13966 return cu;
13967}
13968
ae038cb0
DJ
13969/* Release one cached compilation unit, CU. We unlink it from the tree
13970 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
13971 the caller is responsible for that.
13972 NOTE: DATA is a void * because this function is also used as a
13973 cleanup routine. */
ae038cb0
DJ
13974
13975static void
13976free_one_comp_unit (void *data)
13977{
13978 struct dwarf2_cu *cu = data;
13979
13980 if (cu->per_cu != NULL)
13981 cu->per_cu->cu = NULL;
13982 cu->per_cu = NULL;
13983
13984 obstack_free (&cu->comp_unit_obstack, NULL);
13985
13986 xfree (cu);
13987}
13988
72bf9492 13989/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
13990 when we're finished with it. We can't free the pointer itself, but be
13991 sure to unlink it from the cache. Also release any associated storage
13992 and perform cache maintenance.
72bf9492
DJ
13993
13994 Only used during partial symbol parsing. */
13995
13996static void
13997free_stack_comp_unit (void *data)
13998{
13999 struct dwarf2_cu *cu = data;
14000
14001 obstack_free (&cu->comp_unit_obstack, NULL);
14002 cu->partial_dies = NULL;
ae038cb0
DJ
14003
14004 if (cu->per_cu != NULL)
14005 {
14006 /* This compilation unit is on the stack in our caller, so we
14007 should not xfree it. Just unlink it. */
14008 cu->per_cu->cu = NULL;
14009 cu->per_cu = NULL;
14010
14011 /* If we had a per-cu pointer, then we may have other compilation
14012 units loaded, so age them now. */
14013 age_cached_comp_units ();
14014 }
14015}
14016
14017/* Free all cached compilation units. */
14018
14019static void
14020free_cached_comp_units (void *data)
14021{
14022 struct dwarf2_per_cu_data *per_cu, **last_chain;
14023
14024 per_cu = dwarf2_per_objfile->read_in_chain;
14025 last_chain = &dwarf2_per_objfile->read_in_chain;
14026 while (per_cu != NULL)
14027 {
14028 struct dwarf2_per_cu_data *next_cu;
14029
14030 next_cu = per_cu->cu->read_in_chain;
14031
14032 free_one_comp_unit (per_cu->cu);
14033 *last_chain = next_cu;
14034
14035 per_cu = next_cu;
14036 }
14037}
14038
14039/* Increase the age counter on each cached compilation unit, and free
14040 any that are too old. */
14041
14042static void
14043age_cached_comp_units (void)
14044{
14045 struct dwarf2_per_cu_data *per_cu, **last_chain;
14046
14047 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14048 per_cu = dwarf2_per_objfile->read_in_chain;
14049 while (per_cu != NULL)
14050 {
14051 per_cu->cu->last_used ++;
14052 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14053 dwarf2_mark (per_cu->cu);
14054 per_cu = per_cu->cu->read_in_chain;
14055 }
14056
14057 per_cu = dwarf2_per_objfile->read_in_chain;
14058 last_chain = &dwarf2_per_objfile->read_in_chain;
14059 while (per_cu != NULL)
14060 {
14061 struct dwarf2_per_cu_data *next_cu;
14062
14063 next_cu = per_cu->cu->read_in_chain;
14064
14065 if (!per_cu->cu->mark)
14066 {
14067 free_one_comp_unit (per_cu->cu);
14068 *last_chain = next_cu;
14069 }
14070 else
14071 last_chain = &per_cu->cu->read_in_chain;
14072
14073 per_cu = next_cu;
14074 }
14075}
14076
14077/* Remove a single compilation unit from the cache. */
14078
14079static void
14080free_one_cached_comp_unit (void *target_cu)
14081{
14082 struct dwarf2_per_cu_data *per_cu, **last_chain;
14083
14084 per_cu = dwarf2_per_objfile->read_in_chain;
14085 last_chain = &dwarf2_per_objfile->read_in_chain;
14086 while (per_cu != NULL)
14087 {
14088 struct dwarf2_per_cu_data *next_cu;
14089
14090 next_cu = per_cu->cu->read_in_chain;
14091
14092 if (per_cu->cu == target_cu)
14093 {
14094 free_one_comp_unit (per_cu->cu);
14095 *last_chain = next_cu;
14096 break;
14097 }
14098 else
14099 last_chain = &per_cu->cu->read_in_chain;
14100
14101 per_cu = next_cu;
14102 }
14103}
14104
fe3e1990
DJ
14105/* Release all extra memory associated with OBJFILE. */
14106
14107void
14108dwarf2_free_objfile (struct objfile *objfile)
14109{
14110 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14111
14112 if (dwarf2_per_objfile == NULL)
14113 return;
14114
14115 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14116 free_cached_comp_units (NULL);
14117
9291a0cd
TT
14118 if (dwarf2_per_objfile->using_index)
14119 {
14120 int i;
14121
14122 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14123 {
14124 int j;
14125 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14126
14127 if (!cu->v.quick->lines)
14128 continue;
14129
14130 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
14131 {
14132 if (cu->v.quick->file_names)
14133 xfree ((void *) cu->v.quick->file_names[j]);
14134 if (cu->v.quick->full_names)
14135 xfree ((void *) cu->v.quick->full_names[j]);
14136 }
14137
14138 free_line_header (cu->v.quick->lines);
14139 }
14140 }
14141
fe3e1990
DJ
14142 /* Everything else should be on the objfile obstack. */
14143}
14144
1c379e20
DJ
14145/* A pair of DIE offset and GDB type pointer. We store these
14146 in a hash table separate from the DIEs, and preserve them
14147 when the DIEs are flushed out of cache. */
14148
14149struct dwarf2_offset_and_type
14150{
14151 unsigned int offset;
14152 struct type *type;
14153};
14154
14155/* Hash function for a dwarf2_offset_and_type. */
14156
14157static hashval_t
14158offset_and_type_hash (const void *item)
14159{
14160 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14161
1c379e20
DJ
14162 return ofs->offset;
14163}
14164
14165/* Equality function for a dwarf2_offset_and_type. */
14166
14167static int
14168offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14169{
14170 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14171 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14172
1c379e20
DJ
14173 return ofs_lhs->offset == ofs_rhs->offset;
14174}
14175
14176/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14177 table if necessary. For convenience, return TYPE.
14178
14179 The DIEs reading must have careful ordering to:
14180 * Not cause infite loops trying to read in DIEs as a prerequisite for
14181 reading current DIE.
14182 * Not trying to dereference contents of still incompletely read in types
14183 while reading in other DIEs.
14184 * Enable referencing still incompletely read in types just by a pointer to
14185 the type without accessing its fields.
14186
14187 Therefore caller should follow these rules:
14188 * Try to fetch any prerequisite types we may need to build this DIE type
14189 before building the type and calling set_die_type.
e71ec853 14190 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14191 possible before fetching more types to complete the current type.
14192 * Make the type as complete as possible before fetching more types. */
1c379e20 14193
f792889a 14194static struct type *
1c379e20
DJ
14195set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14196{
14197 struct dwarf2_offset_and_type **slot, ofs;
14198
b4ba55a1
JB
14199 /* For Ada types, make sure that the gnat-specific data is always
14200 initialized (if not already set). There are a few types where
14201 we should not be doing so, because the type-specific area is
14202 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14203 where the type-specific area is used to store the floatformat).
14204 But this is not a problem, because the gnat-specific information
14205 is actually not needed for these types. */
14206 if (need_gnat_info (cu)
14207 && TYPE_CODE (type) != TYPE_CODE_FUNC
14208 && TYPE_CODE (type) != TYPE_CODE_FLT
14209 && !HAVE_GNAT_AUX_INFO (type))
14210 INIT_GNAT_SPECIFIC (type);
14211
f792889a
DJ
14212 if (cu->type_hash == NULL)
14213 {
14214 gdb_assert (cu->per_cu != NULL);
14215 cu->per_cu->type_hash
14216 = htab_create_alloc_ex (cu->header.length / 24,
14217 offset_and_type_hash,
14218 offset_and_type_eq,
14219 NULL,
14220 &cu->objfile->objfile_obstack,
14221 hashtab_obstack_allocate,
14222 dummy_obstack_deallocate);
14223 cu->type_hash = cu->per_cu->type_hash;
14224 }
1c379e20
DJ
14225
14226 ofs.offset = die->offset;
14227 ofs.type = type;
14228 slot = (struct dwarf2_offset_and_type **)
f792889a 14229 htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
7e314c57
JK
14230 if (*slot)
14231 complaint (&symfile_complaints,
14232 _("A problem internal to GDB: DIE 0x%x has type already set"),
14233 die->offset);
1c379e20
DJ
14234 *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
14235 **slot = ofs;
f792889a 14236 return type;
1c379e20
DJ
14237}
14238
f792889a
DJ
14239/* Find the type for DIE in CU's type_hash, or return NULL if DIE does
14240 not have a saved type. */
1c379e20
DJ
14241
14242static struct type *
f792889a 14243get_die_type (struct die_info *die, struct dwarf2_cu *cu)
1c379e20
DJ
14244{
14245 struct dwarf2_offset_and_type *slot, ofs;
f792889a
DJ
14246 htab_t type_hash = cu->type_hash;
14247
14248 if (type_hash == NULL)
14249 return NULL;
1c379e20
DJ
14250
14251 ofs.offset = die->offset;
14252 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14253 if (slot)
14254 return slot->type;
14255 else
14256 return NULL;
14257}
14258
10b3939b
DJ
14259/* Add a dependence relationship from CU to REF_PER_CU. */
14260
14261static void
14262dwarf2_add_dependence (struct dwarf2_cu *cu,
14263 struct dwarf2_per_cu_data *ref_per_cu)
14264{
14265 void **slot;
14266
14267 if (cu->dependencies == NULL)
14268 cu->dependencies
14269 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14270 NULL, &cu->comp_unit_obstack,
14271 hashtab_obstack_allocate,
14272 dummy_obstack_deallocate);
14273
14274 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14275 if (*slot == NULL)
14276 *slot = ref_per_cu;
14277}
1c379e20 14278
f504f079
DE
14279/* Subroutine of dwarf2_mark to pass to htab_traverse.
14280 Set the mark field in every compilation unit in the
ae038cb0
DJ
14281 cache that we must keep because we are keeping CU. */
14282
10b3939b
DJ
14283static int
14284dwarf2_mark_helper (void **slot, void *data)
14285{
14286 struct dwarf2_per_cu_data *per_cu;
14287
14288 per_cu = (struct dwarf2_per_cu_data *) *slot;
14289 if (per_cu->cu->mark)
14290 return 1;
14291 per_cu->cu->mark = 1;
14292
14293 if (per_cu->cu->dependencies != NULL)
14294 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14295
14296 return 1;
14297}
14298
f504f079
DE
14299/* Set the mark field in CU and in every other compilation unit in the
14300 cache that we must keep because we are keeping CU. */
14301
ae038cb0
DJ
14302static void
14303dwarf2_mark (struct dwarf2_cu *cu)
14304{
14305 if (cu->mark)
14306 return;
14307 cu->mark = 1;
10b3939b
DJ
14308 if (cu->dependencies != NULL)
14309 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14310}
14311
14312static void
14313dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14314{
14315 while (per_cu)
14316 {
14317 per_cu->cu->mark = 0;
14318 per_cu = per_cu->cu->read_in_chain;
14319 }
72bf9492
DJ
14320}
14321
72bf9492
DJ
14322/* Trivial hash function for partial_die_info: the hash value of a DIE
14323 is its offset in .debug_info for this objfile. */
14324
14325static hashval_t
14326partial_die_hash (const void *item)
14327{
14328 const struct partial_die_info *part_die = item;
9a619af0 14329
72bf9492
DJ
14330 return part_die->offset;
14331}
14332
14333/* Trivial comparison function for partial_die_info structures: two DIEs
14334 are equal if they have the same offset. */
14335
14336static int
14337partial_die_eq (const void *item_lhs, const void *item_rhs)
14338{
14339 const struct partial_die_info *part_die_lhs = item_lhs;
14340 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14341
72bf9492
DJ
14342 return part_die_lhs->offset == part_die_rhs->offset;
14343}
14344
ae038cb0
DJ
14345static struct cmd_list_element *set_dwarf2_cmdlist;
14346static struct cmd_list_element *show_dwarf2_cmdlist;
14347
14348static void
14349set_dwarf2_cmd (char *args, int from_tty)
14350{
14351 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14352}
14353
14354static void
14355show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14356{
ae038cb0
DJ
14357 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14358}
14359
dce234bc
PP
14360/* If section described by INFO was mmapped, munmap it now. */
14361
14362static void
14363munmap_section_buffer (struct dwarf2_section_info *info)
14364{
14365 if (info->was_mmapped)
14366 {
14367#ifdef HAVE_MMAP
14368 intptr_t begin = (intptr_t) info->buffer;
14369 intptr_t map_begin = begin & ~(pagesize - 1);
14370 size_t map_length = info->size + begin - map_begin;
9a619af0 14371
dce234bc
PP
14372 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14373#else
14374 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 14375 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
14376#endif
14377 }
14378}
14379
14380/* munmap debug sections for OBJFILE, if necessary. */
14381
14382static void
c1bd65d0 14383dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
14384{
14385 struct dwarf2_per_objfile *data = d;
9a619af0 14386
dce234bc
PP
14387 munmap_section_buffer (&data->info);
14388 munmap_section_buffer (&data->abbrev);
14389 munmap_section_buffer (&data->line);
14390 munmap_section_buffer (&data->str);
14391 munmap_section_buffer (&data->macinfo);
14392 munmap_section_buffer (&data->ranges);
14393 munmap_section_buffer (&data->loc);
14394 munmap_section_buffer (&data->frame);
14395 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
14396 munmap_section_buffer (&data->gdb_index);
14397}
14398
14399\f
14400
14401/* The contents of the hash table we create when building the string
14402 table. */
14403struct strtab_entry
14404{
14405 offset_type offset;
14406 const char *str;
14407};
14408
14409/* Hash function for a strtab_entry. */
14410static hashval_t
14411hash_strtab_entry (const void *e)
14412{
14413 const struct strtab_entry *entry = e;
14414 return mapped_index_string_hash (entry->str);
14415}
14416
14417/* Equality function for a strtab_entry. */
14418static int
14419eq_strtab_entry (const void *a, const void *b)
14420{
14421 const struct strtab_entry *ea = a;
14422 const struct strtab_entry *eb = b;
14423 return !strcmp (ea->str, eb->str);
14424}
14425
14426/* Create a strtab_entry hash table. */
14427static htab_t
14428create_strtab (void)
14429{
14430 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14431 xfree, xcalloc, xfree);
14432}
14433
14434/* Add a string to the constant pool. Return the string's offset in
14435 host order. */
14436static offset_type
14437add_string (htab_t table, struct obstack *cpool, const char *str)
14438{
14439 void **slot;
14440 struct strtab_entry entry;
14441 struct strtab_entry *result;
14442
14443 entry.str = str;
14444 slot = htab_find_slot (table, &entry, INSERT);
14445 if (*slot)
14446 result = *slot;
14447 else
14448 {
14449 result = XNEW (struct strtab_entry);
14450 result->offset = obstack_object_size (cpool);
14451 result->str = str;
14452 obstack_grow_str0 (cpool, str);
14453 *slot = result;
14454 }
14455 return result->offset;
14456}
14457
14458/* An entry in the symbol table. */
14459struct symtab_index_entry
14460{
14461 /* The name of the symbol. */
14462 const char *name;
14463 /* The offset of the name in the constant pool. */
14464 offset_type index_offset;
14465 /* A sorted vector of the indices of all the CUs that hold an object
14466 of this name. */
14467 VEC (offset_type) *cu_indices;
14468};
14469
14470/* The symbol table. This is a power-of-2-sized hash table. */
14471struct mapped_symtab
14472{
14473 offset_type n_elements;
14474 offset_type size;
14475 struct symtab_index_entry **data;
14476};
14477
14478/* Hash function for a symtab_index_entry. */
14479static hashval_t
14480hash_symtab_entry (const void *e)
14481{
14482 const struct symtab_index_entry *entry = e;
14483 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14484 sizeof (offset_type) * VEC_length (offset_type,
14485 entry->cu_indices),
14486 0);
14487}
14488
14489/* Equality function for a symtab_index_entry. */
14490static int
14491eq_symtab_entry (const void *a, const void *b)
14492{
14493 const struct symtab_index_entry *ea = a;
14494 const struct symtab_index_entry *eb = b;
14495 int len = VEC_length (offset_type, ea->cu_indices);
14496 if (len != VEC_length (offset_type, eb->cu_indices))
14497 return 0;
14498 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14499 VEC_address (offset_type, eb->cu_indices),
14500 sizeof (offset_type) * len);
14501}
14502
14503/* Destroy a symtab_index_entry. */
14504static void
14505delete_symtab_entry (void *p)
14506{
14507 struct symtab_index_entry *entry = p;
14508 VEC_free (offset_type, entry->cu_indices);
14509 xfree (entry);
14510}
14511
14512/* Create a hash table holding symtab_index_entry objects. */
14513static htab_t
14514create_index_table (void)
14515{
14516 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14517 delete_symtab_entry, xcalloc, xfree);
14518}
14519
14520/* Create a new mapped symtab object. */
14521static struct mapped_symtab *
14522create_mapped_symtab (void)
14523{
14524 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14525 symtab->n_elements = 0;
14526 symtab->size = 1024;
14527 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14528 return symtab;
14529}
14530
14531/* Destroy a mapped_symtab. */
14532static void
14533cleanup_mapped_symtab (void *p)
14534{
14535 struct mapped_symtab *symtab = p;
14536 /* The contents of the array are freed when the other hash table is
14537 destroyed. */
14538 xfree (symtab->data);
14539 xfree (symtab);
14540}
14541
14542/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14543 the slot. */
14544static struct symtab_index_entry **
14545find_slot (struct mapped_symtab *symtab, const char *name)
14546{
14547 offset_type index, step, hash = mapped_index_string_hash (name);
14548
14549 index = hash & (symtab->size - 1);
14550 step = ((hash * 17) & (symtab->size - 1)) | 1;
14551
14552 for (;;)
14553 {
14554 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14555 return &symtab->data[index];
14556 index = (index + step) & (symtab->size - 1);
14557 }
14558}
14559
14560/* Expand SYMTAB's hash table. */
14561static void
14562hash_expand (struct mapped_symtab *symtab)
14563{
14564 offset_type old_size = symtab->size;
14565 offset_type i;
14566 struct symtab_index_entry **old_entries = symtab->data;
14567
14568 symtab->size *= 2;
14569 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14570
14571 for (i = 0; i < old_size; ++i)
14572 {
14573 if (old_entries[i])
14574 {
14575 struct symtab_index_entry **slot = find_slot (symtab,
14576 old_entries[i]->name);
14577 *slot = old_entries[i];
14578 }
14579 }
14580
14581 xfree (old_entries);
14582}
14583
14584/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
14585 is the index of the CU in which the symbol appears. */
14586static void
14587add_index_entry (struct mapped_symtab *symtab, const char *name,
14588 offset_type cu_index)
14589{
14590 struct symtab_index_entry **slot;
14591
14592 ++symtab->n_elements;
14593 if (4 * symtab->n_elements / 3 >= symtab->size)
14594 hash_expand (symtab);
14595
14596 slot = find_slot (symtab, name);
14597 if (!*slot)
14598 {
14599 *slot = XNEW (struct symtab_index_entry);
14600 (*slot)->name = name;
14601 (*slot)->cu_indices = NULL;
14602 }
14603 /* Don't push an index twice. Due to how we add entries we only
14604 have to check the last one. */
14605 if (VEC_empty (offset_type, (*slot)->cu_indices)
14606 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14607 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14608}
14609
14610/* Add a vector of indices to the constant pool. */
14611static offset_type
14612add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14613 struct symtab_index_entry *entry)
14614{
14615 void **slot;
14616
14617 slot = htab_find_slot (index_table, entry, INSERT);
14618 if (!*slot)
14619 {
14620 offset_type len = VEC_length (offset_type, entry->cu_indices);
14621 offset_type val = MAYBE_SWAP (len);
14622 offset_type iter;
14623 int i;
14624
14625 *slot = entry;
14626 entry->index_offset = obstack_object_size (cpool);
14627
14628 obstack_grow (cpool, &val, sizeof (val));
14629 for (i = 0;
14630 VEC_iterate (offset_type, entry->cu_indices, i, iter);
14631 ++i)
14632 {
14633 val = MAYBE_SWAP (iter);
14634 obstack_grow (cpool, &val, sizeof (val));
14635 }
14636 }
14637 else
14638 {
14639 struct symtab_index_entry *old_entry = *slot;
14640 entry->index_offset = old_entry->index_offset;
14641 entry = old_entry;
14642 }
14643 return entry->index_offset;
14644}
14645
14646/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14647 constant pool entries going into the obstack CPOOL. */
14648static void
14649write_hash_table (struct mapped_symtab *symtab,
14650 struct obstack *output, struct obstack *cpool)
14651{
14652 offset_type i;
14653 htab_t index_table;
14654 htab_t str_table;
14655
14656 index_table = create_index_table ();
14657 str_table = create_strtab ();
14658 /* We add all the index vectors to the constant pool first, to
14659 ensure alignment is ok. */
14660 for (i = 0; i < symtab->size; ++i)
14661 {
14662 if (symtab->data[i])
14663 add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14664 }
14665
14666 /* Now write out the hash table. */
14667 for (i = 0; i < symtab->size; ++i)
14668 {
14669 offset_type str_off, vec_off;
14670
14671 if (symtab->data[i])
14672 {
14673 str_off = add_string (str_table, cpool, symtab->data[i]->name);
14674 vec_off = symtab->data[i]->index_offset;
14675 }
14676 else
14677 {
14678 /* While 0 is a valid constant pool index, it is not valid
14679 to have 0 for both offsets. */
14680 str_off = 0;
14681 vec_off = 0;
14682 }
14683
14684 str_off = MAYBE_SWAP (str_off);
14685 vec_off = MAYBE_SWAP (vec_off);
14686
14687 obstack_grow (output, &str_off, sizeof (str_off));
14688 obstack_grow (output, &vec_off, sizeof (vec_off));
14689 }
14690
14691 htab_delete (str_table);
14692 htab_delete (index_table);
14693}
14694
14695/* Write an address entry to ADDR_OBSTACK. The addresses are taken
14696 from PST; CU_INDEX is the index of the CU in the vector of all
14697 CUs. */
14698static void
14699add_address_entry (struct objfile *objfile,
14700 struct obstack *addr_obstack, struct partial_symtab *pst,
14701 unsigned int cu_index)
14702{
14703 offset_type offset;
14704 char addr[8];
14705 CORE_ADDR baseaddr;
14706
1fd400ff
TT
14707 /* Don't bother recording empty ranges. */
14708 if (pst->textlow == pst->texthigh)
14709 return;
14710
9291a0cd
TT
14711 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14712
14713 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
14714 obstack_grow (addr_obstack, addr, 8);
14715 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
14716 obstack_grow (addr_obstack, addr, 8);
14717 offset = MAYBE_SWAP (cu_index);
14718 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
14719}
14720
14721/* Add a list of partial symbols to SYMTAB. */
14722static void
14723write_psymbols (struct mapped_symtab *symtab,
14724 struct partial_symbol **psymp,
14725 int count,
14726 offset_type cu_index)
14727{
14728 for (; count-- > 0; ++psymp)
14729 {
14730 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
14731 error (_("Ada is not currently supported by the index"));
14732 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
14733 }
14734}
14735
14736/* Write the contents of an ("unfinished") obstack to FILE. Throw an
14737 exception if there is an error. */
14738static void
14739write_obstack (FILE *file, struct obstack *obstack)
14740{
14741 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
14742 file)
14743 != obstack_object_size (obstack))
14744 error (_("couldn't data write to file"));
14745}
14746
14747/* Unlink a file if the argument is not NULL. */
14748static void
14749unlink_if_set (void *p)
14750{
14751 char **filename = p;
14752 if (*filename)
14753 unlink (*filename);
14754}
14755
1fd400ff
TT
14756/* A helper struct used when iterating over debug_types. */
14757struct signatured_type_index_data
14758{
14759 struct objfile *objfile;
14760 struct mapped_symtab *symtab;
14761 struct obstack *types_list;
14762 int cu_index;
14763};
14764
14765/* A helper function that writes a single signatured_type to an
14766 obstack. */
14767static int
14768write_one_signatured_type (void **slot, void *d)
14769{
14770 struct signatured_type_index_data *info = d;
14771 struct signatured_type *entry = (struct signatured_type *) *slot;
14772 struct dwarf2_per_cu_data *cu = &entry->per_cu;
14773 struct partial_symtab *psymtab = cu->v.psymtab;
14774 gdb_byte val[8];
14775
14776 write_psymbols (info->symtab,
14777 info->objfile->global_psymbols.list + psymtab->globals_offset,
14778 psymtab->n_global_syms, info->cu_index);
14779 write_psymbols (info->symtab,
14780 info->objfile->static_psymbols.list + psymtab->statics_offset,
14781 psymtab->n_static_syms, info->cu_index);
14782
14783 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
14784 obstack_grow (info->types_list, val, 8);
14785 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
14786 obstack_grow (info->types_list, val, 8);
14787 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
14788 obstack_grow (info->types_list, val, 8);
14789
14790 ++info->cu_index;
14791
14792 return 1;
14793}
14794
9291a0cd
TT
14795/* Create an index file for OBJFILE in the directory DIR. */
14796static void
14797write_psymtabs_to_index (struct objfile *objfile, const char *dir)
14798{
14799 struct cleanup *cleanup;
14800 char *filename, *cleanup_filename;
1fd400ff
TT
14801 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
14802 struct obstack cu_list, types_cu_list;
9291a0cd
TT
14803 int i;
14804 FILE *out_file;
14805 struct mapped_symtab *symtab;
14806 offset_type val, size_of_contents, total_len;
14807 struct stat st;
14808 char buf[8];
14809
14810 if (!objfile->psymtabs)
14811 return;
14812 if (dwarf2_per_objfile->using_index)
14813 error (_("Cannot use an index to create the index"));
14814
14815 if (stat (objfile->name, &st) < 0)
14816 perror_with_name (_("Could not stat"));
14817
14818 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
14819 INDEX_SUFFIX, (char *) NULL);
14820 cleanup = make_cleanup (xfree, filename);
14821
14822 out_file = fopen (filename, "wb");
14823 if (!out_file)
14824 error (_("Can't open `%s' for writing"), filename);
14825
14826 cleanup_filename = filename;
14827 make_cleanup (unlink_if_set, &cleanup_filename);
14828
14829 symtab = create_mapped_symtab ();
14830 make_cleanup (cleanup_mapped_symtab, symtab);
14831
14832 obstack_init (&addr_obstack);
14833 make_cleanup_obstack_free (&addr_obstack);
14834
14835 obstack_init (&cu_list);
14836 make_cleanup_obstack_free (&cu_list);
14837
1fd400ff
TT
14838 obstack_init (&types_cu_list);
14839 make_cleanup_obstack_free (&types_cu_list);
14840
14841 /* The list is already sorted, so we don't need to do additional
14842 work here. Also, the debug_types entries do not appear in
14843 all_comp_units, but only in their own hash table. */
9291a0cd
TT
14844 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14845 {
14846 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14847 struct partial_symtab *psymtab = cu->v.psymtab;
14848 gdb_byte val[8];
14849
14850 write_psymbols (symtab,
14851 objfile->global_psymbols.list + psymtab->globals_offset,
14852 psymtab->n_global_syms, i);
14853 write_psymbols (symtab,
14854 objfile->static_psymbols.list + psymtab->statics_offset,
14855 psymtab->n_static_syms, i);
14856
14857 add_address_entry (objfile, &addr_obstack, psymtab, i);
14858
14859 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->offset);
14860 obstack_grow (&cu_list, val, 8);
14861 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->length);
14862 obstack_grow (&cu_list, val, 8);
14863 }
14864
1fd400ff
TT
14865 /* Write out the .debug_type entries, if any. */
14866 if (dwarf2_per_objfile->signatured_types)
14867 {
14868 struct signatured_type_index_data sig_data;
14869
14870 sig_data.objfile = objfile;
14871 sig_data.symtab = symtab;
14872 sig_data.types_list = &types_cu_list;
14873 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
14874 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
14875 write_one_signatured_type, &sig_data);
14876 }
14877
9291a0cd
TT
14878 obstack_init (&constant_pool);
14879 make_cleanup_obstack_free (&constant_pool);
14880 obstack_init (&symtab_obstack);
14881 make_cleanup_obstack_free (&symtab_obstack);
14882 write_hash_table (symtab, &symtab_obstack, &constant_pool);
14883
14884 obstack_init (&contents);
14885 make_cleanup_obstack_free (&contents);
1fd400ff 14886 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
14887 total_len = size_of_contents;
14888
14889 /* The version number. */
1fd400ff 14890 val = MAYBE_SWAP (2);
9291a0cd
TT
14891 obstack_grow (&contents, &val, sizeof (val));
14892
14893 /* The offset of the CU list from the start of the file. */
14894 val = MAYBE_SWAP (total_len);
14895 obstack_grow (&contents, &val, sizeof (val));
14896 total_len += obstack_object_size (&cu_list);
14897
1fd400ff
TT
14898 /* The offset of the types CU list from the start of the file. */
14899 val = MAYBE_SWAP (total_len);
14900 obstack_grow (&contents, &val, sizeof (val));
14901 total_len += obstack_object_size (&types_cu_list);
14902
9291a0cd
TT
14903 /* The offset of the address table from the start of the file. */
14904 val = MAYBE_SWAP (total_len);
14905 obstack_grow (&contents, &val, sizeof (val));
14906 total_len += obstack_object_size (&addr_obstack);
14907
14908 /* The offset of the symbol table from the start of the file. */
14909 val = MAYBE_SWAP (total_len);
14910 obstack_grow (&contents, &val, sizeof (val));
14911 total_len += obstack_object_size (&symtab_obstack);
14912
14913 /* The offset of the constant pool from the start of the file. */
14914 val = MAYBE_SWAP (total_len);
14915 obstack_grow (&contents, &val, sizeof (val));
14916 total_len += obstack_object_size (&constant_pool);
14917
14918 gdb_assert (obstack_object_size (&contents) == size_of_contents);
14919
14920 write_obstack (out_file, &contents);
14921 write_obstack (out_file, &cu_list);
1fd400ff 14922 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
14923 write_obstack (out_file, &addr_obstack);
14924 write_obstack (out_file, &symtab_obstack);
14925 write_obstack (out_file, &constant_pool);
14926
14927 fclose (out_file);
14928
14929 /* We want to keep the file, so we set cleanup_filename to NULL
14930 here. See unlink_if_set. */
14931 cleanup_filename = NULL;
14932
14933 do_cleanups (cleanup);
14934}
14935
14936/* The mapped index file format is designed to be directly mmap()able
14937 on any architecture. In most cases, a datum is represented using a
14938 little-endian 32-bit integer value, called an offset_type. Big
14939 endian machines must byte-swap the values before using them.
14940 Exceptions to this rule are noted. The data is laid out such that
14941 alignment is always respected.
14942
14943 A mapped index consists of several sections.
14944
14945 1. The file header. This is a sequence of values, of offset_type
14946 unless otherwise noted:
1fd400ff
TT
14947 [0] The version number. Currently 1 or 2. The differences are
14948 noted below. Version 1 did not account for .debug_types sections;
14949 the presence of a .debug_types section invalidates any version 1
14950 index that may exist.
9291a0cd 14951 [1] The offset, from the start of the file, of the CU list.
1fd400ff
TT
14952 [1.5] In version 2, the offset, from the start of the file, of the
14953 types CU list. This offset does not appear in version 1. Note
14954 that this can be empty, in which case this offset will be equal to
14955 the next offset.
9291a0cd
TT
14956 [2] The offset, from the start of the file, of the address section.
14957 [3] The offset, from the start of the file, of the symbol table.
14958 [4] The offset, from the start of the file, of the constant pool.
14959
14960 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
14961 little-endian values, sorted by the CU offset. The first element
14962 in each pair is the offset of a CU in the .debug_info section. The
14963 second element in each pair is the length of that CU. References
14964 to a CU elsewhere in the map are done using a CU index, which is
14965 just the 0-based index into this table. Note that if there are
14966 type CUs, then conceptually CUs and type CUs form a single list for
14967 the purposes of CU indices.
14968
14969 2.5 The types CU list. This does not appear in a version 1 index.
14970 This is a sequence of triplets of 64-bit little-endian values. In
14971 a triplet, the first value is the CU offset, the second value is
14972 the type offset in the CU, and the third value is the type
14973 signature. The types CU list is not sorted.
9291a0cd
TT
14974
14975 3. The address section. The address section consists of a sequence
14976 of address entries. Each address entry has three elements.
14977 [0] The low address. This is a 64-bit little-endian value.
14978 [1] The high address. This is a 64-bit little-endian value.
14979 [2] The CU index. This is an offset_type value.
14980
14981 4. The symbol table. This is a hash table. The size of the hash
14982 table is always a power of 2. The initial hash and the step are
14983 currently defined by the `find_slot' function.
14984
14985 Each slot in the hash table consists of a pair of offset_type
14986 values. The first value is the offset of the symbol's name in the
14987 constant pool. The second value is the offset of the CU vector in
14988 the constant pool.
14989
14990 If both values are 0, then this slot in the hash table is empty.
14991 This is ok because while 0 is a valid constant pool index, it
14992 cannot be a valid index for both a string and a CU vector.
14993
14994 A string in the constant pool is stored as a \0-terminated string,
14995 as you'd expect.
14996
14997 A CU vector in the constant pool is a sequence of offset_type
14998 values. The first value is the number of CU indices in the vector.
14999 Each subsequent value is the index of a CU in the CU list. This
15000 element in the hash table is used to indicate which CUs define the
15001 symbol.
15002
15003 5. The constant pool. This is simply a bunch of bytes. It is
15004 organized so that alignment is correct: CU vectors are stored
15005 first, followed by strings. */
15006static void
15007save_gdb_index_command (char *arg, int from_tty)
15008{
15009 struct objfile *objfile;
15010
15011 if (!arg || !*arg)
96d19272 15012 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15013
15014 ALL_OBJFILES (objfile)
15015 {
15016 struct stat st;
15017
15018 /* If the objfile does not correspond to an actual file, skip it. */
15019 if (stat (objfile->name, &st) < 0)
15020 continue;
15021
15022 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15023 if (dwarf2_per_objfile)
15024 {
15025 volatile struct gdb_exception except;
15026
15027 TRY_CATCH (except, RETURN_MASK_ERROR)
15028 {
15029 write_psymtabs_to_index (objfile, arg);
15030 }
15031 if (except.reason < 0)
15032 exception_fprintf (gdb_stderr, except,
15033 _("Error while writing index for `%s': "),
15034 objfile->name);
15035 }
15036 }
dce234bc
PP
15037}
15038
9291a0cd
TT
15039\f
15040
9eae7c52
TT
15041int dwarf2_always_disassemble;
15042
15043static void
15044show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15045 struct cmd_list_element *c, const char *value)
15046{
15047 fprintf_filtered (file, _("\
15048Whether to always disassemble DWARF expressions is %s.\n"),
15049 value);
15050}
15051
6502dd73
DJ
15052void _initialize_dwarf2_read (void);
15053
15054void
15055_initialize_dwarf2_read (void)
15056{
96d19272
JK
15057 struct cmd_list_element *c;
15058
dce234bc 15059 dwarf2_objfile_data_key
c1bd65d0 15060 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15061
1bedd215
AC
15062 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15063Set DWARF 2 specific variables.\n\
15064Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15065 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15066 0/*allow-unknown*/, &maintenance_set_cmdlist);
15067
1bedd215
AC
15068 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15069Show DWARF 2 specific variables\n\
15070Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15071 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15072 0/*allow-unknown*/, &maintenance_show_cmdlist);
15073
15074 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15075 &dwarf2_max_cache_age, _("\
15076Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15077Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15078A higher limit means that cached compilation units will be stored\n\
15079in memory longer, and more total memory will be used. Zero disables\n\
15080caching, which can slow down startup."),
2c5b56ce 15081 NULL,
920d2a44 15082 show_dwarf2_max_cache_age,
2c5b56ce 15083 &set_dwarf2_cmdlist,
ae038cb0 15084 &show_dwarf2_cmdlist);
d97bc12b 15085
9eae7c52
TT
15086 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15087 &dwarf2_always_disassemble, _("\
15088Set whether `info address' always disassembles DWARF expressions."), _("\
15089Show whether `info address' always disassembles DWARF expressions."), _("\
15090When enabled, DWARF expressions are always printed in an assembly-like\n\
15091syntax. When disabled, expressions will be printed in a more\n\
15092conversational style, when possible."),
15093 NULL,
15094 show_dwarf2_always_disassemble,
15095 &set_dwarf2_cmdlist,
15096 &show_dwarf2_cmdlist);
15097
d97bc12b
DE
15098 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15099Set debugging of the dwarf2 DIE reader."), _("\
15100Show debugging of the dwarf2 DIE reader."), _("\
15101When enabled (non-zero), DIEs are dumped after they are read in.\n\
15102The value is the maximum depth to print."),
15103 NULL,
15104 NULL,
15105 &setdebuglist, &showdebuglist);
9291a0cd 15106
96d19272
JK
15107 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15108 _("Save a .gdb-index file"),
15109 &save_cmdlist);
15110 set_cmd_completer (c, filename_completer);
6502dd73 15111}
This page took 2.155874 seconds and 4 git commands to generate.