PR python/10953:
[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);
6661 return set_die_type (die, type, cu);
6662 }
6663
d3f41bb1
TT
6664 back_to = make_cleanup (null_cleanup, 0);
6665
c0dd20ea 6666 type = alloc_type (objfile);
c906108c 6667 INIT_CPLUS_SPECIFIC (type);
93311388 6668
39cbfefa
DJ
6669 name = dwarf2_name (die, cu);
6670 if (name != NULL)
c906108c 6671 {
987504bb
JJ
6672 if (cu->language == language_cplus
6673 || cu->language == language_java)
63d06c5c 6674 {
3da10d80
KS
6675 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6676
6677 /* dwarf2_full_name might have already finished building the DIE's
6678 type. If so, there is no need to continue. */
6679 if (get_die_type (die, cu) != NULL)
6680 return get_die_type (die, cu);
6681
6682 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6683 if (die->tag == DW_TAG_structure_type
6684 || die->tag == DW_TAG_class_type)
6685 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6686 }
6687 else
6688 {
d8151005
DJ
6689 /* The name is already allocated along with this objfile, so
6690 we don't need to duplicate it for the type. */
94af9270
KS
6691 TYPE_TAG_NAME (type) = (char *) name;
6692 if (die->tag == DW_TAG_class_type)
6693 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6694 }
c906108c
SS
6695 }
6696
6697 if (die->tag == DW_TAG_structure_type)
6698 {
6699 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6700 }
6701 else if (die->tag == DW_TAG_union_type)
6702 {
6703 TYPE_CODE (type) = TYPE_CODE_UNION;
6704 }
6705 else
6706 {
c906108c
SS
6707 TYPE_CODE (type) = TYPE_CODE_CLASS;
6708 }
6709
0cc2414c
TT
6710 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6711 TYPE_DECLARED_CLASS (type) = 1;
6712
e142c38c 6713 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6714 if (attr)
6715 {
6716 TYPE_LENGTH (type) = DW_UNSND (attr);
6717 }
6718 else
6719 {
6720 TYPE_LENGTH (type) = 0;
6721 }
6722
876cecd0 6723 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6724 if (die_is_declaration (die, cu))
876cecd0 6725 TYPE_STUB (type) = 1;
a6c727b2
DJ
6726 else if (attr == NULL && die->child == NULL
6727 && producer_is_realview (cu->producer))
6728 /* RealView does not output the required DW_AT_declaration
6729 on incomplete types. */
6730 TYPE_STUB (type) = 1;
dc718098 6731
c906108c
SS
6732 /* We need to add the type field to the die immediately so we don't
6733 infinitely recurse when dealing with pointers to the structure
6734 type within the structure itself. */
1c379e20 6735 set_die_type (die, type, cu);
c906108c 6736
7e314c57
JK
6737 /* set_die_type should be already done. */
6738 set_descriptive_type (type, die, cu);
6739
e142c38c 6740 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6741 {
6742 struct field_info fi;
6743 struct die_info *child_die;
34eaf542 6744 VEC (symbolp) *template_args = NULL;
c906108c
SS
6745
6746 memset (&fi, 0, sizeof (struct field_info));
6747
639d11d3 6748 child_die = die->child;
c906108c
SS
6749
6750 while (child_die && child_die->tag)
6751 {
a9a9bd0f
DC
6752 if (child_die->tag == DW_TAG_member
6753 || child_die->tag == DW_TAG_variable)
c906108c 6754 {
a9a9bd0f
DC
6755 /* NOTE: carlton/2002-11-05: A C++ static data member
6756 should be a DW_TAG_member that is a declaration, but
6757 all versions of G++ as of this writing (so through at
6758 least 3.2.1) incorrectly generate DW_TAG_variable
6759 tags for them instead. */
e7c27a73 6760 dwarf2_add_field (&fi, child_die, cu);
c906108c 6761 }
8713b1b1 6762 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6763 {
6764 /* C++ member function. */
e7c27a73 6765 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6766 }
6767 else if (child_die->tag == DW_TAG_inheritance)
6768 {
6769 /* C++ base class field. */
e7c27a73 6770 dwarf2_add_field (&fi, child_die, cu);
c906108c 6771 }
98751a41
JK
6772 else if (child_die->tag == DW_TAG_typedef)
6773 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6774 else if (child_die->tag == DW_TAG_template_type_param
6775 || child_die->tag == DW_TAG_template_value_param)
6776 {
6777 struct symbol *arg = new_symbol (child_die, NULL, cu);
6778
6779 VEC_safe_push (symbolp, template_args, arg);
6780 }
6781
c906108c
SS
6782 child_die = sibling_die (child_die);
6783 }
6784
34eaf542
TT
6785 /* Attach template arguments to type. */
6786 if (! VEC_empty (symbolp, template_args))
6787 {
6788 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6789 TYPE_N_TEMPLATE_ARGUMENTS (type)
6790 = VEC_length (symbolp, template_args);
6791 TYPE_TEMPLATE_ARGUMENTS (type)
6792 = obstack_alloc (&objfile->objfile_obstack,
6793 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6794 * sizeof (struct symbol *)));
6795 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6796 VEC_address (symbolp, template_args),
6797 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6798 * sizeof (struct symbol *)));
6799 VEC_free (symbolp, template_args);
6800 }
6801
c906108c
SS
6802 /* Attach fields and member functions to the type. */
6803 if (fi.nfields)
e7c27a73 6804 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6805 if (fi.nfnfields)
6806 {
e7c27a73 6807 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6808
c5aa993b 6809 /* Get the type which refers to the base class (possibly this
c906108c 6810 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6811 class from the DW_AT_containing_type attribute. This use of
6812 DW_AT_containing_type is a GNU extension. */
c906108c 6813
e142c38c 6814 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6815 {
e7c27a73 6816 struct type *t = die_containing_type (die, cu);
c906108c
SS
6817
6818 TYPE_VPTR_BASETYPE (type) = t;
6819 if (type == t)
6820 {
c906108c
SS
6821 int i;
6822
6823 /* Our own class provides vtbl ptr. */
6824 for (i = TYPE_NFIELDS (t) - 1;
6825 i >= TYPE_N_BASECLASSES (t);
6826 --i)
6827 {
6828 char *fieldname = TYPE_FIELD_NAME (t, i);
6829
1168df01 6830 if (is_vtable_name (fieldname, cu))
c906108c
SS
6831 {
6832 TYPE_VPTR_FIELDNO (type) = i;
6833 break;
6834 }
6835 }
6836
6837 /* Complain if virtual function table field not found. */
6838 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6839 complaint (&symfile_complaints,
e2e0b3e5 6840 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6841 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6842 "");
c906108c
SS
6843 }
6844 else
6845 {
6846 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6847 }
6848 }
f6235d4c
EZ
6849 else if (cu->producer
6850 && strncmp (cu->producer,
6851 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6852 {
6853 /* The IBM XLC compiler does not provide direct indication
6854 of the containing type, but the vtable pointer is
6855 always named __vfp. */
6856
6857 int i;
6858
6859 for (i = TYPE_NFIELDS (type) - 1;
6860 i >= TYPE_N_BASECLASSES (type);
6861 --i)
6862 {
6863 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6864 {
6865 TYPE_VPTR_FIELDNO (type) = i;
6866 TYPE_VPTR_BASETYPE (type) = type;
6867 break;
6868 }
6869 }
6870 }
c906108c 6871 }
98751a41
JK
6872
6873 /* Copy fi.typedef_field_list linked list elements content into the
6874 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6875 if (fi.typedef_field_list)
6876 {
6877 int i = fi.typedef_field_list_count;
6878
a0d7a4ff 6879 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6880 TYPE_TYPEDEF_FIELD_ARRAY (type)
6881 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6882 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6883
6884 /* Reverse the list order to keep the debug info elements order. */
6885 while (--i >= 0)
6886 {
6887 struct typedef_field *dest, *src;
6e70227d 6888
98751a41
JK
6889 dest = &TYPE_TYPEDEF_FIELD (type, i);
6890 src = &fi.typedef_field_list->field;
6891 fi.typedef_field_list = fi.typedef_field_list->next;
6892 *dest = *src;
6893 }
6894 }
c906108c 6895 }
63d06c5c 6896
0b92b5bb
TT
6897 quirk_gcc_member_function_pointer (type, cu->objfile);
6898
0114d602 6899 do_cleanups (back_to);
f792889a 6900 return type;
c906108c
SS
6901}
6902
134d01f1
DJ
6903static void
6904process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6905{
90aeadfc 6906 struct die_info *child_die = die->child;
f792889a 6907 struct type *this_type;
c906108c 6908
f792889a
DJ
6909 this_type = get_die_type (die, cu);
6910 if (this_type == NULL)
6911 this_type = read_structure_type (die, cu);
c906108c 6912
90aeadfc
DC
6913 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6914 snapshots) has been known to create a die giving a declaration
6915 for a class that has, as a child, a die giving a definition for a
6916 nested class. So we have to process our children even if the
6917 current die is a declaration. Normally, of course, a declaration
6918 won't have any children at all. */
134d01f1 6919
90aeadfc
DC
6920 while (child_die != NULL && child_die->tag)
6921 {
6922 if (child_die->tag == DW_TAG_member
6923 || child_die->tag == DW_TAG_variable
34eaf542
TT
6924 || child_die->tag == DW_TAG_inheritance
6925 || child_die->tag == DW_TAG_template_value_param
6926 || child_die->tag == DW_TAG_template_type_param)
134d01f1 6927 {
90aeadfc 6928 /* Do nothing. */
134d01f1 6929 }
90aeadfc
DC
6930 else
6931 process_die (child_die, cu);
134d01f1 6932
90aeadfc 6933 child_die = sibling_die (child_die);
134d01f1
DJ
6934 }
6935
fa4028e9
JB
6936 /* Do not consider external references. According to the DWARF standard,
6937 these DIEs are identified by the fact that they have no byte_size
6938 attribute, and a declaration attribute. */
6939 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6940 || !die_is_declaration (die, cu))
f792889a 6941 new_symbol (die, this_type, cu);
134d01f1
DJ
6942}
6943
6944/* Given a DW_AT_enumeration_type die, set its type. We do not
6945 complete the type's fields yet, or create any symbols. */
c906108c 6946
f792889a 6947static struct type *
134d01f1 6948read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6949{
e7c27a73 6950 struct objfile *objfile = cu->objfile;
c906108c 6951 struct type *type;
c906108c 6952 struct attribute *attr;
0114d602 6953 const char *name;
134d01f1 6954
348e048f
DE
6955 /* If the definition of this type lives in .debug_types, read that type.
6956 Don't follow DW_AT_specification though, that will take us back up
6957 the chain and we want to go down. */
6958 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6959 if (attr)
6960 {
6961 struct dwarf2_cu *type_cu = cu;
6962 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6963
348e048f
DE
6964 type = read_type_die (type_die, type_cu);
6965 return set_die_type (die, type, cu);
6966 }
6967
c906108c
SS
6968 type = alloc_type (objfile);
6969
6970 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 6971 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 6972 if (name != NULL)
0114d602 6973 TYPE_TAG_NAME (type) = (char *) name;
c906108c 6974
e142c38c 6975 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6976 if (attr)
6977 {
6978 TYPE_LENGTH (type) = DW_UNSND (attr);
6979 }
6980 else
6981 {
6982 TYPE_LENGTH (type) = 0;
6983 }
6984
137033e9
JB
6985 /* The enumeration DIE can be incomplete. In Ada, any type can be
6986 declared as private in the package spec, and then defined only
6987 inside the package body. Such types are known as Taft Amendment
6988 Types. When another package uses such a type, an incomplete DIE
6989 may be generated by the compiler. */
02eb380e 6990 if (die_is_declaration (die, cu))
876cecd0 6991 TYPE_STUB (type) = 1;
02eb380e 6992
f792889a 6993 return set_die_type (die, type, cu);
134d01f1
DJ
6994}
6995
6996/* Given a pointer to a die which begins an enumeration, process all
6997 the dies that define the members of the enumeration, and create the
6998 symbol for the enumeration type.
6999
7000 NOTE: We reverse the order of the element list. */
7001
7002static void
7003process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7004{
134d01f1
DJ
7005 struct die_info *child_die;
7006 struct field *fields;
134d01f1
DJ
7007 struct symbol *sym;
7008 int num_fields;
7009 int unsigned_enum = 1;
39cbfefa 7010 char *name;
f792889a 7011 struct type *this_type;
134d01f1 7012
c906108c
SS
7013 num_fields = 0;
7014 fields = NULL;
f792889a
DJ
7015 this_type = get_die_type (die, cu);
7016 if (this_type == NULL)
7017 this_type = read_enumeration_type (die, cu);
639d11d3 7018 if (die->child != NULL)
c906108c 7019 {
639d11d3 7020 child_die = die->child;
c906108c
SS
7021 while (child_die && child_die->tag)
7022 {
7023 if (child_die->tag != DW_TAG_enumerator)
7024 {
e7c27a73 7025 process_die (child_die, cu);
c906108c
SS
7026 }
7027 else
7028 {
39cbfefa
DJ
7029 name = dwarf2_name (child_die, cu);
7030 if (name)
c906108c 7031 {
f792889a 7032 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7033 if (SYMBOL_VALUE (sym) < 0)
7034 unsigned_enum = 0;
7035
7036 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7037 {
7038 fields = (struct field *)
7039 xrealloc (fields,
7040 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7041 * sizeof (struct field));
c906108c
SS
7042 }
7043
3567439c 7044 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7045 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7046 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7047 FIELD_BITSIZE (fields[num_fields]) = 0;
7048
7049 num_fields++;
7050 }
7051 }
7052
7053 child_die = sibling_die (child_die);
7054 }
7055
7056 if (num_fields)
7057 {
f792889a
DJ
7058 TYPE_NFIELDS (this_type) = num_fields;
7059 TYPE_FIELDS (this_type) = (struct field *)
7060 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7061 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7062 sizeof (struct field) * num_fields);
b8c9b27d 7063 xfree (fields);
c906108c
SS
7064 }
7065 if (unsigned_enum)
876cecd0 7066 TYPE_UNSIGNED (this_type) = 1;
c906108c 7067 }
134d01f1 7068
f792889a 7069 new_symbol (die, this_type, cu);
c906108c
SS
7070}
7071
7072/* Extract all information from a DW_TAG_array_type DIE and put it in
7073 the DIE's type field. For now, this only handles one dimensional
7074 arrays. */
7075
f792889a 7076static struct type *
e7c27a73 7077read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7078{
e7c27a73 7079 struct objfile *objfile = cu->objfile;
c906108c 7080 struct die_info *child_die;
7e314c57 7081 struct type *type;
c906108c
SS
7082 struct type *element_type, *range_type, *index_type;
7083 struct type **range_types = NULL;
7084 struct attribute *attr;
7085 int ndim = 0;
7086 struct cleanup *back_to;
39cbfefa 7087 char *name;
c906108c 7088
e7c27a73 7089 element_type = die_type (die, cu);
c906108c 7090
7e314c57
JK
7091 /* The die_type call above may have already set the type for this DIE. */
7092 type = get_die_type (die, cu);
7093 if (type)
7094 return type;
7095
c906108c
SS
7096 /* Irix 6.2 native cc creates array types without children for
7097 arrays with unspecified length. */
639d11d3 7098 if (die->child == NULL)
c906108c 7099 {
46bf5051 7100 index_type = objfile_type (objfile)->builtin_int;
c906108c 7101 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7102 type = create_array_type (NULL, element_type, range_type);
7103 return set_die_type (die, type, cu);
c906108c
SS
7104 }
7105
7106 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7107 child_die = die->child;
c906108c
SS
7108 while (child_die && child_die->tag)
7109 {
7110 if (child_die->tag == DW_TAG_subrange_type)
7111 {
f792889a 7112 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7113
f792889a 7114 if (child_type != NULL)
a02abb62
JB
7115 {
7116 /* The range type was succesfully read. Save it for
7117 the array type creation. */
7118 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7119 {
7120 range_types = (struct type **)
7121 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7122 * sizeof (struct type *));
7123 if (ndim == 0)
7124 make_cleanup (free_current_contents, &range_types);
7125 }
f792889a 7126 range_types[ndim++] = child_type;
a02abb62 7127 }
c906108c
SS
7128 }
7129 child_die = sibling_die (child_die);
7130 }
7131
7132 /* Dwarf2 dimensions are output from left to right, create the
7133 necessary array types in backwards order. */
7ca2d3a3 7134
c906108c 7135 type = element_type;
7ca2d3a3
DL
7136
7137 if (read_array_order (die, cu) == DW_ORD_col_major)
7138 {
7139 int i = 0;
9a619af0 7140
7ca2d3a3
DL
7141 while (i < ndim)
7142 type = create_array_type (NULL, type, range_types[i++]);
7143 }
7144 else
7145 {
7146 while (ndim-- > 0)
7147 type = create_array_type (NULL, type, range_types[ndim]);
7148 }
c906108c 7149
f5f8a009
EZ
7150 /* Understand Dwarf2 support for vector types (like they occur on
7151 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7152 array type. This is not part of the Dwarf2/3 standard yet, but a
7153 custom vendor extension. The main difference between a regular
7154 array and the vector variant is that vectors are passed by value
7155 to functions. */
e142c38c 7156 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7157 if (attr)
ea37ba09 7158 make_vector_type (type);
f5f8a009 7159
39cbfefa
DJ
7160 name = dwarf2_name (die, cu);
7161 if (name)
7162 TYPE_NAME (type) = name;
6e70227d 7163
7e314c57
JK
7164 /* Install the type in the die. */
7165 set_die_type (die, type, cu);
7166
7167 /* set_die_type should be already done. */
b4ba55a1
JB
7168 set_descriptive_type (type, die, cu);
7169
c906108c
SS
7170 do_cleanups (back_to);
7171
7e314c57 7172 return type;
c906108c
SS
7173}
7174
7ca2d3a3 7175static enum dwarf_array_dim_ordering
6e70227d 7176read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7177{
7178 struct attribute *attr;
7179
7180 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7181
7182 if (attr) return DW_SND (attr);
7183
7184 /*
7185 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 7186 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
7187 laid out as per normal fortran.
7188
6e70227d 7189 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
7190 version checking.
7191 */
7192
905e0470
PM
7193 if (cu->language == language_fortran
7194 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7195 {
7196 return DW_ORD_row_major;
7197 }
7198
6e70227d 7199 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7200 {
7201 case array_column_major:
7202 return DW_ORD_col_major;
7203 case array_row_major:
7204 default:
7205 return DW_ORD_row_major;
7206 };
7207}
7208
72019c9c
GM
7209/* Extract all information from a DW_TAG_set_type DIE and put it in
7210 the DIE's type field. */
7211
f792889a 7212static struct type *
72019c9c
GM
7213read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7214{
7e314c57
JK
7215 struct type *domain_type, *set_type;
7216 struct attribute *attr;
f792889a 7217
7e314c57
JK
7218 domain_type = die_type (die, cu);
7219
7220 /* The die_type call above may have already set the type for this DIE. */
7221 set_type = get_die_type (die, cu);
7222 if (set_type)
7223 return set_type;
7224
7225 set_type = create_set_type (NULL, domain_type);
7226
7227 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7228 if (attr)
7229 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7230
f792889a 7231 return set_die_type (die, set_type, cu);
72019c9c 7232}
7ca2d3a3 7233
c906108c
SS
7234/* First cut: install each common block member as a global variable. */
7235
7236static void
e7c27a73 7237read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7238{
7239 struct die_info *child_die;
7240 struct attribute *attr;
7241 struct symbol *sym;
7242 CORE_ADDR base = (CORE_ADDR) 0;
7243
e142c38c 7244 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7245 if (attr)
7246 {
8e19ed76
PS
7247 /* Support the .debug_loc offsets */
7248 if (attr_form_is_block (attr))
7249 {
e7c27a73 7250 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7251 }
3690dd37 7252 else if (attr_form_is_section_offset (attr))
8e19ed76 7253 {
4d3c2250 7254 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7255 }
7256 else
7257 {
4d3c2250
KB
7258 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7259 "common block member");
8e19ed76 7260 }
c906108c 7261 }
639d11d3 7262 if (die->child != NULL)
c906108c 7263 {
639d11d3 7264 child_die = die->child;
c906108c
SS
7265 while (child_die && child_die->tag)
7266 {
e7c27a73 7267 sym = new_symbol (child_die, NULL, cu);
e142c38c 7268 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
c906108c
SS
7269 if (attr)
7270 {
d4b96c9a
JK
7271 CORE_ADDR byte_offset = 0;
7272
7273 if (attr_form_is_section_offset (attr))
7274 dwarf2_complex_location_expr_complaint ();
7275 else if (attr_form_is_constant (attr))
7276 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7277 else if (attr_form_is_block (attr))
7278 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7279 else
7280 dwarf2_complex_location_expr_complaint ();
7281
7282 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7283 add_symbol_to_list (sym, &global_symbols);
7284 }
7285 child_die = sibling_die (child_die);
7286 }
7287 }
7288}
7289
0114d602 7290/* Create a type for a C++ namespace. */
d9fa45fe 7291
0114d602
DJ
7292static struct type *
7293read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7294{
e7c27a73 7295 struct objfile *objfile = cu->objfile;
0114d602 7296 const char *previous_prefix, *name;
9219021c 7297 int is_anonymous;
0114d602
DJ
7298 struct type *type;
7299
7300 /* For extensions, reuse the type of the original namespace. */
7301 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7302 {
7303 struct die_info *ext_die;
7304 struct dwarf2_cu *ext_cu = cu;
9a619af0 7305
0114d602
DJ
7306 ext_die = dwarf2_extension (die, &ext_cu);
7307 type = read_type_die (ext_die, ext_cu);
7308 return set_die_type (die, type, cu);
7309 }
9219021c 7310
e142c38c 7311 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7312
7313 /* Now build the name of the current namespace. */
7314
0114d602
DJ
7315 previous_prefix = determine_prefix (die, cu);
7316 if (previous_prefix[0] != '\0')
7317 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7318 previous_prefix, name, 0, cu);
0114d602
DJ
7319
7320 /* Create the type. */
7321 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7322 objfile);
7323 TYPE_NAME (type) = (char *) name;
7324 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7325
60531b24 7326 return set_die_type (die, type, cu);
0114d602
DJ
7327}
7328
7329/* Read a C++ namespace. */
7330
7331static void
7332read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7333{
7334 struct objfile *objfile = cu->objfile;
7335 const char *name;
7336 int is_anonymous;
9219021c 7337
5c4e30ca
DC
7338 /* Add a symbol associated to this if we haven't seen the namespace
7339 before. Also, add a using directive if it's an anonymous
7340 namespace. */
9219021c 7341
f2f0e013 7342 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7343 {
7344 struct type *type;
7345
0114d602 7346 type = read_type_die (die, cu);
e7c27a73 7347 new_symbol (die, type, cu);
5c4e30ca 7348
0114d602 7349 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7350 if (is_anonymous)
0114d602
DJ
7351 {
7352 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7353
c0cc3a76 7354 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7355 NULL, &objfile->objfile_obstack);
0114d602 7356 }
5c4e30ca 7357 }
9219021c 7358
639d11d3 7359 if (die->child != NULL)
d9fa45fe 7360 {
639d11d3 7361 struct die_info *child_die = die->child;
6e70227d 7362
d9fa45fe
DC
7363 while (child_die && child_die->tag)
7364 {
e7c27a73 7365 process_die (child_die, cu);
d9fa45fe
DC
7366 child_die = sibling_die (child_die);
7367 }
7368 }
38d518c9
EZ
7369}
7370
f55ee35c
JK
7371/* Read a Fortran module as type. This DIE can be only a declaration used for
7372 imported module. Still we need that type as local Fortran "use ... only"
7373 declaration imports depend on the created type in determine_prefix. */
7374
7375static struct type *
7376read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7377{
7378 struct objfile *objfile = cu->objfile;
7379 char *module_name;
7380 struct type *type;
7381
7382 module_name = dwarf2_name (die, cu);
7383 if (!module_name)
7384 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7385 die->offset);
7386 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7387
7388 /* determine_prefix uses TYPE_TAG_NAME. */
7389 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7390
7391 return set_die_type (die, type, cu);
7392}
7393
5d7cb8df
JK
7394/* Read a Fortran module. */
7395
7396static void
7397read_module (struct die_info *die, struct dwarf2_cu *cu)
7398{
7399 struct die_info *child_die = die->child;
7400
5d7cb8df
JK
7401 while (child_die && child_die->tag)
7402 {
7403 process_die (child_die, cu);
7404 child_die = sibling_die (child_die);
7405 }
7406}
7407
38d518c9
EZ
7408/* Return the name of the namespace represented by DIE. Set
7409 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7410 namespace. */
7411
7412static const char *
e142c38c 7413namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7414{
7415 struct die_info *current_die;
7416 const char *name = NULL;
7417
7418 /* Loop through the extensions until we find a name. */
7419
7420 for (current_die = die;
7421 current_die != NULL;
f2f0e013 7422 current_die = dwarf2_extension (die, &cu))
38d518c9 7423 {
e142c38c 7424 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7425 if (name != NULL)
7426 break;
7427 }
7428
7429 /* Is it an anonymous namespace? */
7430
7431 *is_anonymous = (name == NULL);
7432 if (*is_anonymous)
7433 name = "(anonymous namespace)";
7434
7435 return name;
d9fa45fe
DC
7436}
7437
c906108c
SS
7438/* Extract all information from a DW_TAG_pointer_type DIE and add to
7439 the user defined type vector. */
7440
f792889a 7441static struct type *
e7c27a73 7442read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7443{
5e2b427d 7444 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7445 struct comp_unit_head *cu_header = &cu->header;
c906108c 7446 struct type *type;
8b2dbe47
KB
7447 struct attribute *attr_byte_size;
7448 struct attribute *attr_address_class;
7449 int byte_size, addr_class;
7e314c57
JK
7450 struct type *target_type;
7451
7452 target_type = die_type (die, cu);
c906108c 7453
7e314c57
JK
7454 /* The die_type call above may have already set the type for this DIE. */
7455 type = get_die_type (die, cu);
7456 if (type)
7457 return type;
7458
7459 type = lookup_pointer_type (target_type);
8b2dbe47 7460
e142c38c 7461 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7462 if (attr_byte_size)
7463 byte_size = DW_UNSND (attr_byte_size);
c906108c 7464 else
8b2dbe47
KB
7465 byte_size = cu_header->addr_size;
7466
e142c38c 7467 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7468 if (attr_address_class)
7469 addr_class = DW_UNSND (attr_address_class);
7470 else
7471 addr_class = DW_ADDR_none;
7472
7473 /* If the pointer size or address class is different than the
7474 default, create a type variant marked as such and set the
7475 length accordingly. */
7476 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7477 {
5e2b427d 7478 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7479 {
7480 int type_flags;
7481
849957d9 7482 type_flags = gdbarch_address_class_type_flags
5e2b427d 7483 (gdbarch, byte_size, addr_class);
876cecd0
TT
7484 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7485 == 0);
8b2dbe47
KB
7486 type = make_type_with_address_space (type, type_flags);
7487 }
7488 else if (TYPE_LENGTH (type) != byte_size)
7489 {
e2e0b3e5 7490 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 7491 }
6e70227d 7492 else
9a619af0
MS
7493 {
7494 /* Should we also complain about unhandled address classes? */
7495 }
c906108c 7496 }
8b2dbe47
KB
7497
7498 TYPE_LENGTH (type) = byte_size;
f792889a 7499 return set_die_type (die, type, cu);
c906108c
SS
7500}
7501
7502/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7503 the user defined type vector. */
7504
f792889a 7505static struct type *
e7c27a73 7506read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7507{
7508 struct type *type;
7509 struct type *to_type;
7510 struct type *domain;
7511
e7c27a73
DJ
7512 to_type = die_type (die, cu);
7513 domain = die_containing_type (die, cu);
0d5de010 7514
7e314c57
JK
7515 /* The calls above may have already set the type for this DIE. */
7516 type = get_die_type (die, cu);
7517 if (type)
7518 return type;
7519
0d5de010
DJ
7520 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7521 type = lookup_methodptr_type (to_type);
7522 else
7523 type = lookup_memberptr_type (to_type, domain);
c906108c 7524
f792889a 7525 return set_die_type (die, type, cu);
c906108c
SS
7526}
7527
7528/* Extract all information from a DW_TAG_reference_type DIE and add to
7529 the user defined type vector. */
7530
f792889a 7531static struct type *
e7c27a73 7532read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7533{
e7c27a73 7534 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7535 struct type *type, *target_type;
c906108c
SS
7536 struct attribute *attr;
7537
7e314c57
JK
7538 target_type = die_type (die, cu);
7539
7540 /* The die_type call above may have already set the type for this DIE. */
7541 type = get_die_type (die, cu);
7542 if (type)
7543 return type;
7544
7545 type = lookup_reference_type (target_type);
e142c38c 7546 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7547 if (attr)
7548 {
7549 TYPE_LENGTH (type) = DW_UNSND (attr);
7550 }
7551 else
7552 {
107d2387 7553 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7554 }
f792889a 7555 return set_die_type (die, type, cu);
c906108c
SS
7556}
7557
f792889a 7558static struct type *
e7c27a73 7559read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7560{
f792889a 7561 struct type *base_type, *cv_type;
c906108c 7562
e7c27a73 7563 base_type = die_type (die, cu);
7e314c57
JK
7564
7565 /* The die_type call above may have already set the type for this DIE. */
7566 cv_type = get_die_type (die, cu);
7567 if (cv_type)
7568 return cv_type;
7569
f792889a
DJ
7570 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7571 return set_die_type (die, cv_type, cu);
c906108c
SS
7572}
7573
f792889a 7574static struct type *
e7c27a73 7575read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7576{
f792889a 7577 struct type *base_type, *cv_type;
c906108c 7578
e7c27a73 7579 base_type = die_type (die, cu);
7e314c57
JK
7580
7581 /* The die_type call above may have already set the type for this DIE. */
7582 cv_type = get_die_type (die, cu);
7583 if (cv_type)
7584 return cv_type;
7585
f792889a
DJ
7586 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7587 return set_die_type (die, cv_type, cu);
c906108c
SS
7588}
7589
7590/* Extract all information from a DW_TAG_string_type DIE and add to
7591 the user defined type vector. It isn't really a user defined type,
7592 but it behaves like one, with other DIE's using an AT_user_def_type
7593 attribute to reference it. */
7594
f792889a 7595static struct type *
e7c27a73 7596read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7597{
e7c27a73 7598 struct objfile *objfile = cu->objfile;
3b7538c0 7599 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7600 struct type *type, *range_type, *index_type, *char_type;
7601 struct attribute *attr;
7602 unsigned int length;
7603
e142c38c 7604 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7605 if (attr)
7606 {
7607 length = DW_UNSND (attr);
7608 }
7609 else
7610 {
b21b22e0 7611 /* check for the DW_AT_byte_size attribute */
e142c38c 7612 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7613 if (attr)
7614 {
7615 length = DW_UNSND (attr);
7616 }
7617 else
7618 {
7619 length = 1;
7620 }
c906108c 7621 }
6ccb9162 7622
46bf5051 7623 index_type = objfile_type (objfile)->builtin_int;
c906108c 7624 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7625 char_type = language_string_char_type (cu->language_defn, gdbarch);
7626 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7627
f792889a 7628 return set_die_type (die, type, cu);
c906108c
SS
7629}
7630
7631/* Handle DIES due to C code like:
7632
7633 struct foo
c5aa993b
JM
7634 {
7635 int (*funcp)(int a, long l);
7636 int b;
7637 };
c906108c
SS
7638
7639 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7640 */
c906108c 7641
f792889a 7642static struct type *
e7c27a73 7643read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7644{
7645 struct type *type; /* Type that this function returns */
7646 struct type *ftype; /* Function that returns above type */
7647 struct attribute *attr;
7648
e7c27a73 7649 type = die_type (die, cu);
7e314c57
JK
7650
7651 /* The die_type call above may have already set the type for this DIE. */
7652 ftype = get_die_type (die, cu);
7653 if (ftype)
7654 return ftype;
7655
0c8b41f1 7656 ftype = lookup_function_type (type);
c906108c 7657
5b8101ae 7658 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7659 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7660 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7661 || cu->language == language_cplus
5b8101ae
PM
7662 || cu->language == language_java
7663 || cu->language == language_pascal)
876cecd0 7664 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7665 else if (producer_is_realview (cu->producer))
7666 /* RealView does not emit DW_AT_prototyped. We can not
7667 distinguish prototyped and unprototyped functions; default to
7668 prototyped, since that is more common in modern code (and
7669 RealView warns about unprototyped functions). */
7670 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7671
c055b101
CV
7672 /* Store the calling convention in the type if it's available in
7673 the subroutine die. Otherwise set the calling convention to
7674 the default value DW_CC_normal. */
7675 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7676 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7677
7678 /* We need to add the subroutine type to the die immediately so
7679 we don't infinitely recurse when dealing with parameters
7680 declared as the same subroutine type. */
7681 set_die_type (die, ftype, cu);
6e70227d 7682
639d11d3 7683 if (die->child != NULL)
c906108c 7684 {
8072405b 7685 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7686 struct die_info *child_die;
8072405b 7687 int nparams, iparams;
c906108c
SS
7688
7689 /* Count the number of parameters.
7690 FIXME: GDB currently ignores vararg functions, but knows about
7691 vararg member functions. */
8072405b 7692 nparams = 0;
639d11d3 7693 child_die = die->child;
c906108c
SS
7694 while (child_die && child_die->tag)
7695 {
7696 if (child_die->tag == DW_TAG_formal_parameter)
7697 nparams++;
7698 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7699 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7700 child_die = sibling_die (child_die);
7701 }
7702
7703 /* Allocate storage for parameters and fill them in. */
7704 TYPE_NFIELDS (ftype) = nparams;
7705 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7706 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7707
8072405b
JK
7708 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7709 even if we error out during the parameters reading below. */
7710 for (iparams = 0; iparams < nparams; iparams++)
7711 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7712
7713 iparams = 0;
639d11d3 7714 child_die = die->child;
c906108c
SS
7715 while (child_die && child_die->tag)
7716 {
7717 if (child_die->tag == DW_TAG_formal_parameter)
7718 {
3ce3b1ba
PA
7719 struct type *arg_type;
7720
7721 /* DWARF version 2 has no clean way to discern C++
7722 static and non-static member functions. G++ helps
7723 GDB by marking the first parameter for non-static
7724 member functions (which is the this pointer) as
7725 artificial. We pass this information to
7726 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7727
7728 DWARF version 3 added DW_AT_object_pointer, which GCC
7729 4.5 does not yet generate. */
e142c38c 7730 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7731 if (attr)
7732 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7733 else
418835cc
KS
7734 {
7735 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7736
7737 /* GCC/43521: In java, the formal parameter
7738 "this" is sometimes not marked with DW_AT_artificial. */
7739 if (cu->language == language_java)
7740 {
7741 const char *name = dwarf2_name (child_die, cu);
9a619af0 7742
418835cc
KS
7743 if (name && !strcmp (name, "this"))
7744 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7745 }
7746 }
3ce3b1ba
PA
7747 arg_type = die_type (child_die, cu);
7748
7749 /* RealView does not mark THIS as const, which the testsuite
7750 expects. GCC marks THIS as const in method definitions,
7751 but not in the class specifications (GCC PR 43053). */
7752 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7753 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7754 {
7755 int is_this = 0;
7756 struct dwarf2_cu *arg_cu = cu;
7757 const char *name = dwarf2_name (child_die, cu);
7758
7759 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7760 if (attr)
7761 {
7762 /* If the compiler emits this, use it. */
7763 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7764 is_this = 1;
7765 }
7766 else if (name && strcmp (name, "this") == 0)
7767 /* Function definitions will have the argument names. */
7768 is_this = 1;
7769 else if (name == NULL && iparams == 0)
7770 /* Declarations may not have the names, so like
7771 elsewhere in GDB, assume an artificial first
7772 argument is "this". */
7773 is_this = 1;
7774
7775 if (is_this)
7776 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7777 arg_type, 0);
7778 }
7779
7780 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
7781 iparams++;
7782 }
7783 child_die = sibling_die (child_die);
7784 }
7785 }
7786
76c10ea2 7787 return ftype;
c906108c
SS
7788}
7789
f792889a 7790static struct type *
e7c27a73 7791read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7792{
e7c27a73 7793 struct objfile *objfile = cu->objfile;
0114d602 7794 const char *name = NULL;
f792889a 7795 struct type *this_type;
c906108c 7796
94af9270 7797 name = dwarf2_full_name (NULL, die, cu);
f792889a 7798 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7799 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7800 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7801 set_die_type (die, this_type, cu);
7802 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7803 return this_type;
c906108c
SS
7804}
7805
7806/* Find a representation of a given base type and install
7807 it in the TYPE field of the die. */
7808
f792889a 7809static struct type *
e7c27a73 7810read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7811{
e7c27a73 7812 struct objfile *objfile = cu->objfile;
c906108c
SS
7813 struct type *type;
7814 struct attribute *attr;
7815 int encoding = 0, size = 0;
39cbfefa 7816 char *name;
6ccb9162
UW
7817 enum type_code code = TYPE_CODE_INT;
7818 int type_flags = 0;
7819 struct type *target_type = NULL;
c906108c 7820
e142c38c 7821 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7822 if (attr)
7823 {
7824 encoding = DW_UNSND (attr);
7825 }
e142c38c 7826 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7827 if (attr)
7828 {
7829 size = DW_UNSND (attr);
7830 }
39cbfefa 7831 name = dwarf2_name (die, cu);
6ccb9162 7832 if (!name)
c906108c 7833 {
6ccb9162
UW
7834 complaint (&symfile_complaints,
7835 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7836 }
6ccb9162
UW
7837
7838 switch (encoding)
c906108c 7839 {
6ccb9162
UW
7840 case DW_ATE_address:
7841 /* Turn DW_ATE_address into a void * pointer. */
7842 code = TYPE_CODE_PTR;
7843 type_flags |= TYPE_FLAG_UNSIGNED;
7844 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7845 break;
7846 case DW_ATE_boolean:
7847 code = TYPE_CODE_BOOL;
7848 type_flags |= TYPE_FLAG_UNSIGNED;
7849 break;
7850 case DW_ATE_complex_float:
7851 code = TYPE_CODE_COMPLEX;
7852 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7853 break;
7854 case DW_ATE_decimal_float:
7855 code = TYPE_CODE_DECFLOAT;
7856 break;
7857 case DW_ATE_float:
7858 code = TYPE_CODE_FLT;
7859 break;
7860 case DW_ATE_signed:
7861 break;
7862 case DW_ATE_unsigned:
7863 type_flags |= TYPE_FLAG_UNSIGNED;
7864 break;
7865 case DW_ATE_signed_char:
6e70227d 7866 if (cu->language == language_ada || cu->language == language_m2
868a0084 7867 || cu->language == language_pascal)
6ccb9162
UW
7868 code = TYPE_CODE_CHAR;
7869 break;
7870 case DW_ATE_unsigned_char:
868a0084
PM
7871 if (cu->language == language_ada || cu->language == language_m2
7872 || cu->language == language_pascal)
6ccb9162
UW
7873 code = TYPE_CODE_CHAR;
7874 type_flags |= TYPE_FLAG_UNSIGNED;
7875 break;
75079b2b
TT
7876 case DW_ATE_UTF:
7877 /* We just treat this as an integer and then recognize the
7878 type by name elsewhere. */
7879 break;
7880
6ccb9162
UW
7881 default:
7882 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7883 dwarf_type_encoding_name (encoding));
7884 break;
c906108c 7885 }
6ccb9162 7886
0114d602
DJ
7887 type = init_type (code, size, type_flags, NULL, objfile);
7888 TYPE_NAME (type) = name;
6ccb9162
UW
7889 TYPE_TARGET_TYPE (type) = target_type;
7890
0114d602 7891 if (name && strcmp (name, "char") == 0)
876cecd0 7892 TYPE_NOSIGN (type) = 1;
0114d602 7893
f792889a 7894 return set_die_type (die, type, cu);
c906108c
SS
7895}
7896
a02abb62
JB
7897/* Read the given DW_AT_subrange DIE. */
7898
f792889a 7899static struct type *
a02abb62
JB
7900read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7901{
5e2b427d 7902 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7903 struct type *base_type;
7904 struct type *range_type;
7905 struct attribute *attr;
43bbcdc2
PH
7906 LONGEST low = 0;
7907 LONGEST high = -1;
39cbfefa 7908 char *name;
43bbcdc2 7909 LONGEST negative_mask;
e77813c8 7910
a02abb62 7911 base_type = die_type (die, cu);
953ac07e
JK
7912 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7913 check_typedef (base_type);
a02abb62 7914
7e314c57
JK
7915 /* The die_type call above may have already set the type for this DIE. */
7916 range_type = get_die_type (die, cu);
7917 if (range_type)
7918 return range_type;
7919
e142c38c 7920 if (cu->language == language_fortran)
6e70227d 7921 {
a02abb62
JB
7922 /* FORTRAN implies a lower bound of 1, if not given. */
7923 low = 1;
7924 }
7925
dd5e6932
DJ
7926 /* FIXME: For variable sized arrays either of these could be
7927 a variable rather than a constant value. We'll allow it,
7928 but we don't know how to handle it. */
e142c38c 7929 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
7930 if (attr)
7931 low = dwarf2_get_attr_constant_value (attr, 0);
7932
e142c38c 7933 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 7934 if (attr)
6e70227d 7935 {
e77813c8 7936 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
7937 {
7938 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 7939 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
7940 FIXME: GDB does not yet know how to handle dynamic
7941 arrays properly, treat them as arrays with unspecified
7942 length for now.
7943
7944 FIXME: jimb/2003-09-22: GDB does not really know
7945 how to handle arrays of unspecified length
7946 either; we just represent them as zero-length
7947 arrays. Choose an appropriate upper bound given
7948 the lower bound we've computed above. */
7949 high = low - 1;
7950 }
7951 else
7952 high = dwarf2_get_attr_constant_value (attr, 1);
7953 }
e77813c8
PM
7954 else
7955 {
7956 attr = dwarf2_attr (die, DW_AT_count, cu);
7957 if (attr)
7958 {
7959 int count = dwarf2_get_attr_constant_value (attr, 1);
7960 high = low + count - 1;
7961 }
7962 }
7963
7964 /* Dwarf-2 specifications explicitly allows to create subrange types
7965 without specifying a base type.
7966 In that case, the base type must be set to the type of
7967 the lower bound, upper bound or count, in that order, if any of these
7968 three attributes references an object that has a type.
7969 If no base type is found, the Dwarf-2 specifications say that
7970 a signed integer type of size equal to the size of an address should
7971 be used.
7972 For the following C code: `extern char gdb_int [];'
7973 GCC produces an empty range DIE.
7974 FIXME: muller/2010-05-28: Possible references to object for low bound,
7975 high bound or count are not yet handled by this code.
7976 */
7977 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
7978 {
7979 struct objfile *objfile = cu->objfile;
7980 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7981 int addr_size = gdbarch_addr_bit (gdbarch) /8;
7982 struct type *int_type = objfile_type (objfile)->builtin_int;
7983
7984 /* Test "int", "long int", and "long long int" objfile types,
7985 and select the first one having a size above or equal to the
7986 architecture address size. */
7987 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7988 base_type = int_type;
7989 else
7990 {
7991 int_type = objfile_type (objfile)->builtin_long;
7992 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7993 base_type = int_type;
7994 else
7995 {
7996 int_type = objfile_type (objfile)->builtin_long_long;
7997 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7998 base_type = int_type;
7999 }
8000 }
8001 }
a02abb62 8002
6e70227d 8003 negative_mask =
43bbcdc2
PH
8004 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8005 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8006 low |= negative_mask;
8007 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8008 high |= negative_mask;
8009
a02abb62
JB
8010 range_type = create_range_type (NULL, base_type, low, high);
8011
bbb0eef6
JK
8012 /* Mark arrays with dynamic length at least as an array of unspecified
8013 length. GDB could check the boundary but before it gets implemented at
8014 least allow accessing the array elements. */
8015 if (attr && attr->form == DW_FORM_block1)
8016 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8017
39cbfefa
DJ
8018 name = dwarf2_name (die, cu);
8019 if (name)
8020 TYPE_NAME (range_type) = name;
6e70227d 8021
e142c38c 8022 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8023 if (attr)
8024 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8025
7e314c57
JK
8026 set_die_type (die, range_type, cu);
8027
8028 /* set_die_type should be already done. */
b4ba55a1
JB
8029 set_descriptive_type (range_type, die, cu);
8030
7e314c57 8031 return range_type;
a02abb62 8032}
6e70227d 8033
f792889a 8034static struct type *
81a17f79
JB
8035read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8036{
8037 struct type *type;
81a17f79 8038
81a17f79
JB
8039 /* For now, we only support the C meaning of an unspecified type: void. */
8040
0114d602
DJ
8041 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8042 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8043
f792889a 8044 return set_die_type (die, type, cu);
81a17f79 8045}
a02abb62 8046
51545339
DJ
8047/* Trivial hash function for die_info: the hash value of a DIE
8048 is its offset in .debug_info for this objfile. */
8049
8050static hashval_t
8051die_hash (const void *item)
8052{
8053 const struct die_info *die = item;
9a619af0 8054
51545339
DJ
8055 return die->offset;
8056}
8057
8058/* Trivial comparison function for die_info structures: two DIEs
8059 are equal if they have the same offset. */
8060
8061static int
8062die_eq (const void *item_lhs, const void *item_rhs)
8063{
8064 const struct die_info *die_lhs = item_lhs;
8065 const struct die_info *die_rhs = item_rhs;
9a619af0 8066
51545339
DJ
8067 return die_lhs->offset == die_rhs->offset;
8068}
8069
c906108c
SS
8070/* Read a whole compilation unit into a linked list of dies. */
8071
f9aca02d 8072static struct die_info *
93311388 8073read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8074{
93311388 8075 struct die_reader_specs reader_specs;
98bfdba5 8076 int read_abbrevs = 0;
1d9ec526 8077 struct cleanup *back_to = NULL;
98bfdba5
PA
8078 struct die_info *die;
8079
8080 if (cu->dwarf2_abbrevs == NULL)
8081 {
8082 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8083 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8084 read_abbrevs = 1;
8085 }
93311388 8086
348e048f 8087 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8088 cu->die_hash
8089 = htab_create_alloc_ex (cu->header.length / 12,
8090 die_hash,
8091 die_eq,
8092 NULL,
8093 &cu->comp_unit_obstack,
8094 hashtab_obstack_allocate,
8095 dummy_obstack_deallocate);
8096
93311388
DE
8097 init_cu_die_reader (&reader_specs, cu);
8098
98bfdba5
PA
8099 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8100
8101 if (read_abbrevs)
8102 do_cleanups (back_to);
8103
8104 return die;
639d11d3
DC
8105}
8106
d97bc12b
DE
8107/* Main entry point for reading a DIE and all children.
8108 Read the DIE and dump it if requested. */
8109
8110static struct die_info *
93311388
DE
8111read_die_and_children (const struct die_reader_specs *reader,
8112 gdb_byte *info_ptr,
d97bc12b
DE
8113 gdb_byte **new_info_ptr,
8114 struct die_info *parent)
8115{
93311388 8116 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8117 new_info_ptr, parent);
8118
8119 if (dwarf2_die_debug)
8120 {
348e048f
DE
8121 fprintf_unfiltered (gdb_stdlog,
8122 "\nRead die from %s of %s:\n",
8123 reader->buffer == dwarf2_per_objfile->info.buffer
8124 ? ".debug_info"
8125 : reader->buffer == dwarf2_per_objfile->types.buffer
8126 ? ".debug_types"
8127 : "unknown section",
8128 reader->abfd->filename);
d97bc12b
DE
8129 dump_die (result, dwarf2_die_debug);
8130 }
8131
8132 return result;
8133}
8134
639d11d3
DC
8135/* Read a single die and all its descendents. Set the die's sibling
8136 field to NULL; set other fields in the die correctly, and set all
8137 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8138 location of the info_ptr after reading all of those dies. PARENT
8139 is the parent of the die in question. */
8140
8141static struct die_info *
93311388
DE
8142read_die_and_children_1 (const struct die_reader_specs *reader,
8143 gdb_byte *info_ptr,
d97bc12b
DE
8144 gdb_byte **new_info_ptr,
8145 struct die_info *parent)
639d11d3
DC
8146{
8147 struct die_info *die;
fe1b8b76 8148 gdb_byte *cur_ptr;
639d11d3
DC
8149 int has_children;
8150
93311388 8151 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8152 if (die == NULL)
8153 {
8154 *new_info_ptr = cur_ptr;
8155 return NULL;
8156 }
93311388 8157 store_in_ref_table (die, reader->cu);
639d11d3
DC
8158
8159 if (has_children)
348e048f 8160 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8161 else
8162 {
8163 die->child = NULL;
8164 *new_info_ptr = cur_ptr;
8165 }
8166
8167 die->sibling = NULL;
8168 die->parent = parent;
8169 return die;
8170}
8171
8172/* Read a die, all of its descendents, and all of its siblings; set
8173 all of the fields of all of the dies correctly. Arguments are as
8174 in read_die_and_children. */
8175
8176static struct die_info *
93311388
DE
8177read_die_and_siblings (const struct die_reader_specs *reader,
8178 gdb_byte *info_ptr,
fe1b8b76 8179 gdb_byte **new_info_ptr,
639d11d3
DC
8180 struct die_info *parent)
8181{
8182 struct die_info *first_die, *last_sibling;
fe1b8b76 8183 gdb_byte *cur_ptr;
639d11d3 8184
c906108c 8185 cur_ptr = info_ptr;
639d11d3
DC
8186 first_die = last_sibling = NULL;
8187
8188 while (1)
c906108c 8189 {
639d11d3 8190 struct die_info *die
93311388 8191 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8192
1d325ec1 8193 if (die == NULL)
c906108c 8194 {
639d11d3
DC
8195 *new_info_ptr = cur_ptr;
8196 return first_die;
c906108c 8197 }
1d325ec1
DJ
8198
8199 if (!first_die)
8200 first_die = die;
c906108c 8201 else
1d325ec1
DJ
8202 last_sibling->sibling = die;
8203
8204 last_sibling = die;
c906108c 8205 }
c906108c
SS
8206}
8207
93311388
DE
8208/* Read the die from the .debug_info section buffer. Set DIEP to
8209 point to a newly allocated die with its information, except for its
8210 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8211 whether the die has children or not. */
8212
8213static gdb_byte *
8214read_full_die (const struct die_reader_specs *reader,
8215 struct die_info **diep, gdb_byte *info_ptr,
8216 int *has_children)
8217{
8218 unsigned int abbrev_number, bytes_read, i, offset;
8219 struct abbrev_info *abbrev;
8220 struct die_info *die;
8221 struct dwarf2_cu *cu = reader->cu;
8222 bfd *abfd = reader->abfd;
8223
8224 offset = info_ptr - reader->buffer;
8225 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8226 info_ptr += bytes_read;
8227 if (!abbrev_number)
8228 {
8229 *diep = NULL;
8230 *has_children = 0;
8231 return info_ptr;
8232 }
8233
8234 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8235 if (!abbrev)
348e048f
DE
8236 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8237 abbrev_number,
8238 bfd_get_filename (abfd));
8239
93311388
DE
8240 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8241 die->offset = offset;
8242 die->tag = abbrev->tag;
8243 die->abbrev = abbrev_number;
8244
8245 die->num_attrs = abbrev->num_attrs;
8246
8247 for (i = 0; i < abbrev->num_attrs; ++i)
8248 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8249 abfd, info_ptr, cu);
8250
8251 *diep = die;
8252 *has_children = abbrev->has_children;
8253 return info_ptr;
8254}
8255
c906108c
SS
8256/* In DWARF version 2, the description of the debugging information is
8257 stored in a separate .debug_abbrev section. Before we read any
8258 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8259 in a hash table. This function also sets flags in CU describing
8260 the data found in the abbrev table. */
c906108c
SS
8261
8262static void
e7c27a73 8263dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8264{
e7c27a73 8265 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8266 gdb_byte *abbrev_ptr;
c906108c
SS
8267 struct abbrev_info *cur_abbrev;
8268 unsigned int abbrev_number, bytes_read, abbrev_name;
8269 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8270 struct attr_abbrev *cur_attrs;
8271 unsigned int allocated_attrs;
c906108c 8272
57349743 8273 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
8274 obstack_init (&cu->abbrev_obstack);
8275 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8276 (ABBREV_HASH_SIZE
8277 * sizeof (struct abbrev_info *)));
8278 memset (cu->dwarf2_abbrevs, 0,
8279 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8280
be391dca
TT
8281 dwarf2_read_section (dwarf2_per_objfile->objfile,
8282 &dwarf2_per_objfile->abbrev);
dce234bc 8283 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8284 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8285 abbrev_ptr += bytes_read;
8286
f3dd6933
DJ
8287 allocated_attrs = ATTR_ALLOC_CHUNK;
8288 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8289
c906108c
SS
8290 /* loop until we reach an abbrev number of 0 */
8291 while (abbrev_number)
8292 {
f3dd6933 8293 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8294
8295 /* read in abbrev header */
8296 cur_abbrev->number = abbrev_number;
8297 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8298 abbrev_ptr += bytes_read;
8299 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8300 abbrev_ptr += 1;
8301
72bf9492
DJ
8302 if (cur_abbrev->tag == DW_TAG_namespace)
8303 cu->has_namespace_info = 1;
8304
c906108c
SS
8305 /* now read in declarations */
8306 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8307 abbrev_ptr += bytes_read;
8308 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8309 abbrev_ptr += bytes_read;
8310 while (abbrev_name)
8311 {
f3dd6933 8312 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8313 {
f3dd6933
DJ
8314 allocated_attrs += ATTR_ALLOC_CHUNK;
8315 cur_attrs
8316 = xrealloc (cur_attrs, (allocated_attrs
8317 * sizeof (struct attr_abbrev)));
c906108c 8318 }
ae038cb0
DJ
8319
8320 /* Record whether this compilation unit might have
8321 inter-compilation-unit references. If we don't know what form
8322 this attribute will have, then it might potentially be a
8323 DW_FORM_ref_addr, so we conservatively expect inter-CU
8324 references. */
8325
8326 if (abbrev_form == DW_FORM_ref_addr
8327 || abbrev_form == DW_FORM_indirect)
8328 cu->has_form_ref_addr = 1;
8329
f3dd6933
DJ
8330 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8331 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8332 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8333 abbrev_ptr += bytes_read;
8334 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8335 abbrev_ptr += bytes_read;
8336 }
8337
f3dd6933
DJ
8338 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8339 (cur_abbrev->num_attrs
8340 * sizeof (struct attr_abbrev)));
8341 memcpy (cur_abbrev->attrs, cur_attrs,
8342 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8343
c906108c 8344 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8345 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8346 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8347
8348 /* Get next abbreviation.
8349 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8350 always properly terminated with an abbrev number of 0.
8351 Exit loop if we encounter an abbreviation which we have
8352 already read (which means we are about to read the abbreviations
8353 for the next compile unit) or if the end of the abbreviation
8354 table is reached. */
dce234bc
PP
8355 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8356 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8357 break;
8358 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8359 abbrev_ptr += bytes_read;
e7c27a73 8360 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8361 break;
8362 }
f3dd6933
DJ
8363
8364 xfree (cur_attrs);
c906108c
SS
8365}
8366
f3dd6933 8367/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8368
c906108c 8369static void
f3dd6933 8370dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8371{
f3dd6933 8372 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8373
f3dd6933
DJ
8374 obstack_free (&cu->abbrev_obstack, NULL);
8375 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8376}
8377
8378/* Lookup an abbrev_info structure in the abbrev hash table. */
8379
8380static struct abbrev_info *
e7c27a73 8381dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8382{
8383 unsigned int hash_number;
8384 struct abbrev_info *abbrev;
8385
8386 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8387 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8388
8389 while (abbrev)
8390 {
8391 if (abbrev->number == number)
8392 return abbrev;
8393 else
8394 abbrev = abbrev->next;
8395 }
8396 return NULL;
8397}
8398
72bf9492
DJ
8399/* Returns nonzero if TAG represents a type that we might generate a partial
8400 symbol for. */
8401
8402static int
8403is_type_tag_for_partial (int tag)
8404{
8405 switch (tag)
8406 {
8407#if 0
8408 /* Some types that would be reasonable to generate partial symbols for,
8409 that we don't at present. */
8410 case DW_TAG_array_type:
8411 case DW_TAG_file_type:
8412 case DW_TAG_ptr_to_member_type:
8413 case DW_TAG_set_type:
8414 case DW_TAG_string_type:
8415 case DW_TAG_subroutine_type:
8416#endif
8417 case DW_TAG_base_type:
8418 case DW_TAG_class_type:
680b30c7 8419 case DW_TAG_interface_type:
72bf9492
DJ
8420 case DW_TAG_enumeration_type:
8421 case DW_TAG_structure_type:
8422 case DW_TAG_subrange_type:
8423 case DW_TAG_typedef:
8424 case DW_TAG_union_type:
8425 return 1;
8426 default:
8427 return 0;
8428 }
8429}
8430
8431/* Load all DIEs that are interesting for partial symbols into memory. */
8432
8433static struct partial_die_info *
93311388
DE
8434load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8435 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8436{
8437 struct partial_die_info *part_die;
8438 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8439 struct abbrev_info *abbrev;
8440 unsigned int bytes_read;
5afb4e99 8441 unsigned int load_all = 0;
72bf9492
DJ
8442
8443 int nesting_level = 1;
8444
8445 parent_die = NULL;
8446 last_die = NULL;
8447
5afb4e99
DJ
8448 if (cu->per_cu && cu->per_cu->load_all_dies)
8449 load_all = 1;
8450
72bf9492
DJ
8451 cu->partial_dies
8452 = htab_create_alloc_ex (cu->header.length / 12,
8453 partial_die_hash,
8454 partial_die_eq,
8455 NULL,
8456 &cu->comp_unit_obstack,
8457 hashtab_obstack_allocate,
8458 dummy_obstack_deallocate);
8459
8460 part_die = obstack_alloc (&cu->comp_unit_obstack,
8461 sizeof (struct partial_die_info));
8462
8463 while (1)
8464 {
8465 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8466
8467 /* A NULL abbrev means the end of a series of children. */
8468 if (abbrev == NULL)
8469 {
8470 if (--nesting_level == 0)
8471 {
8472 /* PART_DIE was probably the last thing allocated on the
8473 comp_unit_obstack, so we could call obstack_free
8474 here. We don't do that because the waste is small,
8475 and will be cleaned up when we're done with this
8476 compilation unit. This way, we're also more robust
8477 against other users of the comp_unit_obstack. */
8478 return first_die;
8479 }
8480 info_ptr += bytes_read;
8481 last_die = parent_die;
8482 parent_die = parent_die->die_parent;
8483 continue;
8484 }
8485
98bfdba5
PA
8486 /* Check for template arguments. We never save these; if
8487 they're seen, we just mark the parent, and go on our way. */
8488 if (parent_die != NULL
8489 && cu->language == language_cplus
8490 && (abbrev->tag == DW_TAG_template_type_param
8491 || abbrev->tag == DW_TAG_template_value_param))
8492 {
8493 parent_die->has_template_arguments = 1;
8494
8495 if (!load_all)
8496 {
8497 /* We don't need a partial DIE for the template argument. */
8498 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8499 cu);
8500 continue;
8501 }
8502 }
8503
8504 /* We only recurse into subprograms looking for template arguments.
8505 Skip their other children. */
8506 if (!load_all
8507 && cu->language == language_cplus
8508 && parent_die != NULL
8509 && parent_die->tag == DW_TAG_subprogram)
8510 {
8511 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8512 continue;
8513 }
8514
5afb4e99
DJ
8515 /* Check whether this DIE is interesting enough to save. Normally
8516 we would not be interested in members here, but there may be
8517 later variables referencing them via DW_AT_specification (for
8518 static members). */
8519 if (!load_all
8520 && !is_type_tag_for_partial (abbrev->tag)
72bf9492
DJ
8521 && abbrev->tag != DW_TAG_enumerator
8522 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8523 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8524 && abbrev->tag != DW_TAG_variable
5afb4e99 8525 && abbrev->tag != DW_TAG_namespace
f55ee35c 8526 && abbrev->tag != DW_TAG_module
5afb4e99 8527 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8528 {
8529 /* Otherwise we skip to the next sibling, if any. */
93311388 8530 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8531 continue;
8532 }
8533
93311388
DE
8534 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8535 buffer, info_ptr, cu);
72bf9492
DJ
8536
8537 /* This two-pass algorithm for processing partial symbols has a
8538 high cost in cache pressure. Thus, handle some simple cases
8539 here which cover the majority of C partial symbols. DIEs
8540 which neither have specification tags in them, nor could have
8541 specification tags elsewhere pointing at them, can simply be
8542 processed and discarded.
8543
8544 This segment is also optional; scan_partial_symbols and
8545 add_partial_symbol will handle these DIEs if we chain
8546 them in normally. When compilers which do not emit large
8547 quantities of duplicate debug information are more common,
8548 this code can probably be removed. */
8549
8550 /* Any complete simple types at the top level (pretty much all
8551 of them, for a language without namespaces), can be processed
8552 directly. */
8553 if (parent_die == NULL
8554 && part_die->has_specification == 0
8555 && part_die->is_declaration == 0
8556 && (part_die->tag == DW_TAG_typedef
8557 || part_die->tag == DW_TAG_base_type
8558 || part_die->tag == DW_TAG_subrange_type))
8559 {
8560 if (building_psymtab && part_die->name != NULL)
04a679b8 8561 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8562 VAR_DOMAIN, LOC_TYPEDEF,
8563 &cu->objfile->static_psymbols,
8564 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8565 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8566 continue;
8567 }
8568
8569 /* If we're at the second level, and we're an enumerator, and
8570 our parent has no specification (meaning possibly lives in a
8571 namespace elsewhere), then we can add the partial symbol now
8572 instead of queueing it. */
8573 if (part_die->tag == DW_TAG_enumerator
8574 && parent_die != NULL
8575 && parent_die->die_parent == NULL
8576 && parent_die->tag == DW_TAG_enumeration_type
8577 && parent_die->has_specification == 0)
8578 {
8579 if (part_die->name == NULL)
e2e0b3e5 8580 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 8581 else if (building_psymtab)
04a679b8 8582 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8583 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8584 (cu->language == language_cplus
8585 || cu->language == language_java)
72bf9492
DJ
8586 ? &cu->objfile->global_psymbols
8587 : &cu->objfile->static_psymbols,
8588 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8589
93311388 8590 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8591 continue;
8592 }
8593
8594 /* We'll save this DIE so link it in. */
8595 part_die->die_parent = parent_die;
8596 part_die->die_sibling = NULL;
8597 part_die->die_child = NULL;
8598
8599 if (last_die && last_die == parent_die)
8600 last_die->die_child = part_die;
8601 else if (last_die)
8602 last_die->die_sibling = part_die;
8603
8604 last_die = part_die;
8605
8606 if (first_die == NULL)
8607 first_die = part_die;
8608
8609 /* Maybe add the DIE to the hash table. Not all DIEs that we
8610 find interesting need to be in the hash table, because we
8611 also have the parent/sibling/child chains; only those that we
8612 might refer to by offset later during partial symbol reading.
8613
8614 For now this means things that might have be the target of a
8615 DW_AT_specification, DW_AT_abstract_origin, or
8616 DW_AT_extension. DW_AT_extension will refer only to
8617 namespaces; DW_AT_abstract_origin refers to functions (and
8618 many things under the function DIE, but we do not recurse
8619 into function DIEs during partial symbol reading) and
8620 possibly variables as well; DW_AT_specification refers to
8621 declarations. Declarations ought to have the DW_AT_declaration
8622 flag. It happens that GCC forgets to put it in sometimes, but
8623 only for functions, not for types.
8624
8625 Adding more things than necessary to the hash table is harmless
8626 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8627 wasted time in find_partial_die, when we reread the compilation
8628 unit with load_all_dies set. */
72bf9492 8629
5afb4e99
DJ
8630 if (load_all
8631 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8632 || abbrev->tag == DW_TAG_variable
8633 || abbrev->tag == DW_TAG_namespace
8634 || part_die->is_declaration)
8635 {
8636 void **slot;
8637
8638 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8639 part_die->offset, INSERT);
8640 *slot = part_die;
8641 }
8642
8643 part_die = obstack_alloc (&cu->comp_unit_obstack,
8644 sizeof (struct partial_die_info));
8645
8646 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8647 we have no reason to follow the children of structures; for other
98bfdba5
PA
8648 languages we have to, so that we can get at method physnames
8649 to infer fully qualified class names, for DW_AT_specification,
8650 and for C++ template arguments. For C++, we also look one level
8651 inside functions to find template arguments (if the name of the
8652 function does not already contain the template arguments).
bc30ff58
JB
8653
8654 For Ada, we need to scan the children of subprograms and lexical
8655 blocks as well because Ada allows the definition of nested
8656 entities that could be interesting for the debugger, such as
8657 nested subprograms for instance. */
72bf9492 8658 if (last_die->has_children
5afb4e99
DJ
8659 && (load_all
8660 || last_die->tag == DW_TAG_namespace
f55ee35c 8661 || last_die->tag == DW_TAG_module
72bf9492 8662 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8663 || (cu->language == language_cplus
8664 && last_die->tag == DW_TAG_subprogram
8665 && (last_die->name == NULL
8666 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8667 || (cu->language != language_c
8668 && (last_die->tag == DW_TAG_class_type
680b30c7 8669 || last_die->tag == DW_TAG_interface_type
72bf9492 8670 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8671 || last_die->tag == DW_TAG_union_type))
8672 || (cu->language == language_ada
8673 && (last_die->tag == DW_TAG_subprogram
8674 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8675 {
8676 nesting_level++;
8677 parent_die = last_die;
8678 continue;
8679 }
8680
8681 /* Otherwise we skip to the next sibling, if any. */
93311388 8682 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8683
8684 /* Back to the top, do it again. */
8685 }
8686}
8687
c906108c
SS
8688/* Read a minimal amount of information into the minimal die structure. */
8689
fe1b8b76 8690static gdb_byte *
72bf9492
DJ
8691read_partial_die (struct partial_die_info *part_die,
8692 struct abbrev_info *abbrev,
8693 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8694 gdb_byte *buffer, gdb_byte *info_ptr,
8695 struct dwarf2_cu *cu)
c906108c 8696{
fa238c03 8697 unsigned int i;
c906108c 8698 struct attribute attr;
c5aa993b 8699 int has_low_pc_attr = 0;
c906108c
SS
8700 int has_high_pc_attr = 0;
8701
72bf9492 8702 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8703
93311388 8704 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8705
8706 info_ptr += abbrev_len;
8707
8708 if (abbrev == NULL)
8709 return info_ptr;
8710
c906108c
SS
8711 part_die->tag = abbrev->tag;
8712 part_die->has_children = abbrev->has_children;
c906108c
SS
8713
8714 for (i = 0; i < abbrev->num_attrs; ++i)
8715 {
e7c27a73 8716 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8717
8718 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8719 partial symbol table. */
c906108c
SS
8720 switch (attr.name)
8721 {
8722 case DW_AT_name:
71c25dea
TT
8723 switch (part_die->tag)
8724 {
8725 case DW_TAG_compile_unit:
348e048f 8726 case DW_TAG_type_unit:
71c25dea
TT
8727 /* Compilation units have a DW_AT_name that is a filename, not
8728 a source language identifier. */
8729 case DW_TAG_enumeration_type:
8730 case DW_TAG_enumerator:
8731 /* These tags always have simple identifiers already; no need
8732 to canonicalize them. */
8733 part_die->name = DW_STRING (&attr);
8734 break;
8735 default:
8736 part_die->name
8737 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8738 &cu->objfile->objfile_obstack);
71c25dea
TT
8739 break;
8740 }
c906108c 8741 break;
31ef98ae 8742 case DW_AT_linkage_name:
c906108c 8743 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8744 /* Note that both forms of linkage name might appear. We
8745 assume they will be the same, and we only store the last
8746 one we see. */
94af9270
KS
8747 if (cu->language == language_ada)
8748 part_die->name = DW_STRING (&attr);
c906108c
SS
8749 break;
8750 case DW_AT_low_pc:
8751 has_low_pc_attr = 1;
8752 part_die->lowpc = DW_ADDR (&attr);
8753 break;
8754 case DW_AT_high_pc:
8755 has_high_pc_attr = 1;
8756 part_die->highpc = DW_ADDR (&attr);
8757 break;
8758 case DW_AT_location:
8e19ed76
PS
8759 /* Support the .debug_loc offsets */
8760 if (attr_form_is_block (&attr))
8761 {
8762 part_die->locdesc = DW_BLOCK (&attr);
8763 }
3690dd37 8764 else if (attr_form_is_section_offset (&attr))
8e19ed76 8765 {
4d3c2250 8766 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8767 }
8768 else
8769 {
4d3c2250
KB
8770 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8771 "partial symbol information");
8e19ed76 8772 }
c906108c 8773 break;
c906108c
SS
8774 case DW_AT_external:
8775 part_die->is_external = DW_UNSND (&attr);
8776 break;
8777 case DW_AT_declaration:
8778 part_die->is_declaration = DW_UNSND (&attr);
8779 break;
8780 case DW_AT_type:
8781 part_die->has_type = 1;
8782 break;
8783 case DW_AT_abstract_origin:
8784 case DW_AT_specification:
72bf9492
DJ
8785 case DW_AT_extension:
8786 part_die->has_specification = 1;
c764a876 8787 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8788 break;
8789 case DW_AT_sibling:
8790 /* Ignore absolute siblings, they might point outside of
8791 the current compile unit. */
8792 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8793 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8794 else
93311388 8795 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8796 break;
fa4028e9
JB
8797 case DW_AT_byte_size:
8798 part_die->has_byte_size = 1;
8799 break;
68511cec
CES
8800 case DW_AT_calling_convention:
8801 /* DWARF doesn't provide a way to identify a program's source-level
8802 entry point. DW_AT_calling_convention attributes are only meant
8803 to describe functions' calling conventions.
8804
8805 However, because it's a necessary piece of information in
8806 Fortran, and because DW_CC_program is the only piece of debugging
8807 information whose definition refers to a 'main program' at all,
8808 several compilers have begun marking Fortran main programs with
8809 DW_CC_program --- even when those functions use the standard
8810 calling conventions.
8811
8812 So until DWARF specifies a way to provide this information and
8813 compilers pick up the new representation, we'll support this
8814 practice. */
8815 if (DW_UNSND (&attr) == DW_CC_program
8816 && cu->language == language_fortran)
8817 set_main_name (part_die->name);
8818 break;
c906108c
SS
8819 default:
8820 break;
8821 }
8822 }
8823
c906108c
SS
8824 /* When using the GNU linker, .gnu.linkonce. sections are used to
8825 eliminate duplicate copies of functions and vtables and such.
8826 The linker will arbitrarily choose one and discard the others.
8827 The AT_*_pc values for such functions refer to local labels in
8828 these sections. If the section from that file was discarded, the
8829 labels are not in the output, so the relocs get a value of 0.
8830 If this is a discarded function, mark the pc bounds as invalid,
8831 so that GDB will ignore it. */
8832 if (has_low_pc_attr && has_high_pc_attr
8833 && part_die->lowpc < part_die->highpc
8834 && (part_die->lowpc != 0
72dca2f5 8835 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8836 part_die->has_pc_info = 1;
85cbf3d3 8837
c906108c
SS
8838 return info_ptr;
8839}
8840
72bf9492
DJ
8841/* Find a cached partial DIE at OFFSET in CU. */
8842
8843static struct partial_die_info *
c764a876 8844find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8845{
8846 struct partial_die_info *lookup_die = NULL;
8847 struct partial_die_info part_die;
8848
8849 part_die.offset = offset;
8850 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8851
72bf9492
DJ
8852 return lookup_die;
8853}
8854
348e048f
DE
8855/* Find a partial DIE at OFFSET, which may or may not be in CU,
8856 except in the case of .debug_types DIEs which do not reference
8857 outside their CU (they do however referencing other types via
8858 DW_FORM_sig8). */
72bf9492
DJ
8859
8860static struct partial_die_info *
c764a876 8861find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8862{
5afb4e99
DJ
8863 struct dwarf2_per_cu_data *per_cu = NULL;
8864 struct partial_die_info *pd = NULL;
72bf9492 8865
348e048f
DE
8866 if (cu->per_cu->from_debug_types)
8867 {
8868 pd = find_partial_die_in_comp_unit (offset, cu);
8869 if (pd != NULL)
8870 return pd;
8871 goto not_found;
8872 }
8873
45452591 8874 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8875 {
8876 pd = find_partial_die_in_comp_unit (offset, cu);
8877 if (pd != NULL)
8878 return pd;
8879 }
72bf9492 8880
ae038cb0
DJ
8881 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8882
98bfdba5
PA
8883 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8884 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8885
8886 per_cu->cu->last_used = 0;
5afb4e99
DJ
8887 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8888
8889 if (pd == NULL && per_cu->load_all_dies == 0)
8890 {
8891 struct cleanup *back_to;
8892 struct partial_die_info comp_unit_die;
8893 struct abbrev_info *abbrev;
8894 unsigned int bytes_read;
8895 char *info_ptr;
8896
8897 per_cu->load_all_dies = 1;
8898
8899 /* Re-read the DIEs. */
8900 back_to = make_cleanup (null_cleanup, 0);
8901 if (per_cu->cu->dwarf2_abbrevs == NULL)
8902 {
8903 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 8904 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 8905 }
dce234bc 8906 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
8907 + per_cu->cu->header.offset
8908 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
8909 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8910 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
8911 per_cu->cu->objfile->obfd,
8912 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
8913 per_cu->cu);
8914 if (comp_unit_die.has_children)
93311388
DE
8915 load_partial_dies (per_cu->cu->objfile->obfd,
8916 dwarf2_per_objfile->info.buffer, info_ptr,
8917 0, per_cu->cu);
5afb4e99
DJ
8918 do_cleanups (back_to);
8919
8920 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8921 }
8922
348e048f
DE
8923 not_found:
8924
5afb4e99
DJ
8925 if (pd == NULL)
8926 internal_error (__FILE__, __LINE__,
c764a876 8927 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
8928 offset, bfd_get_filename (cu->objfile->obfd));
8929 return pd;
72bf9492
DJ
8930}
8931
8932/* Adjust PART_DIE before generating a symbol for it. This function
8933 may set the is_external flag or change the DIE's name. */
8934
8935static void
8936fixup_partial_die (struct partial_die_info *part_die,
8937 struct dwarf2_cu *cu)
8938{
8939 /* If we found a reference attribute and the DIE has no name, try
8940 to find a name in the referred to DIE. */
8941
8942 if (part_die->name == NULL && part_die->has_specification)
8943 {
8944 struct partial_die_info *spec_die;
72bf9492 8945
10b3939b 8946 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 8947
10b3939b 8948 fixup_partial_die (spec_die, cu);
72bf9492
DJ
8949
8950 if (spec_die->name)
8951 {
8952 part_die->name = spec_die->name;
8953
8954 /* Copy DW_AT_external attribute if it is set. */
8955 if (spec_die->is_external)
8956 part_die->is_external = spec_die->is_external;
8957 }
8958 }
8959
8960 /* Set default names for some unnamed DIEs. */
8961 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
8962 || part_die->tag == DW_TAG_class_type))
8963 part_die->name = "(anonymous class)";
8964
8965 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
8966 part_die->name = "(anonymous namespace)";
8967
8968 if (part_die->tag == DW_TAG_structure_type
8969 || part_die->tag == DW_TAG_class_type
8970 || part_die->tag == DW_TAG_union_type)
8971 guess_structure_name (part_die, cu);
8972}
8973
a8329558 8974/* Read an attribute value described by an attribute form. */
c906108c 8975
fe1b8b76 8976static gdb_byte *
a8329558 8977read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 8978 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 8979 struct dwarf2_cu *cu)
c906108c 8980{
e7c27a73 8981 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
8982 unsigned int bytes_read;
8983 struct dwarf_block *blk;
8984
a8329558
KW
8985 attr->form = form;
8986 switch (form)
c906108c 8987 {
c906108c 8988 case DW_FORM_ref_addr:
ae411497
TT
8989 if (cu->header.version == 2)
8990 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
8991 else
8992 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
8993 info_ptr += bytes_read;
8994 break;
8995 case DW_FORM_addr:
e7c27a73 8996 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 8997 info_ptr += bytes_read;
c906108c
SS
8998 break;
8999 case DW_FORM_block2:
7b5a2f43 9000 blk = dwarf_alloc_block (cu);
c906108c
SS
9001 blk->size = read_2_bytes (abfd, info_ptr);
9002 info_ptr += 2;
9003 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9004 info_ptr += blk->size;
9005 DW_BLOCK (attr) = blk;
9006 break;
9007 case DW_FORM_block4:
7b5a2f43 9008 blk = dwarf_alloc_block (cu);
c906108c
SS
9009 blk->size = read_4_bytes (abfd, info_ptr);
9010 info_ptr += 4;
9011 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9012 info_ptr += blk->size;
9013 DW_BLOCK (attr) = blk;
9014 break;
9015 case DW_FORM_data2:
9016 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9017 info_ptr += 2;
9018 break;
9019 case DW_FORM_data4:
9020 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9021 info_ptr += 4;
9022 break;
9023 case DW_FORM_data8:
9024 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9025 info_ptr += 8;
9026 break;
2dc7f7b3
TT
9027 case DW_FORM_sec_offset:
9028 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9029 info_ptr += bytes_read;
9030 break;
c906108c 9031 case DW_FORM_string:
9b1c24c8 9032 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9033 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9034 info_ptr += bytes_read;
9035 break;
4bdf3d34
JJ
9036 case DW_FORM_strp:
9037 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9038 &bytes_read);
8285870a 9039 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9040 info_ptr += bytes_read;
9041 break;
2dc7f7b3 9042 case DW_FORM_exprloc:
c906108c 9043 case DW_FORM_block:
7b5a2f43 9044 blk = dwarf_alloc_block (cu);
c906108c
SS
9045 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9046 info_ptr += bytes_read;
9047 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9048 info_ptr += blk->size;
9049 DW_BLOCK (attr) = blk;
9050 break;
9051 case DW_FORM_block1:
7b5a2f43 9052 blk = dwarf_alloc_block (cu);
c906108c
SS
9053 blk->size = read_1_byte (abfd, info_ptr);
9054 info_ptr += 1;
9055 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9056 info_ptr += blk->size;
9057 DW_BLOCK (attr) = blk;
9058 break;
9059 case DW_FORM_data1:
9060 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9061 info_ptr += 1;
9062 break;
9063 case DW_FORM_flag:
9064 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9065 info_ptr += 1;
9066 break;
2dc7f7b3
TT
9067 case DW_FORM_flag_present:
9068 DW_UNSND (attr) = 1;
9069 break;
c906108c
SS
9070 case DW_FORM_sdata:
9071 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9072 info_ptr += bytes_read;
9073 break;
9074 case DW_FORM_udata:
9075 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9076 info_ptr += bytes_read;
9077 break;
9078 case DW_FORM_ref1:
10b3939b 9079 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9080 info_ptr += 1;
9081 break;
9082 case DW_FORM_ref2:
10b3939b 9083 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9084 info_ptr += 2;
9085 break;
9086 case DW_FORM_ref4:
10b3939b 9087 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9088 info_ptr += 4;
9089 break;
613e1657 9090 case DW_FORM_ref8:
10b3939b 9091 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9092 info_ptr += 8;
9093 break;
348e048f
DE
9094 case DW_FORM_sig8:
9095 /* Convert the signature to something we can record in DW_UNSND
9096 for later lookup.
9097 NOTE: This is NULL if the type wasn't found. */
9098 DW_SIGNATURED_TYPE (attr) =
9099 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9100 info_ptr += 8;
9101 break;
c906108c 9102 case DW_FORM_ref_udata:
10b3939b
DJ
9103 DW_ADDR (attr) = (cu->header.offset
9104 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9105 info_ptr += bytes_read;
9106 break;
c906108c 9107 case DW_FORM_indirect:
a8329558
KW
9108 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9109 info_ptr += bytes_read;
e7c27a73 9110 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9111 break;
c906108c 9112 default:
8a3fe4f8 9113 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9114 dwarf_form_name (form),
9115 bfd_get_filename (abfd));
c906108c 9116 }
28e94949
JB
9117
9118 /* We have seen instances where the compiler tried to emit a byte
9119 size attribute of -1 which ended up being encoded as an unsigned
9120 0xffffffff. Although 0xffffffff is technically a valid size value,
9121 an object of this size seems pretty unlikely so we can relatively
9122 safely treat these cases as if the size attribute was invalid and
9123 treat them as zero by default. */
9124 if (attr->name == DW_AT_byte_size
9125 && form == DW_FORM_data4
9126 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9127 {
9128 complaint
9129 (&symfile_complaints,
43bbcdc2
PH
9130 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9131 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9132 DW_UNSND (attr) = 0;
9133 }
28e94949 9134
c906108c
SS
9135 return info_ptr;
9136}
9137
a8329558
KW
9138/* Read an attribute described by an abbreviated attribute. */
9139
fe1b8b76 9140static gdb_byte *
a8329558 9141read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9142 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9143{
9144 attr->name = abbrev->name;
e7c27a73 9145 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9146}
9147
c906108c
SS
9148/* read dwarf information from a buffer */
9149
9150static unsigned int
fe1b8b76 9151read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9152{
fe1b8b76 9153 return bfd_get_8 (abfd, buf);
c906108c
SS
9154}
9155
9156static int
fe1b8b76 9157read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9158{
fe1b8b76 9159 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9160}
9161
9162static unsigned int
fe1b8b76 9163read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9164{
fe1b8b76 9165 return bfd_get_16 (abfd, buf);
c906108c
SS
9166}
9167
9168static int
fe1b8b76 9169read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9170{
fe1b8b76 9171 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9172}
9173
9174static unsigned int
fe1b8b76 9175read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9176{
fe1b8b76 9177 return bfd_get_32 (abfd, buf);
c906108c
SS
9178}
9179
9180static int
fe1b8b76 9181read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9182{
fe1b8b76 9183 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9184}
9185
93311388 9186static ULONGEST
fe1b8b76 9187read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9188{
fe1b8b76 9189 return bfd_get_64 (abfd, buf);
c906108c
SS
9190}
9191
9192static CORE_ADDR
fe1b8b76 9193read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9194 unsigned int *bytes_read)
c906108c 9195{
e7c27a73 9196 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9197 CORE_ADDR retval = 0;
9198
107d2387 9199 if (cu_header->signed_addr_p)
c906108c 9200 {
107d2387
AC
9201 switch (cu_header->addr_size)
9202 {
9203 case 2:
fe1b8b76 9204 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9205 break;
9206 case 4:
fe1b8b76 9207 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9208 break;
9209 case 8:
fe1b8b76 9210 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9211 break;
9212 default:
8e65ff28 9213 internal_error (__FILE__, __LINE__,
e2e0b3e5 9214 _("read_address: bad switch, signed [in module %s]"),
659b0389 9215 bfd_get_filename (abfd));
107d2387
AC
9216 }
9217 }
9218 else
9219 {
9220 switch (cu_header->addr_size)
9221 {
9222 case 2:
fe1b8b76 9223 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9224 break;
9225 case 4:
fe1b8b76 9226 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9227 break;
9228 case 8:
fe1b8b76 9229 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9230 break;
9231 default:
8e65ff28 9232 internal_error (__FILE__, __LINE__,
e2e0b3e5 9233 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 9234 bfd_get_filename (abfd));
107d2387 9235 }
c906108c 9236 }
64367e0a 9237
107d2387
AC
9238 *bytes_read = cu_header->addr_size;
9239 return retval;
c906108c
SS
9240}
9241
f7ef9339 9242/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9243 specification allows the initial length to take up either 4 bytes
9244 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9245 bytes describe the length and all offsets will be 8 bytes in length
9246 instead of 4.
9247
f7ef9339
KB
9248 An older, non-standard 64-bit format is also handled by this
9249 function. The older format in question stores the initial length
9250 as an 8-byte quantity without an escape value. Lengths greater
9251 than 2^32 aren't very common which means that the initial 4 bytes
9252 is almost always zero. Since a length value of zero doesn't make
9253 sense for the 32-bit format, this initial zero can be considered to
9254 be an escape value which indicates the presence of the older 64-bit
9255 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9256 greater than 4GB. If it becomes necessary to handle lengths
9257 somewhat larger than 4GB, we could allow other small values (such
9258 as the non-sensical values of 1, 2, and 3) to also be used as
9259 escape values indicating the presence of the old format.
f7ef9339 9260
917c78fc
MK
9261 The value returned via bytes_read should be used to increment the
9262 relevant pointer after calling read_initial_length().
c764a876 9263
613e1657
KB
9264 [ Note: read_initial_length() and read_offset() are based on the
9265 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9266 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9267 from:
9268
f7ef9339 9269 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9270
613e1657
KB
9271 This document is only a draft and is subject to change. (So beware.)
9272
f7ef9339 9273 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9274 determined empirically by examining 64-bit ELF files produced by
9275 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9276
9277 - Kevin, July 16, 2002
613e1657
KB
9278 ] */
9279
9280static LONGEST
c764a876 9281read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9282{
fe1b8b76 9283 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9284
dd373385 9285 if (length == 0xffffffff)
613e1657 9286 {
fe1b8b76 9287 length = bfd_get_64 (abfd, buf + 4);
613e1657 9288 *bytes_read = 12;
613e1657 9289 }
dd373385 9290 else if (length == 0)
f7ef9339 9291 {
dd373385 9292 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9293 length = bfd_get_64 (abfd, buf);
f7ef9339 9294 *bytes_read = 8;
f7ef9339 9295 }
613e1657
KB
9296 else
9297 {
9298 *bytes_read = 4;
613e1657
KB
9299 }
9300
c764a876
DE
9301 return length;
9302}
dd373385 9303
c764a876
DE
9304/* Cover function for read_initial_length.
9305 Returns the length of the object at BUF, and stores the size of the
9306 initial length in *BYTES_READ and stores the size that offsets will be in
9307 *OFFSET_SIZE.
9308 If the initial length size is not equivalent to that specified in
9309 CU_HEADER then issue a complaint.
9310 This is useful when reading non-comp-unit headers. */
dd373385 9311
c764a876
DE
9312static LONGEST
9313read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9314 const struct comp_unit_head *cu_header,
9315 unsigned int *bytes_read,
9316 unsigned int *offset_size)
9317{
9318 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9319
9320 gdb_assert (cu_header->initial_length_size == 4
9321 || cu_header->initial_length_size == 8
9322 || cu_header->initial_length_size == 12);
9323
9324 if (cu_header->initial_length_size != *bytes_read)
9325 complaint (&symfile_complaints,
9326 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9327
c764a876 9328 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9329 return length;
613e1657
KB
9330}
9331
9332/* Read an offset from the data stream. The size of the offset is
917c78fc 9333 given by cu_header->offset_size. */
613e1657
KB
9334
9335static LONGEST
fe1b8b76 9336read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9337 unsigned int *bytes_read)
c764a876
DE
9338{
9339 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9340
c764a876
DE
9341 *bytes_read = cu_header->offset_size;
9342 return offset;
9343}
9344
9345/* Read an offset from the data stream. */
9346
9347static LONGEST
9348read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9349{
9350 LONGEST retval = 0;
9351
c764a876 9352 switch (offset_size)
613e1657
KB
9353 {
9354 case 4:
fe1b8b76 9355 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9356 break;
9357 case 8:
fe1b8b76 9358 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9359 break;
9360 default:
8e65ff28 9361 internal_error (__FILE__, __LINE__,
c764a876 9362 _("read_offset_1: bad switch [in module %s]"),
659b0389 9363 bfd_get_filename (abfd));
613e1657
KB
9364 }
9365
917c78fc 9366 return retval;
613e1657
KB
9367}
9368
fe1b8b76
JB
9369static gdb_byte *
9370read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9371{
9372 /* If the size of a host char is 8 bits, we can return a pointer
9373 to the buffer, otherwise we have to copy the data to a buffer
9374 allocated on the temporary obstack. */
4bdf3d34 9375 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9376 return buf;
c906108c
SS
9377}
9378
9379static char *
9b1c24c8 9380read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9381{
9382 /* If the size of a host char is 8 bits, we can return a pointer
9383 to the string, otherwise we have to copy the string to a buffer
9384 allocated on the temporary obstack. */
4bdf3d34 9385 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9386 if (*buf == '\0')
9387 {
9388 *bytes_read_ptr = 1;
9389 return NULL;
9390 }
fe1b8b76
JB
9391 *bytes_read_ptr = strlen ((char *) buf) + 1;
9392 return (char *) buf;
4bdf3d34
JJ
9393}
9394
9395static char *
fe1b8b76 9396read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9397 const struct comp_unit_head *cu_header,
9398 unsigned int *bytes_read_ptr)
9399{
c764a876 9400 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9401
be391dca 9402 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9403 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9404 {
8a3fe4f8 9405 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9406 bfd_get_filename (abfd));
4bdf3d34 9407 return NULL;
c906108c 9408 }
dce234bc 9409 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9410 {
8a3fe4f8 9411 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 9412 bfd_get_filename (abfd));
c906108c
SS
9413 return NULL;
9414 }
4bdf3d34 9415 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9416 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9417 return NULL;
dce234bc 9418 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9419}
9420
ce5d95e1 9421static unsigned long
fe1b8b76 9422read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9423{
ce5d95e1
JB
9424 unsigned long result;
9425 unsigned int num_read;
c906108c
SS
9426 int i, shift;
9427 unsigned char byte;
9428
9429 result = 0;
9430 shift = 0;
9431 num_read = 0;
9432 i = 0;
9433 while (1)
9434 {
fe1b8b76 9435 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9436 buf++;
9437 num_read++;
ce5d95e1 9438 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9439 if ((byte & 128) == 0)
9440 {
9441 break;
9442 }
9443 shift += 7;
9444 }
9445 *bytes_read_ptr = num_read;
9446 return result;
9447}
9448
ce5d95e1 9449static long
fe1b8b76 9450read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9451{
ce5d95e1 9452 long result;
77e0b926 9453 int i, shift, num_read;
c906108c
SS
9454 unsigned char byte;
9455
9456 result = 0;
9457 shift = 0;
c906108c
SS
9458 num_read = 0;
9459 i = 0;
9460 while (1)
9461 {
fe1b8b76 9462 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9463 buf++;
9464 num_read++;
ce5d95e1 9465 result |= ((long)(byte & 127) << shift);
c906108c
SS
9466 shift += 7;
9467 if ((byte & 128) == 0)
9468 {
9469 break;
9470 }
9471 }
77e0b926
DJ
9472 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9473 result |= -(((long)1) << shift);
c906108c
SS
9474 *bytes_read_ptr = num_read;
9475 return result;
9476}
9477
4bb7a0a7
DJ
9478/* Return a pointer to just past the end of an LEB128 number in BUF. */
9479
fe1b8b76
JB
9480static gdb_byte *
9481skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9482{
9483 int byte;
9484
9485 while (1)
9486 {
fe1b8b76 9487 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9488 buf++;
9489 if ((byte & 128) == 0)
9490 return buf;
9491 }
9492}
9493
c906108c 9494static void
e142c38c 9495set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9496{
9497 switch (lang)
9498 {
9499 case DW_LANG_C89:
76bee0cc 9500 case DW_LANG_C99:
c906108c 9501 case DW_LANG_C:
e142c38c 9502 cu->language = language_c;
c906108c
SS
9503 break;
9504 case DW_LANG_C_plus_plus:
e142c38c 9505 cu->language = language_cplus;
c906108c 9506 break;
6aecb9c2
JB
9507 case DW_LANG_D:
9508 cu->language = language_d;
9509 break;
c906108c
SS
9510 case DW_LANG_Fortran77:
9511 case DW_LANG_Fortran90:
b21b22e0 9512 case DW_LANG_Fortran95:
e142c38c 9513 cu->language = language_fortran;
c906108c
SS
9514 break;
9515 case DW_LANG_Mips_Assembler:
e142c38c 9516 cu->language = language_asm;
c906108c 9517 break;
bebd888e 9518 case DW_LANG_Java:
e142c38c 9519 cu->language = language_java;
bebd888e 9520 break;
c906108c 9521 case DW_LANG_Ada83:
8aaf0b47 9522 case DW_LANG_Ada95:
bc5f45f8
JB
9523 cu->language = language_ada;
9524 break;
72019c9c
GM
9525 case DW_LANG_Modula2:
9526 cu->language = language_m2;
9527 break;
fe8e67fd
PM
9528 case DW_LANG_Pascal83:
9529 cu->language = language_pascal;
9530 break;
22566fbd
DJ
9531 case DW_LANG_ObjC:
9532 cu->language = language_objc;
9533 break;
c906108c
SS
9534 case DW_LANG_Cobol74:
9535 case DW_LANG_Cobol85:
c906108c 9536 default:
e142c38c 9537 cu->language = language_minimal;
c906108c
SS
9538 break;
9539 }
e142c38c 9540 cu->language_defn = language_def (cu->language);
c906108c
SS
9541}
9542
9543/* Return the named attribute or NULL if not there. */
9544
9545static struct attribute *
e142c38c 9546dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9547{
9548 unsigned int i;
9549 struct attribute *spec = NULL;
9550
9551 for (i = 0; i < die->num_attrs; ++i)
9552 {
9553 if (die->attrs[i].name == name)
10b3939b 9554 return &die->attrs[i];
c906108c
SS
9555 if (die->attrs[i].name == DW_AT_specification
9556 || die->attrs[i].name == DW_AT_abstract_origin)
9557 spec = &die->attrs[i];
9558 }
c906108c 9559
10b3939b 9560 if (spec)
f2f0e013
DJ
9561 {
9562 die = follow_die_ref (die, spec, &cu);
9563 return dwarf2_attr (die, name, cu);
9564 }
c5aa993b 9565
c906108c
SS
9566 return NULL;
9567}
9568
348e048f
DE
9569/* Return the named attribute or NULL if not there,
9570 but do not follow DW_AT_specification, etc.
9571 This is for use in contexts where we're reading .debug_types dies.
9572 Following DW_AT_specification, DW_AT_abstract_origin will take us
9573 back up the chain, and we want to go down. */
9574
9575static struct attribute *
9576dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9577 struct dwarf2_cu *cu)
9578{
9579 unsigned int i;
9580
9581 for (i = 0; i < die->num_attrs; ++i)
9582 if (die->attrs[i].name == name)
9583 return &die->attrs[i];
9584
9585 return NULL;
9586}
9587
05cf31d1
JB
9588/* Return non-zero iff the attribute NAME is defined for the given DIE,
9589 and holds a non-zero value. This function should only be used for
2dc7f7b3 9590 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9591
9592static int
9593dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9594{
9595 struct attribute *attr = dwarf2_attr (die, name, cu);
9596
9597 return (attr && DW_UNSND (attr));
9598}
9599
3ca72b44 9600static int
e142c38c 9601die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9602{
05cf31d1
JB
9603 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9604 which value is non-zero. However, we have to be careful with
9605 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9606 (via dwarf2_flag_true_p) follows this attribute. So we may
9607 end up accidently finding a declaration attribute that belongs
9608 to a different DIE referenced by the specification attribute,
9609 even though the given DIE does not have a declaration attribute. */
9610 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9611 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9612}
9613
63d06c5c 9614/* Return the die giving the specification for DIE, if there is
f2f0e013 9615 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9616 containing the return value on output. If there is no
9617 specification, but there is an abstract origin, that is
9618 returned. */
63d06c5c
DC
9619
9620static struct die_info *
f2f0e013 9621die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9622{
f2f0e013
DJ
9623 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9624 *spec_cu);
63d06c5c 9625
edb3359d
DJ
9626 if (spec_attr == NULL)
9627 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9628
63d06c5c
DC
9629 if (spec_attr == NULL)
9630 return NULL;
9631 else
f2f0e013 9632 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9633}
c906108c 9634
debd256d
JB
9635/* Free the line_header structure *LH, and any arrays and strings it
9636 refers to. */
9637static void
9638free_line_header (struct line_header *lh)
9639{
9640 if (lh->standard_opcode_lengths)
a8bc7b56 9641 xfree (lh->standard_opcode_lengths);
debd256d
JB
9642
9643 /* Remember that all the lh->file_names[i].name pointers are
9644 pointers into debug_line_buffer, and don't need to be freed. */
9645 if (lh->file_names)
a8bc7b56 9646 xfree (lh->file_names);
debd256d
JB
9647
9648 /* Similarly for the include directory names. */
9649 if (lh->include_dirs)
a8bc7b56 9650 xfree (lh->include_dirs);
debd256d 9651
a8bc7b56 9652 xfree (lh);
debd256d
JB
9653}
9654
9655
9656/* Add an entry to LH's include directory table. */
9657static void
9658add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9659{
debd256d
JB
9660 /* Grow the array if necessary. */
9661 if (lh->include_dirs_size == 0)
c5aa993b 9662 {
debd256d
JB
9663 lh->include_dirs_size = 1; /* for testing */
9664 lh->include_dirs = xmalloc (lh->include_dirs_size
9665 * sizeof (*lh->include_dirs));
9666 }
9667 else if (lh->num_include_dirs >= lh->include_dirs_size)
9668 {
9669 lh->include_dirs_size *= 2;
9670 lh->include_dirs = xrealloc (lh->include_dirs,
9671 (lh->include_dirs_size
9672 * sizeof (*lh->include_dirs)));
c5aa993b 9673 }
c906108c 9674
debd256d
JB
9675 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9676}
6e70227d 9677
debd256d
JB
9678
9679/* Add an entry to LH's file name table. */
9680static void
9681add_file_name (struct line_header *lh,
9682 char *name,
9683 unsigned int dir_index,
9684 unsigned int mod_time,
9685 unsigned int length)
9686{
9687 struct file_entry *fe;
9688
9689 /* Grow the array if necessary. */
9690 if (lh->file_names_size == 0)
9691 {
9692 lh->file_names_size = 1; /* for testing */
9693 lh->file_names = xmalloc (lh->file_names_size
9694 * sizeof (*lh->file_names));
9695 }
9696 else if (lh->num_file_names >= lh->file_names_size)
9697 {
9698 lh->file_names_size *= 2;
9699 lh->file_names = xrealloc (lh->file_names,
9700 (lh->file_names_size
9701 * sizeof (*lh->file_names)));
9702 }
9703
9704 fe = &lh->file_names[lh->num_file_names++];
9705 fe->name = name;
9706 fe->dir_index = dir_index;
9707 fe->mod_time = mod_time;
9708 fe->length = length;
aaa75496 9709 fe->included_p = 0;
cb1df416 9710 fe->symtab = NULL;
debd256d 9711}
6e70227d 9712
debd256d
JB
9713
9714/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9715 .debug_line, according to the endianness of ABFD. Return a pointer
9716 to a struct line_header, allocated using xmalloc.
debd256d
JB
9717
9718 NOTE: the strings in the include directory and file name tables of
9719 the returned object point into debug_line_buffer, and must not be
9720 freed. */
9721static struct line_header *
9722dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9723 struct dwarf2_cu *cu)
debd256d
JB
9724{
9725 struct cleanup *back_to;
9726 struct line_header *lh;
fe1b8b76 9727 gdb_byte *line_ptr;
c764a876 9728 unsigned int bytes_read, offset_size;
debd256d
JB
9729 int i;
9730 char *cur_dir, *cur_file;
9731
be391dca 9732 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9733 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9734 {
e2e0b3e5 9735 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9736 return 0;
9737 }
9738
a738430d
MK
9739 /* Make sure that at least there's room for the total_length field.
9740 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9741 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 9742 {
4d3c2250 9743 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9744 return 0;
9745 }
9746
9747 lh = xmalloc (sizeof (*lh));
9748 memset (lh, 0, sizeof (*lh));
9749 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9750 (void *) lh);
9751
dce234bc 9752 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9753
a738430d 9754 /* Read in the header. */
6e70227d 9755 lh->total_length =
c764a876
DE
9756 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9757 &bytes_read, &offset_size);
debd256d 9758 line_ptr += bytes_read;
dce234bc
PP
9759 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9760 + dwarf2_per_objfile->line.size))
debd256d 9761 {
4d3c2250 9762 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9763 return 0;
9764 }
9765 lh->statement_program_end = line_ptr + lh->total_length;
9766 lh->version = read_2_bytes (abfd, line_ptr);
9767 line_ptr += 2;
c764a876
DE
9768 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9769 line_ptr += offset_size;
debd256d
JB
9770 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9771 line_ptr += 1;
2dc7f7b3
TT
9772 if (lh->version >= 4)
9773 {
9774 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9775 line_ptr += 1;
9776 }
9777 else
9778 lh->maximum_ops_per_instruction = 1;
9779
9780 if (lh->maximum_ops_per_instruction == 0)
9781 {
9782 lh->maximum_ops_per_instruction = 1;
9783 complaint (&symfile_complaints,
9784 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9785 }
9786
debd256d
JB
9787 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9788 line_ptr += 1;
9789 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9790 line_ptr += 1;
9791 lh->line_range = read_1_byte (abfd, line_ptr);
9792 line_ptr += 1;
9793 lh->opcode_base = read_1_byte (abfd, line_ptr);
9794 line_ptr += 1;
9795 lh->standard_opcode_lengths
fe1b8b76 9796 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9797
9798 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9799 for (i = 1; i < lh->opcode_base; ++i)
9800 {
9801 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9802 line_ptr += 1;
9803 }
9804
a738430d 9805 /* Read directory table. */
9b1c24c8 9806 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9807 {
9808 line_ptr += bytes_read;
9809 add_include_dir (lh, cur_dir);
9810 }
9811 line_ptr += bytes_read;
9812
a738430d 9813 /* Read file name table. */
9b1c24c8 9814 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9815 {
9816 unsigned int dir_index, mod_time, length;
9817
9818 line_ptr += bytes_read;
9819 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9820 line_ptr += bytes_read;
9821 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9822 line_ptr += bytes_read;
9823 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9824 line_ptr += bytes_read;
9825
9826 add_file_name (lh, cur_file, dir_index, mod_time, length);
9827 }
9828 line_ptr += bytes_read;
6e70227d 9829 lh->statement_program_start = line_ptr;
debd256d 9830
dce234bc
PP
9831 if (line_ptr > (dwarf2_per_objfile->line.buffer
9832 + dwarf2_per_objfile->line.size))
4d3c2250 9833 complaint (&symfile_complaints,
e2e0b3e5 9834 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
9835
9836 discard_cleanups (back_to);
9837 return lh;
9838}
c906108c 9839
5fb290d7
DJ
9840/* This function exists to work around a bug in certain compilers
9841 (particularly GCC 2.95), in which the first line number marker of a
9842 function does not show up until after the prologue, right before
9843 the second line number marker. This function shifts ADDRESS down
9844 to the beginning of the function if necessary, and is called on
9845 addresses passed to record_line. */
9846
9847static CORE_ADDR
e142c38c 9848check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
9849{
9850 struct function_range *fn;
9851
9852 /* Find the function_range containing address. */
e142c38c 9853 if (!cu->first_fn)
5fb290d7
DJ
9854 return address;
9855
e142c38c
DJ
9856 if (!cu->cached_fn)
9857 cu->cached_fn = cu->first_fn;
5fb290d7 9858
e142c38c 9859 fn = cu->cached_fn;
5fb290d7
DJ
9860 while (fn)
9861 if (fn->lowpc <= address && fn->highpc > address)
9862 goto found;
9863 else
9864 fn = fn->next;
9865
e142c38c
DJ
9866 fn = cu->first_fn;
9867 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
9868 if (fn->lowpc <= address && fn->highpc > address)
9869 goto found;
9870 else
9871 fn = fn->next;
9872
9873 return address;
9874
9875 found:
9876 if (fn->seen_line)
9877 return address;
9878 if (address != fn->lowpc)
4d3c2250 9879 complaint (&symfile_complaints,
e2e0b3e5 9880 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 9881 (unsigned long) address, fn->name);
5fb290d7
DJ
9882 fn->seen_line = 1;
9883 return fn->lowpc;
9884}
9885
aaa75496
JB
9886/* Decode the Line Number Program (LNP) for the given line_header
9887 structure and CU. The actual information extracted and the type
9888 of structures created from the LNP depends on the value of PST.
9889
9890 1. If PST is NULL, then this procedure uses the data from the program
9891 to create all necessary symbol tables, and their linetables.
9892 The compilation directory of the file is passed in COMP_DIR,
9893 and must not be NULL.
6e70227d 9894
aaa75496
JB
9895 2. If PST is not NULL, this procedure reads the program to determine
9896 the list of files included by the unit represented by PST, and
9897 builds all the associated partial symbol tables. In this case,
9898 the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
9899 is not used to compute the full name of the symtab, and therefore
9900 omitting it when building the partial symtab does not introduce
9901 the potential for inconsistency - a partial symtab and its associated
9902 symbtab having a different fullname -). */
debd256d 9903
c906108c 9904static void
debd256d 9905dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 9906 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 9907{
a8c50c1f 9908 gdb_byte *line_ptr, *extended_end;
fe1b8b76 9909 gdb_byte *line_end;
a8c50c1f 9910 unsigned int bytes_read, extended_len;
c906108c 9911 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
9912 CORE_ADDR baseaddr;
9913 struct objfile *objfile = cu->objfile;
fbf65064 9914 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 9915 const int decode_for_pst_p = (pst != NULL);
cb1df416 9916 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
9917
9918 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9919
debd256d
JB
9920 line_ptr = lh->statement_program_start;
9921 line_end = lh->statement_program_end;
c906108c
SS
9922
9923 /* Read the statement sequences until there's nothing left. */
9924 while (line_ptr < line_end)
9925 {
9926 /* state machine registers */
9927 CORE_ADDR address = 0;
9928 unsigned int file = 1;
9929 unsigned int line = 1;
9930 unsigned int column = 0;
debd256d 9931 int is_stmt = lh->default_is_stmt;
c906108c
SS
9932 int basic_block = 0;
9933 int end_sequence = 0;
fbf65064 9934 CORE_ADDR addr;
2dc7f7b3 9935 unsigned char op_index = 0;
c906108c 9936
aaa75496 9937 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 9938 {
aaa75496 9939 /* Start a subfile for the current file of the state machine. */
debd256d
JB
9940 /* lh->include_dirs and lh->file_names are 0-based, but the
9941 directory and file name numbers in the statement program
9942 are 1-based. */
9943 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 9944 char *dir = NULL;
a738430d 9945
debd256d
JB
9946 if (fe->dir_index)
9947 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
9948
9949 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
9950 }
9951
a738430d 9952 /* Decode the table. */
c5aa993b 9953 while (!end_sequence)
c906108c
SS
9954 {
9955 op_code = read_1_byte (abfd, line_ptr);
9956 line_ptr += 1;
59205f5a
JB
9957 if (line_ptr > line_end)
9958 {
9959 dwarf2_debug_line_missing_end_sequence_complaint ();
9960 break;
9961 }
9aa1fe7e 9962
debd256d 9963 if (op_code >= lh->opcode_base)
6e70227d 9964 {
a738430d 9965 /* Special operand. */
debd256d 9966 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
9967 address += (((op_index + (adj_opcode / lh->line_range))
9968 / lh->maximum_ops_per_instruction)
9969 * lh->minimum_instruction_length);
9970 op_index = ((op_index + (adj_opcode / lh->line_range))
9971 % lh->maximum_ops_per_instruction);
debd256d 9972 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 9973 if (lh->num_file_names < file || file == 0)
25e43795 9974 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
9975 /* For now we ignore lines not starting on an
9976 instruction boundary. */
9977 else if (op_index == 0)
25e43795
DJ
9978 {
9979 lh->file_names[file - 1].included_p = 1;
ca5f395d 9980 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
9981 {
9982 if (last_subfile != current_subfile)
9983 {
9984 addr = gdbarch_addr_bits_remove (gdbarch, address);
9985 if (last_subfile)
9986 record_line (last_subfile, 0, addr);
9987 last_subfile = current_subfile;
9988 }
25e43795 9989 /* Append row to matrix using current values. */
fbf65064
UW
9990 addr = check_cu_functions (address, cu);
9991 addr = gdbarch_addr_bits_remove (gdbarch, addr);
9992 record_line (current_subfile, line, addr);
366da635 9993 }
25e43795 9994 }
ca5f395d 9995 basic_block = 0;
9aa1fe7e
GK
9996 }
9997 else switch (op_code)
c906108c
SS
9998 {
9999 case DW_LNS_extended_op:
a8c50c1f 10000 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 10001 line_ptr += bytes_read;
a8c50c1f 10002 extended_end = line_ptr + extended_len;
c906108c
SS
10003 extended_op = read_1_byte (abfd, line_ptr);
10004 line_ptr += 1;
10005 switch (extended_op)
10006 {
10007 case DW_LNE_end_sequence:
10008 end_sequence = 1;
c906108c
SS
10009 break;
10010 case DW_LNE_set_address:
e7c27a73 10011 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10012 op_index = 0;
107d2387
AC
10013 line_ptr += bytes_read;
10014 address += baseaddr;
c906108c
SS
10015 break;
10016 case DW_LNE_define_file:
debd256d
JB
10017 {
10018 char *cur_file;
10019 unsigned int dir_index, mod_time, length;
6e70227d 10020
9b1c24c8 10021 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
debd256d
JB
10022 line_ptr += bytes_read;
10023 dir_index =
10024 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10025 line_ptr += bytes_read;
10026 mod_time =
10027 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10028 line_ptr += bytes_read;
10029 length =
10030 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10031 line_ptr += bytes_read;
10032 add_file_name (lh, cur_file, dir_index, mod_time, length);
10033 }
c906108c 10034 break;
d0c6ba3d
CC
10035 case DW_LNE_set_discriminator:
10036 /* The discriminator is not interesting to the debugger;
10037 just ignore it. */
10038 line_ptr = extended_end;
10039 break;
c906108c 10040 default:
4d3c2250 10041 complaint (&symfile_complaints,
e2e0b3e5 10042 _("mangled .debug_line section"));
debd256d 10043 return;
c906108c 10044 }
a8c50c1f
DJ
10045 /* Make sure that we parsed the extended op correctly. If e.g.
10046 we expected a different address size than the producer used,
10047 we may have read the wrong number of bytes. */
10048 if (line_ptr != extended_end)
10049 {
10050 complaint (&symfile_complaints,
10051 _("mangled .debug_line section"));
10052 return;
10053 }
c906108c
SS
10054 break;
10055 case DW_LNS_copy:
59205f5a 10056 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10057 dwarf2_debug_line_missing_file_complaint ();
10058 else
366da635 10059 {
25e43795 10060 lh->file_names[file - 1].included_p = 1;
ca5f395d 10061 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10062 {
10063 if (last_subfile != current_subfile)
10064 {
10065 addr = gdbarch_addr_bits_remove (gdbarch, address);
10066 if (last_subfile)
10067 record_line (last_subfile, 0, addr);
10068 last_subfile = current_subfile;
10069 }
10070 addr = check_cu_functions (address, cu);
10071 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10072 record_line (current_subfile, line, addr);
10073 }
366da635 10074 }
c906108c
SS
10075 basic_block = 0;
10076 break;
10077 case DW_LNS_advance_pc:
2dc7f7b3
TT
10078 {
10079 CORE_ADDR adjust
10080 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10081
10082 address += (((op_index + adjust)
10083 / lh->maximum_ops_per_instruction)
10084 * lh->minimum_instruction_length);
10085 op_index = ((op_index + adjust)
10086 % lh->maximum_ops_per_instruction);
10087 line_ptr += bytes_read;
10088 }
c906108c
SS
10089 break;
10090 case DW_LNS_advance_line:
10091 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10092 line_ptr += bytes_read;
10093 break;
10094 case DW_LNS_set_file:
debd256d 10095 {
a738430d
MK
10096 /* The arrays lh->include_dirs and lh->file_names are
10097 0-based, but the directory and file name numbers in
10098 the statement program are 1-based. */
debd256d 10099 struct file_entry *fe;
4f1520fb 10100 char *dir = NULL;
a738430d 10101
debd256d
JB
10102 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10103 line_ptr += bytes_read;
59205f5a 10104 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10105 dwarf2_debug_line_missing_file_complaint ();
10106 else
10107 {
10108 fe = &lh->file_names[file - 1];
10109 if (fe->dir_index)
10110 dir = lh->include_dirs[fe->dir_index - 1];
10111 if (!decode_for_pst_p)
10112 {
10113 last_subfile = current_subfile;
10114 dwarf2_start_subfile (fe->name, dir, comp_dir);
10115 }
10116 }
debd256d 10117 }
c906108c
SS
10118 break;
10119 case DW_LNS_set_column:
10120 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10121 line_ptr += bytes_read;
10122 break;
10123 case DW_LNS_negate_stmt:
10124 is_stmt = (!is_stmt);
10125 break;
10126 case DW_LNS_set_basic_block:
10127 basic_block = 1;
10128 break;
c2c6d25f
JM
10129 /* Add to the address register of the state machine the
10130 address increment value corresponding to special opcode
a738430d
MK
10131 255. I.e., this value is scaled by the minimum
10132 instruction length since special opcode 255 would have
10133 scaled the the increment. */
c906108c 10134 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10135 {
10136 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10137
10138 address += (((op_index + adjust)
10139 / lh->maximum_ops_per_instruction)
10140 * lh->minimum_instruction_length);
10141 op_index = ((op_index + adjust)
10142 % lh->maximum_ops_per_instruction);
10143 }
c906108c
SS
10144 break;
10145 case DW_LNS_fixed_advance_pc:
10146 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10147 op_index = 0;
c906108c
SS
10148 line_ptr += 2;
10149 break;
9aa1fe7e 10150 default:
a738430d
MK
10151 {
10152 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10153 int i;
a738430d 10154
debd256d 10155 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10156 {
10157 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10158 line_ptr += bytes_read;
10159 }
10160 }
c906108c
SS
10161 }
10162 }
59205f5a
JB
10163 if (lh->num_file_names < file || file == 0)
10164 dwarf2_debug_line_missing_file_complaint ();
10165 else
10166 {
10167 lh->file_names[file - 1].included_p = 1;
10168 if (!decode_for_pst_p)
fbf65064
UW
10169 {
10170 addr = gdbarch_addr_bits_remove (gdbarch, address);
10171 record_line (current_subfile, 0, addr);
10172 }
59205f5a 10173 }
c906108c 10174 }
aaa75496
JB
10175
10176 if (decode_for_pst_p)
10177 {
10178 int file_index;
10179
10180 /* Now that we're done scanning the Line Header Program, we can
10181 create the psymtab of each included file. */
10182 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10183 if (lh->file_names[file_index].included_p == 1)
10184 {
5b5464ad
JB
10185 const struct file_entry fe = lh->file_names [file_index];
10186 char *include_name = fe.name;
10187 char *dir_name = NULL;
10188 char *pst_filename = pst->filename;
10189
10190 if (fe.dir_index)
10191 dir_name = lh->include_dirs[fe.dir_index - 1];
10192
10193 if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
10194 {
1754f103
MK
10195 include_name = concat (dir_name, SLASH_STRING,
10196 include_name, (char *)NULL);
5b5464ad
JB
10197 make_cleanup (xfree, include_name);
10198 }
10199
10200 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10201 {
1754f103
MK
10202 pst_filename = concat (pst->dirname, SLASH_STRING,
10203 pst_filename, (char *)NULL);
5b5464ad
JB
10204 make_cleanup (xfree, pst_filename);
10205 }
10206
10207 if (strcmp (include_name, pst_filename) != 0)
aaa75496
JB
10208 dwarf2_create_include_psymtab (include_name, pst, objfile);
10209 }
10210 }
cb1df416
DJ
10211 else
10212 {
10213 /* Make sure a symtab is created for every file, even files
10214 which contain only variables (i.e. no code with associated
10215 line numbers). */
10216
10217 int i;
10218 struct file_entry *fe;
10219
10220 for (i = 0; i < lh->num_file_names; i++)
10221 {
10222 char *dir = NULL;
9a619af0 10223
cb1df416
DJ
10224 fe = &lh->file_names[i];
10225 if (fe->dir_index)
10226 dir = lh->include_dirs[fe->dir_index - 1];
10227 dwarf2_start_subfile (fe->name, dir, comp_dir);
10228
10229 /* Skip the main file; we don't need it, and it must be
10230 allocated last, so that it will show up before the
10231 non-primary symtabs in the objfile's symtab list. */
10232 if (current_subfile == first_subfile)
10233 continue;
10234
10235 if (current_subfile->symtab == NULL)
10236 current_subfile->symtab = allocate_symtab (current_subfile->name,
10237 cu->objfile);
10238 fe->symtab = current_subfile->symtab;
10239 }
10240 }
c906108c
SS
10241}
10242
10243/* Start a subfile for DWARF. FILENAME is the name of the file and
10244 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10245 or NULL if not known. COMP_DIR is the compilation directory for the
10246 linetable's compilation unit or NULL if not known.
c906108c
SS
10247 This routine tries to keep line numbers from identical absolute and
10248 relative file names in a common subfile.
10249
10250 Using the `list' example from the GDB testsuite, which resides in
10251 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10252 of /srcdir/list0.c yields the following debugging information for list0.c:
10253
c5aa993b
JM
10254 DW_AT_name: /srcdir/list0.c
10255 DW_AT_comp_dir: /compdir
357e46e7 10256 files.files[0].name: list0.h
c5aa993b 10257 files.files[0].dir: /srcdir
357e46e7 10258 files.files[1].name: list0.c
c5aa993b 10259 files.files[1].dir: /srcdir
c906108c
SS
10260
10261 The line number information for list0.c has to end up in a single
4f1520fb
FR
10262 subfile, so that `break /srcdir/list0.c:1' works as expected.
10263 start_subfile will ensure that this happens provided that we pass the
10264 concatenation of files.files[1].dir and files.files[1].name as the
10265 subfile's name. */
c906108c
SS
10266
10267static void
4f1520fb 10268dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 10269{
4f1520fb
FR
10270 char *fullname;
10271
10272 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10273 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10274 second argument to start_subfile. To be consistent, we do the
10275 same here. In order not to lose the line information directory,
10276 we concatenate it to the filename when it makes sense.
10277 Note that the Dwarf3 standard says (speaking of filenames in line
10278 information): ``The directory index is ignored for file names
10279 that represent full path names''. Thus ignoring dirname in the
10280 `else' branch below isn't an issue. */
c906108c 10281
d5166ae1 10282 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10283 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10284 else
10285 fullname = filename;
c906108c 10286
4f1520fb
FR
10287 start_subfile (fullname, comp_dir);
10288
10289 if (fullname != filename)
10290 xfree (fullname);
c906108c
SS
10291}
10292
4c2df51b
DJ
10293static void
10294var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10295 struct dwarf2_cu *cu)
4c2df51b 10296{
e7c27a73
DJ
10297 struct objfile *objfile = cu->objfile;
10298 struct comp_unit_head *cu_header = &cu->header;
10299
4c2df51b
DJ
10300 /* NOTE drow/2003-01-30: There used to be a comment and some special
10301 code here to turn a symbol with DW_AT_external and a
10302 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10303 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10304 with some versions of binutils) where shared libraries could have
10305 relocations against symbols in their debug information - the
10306 minimal symbol would have the right address, but the debug info
10307 would not. It's no longer necessary, because we will explicitly
10308 apply relocations when we read in the debug information now. */
10309
10310 /* A DW_AT_location attribute with no contents indicates that a
10311 variable has been optimized away. */
10312 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10313 {
10314 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10315 return;
10316 }
10317
10318 /* Handle one degenerate form of location expression specially, to
10319 preserve GDB's previous behavior when section offsets are
10320 specified. If this is just a DW_OP_addr then mark this symbol
10321 as LOC_STATIC. */
10322
10323 if (attr_form_is_block (attr)
10324 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10325 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10326 {
891d2f0b 10327 unsigned int dummy;
4c2df51b
DJ
10328
10329 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10330 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10331 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10332 fixup_symbol_section (sym, objfile);
10333 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10334 SYMBOL_SECTION (sym));
4c2df51b
DJ
10335 return;
10336 }
10337
10338 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10339 expression evaluator, and use LOC_COMPUTED only when necessary
10340 (i.e. when the value of a register or memory location is
10341 referenced, or a thread-local block, etc.). Then again, it might
10342 not be worthwhile. I'm assuming that it isn't unless performance
10343 or memory numbers show me otherwise. */
10344
e7c27a73 10345 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10346 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10347}
10348
c906108c
SS
10349/* Given a pointer to a DWARF information entry, figure out if we need
10350 to make a symbol table entry for it, and if so, create a new entry
10351 and return a pointer to it.
10352 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10353 used the passed type.
10354 If SPACE is not NULL, use it to hold the new symbol. If it is
10355 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10356
10357static struct symbol *
34eaf542
TT
10358new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10359 struct symbol *space)
c906108c 10360{
e7c27a73 10361 struct objfile *objfile = cu->objfile;
c906108c
SS
10362 struct symbol *sym = NULL;
10363 char *name;
10364 struct attribute *attr = NULL;
10365 struct attribute *attr2 = NULL;
e142c38c 10366 CORE_ADDR baseaddr;
e37fd15a
SW
10367 struct pending **list_to_add = NULL;
10368
edb3359d 10369 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10370
10371 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10372
94af9270 10373 name = dwarf2_name (die, cu);
c906108c
SS
10374 if (name)
10375 {
94af9270 10376 const char *linkagename;
34eaf542 10377 int suppress_add = 0;
94af9270 10378
34eaf542
TT
10379 if (space)
10380 sym = space;
10381 else
10382 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10383 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10384
10385 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10386 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10387 linkagename = dwarf2_physname (name, die, cu);
10388 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10389
f55ee35c
JK
10390 /* Fortran does not have mangling standard and the mangling does differ
10391 between gfortran, iFort etc. */
10392 if (cu->language == language_fortran
b250c185 10393 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10394 symbol_set_demangled_name (&(sym->ginfo),
10395 (char *) dwarf2_full_name (name, die, cu),
10396 NULL);
f55ee35c 10397
c906108c 10398 /* Default assumptions.
c5aa993b 10399 Use the passed type or decode it from the die. */
176620f1 10400 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10401 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10402 if (type != NULL)
10403 SYMBOL_TYPE (sym) = type;
10404 else
e7c27a73 10405 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10406 attr = dwarf2_attr (die,
10407 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10408 cu);
c906108c
SS
10409 if (attr)
10410 {
10411 SYMBOL_LINE (sym) = DW_UNSND (attr);
10412 }
cb1df416 10413
edb3359d
DJ
10414 attr = dwarf2_attr (die,
10415 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10416 cu);
cb1df416
DJ
10417 if (attr)
10418 {
10419 int file_index = DW_UNSND (attr);
9a619af0 10420
cb1df416
DJ
10421 if (cu->line_header == NULL
10422 || file_index > cu->line_header->num_file_names)
10423 complaint (&symfile_complaints,
10424 _("file index out of range"));
1c3d648d 10425 else if (file_index > 0)
cb1df416
DJ
10426 {
10427 struct file_entry *fe;
9a619af0 10428
cb1df416
DJ
10429 fe = &cu->line_header->file_names[file_index - 1];
10430 SYMBOL_SYMTAB (sym) = fe->symtab;
10431 }
10432 }
10433
c906108c
SS
10434 switch (die->tag)
10435 {
10436 case DW_TAG_label:
e142c38c 10437 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10438 if (attr)
10439 {
10440 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10441 }
0f5238ed
TT
10442 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10443 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10444 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10445 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10446 break;
10447 case DW_TAG_subprogram:
10448 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10449 finish_block. */
10450 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10451 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10452 if ((attr2 && (DW_UNSND (attr2) != 0))
10453 || cu->language == language_ada)
c906108c 10454 {
2cfa0c8d
JB
10455 /* Subprograms marked external are stored as a global symbol.
10456 Ada subprograms, whether marked external or not, are always
10457 stored as a global symbol, because we want to be able to
10458 access them globally. For instance, we want to be able
10459 to break on a nested subprogram without having to
10460 specify the context. */
e37fd15a 10461 list_to_add = &global_symbols;
c906108c
SS
10462 }
10463 else
10464 {
e37fd15a 10465 list_to_add = cu->list_in_scope;
c906108c
SS
10466 }
10467 break;
edb3359d
DJ
10468 case DW_TAG_inlined_subroutine:
10469 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10470 finish_block. */
10471 SYMBOL_CLASS (sym) = LOC_BLOCK;
10472 SYMBOL_INLINED (sym) = 1;
10473 /* Do not add the symbol to any lists. It will be found via
10474 BLOCK_FUNCTION from the blockvector. */
10475 break;
34eaf542
TT
10476 case DW_TAG_template_value_param:
10477 suppress_add = 1;
10478 /* Fall through. */
c906108c 10479 case DW_TAG_variable:
254e6b9e 10480 case DW_TAG_member:
c906108c
SS
10481 /* Compilation with minimal debug info may result in variables
10482 with missing type entries. Change the misleading `void' type
10483 to something sensible. */
10484 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10485 SYMBOL_TYPE (sym)
46bf5051 10486 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10487
e142c38c 10488 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10489 /* In the case of DW_TAG_member, we should only be called for
10490 static const members. */
10491 if (die->tag == DW_TAG_member)
10492 {
3863f96c
DE
10493 /* dwarf2_add_field uses die_is_declaration,
10494 so we do the same. */
254e6b9e
DE
10495 gdb_assert (die_is_declaration (die, cu));
10496 gdb_assert (attr);
10497 }
c906108c
SS
10498 if (attr)
10499 {
e7c27a73 10500 dwarf2_const_value (attr, sym, cu);
e142c38c 10501 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10502 if (!suppress_add)
34eaf542
TT
10503 {
10504 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10505 list_to_add = &global_symbols;
34eaf542 10506 else
e37fd15a 10507 list_to_add = cu->list_in_scope;
34eaf542 10508 }
c906108c
SS
10509 break;
10510 }
e142c38c 10511 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10512 if (attr)
10513 {
e7c27a73 10514 var_decode_location (attr, sym, cu);
e142c38c 10515 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10516 if (SYMBOL_CLASS (sym) == LOC_STATIC
10517 && SYMBOL_VALUE_ADDRESS (sym) == 0
10518 && !dwarf2_per_objfile->has_section_at_zero)
10519 {
10520 /* When a static variable is eliminated by the linker,
10521 the corresponding debug information is not stripped
10522 out, but the variable address is set to null;
10523 do not add such variables into symbol table. */
10524 }
10525 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10526 {
f55ee35c
JK
10527 /* Workaround gfortran PR debug/40040 - it uses
10528 DW_AT_location for variables in -fPIC libraries which may
10529 get overriden by other libraries/executable and get
10530 a different address. Resolve it by the minimal symbol
10531 which may come from inferior's executable using copy
10532 relocation. Make this workaround only for gfortran as for
10533 other compilers GDB cannot guess the minimal symbol
10534 Fortran mangling kind. */
10535 if (cu->language == language_fortran && die->parent
10536 && die->parent->tag == DW_TAG_module
10537 && cu->producer
10538 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10539 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10540
1c809c68
TT
10541 /* A variable with DW_AT_external is never static,
10542 but it may be block-scoped. */
10543 list_to_add = (cu->list_in_scope == &file_symbols
10544 ? &global_symbols : cu->list_in_scope);
1c809c68 10545 }
c906108c 10546 else
e37fd15a 10547 list_to_add = cu->list_in_scope;
c906108c
SS
10548 }
10549 else
10550 {
10551 /* We do not know the address of this symbol.
c5aa993b
JM
10552 If it is an external symbol and we have type information
10553 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10554 The address of the variable will then be determined from
10555 the minimal symbol table whenever the variable is
10556 referenced. */
e142c38c 10557 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10558 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10559 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10560 {
0fe7935b
DJ
10561 /* A variable with DW_AT_external is never static, but it
10562 may be block-scoped. */
10563 list_to_add = (cu->list_in_scope == &file_symbols
10564 ? &global_symbols : cu->list_in_scope);
10565
c906108c 10566 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10567 }
442ddf59
JK
10568 else if (!die_is_declaration (die, cu))
10569 {
10570 /* Use the default LOC_OPTIMIZED_OUT class. */
10571 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10572 if (!suppress_add)
10573 list_to_add = cu->list_in_scope;
442ddf59 10574 }
c906108c
SS
10575 }
10576 break;
10577 case DW_TAG_formal_parameter:
edb3359d
DJ
10578 /* If we are inside a function, mark this as an argument. If
10579 not, we might be looking at an argument to an inlined function
10580 when we do not have enough information to show inlined frames;
10581 pretend it's a local variable in that case so that the user can
10582 still see it. */
10583 if (context_stack_depth > 0
10584 && context_stack[context_stack_depth - 1].name != NULL)
10585 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10586 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10587 if (attr)
10588 {
e7c27a73 10589 var_decode_location (attr, sym, cu);
c906108c 10590 }
e142c38c 10591 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10592 if (attr)
10593 {
e7c27a73 10594 dwarf2_const_value (attr, sym, cu);
c906108c 10595 }
f346a30d
PM
10596 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10597 if (attr && DW_UNSND (attr))
10598 {
10599 struct type *ref_type;
10600
10601 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10602 SYMBOL_TYPE (sym) = ref_type;
10603 }
10604
e37fd15a 10605 list_to_add = cu->list_in_scope;
c906108c
SS
10606 break;
10607 case DW_TAG_unspecified_parameters:
10608 /* From varargs functions; gdb doesn't seem to have any
10609 interest in this information, so just ignore it for now.
10610 (FIXME?) */
10611 break;
34eaf542
TT
10612 case DW_TAG_template_type_param:
10613 suppress_add = 1;
10614 /* Fall through. */
c906108c 10615 case DW_TAG_class_type:
680b30c7 10616 case DW_TAG_interface_type:
c906108c
SS
10617 case DW_TAG_structure_type:
10618 case DW_TAG_union_type:
72019c9c 10619 case DW_TAG_set_type:
c906108c
SS
10620 case DW_TAG_enumeration_type:
10621 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10622 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 10623
63d06c5c 10624 {
987504bb 10625 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
10626 really ever be static objects: otherwise, if you try
10627 to, say, break of a class's method and you're in a file
10628 which doesn't mention that class, it won't work unless
10629 the check for all static symbols in lookup_symbol_aux
10630 saves you. See the OtherFileClass tests in
10631 gdb.c++/namespace.exp. */
10632
e37fd15a 10633 if (!suppress_add)
34eaf542 10634 {
34eaf542
TT
10635 list_to_add = (cu->list_in_scope == &file_symbols
10636 && (cu->language == language_cplus
10637 || cu->language == language_java)
10638 ? &global_symbols : cu->list_in_scope);
34eaf542 10639 }
63d06c5c
DC
10640
10641 /* The semantics of C++ state that "struct foo { ... }" also
987504bb 10642 defines a typedef for "foo". A Java class declaration also
5eeb2539 10643 defines a typedef for the class. */
987504bb 10644 if (cu->language == language_cplus
8c6860bb
JB
10645 || cu->language == language_java
10646 || cu->language == language_ada)
63d06c5c 10647 {
d8151005
DJ
10648 /* The symbol's name is already allocated along with
10649 this objfile, so we don't need to duplicate it for
10650 the type. */
63d06c5c 10651 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
77ef991d 10652 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
63d06c5c
DC
10653 }
10654 }
c906108c
SS
10655 break;
10656 case DW_TAG_typedef:
63d06c5c
DC
10657 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10658 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10659 list_to_add = cu->list_in_scope;
63d06c5c 10660 break;
c906108c 10661 case DW_TAG_base_type:
a02abb62 10662 case DW_TAG_subrange_type:
c906108c 10663 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10664 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10665 list_to_add = cu->list_in_scope;
c906108c
SS
10666 break;
10667 case DW_TAG_enumerator:
e142c38c 10668 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10669 if (attr)
10670 {
e7c27a73 10671 dwarf2_const_value (attr, sym, cu);
c906108c 10672 }
63d06c5c
DC
10673 {
10674 /* NOTE: carlton/2003-11-10: See comment above in the
10675 DW_TAG_class_type, etc. block. */
10676
e142c38c 10677 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10678 && (cu->language == language_cplus
10679 || cu->language == language_java)
e142c38c 10680 ? &global_symbols : cu->list_in_scope);
63d06c5c 10681 }
c906108c 10682 break;
5c4e30ca
DC
10683 case DW_TAG_namespace:
10684 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 10685 list_to_add = &global_symbols;
5c4e30ca 10686 break;
c906108c
SS
10687 default:
10688 /* Not a tag we recognize. Hopefully we aren't processing
10689 trash data, but since we must specifically ignore things
10690 we don't recognize, there is nothing else we should do at
10691 this point. */
e2e0b3e5 10692 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10693 dwarf_tag_name (die->tag));
c906108c
SS
10694 break;
10695 }
df8a16a1 10696
e37fd15a
SW
10697 if (suppress_add)
10698 {
10699 sym->hash_next = objfile->template_symbols;
10700 objfile->template_symbols = sym;
10701 list_to_add = NULL;
10702 }
10703
10704 if (list_to_add != NULL)
10705 add_symbol_to_list (sym, list_to_add);
10706
df8a16a1
DJ
10707 /* For the benefit of old versions of GCC, check for anonymous
10708 namespaces based on the demangled name. */
10709 if (!processing_has_namespace_info
94af9270 10710 && cu->language == language_cplus)
df8a16a1 10711 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10712 }
10713 return (sym);
10714}
10715
34eaf542
TT
10716/* A wrapper for new_symbol_full that always allocates a new symbol. */
10717
10718static struct symbol *
10719new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10720{
10721 return new_symbol_full (die, type, cu, NULL);
10722}
10723
98bfdba5
PA
10724/* Given an attr with a DW_FORM_dataN value in host byte order,
10725 zero-extend it as appropriate for the symbol's type. The DWARF
10726 standard (v4) is not entirely clear about the meaning of using
10727 DW_FORM_dataN for a constant with a signed type, where the type is
10728 wider than the data. The conclusion of a discussion on the DWARF
10729 list was that this is unspecified. We choose to always zero-extend
10730 because that is the interpretation long in use by GCC. */
c906108c 10731
98bfdba5
PA
10732static gdb_byte *
10733dwarf2_const_value_data (struct attribute *attr, struct type *type,
10734 const char *name, struct obstack *obstack,
10735 struct dwarf2_cu *cu, long *value, int bits)
c906108c 10736{
e7c27a73 10737 struct objfile *objfile = cu->objfile;
e17a4113
UW
10738 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10739 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
10740 LONGEST l = DW_UNSND (attr);
10741
10742 if (bits < sizeof (*value) * 8)
10743 {
10744 l &= ((LONGEST) 1 << bits) - 1;
10745 *value = l;
10746 }
10747 else if (bits == sizeof (*value) * 8)
10748 *value = l;
10749 else
10750 {
10751 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10752 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10753 return bytes;
10754 }
10755
10756 return NULL;
10757}
10758
10759/* Read a constant value from an attribute. Either set *VALUE, or if
10760 the value does not fit in *VALUE, set *BYTES - either already
10761 allocated on the objfile obstack, or newly allocated on OBSTACK,
10762 or, set *BATON, if we translated the constant to a location
10763 expression. */
10764
10765static void
10766dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10767 const char *name, struct obstack *obstack,
10768 struct dwarf2_cu *cu,
10769 long *value, gdb_byte **bytes,
10770 struct dwarf2_locexpr_baton **baton)
10771{
10772 struct objfile *objfile = cu->objfile;
10773 struct comp_unit_head *cu_header = &cu->header;
c906108c 10774 struct dwarf_block *blk;
98bfdba5
PA
10775 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10776 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10777
10778 *value = 0;
10779 *bytes = NULL;
10780 *baton = NULL;
c906108c
SS
10781
10782 switch (attr->form)
10783 {
10784 case DW_FORM_addr:
ac56253d 10785 {
ac56253d
TT
10786 gdb_byte *data;
10787
98bfdba5
PA
10788 if (TYPE_LENGTH (type) != cu_header->addr_size)
10789 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 10790 cu_header->addr_size,
98bfdba5 10791 TYPE_LENGTH (type));
ac56253d
TT
10792 /* Symbols of this form are reasonably rare, so we just
10793 piggyback on the existing location code rather than writing
10794 a new implementation of symbol_computed_ops. */
98bfdba5
PA
10795 *baton = obstack_alloc (&objfile->objfile_obstack,
10796 sizeof (struct dwarf2_locexpr_baton));
10797 (*baton)->per_cu = cu->per_cu;
10798 gdb_assert ((*baton)->per_cu);
ac56253d 10799
98bfdba5
PA
10800 (*baton)->size = 2 + cu_header->addr_size;
10801 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
10802 (*baton)->data = data;
ac56253d
TT
10803
10804 data[0] = DW_OP_addr;
10805 store_unsigned_integer (&data[1], cu_header->addr_size,
10806 byte_order, DW_ADDR (attr));
10807 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 10808 }
c906108c 10809 break;
4ac36638 10810 case DW_FORM_string:
93b5768b 10811 case DW_FORM_strp:
98bfdba5
PA
10812 /* DW_STRING is already allocated on the objfile obstack, point
10813 directly to it. */
10814 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 10815 break;
c906108c
SS
10816 case DW_FORM_block1:
10817 case DW_FORM_block2:
10818 case DW_FORM_block4:
10819 case DW_FORM_block:
2dc7f7b3 10820 case DW_FORM_exprloc:
c906108c 10821 blk = DW_BLOCK (attr);
98bfdba5
PA
10822 if (TYPE_LENGTH (type) != blk->size)
10823 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
10824 TYPE_LENGTH (type));
10825 *bytes = blk->data;
c906108c 10826 break;
2df3850c
JM
10827
10828 /* The DW_AT_const_value attributes are supposed to carry the
10829 symbol's value "represented as it would be on the target
10830 architecture." By the time we get here, it's already been
10831 converted to host endianness, so we just need to sign- or
10832 zero-extend it as appropriate. */
10833 case DW_FORM_data1:
98bfdba5 10834 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
2df3850c 10835 break;
c906108c 10836 case DW_FORM_data2:
98bfdba5 10837 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
2df3850c 10838 break;
c906108c 10839 case DW_FORM_data4:
98bfdba5 10840 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
2df3850c 10841 break;
c906108c 10842 case DW_FORM_data8:
98bfdba5 10843 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
2df3850c
JM
10844 break;
10845
c906108c 10846 case DW_FORM_sdata:
98bfdba5 10847 *value = DW_SND (attr);
2df3850c
JM
10848 break;
10849
c906108c 10850 case DW_FORM_udata:
98bfdba5 10851 *value = DW_UNSND (attr);
c906108c 10852 break;
2df3850c 10853
c906108c 10854 default:
4d3c2250 10855 complaint (&symfile_complaints,
e2e0b3e5 10856 _("unsupported const value attribute form: '%s'"),
4d3c2250 10857 dwarf_form_name (attr->form));
98bfdba5 10858 *value = 0;
c906108c
SS
10859 break;
10860 }
10861}
10862
2df3850c 10863
98bfdba5
PA
10864/* Copy constant value from an attribute to a symbol. */
10865
2df3850c 10866static void
98bfdba5
PA
10867dwarf2_const_value (struct attribute *attr, struct symbol *sym,
10868 struct dwarf2_cu *cu)
2df3850c 10869{
98bfdba5
PA
10870 struct objfile *objfile = cu->objfile;
10871 struct comp_unit_head *cu_header = &cu->header;
10872 long value;
10873 gdb_byte *bytes;
10874 struct dwarf2_locexpr_baton *baton;
2df3850c 10875
98bfdba5
PA
10876 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
10877 SYMBOL_PRINT_NAME (sym),
10878 &objfile->objfile_obstack, cu,
10879 &value, &bytes, &baton);
2df3850c 10880
98bfdba5
PA
10881 if (baton != NULL)
10882 {
10883 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10884 SYMBOL_LOCATION_BATON (sym) = baton;
10885 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10886 }
10887 else if (bytes != NULL)
10888 {
10889 SYMBOL_VALUE_BYTES (sym) = bytes;
10890 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10891 }
10892 else
10893 {
10894 SYMBOL_VALUE (sym) = value;
10895 SYMBOL_CLASS (sym) = LOC_CONST;
10896 }
2df3850c
JM
10897}
10898
10899
c906108c
SS
10900/* Return the type of the die in question using its DW_AT_type attribute. */
10901
10902static struct type *
e7c27a73 10903die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10904{
c906108c
SS
10905 struct attribute *type_attr;
10906 struct die_info *type_die;
c906108c 10907
e142c38c 10908 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
10909 if (!type_attr)
10910 {
10911 /* A missing DW_AT_type represents a void type. */
46bf5051 10912 return objfile_type (cu->objfile)->builtin_void;
c906108c 10913 }
348e048f
DE
10914
10915 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10b3939b 10916
33ac96f0 10917 return tag_type_to_type (type_die, cu);
c906108c
SS
10918}
10919
b4ba55a1
JB
10920/* True iff CU's producer generates GNAT Ada auxiliary information
10921 that allows to find parallel types through that information instead
10922 of having to do expensive parallel lookups by type name. */
10923
10924static int
10925need_gnat_info (struct dwarf2_cu *cu)
10926{
10927 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
10928 of GNAT produces this auxiliary information, without any indication
10929 that it is produced. Part of enhancing the FSF version of GNAT
10930 to produce that information will be to put in place an indicator
10931 that we can use in order to determine whether the descriptive type
10932 info is available or not. One suggestion that has been made is
10933 to use a new attribute, attached to the CU die. For now, assume
10934 that the descriptive type info is not available. */
10935 return 0;
10936}
10937
10938
10939/* Return the auxiliary type of the die in question using its
10940 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
10941 attribute is not present. */
10942
10943static struct type *
10944die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
10945{
b4ba55a1
JB
10946 struct attribute *type_attr;
10947 struct die_info *type_die;
10948
10949 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
10950 if (!type_attr)
10951 return NULL;
10952
10953 type_die = follow_die_ref (die, type_attr, &cu);
33ac96f0 10954 return tag_type_to_type (type_die, cu);
b4ba55a1
JB
10955}
10956
10957/* If DIE has a descriptive_type attribute, then set the TYPE's
10958 descriptive type accordingly. */
10959
10960static void
10961set_descriptive_type (struct type *type, struct die_info *die,
10962 struct dwarf2_cu *cu)
10963{
10964 struct type *descriptive_type = die_descriptive_type (die, cu);
10965
10966 if (descriptive_type)
10967 {
10968 ALLOCATE_GNAT_AUX_TYPE (type);
10969 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
10970 }
10971}
10972
c906108c
SS
10973/* Return the containing type of the die in question using its
10974 DW_AT_containing_type attribute. */
10975
10976static struct type *
e7c27a73 10977die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10978{
c906108c 10979 struct attribute *type_attr;
33ac96f0 10980 struct die_info *type_die;
c906108c 10981
e142c38c 10982 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
10983 if (!type_attr)
10984 error (_("Dwarf Error: Problem turning containing type into gdb type "
10985 "[in module %s]"), cu->objfile->name);
10986
10987 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10988 return tag_type_to_type (type_die, cu);
c906108c
SS
10989}
10990
c906108c 10991static struct type *
e7c27a73 10992tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10993{
f792889a
DJ
10994 struct type *this_type;
10995
10996 this_type = read_type_die (die, cu);
10997 if (!this_type)
c906108c 10998 {
b00fdb78
TT
10999 char *message, *saved;
11000
11001 /* read_type_die already issued a complaint. */
11002 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11003 cu->objfile->name,
11004 cu->header.offset,
11005 die->offset);
11006 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11007 message, strlen (message));
11008 xfree (message);
11009
11010 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11011 }
f792889a 11012 return this_type;
c906108c
SS
11013}
11014
f792889a 11015static struct type *
e7c27a73 11016read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11017{
f792889a
DJ
11018 struct type *this_type;
11019
11020 this_type = get_die_type (die, cu);
11021 if (this_type)
11022 return this_type;
11023
c906108c
SS
11024 switch (die->tag)
11025 {
11026 case DW_TAG_class_type:
680b30c7 11027 case DW_TAG_interface_type:
c906108c
SS
11028 case DW_TAG_structure_type:
11029 case DW_TAG_union_type:
f792889a 11030 this_type = read_structure_type (die, cu);
c906108c
SS
11031 break;
11032 case DW_TAG_enumeration_type:
f792889a 11033 this_type = read_enumeration_type (die, cu);
c906108c
SS
11034 break;
11035 case DW_TAG_subprogram:
11036 case DW_TAG_subroutine_type:
edb3359d 11037 case DW_TAG_inlined_subroutine:
f792889a 11038 this_type = read_subroutine_type (die, cu);
c906108c
SS
11039 break;
11040 case DW_TAG_array_type:
f792889a 11041 this_type = read_array_type (die, cu);
c906108c 11042 break;
72019c9c 11043 case DW_TAG_set_type:
f792889a 11044 this_type = read_set_type (die, cu);
72019c9c 11045 break;
c906108c 11046 case DW_TAG_pointer_type:
f792889a 11047 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11048 break;
11049 case DW_TAG_ptr_to_member_type:
f792889a 11050 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11051 break;
11052 case DW_TAG_reference_type:
f792889a 11053 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11054 break;
11055 case DW_TAG_const_type:
f792889a 11056 this_type = read_tag_const_type (die, cu);
c906108c
SS
11057 break;
11058 case DW_TAG_volatile_type:
f792889a 11059 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11060 break;
11061 case DW_TAG_string_type:
f792889a 11062 this_type = read_tag_string_type (die, cu);
c906108c
SS
11063 break;
11064 case DW_TAG_typedef:
f792889a 11065 this_type = read_typedef (die, cu);
c906108c 11066 break;
a02abb62 11067 case DW_TAG_subrange_type:
f792889a 11068 this_type = read_subrange_type (die, cu);
a02abb62 11069 break;
c906108c 11070 case DW_TAG_base_type:
f792889a 11071 this_type = read_base_type (die, cu);
c906108c 11072 break;
81a17f79 11073 case DW_TAG_unspecified_type:
f792889a 11074 this_type = read_unspecified_type (die, cu);
81a17f79 11075 break;
0114d602
DJ
11076 case DW_TAG_namespace:
11077 this_type = read_namespace_type (die, cu);
11078 break;
f55ee35c
JK
11079 case DW_TAG_module:
11080 this_type = read_module_type (die, cu);
11081 break;
c906108c 11082 default:
a1f5b845 11083 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11084 dwarf_tag_name (die->tag));
c906108c
SS
11085 break;
11086 }
63d06c5c 11087
f792889a 11088 return this_type;
63d06c5c
DC
11089}
11090
fdde2d81 11091/* Return the name of the namespace/class that DIE is defined within,
0114d602 11092 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11093
0114d602
DJ
11094 For example, if we're within the method foo() in the following
11095 code:
11096
11097 namespace N {
11098 class C {
11099 void foo () {
11100 }
11101 };
11102 }
11103
11104 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11105
11106static char *
e142c38c 11107determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11108{
0114d602
DJ
11109 struct die_info *parent, *spec_die;
11110 struct dwarf2_cu *spec_cu;
11111 struct type *parent_type;
63d06c5c 11112
f55ee35c
JK
11113 if (cu->language != language_cplus && cu->language != language_java
11114 && cu->language != language_fortran)
0114d602
DJ
11115 return "";
11116
11117 /* We have to be careful in the presence of DW_AT_specification.
11118 For example, with GCC 3.4, given the code
11119
11120 namespace N {
11121 void foo() {
11122 // Definition of N::foo.
11123 }
11124 }
11125
11126 then we'll have a tree of DIEs like this:
11127
11128 1: DW_TAG_compile_unit
11129 2: DW_TAG_namespace // N
11130 3: DW_TAG_subprogram // declaration of N::foo
11131 4: DW_TAG_subprogram // definition of N::foo
11132 DW_AT_specification // refers to die #3
11133
11134 Thus, when processing die #4, we have to pretend that we're in
11135 the context of its DW_AT_specification, namely the contex of die
11136 #3. */
11137 spec_cu = cu;
11138 spec_die = die_specification (die, &spec_cu);
11139 if (spec_die == NULL)
11140 parent = die->parent;
11141 else
63d06c5c 11142 {
0114d602
DJ
11143 parent = spec_die->parent;
11144 cu = spec_cu;
63d06c5c 11145 }
0114d602
DJ
11146
11147 if (parent == NULL)
11148 return "";
98bfdba5
PA
11149 else if (parent->building_fullname)
11150 {
11151 const char *name;
11152 const char *parent_name;
11153
11154 /* It has been seen on RealView 2.2 built binaries,
11155 DW_TAG_template_type_param types actually _defined_ as
11156 children of the parent class:
11157
11158 enum E {};
11159 template class <class Enum> Class{};
11160 Class<enum E> class_e;
11161
11162 1: DW_TAG_class_type (Class)
11163 2: DW_TAG_enumeration_type (E)
11164 3: DW_TAG_enumerator (enum1:0)
11165 3: DW_TAG_enumerator (enum2:1)
11166 ...
11167 2: DW_TAG_template_type_param
11168 DW_AT_type DW_FORM_ref_udata (E)
11169
11170 Besides being broken debug info, it can put GDB into an
11171 infinite loop. Consider:
11172
11173 When we're building the full name for Class<E>, we'll start
11174 at Class, and go look over its template type parameters,
11175 finding E. We'll then try to build the full name of E, and
11176 reach here. We're now trying to build the full name of E,
11177 and look over the parent DIE for containing scope. In the
11178 broken case, if we followed the parent DIE of E, we'd again
11179 find Class, and once again go look at its template type
11180 arguments, etc., etc. Simply don't consider such parent die
11181 as source-level parent of this die (it can't be, the language
11182 doesn't allow it), and break the loop here. */
11183 name = dwarf2_name (die, cu);
11184 parent_name = dwarf2_name (parent, cu);
11185 complaint (&symfile_complaints,
11186 _("template param type '%s' defined within parent '%s'"),
11187 name ? name : "<unknown>",
11188 parent_name ? parent_name : "<unknown>");
11189 return "";
11190 }
63d06c5c 11191 else
0114d602
DJ
11192 switch (parent->tag)
11193 {
63d06c5c 11194 case DW_TAG_namespace:
0114d602 11195 parent_type = read_type_die (parent, cu);
acebe513
UW
11196 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11197 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11198 Work around this problem here. */
11199 if (cu->language == language_cplus
11200 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11201 return "";
0114d602
DJ
11202 /* We give a name to even anonymous namespaces. */
11203 return TYPE_TAG_NAME (parent_type);
63d06c5c 11204 case DW_TAG_class_type:
680b30c7 11205 case DW_TAG_interface_type:
63d06c5c 11206 case DW_TAG_structure_type:
0114d602 11207 case DW_TAG_union_type:
f55ee35c 11208 case DW_TAG_module:
0114d602
DJ
11209 parent_type = read_type_die (parent, cu);
11210 if (TYPE_TAG_NAME (parent_type) != NULL)
11211 return TYPE_TAG_NAME (parent_type);
11212 else
11213 /* An anonymous structure is only allowed non-static data
11214 members; no typedefs, no member functions, et cetera.
11215 So it does not need a prefix. */
11216 return "";
63d06c5c 11217 default:
8176b9b8 11218 return determine_prefix (parent, cu);
63d06c5c 11219 }
63d06c5c
DC
11220}
11221
987504bb
JJ
11222/* Return a newly-allocated string formed by concatenating PREFIX and
11223 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11224 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11225 perform an obconcat, otherwise allocate storage for the result. The CU argument
11226 is used to determine the language and hence, the appropriate separator. */
11227
f55ee35c 11228#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11229
11230static char *
f55ee35c
JK
11231typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11232 int physname, struct dwarf2_cu *cu)
63d06c5c 11233{
f55ee35c 11234 const char *lead = "";
5c315b68 11235 const char *sep;
63d06c5c 11236
987504bb
JJ
11237 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11238 sep = "";
11239 else if (cu->language == language_java)
11240 sep = ".";
f55ee35c
JK
11241 else if (cu->language == language_fortran && physname)
11242 {
11243 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11244 DW_AT_MIPS_linkage_name is preferred and used instead. */
11245
11246 lead = "__";
11247 sep = "_MOD_";
11248 }
987504bb
JJ
11249 else
11250 sep = "::";
63d06c5c 11251
6dd47d34
DE
11252 if (prefix == NULL)
11253 prefix = "";
11254 if (suffix == NULL)
11255 suffix = "";
11256
987504bb
JJ
11257 if (obs == NULL)
11258 {
11259 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11260
f55ee35c
JK
11261 strcpy (retval, lead);
11262 strcat (retval, prefix);
6dd47d34
DE
11263 strcat (retval, sep);
11264 strcat (retval, suffix);
63d06c5c
DC
11265 return retval;
11266 }
987504bb
JJ
11267 else
11268 {
11269 /* We have an obstack. */
f55ee35c 11270 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11271 }
63d06c5c
DC
11272}
11273
c906108c
SS
11274/* Return sibling of die, NULL if no sibling. */
11275
f9aca02d 11276static struct die_info *
fba45db2 11277sibling_die (struct die_info *die)
c906108c 11278{
639d11d3 11279 return die->sibling;
c906108c
SS
11280}
11281
71c25dea
TT
11282/* Get name of a die, return NULL if not found. */
11283
11284static char *
11285dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11286 struct obstack *obstack)
11287{
11288 if (name && cu->language == language_cplus)
11289 {
11290 char *canon_name = cp_canonicalize_string (name);
11291
11292 if (canon_name != NULL)
11293 {
11294 if (strcmp (canon_name, name) != 0)
11295 name = obsavestring (canon_name, strlen (canon_name),
11296 obstack);
11297 xfree (canon_name);
11298 }
11299 }
11300
11301 return name;
c906108c
SS
11302}
11303
9219021c
DC
11304/* Get name of a die, return NULL if not found. */
11305
11306static char *
e142c38c 11307dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11308{
11309 struct attribute *attr;
11310
e142c38c 11311 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11312 if (!attr || !DW_STRING (attr))
11313 return NULL;
11314
11315 switch (die->tag)
11316 {
11317 case DW_TAG_compile_unit:
11318 /* Compilation units have a DW_AT_name that is a filename, not
11319 a source language identifier. */
11320 case DW_TAG_enumeration_type:
11321 case DW_TAG_enumerator:
11322 /* These tags always have simple identifiers already; no need
11323 to canonicalize them. */
11324 return DW_STRING (attr);
907af001 11325
418835cc
KS
11326 case DW_TAG_subprogram:
11327 /* Java constructors will all be named "<init>", so return
11328 the class name when we see this special case. */
11329 if (cu->language == language_java
11330 && DW_STRING (attr) != NULL
11331 && strcmp (DW_STRING (attr), "<init>") == 0)
11332 {
11333 struct dwarf2_cu *spec_cu = cu;
11334 struct die_info *spec_die;
11335
11336 /* GCJ will output '<init>' for Java constructor names.
11337 For this special case, return the name of the parent class. */
11338
11339 /* GCJ may output suprogram DIEs with AT_specification set.
11340 If so, use the name of the specified DIE. */
11341 spec_die = die_specification (die, &spec_cu);
11342 if (spec_die != NULL)
11343 return dwarf2_name (spec_die, spec_cu);
11344
11345 do
11346 {
11347 die = die->parent;
11348 if (die->tag == DW_TAG_class_type)
11349 return dwarf2_name (die, cu);
11350 }
11351 while (die->tag != DW_TAG_compile_unit);
11352 }
907af001
UW
11353 break;
11354
11355 case DW_TAG_class_type:
11356 case DW_TAG_interface_type:
11357 case DW_TAG_structure_type:
11358 case DW_TAG_union_type:
11359 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11360 structures or unions. These were of the form "._%d" in GCC 4.1,
11361 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11362 and GCC 4.4. We work around this problem by ignoring these. */
11363 if (strncmp (DW_STRING (attr), "._", 2) == 0
11364 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11365 return NULL;
11366 break;
11367
71c25dea 11368 default:
907af001
UW
11369 break;
11370 }
11371
11372 if (!DW_STRING_IS_CANONICAL (attr))
11373 {
11374 DW_STRING (attr)
11375 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11376 &cu->objfile->objfile_obstack);
11377 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11378 }
907af001 11379 return DW_STRING (attr);
9219021c
DC
11380}
11381
11382/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11383 is none. *EXT_CU is the CU containing DIE on input, and the CU
11384 containing the return value on output. */
9219021c
DC
11385
11386static struct die_info *
f2f0e013 11387dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11388{
11389 struct attribute *attr;
9219021c 11390
f2f0e013 11391 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11392 if (attr == NULL)
11393 return NULL;
11394
f2f0e013 11395 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11396}
11397
c906108c
SS
11398/* Convert a DIE tag into its string name. */
11399
11400static char *
aa1ee363 11401dwarf_tag_name (unsigned tag)
c906108c
SS
11402{
11403 switch (tag)
11404 {
11405 case DW_TAG_padding:
11406 return "DW_TAG_padding";
11407 case DW_TAG_array_type:
11408 return "DW_TAG_array_type";
11409 case DW_TAG_class_type:
11410 return "DW_TAG_class_type";
11411 case DW_TAG_entry_point:
11412 return "DW_TAG_entry_point";
11413 case DW_TAG_enumeration_type:
11414 return "DW_TAG_enumeration_type";
11415 case DW_TAG_formal_parameter:
11416 return "DW_TAG_formal_parameter";
11417 case DW_TAG_imported_declaration:
11418 return "DW_TAG_imported_declaration";
11419 case DW_TAG_label:
11420 return "DW_TAG_label";
11421 case DW_TAG_lexical_block:
11422 return "DW_TAG_lexical_block";
11423 case DW_TAG_member:
11424 return "DW_TAG_member";
11425 case DW_TAG_pointer_type:
11426 return "DW_TAG_pointer_type";
11427 case DW_TAG_reference_type:
11428 return "DW_TAG_reference_type";
11429 case DW_TAG_compile_unit:
11430 return "DW_TAG_compile_unit";
11431 case DW_TAG_string_type:
11432 return "DW_TAG_string_type";
11433 case DW_TAG_structure_type:
11434 return "DW_TAG_structure_type";
11435 case DW_TAG_subroutine_type:
11436 return "DW_TAG_subroutine_type";
11437 case DW_TAG_typedef:
11438 return "DW_TAG_typedef";
11439 case DW_TAG_union_type:
11440 return "DW_TAG_union_type";
11441 case DW_TAG_unspecified_parameters:
11442 return "DW_TAG_unspecified_parameters";
11443 case DW_TAG_variant:
11444 return "DW_TAG_variant";
11445 case DW_TAG_common_block:
11446 return "DW_TAG_common_block";
11447 case DW_TAG_common_inclusion:
11448 return "DW_TAG_common_inclusion";
11449 case DW_TAG_inheritance:
11450 return "DW_TAG_inheritance";
11451 case DW_TAG_inlined_subroutine:
11452 return "DW_TAG_inlined_subroutine";
11453 case DW_TAG_module:
11454 return "DW_TAG_module";
11455 case DW_TAG_ptr_to_member_type:
11456 return "DW_TAG_ptr_to_member_type";
11457 case DW_TAG_set_type:
11458 return "DW_TAG_set_type";
11459 case DW_TAG_subrange_type:
11460 return "DW_TAG_subrange_type";
11461 case DW_TAG_with_stmt:
11462 return "DW_TAG_with_stmt";
11463 case DW_TAG_access_declaration:
11464 return "DW_TAG_access_declaration";
11465 case DW_TAG_base_type:
11466 return "DW_TAG_base_type";
11467 case DW_TAG_catch_block:
11468 return "DW_TAG_catch_block";
11469 case DW_TAG_const_type:
11470 return "DW_TAG_const_type";
11471 case DW_TAG_constant:
11472 return "DW_TAG_constant";
11473 case DW_TAG_enumerator:
11474 return "DW_TAG_enumerator";
11475 case DW_TAG_file_type:
11476 return "DW_TAG_file_type";
11477 case DW_TAG_friend:
11478 return "DW_TAG_friend";
11479 case DW_TAG_namelist:
11480 return "DW_TAG_namelist";
11481 case DW_TAG_namelist_item:
11482 return "DW_TAG_namelist_item";
11483 case DW_TAG_packed_type:
11484 return "DW_TAG_packed_type";
11485 case DW_TAG_subprogram:
11486 return "DW_TAG_subprogram";
11487 case DW_TAG_template_type_param:
11488 return "DW_TAG_template_type_param";
11489 case DW_TAG_template_value_param:
11490 return "DW_TAG_template_value_param";
11491 case DW_TAG_thrown_type:
11492 return "DW_TAG_thrown_type";
11493 case DW_TAG_try_block:
11494 return "DW_TAG_try_block";
11495 case DW_TAG_variant_part:
11496 return "DW_TAG_variant_part";
11497 case DW_TAG_variable:
11498 return "DW_TAG_variable";
11499 case DW_TAG_volatile_type:
11500 return "DW_TAG_volatile_type";
d9fa45fe
DC
11501 case DW_TAG_dwarf_procedure:
11502 return "DW_TAG_dwarf_procedure";
11503 case DW_TAG_restrict_type:
11504 return "DW_TAG_restrict_type";
11505 case DW_TAG_interface_type:
11506 return "DW_TAG_interface_type";
11507 case DW_TAG_namespace:
11508 return "DW_TAG_namespace";
11509 case DW_TAG_imported_module:
11510 return "DW_TAG_imported_module";
11511 case DW_TAG_unspecified_type:
11512 return "DW_TAG_unspecified_type";
11513 case DW_TAG_partial_unit:
11514 return "DW_TAG_partial_unit";
11515 case DW_TAG_imported_unit:
11516 return "DW_TAG_imported_unit";
b7619582
GF
11517 case DW_TAG_condition:
11518 return "DW_TAG_condition";
11519 case DW_TAG_shared_type:
11520 return "DW_TAG_shared_type";
348e048f
DE
11521 case DW_TAG_type_unit:
11522 return "DW_TAG_type_unit";
c906108c
SS
11523 case DW_TAG_MIPS_loop:
11524 return "DW_TAG_MIPS_loop";
b7619582
GF
11525 case DW_TAG_HP_array_descriptor:
11526 return "DW_TAG_HP_array_descriptor";
c906108c
SS
11527 case DW_TAG_format_label:
11528 return "DW_TAG_format_label";
11529 case DW_TAG_function_template:
11530 return "DW_TAG_function_template";
11531 case DW_TAG_class_template:
11532 return "DW_TAG_class_template";
b7619582
GF
11533 case DW_TAG_GNU_BINCL:
11534 return "DW_TAG_GNU_BINCL";
11535 case DW_TAG_GNU_EINCL:
11536 return "DW_TAG_GNU_EINCL";
11537 case DW_TAG_upc_shared_type:
11538 return "DW_TAG_upc_shared_type";
11539 case DW_TAG_upc_strict_type:
11540 return "DW_TAG_upc_strict_type";
11541 case DW_TAG_upc_relaxed_type:
11542 return "DW_TAG_upc_relaxed_type";
11543 case DW_TAG_PGI_kanji_type:
11544 return "DW_TAG_PGI_kanji_type";
11545 case DW_TAG_PGI_interface_block:
11546 return "DW_TAG_PGI_interface_block";
c906108c
SS
11547 default:
11548 return "DW_TAG_<unknown>";
11549 }
11550}
11551
11552/* Convert a DWARF attribute code into its string name. */
11553
11554static char *
aa1ee363 11555dwarf_attr_name (unsigned attr)
c906108c
SS
11556{
11557 switch (attr)
11558 {
11559 case DW_AT_sibling:
11560 return "DW_AT_sibling";
11561 case DW_AT_location:
11562 return "DW_AT_location";
11563 case DW_AT_name:
11564 return "DW_AT_name";
11565 case DW_AT_ordering:
11566 return "DW_AT_ordering";
11567 case DW_AT_subscr_data:
11568 return "DW_AT_subscr_data";
11569 case DW_AT_byte_size:
11570 return "DW_AT_byte_size";
11571 case DW_AT_bit_offset:
11572 return "DW_AT_bit_offset";
11573 case DW_AT_bit_size:
11574 return "DW_AT_bit_size";
11575 case DW_AT_element_list:
11576 return "DW_AT_element_list";
11577 case DW_AT_stmt_list:
11578 return "DW_AT_stmt_list";
11579 case DW_AT_low_pc:
11580 return "DW_AT_low_pc";
11581 case DW_AT_high_pc:
11582 return "DW_AT_high_pc";
11583 case DW_AT_language:
11584 return "DW_AT_language";
11585 case DW_AT_member:
11586 return "DW_AT_member";
11587 case DW_AT_discr:
11588 return "DW_AT_discr";
11589 case DW_AT_discr_value:
11590 return "DW_AT_discr_value";
11591 case DW_AT_visibility:
11592 return "DW_AT_visibility";
11593 case DW_AT_import:
11594 return "DW_AT_import";
11595 case DW_AT_string_length:
11596 return "DW_AT_string_length";
11597 case DW_AT_common_reference:
11598 return "DW_AT_common_reference";
11599 case DW_AT_comp_dir:
11600 return "DW_AT_comp_dir";
11601 case DW_AT_const_value:
11602 return "DW_AT_const_value";
11603 case DW_AT_containing_type:
11604 return "DW_AT_containing_type";
11605 case DW_AT_default_value:
11606 return "DW_AT_default_value";
11607 case DW_AT_inline:
11608 return "DW_AT_inline";
11609 case DW_AT_is_optional:
11610 return "DW_AT_is_optional";
11611 case DW_AT_lower_bound:
11612 return "DW_AT_lower_bound";
11613 case DW_AT_producer:
11614 return "DW_AT_producer";
11615 case DW_AT_prototyped:
11616 return "DW_AT_prototyped";
11617 case DW_AT_return_addr:
11618 return "DW_AT_return_addr";
11619 case DW_AT_start_scope:
11620 return "DW_AT_start_scope";
09fa0d7c
JK
11621 case DW_AT_bit_stride:
11622 return "DW_AT_bit_stride";
c906108c
SS
11623 case DW_AT_upper_bound:
11624 return "DW_AT_upper_bound";
11625 case DW_AT_abstract_origin:
11626 return "DW_AT_abstract_origin";
11627 case DW_AT_accessibility:
11628 return "DW_AT_accessibility";
11629 case DW_AT_address_class:
11630 return "DW_AT_address_class";
11631 case DW_AT_artificial:
11632 return "DW_AT_artificial";
11633 case DW_AT_base_types:
11634 return "DW_AT_base_types";
11635 case DW_AT_calling_convention:
11636 return "DW_AT_calling_convention";
11637 case DW_AT_count:
11638 return "DW_AT_count";
11639 case DW_AT_data_member_location:
11640 return "DW_AT_data_member_location";
11641 case DW_AT_decl_column:
11642 return "DW_AT_decl_column";
11643 case DW_AT_decl_file:
11644 return "DW_AT_decl_file";
11645 case DW_AT_decl_line:
11646 return "DW_AT_decl_line";
11647 case DW_AT_declaration:
11648 return "DW_AT_declaration";
11649 case DW_AT_discr_list:
11650 return "DW_AT_discr_list";
11651 case DW_AT_encoding:
11652 return "DW_AT_encoding";
11653 case DW_AT_external:
11654 return "DW_AT_external";
11655 case DW_AT_frame_base:
11656 return "DW_AT_frame_base";
11657 case DW_AT_friend:
11658 return "DW_AT_friend";
11659 case DW_AT_identifier_case:
11660 return "DW_AT_identifier_case";
11661 case DW_AT_macro_info:
11662 return "DW_AT_macro_info";
11663 case DW_AT_namelist_items:
11664 return "DW_AT_namelist_items";
11665 case DW_AT_priority:
11666 return "DW_AT_priority";
11667 case DW_AT_segment:
11668 return "DW_AT_segment";
11669 case DW_AT_specification:
11670 return "DW_AT_specification";
11671 case DW_AT_static_link:
11672 return "DW_AT_static_link";
11673 case DW_AT_type:
11674 return "DW_AT_type";
11675 case DW_AT_use_location:
11676 return "DW_AT_use_location";
11677 case DW_AT_variable_parameter:
11678 return "DW_AT_variable_parameter";
11679 case DW_AT_virtuality:
11680 return "DW_AT_virtuality";
11681 case DW_AT_vtable_elem_location:
11682 return "DW_AT_vtable_elem_location";
b7619582 11683 /* DWARF 3 values. */
d9fa45fe
DC
11684 case DW_AT_allocated:
11685 return "DW_AT_allocated";
11686 case DW_AT_associated:
11687 return "DW_AT_associated";
11688 case DW_AT_data_location:
11689 return "DW_AT_data_location";
09fa0d7c
JK
11690 case DW_AT_byte_stride:
11691 return "DW_AT_byte_stride";
d9fa45fe
DC
11692 case DW_AT_entry_pc:
11693 return "DW_AT_entry_pc";
11694 case DW_AT_use_UTF8:
11695 return "DW_AT_use_UTF8";
11696 case DW_AT_extension:
11697 return "DW_AT_extension";
11698 case DW_AT_ranges:
11699 return "DW_AT_ranges";
11700 case DW_AT_trampoline:
11701 return "DW_AT_trampoline";
11702 case DW_AT_call_column:
11703 return "DW_AT_call_column";
11704 case DW_AT_call_file:
11705 return "DW_AT_call_file";
11706 case DW_AT_call_line:
11707 return "DW_AT_call_line";
b7619582
GF
11708 case DW_AT_description:
11709 return "DW_AT_description";
11710 case DW_AT_binary_scale:
11711 return "DW_AT_binary_scale";
11712 case DW_AT_decimal_scale:
11713 return "DW_AT_decimal_scale";
11714 case DW_AT_small:
11715 return "DW_AT_small";
11716 case DW_AT_decimal_sign:
11717 return "DW_AT_decimal_sign";
11718 case DW_AT_digit_count:
11719 return "DW_AT_digit_count";
11720 case DW_AT_picture_string:
11721 return "DW_AT_picture_string";
11722 case DW_AT_mutable:
11723 return "DW_AT_mutable";
11724 case DW_AT_threads_scaled:
11725 return "DW_AT_threads_scaled";
11726 case DW_AT_explicit:
11727 return "DW_AT_explicit";
11728 case DW_AT_object_pointer:
11729 return "DW_AT_object_pointer";
11730 case DW_AT_endianity:
11731 return "DW_AT_endianity";
11732 case DW_AT_elemental:
11733 return "DW_AT_elemental";
11734 case DW_AT_pure:
11735 return "DW_AT_pure";
11736 case DW_AT_recursive:
11737 return "DW_AT_recursive";
348e048f
DE
11738 /* DWARF 4 values. */
11739 case DW_AT_signature:
11740 return "DW_AT_signature";
31ef98ae
TT
11741 case DW_AT_linkage_name:
11742 return "DW_AT_linkage_name";
b7619582 11743 /* SGI/MIPS extensions. */
c764a876 11744#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
11745 case DW_AT_MIPS_fde:
11746 return "DW_AT_MIPS_fde";
c764a876 11747#endif
c906108c
SS
11748 case DW_AT_MIPS_loop_begin:
11749 return "DW_AT_MIPS_loop_begin";
11750 case DW_AT_MIPS_tail_loop_begin:
11751 return "DW_AT_MIPS_tail_loop_begin";
11752 case DW_AT_MIPS_epilog_begin:
11753 return "DW_AT_MIPS_epilog_begin";
11754 case DW_AT_MIPS_loop_unroll_factor:
11755 return "DW_AT_MIPS_loop_unroll_factor";
11756 case DW_AT_MIPS_software_pipeline_depth:
11757 return "DW_AT_MIPS_software_pipeline_depth";
11758 case DW_AT_MIPS_linkage_name:
11759 return "DW_AT_MIPS_linkage_name";
b7619582
GF
11760 case DW_AT_MIPS_stride:
11761 return "DW_AT_MIPS_stride";
11762 case DW_AT_MIPS_abstract_name:
11763 return "DW_AT_MIPS_abstract_name";
11764 case DW_AT_MIPS_clone_origin:
11765 return "DW_AT_MIPS_clone_origin";
11766 case DW_AT_MIPS_has_inlines:
11767 return "DW_AT_MIPS_has_inlines";
b7619582 11768 /* HP extensions. */
c764a876 11769#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
11770 case DW_AT_HP_block_index:
11771 return "DW_AT_HP_block_index";
c764a876 11772#endif
b7619582
GF
11773 case DW_AT_HP_unmodifiable:
11774 return "DW_AT_HP_unmodifiable";
11775 case DW_AT_HP_actuals_stmt_list:
11776 return "DW_AT_HP_actuals_stmt_list";
11777 case DW_AT_HP_proc_per_section:
11778 return "DW_AT_HP_proc_per_section";
11779 case DW_AT_HP_raw_data_ptr:
11780 return "DW_AT_HP_raw_data_ptr";
11781 case DW_AT_HP_pass_by_reference:
11782 return "DW_AT_HP_pass_by_reference";
11783 case DW_AT_HP_opt_level:
11784 return "DW_AT_HP_opt_level";
11785 case DW_AT_HP_prof_version_id:
11786 return "DW_AT_HP_prof_version_id";
11787 case DW_AT_HP_opt_flags:
11788 return "DW_AT_HP_opt_flags";
11789 case DW_AT_HP_cold_region_low_pc:
11790 return "DW_AT_HP_cold_region_low_pc";
11791 case DW_AT_HP_cold_region_high_pc:
11792 return "DW_AT_HP_cold_region_high_pc";
11793 case DW_AT_HP_all_variables_modifiable:
11794 return "DW_AT_HP_all_variables_modifiable";
11795 case DW_AT_HP_linkage_name:
11796 return "DW_AT_HP_linkage_name";
11797 case DW_AT_HP_prof_flags:
11798 return "DW_AT_HP_prof_flags";
11799 /* GNU extensions. */
c906108c
SS
11800 case DW_AT_sf_names:
11801 return "DW_AT_sf_names";
11802 case DW_AT_src_info:
11803 return "DW_AT_src_info";
11804 case DW_AT_mac_info:
11805 return "DW_AT_mac_info";
11806 case DW_AT_src_coords:
11807 return "DW_AT_src_coords";
11808 case DW_AT_body_begin:
11809 return "DW_AT_body_begin";
11810 case DW_AT_body_end:
11811 return "DW_AT_body_end";
f5f8a009
EZ
11812 case DW_AT_GNU_vector:
11813 return "DW_AT_GNU_vector";
2de00c64
DE
11814 case DW_AT_GNU_odr_signature:
11815 return "DW_AT_GNU_odr_signature";
b7619582
GF
11816 /* VMS extensions. */
11817 case DW_AT_VMS_rtnbeg_pd_address:
11818 return "DW_AT_VMS_rtnbeg_pd_address";
11819 /* UPC extension. */
11820 case DW_AT_upc_threads_scaled:
11821 return "DW_AT_upc_threads_scaled";
11822 /* PGI (STMicroelectronics) extensions. */
11823 case DW_AT_PGI_lbase:
11824 return "DW_AT_PGI_lbase";
11825 case DW_AT_PGI_soffset:
11826 return "DW_AT_PGI_soffset";
11827 case DW_AT_PGI_lstride:
11828 return "DW_AT_PGI_lstride";
c906108c
SS
11829 default:
11830 return "DW_AT_<unknown>";
11831 }
11832}
11833
11834/* Convert a DWARF value form code into its string name. */
11835
11836static char *
aa1ee363 11837dwarf_form_name (unsigned form)
c906108c
SS
11838{
11839 switch (form)
11840 {
11841 case DW_FORM_addr:
11842 return "DW_FORM_addr";
11843 case DW_FORM_block2:
11844 return "DW_FORM_block2";
11845 case DW_FORM_block4:
11846 return "DW_FORM_block4";
11847 case DW_FORM_data2:
11848 return "DW_FORM_data2";
11849 case DW_FORM_data4:
11850 return "DW_FORM_data4";
11851 case DW_FORM_data8:
11852 return "DW_FORM_data8";
11853 case DW_FORM_string:
11854 return "DW_FORM_string";
11855 case DW_FORM_block:
11856 return "DW_FORM_block";
11857 case DW_FORM_block1:
11858 return "DW_FORM_block1";
11859 case DW_FORM_data1:
11860 return "DW_FORM_data1";
11861 case DW_FORM_flag:
11862 return "DW_FORM_flag";
11863 case DW_FORM_sdata:
11864 return "DW_FORM_sdata";
11865 case DW_FORM_strp:
11866 return "DW_FORM_strp";
11867 case DW_FORM_udata:
11868 return "DW_FORM_udata";
11869 case DW_FORM_ref_addr:
11870 return "DW_FORM_ref_addr";
11871 case DW_FORM_ref1:
11872 return "DW_FORM_ref1";
11873 case DW_FORM_ref2:
11874 return "DW_FORM_ref2";
11875 case DW_FORM_ref4:
11876 return "DW_FORM_ref4";
11877 case DW_FORM_ref8:
11878 return "DW_FORM_ref8";
11879 case DW_FORM_ref_udata:
11880 return "DW_FORM_ref_udata";
11881 case DW_FORM_indirect:
11882 return "DW_FORM_indirect";
348e048f
DE
11883 case DW_FORM_sec_offset:
11884 return "DW_FORM_sec_offset";
11885 case DW_FORM_exprloc:
11886 return "DW_FORM_exprloc";
11887 case DW_FORM_flag_present:
11888 return "DW_FORM_flag_present";
11889 case DW_FORM_sig8:
11890 return "DW_FORM_sig8";
c906108c
SS
11891 default:
11892 return "DW_FORM_<unknown>";
11893 }
11894}
11895
11896/* Convert a DWARF stack opcode into its string name. */
11897
9eae7c52
TT
11898const char *
11899dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
11900{
11901 switch (op)
11902 {
11903 case DW_OP_addr:
11904 return "DW_OP_addr";
11905 case DW_OP_deref:
11906 return "DW_OP_deref";
11907 case DW_OP_const1u:
11908 return "DW_OP_const1u";
11909 case DW_OP_const1s:
11910 return "DW_OP_const1s";
11911 case DW_OP_const2u:
11912 return "DW_OP_const2u";
11913 case DW_OP_const2s:
11914 return "DW_OP_const2s";
11915 case DW_OP_const4u:
11916 return "DW_OP_const4u";
11917 case DW_OP_const4s:
11918 return "DW_OP_const4s";
11919 case DW_OP_const8u:
11920 return "DW_OP_const8u";
11921 case DW_OP_const8s:
11922 return "DW_OP_const8s";
11923 case DW_OP_constu:
11924 return "DW_OP_constu";
11925 case DW_OP_consts:
11926 return "DW_OP_consts";
11927 case DW_OP_dup:
11928 return "DW_OP_dup";
11929 case DW_OP_drop:
11930 return "DW_OP_drop";
11931 case DW_OP_over:
11932 return "DW_OP_over";
11933 case DW_OP_pick:
11934 return "DW_OP_pick";
11935 case DW_OP_swap:
11936 return "DW_OP_swap";
11937 case DW_OP_rot:
11938 return "DW_OP_rot";
11939 case DW_OP_xderef:
11940 return "DW_OP_xderef";
11941 case DW_OP_abs:
11942 return "DW_OP_abs";
11943 case DW_OP_and:
11944 return "DW_OP_and";
11945 case DW_OP_div:
11946 return "DW_OP_div";
11947 case DW_OP_minus:
11948 return "DW_OP_minus";
11949 case DW_OP_mod:
11950 return "DW_OP_mod";
11951 case DW_OP_mul:
11952 return "DW_OP_mul";
11953 case DW_OP_neg:
11954 return "DW_OP_neg";
11955 case DW_OP_not:
11956 return "DW_OP_not";
11957 case DW_OP_or:
11958 return "DW_OP_or";
11959 case DW_OP_plus:
11960 return "DW_OP_plus";
11961 case DW_OP_plus_uconst:
11962 return "DW_OP_plus_uconst";
11963 case DW_OP_shl:
11964 return "DW_OP_shl";
11965 case DW_OP_shr:
11966 return "DW_OP_shr";
11967 case DW_OP_shra:
11968 return "DW_OP_shra";
11969 case DW_OP_xor:
11970 return "DW_OP_xor";
11971 case DW_OP_bra:
11972 return "DW_OP_bra";
11973 case DW_OP_eq:
11974 return "DW_OP_eq";
11975 case DW_OP_ge:
11976 return "DW_OP_ge";
11977 case DW_OP_gt:
11978 return "DW_OP_gt";
11979 case DW_OP_le:
11980 return "DW_OP_le";
11981 case DW_OP_lt:
11982 return "DW_OP_lt";
11983 case DW_OP_ne:
11984 return "DW_OP_ne";
11985 case DW_OP_skip:
11986 return "DW_OP_skip";
11987 case DW_OP_lit0:
11988 return "DW_OP_lit0";
11989 case DW_OP_lit1:
11990 return "DW_OP_lit1";
11991 case DW_OP_lit2:
11992 return "DW_OP_lit2";
11993 case DW_OP_lit3:
11994 return "DW_OP_lit3";
11995 case DW_OP_lit4:
11996 return "DW_OP_lit4";
11997 case DW_OP_lit5:
11998 return "DW_OP_lit5";
11999 case DW_OP_lit6:
12000 return "DW_OP_lit6";
12001 case DW_OP_lit7:
12002 return "DW_OP_lit7";
12003 case DW_OP_lit8:
12004 return "DW_OP_lit8";
12005 case DW_OP_lit9:
12006 return "DW_OP_lit9";
12007 case DW_OP_lit10:
12008 return "DW_OP_lit10";
12009 case DW_OP_lit11:
12010 return "DW_OP_lit11";
12011 case DW_OP_lit12:
12012 return "DW_OP_lit12";
12013 case DW_OP_lit13:
12014 return "DW_OP_lit13";
12015 case DW_OP_lit14:
12016 return "DW_OP_lit14";
12017 case DW_OP_lit15:
12018 return "DW_OP_lit15";
12019 case DW_OP_lit16:
12020 return "DW_OP_lit16";
12021 case DW_OP_lit17:
12022 return "DW_OP_lit17";
12023 case DW_OP_lit18:
12024 return "DW_OP_lit18";
12025 case DW_OP_lit19:
12026 return "DW_OP_lit19";
12027 case DW_OP_lit20:
12028 return "DW_OP_lit20";
12029 case DW_OP_lit21:
12030 return "DW_OP_lit21";
12031 case DW_OP_lit22:
12032 return "DW_OP_lit22";
12033 case DW_OP_lit23:
12034 return "DW_OP_lit23";
12035 case DW_OP_lit24:
12036 return "DW_OP_lit24";
12037 case DW_OP_lit25:
12038 return "DW_OP_lit25";
12039 case DW_OP_lit26:
12040 return "DW_OP_lit26";
12041 case DW_OP_lit27:
12042 return "DW_OP_lit27";
12043 case DW_OP_lit28:
12044 return "DW_OP_lit28";
12045 case DW_OP_lit29:
12046 return "DW_OP_lit29";
12047 case DW_OP_lit30:
12048 return "DW_OP_lit30";
12049 case DW_OP_lit31:
12050 return "DW_OP_lit31";
12051 case DW_OP_reg0:
12052 return "DW_OP_reg0";
12053 case DW_OP_reg1:
12054 return "DW_OP_reg1";
12055 case DW_OP_reg2:
12056 return "DW_OP_reg2";
12057 case DW_OP_reg3:
12058 return "DW_OP_reg3";
12059 case DW_OP_reg4:
12060 return "DW_OP_reg4";
12061 case DW_OP_reg5:
12062 return "DW_OP_reg5";
12063 case DW_OP_reg6:
12064 return "DW_OP_reg6";
12065 case DW_OP_reg7:
12066 return "DW_OP_reg7";
12067 case DW_OP_reg8:
12068 return "DW_OP_reg8";
12069 case DW_OP_reg9:
12070 return "DW_OP_reg9";
12071 case DW_OP_reg10:
12072 return "DW_OP_reg10";
12073 case DW_OP_reg11:
12074 return "DW_OP_reg11";
12075 case DW_OP_reg12:
12076 return "DW_OP_reg12";
12077 case DW_OP_reg13:
12078 return "DW_OP_reg13";
12079 case DW_OP_reg14:
12080 return "DW_OP_reg14";
12081 case DW_OP_reg15:
12082 return "DW_OP_reg15";
12083 case DW_OP_reg16:
12084 return "DW_OP_reg16";
12085 case DW_OP_reg17:
12086 return "DW_OP_reg17";
12087 case DW_OP_reg18:
12088 return "DW_OP_reg18";
12089 case DW_OP_reg19:
12090 return "DW_OP_reg19";
12091 case DW_OP_reg20:
12092 return "DW_OP_reg20";
12093 case DW_OP_reg21:
12094 return "DW_OP_reg21";
12095 case DW_OP_reg22:
12096 return "DW_OP_reg22";
12097 case DW_OP_reg23:
12098 return "DW_OP_reg23";
12099 case DW_OP_reg24:
12100 return "DW_OP_reg24";
12101 case DW_OP_reg25:
12102 return "DW_OP_reg25";
12103 case DW_OP_reg26:
12104 return "DW_OP_reg26";
12105 case DW_OP_reg27:
12106 return "DW_OP_reg27";
12107 case DW_OP_reg28:
12108 return "DW_OP_reg28";
12109 case DW_OP_reg29:
12110 return "DW_OP_reg29";
12111 case DW_OP_reg30:
12112 return "DW_OP_reg30";
12113 case DW_OP_reg31:
12114 return "DW_OP_reg31";
12115 case DW_OP_breg0:
12116 return "DW_OP_breg0";
12117 case DW_OP_breg1:
12118 return "DW_OP_breg1";
12119 case DW_OP_breg2:
12120 return "DW_OP_breg2";
12121 case DW_OP_breg3:
12122 return "DW_OP_breg3";
12123 case DW_OP_breg4:
12124 return "DW_OP_breg4";
12125 case DW_OP_breg5:
12126 return "DW_OP_breg5";
12127 case DW_OP_breg6:
12128 return "DW_OP_breg6";
12129 case DW_OP_breg7:
12130 return "DW_OP_breg7";
12131 case DW_OP_breg8:
12132 return "DW_OP_breg8";
12133 case DW_OP_breg9:
12134 return "DW_OP_breg9";
12135 case DW_OP_breg10:
12136 return "DW_OP_breg10";
12137 case DW_OP_breg11:
12138 return "DW_OP_breg11";
12139 case DW_OP_breg12:
12140 return "DW_OP_breg12";
12141 case DW_OP_breg13:
12142 return "DW_OP_breg13";
12143 case DW_OP_breg14:
12144 return "DW_OP_breg14";
12145 case DW_OP_breg15:
12146 return "DW_OP_breg15";
12147 case DW_OP_breg16:
12148 return "DW_OP_breg16";
12149 case DW_OP_breg17:
12150 return "DW_OP_breg17";
12151 case DW_OP_breg18:
12152 return "DW_OP_breg18";
12153 case DW_OP_breg19:
12154 return "DW_OP_breg19";
12155 case DW_OP_breg20:
12156 return "DW_OP_breg20";
12157 case DW_OP_breg21:
12158 return "DW_OP_breg21";
12159 case DW_OP_breg22:
12160 return "DW_OP_breg22";
12161 case DW_OP_breg23:
12162 return "DW_OP_breg23";
12163 case DW_OP_breg24:
12164 return "DW_OP_breg24";
12165 case DW_OP_breg25:
12166 return "DW_OP_breg25";
12167 case DW_OP_breg26:
12168 return "DW_OP_breg26";
12169 case DW_OP_breg27:
12170 return "DW_OP_breg27";
12171 case DW_OP_breg28:
12172 return "DW_OP_breg28";
12173 case DW_OP_breg29:
12174 return "DW_OP_breg29";
12175 case DW_OP_breg30:
12176 return "DW_OP_breg30";
12177 case DW_OP_breg31:
12178 return "DW_OP_breg31";
12179 case DW_OP_regx:
12180 return "DW_OP_regx";
12181 case DW_OP_fbreg:
12182 return "DW_OP_fbreg";
12183 case DW_OP_bregx:
12184 return "DW_OP_bregx";
12185 case DW_OP_piece:
12186 return "DW_OP_piece";
12187 case DW_OP_deref_size:
12188 return "DW_OP_deref_size";
12189 case DW_OP_xderef_size:
12190 return "DW_OP_xderef_size";
12191 case DW_OP_nop:
12192 return "DW_OP_nop";
b7619582 12193 /* DWARF 3 extensions. */
ed348acc
EZ
12194 case DW_OP_push_object_address:
12195 return "DW_OP_push_object_address";
12196 case DW_OP_call2:
12197 return "DW_OP_call2";
12198 case DW_OP_call4:
12199 return "DW_OP_call4";
12200 case DW_OP_call_ref:
12201 return "DW_OP_call_ref";
b7619582
GF
12202 case DW_OP_form_tls_address:
12203 return "DW_OP_form_tls_address";
12204 case DW_OP_call_frame_cfa:
12205 return "DW_OP_call_frame_cfa";
12206 case DW_OP_bit_piece:
12207 return "DW_OP_bit_piece";
9eae7c52
TT
12208 /* DWARF 4 extensions. */
12209 case DW_OP_implicit_value:
12210 return "DW_OP_implicit_value";
12211 case DW_OP_stack_value:
12212 return "DW_OP_stack_value";
12213 /* GNU extensions. */
ed348acc
EZ
12214 case DW_OP_GNU_push_tls_address:
12215 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12216 case DW_OP_GNU_uninit:
12217 return "DW_OP_GNU_uninit";
c906108c 12218 default:
9eae7c52 12219 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12220 }
12221}
12222
12223static char *
fba45db2 12224dwarf_bool_name (unsigned mybool)
c906108c
SS
12225{
12226 if (mybool)
12227 return "TRUE";
12228 else
12229 return "FALSE";
12230}
12231
12232/* Convert a DWARF type code into its string name. */
12233
12234static char *
aa1ee363 12235dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12236{
12237 switch (enc)
12238 {
b7619582
GF
12239 case DW_ATE_void:
12240 return "DW_ATE_void";
c906108c
SS
12241 case DW_ATE_address:
12242 return "DW_ATE_address";
12243 case DW_ATE_boolean:
12244 return "DW_ATE_boolean";
12245 case DW_ATE_complex_float:
12246 return "DW_ATE_complex_float";
12247 case DW_ATE_float:
12248 return "DW_ATE_float";
12249 case DW_ATE_signed:
12250 return "DW_ATE_signed";
12251 case DW_ATE_signed_char:
12252 return "DW_ATE_signed_char";
12253 case DW_ATE_unsigned:
12254 return "DW_ATE_unsigned";
12255 case DW_ATE_unsigned_char:
12256 return "DW_ATE_unsigned_char";
b7619582 12257 /* DWARF 3. */
d9fa45fe
DC
12258 case DW_ATE_imaginary_float:
12259 return "DW_ATE_imaginary_float";
b7619582
GF
12260 case DW_ATE_packed_decimal:
12261 return "DW_ATE_packed_decimal";
12262 case DW_ATE_numeric_string:
12263 return "DW_ATE_numeric_string";
12264 case DW_ATE_edited:
12265 return "DW_ATE_edited";
12266 case DW_ATE_signed_fixed:
12267 return "DW_ATE_signed_fixed";
12268 case DW_ATE_unsigned_fixed:
12269 return "DW_ATE_unsigned_fixed";
12270 case DW_ATE_decimal_float:
12271 return "DW_ATE_decimal_float";
75079b2b
TT
12272 /* DWARF 4. */
12273 case DW_ATE_UTF:
12274 return "DW_ATE_UTF";
b7619582
GF
12275 /* HP extensions. */
12276 case DW_ATE_HP_float80:
12277 return "DW_ATE_HP_float80";
12278 case DW_ATE_HP_complex_float80:
12279 return "DW_ATE_HP_complex_float80";
12280 case DW_ATE_HP_float128:
12281 return "DW_ATE_HP_float128";
12282 case DW_ATE_HP_complex_float128:
12283 return "DW_ATE_HP_complex_float128";
12284 case DW_ATE_HP_floathpintel:
12285 return "DW_ATE_HP_floathpintel";
12286 case DW_ATE_HP_imaginary_float80:
12287 return "DW_ATE_HP_imaginary_float80";
12288 case DW_ATE_HP_imaginary_float128:
12289 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12290 default:
12291 return "DW_ATE_<unknown>";
12292 }
12293}
12294
12295/* Convert a DWARF call frame info operation to its string name. */
12296
12297#if 0
12298static char *
aa1ee363 12299dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12300{
12301 switch (cfi_opc)
12302 {
12303 case DW_CFA_advance_loc:
12304 return "DW_CFA_advance_loc";
12305 case DW_CFA_offset:
12306 return "DW_CFA_offset";
12307 case DW_CFA_restore:
12308 return "DW_CFA_restore";
12309 case DW_CFA_nop:
12310 return "DW_CFA_nop";
12311 case DW_CFA_set_loc:
12312 return "DW_CFA_set_loc";
12313 case DW_CFA_advance_loc1:
12314 return "DW_CFA_advance_loc1";
12315 case DW_CFA_advance_loc2:
12316 return "DW_CFA_advance_loc2";
12317 case DW_CFA_advance_loc4:
12318 return "DW_CFA_advance_loc4";
12319 case DW_CFA_offset_extended:
12320 return "DW_CFA_offset_extended";
12321 case DW_CFA_restore_extended:
12322 return "DW_CFA_restore_extended";
12323 case DW_CFA_undefined:
12324 return "DW_CFA_undefined";
12325 case DW_CFA_same_value:
12326 return "DW_CFA_same_value";
12327 case DW_CFA_register:
12328 return "DW_CFA_register";
12329 case DW_CFA_remember_state:
12330 return "DW_CFA_remember_state";
12331 case DW_CFA_restore_state:
12332 return "DW_CFA_restore_state";
12333 case DW_CFA_def_cfa:
12334 return "DW_CFA_def_cfa";
12335 case DW_CFA_def_cfa_register:
12336 return "DW_CFA_def_cfa_register";
12337 case DW_CFA_def_cfa_offset:
12338 return "DW_CFA_def_cfa_offset";
b7619582 12339 /* DWARF 3. */
985cb1a3
JM
12340 case DW_CFA_def_cfa_expression:
12341 return "DW_CFA_def_cfa_expression";
12342 case DW_CFA_expression:
12343 return "DW_CFA_expression";
12344 case DW_CFA_offset_extended_sf:
12345 return "DW_CFA_offset_extended_sf";
12346 case DW_CFA_def_cfa_sf:
12347 return "DW_CFA_def_cfa_sf";
12348 case DW_CFA_def_cfa_offset_sf:
12349 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12350 case DW_CFA_val_offset:
12351 return "DW_CFA_val_offset";
12352 case DW_CFA_val_offset_sf:
12353 return "DW_CFA_val_offset_sf";
12354 case DW_CFA_val_expression:
12355 return "DW_CFA_val_expression";
12356 /* SGI/MIPS specific. */
c906108c
SS
12357 case DW_CFA_MIPS_advance_loc8:
12358 return "DW_CFA_MIPS_advance_loc8";
b7619582 12359 /* GNU extensions. */
985cb1a3
JM
12360 case DW_CFA_GNU_window_save:
12361 return "DW_CFA_GNU_window_save";
12362 case DW_CFA_GNU_args_size:
12363 return "DW_CFA_GNU_args_size";
12364 case DW_CFA_GNU_negative_offset_extended:
12365 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12366 default:
12367 return "DW_CFA_<unknown>";
12368 }
12369}
12370#endif
12371
f9aca02d 12372static void
d97bc12b 12373dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12374{
12375 unsigned int i;
12376
d97bc12b
DE
12377 print_spaces (indent, f);
12378 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12379 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12380
12381 if (die->parent != NULL)
12382 {
12383 print_spaces (indent, f);
12384 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12385 die->parent->offset);
12386 }
12387
12388 print_spaces (indent, f);
12389 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12390 dwarf_bool_name (die->child != NULL));
c906108c 12391
d97bc12b
DE
12392 print_spaces (indent, f);
12393 fprintf_unfiltered (f, " attributes:\n");
12394
c906108c
SS
12395 for (i = 0; i < die->num_attrs; ++i)
12396 {
d97bc12b
DE
12397 print_spaces (indent, f);
12398 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12399 dwarf_attr_name (die->attrs[i].name),
12400 dwarf_form_name (die->attrs[i].form));
d97bc12b 12401
c906108c
SS
12402 switch (die->attrs[i].form)
12403 {
12404 case DW_FORM_ref_addr:
12405 case DW_FORM_addr:
d97bc12b 12406 fprintf_unfiltered (f, "address: ");
5af949e3 12407 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12408 break;
12409 case DW_FORM_block2:
12410 case DW_FORM_block4:
12411 case DW_FORM_block:
12412 case DW_FORM_block1:
d97bc12b 12413 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 12414 break;
2dc7f7b3
TT
12415 case DW_FORM_exprloc:
12416 fprintf_unfiltered (f, "expression: size %u",
12417 DW_BLOCK (&die->attrs[i])->size);
12418 break;
10b3939b
DJ
12419 case DW_FORM_ref1:
12420 case DW_FORM_ref2:
12421 case DW_FORM_ref4:
d97bc12b 12422 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12423 (long) (DW_ADDR (&die->attrs[i])));
12424 break;
c906108c
SS
12425 case DW_FORM_data1:
12426 case DW_FORM_data2:
12427 case DW_FORM_data4:
ce5d95e1 12428 case DW_FORM_data8:
c906108c
SS
12429 case DW_FORM_udata:
12430 case DW_FORM_sdata:
43bbcdc2
PH
12431 fprintf_unfiltered (f, "constant: %s",
12432 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 12433 break;
2dc7f7b3
TT
12434 case DW_FORM_sec_offset:
12435 fprintf_unfiltered (f, "section offset: %s",
12436 pulongest (DW_UNSND (&die->attrs[i])));
12437 break;
348e048f
DE
12438 case DW_FORM_sig8:
12439 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12440 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12441 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12442 else
12443 fprintf_unfiltered (f, "signatured type, offset: unknown");
12444 break;
c906108c 12445 case DW_FORM_string:
4bdf3d34 12446 case DW_FORM_strp:
8285870a 12447 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 12448 DW_STRING (&die->attrs[i])
8285870a
JK
12449 ? DW_STRING (&die->attrs[i]) : "",
12450 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
12451 break;
12452 case DW_FORM_flag:
12453 if (DW_UNSND (&die->attrs[i]))
d97bc12b 12454 fprintf_unfiltered (f, "flag: TRUE");
c906108c 12455 else
d97bc12b 12456 fprintf_unfiltered (f, "flag: FALSE");
c906108c 12457 break;
2dc7f7b3
TT
12458 case DW_FORM_flag_present:
12459 fprintf_unfiltered (f, "flag: TRUE");
12460 break;
a8329558
KW
12461 case DW_FORM_indirect:
12462 /* the reader will have reduced the indirect form to
12463 the "base form" so this form should not occur */
d97bc12b 12464 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 12465 break;
c906108c 12466 default:
d97bc12b 12467 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 12468 die->attrs[i].form);
d97bc12b 12469 break;
c906108c 12470 }
d97bc12b 12471 fprintf_unfiltered (f, "\n");
c906108c
SS
12472 }
12473}
12474
f9aca02d 12475static void
d97bc12b 12476dump_die_for_error (struct die_info *die)
c906108c 12477{
d97bc12b
DE
12478 dump_die_shallow (gdb_stderr, 0, die);
12479}
12480
12481static void
12482dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12483{
12484 int indent = level * 4;
12485
12486 gdb_assert (die != NULL);
12487
12488 if (level >= max_level)
12489 return;
12490
12491 dump_die_shallow (f, indent, die);
12492
12493 if (die->child != NULL)
c906108c 12494 {
d97bc12b
DE
12495 print_spaces (indent, f);
12496 fprintf_unfiltered (f, " Children:");
12497 if (level + 1 < max_level)
12498 {
12499 fprintf_unfiltered (f, "\n");
12500 dump_die_1 (f, level + 1, max_level, die->child);
12501 }
12502 else
12503 {
12504 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12505 }
12506 }
12507
12508 if (die->sibling != NULL && level > 0)
12509 {
12510 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
12511 }
12512}
12513
d97bc12b
DE
12514/* This is called from the pdie macro in gdbinit.in.
12515 It's not static so gcc will keep a copy callable from gdb. */
12516
12517void
12518dump_die (struct die_info *die, int max_level)
12519{
12520 dump_die_1 (gdb_stdlog, 0, max_level, die);
12521}
12522
f9aca02d 12523static void
51545339 12524store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12525{
51545339 12526 void **slot;
c906108c 12527
51545339
DJ
12528 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12529
12530 *slot = die;
c906108c
SS
12531}
12532
93311388
DE
12533static int
12534is_ref_attr (struct attribute *attr)
c906108c 12535{
c906108c
SS
12536 switch (attr->form)
12537 {
12538 case DW_FORM_ref_addr:
c906108c
SS
12539 case DW_FORM_ref1:
12540 case DW_FORM_ref2:
12541 case DW_FORM_ref4:
613e1657 12542 case DW_FORM_ref8:
c906108c 12543 case DW_FORM_ref_udata:
93311388 12544 return 1;
c906108c 12545 default:
93311388 12546 return 0;
c906108c 12547 }
93311388
DE
12548}
12549
12550static unsigned int
12551dwarf2_get_ref_die_offset (struct attribute *attr)
12552{
12553 if (is_ref_attr (attr))
12554 return DW_ADDR (attr);
12555
12556 complaint (&symfile_complaints,
12557 _("unsupported die ref attribute form: '%s'"),
12558 dwarf_form_name (attr->form));
12559 return 0;
c906108c
SS
12560}
12561
43bbcdc2
PH
12562/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12563 * the value held by the attribute is not constant. */
a02abb62 12564
43bbcdc2 12565static LONGEST
a02abb62
JB
12566dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12567{
12568 if (attr->form == DW_FORM_sdata)
12569 return DW_SND (attr);
12570 else if (attr->form == DW_FORM_udata
12571 || attr->form == DW_FORM_data1
12572 || attr->form == DW_FORM_data2
12573 || attr->form == DW_FORM_data4
12574 || attr->form == DW_FORM_data8)
12575 return DW_UNSND (attr);
12576 else
12577 {
e2e0b3e5 12578 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
12579 dwarf_form_name (attr->form));
12580 return default_value;
12581 }
12582}
12583
03dd20cc 12584/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
12585 unit and add it to our queue.
12586 The result is non-zero if PER_CU was queued, otherwise the result is zero
12587 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 12588
348e048f 12589static int
03dd20cc
DJ
12590maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12591 struct dwarf2_per_cu_data *per_cu)
12592{
98bfdba5
PA
12593 /* We may arrive here during partial symbol reading, if we need full
12594 DIEs to process an unusual case (e.g. template arguments). Do
12595 not queue PER_CU, just tell our caller to load its DIEs. */
12596 if (dwarf2_per_objfile->reading_partial_symbols)
12597 {
12598 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12599 return 1;
12600 return 0;
12601 }
12602
03dd20cc
DJ
12603 /* Mark the dependence relation so that we don't flush PER_CU
12604 too early. */
12605 dwarf2_add_dependence (this_cu, per_cu);
12606
12607 /* If it's already on the queue, we have nothing to do. */
12608 if (per_cu->queued)
348e048f 12609 return 0;
03dd20cc
DJ
12610
12611 /* If the compilation unit is already loaded, just mark it as
12612 used. */
12613 if (per_cu->cu != NULL)
12614 {
12615 per_cu->cu->last_used = 0;
348e048f 12616 return 0;
03dd20cc
DJ
12617 }
12618
12619 /* Add it to the queue. */
12620 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
12621
12622 return 1;
12623}
12624
12625/* Follow reference or signature attribute ATTR of SRC_DIE.
12626 On entry *REF_CU is the CU of SRC_DIE.
12627 On exit *REF_CU is the CU of the result. */
12628
12629static struct die_info *
12630follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12631 struct dwarf2_cu **ref_cu)
12632{
12633 struct die_info *die;
12634
12635 if (is_ref_attr (attr))
12636 die = follow_die_ref (src_die, attr, ref_cu);
12637 else if (attr->form == DW_FORM_sig8)
12638 die = follow_die_sig (src_die, attr, ref_cu);
12639 else
12640 {
12641 dump_die_for_error (src_die);
12642 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12643 (*ref_cu)->objfile->name);
12644 }
12645
12646 return die;
03dd20cc
DJ
12647}
12648
5c631832
JK
12649/* Follow reference OFFSET.
12650 On entry *REF_CU is the CU of source DIE referencing OFFSET.
f504f079
DE
12651 On exit *REF_CU is the CU of the result. */
12652
f9aca02d 12653static struct die_info *
5c631832 12654follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 12655{
10b3939b 12656 struct die_info temp_die;
f2f0e013 12657 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 12658
348e048f
DE
12659 gdb_assert (cu->per_cu != NULL);
12660
98bfdba5
PA
12661 target_cu = cu;
12662
348e048f
DE
12663 if (cu->per_cu->from_debug_types)
12664 {
12665 /* .debug_types CUs cannot reference anything outside their CU.
12666 If they need to, they have to reference a signatured type via
12667 DW_FORM_sig8. */
12668 if (! offset_in_cu_p (&cu->header, offset))
5c631832 12669 return NULL;
348e048f
DE
12670 }
12671 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
12672 {
12673 struct dwarf2_per_cu_data *per_cu;
9a619af0 12674
45452591 12675 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
12676
12677 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
12678 if (maybe_queue_comp_unit (cu, per_cu))
12679 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 12680
10b3939b
DJ
12681 target_cu = per_cu->cu;
12682 }
98bfdba5
PA
12683 else if (cu->dies == NULL)
12684 {
12685 /* We're loading full DIEs during partial symbol reading. */
12686 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
12687 load_full_comp_unit (cu->per_cu, cu->objfile);
12688 }
c906108c 12689
f2f0e013 12690 *ref_cu = target_cu;
51545339 12691 temp_die.offset = offset;
5c631832
JK
12692 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12693}
10b3939b 12694
5c631832
JK
12695/* Follow reference attribute ATTR of SRC_DIE.
12696 On entry *REF_CU is the CU of SRC_DIE.
12697 On exit *REF_CU is the CU of the result. */
12698
12699static struct die_info *
12700follow_die_ref (struct die_info *src_die, struct attribute *attr,
12701 struct dwarf2_cu **ref_cu)
12702{
12703 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12704 struct dwarf2_cu *cu = *ref_cu;
12705 struct die_info *die;
12706
12707 die = follow_die_offset (offset, ref_cu);
12708 if (!die)
12709 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
12710 "at 0x%x [in module %s]"),
12711 offset, src_die->offset, cu->objfile->name);
348e048f 12712
5c631832
JK
12713 return die;
12714}
12715
12716/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
12717 value is intended for DW_OP_call*. */
12718
12719struct dwarf2_locexpr_baton
12720dwarf2_fetch_die_location_block (unsigned int offset,
12721 struct dwarf2_per_cu_data *per_cu)
12722{
12723 struct dwarf2_cu *cu = per_cu->cu;
12724 struct die_info *die;
12725 struct attribute *attr;
12726 struct dwarf2_locexpr_baton retval;
12727
12728 die = follow_die_offset (offset, &cu);
12729 if (!die)
12730 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
12731 offset, per_cu->cu->objfile->name);
12732
12733 attr = dwarf2_attr (die, DW_AT_location, cu);
12734 if (!attr)
12735 {
12736 /* DWARF: "If there is no such attribute, then there is no effect.". */
12737
12738 retval.data = NULL;
12739 retval.size = 0;
12740 }
12741 else
12742 {
12743 if (!attr_form_is_block (attr))
12744 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
12745 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
12746 offset, per_cu->cu->objfile->name);
12747
12748 retval.data = DW_BLOCK (attr)->data;
12749 retval.size = DW_BLOCK (attr)->size;
12750 }
12751 retval.per_cu = cu->per_cu;
12752 return retval;
348e048f
DE
12753}
12754
12755/* Follow the signature attribute ATTR in SRC_DIE.
12756 On entry *REF_CU is the CU of SRC_DIE.
12757 On exit *REF_CU is the CU of the result. */
12758
12759static struct die_info *
12760follow_die_sig (struct die_info *src_die, struct attribute *attr,
12761 struct dwarf2_cu **ref_cu)
12762{
12763 struct objfile *objfile = (*ref_cu)->objfile;
12764 struct die_info temp_die;
12765 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12766 struct dwarf2_cu *sig_cu;
12767 struct die_info *die;
12768
12769 /* sig_type will be NULL if the signatured type is missing from
12770 the debug info. */
12771 if (sig_type == NULL)
12772 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12773 "at 0x%x [in module %s]"),
12774 src_die->offset, objfile->name);
12775
12776 /* If necessary, add it to the queue and load its DIEs. */
12777
12778 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12779 read_signatured_type (objfile, sig_type);
12780
12781 gdb_assert (sig_type->per_cu.cu != NULL);
12782
12783 sig_cu = sig_type->per_cu.cu;
12784 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12785 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12786 if (die)
12787 {
12788 *ref_cu = sig_cu;
12789 return die;
12790 }
12791
12792 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12793 "at 0x%x [in module %s]"),
12794 sig_type->type_offset, src_die->offset, objfile->name);
12795}
12796
12797/* Given an offset of a signatured type, return its signatured_type. */
12798
12799static struct signatured_type *
12800lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12801{
12802 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12803 unsigned int length, initial_length_size;
12804 unsigned int sig_offset;
12805 struct signatured_type find_entry, *type_sig;
12806
12807 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12808 sig_offset = (initial_length_size
12809 + 2 /*version*/
12810 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12811 + 1 /*address_size*/);
12812 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12813 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12814
12815 /* This is only used to lookup previously recorded types.
12816 If we didn't find it, it's our bug. */
12817 gdb_assert (type_sig != NULL);
12818 gdb_assert (offset == type_sig->offset);
12819
12820 return type_sig;
12821}
12822
12823/* Read in signatured type at OFFSET and build its CU and die(s). */
12824
12825static void
12826read_signatured_type_at_offset (struct objfile *objfile,
12827 unsigned int offset)
12828{
12829 struct signatured_type *type_sig;
12830
be391dca
TT
12831 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12832
348e048f
DE
12833 /* We have the section offset, but we need the signature to do the
12834 hash table lookup. */
12835 type_sig = lookup_signatured_type_at_offset (objfile, offset);
12836
12837 gdb_assert (type_sig->per_cu.cu == NULL);
12838
12839 read_signatured_type (objfile, type_sig);
12840
12841 gdb_assert (type_sig->per_cu.cu != NULL);
12842}
12843
12844/* Read in a signatured type and build its CU and DIEs. */
12845
12846static void
12847read_signatured_type (struct objfile *objfile,
12848 struct signatured_type *type_sig)
12849{
1fd400ff 12850 gdb_byte *types_ptr;
348e048f
DE
12851 struct die_reader_specs reader_specs;
12852 struct dwarf2_cu *cu;
12853 ULONGEST signature;
12854 struct cleanup *back_to, *free_cu_cleanup;
12855 struct attribute *attr;
12856
1fd400ff
TT
12857 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12858 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
12859
348e048f
DE
12860 gdb_assert (type_sig->per_cu.cu == NULL);
12861
12862 cu = xmalloc (sizeof (struct dwarf2_cu));
12863 memset (cu, 0, sizeof (struct dwarf2_cu));
12864 obstack_init (&cu->comp_unit_obstack);
12865 cu->objfile = objfile;
12866 type_sig->per_cu.cu = cu;
12867 cu->per_cu = &type_sig->per_cu;
12868
12869 /* If an error occurs while loading, release our storage. */
12870 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
12871
12872 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
12873 types_ptr, objfile->obfd);
12874 gdb_assert (signature == type_sig->signature);
12875
12876 cu->die_hash
12877 = htab_create_alloc_ex (cu->header.length / 12,
12878 die_hash,
12879 die_eq,
12880 NULL,
12881 &cu->comp_unit_obstack,
12882 hashtab_obstack_allocate,
12883 dummy_obstack_deallocate);
12884
12885 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
12886 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
12887
12888 init_cu_die_reader (&reader_specs, cu);
12889
12890 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
12891 NULL /*parent*/);
12892
12893 /* We try not to read any attributes in this function, because not
12894 all objfiles needed for references have been loaded yet, and symbol
12895 table processing isn't initialized. But we have to set the CU language,
12896 or we won't be able to build types correctly. */
12897 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
12898 if (attr)
12899 set_cu_language (DW_UNSND (attr), cu);
12900 else
12901 set_cu_language (language_minimal, cu);
12902
12903 do_cleanups (back_to);
12904
12905 /* We've successfully allocated this compilation unit. Let our caller
12906 clean it up when finished with it. */
12907 discard_cleanups (free_cu_cleanup);
12908
12909 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
12910 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
12911}
12912
c906108c
SS
12913/* Decode simple location descriptions.
12914 Given a pointer to a dwarf block that defines a location, compute
12915 the location and return the value.
12916
4cecd739
DJ
12917 NOTE drow/2003-11-18: This function is called in two situations
12918 now: for the address of static or global variables (partial symbols
12919 only) and for offsets into structures which are expected to be
12920 (more or less) constant. The partial symbol case should go away,
12921 and only the constant case should remain. That will let this
12922 function complain more accurately. A few special modes are allowed
12923 without complaint for global variables (for instance, global
12924 register values and thread-local values).
c906108c
SS
12925
12926 A location description containing no operations indicates that the
4cecd739 12927 object is optimized out. The return value is 0 for that case.
6b992462
DJ
12928 FIXME drow/2003-11-16: No callers check for this case any more; soon all
12929 callers will only want a very basic result and this can become a
12930 complaint.
c906108c 12931
c906108c
SS
12932 Note that stack[0] is unused except as a default error return.
12933 Note that stack overflow is not yet handled. */
12934
12935static CORE_ADDR
e7c27a73 12936decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 12937{
e7c27a73 12938 struct objfile *objfile = cu->objfile;
c906108c
SS
12939 int i;
12940 int size = blk->size;
fe1b8b76 12941 gdb_byte *data = blk->data;
c906108c
SS
12942 CORE_ADDR stack[64];
12943 int stacki;
12944 unsigned int bytes_read, unsnd;
fe1b8b76 12945 gdb_byte op;
c906108c
SS
12946
12947 i = 0;
12948 stacki = 0;
12949 stack[stacki] = 0;
c906108c
SS
12950
12951 while (i < size)
12952 {
c906108c
SS
12953 op = data[i++];
12954 switch (op)
12955 {
f1bea926
JM
12956 case DW_OP_lit0:
12957 case DW_OP_lit1:
12958 case DW_OP_lit2:
12959 case DW_OP_lit3:
12960 case DW_OP_lit4:
12961 case DW_OP_lit5:
12962 case DW_OP_lit6:
12963 case DW_OP_lit7:
12964 case DW_OP_lit8:
12965 case DW_OP_lit9:
12966 case DW_OP_lit10:
12967 case DW_OP_lit11:
12968 case DW_OP_lit12:
12969 case DW_OP_lit13:
12970 case DW_OP_lit14:
12971 case DW_OP_lit15:
12972 case DW_OP_lit16:
12973 case DW_OP_lit17:
12974 case DW_OP_lit18:
12975 case DW_OP_lit19:
12976 case DW_OP_lit20:
12977 case DW_OP_lit21:
12978 case DW_OP_lit22:
12979 case DW_OP_lit23:
12980 case DW_OP_lit24:
12981 case DW_OP_lit25:
12982 case DW_OP_lit26:
12983 case DW_OP_lit27:
12984 case DW_OP_lit28:
12985 case DW_OP_lit29:
12986 case DW_OP_lit30:
12987 case DW_OP_lit31:
12988 stack[++stacki] = op - DW_OP_lit0;
12989 break;
12990
c906108c
SS
12991 case DW_OP_reg0:
12992 case DW_OP_reg1:
12993 case DW_OP_reg2:
12994 case DW_OP_reg3:
12995 case DW_OP_reg4:
12996 case DW_OP_reg5:
12997 case DW_OP_reg6:
12998 case DW_OP_reg7:
12999 case DW_OP_reg8:
13000 case DW_OP_reg9:
13001 case DW_OP_reg10:
13002 case DW_OP_reg11:
13003 case DW_OP_reg12:
13004 case DW_OP_reg13:
13005 case DW_OP_reg14:
13006 case DW_OP_reg15:
13007 case DW_OP_reg16:
13008 case DW_OP_reg17:
13009 case DW_OP_reg18:
13010 case DW_OP_reg19:
13011 case DW_OP_reg20:
13012 case DW_OP_reg21:
13013 case DW_OP_reg22:
13014 case DW_OP_reg23:
13015 case DW_OP_reg24:
13016 case DW_OP_reg25:
13017 case DW_OP_reg26:
13018 case DW_OP_reg27:
13019 case DW_OP_reg28:
13020 case DW_OP_reg29:
13021 case DW_OP_reg30:
13022 case DW_OP_reg31:
c906108c 13023 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13024 if (i < size)
13025 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13026 break;
13027
13028 case DW_OP_regx:
c906108c
SS
13029 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13030 i += bytes_read;
c906108c 13031 stack[++stacki] = unsnd;
4cecd739
DJ
13032 if (i < size)
13033 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13034 break;
13035
13036 case DW_OP_addr:
107d2387 13037 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13038 cu, &bytes_read);
107d2387 13039 i += bytes_read;
c906108c
SS
13040 break;
13041
13042 case DW_OP_const1u:
13043 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13044 i += 1;
13045 break;
13046
13047 case DW_OP_const1s:
13048 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13049 i += 1;
13050 break;
13051
13052 case DW_OP_const2u:
13053 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13054 i += 2;
13055 break;
13056
13057 case DW_OP_const2s:
13058 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13059 i += 2;
13060 break;
13061
13062 case DW_OP_const4u:
13063 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13064 i += 4;
13065 break;
13066
13067 case DW_OP_const4s:
13068 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13069 i += 4;
13070 break;
13071
13072 case DW_OP_constu:
13073 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13074 &bytes_read);
c906108c
SS
13075 i += bytes_read;
13076 break;
13077
13078 case DW_OP_consts:
13079 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13080 i += bytes_read;
13081 break;
13082
f1bea926
JM
13083 case DW_OP_dup:
13084 stack[stacki + 1] = stack[stacki];
13085 stacki++;
13086 break;
13087
c906108c
SS
13088 case DW_OP_plus:
13089 stack[stacki - 1] += stack[stacki];
13090 stacki--;
13091 break;
13092
13093 case DW_OP_plus_uconst:
13094 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13095 i += bytes_read;
13096 break;
13097
13098 case DW_OP_minus:
f1bea926 13099 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13100 stacki--;
13101 break;
13102
7a292a7a 13103 case DW_OP_deref:
7a292a7a 13104 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13105 this using GDB's address_class enum. This is valid for partial
13106 global symbols, although the variable's address will be bogus
13107 in the psymtab. */
7a292a7a 13108 if (i < size)
4d3c2250 13109 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13110 break;
13111
9d774e44 13112 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13113 /* The top of the stack has the offset from the beginning
13114 of the thread control block at which the variable is located. */
13115 /* Nothing should follow this operator, so the top of stack would
13116 be returned. */
4cecd739
DJ
13117 /* This is valid for partial global symbols, but the variable's
13118 address will be bogus in the psymtab. */
9d774e44 13119 if (i < size)
4d3c2250 13120 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13121 break;
13122
42be36b3
CT
13123 case DW_OP_GNU_uninit:
13124 break;
13125
c906108c 13126 default:
e2e0b3e5 13127 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13128 dwarf_stack_op_name (op, 1));
c906108c
SS
13129 return (stack[stacki]);
13130 }
13131 }
13132 return (stack[stacki]);
13133}
13134
13135/* memory allocation interface */
13136
c906108c 13137static struct dwarf_block *
7b5a2f43 13138dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13139{
13140 struct dwarf_block *blk;
13141
13142 blk = (struct dwarf_block *)
7b5a2f43 13143 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13144 return (blk);
13145}
13146
13147static struct abbrev_info *
f3dd6933 13148dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13149{
13150 struct abbrev_info *abbrev;
13151
f3dd6933
DJ
13152 abbrev = (struct abbrev_info *)
13153 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13154 memset (abbrev, 0, sizeof (struct abbrev_info));
13155 return (abbrev);
13156}
13157
13158static struct die_info *
b60c80d6 13159dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13160{
13161 struct die_info *die;
b60c80d6
DJ
13162 size_t size = sizeof (struct die_info);
13163
13164 if (num_attrs > 1)
13165 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13166
b60c80d6 13167 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13168 memset (die, 0, sizeof (struct die_info));
13169 return (die);
13170}
2e276125
JB
13171
13172\f
13173/* Macro support. */
13174
13175
13176/* Return the full name of file number I in *LH's file name table.
13177 Use COMP_DIR as the name of the current directory of the
13178 compilation. The result is allocated using xmalloc; the caller is
13179 responsible for freeing it. */
13180static char *
13181file_full_name (int file, struct line_header *lh, const char *comp_dir)
13182{
6a83a1e6
EZ
13183 /* Is the file number a valid index into the line header's file name
13184 table? Remember that file numbers start with one, not zero. */
13185 if (1 <= file && file <= lh->num_file_names)
13186 {
13187 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13188
6a83a1e6
EZ
13189 if (IS_ABSOLUTE_PATH (fe->name))
13190 return xstrdup (fe->name);
13191 else
13192 {
13193 const char *dir;
13194 int dir_len;
13195 char *full_name;
13196
13197 if (fe->dir_index)
13198 dir = lh->include_dirs[fe->dir_index - 1];
13199 else
13200 dir = comp_dir;
13201
13202 if (dir)
13203 {
13204 dir_len = strlen (dir);
13205 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13206 strcpy (full_name, dir);
13207 full_name[dir_len] = '/';
13208 strcpy (full_name + dir_len + 1, fe->name);
13209 return full_name;
13210 }
13211 else
13212 return xstrdup (fe->name);
13213 }
13214 }
2e276125
JB
13215 else
13216 {
6a83a1e6
EZ
13217 /* The compiler produced a bogus file number. We can at least
13218 record the macro definitions made in the file, even if we
13219 won't be able to find the file by name. */
13220 char fake_name[80];
9a619af0 13221
6a83a1e6 13222 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13223
6e70227d 13224 complaint (&symfile_complaints,
6a83a1e6
EZ
13225 _("bad file number in macro information (%d)"),
13226 file);
2e276125 13227
6a83a1e6 13228 return xstrdup (fake_name);
2e276125
JB
13229 }
13230}
13231
13232
13233static struct macro_source_file *
13234macro_start_file (int file, int line,
13235 struct macro_source_file *current_file,
13236 const char *comp_dir,
13237 struct line_header *lh, struct objfile *objfile)
13238{
13239 /* The full name of this source file. */
13240 char *full_name = file_full_name (file, lh, comp_dir);
13241
13242 /* We don't create a macro table for this compilation unit
13243 at all until we actually get a filename. */
13244 if (! pending_macros)
4a146b47 13245 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13246 objfile->macro_cache);
2e276125
JB
13247
13248 if (! current_file)
13249 /* If we have no current file, then this must be the start_file
13250 directive for the compilation unit's main source file. */
13251 current_file = macro_set_main (pending_macros, full_name);
13252 else
13253 current_file = macro_include (current_file, line, full_name);
13254
13255 xfree (full_name);
6e70227d 13256
2e276125
JB
13257 return current_file;
13258}
13259
13260
13261/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13262 followed by a null byte. */
13263static char *
13264copy_string (const char *buf, int len)
13265{
13266 char *s = xmalloc (len + 1);
9a619af0 13267
2e276125
JB
13268 memcpy (s, buf, len);
13269 s[len] = '\0';
2e276125
JB
13270 return s;
13271}
13272
13273
13274static const char *
13275consume_improper_spaces (const char *p, const char *body)
13276{
13277 if (*p == ' ')
13278 {
4d3c2250 13279 complaint (&symfile_complaints,
e2e0b3e5 13280 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 13281 body);
2e276125
JB
13282
13283 while (*p == ' ')
13284 p++;
13285 }
13286
13287 return p;
13288}
13289
13290
13291static void
13292parse_macro_definition (struct macro_source_file *file, int line,
13293 const char *body)
13294{
13295 const char *p;
13296
13297 /* The body string takes one of two forms. For object-like macro
13298 definitions, it should be:
13299
13300 <macro name> " " <definition>
13301
13302 For function-like macro definitions, it should be:
13303
13304 <macro name> "() " <definition>
13305 or
13306 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13307
13308 Spaces may appear only where explicitly indicated, and in the
13309 <definition>.
13310
13311 The Dwarf 2 spec says that an object-like macro's name is always
13312 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13313 the space when the macro's definition is the empty string.
2e276125
JB
13314
13315 The Dwarf 2 spec says that there should be no spaces between the
13316 formal arguments in a function-like macro's formal argument list,
13317 but versions of GCC around March 2002 include spaces after the
13318 commas. */
13319
13320
13321 /* Find the extent of the macro name. The macro name is terminated
13322 by either a space or null character (for an object-like macro) or
13323 an opening paren (for a function-like macro). */
13324 for (p = body; *p; p++)
13325 if (*p == ' ' || *p == '(')
13326 break;
13327
13328 if (*p == ' ' || *p == '\0')
13329 {
13330 /* It's an object-like macro. */
13331 int name_len = p - body;
13332 char *name = copy_string (body, name_len);
13333 const char *replacement;
13334
13335 if (*p == ' ')
13336 replacement = body + name_len + 1;
13337 else
13338 {
4d3c2250 13339 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13340 replacement = body + name_len;
13341 }
6e70227d 13342
2e276125
JB
13343 macro_define_object (file, line, name, replacement);
13344
13345 xfree (name);
13346 }
13347 else if (*p == '(')
13348 {
13349 /* It's a function-like macro. */
13350 char *name = copy_string (body, p - body);
13351 int argc = 0;
13352 int argv_size = 1;
13353 char **argv = xmalloc (argv_size * sizeof (*argv));
13354
13355 p++;
13356
13357 p = consume_improper_spaces (p, body);
13358
13359 /* Parse the formal argument list. */
13360 while (*p && *p != ')')
13361 {
13362 /* Find the extent of the current argument name. */
13363 const char *arg_start = p;
13364
13365 while (*p && *p != ',' && *p != ')' && *p != ' ')
13366 p++;
13367
13368 if (! *p || p == arg_start)
4d3c2250 13369 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13370 else
13371 {
13372 /* Make sure argv has room for the new argument. */
13373 if (argc >= argv_size)
13374 {
13375 argv_size *= 2;
13376 argv = xrealloc (argv, argv_size * sizeof (*argv));
13377 }
13378
13379 argv[argc++] = copy_string (arg_start, p - arg_start);
13380 }
13381
13382 p = consume_improper_spaces (p, body);
13383
13384 /* Consume the comma, if present. */
13385 if (*p == ',')
13386 {
13387 p++;
13388
13389 p = consume_improper_spaces (p, body);
13390 }
13391 }
13392
13393 if (*p == ')')
13394 {
13395 p++;
13396
13397 if (*p == ' ')
13398 /* Perfectly formed definition, no complaints. */
13399 macro_define_function (file, line, name,
6e70227d 13400 argc, (const char **) argv,
2e276125
JB
13401 p + 1);
13402 else if (*p == '\0')
13403 {
13404 /* Complain, but do define it. */
4d3c2250 13405 dwarf2_macro_malformed_definition_complaint (body);
2e276125 13406 macro_define_function (file, line, name,
6e70227d 13407 argc, (const char **) argv,
2e276125
JB
13408 p);
13409 }
13410 else
13411 /* Just complain. */
4d3c2250 13412 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13413 }
13414 else
13415 /* Just complain. */
4d3c2250 13416 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13417
13418 xfree (name);
13419 {
13420 int i;
13421
13422 for (i = 0; i < argc; i++)
13423 xfree (argv[i]);
13424 }
13425 xfree (argv);
13426 }
13427 else
4d3c2250 13428 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13429}
13430
13431
13432static void
13433dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13434 char *comp_dir, bfd *abfd,
e7c27a73 13435 struct dwarf2_cu *cu)
2e276125 13436{
fe1b8b76 13437 gdb_byte *mac_ptr, *mac_end;
2e276125 13438 struct macro_source_file *current_file = 0;
757a13d0
JK
13439 enum dwarf_macinfo_record_type macinfo_type;
13440 int at_commandline;
2e276125 13441
be391dca
TT
13442 dwarf2_read_section (dwarf2_per_objfile->objfile,
13443 &dwarf2_per_objfile->macinfo);
dce234bc 13444 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 13445 {
e2e0b3e5 13446 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
13447 return;
13448 }
13449
757a13d0
JK
13450 /* First pass: Find the name of the base filename.
13451 This filename is needed in order to process all macros whose definition
13452 (or undefinition) comes from the command line. These macros are defined
13453 before the first DW_MACINFO_start_file entry, and yet still need to be
13454 associated to the base file.
13455
13456 To determine the base file name, we scan the macro definitions until we
13457 reach the first DW_MACINFO_start_file entry. We then initialize
13458 CURRENT_FILE accordingly so that any macro definition found before the
13459 first DW_MACINFO_start_file can still be associated to the base file. */
13460
dce234bc
PP
13461 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13462 mac_end = dwarf2_per_objfile->macinfo.buffer
13463 + dwarf2_per_objfile->macinfo.size;
2e276125 13464
757a13d0 13465 do
2e276125 13466 {
2e276125
JB
13467 /* Do we at least have room for a macinfo type byte? */
13468 if (mac_ptr >= mac_end)
13469 {
757a13d0
JK
13470 /* Complaint is printed during the second pass as GDB will probably
13471 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13472 break;
2e276125
JB
13473 }
13474
13475 macinfo_type = read_1_byte (abfd, mac_ptr);
13476 mac_ptr++;
13477
13478 switch (macinfo_type)
13479 {
13480 /* A zero macinfo type indicates the end of the macro
13481 information. */
13482 case 0:
757a13d0
JK
13483 break;
13484
13485 case DW_MACINFO_define:
13486 case DW_MACINFO_undef:
13487 /* Only skip the data by MAC_PTR. */
13488 {
13489 unsigned int bytes_read;
13490
13491 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13492 mac_ptr += bytes_read;
9b1c24c8 13493 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13494 mac_ptr += bytes_read;
13495 }
13496 break;
13497
13498 case DW_MACINFO_start_file:
13499 {
13500 unsigned int bytes_read;
13501 int line, file;
13502
13503 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13504 mac_ptr += bytes_read;
13505 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13506 mac_ptr += bytes_read;
13507
13508 current_file = macro_start_file (file, line, current_file, comp_dir,
13509 lh, cu->objfile);
13510 }
13511 break;
13512
13513 case DW_MACINFO_end_file:
13514 /* No data to skip by MAC_PTR. */
13515 break;
13516
13517 case DW_MACINFO_vendor_ext:
13518 /* Only skip the data by MAC_PTR. */
13519 {
13520 unsigned int bytes_read;
13521
13522 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13523 mac_ptr += bytes_read;
9b1c24c8 13524 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13525 mac_ptr += bytes_read;
13526 }
13527 break;
13528
13529 default:
13530 break;
13531 }
13532 } while (macinfo_type != 0 && current_file == NULL);
13533
13534 /* Second pass: Process all entries.
13535
13536 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13537 command-line macro definitions/undefinitions. This flag is unset when we
13538 reach the first DW_MACINFO_start_file entry. */
13539
dce234bc 13540 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
13541
13542 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13543 GDB is still reading the definitions from command line. First
13544 DW_MACINFO_start_file will need to be ignored as it was already executed
13545 to create CURRENT_FILE for the main source holding also the command line
13546 definitions. On first met DW_MACINFO_start_file this flag is reset to
13547 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13548
13549 at_commandline = 1;
13550
13551 do
13552 {
13553 /* Do we at least have room for a macinfo type byte? */
13554 if (mac_ptr >= mac_end)
13555 {
13556 dwarf2_macros_too_long_complaint ();
13557 break;
13558 }
13559
13560 macinfo_type = read_1_byte (abfd, mac_ptr);
13561 mac_ptr++;
13562
13563 switch (macinfo_type)
13564 {
13565 /* A zero macinfo type indicates the end of the macro
13566 information. */
13567 case 0:
13568 break;
2e276125
JB
13569
13570 case DW_MACINFO_define:
13571 case DW_MACINFO_undef:
13572 {
891d2f0b 13573 unsigned int bytes_read;
2e276125
JB
13574 int line;
13575 char *body;
13576
13577 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13578 mac_ptr += bytes_read;
9b1c24c8 13579 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13580 mac_ptr += bytes_read;
13581
13582 if (! current_file)
757a13d0
JK
13583 {
13584 /* DWARF violation as no main source is present. */
13585 complaint (&symfile_complaints,
13586 _("debug info with no main source gives macro %s "
13587 "on line %d: %s"),
6e70227d
DE
13588 macinfo_type == DW_MACINFO_define ?
13589 _("definition") :
905e0470
PM
13590 macinfo_type == DW_MACINFO_undef ?
13591 _("undefinition") :
13592 _("something-or-other"), line, body);
757a13d0
JK
13593 break;
13594 }
13595 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 13596 complaint (&symfile_complaints,
757a13d0
JK
13597 _("debug info gives %s macro %s with %s line %d: %s"),
13598 at_commandline ? _("command-line") : _("in-file"),
905e0470 13599 macinfo_type == DW_MACINFO_define ?
6e70227d 13600 _("definition") :
905e0470
PM
13601 macinfo_type == DW_MACINFO_undef ?
13602 _("undefinition") :
13603 _("something-or-other"),
757a13d0
JK
13604 line == 0 ? _("zero") : _("non-zero"), line, body);
13605
13606 if (macinfo_type == DW_MACINFO_define)
13607 parse_macro_definition (current_file, line, body);
13608 else if (macinfo_type == DW_MACINFO_undef)
13609 macro_undef (current_file, line, body);
2e276125
JB
13610 }
13611 break;
13612
13613 case DW_MACINFO_start_file:
13614 {
891d2f0b 13615 unsigned int bytes_read;
2e276125
JB
13616 int line, file;
13617
13618 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13619 mac_ptr += bytes_read;
13620 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13621 mac_ptr += bytes_read;
13622
757a13d0
JK
13623 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13624 complaint (&symfile_complaints,
13625 _("debug info gives source %d included "
13626 "from %s at %s line %d"),
13627 file, at_commandline ? _("command-line") : _("file"),
13628 line == 0 ? _("zero") : _("non-zero"), line);
13629
13630 if (at_commandline)
13631 {
13632 /* This DW_MACINFO_start_file was executed in the pass one. */
13633 at_commandline = 0;
13634 }
13635 else
13636 current_file = macro_start_file (file, line,
13637 current_file, comp_dir,
13638 lh, cu->objfile);
2e276125
JB
13639 }
13640 break;
13641
13642 case DW_MACINFO_end_file:
13643 if (! current_file)
4d3c2250 13644 complaint (&symfile_complaints,
e2e0b3e5 13645 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
13646 else
13647 {
13648 current_file = current_file->included_by;
13649 if (! current_file)
13650 {
13651 enum dwarf_macinfo_record_type next_type;
13652
13653 /* GCC circa March 2002 doesn't produce the zero
13654 type byte marking the end of the compilation
13655 unit. Complain if it's not there, but exit no
13656 matter what. */
13657
13658 /* Do we at least have room for a macinfo type byte? */
13659 if (mac_ptr >= mac_end)
13660 {
4d3c2250 13661 dwarf2_macros_too_long_complaint ();
2e276125
JB
13662 return;
13663 }
13664
13665 /* We don't increment mac_ptr here, so this is just
13666 a look-ahead. */
13667 next_type = read_1_byte (abfd, mac_ptr);
13668 if (next_type != 0)
4d3c2250 13669 complaint (&symfile_complaints,
e2e0b3e5 13670 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
13671
13672 return;
13673 }
13674 }
13675 break;
13676
13677 case DW_MACINFO_vendor_ext:
13678 {
891d2f0b 13679 unsigned int bytes_read;
2e276125
JB
13680 int constant;
13681 char *string;
13682
13683 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13684 mac_ptr += bytes_read;
9b1c24c8 13685 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13686 mac_ptr += bytes_read;
13687
13688 /* We don't recognize any vendor extensions. */
13689 }
13690 break;
13691 }
757a13d0 13692 } while (macinfo_type != 0);
2e276125 13693}
8e19ed76
PS
13694
13695/* Check if the attribute's form is a DW_FORM_block*
13696 if so return true else false. */
13697static int
13698attr_form_is_block (struct attribute *attr)
13699{
13700 return (attr == NULL ? 0 :
13701 attr->form == DW_FORM_block1
13702 || attr->form == DW_FORM_block2
13703 || attr->form == DW_FORM_block4
2dc7f7b3
TT
13704 || attr->form == DW_FORM_block
13705 || attr->form == DW_FORM_exprloc);
8e19ed76 13706}
4c2df51b 13707
c6a0999f
JB
13708/* Return non-zero if ATTR's value is a section offset --- classes
13709 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
13710 You may use DW_UNSND (attr) to retrieve such offsets.
13711
13712 Section 7.5.4, "Attribute Encodings", explains that no attribute
13713 may have a value that belongs to more than one of these classes; it
13714 would be ambiguous if we did, because we use the same forms for all
13715 of them. */
3690dd37
JB
13716static int
13717attr_form_is_section_offset (struct attribute *attr)
13718{
13719 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
13720 || attr->form == DW_FORM_data8
13721 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
13722}
13723
13724
13725/* Return non-zero if ATTR's value falls in the 'constant' class, or
13726 zero otherwise. When this function returns true, you can apply
13727 dwarf2_get_attr_constant_value to it.
13728
13729 However, note that for some attributes you must check
13730 attr_form_is_section_offset before using this test. DW_FORM_data4
13731 and DW_FORM_data8 are members of both the constant class, and of
13732 the classes that contain offsets into other debug sections
13733 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
13734 that, if an attribute's can be either a constant or one of the
13735 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
13736 taken as section offsets, not constants. */
13737static int
13738attr_form_is_constant (struct attribute *attr)
13739{
13740 switch (attr->form)
13741 {
13742 case DW_FORM_sdata:
13743 case DW_FORM_udata:
13744 case DW_FORM_data1:
13745 case DW_FORM_data2:
13746 case DW_FORM_data4:
13747 case DW_FORM_data8:
13748 return 1;
13749 default:
13750 return 0;
13751 }
13752}
13753
4c2df51b
DJ
13754static void
13755dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 13756 struct dwarf2_cu *cu)
4c2df51b 13757{
3690dd37 13758 if (attr_form_is_section_offset (attr)
99bcc461
DJ
13759 /* ".debug_loc" may not exist at all, or the offset may be outside
13760 the section. If so, fall through to the complaint in the
13761 other branch. */
dce234bc 13762 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 13763 {
0d53c4c4 13764 struct dwarf2_loclist_baton *baton;
4c2df51b 13765
4a146b47 13766 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13767 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
13768 baton->per_cu = cu->per_cu;
13769 gdb_assert (baton->per_cu);
4c2df51b 13770
be391dca
TT
13771 dwarf2_read_section (dwarf2_per_objfile->objfile,
13772 &dwarf2_per_objfile->loc);
13773
0d53c4c4
DJ
13774 /* We don't know how long the location list is, but make sure we
13775 don't run off the edge of the section. */
dce234bc
PP
13776 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13777 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
13778 baton->base_address = cu->base_address;
13779 if (cu->base_known == 0)
0d53c4c4 13780 complaint (&symfile_complaints,
e2e0b3e5 13781 _("Location list used without specifying the CU base address."));
4c2df51b 13782
768a979c 13783 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
13784 SYMBOL_LOCATION_BATON (sym) = baton;
13785 }
13786 else
13787 {
13788 struct dwarf2_locexpr_baton *baton;
13789
4a146b47 13790 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13791 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
13792 baton->per_cu = cu->per_cu;
13793 gdb_assert (baton->per_cu);
0d53c4c4
DJ
13794
13795 if (attr_form_is_block (attr))
13796 {
13797 /* Note that we're just copying the block's data pointer
13798 here, not the actual data. We're still pointing into the
6502dd73
DJ
13799 info_buffer for SYM's objfile; right now we never release
13800 that buffer, but when we do clean up properly this may
13801 need to change. */
0d53c4c4
DJ
13802 baton->size = DW_BLOCK (attr)->size;
13803 baton->data = DW_BLOCK (attr)->data;
13804 }
13805 else
13806 {
13807 dwarf2_invalid_attrib_class_complaint ("location description",
13808 SYMBOL_NATURAL_NAME (sym));
13809 baton->size = 0;
13810 baton->data = NULL;
13811 }
6e70227d 13812
768a979c 13813 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
13814 SYMBOL_LOCATION_BATON (sym) = baton;
13815 }
4c2df51b 13816}
6502dd73 13817
9aa1f1e3
TT
13818/* Return the OBJFILE associated with the compilation unit CU. If CU
13819 came from a separate debuginfo file, then the master objfile is
13820 returned. */
ae0d2f24
UW
13821
13822struct objfile *
13823dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13824{
9291a0cd 13825 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13826
13827 /* Return the master objfile, so that we can report and look up the
13828 correct file containing this variable. */
13829 if (objfile->separate_debug_objfile_backlink)
13830 objfile = objfile->separate_debug_objfile_backlink;
13831
13832 return objfile;
13833}
13834
13835/* Return the address size given in the compilation unit header for CU. */
13836
13837CORE_ADDR
13838dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
13839{
13840 if (per_cu->cu)
13841 return per_cu->cu->header.addr_size;
13842 else
13843 {
13844 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 13845 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13846 struct dwarf2_per_objfile *per_objfile
13847 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 13848 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 13849 struct comp_unit_head cu_header;
9a619af0 13850
ae0d2f24
UW
13851 memset (&cu_header, 0, sizeof cu_header);
13852 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13853 return cu_header.addr_size;
13854 }
13855}
13856
9eae7c52
TT
13857/* Return the offset size given in the compilation unit header for CU. */
13858
13859int
13860dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
13861{
13862 if (per_cu->cu)
13863 return per_cu->cu->header.offset_size;
13864 else
13865 {
13866 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 13867 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
13868 struct dwarf2_per_objfile *per_objfile
13869 = objfile_data (objfile, dwarf2_objfile_data_key);
13870 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13871 struct comp_unit_head cu_header;
13872
13873 memset (&cu_header, 0, sizeof cu_header);
13874 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13875 return cu_header.offset_size;
13876 }
13877}
13878
9aa1f1e3
TT
13879/* Return the text offset of the CU. The returned offset comes from
13880 this CU's objfile. If this objfile came from a separate debuginfo
13881 file, then the offset may be different from the corresponding
13882 offset in the parent objfile. */
13883
13884CORE_ADDR
13885dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
13886{
bb3fa9d0 13887 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
13888
13889 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13890}
13891
348e048f
DE
13892/* Locate the .debug_info compilation unit from CU's objfile which contains
13893 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
13894
13895static struct dwarf2_per_cu_data *
c764a876 13896dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
13897 struct objfile *objfile)
13898{
13899 struct dwarf2_per_cu_data *this_cu;
13900 int low, high;
13901
ae038cb0
DJ
13902 low = 0;
13903 high = dwarf2_per_objfile->n_comp_units - 1;
13904 while (high > low)
13905 {
13906 int mid = low + (high - low) / 2;
9a619af0 13907
ae038cb0
DJ
13908 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
13909 high = mid;
13910 else
13911 low = mid + 1;
13912 }
13913 gdb_assert (low == high);
13914 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
13915 {
10b3939b 13916 if (low == 0)
8a3fe4f8
AC
13917 error (_("Dwarf Error: could not find partial DIE containing "
13918 "offset 0x%lx [in module %s]"),
10b3939b
DJ
13919 (long) offset, bfd_get_filename (objfile->obfd));
13920
ae038cb0
DJ
13921 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
13922 return dwarf2_per_objfile->all_comp_units[low-1];
13923 }
13924 else
13925 {
13926 this_cu = dwarf2_per_objfile->all_comp_units[low];
13927 if (low == dwarf2_per_objfile->n_comp_units - 1
13928 && offset >= this_cu->offset + this_cu->length)
c764a876 13929 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
13930 gdb_assert (offset < this_cu->offset + this_cu->length);
13931 return this_cu;
13932 }
13933}
13934
10b3939b
DJ
13935/* Locate the compilation unit from OBJFILE which is located at exactly
13936 OFFSET. Raises an error on failure. */
13937
ae038cb0 13938static struct dwarf2_per_cu_data *
c764a876 13939dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
13940{
13941 struct dwarf2_per_cu_data *this_cu;
9a619af0 13942
ae038cb0
DJ
13943 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
13944 if (this_cu->offset != offset)
c764a876 13945 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
13946 return this_cu;
13947}
13948
93311388
DE
13949/* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
13950
13951static struct dwarf2_cu *
13952alloc_one_comp_unit (struct objfile *objfile)
13953{
13954 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
13955 cu->objfile = objfile;
13956 obstack_init (&cu->comp_unit_obstack);
13957 return cu;
13958}
13959
ae038cb0
DJ
13960/* Release one cached compilation unit, CU. We unlink it from the tree
13961 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
13962 the caller is responsible for that.
13963 NOTE: DATA is a void * because this function is also used as a
13964 cleanup routine. */
ae038cb0
DJ
13965
13966static void
13967free_one_comp_unit (void *data)
13968{
13969 struct dwarf2_cu *cu = data;
13970
13971 if (cu->per_cu != NULL)
13972 cu->per_cu->cu = NULL;
13973 cu->per_cu = NULL;
13974
13975 obstack_free (&cu->comp_unit_obstack, NULL);
13976
13977 xfree (cu);
13978}
13979
72bf9492 13980/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
13981 when we're finished with it. We can't free the pointer itself, but be
13982 sure to unlink it from the cache. Also release any associated storage
13983 and perform cache maintenance.
72bf9492
DJ
13984
13985 Only used during partial symbol parsing. */
13986
13987static void
13988free_stack_comp_unit (void *data)
13989{
13990 struct dwarf2_cu *cu = data;
13991
13992 obstack_free (&cu->comp_unit_obstack, NULL);
13993 cu->partial_dies = NULL;
ae038cb0
DJ
13994
13995 if (cu->per_cu != NULL)
13996 {
13997 /* This compilation unit is on the stack in our caller, so we
13998 should not xfree it. Just unlink it. */
13999 cu->per_cu->cu = NULL;
14000 cu->per_cu = NULL;
14001
14002 /* If we had a per-cu pointer, then we may have other compilation
14003 units loaded, so age them now. */
14004 age_cached_comp_units ();
14005 }
14006}
14007
14008/* Free all cached compilation units. */
14009
14010static void
14011free_cached_comp_units (void *data)
14012{
14013 struct dwarf2_per_cu_data *per_cu, **last_chain;
14014
14015 per_cu = dwarf2_per_objfile->read_in_chain;
14016 last_chain = &dwarf2_per_objfile->read_in_chain;
14017 while (per_cu != NULL)
14018 {
14019 struct dwarf2_per_cu_data *next_cu;
14020
14021 next_cu = per_cu->cu->read_in_chain;
14022
14023 free_one_comp_unit (per_cu->cu);
14024 *last_chain = next_cu;
14025
14026 per_cu = next_cu;
14027 }
14028}
14029
14030/* Increase the age counter on each cached compilation unit, and free
14031 any that are too old. */
14032
14033static void
14034age_cached_comp_units (void)
14035{
14036 struct dwarf2_per_cu_data *per_cu, **last_chain;
14037
14038 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14039 per_cu = dwarf2_per_objfile->read_in_chain;
14040 while (per_cu != NULL)
14041 {
14042 per_cu->cu->last_used ++;
14043 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14044 dwarf2_mark (per_cu->cu);
14045 per_cu = per_cu->cu->read_in_chain;
14046 }
14047
14048 per_cu = dwarf2_per_objfile->read_in_chain;
14049 last_chain = &dwarf2_per_objfile->read_in_chain;
14050 while (per_cu != NULL)
14051 {
14052 struct dwarf2_per_cu_data *next_cu;
14053
14054 next_cu = per_cu->cu->read_in_chain;
14055
14056 if (!per_cu->cu->mark)
14057 {
14058 free_one_comp_unit (per_cu->cu);
14059 *last_chain = next_cu;
14060 }
14061 else
14062 last_chain = &per_cu->cu->read_in_chain;
14063
14064 per_cu = next_cu;
14065 }
14066}
14067
14068/* Remove a single compilation unit from the cache. */
14069
14070static void
14071free_one_cached_comp_unit (void *target_cu)
14072{
14073 struct dwarf2_per_cu_data *per_cu, **last_chain;
14074
14075 per_cu = dwarf2_per_objfile->read_in_chain;
14076 last_chain = &dwarf2_per_objfile->read_in_chain;
14077 while (per_cu != NULL)
14078 {
14079 struct dwarf2_per_cu_data *next_cu;
14080
14081 next_cu = per_cu->cu->read_in_chain;
14082
14083 if (per_cu->cu == target_cu)
14084 {
14085 free_one_comp_unit (per_cu->cu);
14086 *last_chain = next_cu;
14087 break;
14088 }
14089 else
14090 last_chain = &per_cu->cu->read_in_chain;
14091
14092 per_cu = next_cu;
14093 }
14094}
14095
fe3e1990
DJ
14096/* Release all extra memory associated with OBJFILE. */
14097
14098void
14099dwarf2_free_objfile (struct objfile *objfile)
14100{
14101 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14102
14103 if (dwarf2_per_objfile == NULL)
14104 return;
14105
14106 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14107 free_cached_comp_units (NULL);
14108
9291a0cd
TT
14109 if (dwarf2_per_objfile->using_index)
14110 {
14111 int i;
14112
14113 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14114 {
14115 int j;
14116 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14117
14118 if (!cu->v.quick->lines)
14119 continue;
14120
14121 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
14122 {
14123 if (cu->v.quick->file_names)
14124 xfree ((void *) cu->v.quick->file_names[j]);
14125 if (cu->v.quick->full_names)
14126 xfree ((void *) cu->v.quick->full_names[j]);
14127 }
14128
14129 free_line_header (cu->v.quick->lines);
14130 }
14131 }
14132
fe3e1990
DJ
14133 /* Everything else should be on the objfile obstack. */
14134}
14135
1c379e20
DJ
14136/* A pair of DIE offset and GDB type pointer. We store these
14137 in a hash table separate from the DIEs, and preserve them
14138 when the DIEs are flushed out of cache. */
14139
14140struct dwarf2_offset_and_type
14141{
14142 unsigned int offset;
14143 struct type *type;
14144};
14145
14146/* Hash function for a dwarf2_offset_and_type. */
14147
14148static hashval_t
14149offset_and_type_hash (const void *item)
14150{
14151 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14152
1c379e20
DJ
14153 return ofs->offset;
14154}
14155
14156/* Equality function for a dwarf2_offset_and_type. */
14157
14158static int
14159offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14160{
14161 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14162 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14163
1c379e20
DJ
14164 return ofs_lhs->offset == ofs_rhs->offset;
14165}
14166
14167/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14168 table if necessary. For convenience, return TYPE.
14169
14170 The DIEs reading must have careful ordering to:
14171 * Not cause infite loops trying to read in DIEs as a prerequisite for
14172 reading current DIE.
14173 * Not trying to dereference contents of still incompletely read in types
14174 while reading in other DIEs.
14175 * Enable referencing still incompletely read in types just by a pointer to
14176 the type without accessing its fields.
14177
14178 Therefore caller should follow these rules:
14179 * Try to fetch any prerequisite types we may need to build this DIE type
14180 before building the type and calling set_die_type.
14181 * After building typer call set_die_type for current DIE as soon as
14182 possible before fetching more types to complete the current type.
14183 * Make the type as complete as possible before fetching more types. */
1c379e20 14184
f792889a 14185static struct type *
1c379e20
DJ
14186set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14187{
14188 struct dwarf2_offset_and_type **slot, ofs;
14189
b4ba55a1
JB
14190 /* For Ada types, make sure that the gnat-specific data is always
14191 initialized (if not already set). There are a few types where
14192 we should not be doing so, because the type-specific area is
14193 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14194 where the type-specific area is used to store the floatformat).
14195 But this is not a problem, because the gnat-specific information
14196 is actually not needed for these types. */
14197 if (need_gnat_info (cu)
14198 && TYPE_CODE (type) != TYPE_CODE_FUNC
14199 && TYPE_CODE (type) != TYPE_CODE_FLT
14200 && !HAVE_GNAT_AUX_INFO (type))
14201 INIT_GNAT_SPECIFIC (type);
14202
f792889a
DJ
14203 if (cu->type_hash == NULL)
14204 {
14205 gdb_assert (cu->per_cu != NULL);
14206 cu->per_cu->type_hash
14207 = htab_create_alloc_ex (cu->header.length / 24,
14208 offset_and_type_hash,
14209 offset_and_type_eq,
14210 NULL,
14211 &cu->objfile->objfile_obstack,
14212 hashtab_obstack_allocate,
14213 dummy_obstack_deallocate);
14214 cu->type_hash = cu->per_cu->type_hash;
14215 }
1c379e20
DJ
14216
14217 ofs.offset = die->offset;
14218 ofs.type = type;
14219 slot = (struct dwarf2_offset_and_type **)
f792889a 14220 htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
7e314c57
JK
14221 if (*slot)
14222 complaint (&symfile_complaints,
14223 _("A problem internal to GDB: DIE 0x%x has type already set"),
14224 die->offset);
1c379e20
DJ
14225 *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
14226 **slot = ofs;
f792889a 14227 return type;
1c379e20
DJ
14228}
14229
f792889a
DJ
14230/* Find the type for DIE in CU's type_hash, or return NULL if DIE does
14231 not have a saved type. */
1c379e20
DJ
14232
14233static struct type *
f792889a 14234get_die_type (struct die_info *die, struct dwarf2_cu *cu)
1c379e20
DJ
14235{
14236 struct dwarf2_offset_and_type *slot, ofs;
f792889a
DJ
14237 htab_t type_hash = cu->type_hash;
14238
14239 if (type_hash == NULL)
14240 return NULL;
1c379e20
DJ
14241
14242 ofs.offset = die->offset;
14243 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14244 if (slot)
14245 return slot->type;
14246 else
14247 return NULL;
14248}
14249
10b3939b
DJ
14250/* Add a dependence relationship from CU to REF_PER_CU. */
14251
14252static void
14253dwarf2_add_dependence (struct dwarf2_cu *cu,
14254 struct dwarf2_per_cu_data *ref_per_cu)
14255{
14256 void **slot;
14257
14258 if (cu->dependencies == NULL)
14259 cu->dependencies
14260 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14261 NULL, &cu->comp_unit_obstack,
14262 hashtab_obstack_allocate,
14263 dummy_obstack_deallocate);
14264
14265 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14266 if (*slot == NULL)
14267 *slot = ref_per_cu;
14268}
1c379e20 14269
f504f079
DE
14270/* Subroutine of dwarf2_mark to pass to htab_traverse.
14271 Set the mark field in every compilation unit in the
ae038cb0
DJ
14272 cache that we must keep because we are keeping CU. */
14273
10b3939b
DJ
14274static int
14275dwarf2_mark_helper (void **slot, void *data)
14276{
14277 struct dwarf2_per_cu_data *per_cu;
14278
14279 per_cu = (struct dwarf2_per_cu_data *) *slot;
14280 if (per_cu->cu->mark)
14281 return 1;
14282 per_cu->cu->mark = 1;
14283
14284 if (per_cu->cu->dependencies != NULL)
14285 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14286
14287 return 1;
14288}
14289
f504f079
DE
14290/* Set the mark field in CU and in every other compilation unit in the
14291 cache that we must keep because we are keeping CU. */
14292
ae038cb0
DJ
14293static void
14294dwarf2_mark (struct dwarf2_cu *cu)
14295{
14296 if (cu->mark)
14297 return;
14298 cu->mark = 1;
10b3939b
DJ
14299 if (cu->dependencies != NULL)
14300 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14301}
14302
14303static void
14304dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14305{
14306 while (per_cu)
14307 {
14308 per_cu->cu->mark = 0;
14309 per_cu = per_cu->cu->read_in_chain;
14310 }
72bf9492
DJ
14311}
14312
72bf9492
DJ
14313/* Trivial hash function for partial_die_info: the hash value of a DIE
14314 is its offset in .debug_info for this objfile. */
14315
14316static hashval_t
14317partial_die_hash (const void *item)
14318{
14319 const struct partial_die_info *part_die = item;
9a619af0 14320
72bf9492
DJ
14321 return part_die->offset;
14322}
14323
14324/* Trivial comparison function for partial_die_info structures: two DIEs
14325 are equal if they have the same offset. */
14326
14327static int
14328partial_die_eq (const void *item_lhs, const void *item_rhs)
14329{
14330 const struct partial_die_info *part_die_lhs = item_lhs;
14331 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14332
72bf9492
DJ
14333 return part_die_lhs->offset == part_die_rhs->offset;
14334}
14335
ae038cb0
DJ
14336static struct cmd_list_element *set_dwarf2_cmdlist;
14337static struct cmd_list_element *show_dwarf2_cmdlist;
14338
14339static void
14340set_dwarf2_cmd (char *args, int from_tty)
14341{
14342 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14343}
14344
14345static void
14346show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14347{
ae038cb0
DJ
14348 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14349}
14350
dce234bc
PP
14351/* If section described by INFO was mmapped, munmap it now. */
14352
14353static void
14354munmap_section_buffer (struct dwarf2_section_info *info)
14355{
14356 if (info->was_mmapped)
14357 {
14358#ifdef HAVE_MMAP
14359 intptr_t begin = (intptr_t) info->buffer;
14360 intptr_t map_begin = begin & ~(pagesize - 1);
14361 size_t map_length = info->size + begin - map_begin;
9a619af0 14362
dce234bc
PP
14363 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14364#else
14365 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 14366 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
14367#endif
14368 }
14369}
14370
14371/* munmap debug sections for OBJFILE, if necessary. */
14372
14373static void
c1bd65d0 14374dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
14375{
14376 struct dwarf2_per_objfile *data = d;
9a619af0 14377
dce234bc
PP
14378 munmap_section_buffer (&data->info);
14379 munmap_section_buffer (&data->abbrev);
14380 munmap_section_buffer (&data->line);
14381 munmap_section_buffer (&data->str);
14382 munmap_section_buffer (&data->macinfo);
14383 munmap_section_buffer (&data->ranges);
14384 munmap_section_buffer (&data->loc);
14385 munmap_section_buffer (&data->frame);
14386 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
14387 munmap_section_buffer (&data->gdb_index);
14388}
14389
14390\f
14391
14392/* The contents of the hash table we create when building the string
14393 table. */
14394struct strtab_entry
14395{
14396 offset_type offset;
14397 const char *str;
14398};
14399
14400/* Hash function for a strtab_entry. */
14401static hashval_t
14402hash_strtab_entry (const void *e)
14403{
14404 const struct strtab_entry *entry = e;
14405 return mapped_index_string_hash (entry->str);
14406}
14407
14408/* Equality function for a strtab_entry. */
14409static int
14410eq_strtab_entry (const void *a, const void *b)
14411{
14412 const struct strtab_entry *ea = a;
14413 const struct strtab_entry *eb = b;
14414 return !strcmp (ea->str, eb->str);
14415}
14416
14417/* Create a strtab_entry hash table. */
14418static htab_t
14419create_strtab (void)
14420{
14421 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14422 xfree, xcalloc, xfree);
14423}
14424
14425/* Add a string to the constant pool. Return the string's offset in
14426 host order. */
14427static offset_type
14428add_string (htab_t table, struct obstack *cpool, const char *str)
14429{
14430 void **slot;
14431 struct strtab_entry entry;
14432 struct strtab_entry *result;
14433
14434 entry.str = str;
14435 slot = htab_find_slot (table, &entry, INSERT);
14436 if (*slot)
14437 result = *slot;
14438 else
14439 {
14440 result = XNEW (struct strtab_entry);
14441 result->offset = obstack_object_size (cpool);
14442 result->str = str;
14443 obstack_grow_str0 (cpool, str);
14444 *slot = result;
14445 }
14446 return result->offset;
14447}
14448
14449/* An entry in the symbol table. */
14450struct symtab_index_entry
14451{
14452 /* The name of the symbol. */
14453 const char *name;
14454 /* The offset of the name in the constant pool. */
14455 offset_type index_offset;
14456 /* A sorted vector of the indices of all the CUs that hold an object
14457 of this name. */
14458 VEC (offset_type) *cu_indices;
14459};
14460
14461/* The symbol table. This is a power-of-2-sized hash table. */
14462struct mapped_symtab
14463{
14464 offset_type n_elements;
14465 offset_type size;
14466 struct symtab_index_entry **data;
14467};
14468
14469/* Hash function for a symtab_index_entry. */
14470static hashval_t
14471hash_symtab_entry (const void *e)
14472{
14473 const struct symtab_index_entry *entry = e;
14474 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14475 sizeof (offset_type) * VEC_length (offset_type,
14476 entry->cu_indices),
14477 0);
14478}
14479
14480/* Equality function for a symtab_index_entry. */
14481static int
14482eq_symtab_entry (const void *a, const void *b)
14483{
14484 const struct symtab_index_entry *ea = a;
14485 const struct symtab_index_entry *eb = b;
14486 int len = VEC_length (offset_type, ea->cu_indices);
14487 if (len != VEC_length (offset_type, eb->cu_indices))
14488 return 0;
14489 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14490 VEC_address (offset_type, eb->cu_indices),
14491 sizeof (offset_type) * len);
14492}
14493
14494/* Destroy a symtab_index_entry. */
14495static void
14496delete_symtab_entry (void *p)
14497{
14498 struct symtab_index_entry *entry = p;
14499 VEC_free (offset_type, entry->cu_indices);
14500 xfree (entry);
14501}
14502
14503/* Create a hash table holding symtab_index_entry objects. */
14504static htab_t
14505create_index_table (void)
14506{
14507 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14508 delete_symtab_entry, xcalloc, xfree);
14509}
14510
14511/* Create a new mapped symtab object. */
14512static struct mapped_symtab *
14513create_mapped_symtab (void)
14514{
14515 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14516 symtab->n_elements = 0;
14517 symtab->size = 1024;
14518 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14519 return symtab;
14520}
14521
14522/* Destroy a mapped_symtab. */
14523static void
14524cleanup_mapped_symtab (void *p)
14525{
14526 struct mapped_symtab *symtab = p;
14527 /* The contents of the array are freed when the other hash table is
14528 destroyed. */
14529 xfree (symtab->data);
14530 xfree (symtab);
14531}
14532
14533/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14534 the slot. */
14535static struct symtab_index_entry **
14536find_slot (struct mapped_symtab *symtab, const char *name)
14537{
14538 offset_type index, step, hash = mapped_index_string_hash (name);
14539
14540 index = hash & (symtab->size - 1);
14541 step = ((hash * 17) & (symtab->size - 1)) | 1;
14542
14543 for (;;)
14544 {
14545 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14546 return &symtab->data[index];
14547 index = (index + step) & (symtab->size - 1);
14548 }
14549}
14550
14551/* Expand SYMTAB's hash table. */
14552static void
14553hash_expand (struct mapped_symtab *symtab)
14554{
14555 offset_type old_size = symtab->size;
14556 offset_type i;
14557 struct symtab_index_entry **old_entries = symtab->data;
14558
14559 symtab->size *= 2;
14560 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14561
14562 for (i = 0; i < old_size; ++i)
14563 {
14564 if (old_entries[i])
14565 {
14566 struct symtab_index_entry **slot = find_slot (symtab,
14567 old_entries[i]->name);
14568 *slot = old_entries[i];
14569 }
14570 }
14571
14572 xfree (old_entries);
14573}
14574
14575/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
14576 is the index of the CU in which the symbol appears. */
14577static void
14578add_index_entry (struct mapped_symtab *symtab, const char *name,
14579 offset_type cu_index)
14580{
14581 struct symtab_index_entry **slot;
14582
14583 ++symtab->n_elements;
14584 if (4 * symtab->n_elements / 3 >= symtab->size)
14585 hash_expand (symtab);
14586
14587 slot = find_slot (symtab, name);
14588 if (!*slot)
14589 {
14590 *slot = XNEW (struct symtab_index_entry);
14591 (*slot)->name = name;
14592 (*slot)->cu_indices = NULL;
14593 }
14594 /* Don't push an index twice. Due to how we add entries we only
14595 have to check the last one. */
14596 if (VEC_empty (offset_type, (*slot)->cu_indices)
14597 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14598 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14599}
14600
14601/* Add a vector of indices to the constant pool. */
14602static offset_type
14603add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14604 struct symtab_index_entry *entry)
14605{
14606 void **slot;
14607
14608 slot = htab_find_slot (index_table, entry, INSERT);
14609 if (!*slot)
14610 {
14611 offset_type len = VEC_length (offset_type, entry->cu_indices);
14612 offset_type val = MAYBE_SWAP (len);
14613 offset_type iter;
14614 int i;
14615
14616 *slot = entry;
14617 entry->index_offset = obstack_object_size (cpool);
14618
14619 obstack_grow (cpool, &val, sizeof (val));
14620 for (i = 0;
14621 VEC_iterate (offset_type, entry->cu_indices, i, iter);
14622 ++i)
14623 {
14624 val = MAYBE_SWAP (iter);
14625 obstack_grow (cpool, &val, sizeof (val));
14626 }
14627 }
14628 else
14629 {
14630 struct symtab_index_entry *old_entry = *slot;
14631 entry->index_offset = old_entry->index_offset;
14632 entry = old_entry;
14633 }
14634 return entry->index_offset;
14635}
14636
14637/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14638 constant pool entries going into the obstack CPOOL. */
14639static void
14640write_hash_table (struct mapped_symtab *symtab,
14641 struct obstack *output, struct obstack *cpool)
14642{
14643 offset_type i;
14644 htab_t index_table;
14645 htab_t str_table;
14646
14647 index_table = create_index_table ();
14648 str_table = create_strtab ();
14649 /* We add all the index vectors to the constant pool first, to
14650 ensure alignment is ok. */
14651 for (i = 0; i < symtab->size; ++i)
14652 {
14653 if (symtab->data[i])
14654 add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14655 }
14656
14657 /* Now write out the hash table. */
14658 for (i = 0; i < symtab->size; ++i)
14659 {
14660 offset_type str_off, vec_off;
14661
14662 if (symtab->data[i])
14663 {
14664 str_off = add_string (str_table, cpool, symtab->data[i]->name);
14665 vec_off = symtab->data[i]->index_offset;
14666 }
14667 else
14668 {
14669 /* While 0 is a valid constant pool index, it is not valid
14670 to have 0 for both offsets. */
14671 str_off = 0;
14672 vec_off = 0;
14673 }
14674
14675 str_off = MAYBE_SWAP (str_off);
14676 vec_off = MAYBE_SWAP (vec_off);
14677
14678 obstack_grow (output, &str_off, sizeof (str_off));
14679 obstack_grow (output, &vec_off, sizeof (vec_off));
14680 }
14681
14682 htab_delete (str_table);
14683 htab_delete (index_table);
14684}
14685
14686/* Write an address entry to ADDR_OBSTACK. The addresses are taken
14687 from PST; CU_INDEX is the index of the CU in the vector of all
14688 CUs. */
14689static void
14690add_address_entry (struct objfile *objfile,
14691 struct obstack *addr_obstack, struct partial_symtab *pst,
14692 unsigned int cu_index)
14693{
14694 offset_type offset;
14695 char addr[8];
14696 CORE_ADDR baseaddr;
14697
1fd400ff
TT
14698 /* Don't bother recording empty ranges. */
14699 if (pst->textlow == pst->texthigh)
14700 return;
14701
9291a0cd
TT
14702 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14703
14704 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
14705 obstack_grow (addr_obstack, addr, 8);
14706 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
14707 obstack_grow (addr_obstack, addr, 8);
14708 offset = MAYBE_SWAP (cu_index);
14709 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
14710}
14711
14712/* Add a list of partial symbols to SYMTAB. */
14713static void
14714write_psymbols (struct mapped_symtab *symtab,
14715 struct partial_symbol **psymp,
14716 int count,
14717 offset_type cu_index)
14718{
14719 for (; count-- > 0; ++psymp)
14720 {
14721 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
14722 error (_("Ada is not currently supported by the index"));
14723 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
14724 }
14725}
14726
14727/* Write the contents of an ("unfinished") obstack to FILE. Throw an
14728 exception if there is an error. */
14729static void
14730write_obstack (FILE *file, struct obstack *obstack)
14731{
14732 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
14733 file)
14734 != obstack_object_size (obstack))
14735 error (_("couldn't data write to file"));
14736}
14737
14738/* Unlink a file if the argument is not NULL. */
14739static void
14740unlink_if_set (void *p)
14741{
14742 char **filename = p;
14743 if (*filename)
14744 unlink (*filename);
14745}
14746
1fd400ff
TT
14747/* A helper struct used when iterating over debug_types. */
14748struct signatured_type_index_data
14749{
14750 struct objfile *objfile;
14751 struct mapped_symtab *symtab;
14752 struct obstack *types_list;
14753 int cu_index;
14754};
14755
14756/* A helper function that writes a single signatured_type to an
14757 obstack. */
14758static int
14759write_one_signatured_type (void **slot, void *d)
14760{
14761 struct signatured_type_index_data *info = d;
14762 struct signatured_type *entry = (struct signatured_type *) *slot;
14763 struct dwarf2_per_cu_data *cu = &entry->per_cu;
14764 struct partial_symtab *psymtab = cu->v.psymtab;
14765 gdb_byte val[8];
14766
14767 write_psymbols (info->symtab,
14768 info->objfile->global_psymbols.list + psymtab->globals_offset,
14769 psymtab->n_global_syms, info->cu_index);
14770 write_psymbols (info->symtab,
14771 info->objfile->static_psymbols.list + psymtab->statics_offset,
14772 psymtab->n_static_syms, info->cu_index);
14773
14774 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
14775 obstack_grow (info->types_list, val, 8);
14776 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
14777 obstack_grow (info->types_list, val, 8);
14778 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
14779 obstack_grow (info->types_list, val, 8);
14780
14781 ++info->cu_index;
14782
14783 return 1;
14784}
14785
9291a0cd
TT
14786/* Create an index file for OBJFILE in the directory DIR. */
14787static void
14788write_psymtabs_to_index (struct objfile *objfile, const char *dir)
14789{
14790 struct cleanup *cleanup;
14791 char *filename, *cleanup_filename;
1fd400ff
TT
14792 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
14793 struct obstack cu_list, types_cu_list;
9291a0cd
TT
14794 int i;
14795 FILE *out_file;
14796 struct mapped_symtab *symtab;
14797 offset_type val, size_of_contents, total_len;
14798 struct stat st;
14799 char buf[8];
14800
14801 if (!objfile->psymtabs)
14802 return;
14803 if (dwarf2_per_objfile->using_index)
14804 error (_("Cannot use an index to create the index"));
14805
14806 if (stat (objfile->name, &st) < 0)
14807 perror_with_name (_("Could not stat"));
14808
14809 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
14810 INDEX_SUFFIX, (char *) NULL);
14811 cleanup = make_cleanup (xfree, filename);
14812
14813 out_file = fopen (filename, "wb");
14814 if (!out_file)
14815 error (_("Can't open `%s' for writing"), filename);
14816
14817 cleanup_filename = filename;
14818 make_cleanup (unlink_if_set, &cleanup_filename);
14819
14820 symtab = create_mapped_symtab ();
14821 make_cleanup (cleanup_mapped_symtab, symtab);
14822
14823 obstack_init (&addr_obstack);
14824 make_cleanup_obstack_free (&addr_obstack);
14825
14826 obstack_init (&cu_list);
14827 make_cleanup_obstack_free (&cu_list);
14828
1fd400ff
TT
14829 obstack_init (&types_cu_list);
14830 make_cleanup_obstack_free (&types_cu_list);
14831
14832 /* The list is already sorted, so we don't need to do additional
14833 work here. Also, the debug_types entries do not appear in
14834 all_comp_units, but only in their own hash table. */
9291a0cd
TT
14835 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14836 {
14837 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14838 struct partial_symtab *psymtab = cu->v.psymtab;
14839 gdb_byte val[8];
14840
14841 write_psymbols (symtab,
14842 objfile->global_psymbols.list + psymtab->globals_offset,
14843 psymtab->n_global_syms, i);
14844 write_psymbols (symtab,
14845 objfile->static_psymbols.list + psymtab->statics_offset,
14846 psymtab->n_static_syms, i);
14847
14848 add_address_entry (objfile, &addr_obstack, psymtab, i);
14849
14850 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->offset);
14851 obstack_grow (&cu_list, val, 8);
14852 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->length);
14853 obstack_grow (&cu_list, val, 8);
14854 }
14855
1fd400ff
TT
14856 /* Write out the .debug_type entries, if any. */
14857 if (dwarf2_per_objfile->signatured_types)
14858 {
14859 struct signatured_type_index_data sig_data;
14860
14861 sig_data.objfile = objfile;
14862 sig_data.symtab = symtab;
14863 sig_data.types_list = &types_cu_list;
14864 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
14865 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
14866 write_one_signatured_type, &sig_data);
14867 }
14868
9291a0cd
TT
14869 obstack_init (&constant_pool);
14870 make_cleanup_obstack_free (&constant_pool);
14871 obstack_init (&symtab_obstack);
14872 make_cleanup_obstack_free (&symtab_obstack);
14873 write_hash_table (symtab, &symtab_obstack, &constant_pool);
14874
14875 obstack_init (&contents);
14876 make_cleanup_obstack_free (&contents);
1fd400ff 14877 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
14878 total_len = size_of_contents;
14879
14880 /* The version number. */
1fd400ff 14881 val = MAYBE_SWAP (2);
9291a0cd
TT
14882 obstack_grow (&contents, &val, sizeof (val));
14883
14884 /* The offset of the CU list from the start of the file. */
14885 val = MAYBE_SWAP (total_len);
14886 obstack_grow (&contents, &val, sizeof (val));
14887 total_len += obstack_object_size (&cu_list);
14888
1fd400ff
TT
14889 /* The offset of the types CU list from the start of the file. */
14890 val = MAYBE_SWAP (total_len);
14891 obstack_grow (&contents, &val, sizeof (val));
14892 total_len += obstack_object_size (&types_cu_list);
14893
9291a0cd
TT
14894 /* The offset of the address table from the start of the file. */
14895 val = MAYBE_SWAP (total_len);
14896 obstack_grow (&contents, &val, sizeof (val));
14897 total_len += obstack_object_size (&addr_obstack);
14898
14899 /* The offset of the symbol table from the start of the file. */
14900 val = MAYBE_SWAP (total_len);
14901 obstack_grow (&contents, &val, sizeof (val));
14902 total_len += obstack_object_size (&symtab_obstack);
14903
14904 /* The offset of the constant pool from the start of the file. */
14905 val = MAYBE_SWAP (total_len);
14906 obstack_grow (&contents, &val, sizeof (val));
14907 total_len += obstack_object_size (&constant_pool);
14908
14909 gdb_assert (obstack_object_size (&contents) == size_of_contents);
14910
14911 write_obstack (out_file, &contents);
14912 write_obstack (out_file, &cu_list);
1fd400ff 14913 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
14914 write_obstack (out_file, &addr_obstack);
14915 write_obstack (out_file, &symtab_obstack);
14916 write_obstack (out_file, &constant_pool);
14917
14918 fclose (out_file);
14919
14920 /* We want to keep the file, so we set cleanup_filename to NULL
14921 here. See unlink_if_set. */
14922 cleanup_filename = NULL;
14923
14924 do_cleanups (cleanup);
14925}
14926
14927/* The mapped index file format is designed to be directly mmap()able
14928 on any architecture. In most cases, a datum is represented using a
14929 little-endian 32-bit integer value, called an offset_type. Big
14930 endian machines must byte-swap the values before using them.
14931 Exceptions to this rule are noted. The data is laid out such that
14932 alignment is always respected.
14933
14934 A mapped index consists of several sections.
14935
14936 1. The file header. This is a sequence of values, of offset_type
14937 unless otherwise noted:
1fd400ff
TT
14938 [0] The version number. Currently 1 or 2. The differences are
14939 noted below. Version 1 did not account for .debug_types sections;
14940 the presence of a .debug_types section invalidates any version 1
14941 index that may exist.
9291a0cd 14942 [1] The offset, from the start of the file, of the CU list.
1fd400ff
TT
14943 [1.5] In version 2, the offset, from the start of the file, of the
14944 types CU list. This offset does not appear in version 1. Note
14945 that this can be empty, in which case this offset will be equal to
14946 the next offset.
9291a0cd
TT
14947 [2] The offset, from the start of the file, of the address section.
14948 [3] The offset, from the start of the file, of the symbol table.
14949 [4] The offset, from the start of the file, of the constant pool.
14950
14951 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
14952 little-endian values, sorted by the CU offset. The first element
14953 in each pair is the offset of a CU in the .debug_info section. The
14954 second element in each pair is the length of that CU. References
14955 to a CU elsewhere in the map are done using a CU index, which is
14956 just the 0-based index into this table. Note that if there are
14957 type CUs, then conceptually CUs and type CUs form a single list for
14958 the purposes of CU indices.
14959
14960 2.5 The types CU list. This does not appear in a version 1 index.
14961 This is a sequence of triplets of 64-bit little-endian values. In
14962 a triplet, the first value is the CU offset, the second value is
14963 the type offset in the CU, and the third value is the type
14964 signature. The types CU list is not sorted.
9291a0cd
TT
14965
14966 3. The address section. The address section consists of a sequence
14967 of address entries. Each address entry has three elements.
14968 [0] The low address. This is a 64-bit little-endian value.
14969 [1] The high address. This is a 64-bit little-endian value.
14970 [2] The CU index. This is an offset_type value.
14971
14972 4. The symbol table. This is a hash table. The size of the hash
14973 table is always a power of 2. The initial hash and the step are
14974 currently defined by the `find_slot' function.
14975
14976 Each slot in the hash table consists of a pair of offset_type
14977 values. The first value is the offset of the symbol's name in the
14978 constant pool. The second value is the offset of the CU vector in
14979 the constant pool.
14980
14981 If both values are 0, then this slot in the hash table is empty.
14982 This is ok because while 0 is a valid constant pool index, it
14983 cannot be a valid index for both a string and a CU vector.
14984
14985 A string in the constant pool is stored as a \0-terminated string,
14986 as you'd expect.
14987
14988 A CU vector in the constant pool is a sequence of offset_type
14989 values. The first value is the number of CU indices in the vector.
14990 Each subsequent value is the index of a CU in the CU list. This
14991 element in the hash table is used to indicate which CUs define the
14992 symbol.
14993
14994 5. The constant pool. This is simply a bunch of bytes. It is
14995 organized so that alignment is correct: CU vectors are stored
14996 first, followed by strings. */
14997static void
14998save_gdb_index_command (char *arg, int from_tty)
14999{
15000 struct objfile *objfile;
15001
15002 if (!arg || !*arg)
96d19272 15003 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15004
15005 ALL_OBJFILES (objfile)
15006 {
15007 struct stat st;
15008
15009 /* If the objfile does not correspond to an actual file, skip it. */
15010 if (stat (objfile->name, &st) < 0)
15011 continue;
15012
15013 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15014 if (dwarf2_per_objfile)
15015 {
15016 volatile struct gdb_exception except;
15017
15018 TRY_CATCH (except, RETURN_MASK_ERROR)
15019 {
15020 write_psymtabs_to_index (objfile, arg);
15021 }
15022 if (except.reason < 0)
15023 exception_fprintf (gdb_stderr, except,
15024 _("Error while writing index for `%s': "),
15025 objfile->name);
15026 }
15027 }
dce234bc
PP
15028}
15029
9291a0cd
TT
15030\f
15031
9eae7c52
TT
15032int dwarf2_always_disassemble;
15033
15034static void
15035show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15036 struct cmd_list_element *c, const char *value)
15037{
15038 fprintf_filtered (file, _("\
15039Whether to always disassemble DWARF expressions is %s.\n"),
15040 value);
15041}
15042
6502dd73
DJ
15043void _initialize_dwarf2_read (void);
15044
15045void
15046_initialize_dwarf2_read (void)
15047{
96d19272
JK
15048 struct cmd_list_element *c;
15049
dce234bc 15050 dwarf2_objfile_data_key
c1bd65d0 15051 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15052
1bedd215
AC
15053 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15054Set DWARF 2 specific variables.\n\
15055Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15056 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15057 0/*allow-unknown*/, &maintenance_set_cmdlist);
15058
1bedd215
AC
15059 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15060Show DWARF 2 specific variables\n\
15061Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15062 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15063 0/*allow-unknown*/, &maintenance_show_cmdlist);
15064
15065 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15066 &dwarf2_max_cache_age, _("\
15067Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15068Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15069A higher limit means that cached compilation units will be stored\n\
15070in memory longer, and more total memory will be used. Zero disables\n\
15071caching, which can slow down startup."),
2c5b56ce 15072 NULL,
920d2a44 15073 show_dwarf2_max_cache_age,
2c5b56ce 15074 &set_dwarf2_cmdlist,
ae038cb0 15075 &show_dwarf2_cmdlist);
d97bc12b 15076
9eae7c52
TT
15077 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15078 &dwarf2_always_disassemble, _("\
15079Set whether `info address' always disassembles DWARF expressions."), _("\
15080Show whether `info address' always disassembles DWARF expressions."), _("\
15081When enabled, DWARF expressions are always printed in an assembly-like\n\
15082syntax. When disabled, expressions will be printed in a more\n\
15083conversational style, when possible."),
15084 NULL,
15085 show_dwarf2_always_disassemble,
15086 &set_dwarf2_cmdlist,
15087 &show_dwarf2_cmdlist);
15088
d97bc12b
DE
15089 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15090Set debugging of the dwarf2 DIE reader."), _("\
15091Show debugging of the dwarf2 DIE reader."), _("\
15092When enabled (non-zero), DIEs are dumped after they are read in.\n\
15093The value is the maximum depth to print."),
15094 NULL,
15095 NULL,
15096 &setdebuglist, &showdebuglist);
9291a0cd 15097
96d19272
JK
15098 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15099 _("Save a .gdb-index file"),
15100 &save_cmdlist);
15101 set_cmd_completer (c, filename_completer);
6502dd73 15102}
This page took 2.102941 seconds and 4 git commands to generate.