* MAINTAINERS: Move Jim Blandy to past maintainers.
[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,
7b6bb8da 4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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"
60d5a603 60#include <ctype.h>
4c2df51b 61
c906108c
SS
62#include <fcntl.h>
63#include "gdb_string.h"
4bdf3d34 64#include "gdb_assert.h"
c906108c 65#include <sys/types.h>
233a11ab
CS
66#ifdef HAVE_ZLIB_H
67#include <zlib.h>
68#endif
dce234bc
PP
69#ifdef HAVE_MMAP
70#include <sys/mman.h>
85d9bd0e
TT
71#ifndef MAP_FAILED
72#define MAP_FAILED ((void *) -1)
73#endif
dce234bc 74#endif
d8151005 75
34eaf542
TT
76typedef struct symbol *symbolp;
77DEF_VEC_P (symbolp);
78
107d2387 79#if 0
357e46e7 80/* .debug_info header for a compilation unit
c906108c
SS
81 Because of alignment constraints, this structure has padding and cannot
82 be mapped directly onto the beginning of the .debug_info section. */
83typedef struct comp_unit_header
84 {
85 unsigned int length; /* length of the .debug_info
86 contribution */
87 unsigned short version; /* version number -- 2 for DWARF
88 version 2 */
89 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
90 unsigned char addr_size; /* byte size of an address -- 4 */
91 }
92_COMP_UNIT_HEADER;
93#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 94#endif
c906108c 95
c906108c
SS
96/* .debug_line statement program prologue
97 Because of alignment constraints, this structure has padding and cannot
98 be mapped directly onto the beginning of the .debug_info section. */
99typedef struct statement_prologue
100 {
101 unsigned int total_length; /* byte length of the statement
102 information */
103 unsigned short version; /* version number -- 2 for DWARF
104 version 2 */
105 unsigned int prologue_length; /* # bytes between prologue &
106 stmt program */
107 unsigned char minimum_instruction_length; /* byte size of
108 smallest instr */
109 unsigned char default_is_stmt; /* initial value of is_stmt
110 register */
111 char line_base;
112 unsigned char line_range;
113 unsigned char opcode_base; /* number assigned to first special
114 opcode */
115 unsigned char *standard_opcode_lengths;
116 }
117_STATEMENT_PROLOGUE;
118
d97bc12b
DE
119/* When non-zero, dump DIEs after they are read in. */
120static int dwarf2_die_debug = 0;
121
dce234bc
PP
122static int pagesize;
123
df8a16a1
DJ
124/* When set, the file that we're processing is known to have debugging
125 info for C++ namespaces. GCC 3.3.x did not produce this information,
126 but later versions do. */
127
128static int processing_has_namespace_info;
129
6502dd73
DJ
130static const struct objfile_data *dwarf2_objfile_data_key;
131
dce234bc
PP
132struct dwarf2_section_info
133{
134 asection *asection;
135 gdb_byte *buffer;
136 bfd_size_type size;
137 int was_mmapped;
be391dca
TT
138 /* True if we have tried to read this section. */
139 int readin;
dce234bc
PP
140};
141
9291a0cd
TT
142/* All offsets in the index are of this type. It must be
143 architecture-independent. */
144typedef uint32_t offset_type;
145
146DEF_VEC_I (offset_type);
147
148/* A description of the mapped index. The file format is described in
149 a comment by the code that writes the index. */
150struct mapped_index
151{
559a7a62
JK
152 /* Index data format version. */
153 int version;
154
9291a0cd
TT
155 /* The total length of the buffer. */
156 off_t total_size;
b11b1f88 157
9291a0cd
TT
158 /* A pointer to the address table data. */
159 const gdb_byte *address_table;
b11b1f88 160
9291a0cd
TT
161 /* Size of the address table data in bytes. */
162 offset_type address_table_size;
b11b1f88 163
3876f04e
DE
164 /* The symbol table, implemented as a hash table. */
165 const offset_type *symbol_table;
b11b1f88 166
9291a0cd 167 /* Size in slots, each slot is 2 offset_types. */
3876f04e 168 offset_type symbol_table_slots;
b11b1f88 169
9291a0cd
TT
170 /* A pointer to the constant pool. */
171 const char *constant_pool;
172};
173
6502dd73
DJ
174struct dwarf2_per_objfile
175{
dce234bc
PP
176 struct dwarf2_section_info info;
177 struct dwarf2_section_info abbrev;
178 struct dwarf2_section_info line;
dce234bc
PP
179 struct dwarf2_section_info loc;
180 struct dwarf2_section_info macinfo;
181 struct dwarf2_section_info str;
182 struct dwarf2_section_info ranges;
348e048f 183 struct dwarf2_section_info types;
dce234bc
PP
184 struct dwarf2_section_info frame;
185 struct dwarf2_section_info eh_frame;
9291a0cd 186 struct dwarf2_section_info gdb_index;
ae038cb0 187
be391dca
TT
188 /* Back link. */
189 struct objfile *objfile;
190
10b3939b
DJ
191 /* A list of all the compilation units. This is used to locate
192 the target compilation unit of a particular reference. */
ae038cb0
DJ
193 struct dwarf2_per_cu_data **all_comp_units;
194
195 /* The number of compilation units in ALL_COMP_UNITS. */
196 int n_comp_units;
197
1fd400ff
TT
198 /* The number of .debug_types-related CUs. */
199 int n_type_comp_units;
200
201 /* The .debug_types-related CUs. */
202 struct dwarf2_per_cu_data **type_comp_units;
203
ae038cb0
DJ
204 /* A chain of compilation units that are currently read in, so that
205 they can be freed later. */
206 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 207
348e048f
DE
208 /* A table mapping .debug_types signatures to its signatured_type entry.
209 This is NULL if the .debug_types section hasn't been read in yet. */
210 htab_t signatured_types;
211
72dca2f5
FR
212 /* A flag indicating wether this objfile has a section loaded at a
213 VMA of 0. */
214 int has_section_at_zero;
9291a0cd 215
ae2de4f8
DE
216 /* True if we are using the mapped index,
217 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
218 unsigned char using_index;
219
ae2de4f8 220 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 221 struct mapped_index *index_table;
98bfdba5 222
7b9f3c50
DE
223 /* When using index_table, this keeps track of all quick_file_names entries.
224 TUs can share line table entries with CUs or other TUs, and there can be
225 a lot more TUs than unique line tables, so we maintain a separate table
226 of all line table entries to support the sharing. */
227 htab_t quick_file_names_table;
228
98bfdba5
PA
229 /* Set during partial symbol reading, to prevent queueing of full
230 symbols. */
231 int reading_partial_symbols;
673bfd45
DE
232
233 /* Table mapping type .debug_info DIE offsets to types.
234 This is NULL if not allocated yet.
235 It (currently) makes sense to allocate debug_types_type_hash lazily.
236 To keep things simple we allocate both lazily. */
237 htab_t debug_info_type_hash;
238
239 /* Table mapping type .debug_types DIE offsets to types.
240 This is NULL if not allocated yet. */
241 htab_t debug_types_type_hash;
6502dd73
DJ
242};
243
244static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
245
246/* names of the debugging sections */
247
233a11ab
CS
248/* Note that if the debugging section has been compressed, it might
249 have a name like .zdebug_info. */
250
251#define INFO_SECTION "debug_info"
252#define ABBREV_SECTION "debug_abbrev"
253#define LINE_SECTION "debug_line"
233a11ab
CS
254#define LOC_SECTION "debug_loc"
255#define MACINFO_SECTION "debug_macinfo"
256#define STR_SECTION "debug_str"
257#define RANGES_SECTION "debug_ranges"
348e048f 258#define TYPES_SECTION "debug_types"
233a11ab
CS
259#define FRAME_SECTION "debug_frame"
260#define EH_FRAME_SECTION "eh_frame"
9291a0cd 261#define GDB_INDEX_SECTION "gdb_index"
c906108c
SS
262
263/* local data types */
264
0963b4bd 265/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
266#ifndef ABBREV_HASH_SIZE
267#define ABBREV_HASH_SIZE 121
268#endif
269
107d2387
AC
270/* The data in a compilation unit header, after target2host
271 translation, looks like this. */
c906108c 272struct comp_unit_head
a738430d 273{
c764a876 274 unsigned int length;
a738430d 275 short version;
a738430d
MK
276 unsigned char addr_size;
277 unsigned char signed_addr_p;
9cbfa09e 278 unsigned int abbrev_offset;
57349743 279
a738430d
MK
280 /* Size of file offsets; either 4 or 8. */
281 unsigned int offset_size;
57349743 282
a738430d
MK
283 /* Size of the length field; either 4 or 12. */
284 unsigned int initial_length_size;
57349743 285
a738430d
MK
286 /* Offset to the first byte of this compilation unit header in the
287 .debug_info section, for resolving relative reference dies. */
288 unsigned int offset;
57349743 289
d00adf39
DE
290 /* Offset to first die in this cu from the start of the cu.
291 This will be the first byte following the compilation unit header. */
292 unsigned int first_die_offset;
a738430d 293};
c906108c 294
3da10d80
KS
295/* Type used for delaying computation of method physnames.
296 See comments for compute_delayed_physnames. */
297struct delayed_method_info
298{
299 /* The type to which the method is attached, i.e., its parent class. */
300 struct type *type;
301
302 /* The index of the method in the type's function fieldlists. */
303 int fnfield_index;
304
305 /* The index of the method in the fieldlist. */
306 int index;
307
308 /* The name of the DIE. */
309 const char *name;
310
311 /* The DIE associated with this method. */
312 struct die_info *die;
313};
314
315typedef struct delayed_method_info delayed_method_info;
316DEF_VEC_O (delayed_method_info);
317
e7c27a73
DJ
318/* Internal state when decoding a particular compilation unit. */
319struct dwarf2_cu
320{
321 /* The objfile containing this compilation unit. */
322 struct objfile *objfile;
323
d00adf39 324 /* The header of the compilation unit. */
e7c27a73 325 struct comp_unit_head header;
e142c38c 326
d00adf39
DE
327 /* Base address of this compilation unit. */
328 CORE_ADDR base_address;
329
330 /* Non-zero if base_address has been set. */
331 int base_known;
332
e142c38c
DJ
333 struct function_range *first_fn, *last_fn, *cached_fn;
334
335 /* The language we are debugging. */
336 enum language language;
337 const struct language_defn *language_defn;
338
b0f35d58
DL
339 const char *producer;
340
e142c38c
DJ
341 /* The generic symbol table building routines have separate lists for
342 file scope symbols and all all other scopes (local scopes). So
343 we need to select the right one to pass to add_symbol_to_list().
344 We do it by keeping a pointer to the correct list in list_in_scope.
345
346 FIXME: The original dwarf code just treated the file scope as the
347 first local scope, and all other local scopes as nested local
348 scopes, and worked fine. Check to see if we really need to
349 distinguish these in buildsym.c. */
350 struct pending **list_in_scope;
351
f3dd6933
DJ
352 /* DWARF abbreviation table associated with this compilation unit. */
353 struct abbrev_info **dwarf2_abbrevs;
354
355 /* Storage for the abbrev table. */
356 struct obstack abbrev_obstack;
72bf9492
DJ
357
358 /* Hash table holding all the loaded partial DIEs. */
359 htab_t partial_dies;
360
361 /* Storage for things with the same lifetime as this read-in compilation
362 unit, including partial DIEs. */
363 struct obstack comp_unit_obstack;
364
ae038cb0
DJ
365 /* When multiple dwarf2_cu structures are living in memory, this field
366 chains them all together, so that they can be released efficiently.
367 We will probably also want a generation counter so that most-recently-used
368 compilation units are cached... */
369 struct dwarf2_per_cu_data *read_in_chain;
370
371 /* Backchain to our per_cu entry if the tree has been built. */
372 struct dwarf2_per_cu_data *per_cu;
373
374 /* How many compilation units ago was this CU last referenced? */
375 int last_used;
376
10b3939b 377 /* A hash table of die offsets for following references. */
51545339 378 htab_t die_hash;
10b3939b
DJ
379
380 /* Full DIEs if read in. */
381 struct die_info *dies;
382
383 /* A set of pointers to dwarf2_per_cu_data objects for compilation
384 units referenced by this one. Only set during full symbol processing;
385 partial symbol tables do not have dependencies. */
386 htab_t dependencies;
387
cb1df416
DJ
388 /* Header data from the line table, during full symbol processing. */
389 struct line_header *line_header;
390
3da10d80
KS
391 /* A list of methods which need to have physnames computed
392 after all type information has been read. */
393 VEC (delayed_method_info) *method_list;
394
ae038cb0
DJ
395 /* Mark used when releasing cached dies. */
396 unsigned int mark : 1;
397
398 /* This flag will be set if this compilation unit might include
399 inter-compilation-unit references. */
400 unsigned int has_form_ref_addr : 1;
401
72bf9492
DJ
402 /* This flag will be set if this compilation unit includes any
403 DW_TAG_namespace DIEs. If we know that there are explicit
404 DIEs for namespaces, we don't need to try to infer them
405 from mangled names. */
406 unsigned int has_namespace_info : 1;
8be455d7
JK
407
408 /* This CU references .debug_loc. See the symtab->locations_valid field.
409 This test is imperfect as there may exist optimized debug code not using
410 any location list and still facing inlining issues if handled as
411 unoptimized code. For a future better test see GCC PR other/32998. */
412
413 unsigned int has_loclist : 1;
e7c27a73
DJ
414};
415
10b3939b
DJ
416/* Persistent data held for a compilation unit, even when not
417 processing it. We put a pointer to this structure in the
418 read_symtab_private field of the psymtab. If we encounter
419 inter-compilation-unit references, we also maintain a sorted
420 list of all compilation units. */
421
ae038cb0
DJ
422struct dwarf2_per_cu_data
423{
348e048f 424 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 425 bytes should suffice to store the length of any compilation unit
45452591
DE
426 - if it doesn't, GDB will fall over anyway.
427 NOTE: Unlike comp_unit_head.length, this length includes
428 initial_length_size. */
c764a876 429 unsigned int offset;
348e048f 430 unsigned int length : 29;
ae038cb0
DJ
431
432 /* Flag indicating this compilation unit will be read in before
433 any of the current compilation units are processed. */
c764a876 434 unsigned int queued : 1;
ae038cb0 435
5afb4e99
DJ
436 /* This flag will be set if we need to load absolutely all DIEs
437 for this compilation unit, instead of just the ones we think
438 are interesting. It gets set if we look for a DIE in the
439 hash table and don't find it. */
440 unsigned int load_all_dies : 1;
441
348e048f
DE
442 /* Non-zero if this CU is from .debug_types.
443 Otherwise it's from .debug_info. */
444 unsigned int from_debug_types : 1;
445
17ea53c3
JK
446 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
447 of the CU cache it gets reset to NULL again. */
ae038cb0 448 struct dwarf2_cu *cu;
1c379e20 449
9291a0cd
TT
450 /* The corresponding objfile. */
451 struct objfile *objfile;
452
453 /* When using partial symbol tables, the 'psymtab' field is active.
454 Otherwise the 'quick' field is active. */
455 union
456 {
457 /* The partial symbol table associated with this compilation unit,
458 or NULL for partial units (which do not have an associated
459 symtab). */
460 struct partial_symtab *psymtab;
461
462 /* Data needed by the "quick" functions. */
463 struct dwarf2_per_cu_quick_data *quick;
464 } v;
ae038cb0
DJ
465};
466
348e048f
DE
467/* Entry in the signatured_types hash table. */
468
469struct signatured_type
470{
471 ULONGEST signature;
472
348e048f
DE
473 /* Offset in .debug_types of the type defined by this TU. */
474 unsigned int type_offset;
475
476 /* The CU(/TU) of this type. */
477 struct dwarf2_per_cu_data per_cu;
478};
479
0963b4bd
MS
480/* Struct used to pass misc. parameters to read_die_and_children, et
481 al. which are used for both .debug_info and .debug_types dies.
482 All parameters here are unchanging for the life of the call. This
483 struct exists to abstract away the constant parameters of die
484 reading. */
93311388
DE
485
486struct die_reader_specs
487{
488 /* The bfd of this objfile. */
489 bfd* abfd;
490
491 /* The CU of the DIE we are parsing. */
492 struct dwarf2_cu *cu;
493
494 /* Pointer to start of section buffer.
495 This is either the start of .debug_info or .debug_types. */
496 const gdb_byte *buffer;
497};
498
debd256d
JB
499/* The line number information for a compilation unit (found in the
500 .debug_line section) begins with a "statement program header",
501 which contains the following information. */
502struct line_header
503{
504 unsigned int total_length;
505 unsigned short version;
506 unsigned int header_length;
507 unsigned char minimum_instruction_length;
2dc7f7b3 508 unsigned char maximum_ops_per_instruction;
debd256d
JB
509 unsigned char default_is_stmt;
510 int line_base;
511 unsigned char line_range;
512 unsigned char opcode_base;
513
514 /* standard_opcode_lengths[i] is the number of operands for the
515 standard opcode whose value is i. This means that
516 standard_opcode_lengths[0] is unused, and the last meaningful
517 element is standard_opcode_lengths[opcode_base - 1]. */
518 unsigned char *standard_opcode_lengths;
519
520 /* The include_directories table. NOTE! These strings are not
521 allocated with xmalloc; instead, they are pointers into
522 debug_line_buffer. If you try to free them, `free' will get
523 indigestion. */
524 unsigned int num_include_dirs, include_dirs_size;
525 char **include_dirs;
526
527 /* The file_names table. NOTE! These strings are not allocated
528 with xmalloc; instead, they are pointers into debug_line_buffer.
529 Don't try to free them directly. */
530 unsigned int num_file_names, file_names_size;
531 struct file_entry
c906108c 532 {
debd256d
JB
533 char *name;
534 unsigned int dir_index;
535 unsigned int mod_time;
536 unsigned int length;
aaa75496 537 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 538 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
539 } *file_names;
540
541 /* The start and end of the statement program following this
6502dd73 542 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 543 gdb_byte *statement_program_start, *statement_program_end;
debd256d 544};
c906108c
SS
545
546/* When we construct a partial symbol table entry we only
0963b4bd 547 need this much information. */
c906108c
SS
548struct partial_die_info
549 {
72bf9492 550 /* Offset of this DIE. */
c906108c 551 unsigned int offset;
72bf9492
DJ
552
553 /* DWARF-2 tag for this DIE. */
554 ENUM_BITFIELD(dwarf_tag) tag : 16;
555
72bf9492
DJ
556 /* Assorted flags describing the data found in this DIE. */
557 unsigned int has_children : 1;
558 unsigned int is_external : 1;
559 unsigned int is_declaration : 1;
560 unsigned int has_type : 1;
561 unsigned int has_specification : 1;
562 unsigned int has_pc_info : 1;
563
564 /* Flag set if the SCOPE field of this structure has been
565 computed. */
566 unsigned int scope_set : 1;
567
fa4028e9
JB
568 /* Flag set if the DIE has a byte_size attribute. */
569 unsigned int has_byte_size : 1;
570
98bfdba5
PA
571 /* Flag set if any of the DIE's children are template arguments. */
572 unsigned int has_template_arguments : 1;
573
abc72ce4
DE
574 /* Flag set if fixup_partial_die has been called on this die. */
575 unsigned int fixup_called : 1;
576
72bf9492 577 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 578 sometimes a default name for unnamed DIEs. */
c906108c 579 char *name;
72bf9492 580
abc72ce4
DE
581 /* The linkage name, if present. */
582 const char *linkage_name;
583
72bf9492
DJ
584 /* The scope to prepend to our children. This is generally
585 allocated on the comp_unit_obstack, so will disappear
586 when this compilation unit leaves the cache. */
587 char *scope;
588
589 /* The location description associated with this DIE, if any. */
590 struct dwarf_block *locdesc;
591
592 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
593 CORE_ADDR lowpc;
594 CORE_ADDR highpc;
72bf9492 595
93311388 596 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 597 DW_AT_sibling, if any. */
abc72ce4
DE
598 /* NOTE: This member isn't strictly necessary, read_partial_die could
599 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 600 gdb_byte *sibling;
72bf9492
DJ
601
602 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
603 DW_AT_specification (or DW_AT_abstract_origin or
604 DW_AT_extension). */
605 unsigned int spec_offset;
606
607 /* Pointers to this DIE's parent, first child, and next sibling,
608 if any. */
609 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
610 };
611
0963b4bd 612/* This data structure holds the information of an abbrev. */
c906108c
SS
613struct abbrev_info
614 {
615 unsigned int number; /* number identifying abbrev */
616 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
617 unsigned short has_children; /* boolean */
618 unsigned short num_attrs; /* number of attributes */
c906108c
SS
619 struct attr_abbrev *attrs; /* an array of attribute descriptions */
620 struct abbrev_info *next; /* next in chain */
621 };
622
623struct attr_abbrev
624 {
9d25dd43
DE
625 ENUM_BITFIELD(dwarf_attribute) name : 16;
626 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
627 };
628
0963b4bd 629/* Attributes have a name and a value. */
b60c80d6
DJ
630struct attribute
631 {
9d25dd43 632 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
633 ENUM_BITFIELD(dwarf_form) form : 15;
634
635 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
636 field should be in u.str (existing only for DW_STRING) but it is kept
637 here for better struct attribute alignment. */
638 unsigned int string_is_canonical : 1;
639
b60c80d6
DJ
640 union
641 {
642 char *str;
643 struct dwarf_block *blk;
43bbcdc2
PH
644 ULONGEST unsnd;
645 LONGEST snd;
b60c80d6 646 CORE_ADDR addr;
348e048f 647 struct signatured_type *signatured_type;
b60c80d6
DJ
648 }
649 u;
650 };
651
0963b4bd 652/* This data structure holds a complete die structure. */
c906108c
SS
653struct die_info
654 {
76815b17
DE
655 /* DWARF-2 tag for this DIE. */
656 ENUM_BITFIELD(dwarf_tag) tag : 16;
657
658 /* Number of attributes */
98bfdba5
PA
659 unsigned char num_attrs;
660
661 /* True if we're presently building the full type name for the
662 type derived from this DIE. */
663 unsigned char building_fullname : 1;
76815b17
DE
664
665 /* Abbrev number */
666 unsigned int abbrev;
667
93311388 668 /* Offset in .debug_info or .debug_types section. */
76815b17 669 unsigned int offset;
78ba4af6
JB
670
671 /* The dies in a compilation unit form an n-ary tree. PARENT
672 points to this die's parent; CHILD points to the first child of
673 this node; and all the children of a given node are chained
4950bc1c 674 together via their SIBLING fields. */
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
0963b4bd 693/* Get at parts of an attribute structure. */
c906108c
SS
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 702
0963b4bd 703/* Blocks are a bunch of untyped bytes. */
c906108c
SS
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 728 {
0963b4bd 729 /* List of data member and baseclasses fields. */
c5aa993b
JM
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{
3e43a32a
MS
803 fprintf_filtered (file, _("The upper bound on the age of cached "
804 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
805 value);
806}
807
ae038cb0 808
0963b4bd 809/* Various complaints about symbol reading that don't abort the process. */
c906108c 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,
3e43a32a
MS
829 _(".debug_line section has line "
830 "program sequence without an end"));
59205f5a
JB
831}
832
4d3c2250
KB
833static void
834dwarf2_complex_location_expr_complaint (void)
2e276125 835{
e2e0b3e5 836 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
837}
838
4d3c2250
KB
839static void
840dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
841 int arg3)
2e276125 842{
4d3c2250 843 complaint (&symfile_complaints,
3e43a32a
MS
844 _("const value length mismatch for '%s', got %d, expected %d"),
845 arg1, arg2, arg3);
4d3c2250
KB
846}
847
848static void
849dwarf2_macros_too_long_complaint (void)
2e276125 850{
4d3c2250 851 complaint (&symfile_complaints,
e2e0b3e5 852 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
853}
854
855static void
856dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 857{
4d3c2250 858 complaint (&symfile_complaints,
3e43a32a
MS
859 _("macro debug info contains a "
860 "malformed macro definition:\n`%s'"),
4d3c2250
KB
861 arg1);
862}
863
864static void
865dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 866{
4d3c2250 867 complaint (&symfile_complaints,
3e43a32a
MS
868 _("invalid attribute class or form for '%s' in '%s'"),
869 arg1, arg2);
4d3c2250 870}
c906108c 871
c906108c
SS
872/* local function prototypes */
873
4efb68b1 874static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 875
aaa75496
JB
876static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
877 struct objfile *);
878
c67a9c90 879static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 880
72bf9492
DJ
881static void scan_partial_symbols (struct partial_die_info *,
882 CORE_ADDR *, CORE_ADDR *,
5734ee8b 883 int, struct dwarf2_cu *);
c906108c 884
72bf9492
DJ
885static void add_partial_symbol (struct partial_die_info *,
886 struct dwarf2_cu *);
63d06c5c 887
72bf9492
DJ
888static void add_partial_namespace (struct partial_die_info *pdi,
889 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 890 int need_pc, struct dwarf2_cu *cu);
63d06c5c 891
5d7cb8df
JK
892static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
893 CORE_ADDR *highpc, int need_pc,
894 struct dwarf2_cu *cu);
895
72bf9492
DJ
896static void add_partial_enumeration (struct partial_die_info *enum_pdi,
897 struct dwarf2_cu *cu);
91c24f0a 898
bc30ff58
JB
899static void add_partial_subprogram (struct partial_die_info *pdi,
900 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 901 int need_pc, struct dwarf2_cu *cu);
bc30ff58 902
fe1b8b76 903static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
904 gdb_byte *buffer, gdb_byte *info_ptr,
905 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 906
a14ed312 907static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 908
a14ed312 909static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 910
e7c27a73 911static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 912
f3dd6933 913static void dwarf2_free_abbrev_table (void *);
c906108c 914
fe1b8b76 915static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 916 struct dwarf2_cu *);
72bf9492 917
57349743 918static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 919 struct dwarf2_cu *);
c906108c 920
93311388
DE
921static struct partial_die_info *load_partial_dies (bfd *,
922 gdb_byte *, gdb_byte *,
923 int, struct dwarf2_cu *);
72bf9492 924
fe1b8b76 925static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
926 struct abbrev_info *abbrev,
927 unsigned int, bfd *,
928 gdb_byte *, gdb_byte *,
929 struct dwarf2_cu *);
c906108c 930
c764a876 931static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 932 struct dwarf2_cu *);
72bf9492
DJ
933
934static void fixup_partial_die (struct partial_die_info *,
935 struct dwarf2_cu *);
936
fe1b8b76
JB
937static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
938 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 939
fe1b8b76
JB
940static gdb_byte *read_attribute_value (struct attribute *, unsigned,
941 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 942
fe1b8b76 943static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 944
fe1b8b76 945static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 946
fe1b8b76 947static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 948
fe1b8b76 949static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 950
93311388 951static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 952
fe1b8b76 953static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 954 unsigned int *);
c906108c 955
c764a876
DE
956static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
957
958static LONGEST read_checked_initial_length_and_offset
959 (bfd *, gdb_byte *, const struct comp_unit_head *,
960 unsigned int *, unsigned int *);
613e1657 961
fe1b8b76 962static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
963 unsigned int *);
964
965static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 966
fe1b8b76 967static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 968
9b1c24c8 969static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 970
fe1b8b76
JB
971static char *read_indirect_string (bfd *, gdb_byte *,
972 const struct comp_unit_head *,
973 unsigned int *);
4bdf3d34 974
fe1b8b76 975static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 976
fe1b8b76 977static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 978
fe1b8b76 979static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 980
e142c38c 981static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 982
e142c38c
DJ
983static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
984 struct dwarf2_cu *);
c906108c 985
348e048f
DE
986static struct attribute *dwarf2_attr_no_follow (struct die_info *,
987 unsigned int,
988 struct dwarf2_cu *);
989
05cf31d1
JB
990static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
991 struct dwarf2_cu *cu);
992
e142c38c 993static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 994
e142c38c 995static struct die_info *die_specification (struct die_info *die,
f2f0e013 996 struct dwarf2_cu **);
63d06c5c 997
debd256d
JB
998static void free_line_header (struct line_header *lh);
999
aaa75496
JB
1000static void add_file_name (struct line_header *, char *, unsigned int,
1001 unsigned int, unsigned int);
1002
debd256d
JB
1003static struct line_header *(dwarf_decode_line_header
1004 (unsigned int offset,
e7c27a73 1005 bfd *abfd, struct dwarf2_cu *cu));
debd256d 1006
72b9f47f 1007static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 1008 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1009
72b9f47f 1010static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1011
a14ed312 1012static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1013 struct dwarf2_cu *);
c906108c 1014
34eaf542
TT
1015static struct symbol *new_symbol_full (struct die_info *, struct type *,
1016 struct dwarf2_cu *, struct symbol *);
1017
a14ed312 1018static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1019 struct dwarf2_cu *);
c906108c 1020
98bfdba5
PA
1021static void dwarf2_const_value_attr (struct attribute *attr,
1022 struct type *type,
1023 const char *name,
1024 struct obstack *obstack,
1025 struct dwarf2_cu *cu, long *value,
1026 gdb_byte **bytes,
1027 struct dwarf2_locexpr_baton **baton);
2df3850c 1028
e7c27a73 1029static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1030
b4ba55a1
JB
1031static int need_gnat_info (struct dwarf2_cu *);
1032
3e43a32a
MS
1033static struct type *die_descriptive_type (struct die_info *,
1034 struct dwarf2_cu *);
b4ba55a1
JB
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
673bfd45
DE
1042static struct type *lookup_die_type (struct die_info *, struct attribute *,
1043 struct dwarf2_cu *);
c906108c 1044
f792889a 1045static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1046
673bfd45
DE
1047static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1048
086ed43d 1049static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1050
6e70227d 1051static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1052 const char *suffix, int physname,
1053 struct dwarf2_cu *cu);
63d06c5c 1054
e7c27a73 1055static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1056
348e048f
DE
1057static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1058
e7c27a73 1059static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1060
e7c27a73 1061static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1062
ff013f42
JK
1063static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1064 struct dwarf2_cu *, struct partial_symtab *);
1065
a14ed312 1066static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1067 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1068 struct partial_symtab *);
c906108c 1069
fae299cd
DC
1070static void get_scope_pc_bounds (struct die_info *,
1071 CORE_ADDR *, CORE_ADDR *,
1072 struct dwarf2_cu *);
1073
801e3a5b
JB
1074static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1075 CORE_ADDR, struct dwarf2_cu *);
1076
a14ed312 1077static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1078 struct dwarf2_cu *);
c906108c 1079
a14ed312 1080static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1081 struct type *, struct dwarf2_cu *);
c906108c 1082
a14ed312 1083static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1084 struct die_info *, struct type *,
e7c27a73 1085 struct dwarf2_cu *);
c906108c 1086
a14ed312 1087static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1088 struct type *,
1089 struct dwarf2_cu *);
c906108c 1090
134d01f1 1091static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1092
e7c27a73 1093static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1094
e7c27a73 1095static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1096
5d7cb8df
JK
1097static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1098
27aa8d6a
SW
1099static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1100
f55ee35c
JK
1101static struct type *read_module_type (struct die_info *die,
1102 struct dwarf2_cu *cu);
1103
38d518c9 1104static const char *namespace_name (struct die_info *die,
e142c38c 1105 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1106
134d01f1 1107static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1108
e7c27a73 1109static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1110
6e70227d 1111static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1112 struct dwarf2_cu *);
1113
93311388 1114static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1115
93311388
DE
1116static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1117 gdb_byte *info_ptr,
d97bc12b
DE
1118 gdb_byte **new_info_ptr,
1119 struct die_info *parent);
1120
93311388
DE
1121static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1122 gdb_byte *info_ptr,
fe1b8b76 1123 gdb_byte **new_info_ptr,
639d11d3
DC
1124 struct die_info *parent);
1125
93311388
DE
1126static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1127 gdb_byte *info_ptr,
fe1b8b76 1128 gdb_byte **new_info_ptr,
639d11d3
DC
1129 struct die_info *parent);
1130
93311388
DE
1131static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1132 struct die_info **, gdb_byte *,
1133 int *);
1134
e7c27a73 1135static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1136
71c25dea
TT
1137static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1138 struct obstack *);
1139
e142c38c 1140static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1141
98bfdba5
PA
1142static const char *dwarf2_full_name (char *name,
1143 struct die_info *die,
1144 struct dwarf2_cu *cu);
1145
e142c38c 1146static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1147 struct dwarf2_cu **);
9219021c 1148
a14ed312 1149static char *dwarf_tag_name (unsigned int);
c906108c 1150
a14ed312 1151static char *dwarf_attr_name (unsigned int);
c906108c 1152
a14ed312 1153static char *dwarf_form_name (unsigned int);
c906108c 1154
a14ed312 1155static char *dwarf_bool_name (unsigned int);
c906108c 1156
a14ed312 1157static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1158
1159#if 0
a14ed312 1160static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1161#endif
1162
f9aca02d 1163static struct die_info *sibling_die (struct die_info *);
c906108c 1164
d97bc12b
DE
1165static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1166
1167static void dump_die_for_error (struct die_info *);
1168
1169static void dump_die_1 (struct ui_file *, int level, int max_level,
1170 struct die_info *);
c906108c 1171
d97bc12b 1172/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1173
51545339 1174static void store_in_ref_table (struct die_info *,
10b3939b 1175 struct dwarf2_cu *);
c906108c 1176
93311388
DE
1177static int is_ref_attr (struct attribute *);
1178
c764a876 1179static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1180
43bbcdc2 1181static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1182
348e048f
DE
1183static struct die_info *follow_die_ref_or_sig (struct die_info *,
1184 struct attribute *,
1185 struct dwarf2_cu **);
1186
10b3939b
DJ
1187static struct die_info *follow_die_ref (struct die_info *,
1188 struct attribute *,
f2f0e013 1189 struct dwarf2_cu **);
c906108c 1190
348e048f
DE
1191static struct die_info *follow_die_sig (struct die_info *,
1192 struct attribute *,
1193 struct dwarf2_cu **);
1194
1195static void read_signatured_type_at_offset (struct objfile *objfile,
1196 unsigned int offset);
1197
1198static void read_signatured_type (struct objfile *,
1199 struct signatured_type *type_sig);
1200
c906108c
SS
1201/* memory allocation interface */
1202
7b5a2f43 1203static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1204
f3dd6933 1205static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1206
b60c80d6 1207static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1208
e142c38c 1209static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1210
e142c38c
DJ
1211static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1212 struct dwarf2_cu *);
5fb290d7 1213
2e276125 1214static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1215 char *, bfd *, struct dwarf2_cu *);
2e276125 1216
8e19ed76
PS
1217static int attr_form_is_block (struct attribute *);
1218
3690dd37
JB
1219static int attr_form_is_section_offset (struct attribute *);
1220
1221static int attr_form_is_constant (struct attribute *);
1222
8cf6f0b1
TT
1223static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1224 struct dwarf2_loclist_baton *baton,
1225 struct attribute *attr);
1226
93e7bd98
DJ
1227static void dwarf2_symbol_mark_computed (struct attribute *attr,
1228 struct symbol *sym,
1229 struct dwarf2_cu *cu);
4c2df51b 1230
93311388
DE
1231static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1232 struct abbrev_info *abbrev,
1233 struct dwarf2_cu *cu);
4bb7a0a7 1234
72bf9492
DJ
1235static void free_stack_comp_unit (void *);
1236
72bf9492
DJ
1237static hashval_t partial_die_hash (const void *item);
1238
1239static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1240
ae038cb0 1241static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1242 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1243
1244static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1245 (unsigned int offset, struct objfile *objfile);
ae038cb0 1246
9816fde3
JK
1247static void init_one_comp_unit (struct dwarf2_cu *cu,
1248 struct objfile *objfile);
1249
1250static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1251 struct die_info *comp_unit_die);
93311388 1252
ae038cb0
DJ
1253static void free_one_comp_unit (void *);
1254
1255static void free_cached_comp_units (void *);
1256
1257static void age_cached_comp_units (void);
1258
1259static void free_one_cached_comp_unit (void *);
1260
f792889a
DJ
1261static struct type *set_die_type (struct die_info *, struct type *,
1262 struct dwarf2_cu *);
1c379e20 1263
ae038cb0
DJ
1264static void create_all_comp_units (struct objfile *);
1265
1fd400ff
TT
1266static int create_debug_types_hash_table (struct objfile *objfile);
1267
93311388
DE
1268static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1269 struct objfile *);
10b3939b
DJ
1270
1271static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1272
1273static void dwarf2_add_dependence (struct dwarf2_cu *,
1274 struct dwarf2_per_cu_data *);
1275
ae038cb0
DJ
1276static void dwarf2_mark (struct dwarf2_cu *);
1277
1278static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1279
673bfd45
DE
1280static struct type *get_die_type_at_offset (unsigned int,
1281 struct dwarf2_per_cu_data *per_cu);
1282
f792889a 1283static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1284
9291a0cd
TT
1285static void dwarf2_release_queue (void *dummy);
1286
1287static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1288 struct objfile *objfile);
1289
1290static void process_queue (struct objfile *objfile);
1291
1292static void find_file_and_directory (struct die_info *die,
1293 struct dwarf2_cu *cu,
1294 char **name, char **comp_dir);
1295
1296static char *file_full_name (int file, struct line_header *lh,
1297 const char *comp_dir);
1298
1299static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1300 gdb_byte *info_ptr,
1301 gdb_byte *buffer,
1302 unsigned int buffer_size,
1303 bfd *abfd);
1304
1305static void init_cu_die_reader (struct die_reader_specs *reader,
1306 struct dwarf2_cu *cu);
1307
673bfd45 1308static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1309
9291a0cd
TT
1310#if WORDS_BIGENDIAN
1311
1312/* Convert VALUE between big- and little-endian. */
1313static offset_type
1314byte_swap (offset_type value)
1315{
1316 offset_type result;
1317
1318 result = (value & 0xff) << 24;
1319 result |= (value & 0xff00) << 8;
1320 result |= (value & 0xff0000) >> 8;
1321 result |= (value & 0xff000000) >> 24;
1322 return result;
1323}
1324
1325#define MAYBE_SWAP(V) byte_swap (V)
1326
1327#else
1328#define MAYBE_SWAP(V) (V)
1329#endif /* WORDS_BIGENDIAN */
1330
1331/* The suffix for an index file. */
1332#define INDEX_SUFFIX ".gdb-index"
1333
3da10d80
KS
1334static const char *dwarf2_physname (char *name, struct die_info *die,
1335 struct dwarf2_cu *cu);
1336
c906108c
SS
1337/* Try to locate the sections we need for DWARF 2 debugging
1338 information and return true if we have enough to do something. */
1339
1340int
6502dd73 1341dwarf2_has_info (struct objfile *objfile)
c906108c 1342{
be391dca
TT
1343 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1344 if (!dwarf2_per_objfile)
1345 {
1346 /* Initialize per-objfile state. */
1347 struct dwarf2_per_objfile *data
1348 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1349
be391dca
TT
1350 memset (data, 0, sizeof (*data));
1351 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1352 dwarf2_per_objfile = data;
6502dd73 1353
be391dca
TT
1354 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1355 dwarf2_per_objfile->objfile = objfile;
1356 }
1357 return (dwarf2_per_objfile->info.asection != NULL
1358 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1359}
1360
233a11ab
CS
1361/* When loading sections, we can either look for ".<name>", or for
1362 * ".z<name>", which indicates a compressed section. */
1363
1364static int
dce234bc 1365section_is_p (const char *section_name, const char *name)
233a11ab 1366{
dce234bc
PP
1367 return (section_name[0] == '.'
1368 && (strcmp (section_name + 1, name) == 0
1369 || (section_name[1] == 'z'
1370 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1371}
1372
c906108c
SS
1373/* This function is mapped across the sections and remembers the
1374 offset and size of each of the debugging sections we are interested
1375 in. */
1376
1377static void
72dca2f5 1378dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1379{
dce234bc 1380 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1381 {
dce234bc
PP
1382 dwarf2_per_objfile->info.asection = sectp;
1383 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1384 }
dce234bc 1385 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1386 {
dce234bc
PP
1387 dwarf2_per_objfile->abbrev.asection = sectp;
1388 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1389 }
dce234bc 1390 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1391 {
dce234bc
PP
1392 dwarf2_per_objfile->line.asection = sectp;
1393 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1394 }
dce234bc 1395 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1396 {
dce234bc
PP
1397 dwarf2_per_objfile->loc.asection = sectp;
1398 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1399 }
dce234bc 1400 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1401 {
dce234bc
PP
1402 dwarf2_per_objfile->macinfo.asection = sectp;
1403 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1404 }
dce234bc 1405 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1406 {
dce234bc
PP
1407 dwarf2_per_objfile->str.asection = sectp;
1408 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1409 }
dce234bc 1410 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1411 {
dce234bc
PP
1412 dwarf2_per_objfile->frame.asection = sectp;
1413 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1414 }
dce234bc 1415 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1416 {
3799ccc6 1417 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1418
3799ccc6
EZ
1419 if (aflag & SEC_HAS_CONTENTS)
1420 {
dce234bc
PP
1421 dwarf2_per_objfile->eh_frame.asection = sectp;
1422 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1423 }
b6af0555 1424 }
dce234bc 1425 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1426 {
dce234bc
PP
1427 dwarf2_per_objfile->ranges.asection = sectp;
1428 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1429 }
348e048f
DE
1430 else if (section_is_p (sectp->name, TYPES_SECTION))
1431 {
1432 dwarf2_per_objfile->types.asection = sectp;
1433 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1434 }
9291a0cd
TT
1435 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1436 {
1437 dwarf2_per_objfile->gdb_index.asection = sectp;
1438 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1439 }
dce234bc 1440
72dca2f5
FR
1441 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1442 && bfd_section_vma (abfd, sectp) == 0)
1443 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1444}
1445
dce234bc
PP
1446/* Decompress a section that was compressed using zlib. Store the
1447 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1448
1449static void
dce234bc
PP
1450zlib_decompress_section (struct objfile *objfile, asection *sectp,
1451 gdb_byte **outbuf, bfd_size_type *outsize)
1452{
1453 bfd *abfd = objfile->obfd;
1454#ifndef HAVE_ZLIB_H
1455 error (_("Support for zlib-compressed DWARF data (from '%s') "
1456 "is disabled in this copy of GDB"),
1457 bfd_get_filename (abfd));
1458#else
1459 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1460 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1461 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1462 bfd_size_type uncompressed_size;
1463 gdb_byte *uncompressed_buffer;
1464 z_stream strm;
1465 int rc;
1466 int header_size = 12;
1467
1468 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1469 || bfd_bread (compressed_buffer,
1470 compressed_size, abfd) != compressed_size)
dce234bc
PP
1471 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1472 bfd_get_filename (abfd));
1473
1474 /* Read the zlib header. In this case, it should be "ZLIB" followed
1475 by the uncompressed section size, 8 bytes in big-endian order. */
1476 if (compressed_size < header_size
1477 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1478 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1479 bfd_get_filename (abfd));
1480 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1481 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1482 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1483 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1484 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1485 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1486 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1487 uncompressed_size += compressed_buffer[11];
1488
1489 /* It is possible the section consists of several compressed
1490 buffers concatenated together, so we uncompress in a loop. */
1491 strm.zalloc = NULL;
1492 strm.zfree = NULL;
1493 strm.opaque = NULL;
1494 strm.avail_in = compressed_size - header_size;
1495 strm.next_in = (Bytef*) compressed_buffer + header_size;
1496 strm.avail_out = uncompressed_size;
1497 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1498 uncompressed_size);
1499 rc = inflateInit (&strm);
1500 while (strm.avail_in > 0)
1501 {
1502 if (rc != Z_OK)
1503 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1504 bfd_get_filename (abfd), rc);
1505 strm.next_out = ((Bytef*) uncompressed_buffer
1506 + (uncompressed_size - strm.avail_out));
1507 rc = inflate (&strm, Z_FINISH);
1508 if (rc != Z_STREAM_END)
1509 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1510 bfd_get_filename (abfd), rc);
1511 rc = inflateReset (&strm);
1512 }
1513 rc = inflateEnd (&strm);
1514 if (rc != Z_OK
1515 || strm.avail_out != 0)
1516 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1517 bfd_get_filename (abfd), rc);
1518
affddf13 1519 do_cleanups (cleanup);
dce234bc
PP
1520 *outbuf = uncompressed_buffer;
1521 *outsize = uncompressed_size;
1522#endif
233a11ab
CS
1523}
1524
9e0ac564
TT
1525/* A helper function that decides whether a section is empty. */
1526
1527static int
1528dwarf2_section_empty_p (struct dwarf2_section_info *info)
1529{
1530 return info->asection == NULL || info->size == 0;
1531}
1532
dce234bc
PP
1533/* Read the contents of the section SECTP from object file specified by
1534 OBJFILE, store info about the section into INFO.
1535 If the section is compressed, uncompress it before returning. */
c906108c 1536
dce234bc
PP
1537static void
1538dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1539{
dce234bc
PP
1540 bfd *abfd = objfile->obfd;
1541 asection *sectp = info->asection;
1542 gdb_byte *buf, *retbuf;
1543 unsigned char header[4];
c906108c 1544
be391dca
TT
1545 if (info->readin)
1546 return;
dce234bc
PP
1547 info->buffer = NULL;
1548 info->was_mmapped = 0;
be391dca 1549 info->readin = 1;
188dd5d6 1550
9e0ac564 1551 if (dwarf2_section_empty_p (info))
dce234bc 1552 return;
c906108c 1553
dce234bc
PP
1554 /* Check if the file has a 4-byte header indicating compression. */
1555 if (info->size > sizeof (header)
1556 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1557 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1558 {
1559 /* Upon decompression, update the buffer and its size. */
1560 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1561 {
1562 zlib_decompress_section (objfile, sectp, &info->buffer,
1563 &info->size);
1564 return;
1565 }
1566 }
4bdf3d34 1567
dce234bc
PP
1568#ifdef HAVE_MMAP
1569 if (pagesize == 0)
1570 pagesize = getpagesize ();
2e276125 1571
dce234bc
PP
1572 /* Only try to mmap sections which are large enough: we don't want to
1573 waste space due to fragmentation. Also, only try mmap for sections
1574 without relocations. */
1575
1576 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1577 {
1578 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1579 size_t map_length = info->size + sectp->filepos - pg_offset;
1580 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1581 MAP_PRIVATE, pg_offset);
1582
1583 if (retbuf != MAP_FAILED)
1584 {
1585 info->was_mmapped = 1;
1586 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1587#if HAVE_POSIX_MADVISE
1588 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1589#endif
dce234bc
PP
1590 return;
1591 }
1592 }
1593#endif
1594
1595 /* If we get here, we are a normal, not-compressed section. */
1596 info->buffer = buf
1597 = obstack_alloc (&objfile->objfile_obstack, info->size);
1598
1599 /* When debugging .o files, we may need to apply relocations; see
1600 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1601 We never compress sections in .o files, so we only need to
1602 try this when the section is not compressed. */
ac8035ab 1603 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1604 if (retbuf != NULL)
1605 {
1606 info->buffer = retbuf;
1607 return;
1608 }
1609
1610 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1611 || bfd_bread (buf, info->size, abfd) != info->size)
1612 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1613 bfd_get_filename (abfd));
1614}
1615
9e0ac564
TT
1616/* A helper function that returns the size of a section in a safe way.
1617 If you are positive that the section has been read before using the
1618 size, then it is safe to refer to the dwarf2_section_info object's
1619 "size" field directly. In other cases, you must call this
1620 function, because for compressed sections the size field is not set
1621 correctly until the section has been read. */
1622
1623static bfd_size_type
1624dwarf2_section_size (struct objfile *objfile,
1625 struct dwarf2_section_info *info)
1626{
1627 if (!info->readin)
1628 dwarf2_read_section (objfile, info);
1629 return info->size;
1630}
1631
dce234bc 1632/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1633 SECTION_NAME. */
af34e669 1634
dce234bc 1635void
3017a003
TG
1636dwarf2_get_section_info (struct objfile *objfile,
1637 enum dwarf2_section_enum sect,
dce234bc
PP
1638 asection **sectp, gdb_byte **bufp,
1639 bfd_size_type *sizep)
1640{
1641 struct dwarf2_per_objfile *data
1642 = objfile_data (objfile, dwarf2_objfile_data_key);
1643 struct dwarf2_section_info *info;
a3b2a86b
TT
1644
1645 /* We may see an objfile without any DWARF, in which case we just
1646 return nothing. */
1647 if (data == NULL)
1648 {
1649 *sectp = NULL;
1650 *bufp = NULL;
1651 *sizep = 0;
1652 return;
1653 }
3017a003
TG
1654 switch (sect)
1655 {
1656 case DWARF2_DEBUG_FRAME:
1657 info = &data->frame;
1658 break;
1659 case DWARF2_EH_FRAME:
1660 info = &data->eh_frame;
1661 break;
1662 default:
1663 gdb_assert_not_reached ("unexpected section");
1664 }
dce234bc 1665
9e0ac564 1666 dwarf2_read_section (objfile, info);
dce234bc
PP
1667
1668 *sectp = info->asection;
1669 *bufp = info->buffer;
1670 *sizep = info->size;
1671}
1672
9291a0cd 1673\f
7b9f3c50
DE
1674/* DWARF quick_symbols_functions support. */
1675
1676/* TUs can share .debug_line entries, and there can be a lot more TUs than
1677 unique line tables, so we maintain a separate table of all .debug_line
1678 derived entries to support the sharing.
1679 All the quick functions need is the list of file names. We discard the
1680 line_header when we're done and don't need to record it here. */
1681struct quick_file_names
1682{
1683 /* The offset in .debug_line of the line table. We hash on this. */
1684 unsigned int offset;
1685
1686 /* The number of entries in file_names, real_names. */
1687 unsigned int num_file_names;
1688
1689 /* The file names from the line table, after being run through
1690 file_full_name. */
1691 const char **file_names;
1692
1693 /* The file names from the line table after being run through
1694 gdb_realpath. These are computed lazily. */
1695 const char **real_names;
1696};
1697
1698/* When using the index (and thus not using psymtabs), each CU has an
1699 object of this type. This is used to hold information needed by
1700 the various "quick" methods. */
1701struct dwarf2_per_cu_quick_data
1702{
1703 /* The file table. This can be NULL if there was no file table
1704 or it's currently not read in.
1705 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1706 struct quick_file_names *file_names;
1707
1708 /* The corresponding symbol table. This is NULL if symbols for this
1709 CU have not yet been read. */
1710 struct symtab *symtab;
1711
1712 /* A temporary mark bit used when iterating over all CUs in
1713 expand_symtabs_matching. */
1714 unsigned int mark : 1;
1715
1716 /* True if we've tried to read the file table and found there isn't one.
1717 There will be no point in trying to read it again next time. */
1718 unsigned int no_file_data : 1;
1719};
1720
1721/* Hash function for a quick_file_names. */
1722
1723static hashval_t
1724hash_file_name_entry (const void *e)
1725{
1726 const struct quick_file_names *file_data = e;
1727
1728 return file_data->offset;
1729}
1730
1731/* Equality function for a quick_file_names. */
1732
1733static int
1734eq_file_name_entry (const void *a, const void *b)
1735{
1736 const struct quick_file_names *ea = a;
1737 const struct quick_file_names *eb = b;
1738
1739 return ea->offset == eb->offset;
1740}
1741
1742/* Delete function for a quick_file_names. */
1743
1744static void
1745delete_file_name_entry (void *e)
1746{
1747 struct quick_file_names *file_data = e;
1748 int i;
1749
1750 for (i = 0; i < file_data->num_file_names; ++i)
1751 {
1752 xfree ((void*) file_data->file_names[i]);
1753 if (file_data->real_names)
1754 xfree ((void*) file_data->real_names[i]);
1755 }
1756
1757 /* The space for the struct itself lives on objfile_obstack,
1758 so we don't free it here. */
1759}
1760
1761/* Create a quick_file_names hash table. */
1762
1763static htab_t
1764create_quick_file_names_table (unsigned int nr_initial_entries)
1765{
1766 return htab_create_alloc (nr_initial_entries,
1767 hash_file_name_entry, eq_file_name_entry,
1768 delete_file_name_entry, xcalloc, xfree);
1769}
9291a0cd
TT
1770
1771/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1772 this CU came. */
2fdf6df6 1773
9291a0cd
TT
1774static void
1775dw2_do_instantiate_symtab (struct objfile *objfile,
1776 struct dwarf2_per_cu_data *per_cu)
1777{
1778 struct cleanup *back_to;
1779
1780 back_to = make_cleanup (dwarf2_release_queue, NULL);
1781
1782 queue_comp_unit (per_cu, objfile);
1783
1784 if (per_cu->from_debug_types)
1785 read_signatured_type_at_offset (objfile, per_cu->offset);
1786 else
1787 load_full_comp_unit (per_cu, objfile);
1788
1789 process_queue (objfile);
1790
1791 /* Age the cache, releasing compilation units that have not
1792 been used recently. */
1793 age_cached_comp_units ();
1794
1795 do_cleanups (back_to);
1796}
1797
1798/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1799 the objfile from which this CU came. Returns the resulting symbol
1800 table. */
2fdf6df6 1801
9291a0cd
TT
1802static struct symtab *
1803dw2_instantiate_symtab (struct objfile *objfile,
1804 struct dwarf2_per_cu_data *per_cu)
1805{
1806 if (!per_cu->v.quick->symtab)
1807 {
1808 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1809 increment_reading_symtab ();
1810 dw2_do_instantiate_symtab (objfile, per_cu);
1811 do_cleanups (back_to);
1812 }
1813 return per_cu->v.quick->symtab;
1814}
1815
1fd400ff 1816/* Return the CU given its index. */
2fdf6df6 1817
1fd400ff
TT
1818static struct dwarf2_per_cu_data *
1819dw2_get_cu (int index)
1820{
1821 if (index >= dwarf2_per_objfile->n_comp_units)
1822 {
1823 index -= dwarf2_per_objfile->n_comp_units;
1824 return dwarf2_per_objfile->type_comp_units[index];
1825 }
1826 return dwarf2_per_objfile->all_comp_units[index];
1827}
1828
9291a0cd
TT
1829/* A helper function that knows how to read a 64-bit value in a way
1830 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1831 otherwise. */
2fdf6df6 1832
9291a0cd
TT
1833static int
1834extract_cu_value (const char *bytes, ULONGEST *result)
1835{
1836 if (sizeof (ULONGEST) < 8)
1837 {
1838 int i;
1839
1840 /* Ignore the upper 4 bytes if they are all zero. */
1841 for (i = 0; i < 4; ++i)
1842 if (bytes[i + 4] != 0)
1843 return 0;
1844
1845 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1846 }
1847 else
1848 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1849 return 1;
1850}
1851
1852/* Read the CU list from the mapped index, and use it to create all
1853 the CU objects for this objfile. Return 0 if something went wrong,
1854 1 if everything went ok. */
2fdf6df6 1855
9291a0cd 1856static int
1fd400ff
TT
1857create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1858 offset_type cu_list_elements)
9291a0cd
TT
1859{
1860 offset_type i;
9291a0cd
TT
1861
1862 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1863 dwarf2_per_objfile->all_comp_units
1864 = obstack_alloc (&objfile->objfile_obstack,
1865 dwarf2_per_objfile->n_comp_units
1866 * sizeof (struct dwarf2_per_cu_data *));
1867
1868 for (i = 0; i < cu_list_elements; i += 2)
1869 {
1870 struct dwarf2_per_cu_data *the_cu;
1871 ULONGEST offset, length;
1872
1873 if (!extract_cu_value (cu_list, &offset)
1874 || !extract_cu_value (cu_list + 8, &length))
1875 return 0;
1876 cu_list += 2 * 8;
1877
1878 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1879 struct dwarf2_per_cu_data);
1880 the_cu->offset = offset;
1881 the_cu->length = length;
1882 the_cu->objfile = objfile;
1883 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1884 struct dwarf2_per_cu_quick_data);
1885 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1886 }
1887
1888 return 1;
1889}
1890
1fd400ff 1891/* Create the signatured type hash table from the index. */
673bfd45 1892
1fd400ff 1893static int
673bfd45
DE
1894create_signatured_type_table_from_index (struct objfile *objfile,
1895 const gdb_byte *bytes,
1896 offset_type elements)
1fd400ff
TT
1897{
1898 offset_type i;
673bfd45 1899 htab_t sig_types_hash;
1fd400ff
TT
1900
1901 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1902 dwarf2_per_objfile->type_comp_units
1903 = obstack_alloc (&objfile->objfile_obstack,
1904 dwarf2_per_objfile->n_type_comp_units
1905 * sizeof (struct dwarf2_per_cu_data *));
1906
673bfd45 1907 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1908
1909 for (i = 0; i < elements; i += 3)
1910 {
1911 struct signatured_type *type_sig;
1912 ULONGEST offset, type_offset, signature;
1913 void **slot;
1914
1915 if (!extract_cu_value (bytes, &offset)
1916 || !extract_cu_value (bytes + 8, &type_offset))
1917 return 0;
1918 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1919 bytes += 3 * 8;
1920
1921 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1922 struct signatured_type);
1923 type_sig->signature = signature;
1fd400ff
TT
1924 type_sig->type_offset = type_offset;
1925 type_sig->per_cu.from_debug_types = 1;
1926 type_sig->per_cu.offset = offset;
1927 type_sig->per_cu.objfile = objfile;
1928 type_sig->per_cu.v.quick
1929 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1930 struct dwarf2_per_cu_quick_data);
1931
673bfd45 1932 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1933 *slot = type_sig;
1934
1935 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1936 }
1937
673bfd45 1938 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1939
1940 return 1;
1941}
1942
9291a0cd
TT
1943/* Read the address map data from the mapped index, and use it to
1944 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1945
9291a0cd
TT
1946static void
1947create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1948{
1949 const gdb_byte *iter, *end;
1950 struct obstack temp_obstack;
1951 struct addrmap *mutable_map;
1952 struct cleanup *cleanup;
1953 CORE_ADDR baseaddr;
1954
1955 obstack_init (&temp_obstack);
1956 cleanup = make_cleanup_obstack_free (&temp_obstack);
1957 mutable_map = addrmap_create_mutable (&temp_obstack);
1958
1959 iter = index->address_table;
1960 end = iter + index->address_table_size;
1961
1962 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1963
1964 while (iter < end)
1965 {
1966 ULONGEST hi, lo, cu_index;
1967 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1968 iter += 8;
1969 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1970 iter += 8;
1971 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1972 iter += 4;
1973
1974 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1975 dw2_get_cu (cu_index));
9291a0cd
TT
1976 }
1977
1978 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1979 &objfile->objfile_obstack);
1980 do_cleanups (cleanup);
1981}
1982
59d7bcaf
JK
1983/* The hash function for strings in the mapped index. This is the same as
1984 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
1985 implementation. This is necessary because the hash function is tied to the
1986 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
1987 SYMBOL_HASH_NEXT.
1988
1989 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 1990
9291a0cd 1991static hashval_t
559a7a62 1992mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
1993{
1994 const unsigned char *str = (const unsigned char *) p;
1995 hashval_t r = 0;
1996 unsigned char c;
1997
1998 while ((c = *str++) != 0)
559a7a62
JK
1999 {
2000 if (index_version >= 5)
2001 c = tolower (c);
2002 r = r * 67 + c - 113;
2003 }
9291a0cd
TT
2004
2005 return r;
2006}
2007
2008/* Find a slot in the mapped index INDEX for the object named NAME.
2009 If NAME is found, set *VEC_OUT to point to the CU vector in the
2010 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2011
9291a0cd
TT
2012static int
2013find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2014 offset_type **vec_out)
2015{
0cf03b49
JK
2016 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2017 offset_type hash;
9291a0cd 2018 offset_type slot, step;
559a7a62 2019 int (*cmp) (const char *, const char *);
9291a0cd 2020
0cf03b49
JK
2021 if (current_language->la_language == language_cplus
2022 || current_language->la_language == language_java
2023 || current_language->la_language == language_fortran)
2024 {
2025 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2026 not contain any. */
2027 const char *paren = strchr (name, '(');
2028
2029 if (paren)
2030 {
2031 char *dup;
2032
2033 dup = xmalloc (paren - name + 1);
2034 memcpy (dup, name, paren - name);
2035 dup[paren - name] = 0;
2036
2037 make_cleanup (xfree, dup);
2038 name = dup;
2039 }
2040 }
2041
559a7a62
JK
2042 /* Index version 4 did not support case insensitive searches. But the
2043 indexes for case insensitive languages are built in lowercase, therefore
2044 simulate our NAME being searched is also lowercased. */
2045 hash = mapped_index_string_hash ((index->version == 4
2046 && case_sensitivity == case_sensitive_off
2047 ? 5 : index->version),
2048 name);
2049
3876f04e
DE
2050 slot = hash & (index->symbol_table_slots - 1);
2051 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2052 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2053
2054 for (;;)
2055 {
2056 /* Convert a slot number to an offset into the table. */
2057 offset_type i = 2 * slot;
2058 const char *str;
3876f04e 2059 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2060 {
2061 do_cleanups (back_to);
2062 return 0;
2063 }
9291a0cd 2064
3876f04e 2065 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2066 if (!cmp (name, str))
9291a0cd
TT
2067 {
2068 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2069 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2070 do_cleanups (back_to);
9291a0cd
TT
2071 return 1;
2072 }
2073
3876f04e 2074 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2075 }
2076}
2077
2078/* Read the index file. If everything went ok, initialize the "quick"
2079 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2080
9291a0cd
TT
2081static int
2082dwarf2_read_index (struct objfile *objfile)
2083{
9291a0cd
TT
2084 char *addr;
2085 struct mapped_index *map;
b3b272e1 2086 offset_type *metadata;
ac0b195c
KW
2087 const gdb_byte *cu_list;
2088 const gdb_byte *types_list = NULL;
2089 offset_type version, cu_list_elements;
2090 offset_type types_list_elements = 0;
1fd400ff 2091 int i;
9291a0cd 2092
9e0ac564 2093 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2094 return 0;
82430852
JK
2095
2096 /* Older elfutils strip versions could keep the section in the main
2097 executable while splitting it for the separate debug info file. */
2098 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2099 & SEC_HAS_CONTENTS) == 0)
2100 return 0;
2101
9291a0cd
TT
2102 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2103
2104 addr = dwarf2_per_objfile->gdb_index.buffer;
2105 /* Version check. */
1fd400ff 2106 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2107 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2108 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2109 contained incomplete addrmap. So, it seems better to just ignore such
559a7a62
JK
2110 indices. Index version 4 uses a different hash function than index
2111 version 5 and later. */
831adc1f 2112 if (version < 4)
9291a0cd 2113 return 0;
594e8718
JK
2114 /* Indexes with higher version than the one supported by GDB may be no
2115 longer backward compatible. */
559a7a62 2116 if (version > 5)
594e8718 2117 return 0;
9291a0cd
TT
2118
2119 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2120 map->version = version;
b3b272e1 2121 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2122
2123 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2124
2125 i = 0;
2126 cu_list = addr + MAYBE_SWAP (metadata[i]);
2127 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2128 / 8);
1fd400ff
TT
2129 ++i;
2130
987d643c
TT
2131 types_list = addr + MAYBE_SWAP (metadata[i]);
2132 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2133 - MAYBE_SWAP (metadata[i]))
2134 / 8);
2135 ++i;
1fd400ff
TT
2136
2137 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2138 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2139 - MAYBE_SWAP (metadata[i]));
2140 ++i;
2141
3876f04e
DE
2142 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2143 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2144 - MAYBE_SWAP (metadata[i]))
2145 / (2 * sizeof (offset_type)));
1fd400ff 2146 ++i;
9291a0cd 2147
1fd400ff
TT
2148 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2149
2150 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2151 return 0;
2152
987d643c 2153 if (types_list_elements
673bfd45
DE
2154 && !create_signatured_type_table_from_index (objfile, types_list,
2155 types_list_elements))
9291a0cd
TT
2156 return 0;
2157
2158 create_addrmap_from_index (objfile, map);
2159
2160 dwarf2_per_objfile->index_table = map;
2161 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2162 dwarf2_per_objfile->quick_file_names_table =
2163 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2164
2165 return 1;
2166}
2167
2168/* A helper for the "quick" functions which sets the global
2169 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2170
9291a0cd
TT
2171static void
2172dw2_setup (struct objfile *objfile)
2173{
2174 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2175 gdb_assert (dwarf2_per_objfile);
2176}
2177
2178/* A helper for the "quick" functions which attempts to read the line
2179 table for THIS_CU. */
2fdf6df6 2180
7b9f3c50
DE
2181static struct quick_file_names *
2182dw2_get_file_names (struct objfile *objfile,
2183 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2184{
2185 bfd *abfd = objfile->obfd;
7b9f3c50 2186 struct line_header *lh;
9291a0cd
TT
2187 struct attribute *attr;
2188 struct cleanup *cleanups;
2189 struct die_info *comp_unit_die;
36374493 2190 struct dwarf2_section_info* sec;
9291a0cd
TT
2191 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2192 int has_children, i;
2193 struct dwarf2_cu cu;
2194 unsigned int bytes_read, buffer_size;
2195 struct die_reader_specs reader_specs;
2196 char *name, *comp_dir;
7b9f3c50
DE
2197 void **slot;
2198 struct quick_file_names *qfn;
2199 unsigned int line_offset;
9291a0cd 2200
7b9f3c50
DE
2201 if (this_cu->v.quick->file_names != NULL)
2202 return this_cu->v.quick->file_names;
2203 /* If we know there is no line data, no point in looking again. */
2204 if (this_cu->v.quick->no_file_data)
2205 return NULL;
9291a0cd 2206
9816fde3 2207 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2208 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2209
36374493
DE
2210 if (this_cu->from_debug_types)
2211 sec = &dwarf2_per_objfile->types;
2212 else
2213 sec = &dwarf2_per_objfile->info;
2214 dwarf2_read_section (objfile, sec);
2215 buffer_size = sec->size;
2216 buffer = sec->buffer;
9291a0cd
TT
2217 info_ptr = buffer + this_cu->offset;
2218 beg_of_comp_unit = info_ptr;
2219
2220 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2221 buffer, buffer_size,
2222 abfd);
2223
2224 /* Complete the cu_header. */
2225 cu.header.offset = beg_of_comp_unit - buffer;
2226 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2227
2228 this_cu->cu = &cu;
2229 cu.per_cu = this_cu;
2230
2231 dwarf2_read_abbrevs (abfd, &cu);
2232 make_cleanup (dwarf2_free_abbrev_table, &cu);
2233
2234 if (this_cu->from_debug_types)
2235 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2236 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2237 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2238 &has_children);
9291a0cd 2239
7b9f3c50
DE
2240 lh = NULL;
2241 slot = NULL;
2242 line_offset = 0;
9291a0cd
TT
2243 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2244 if (attr)
2245 {
7b9f3c50
DE
2246 struct quick_file_names find_entry;
2247
2248 line_offset = DW_UNSND (attr);
2249
2250 /* We may have already read in this line header (TU line header sharing).
2251 If we have we're done. */
2252 find_entry.offset = line_offset;
2253 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2254 &find_entry, INSERT);
2255 if (*slot != NULL)
2256 {
2257 do_cleanups (cleanups);
2258 this_cu->v.quick->file_names = *slot;
2259 return *slot;
2260 }
2261
9291a0cd
TT
2262 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2263 }
2264 if (lh == NULL)
2265 {
2266 do_cleanups (cleanups);
7b9f3c50
DE
2267 this_cu->v.quick->no_file_data = 1;
2268 return NULL;
9291a0cd
TT
2269 }
2270
7b9f3c50
DE
2271 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2272 qfn->offset = line_offset;
2273 gdb_assert (slot != NULL);
2274 *slot = qfn;
9291a0cd 2275
7b9f3c50 2276 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2277
7b9f3c50
DE
2278 qfn->num_file_names = lh->num_file_names;
2279 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2280 lh->num_file_names * sizeof (char *));
9291a0cd 2281 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2282 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2283 qfn->real_names = NULL;
9291a0cd 2284
7b9f3c50 2285 free_line_header (lh);
9291a0cd 2286 do_cleanups (cleanups);
7b9f3c50
DE
2287
2288 this_cu->v.quick->file_names = qfn;
2289 return qfn;
9291a0cd
TT
2290}
2291
2292/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2293 real path for a given file name from the line table. */
2fdf6df6 2294
9291a0cd 2295static const char *
7b9f3c50
DE
2296dw2_get_real_path (struct objfile *objfile,
2297 struct quick_file_names *qfn, int index)
9291a0cd 2298{
7b9f3c50
DE
2299 if (qfn->real_names == NULL)
2300 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2301 qfn->num_file_names, sizeof (char *));
9291a0cd 2302
7b9f3c50
DE
2303 if (qfn->real_names[index] == NULL)
2304 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2305
7b9f3c50 2306 return qfn->real_names[index];
9291a0cd
TT
2307}
2308
2309static struct symtab *
2310dw2_find_last_source_symtab (struct objfile *objfile)
2311{
2312 int index;
ae2de4f8 2313
9291a0cd
TT
2314 dw2_setup (objfile);
2315 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2316 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2317}
2318
7b9f3c50
DE
2319/* Traversal function for dw2_forget_cached_source_info. */
2320
2321static int
2322dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2323{
7b9f3c50 2324 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2325
7b9f3c50 2326 if (file_data->real_names)
9291a0cd 2327 {
7b9f3c50 2328 int i;
9291a0cd 2329
7b9f3c50 2330 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2331 {
7b9f3c50
DE
2332 xfree ((void*) file_data->real_names[i]);
2333 file_data->real_names[i] = NULL;
9291a0cd
TT
2334 }
2335 }
7b9f3c50
DE
2336
2337 return 1;
2338}
2339
2340static void
2341dw2_forget_cached_source_info (struct objfile *objfile)
2342{
2343 dw2_setup (objfile);
2344
2345 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2346 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2347}
2348
2349static int
2350dw2_lookup_symtab (struct objfile *objfile, const char *name,
2351 const char *full_path, const char *real_path,
2352 struct symtab **result)
2353{
2354 int i;
2355 int check_basename = lbasename (name) == name;
2356 struct dwarf2_per_cu_data *base_cu = NULL;
2357
2358 dw2_setup (objfile);
ae2de4f8 2359
1fd400ff
TT
2360 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2361 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2362 {
2363 int j;
e254ef6a 2364 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2365 struct quick_file_names *file_data;
9291a0cd 2366
e254ef6a 2367 if (per_cu->v.quick->symtab)
9291a0cd
TT
2368 continue;
2369
7b9f3c50
DE
2370 file_data = dw2_get_file_names (objfile, per_cu);
2371 if (file_data == NULL)
9291a0cd
TT
2372 continue;
2373
7b9f3c50 2374 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2375 {
7b9f3c50 2376 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2377
2378 if (FILENAME_CMP (name, this_name) == 0)
2379 {
e254ef6a 2380 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2381 return 1;
2382 }
2383
2384 if (check_basename && ! base_cu
2385 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2386 base_cu = per_cu;
9291a0cd
TT
2387
2388 if (full_path != NULL)
2389 {
7b9f3c50
DE
2390 const char *this_real_name = dw2_get_real_path (objfile,
2391 file_data, j);
9291a0cd 2392
7b9f3c50
DE
2393 if (this_real_name != NULL
2394 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2395 {
e254ef6a 2396 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2397 return 1;
2398 }
2399 }
2400
2401 if (real_path != NULL)
2402 {
7b9f3c50
DE
2403 const char *this_real_name = dw2_get_real_path (objfile,
2404 file_data, j);
9291a0cd 2405
7b9f3c50
DE
2406 if (this_real_name != NULL
2407 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2408 {
74dd2ca6
DE
2409 *result = dw2_instantiate_symtab (objfile, per_cu);
2410 return 1;
9291a0cd
TT
2411 }
2412 }
2413 }
2414 }
2415
2416 if (base_cu)
2417 {
2418 *result = dw2_instantiate_symtab (objfile, base_cu);
2419 return 1;
2420 }
2421
2422 return 0;
2423}
2424
2425static struct symtab *
2426dw2_lookup_symbol (struct objfile *objfile, int block_index,
2427 const char *name, domain_enum domain)
2428{
774b6a14 2429 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2430 instead. */
2431 return NULL;
2432}
2433
2434/* A helper function that expands all symtabs that hold an object
2435 named NAME. */
2fdf6df6 2436
9291a0cd
TT
2437static void
2438dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2439{
2440 dw2_setup (objfile);
2441
ae2de4f8 2442 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2443 if (dwarf2_per_objfile->index_table)
2444 {
2445 offset_type *vec;
2446
2447 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2448 name, &vec))
2449 {
2450 offset_type i, len = MAYBE_SWAP (*vec);
2451 for (i = 0; i < len; ++i)
2452 {
2453 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2454 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2455
e254ef6a 2456 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2457 }
2458 }
2459 }
2460}
2461
774b6a14
TT
2462static void
2463dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2464 enum block_enum block_kind, const char *name,
774b6a14 2465 domain_enum domain)
9291a0cd 2466{
774b6a14 2467 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2468}
2469
2470static void
2471dw2_print_stats (struct objfile *objfile)
2472{
2473 int i, count;
2474
2475 dw2_setup (objfile);
2476 count = 0;
1fd400ff
TT
2477 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2478 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2479 {
e254ef6a 2480 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2481
e254ef6a 2482 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2483 ++count;
2484 }
2485 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2486}
2487
2488static void
2489dw2_dump (struct objfile *objfile)
2490{
2491 /* Nothing worth printing. */
2492}
2493
2494static void
2495dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2496 struct section_offsets *delta)
2497{
2498 /* There's nothing to relocate here. */
2499}
2500
2501static void
2502dw2_expand_symtabs_for_function (struct objfile *objfile,
2503 const char *func_name)
2504{
2505 dw2_do_expand_symtabs_matching (objfile, func_name);
2506}
2507
2508static void
2509dw2_expand_all_symtabs (struct objfile *objfile)
2510{
2511 int i;
2512
2513 dw2_setup (objfile);
1fd400ff
TT
2514
2515 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2516 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2517 {
e254ef6a 2518 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2519
e254ef6a 2520 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2521 }
2522}
2523
2524static void
2525dw2_expand_symtabs_with_filename (struct objfile *objfile,
2526 const char *filename)
2527{
2528 int i;
2529
2530 dw2_setup (objfile);
d4637a04
DE
2531
2532 /* We don't need to consider type units here.
2533 This is only called for examining code, e.g. expand_line_sal.
2534 There can be an order of magnitude (or more) more type units
2535 than comp units, and we avoid them if we can. */
2536
2537 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2538 {
2539 int j;
e254ef6a 2540 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2541 struct quick_file_names *file_data;
9291a0cd 2542
e254ef6a 2543 if (per_cu->v.quick->symtab)
9291a0cd
TT
2544 continue;
2545
7b9f3c50
DE
2546 file_data = dw2_get_file_names (objfile, per_cu);
2547 if (file_data == NULL)
9291a0cd
TT
2548 continue;
2549
7b9f3c50 2550 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2551 {
7b9f3c50 2552 const char *this_name = file_data->file_names[j];
1ef75ecc 2553 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2554 {
e254ef6a 2555 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2556 break;
2557 }
2558 }
2559 }
2560}
2561
dd786858 2562static const char *
9291a0cd
TT
2563dw2_find_symbol_file (struct objfile *objfile, const char *name)
2564{
e254ef6a 2565 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2566 offset_type *vec;
7b9f3c50 2567 struct quick_file_names *file_data;
9291a0cd
TT
2568
2569 dw2_setup (objfile);
2570
ae2de4f8 2571 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2572 if (!dwarf2_per_objfile->index_table)
2573 return NULL;
2574
2575 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2576 name, &vec))
2577 return NULL;
2578
2579 /* Note that this just looks at the very first one named NAME -- but
2580 actually we are looking for a function. find_main_filename
2581 should be rewritten so that it doesn't require a custom hook. It
2582 could just use the ordinary symbol tables. */
2583 /* vec[0] is the length, which must always be >0. */
e254ef6a 2584 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2585
7b9f3c50
DE
2586 file_data = dw2_get_file_names (objfile, per_cu);
2587 if (file_data == NULL)
9291a0cd
TT
2588 return NULL;
2589
7b9f3c50 2590 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2591}
2592
2593static void
40658b94
PH
2594dw2_map_matching_symbols (const char * name, domain_enum namespace,
2595 struct objfile *objfile, int global,
2596 int (*callback) (struct block *,
2597 struct symbol *, void *),
2edb89d3
JK
2598 void *data, symbol_compare_ftype *match,
2599 symbol_compare_ftype *ordered_compare)
9291a0cd 2600{
40658b94 2601 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2602 current language is Ada for a non-Ada objfile using GNU index. As Ada
2603 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2604}
2605
2606static void
2607dw2_expand_symtabs_matching (struct objfile *objfile,
2608 int (*file_matcher) (const char *, void *),
2609 int (*name_matcher) (const char *, void *),
8903c50d 2610 enum search_domain kind,
9291a0cd
TT
2611 void *data)
2612{
2613 int i;
2614 offset_type iter;
4b5246aa 2615 struct mapped_index *index;
9291a0cd
TT
2616
2617 dw2_setup (objfile);
ae2de4f8
DE
2618
2619 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2620 if (!dwarf2_per_objfile->index_table)
2621 return;
4b5246aa 2622 index = dwarf2_per_objfile->index_table;
9291a0cd 2623
7b08b9eb
JK
2624 if (file_matcher != NULL)
2625 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2626 + dwarf2_per_objfile->n_type_comp_units); ++i)
2627 {
2628 int j;
2629 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2630 struct quick_file_names *file_data;
2631
2632 per_cu->v.quick->mark = 0;
2633 if (per_cu->v.quick->symtab)
2634 continue;
2635
2636 file_data = dw2_get_file_names (objfile, per_cu);
2637 if (file_data == NULL)
2638 continue;
2639
2640 for (j = 0; j < file_data->num_file_names; ++j)
2641 {
2642 if (file_matcher (file_data->file_names[j], data))
2643 {
2644 per_cu->v.quick->mark = 1;
2645 break;
2646 }
2647 }
2648 }
9291a0cd 2649
3876f04e 2650 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2651 {
2652 offset_type idx = 2 * iter;
2653 const char *name;
2654 offset_type *vec, vec_len, vec_idx;
2655
3876f04e 2656 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2657 continue;
2658
3876f04e 2659 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2660
2661 if (! (*name_matcher) (name, data))
2662 continue;
2663
2664 /* The name was matched, now expand corresponding CUs that were
2665 marked. */
4b5246aa 2666 vec = (offset_type *) (index->constant_pool
3876f04e 2667 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2668 vec_len = MAYBE_SWAP (vec[0]);
2669 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2670 {
e254ef6a 2671 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2672
e254ef6a 2673 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2674 if (file_matcher == NULL || per_cu->v.quick->mark)
e254ef6a 2675 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2676 }
2677 }
2678}
2679
2680static struct symtab *
2681dw2_find_pc_sect_symtab (struct objfile *objfile,
2682 struct minimal_symbol *msymbol,
2683 CORE_ADDR pc,
2684 struct obj_section *section,
2685 int warn_if_readin)
2686{
2687 struct dwarf2_per_cu_data *data;
2688
2689 dw2_setup (objfile);
2690
2691 if (!objfile->psymtabs_addrmap)
2692 return NULL;
2693
2694 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2695 if (!data)
2696 return NULL;
2697
2698 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2699 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2700 paddress (get_objfile_arch (objfile), pc));
2701
2702 return dw2_instantiate_symtab (objfile, data);
2703}
2704
9291a0cd
TT
2705static void
2706dw2_map_symbol_filenames (struct objfile *objfile,
2707 void (*fun) (const char *, const char *, void *),
2708 void *data)
2709{
2710 int i;
2711
2712 dw2_setup (objfile);
ae2de4f8 2713
1fd400ff
TT
2714 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2715 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2716 {
2717 int j;
e254ef6a 2718 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2719 struct quick_file_names *file_data;
9291a0cd 2720
e254ef6a 2721 if (per_cu->v.quick->symtab)
9291a0cd
TT
2722 continue;
2723
7b9f3c50
DE
2724 file_data = dw2_get_file_names (objfile, per_cu);
2725 if (file_data == NULL)
9291a0cd
TT
2726 continue;
2727
7b9f3c50 2728 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2729 {
7b9f3c50
DE
2730 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2731 j);
2732 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2733 }
2734 }
2735}
2736
2737static int
2738dw2_has_symbols (struct objfile *objfile)
2739{
2740 return 1;
2741}
2742
2743const struct quick_symbol_functions dwarf2_gdb_index_functions =
2744{
2745 dw2_has_symbols,
2746 dw2_find_last_source_symtab,
2747 dw2_forget_cached_source_info,
2748 dw2_lookup_symtab,
2749 dw2_lookup_symbol,
774b6a14 2750 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2751 dw2_print_stats,
2752 dw2_dump,
2753 dw2_relocate,
2754 dw2_expand_symtabs_for_function,
2755 dw2_expand_all_symtabs,
2756 dw2_expand_symtabs_with_filename,
2757 dw2_find_symbol_file,
40658b94 2758 dw2_map_matching_symbols,
9291a0cd
TT
2759 dw2_expand_symtabs_matching,
2760 dw2_find_pc_sect_symtab,
9291a0cd
TT
2761 dw2_map_symbol_filenames
2762};
2763
2764/* Initialize for reading DWARF for this objfile. Return 0 if this
2765 file will use psymtabs, or 1 if using the GNU index. */
2766
2767int
2768dwarf2_initialize_objfile (struct objfile *objfile)
2769{
2770 /* If we're about to read full symbols, don't bother with the
2771 indices. In this case we also don't care if some other debug
2772 format is making psymtabs, because they are all about to be
2773 expanded anyway. */
2774 if ((objfile->flags & OBJF_READNOW))
2775 {
2776 int i;
2777
2778 dwarf2_per_objfile->using_index = 1;
2779 create_all_comp_units (objfile);
1fd400ff 2780 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2781 dwarf2_per_objfile->quick_file_names_table =
2782 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2783
1fd400ff
TT
2784 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2785 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2786 {
e254ef6a 2787 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2788
e254ef6a
DE
2789 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2790 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2791 }
2792
2793 /* Return 1 so that gdb sees the "quick" functions. However,
2794 these functions will be no-ops because we will have expanded
2795 all symtabs. */
2796 return 1;
2797 }
2798
2799 if (dwarf2_read_index (objfile))
2800 return 1;
2801
9291a0cd
TT
2802 return 0;
2803}
2804
2805\f
2806
dce234bc
PP
2807/* Build a partial symbol table. */
2808
2809void
f29dff0a 2810dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2811{
f29dff0a 2812 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2813 {
2814 init_psymbol_list (objfile, 1024);
2815 }
2816
d146bf1e 2817 dwarf2_build_psymtabs_hard (objfile);
c906108c 2818}
c906108c 2819
45452591
DE
2820/* Return TRUE if OFFSET is within CU_HEADER. */
2821
2822static inline int
2823offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2824{
2825 unsigned int bottom = cu_header->offset;
2826 unsigned int top = (cu_header->offset
2827 + cu_header->length
2828 + cu_header->initial_length_size);
9a619af0 2829
45452591
DE
2830 return (offset >= bottom && offset < top);
2831}
2832
93311388
DE
2833/* Read in the comp unit header information from the debug_info at info_ptr.
2834 NOTE: This leaves members offset, first_die_offset to be filled in
2835 by the caller. */
107d2387 2836
fe1b8b76 2837static gdb_byte *
107d2387 2838read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2839 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2840{
2841 int signed_addr;
891d2f0b 2842 unsigned int bytes_read;
c764a876
DE
2843
2844 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2845 cu_header->initial_length_size = bytes_read;
2846 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2847 info_ptr += bytes_read;
107d2387
AC
2848 cu_header->version = read_2_bytes (abfd, info_ptr);
2849 info_ptr += 2;
613e1657 2850 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2851 &bytes_read);
613e1657 2852 info_ptr += bytes_read;
107d2387
AC
2853 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2854 info_ptr += 1;
2855 signed_addr = bfd_get_sign_extend_vma (abfd);
2856 if (signed_addr < 0)
8e65ff28 2857 internal_error (__FILE__, __LINE__,
e2e0b3e5 2858 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2859 cu_header->signed_addr_p = signed_addr;
c764a876 2860
107d2387
AC
2861 return info_ptr;
2862}
2863
fe1b8b76
JB
2864static gdb_byte *
2865partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2866 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2867 bfd *abfd)
2868{
fe1b8b76 2869 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2870
2871 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2872
2dc7f7b3 2873 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2874 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2875 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2876 bfd_get_filename (abfd));
72bf9492 2877
9e0ac564
TT
2878 if (header->abbrev_offset
2879 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2880 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
2881 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2882 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2883 (long) header->abbrev_offset,
93311388 2884 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2885 bfd_get_filename (abfd));
2886
2887 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2888 > buffer + buffer_size)
8a3fe4f8
AC
2889 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2890 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2891 (long) header->length,
93311388 2892 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2893 bfd_get_filename (abfd));
2894
2895 return info_ptr;
2896}
2897
348e048f
DE
2898/* Read in the types comp unit header information from .debug_types entry at
2899 types_ptr. The result is a pointer to one past the end of the header. */
2900
2901static gdb_byte *
2902read_type_comp_unit_head (struct comp_unit_head *cu_header,
2903 ULONGEST *signature,
2904 gdb_byte *types_ptr, bfd *abfd)
2905{
348e048f
DE
2906 gdb_byte *initial_types_ptr = types_ptr;
2907
6e70227d 2908 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2909 &dwarf2_per_objfile->types);
348e048f
DE
2910 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2911
2912 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2913
2914 *signature = read_8_bytes (abfd, types_ptr);
2915 types_ptr += 8;
2916 types_ptr += cu_header->offset_size;
2917 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2918
2919 return types_ptr;
2920}
2921
aaa75496
JB
2922/* Allocate a new partial symtab for file named NAME and mark this new
2923 partial symtab as being an include of PST. */
2924
2925static void
2926dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2927 struct objfile *objfile)
2928{
2929 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2930
2931 subpst->section_offsets = pst->section_offsets;
2932 subpst->textlow = 0;
2933 subpst->texthigh = 0;
2934
2935 subpst->dependencies = (struct partial_symtab **)
2936 obstack_alloc (&objfile->objfile_obstack,
2937 sizeof (struct partial_symtab *));
2938 subpst->dependencies[0] = pst;
2939 subpst->number_of_dependencies = 1;
2940
2941 subpst->globals_offset = 0;
2942 subpst->n_global_syms = 0;
2943 subpst->statics_offset = 0;
2944 subpst->n_static_syms = 0;
2945 subpst->symtab = NULL;
2946 subpst->read_symtab = pst->read_symtab;
2947 subpst->readin = 0;
2948
2949 /* No private part is necessary for include psymtabs. This property
2950 can be used to differentiate between such include psymtabs and
10b3939b 2951 the regular ones. */
58a9656e 2952 subpst->read_symtab_private = NULL;
aaa75496
JB
2953}
2954
2955/* Read the Line Number Program data and extract the list of files
2956 included by the source file represented by PST. Build an include
d85a05f0 2957 partial symtab for each of these included files. */
aaa75496
JB
2958
2959static void
2960dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2961 struct die_info *die,
aaa75496
JB
2962 struct partial_symtab *pst)
2963{
2964 struct objfile *objfile = cu->objfile;
2965 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2966 struct line_header *lh = NULL;
2967 struct attribute *attr;
aaa75496 2968
d85a05f0
DJ
2969 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2970 if (attr)
2971 {
2972 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2973
d85a05f0
DJ
2974 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2975 }
aaa75496
JB
2976 if (lh == NULL)
2977 return; /* No linetable, so no includes. */
2978
c6da4cef
DE
2979 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2980 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2981
2982 free_line_header (lh);
2983}
2984
348e048f
DE
2985static hashval_t
2986hash_type_signature (const void *item)
2987{
2988 const struct signatured_type *type_sig = item;
9a619af0 2989
348e048f
DE
2990 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2991 return type_sig->signature;
2992}
2993
2994static int
2995eq_type_signature (const void *item_lhs, const void *item_rhs)
2996{
2997 const struct signatured_type *lhs = item_lhs;
2998 const struct signatured_type *rhs = item_rhs;
9a619af0 2999
348e048f
DE
3000 return lhs->signature == rhs->signature;
3001}
3002
1fd400ff
TT
3003/* Allocate a hash table for signatured types. */
3004
3005static htab_t
673bfd45 3006allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3007{
3008 return htab_create_alloc_ex (41,
3009 hash_type_signature,
3010 eq_type_signature,
3011 NULL,
3012 &objfile->objfile_obstack,
3013 hashtab_obstack_allocate,
3014 dummy_obstack_deallocate);
3015}
3016
3017/* A helper function to add a signatured type CU to a list. */
3018
3019static int
3020add_signatured_type_cu_to_list (void **slot, void *datum)
3021{
3022 struct signatured_type *sigt = *slot;
3023 struct dwarf2_per_cu_data ***datap = datum;
3024
3025 **datap = &sigt->per_cu;
3026 ++*datap;
3027
3028 return 1;
3029}
3030
348e048f
DE
3031/* Create the hash table of all entries in the .debug_types section.
3032 The result is zero if there is an error (e.g. missing .debug_types section),
3033 otherwise non-zero. */
3034
3035static int
3036create_debug_types_hash_table (struct objfile *objfile)
3037{
be391dca 3038 gdb_byte *info_ptr;
348e048f 3039 htab_t types_htab;
1fd400ff 3040 struct dwarf2_per_cu_data **iter;
348e048f 3041
be391dca
TT
3042 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3043 info_ptr = dwarf2_per_objfile->types.buffer;
3044
348e048f
DE
3045 if (info_ptr == NULL)
3046 {
3047 dwarf2_per_objfile->signatured_types = NULL;
3048 return 0;
3049 }
3050
673bfd45 3051 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
3052
3053 if (dwarf2_die_debug)
3054 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3055
3e43a32a
MS
3056 while (info_ptr < dwarf2_per_objfile->types.buffer
3057 + dwarf2_per_objfile->types.size)
348e048f
DE
3058 {
3059 unsigned int offset;
3060 unsigned int offset_size;
3061 unsigned int type_offset;
3062 unsigned int length, initial_length_size;
3063 unsigned short version;
3064 ULONGEST signature;
3065 struct signatured_type *type_sig;
3066 void **slot;
3067 gdb_byte *ptr = info_ptr;
3068
3069 offset = ptr - dwarf2_per_objfile->types.buffer;
3070
3071 /* We need to read the type's signature in order to build the hash
3072 table, but we don't need to read anything else just yet. */
3073
3074 /* Sanity check to ensure entire cu is present. */
3075 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3076 if (ptr + length + initial_length_size
3077 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3078 {
3079 complaint (&symfile_complaints,
3e43a32a
MS
3080 _("debug type entry runs off end "
3081 "of `.debug_types' section, ignored"));
348e048f
DE
3082 break;
3083 }
3084
3085 offset_size = initial_length_size == 4 ? 4 : 8;
3086 ptr += initial_length_size;
3087 version = bfd_get_16 (objfile->obfd, ptr);
3088 ptr += 2;
3089 ptr += offset_size; /* abbrev offset */
3090 ptr += 1; /* address size */
3091 signature = bfd_get_64 (objfile->obfd, ptr);
3092 ptr += 8;
3093 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3094
3095 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3096 memset (type_sig, 0, sizeof (*type_sig));
3097 type_sig->signature = signature;
348e048f 3098 type_sig->type_offset = type_offset;
ca1f3406 3099 type_sig->per_cu.objfile = objfile;
1fd400ff 3100 type_sig->per_cu.from_debug_types = 1;
b3c8eb43 3101 type_sig->per_cu.offset = offset;
348e048f
DE
3102
3103 slot = htab_find_slot (types_htab, type_sig, INSERT);
3104 gdb_assert (slot != NULL);
b3c8eb43
JK
3105 if (*slot != NULL)
3106 {
3107 const struct signatured_type *dup_sig = *slot;
3108
3109 complaint (&symfile_complaints,
3110 _("debug type entry at offset 0x%x is duplicate to the "
3111 "entry at offset 0x%x, signature 0x%s"),
3112 offset, dup_sig->per_cu.offset,
3113 phex (signature, sizeof (signature)));
3114 gdb_assert (signature == dup_sig->signature);
3115 }
348e048f
DE
3116 *slot = type_sig;
3117
3118 if (dwarf2_die_debug)
3119 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3120 offset, phex (signature, sizeof (signature)));
3121
3122 info_ptr = info_ptr + initial_length_size + length;
3123 }
3124
3125 dwarf2_per_objfile->signatured_types = types_htab;
3126
1fd400ff
TT
3127 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3128 dwarf2_per_objfile->type_comp_units
3129 = obstack_alloc (&objfile->objfile_obstack,
3130 dwarf2_per_objfile->n_type_comp_units
3131 * sizeof (struct dwarf2_per_cu_data *));
3132 iter = &dwarf2_per_objfile->type_comp_units[0];
3133 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3134 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3135 == dwarf2_per_objfile->n_type_comp_units);
3136
348e048f
DE
3137 return 1;
3138}
3139
3140/* Lookup a signature based type.
3141 Returns NULL if SIG is not present in the table. */
3142
3143static struct signatured_type *
3144lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3145{
3146 struct signatured_type find_entry, *entry;
3147
3148 if (dwarf2_per_objfile->signatured_types == NULL)
3149 {
3150 complaint (&symfile_complaints,
55f1336d 3151 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3152 return 0;
3153 }
3154
3155 find_entry.signature = sig;
3156 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3157 return entry;
3158}
3159
d85a05f0
DJ
3160/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3161
3162static void
3163init_cu_die_reader (struct die_reader_specs *reader,
3164 struct dwarf2_cu *cu)
3165{
3166 reader->abfd = cu->objfile->obfd;
3167 reader->cu = cu;
3168 if (cu->per_cu->from_debug_types)
be391dca
TT
3169 {
3170 gdb_assert (dwarf2_per_objfile->types.readin);
3171 reader->buffer = dwarf2_per_objfile->types.buffer;
3172 }
d85a05f0 3173 else
be391dca
TT
3174 {
3175 gdb_assert (dwarf2_per_objfile->info.readin);
3176 reader->buffer = dwarf2_per_objfile->info.buffer;
3177 }
d85a05f0
DJ
3178}
3179
3180/* Find the base address of the compilation unit for range lists and
3181 location lists. It will normally be specified by DW_AT_low_pc.
3182 In DWARF-3 draft 4, the base address could be overridden by
3183 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3184 compilation units with discontinuous ranges. */
3185
3186static void
3187dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3188{
3189 struct attribute *attr;
3190
3191 cu->base_known = 0;
3192 cu->base_address = 0;
3193
3194 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3195 if (attr)
3196 {
3197 cu->base_address = DW_ADDR (attr);
3198 cu->base_known = 1;
3199 }
3200 else
3201 {
3202 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3203 if (attr)
3204 {
3205 cu->base_address = DW_ADDR (attr);
3206 cu->base_known = 1;
3207 }
3208 }
3209}
3210
348e048f
DE
3211/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3212 to combine the common parts.
93311388 3213 Process a compilation unit for a psymtab.
348e048f
DE
3214 BUFFER is a pointer to the beginning of the dwarf section buffer,
3215 either .debug_info or debug_types.
93311388
DE
3216 INFO_PTR is a pointer to the start of the CU.
3217 Returns a pointer to the next CU. */
aaa75496 3218
93311388
DE
3219static gdb_byte *
3220process_psymtab_comp_unit (struct objfile *objfile,
3221 struct dwarf2_per_cu_data *this_cu,
3222 gdb_byte *buffer, gdb_byte *info_ptr,
3223 unsigned int buffer_size)
c906108c 3224{
c906108c 3225 bfd *abfd = objfile->obfd;
93311388 3226 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3227 struct die_info *comp_unit_die;
c906108c 3228 struct partial_symtab *pst;
5734ee8b 3229 CORE_ADDR baseaddr;
93311388
DE
3230 struct cleanup *back_to_inner;
3231 struct dwarf2_cu cu;
d85a05f0
DJ
3232 int has_children, has_pc_info;
3233 struct attribute *attr;
d85a05f0
DJ
3234 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3235 struct die_reader_specs reader_specs;
3e2a0cee 3236 const char *filename;
c906108c 3237
9816fde3 3238 init_one_comp_unit (&cu, objfile);
93311388 3239 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3240
93311388
DE
3241 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3242 buffer, buffer_size,
3243 abfd);
10b3939b 3244
93311388
DE
3245 /* Complete the cu_header. */
3246 cu.header.offset = beg_of_comp_unit - buffer;
3247 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3248
93311388 3249 cu.list_in_scope = &file_symbols;
af703f96 3250
328c9494
DJ
3251 /* If this compilation unit was already read in, free the
3252 cached copy in order to read it in again. This is
3253 necessary because we skipped some symbols when we first
3254 read in the compilation unit (see load_partial_dies).
3255 This problem could be avoided, but the benefit is
3256 unclear. */
3257 if (this_cu->cu != NULL)
3258 free_one_cached_comp_unit (this_cu->cu);
3259
3260 /* Note that this is a pointer to our stack frame, being
3261 added to a global data structure. It will be cleaned up
3262 in free_stack_comp_unit when we finish with this
3263 compilation unit. */
3264 this_cu->cu = &cu;
d85a05f0
DJ
3265 cu.per_cu = this_cu;
3266
93311388
DE
3267 /* Read the abbrevs for this compilation unit into a table. */
3268 dwarf2_read_abbrevs (abfd, &cu);
3269 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3270
93311388 3271 /* Read the compilation unit die. */
348e048f
DE
3272 if (this_cu->from_debug_types)
3273 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3274 init_cu_die_reader (&reader_specs, &cu);
3275 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3276 &has_children);
93311388 3277
348e048f
DE
3278 if (this_cu->from_debug_types)
3279 {
b3c8eb43
JK
3280 /* LENGTH has not been set yet for type units. */
3281 gdb_assert (this_cu->offset == cu.header.offset);
348e048f
DE
3282 this_cu->length = cu.header.length + cu.header.initial_length_size;
3283 }
d85a05f0 3284 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3285 {
93311388
DE
3286 info_ptr = (beg_of_comp_unit + cu.header.length
3287 + cu.header.initial_length_size);
3288 do_cleanups (back_to_inner);
3289 return info_ptr;
3290 }
72bf9492 3291
9816fde3 3292 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3293
93311388 3294 /* Allocate a new partial symbol table structure. */
d85a05f0 3295 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3296 if (attr == NULL || !DW_STRING (attr))
3297 filename = "";
3298 else
3299 filename = DW_STRING (attr);
93311388 3300 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3301 filename,
93311388
DE
3302 /* TEXTLOW and TEXTHIGH are set below. */
3303 0,
3304 objfile->global_psymbols.next,
3305 objfile->static_psymbols.next);
72bf9492 3306
d85a05f0
DJ
3307 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3308 if (attr != NULL)
3309 pst->dirname = DW_STRING (attr);
72bf9492 3310
e38df1d0 3311 pst->read_symtab_private = this_cu;
72bf9492 3312
93311388 3313 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3314
0963b4bd 3315 /* Store the function that reads in the rest of the symbol table. */
93311388 3316 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3317
9291a0cd 3318 this_cu->v.psymtab = pst;
c906108c 3319
d85a05f0
DJ
3320 dwarf2_find_base_address (comp_unit_die, &cu);
3321
93311388
DE
3322 /* Possibly set the default values of LOWPC and HIGHPC from
3323 `DW_AT_ranges'. */
d85a05f0
DJ
3324 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3325 &best_highpc, &cu, pst);
3326 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3327 /* Store the contiguous range if it is not empty; it can be empty for
3328 CUs with no code. */
3329 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3330 best_lowpc + baseaddr,
3331 best_highpc + baseaddr - 1, pst);
93311388
DE
3332
3333 /* Check if comp unit has_children.
3334 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3335 If not, there's no more debug_info for this comp unit. */
d85a05f0 3336 if (has_children)
93311388
DE
3337 {
3338 struct partial_die_info *first_die;
3339 CORE_ADDR lowpc, highpc;
31ffec48 3340
93311388
DE
3341 lowpc = ((CORE_ADDR) -1);
3342 highpc = ((CORE_ADDR) 0);
c906108c 3343
93311388 3344 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3345
93311388 3346 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3347 ! has_pc_info, &cu);
57c22c6c 3348
93311388
DE
3349 /* If we didn't find a lowpc, set it to highpc to avoid
3350 complaints from `maint check'. */
3351 if (lowpc == ((CORE_ADDR) -1))
3352 lowpc = highpc;
10b3939b 3353
93311388
DE
3354 /* If the compilation unit didn't have an explicit address range,
3355 then use the information extracted from its child dies. */
d85a05f0 3356 if (! has_pc_info)
93311388 3357 {
d85a05f0
DJ
3358 best_lowpc = lowpc;
3359 best_highpc = highpc;
93311388
DE
3360 }
3361 }
d85a05f0
DJ
3362 pst->textlow = best_lowpc + baseaddr;
3363 pst->texthigh = best_highpc + baseaddr;
c906108c 3364
93311388
DE
3365 pst->n_global_syms = objfile->global_psymbols.next -
3366 (objfile->global_psymbols.list + pst->globals_offset);
3367 pst->n_static_syms = objfile->static_psymbols.next -
3368 (objfile->static_psymbols.list + pst->statics_offset);
3369 sort_pst_symbols (pst);
c906108c 3370
93311388
DE
3371 info_ptr = (beg_of_comp_unit + cu.header.length
3372 + cu.header.initial_length_size);
ae038cb0 3373
348e048f
DE
3374 if (this_cu->from_debug_types)
3375 {
3376 /* It's not clear we want to do anything with stmt lists here.
3377 Waiting to see what gcc ultimately does. */
3378 }
d85a05f0 3379 else
93311388
DE
3380 {
3381 /* Get the list of files included in the current compilation unit,
3382 and build a psymtab for each of them. */
d85a05f0 3383 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3384 }
ae038cb0 3385
93311388 3386 do_cleanups (back_to_inner);
ae038cb0 3387
93311388
DE
3388 return info_ptr;
3389}
ff013f42 3390
348e048f
DE
3391/* Traversal function for htab_traverse_noresize.
3392 Process one .debug_types comp-unit. */
3393
3394static int
3395process_type_comp_unit (void **slot, void *info)
3396{
3397 struct signatured_type *entry = (struct signatured_type *) *slot;
3398 struct objfile *objfile = (struct objfile *) info;
3399 struct dwarf2_per_cu_data *this_cu;
3400
3401 this_cu = &entry->per_cu;
348e048f 3402
be391dca 3403 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3404 process_psymtab_comp_unit (objfile, this_cu,
3405 dwarf2_per_objfile->types.buffer,
b3c8eb43 3406 dwarf2_per_objfile->types.buffer + this_cu->offset,
348e048f
DE
3407 dwarf2_per_objfile->types.size);
3408
3409 return 1;
3410}
3411
3412/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3413 Build partial symbol tables for the .debug_types comp-units. */
3414
3415static void
3416build_type_psymtabs (struct objfile *objfile)
3417{
3418 if (! create_debug_types_hash_table (objfile))
3419 return;
3420
3421 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3422 process_type_comp_unit, objfile);
3423}
3424
60606b2c
TT
3425/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3426
3427static void
3428psymtabs_addrmap_cleanup (void *o)
3429{
3430 struct objfile *objfile = o;
ec61707d 3431
60606b2c
TT
3432 objfile->psymtabs_addrmap = NULL;
3433}
3434
93311388
DE
3435/* Build the partial symbol table by doing a quick pass through the
3436 .debug_info and .debug_abbrev sections. */
72bf9492 3437
93311388 3438static void
c67a9c90 3439dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3440{
93311388 3441 gdb_byte *info_ptr;
60606b2c
TT
3442 struct cleanup *back_to, *addrmap_cleanup;
3443 struct obstack temp_obstack;
93311388 3444
98bfdba5
PA
3445 dwarf2_per_objfile->reading_partial_symbols = 1;
3446
be391dca 3447 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3448 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3449
93311388
DE
3450 /* Any cached compilation units will be linked by the per-objfile
3451 read_in_chain. Make sure to free them when we're done. */
3452 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3453
348e048f
DE
3454 build_type_psymtabs (objfile);
3455
93311388 3456 create_all_comp_units (objfile);
c906108c 3457
60606b2c
TT
3458 /* Create a temporary address map on a temporary obstack. We later
3459 copy this to the final obstack. */
3460 obstack_init (&temp_obstack);
3461 make_cleanup_obstack_free (&temp_obstack);
3462 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3463 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3464
93311388
DE
3465 /* Since the objects we're extracting from .debug_info vary in
3466 length, only the individual functions to extract them (like
3467 read_comp_unit_head and load_partial_die) can really know whether
3468 the buffer is large enough to hold another complete object.
c906108c 3469
93311388
DE
3470 At the moment, they don't actually check that. If .debug_info
3471 holds just one extra byte after the last compilation unit's dies,
3472 then read_comp_unit_head will happily read off the end of the
3473 buffer. read_partial_die is similarly casual. Those functions
3474 should be fixed.
c906108c 3475
93311388
DE
3476 For this loop condition, simply checking whether there's any data
3477 left at all should be sufficient. */
c906108c 3478
93311388
DE
3479 while (info_ptr < (dwarf2_per_objfile->info.buffer
3480 + dwarf2_per_objfile->info.size))
3481 {
3482 struct dwarf2_per_cu_data *this_cu;
dd373385 3483
3e43a32a
MS
3484 this_cu = dwarf2_find_comp_unit (info_ptr
3485 - dwarf2_per_objfile->info.buffer,
93311388 3486 objfile);
aaa75496 3487
93311388
DE
3488 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3489 dwarf2_per_objfile->info.buffer,
3490 info_ptr,
3491 dwarf2_per_objfile->info.size);
c906108c 3492 }
ff013f42
JK
3493
3494 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3495 &objfile->objfile_obstack);
60606b2c 3496 discard_cleanups (addrmap_cleanup);
ff013f42 3497
ae038cb0
DJ
3498 do_cleanups (back_to);
3499}
3500
93311388 3501/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3502
3503static void
93311388
DE
3504load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3505 struct objfile *objfile)
ae038cb0
DJ
3506{
3507 bfd *abfd = objfile->obfd;
fe1b8b76 3508 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3509 struct die_info *comp_unit_die;
ae038cb0 3510 struct dwarf2_cu *cu;
1d9ec526 3511 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3512 int has_children;
3513 struct die_reader_specs reader_specs;
98bfdba5 3514 int read_cu = 0;
ae038cb0 3515
348e048f
DE
3516 gdb_assert (! this_cu->from_debug_types);
3517
be391dca 3518 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3519 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3520 beg_of_comp_unit = info_ptr;
3521
98bfdba5
PA
3522 if (this_cu->cu == NULL)
3523 {
9816fde3
JK
3524 cu = xmalloc (sizeof (*cu));
3525 init_one_comp_unit (cu, objfile);
ae038cb0 3526
98bfdba5 3527 read_cu = 1;
ae038cb0 3528
98bfdba5
PA
3529 /* If an error occurs while loading, release our storage. */
3530 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3531
98bfdba5
PA
3532 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3533 dwarf2_per_objfile->info.buffer,
3534 dwarf2_per_objfile->info.size,
3535 abfd);
ae038cb0 3536
98bfdba5
PA
3537 /* Complete the cu_header. */
3538 cu->header.offset = this_cu->offset;
3539 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3540
3541 /* Link this compilation unit into the compilation unit tree. */
3542 this_cu->cu = cu;
3543 cu->per_cu = this_cu;
98bfdba5
PA
3544
3545 /* Link this CU into read_in_chain. */
3546 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3547 dwarf2_per_objfile->read_in_chain = this_cu;
3548 }
3549 else
3550 {
3551 cu = this_cu->cu;
3552 info_ptr += cu->header.first_die_offset;
3553 }
ae038cb0
DJ
3554
3555 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3556 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3557 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3558 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3559
3560 /* Read the compilation unit die. */
d85a05f0
DJ
3561 init_cu_die_reader (&reader_specs, cu);
3562 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3563 &has_children);
ae038cb0 3564
9816fde3 3565 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3566
ae038cb0
DJ
3567 /* Check if comp unit has_children.
3568 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3569 If not, there's no more debug_info for this comp unit. */
d85a05f0 3570 if (has_children)
93311388 3571 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3572
98bfdba5
PA
3573 do_cleanups (free_abbrevs_cleanup);
3574
3575 if (read_cu)
3576 {
3577 /* We've successfully allocated this compilation unit. Let our
3578 caller clean it up when finished with it. */
3579 discard_cleanups (free_cu_cleanup);
3580 }
ae038cb0
DJ
3581}
3582
3583/* Create a list of all compilation units in OBJFILE. We do this only
3584 if an inter-comp-unit reference is found; presumably if there is one,
3585 there will be many, and one will occur early in the .debug_info section.
3586 So there's no point in building this list incrementally. */
3587
3588static void
3589create_all_comp_units (struct objfile *objfile)
3590{
3591 int n_allocated;
3592 int n_comp_units;
3593 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3594 gdb_byte *info_ptr;
3595
3596 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3597 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3598
3599 n_comp_units = 0;
3600 n_allocated = 10;
3601 all_comp_units = xmalloc (n_allocated
3602 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3603
3e43a32a
MS
3604 while (info_ptr < dwarf2_per_objfile->info.buffer
3605 + dwarf2_per_objfile->info.size)
ae038cb0 3606 {
c764a876 3607 unsigned int length, initial_length_size;
ae038cb0 3608 struct dwarf2_per_cu_data *this_cu;
c764a876 3609 unsigned int offset;
ae038cb0 3610
dce234bc 3611 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3612
3613 /* Read just enough information to find out where the next
3614 compilation unit is. */
c764a876
DE
3615 length = read_initial_length (objfile->obfd, info_ptr,
3616 &initial_length_size);
ae038cb0
DJ
3617
3618 /* Save the compilation unit for later lookup. */
3619 this_cu = obstack_alloc (&objfile->objfile_obstack,
3620 sizeof (struct dwarf2_per_cu_data));
3621 memset (this_cu, 0, sizeof (*this_cu));
3622 this_cu->offset = offset;
c764a876 3623 this_cu->length = length + initial_length_size;
9291a0cd 3624 this_cu->objfile = objfile;
ae038cb0
DJ
3625
3626 if (n_comp_units == n_allocated)
3627 {
3628 n_allocated *= 2;
3629 all_comp_units = xrealloc (all_comp_units,
3630 n_allocated
3631 * sizeof (struct dwarf2_per_cu_data *));
3632 }
3633 all_comp_units[n_comp_units++] = this_cu;
3634
3635 info_ptr = info_ptr + this_cu->length;
3636 }
3637
3638 dwarf2_per_objfile->all_comp_units
3639 = obstack_alloc (&objfile->objfile_obstack,
3640 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3641 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3642 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3643 xfree (all_comp_units);
3644 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3645}
3646
5734ee8b
DJ
3647/* Process all loaded DIEs for compilation unit CU, starting at
3648 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3649 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3650 DW_AT_ranges). If NEED_PC is set, then this function will set
3651 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3652 and record the covered ranges in the addrmap. */
c906108c 3653
72bf9492
DJ
3654static void
3655scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3656 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3657{
72bf9492 3658 struct partial_die_info *pdi;
c906108c 3659
91c24f0a
DC
3660 /* Now, march along the PDI's, descending into ones which have
3661 interesting children but skipping the children of the other ones,
3662 until we reach the end of the compilation unit. */
c906108c 3663
72bf9492 3664 pdi = first_die;
91c24f0a 3665
72bf9492
DJ
3666 while (pdi != NULL)
3667 {
3668 fixup_partial_die (pdi, cu);
c906108c 3669
f55ee35c 3670 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3671 children, so we need to look at them. Ditto for anonymous
3672 enums. */
933c6fe4 3673
72bf9492 3674 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3675 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3676 {
72bf9492 3677 switch (pdi->tag)
c906108c
SS
3678 {
3679 case DW_TAG_subprogram:
5734ee8b 3680 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3681 break;
72929c62 3682 case DW_TAG_constant:
c906108c
SS
3683 case DW_TAG_variable:
3684 case DW_TAG_typedef:
91c24f0a 3685 case DW_TAG_union_type:
72bf9492 3686 if (!pdi->is_declaration)
63d06c5c 3687 {
72bf9492 3688 add_partial_symbol (pdi, cu);
63d06c5c
DC
3689 }
3690 break;
c906108c 3691 case DW_TAG_class_type:
680b30c7 3692 case DW_TAG_interface_type:
c906108c 3693 case DW_TAG_structure_type:
72bf9492 3694 if (!pdi->is_declaration)
c906108c 3695 {
72bf9492 3696 add_partial_symbol (pdi, cu);
c906108c
SS
3697 }
3698 break;
91c24f0a 3699 case DW_TAG_enumeration_type:
72bf9492
DJ
3700 if (!pdi->is_declaration)
3701 add_partial_enumeration (pdi, cu);
c906108c
SS
3702 break;
3703 case DW_TAG_base_type:
a02abb62 3704 case DW_TAG_subrange_type:
c906108c 3705 /* File scope base type definitions are added to the partial
c5aa993b 3706 symbol table. */
72bf9492 3707 add_partial_symbol (pdi, cu);
c906108c 3708 break;
d9fa45fe 3709 case DW_TAG_namespace:
5734ee8b 3710 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3711 break;
5d7cb8df
JK
3712 case DW_TAG_module:
3713 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3714 break;
c906108c
SS
3715 default:
3716 break;
3717 }
3718 }
3719
72bf9492
DJ
3720 /* If the die has a sibling, skip to the sibling. */
3721
3722 pdi = pdi->die_sibling;
3723 }
3724}
3725
3726/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3727
72bf9492 3728 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3729 name is concatenated with "::" and the partial DIE's name. For
3730 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3731 Enumerators are an exception; they use the scope of their parent
3732 enumeration type, i.e. the name of the enumeration type is not
3733 prepended to the enumerator.
91c24f0a 3734
72bf9492
DJ
3735 There are two complexities. One is DW_AT_specification; in this
3736 case "parent" means the parent of the target of the specification,
3737 instead of the direct parent of the DIE. The other is compilers
3738 which do not emit DW_TAG_namespace; in this case we try to guess
3739 the fully qualified name of structure types from their members'
3740 linkage names. This must be done using the DIE's children rather
3741 than the children of any DW_AT_specification target. We only need
3742 to do this for structures at the top level, i.e. if the target of
3743 any DW_AT_specification (if any; otherwise the DIE itself) does not
3744 have a parent. */
3745
3746/* Compute the scope prefix associated with PDI's parent, in
3747 compilation unit CU. The result will be allocated on CU's
3748 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3749 field. NULL is returned if no prefix is necessary. */
3750static char *
3751partial_die_parent_scope (struct partial_die_info *pdi,
3752 struct dwarf2_cu *cu)
3753{
3754 char *grandparent_scope;
3755 struct partial_die_info *parent, *real_pdi;
91c24f0a 3756
72bf9492
DJ
3757 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3758 then this means the parent of the specification DIE. */
3759
3760 real_pdi = pdi;
72bf9492 3761 while (real_pdi->has_specification)
10b3939b 3762 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3763
3764 parent = real_pdi->die_parent;
3765 if (parent == NULL)
3766 return NULL;
3767
3768 if (parent->scope_set)
3769 return parent->scope;
3770
3771 fixup_partial_die (parent, cu);
3772
10b3939b 3773 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3774
acebe513
UW
3775 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3776 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3777 Work around this problem here. */
3778 if (cu->language == language_cplus
6e70227d 3779 && parent->tag == DW_TAG_namespace
acebe513
UW
3780 && strcmp (parent->name, "::") == 0
3781 && grandparent_scope == NULL)
3782 {
3783 parent->scope = NULL;
3784 parent->scope_set = 1;
3785 return NULL;
3786 }
3787
72bf9492 3788 if (parent->tag == DW_TAG_namespace
f55ee35c 3789 || parent->tag == DW_TAG_module
72bf9492
DJ
3790 || parent->tag == DW_TAG_structure_type
3791 || parent->tag == DW_TAG_class_type
680b30c7 3792 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3793 || parent->tag == DW_TAG_union_type
3794 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3795 {
3796 if (grandparent_scope == NULL)
3797 parent->scope = parent->name;
3798 else
3e43a32a
MS
3799 parent->scope = typename_concat (&cu->comp_unit_obstack,
3800 grandparent_scope,
f55ee35c 3801 parent->name, 0, cu);
72bf9492 3802 }
ceeb3d5a 3803 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3804 /* Enumerators should not get the name of the enumeration as a prefix. */
3805 parent->scope = grandparent_scope;
3806 else
3807 {
3808 /* FIXME drow/2004-04-01: What should we be doing with
3809 function-local names? For partial symbols, we should probably be
3810 ignoring them. */
3811 complaint (&symfile_complaints,
e2e0b3e5 3812 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3813 parent->tag, pdi->offset);
3814 parent->scope = grandparent_scope;
c906108c
SS
3815 }
3816
72bf9492
DJ
3817 parent->scope_set = 1;
3818 return parent->scope;
3819}
3820
3821/* Return the fully scoped name associated with PDI, from compilation unit
3822 CU. The result will be allocated with malloc. */
3823static char *
3824partial_die_full_name (struct partial_die_info *pdi,
3825 struct dwarf2_cu *cu)
3826{
3827 char *parent_scope;
3828
98bfdba5
PA
3829 /* If this is a template instantiation, we can not work out the
3830 template arguments from partial DIEs. So, unfortunately, we have
3831 to go through the full DIEs. At least any work we do building
3832 types here will be reused if full symbols are loaded later. */
3833 if (pdi->has_template_arguments)
3834 {
3835 fixup_partial_die (pdi, cu);
3836
3837 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3838 {
3839 struct die_info *die;
3840 struct attribute attr;
3841 struct dwarf2_cu *ref_cu = cu;
3842
3843 attr.name = 0;
3844 attr.form = DW_FORM_ref_addr;
3845 attr.u.addr = pdi->offset;
3846 die = follow_die_ref (NULL, &attr, &ref_cu);
3847
3848 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3849 }
3850 }
3851
72bf9492
DJ
3852 parent_scope = partial_die_parent_scope (pdi, cu);
3853 if (parent_scope == NULL)
3854 return NULL;
3855 else
f55ee35c 3856 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3857}
3858
3859static void
72bf9492 3860add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3861{
e7c27a73 3862 struct objfile *objfile = cu->objfile;
c906108c 3863 CORE_ADDR addr = 0;
decbce07 3864 char *actual_name = NULL;
5c4e30ca 3865 const struct partial_symbol *psym = NULL;
e142c38c 3866 CORE_ADDR baseaddr;
72bf9492 3867 int built_actual_name = 0;
e142c38c
DJ
3868
3869 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3870
94af9270
KS
3871 actual_name = partial_die_full_name (pdi, cu);
3872 if (actual_name)
3873 built_actual_name = 1;
63d06c5c 3874
72bf9492
DJ
3875 if (actual_name == NULL)
3876 actual_name = pdi->name;
3877
c906108c
SS
3878 switch (pdi->tag)
3879 {
3880 case DW_TAG_subprogram:
2cfa0c8d 3881 if (pdi->is_external || cu->language == language_ada)
c906108c 3882 {
2cfa0c8d
JB
3883 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3884 of the global scope. But in Ada, we want to be able to access
3885 nested procedures globally. So all Ada subprograms are stored
3886 in the global scope. */
f47fb265 3887 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3888 mst_text, objfile); */
f47fb265
MS
3889 add_psymbol_to_list (actual_name, strlen (actual_name),
3890 built_actual_name,
3891 VAR_DOMAIN, LOC_BLOCK,
3892 &objfile->global_psymbols,
3893 0, pdi->lowpc + baseaddr,
3894 cu->language, objfile);
c906108c
SS
3895 }
3896 else
3897 {
f47fb265 3898 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3899 mst_file_text, objfile); */
f47fb265
MS
3900 add_psymbol_to_list (actual_name, strlen (actual_name),
3901 built_actual_name,
3902 VAR_DOMAIN, LOC_BLOCK,
3903 &objfile->static_psymbols,
3904 0, pdi->lowpc + baseaddr,
3905 cu->language, objfile);
c906108c
SS
3906 }
3907 break;
72929c62
JB
3908 case DW_TAG_constant:
3909 {
3910 struct psymbol_allocation_list *list;
3911
3912 if (pdi->is_external)
3913 list = &objfile->global_psymbols;
3914 else
3915 list = &objfile->static_psymbols;
f47fb265
MS
3916 add_psymbol_to_list (actual_name, strlen (actual_name),
3917 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3918 list, 0, 0, cu->language, objfile);
72929c62
JB
3919 }
3920 break;
c906108c 3921 case DW_TAG_variable:
caac4577
JG
3922 if (pdi->locdesc)
3923 addr = decode_locdesc (pdi->locdesc, cu);
3924
3925 if (pdi->locdesc
3926 && addr == 0
3927 && !dwarf2_per_objfile->has_section_at_zero)
3928 {
3929 /* A global or static variable may also have been stripped
3930 out by the linker if unused, in which case its address
3931 will be nullified; do not add such variables into partial
3932 symbol table then. */
3933 }
3934 else if (pdi->is_external)
c906108c
SS
3935 {
3936 /* Global Variable.
3937 Don't enter into the minimal symbol tables as there is
3938 a minimal symbol table entry from the ELF symbols already.
3939 Enter into partial symbol table if it has a location
3940 descriptor or a type.
3941 If the location descriptor is missing, new_symbol will create
3942 a LOC_UNRESOLVED symbol, the address of the variable will then
3943 be determined from the minimal symbol table whenever the variable
3944 is referenced.
3945 The address for the partial symbol table entry is not
3946 used by GDB, but it comes in handy for debugging partial symbol
3947 table building. */
3948
c906108c 3949 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
3950 add_psymbol_to_list (actual_name, strlen (actual_name),
3951 built_actual_name,
3952 VAR_DOMAIN, LOC_STATIC,
3953 &objfile->global_psymbols,
3954 0, addr + baseaddr,
3955 cu->language, objfile);
c906108c
SS
3956 }
3957 else
3958 {
0963b4bd 3959 /* Static Variable. Skip symbols without location descriptors. */
c906108c 3960 if (pdi->locdesc == NULL)
decbce07
MS
3961 {
3962 if (built_actual_name)
3963 xfree (actual_name);
3964 return;
3965 }
f47fb265 3966 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3967 mst_file_data, objfile); */
f47fb265
MS
3968 add_psymbol_to_list (actual_name, strlen (actual_name),
3969 built_actual_name,
3970 VAR_DOMAIN, LOC_STATIC,
3971 &objfile->static_psymbols,
3972 0, addr + baseaddr,
3973 cu->language, objfile);
c906108c
SS
3974 }
3975 break;
3976 case DW_TAG_typedef:
3977 case DW_TAG_base_type:
a02abb62 3978 case DW_TAG_subrange_type:
38d518c9 3979 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3980 built_actual_name,
176620f1 3981 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3982 &objfile->static_psymbols,
e142c38c 3983 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3984 break;
72bf9492
DJ
3985 case DW_TAG_namespace:
3986 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3987 built_actual_name,
72bf9492
DJ
3988 VAR_DOMAIN, LOC_TYPEDEF,
3989 &objfile->global_psymbols,
3990 0, (CORE_ADDR) 0, cu->language, objfile);
3991 break;
c906108c 3992 case DW_TAG_class_type:
680b30c7 3993 case DW_TAG_interface_type:
c906108c
SS
3994 case DW_TAG_structure_type:
3995 case DW_TAG_union_type:
3996 case DW_TAG_enumeration_type:
fa4028e9
JB
3997 /* Skip external references. The DWARF standard says in the section
3998 about "Structure, Union, and Class Type Entries": "An incomplete
3999 structure, union or class type is represented by a structure,
4000 union or class entry that does not have a byte size attribute
4001 and that has a DW_AT_declaration attribute." */
4002 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4003 {
4004 if (built_actual_name)
4005 xfree (actual_name);
4006 return;
4007 }
fa4028e9 4008
63d06c5c
DC
4009 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4010 static vs. global. */
38d518c9 4011 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4012 built_actual_name,
176620f1 4013 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4014 (cu->language == language_cplus
4015 || cu->language == language_java)
63d06c5c
DC
4016 ? &objfile->global_psymbols
4017 : &objfile->static_psymbols,
e142c38c 4018 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4019
c906108c
SS
4020 break;
4021 case DW_TAG_enumerator:
38d518c9 4022 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4023 built_actual_name,
176620f1 4024 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4025 (cu->language == language_cplus
4026 || cu->language == language_java)
f6fe98ef
DJ
4027 ? &objfile->global_psymbols
4028 : &objfile->static_psymbols,
e142c38c 4029 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4030 break;
4031 default:
4032 break;
4033 }
5c4e30ca 4034
72bf9492
DJ
4035 if (built_actual_name)
4036 xfree (actual_name);
c906108c
SS
4037}
4038
5c4e30ca
DC
4039/* Read a partial die corresponding to a namespace; also, add a symbol
4040 corresponding to that namespace to the symbol table. NAMESPACE is
4041 the name of the enclosing namespace. */
91c24f0a 4042
72bf9492
DJ
4043static void
4044add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4045 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4046 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4047{
72bf9492 4048 /* Add a symbol for the namespace. */
e7c27a73 4049
72bf9492 4050 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4051
4052 /* Now scan partial symbols in that namespace. */
4053
91c24f0a 4054 if (pdi->has_children)
5734ee8b 4055 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4056}
4057
5d7cb8df
JK
4058/* Read a partial die corresponding to a Fortran module. */
4059
4060static void
4061add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4062 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4063{
f55ee35c 4064 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4065
4066 if (pdi->has_children)
4067 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4068}
4069
bc30ff58
JB
4070/* Read a partial die corresponding to a subprogram and create a partial
4071 symbol for that subprogram. When the CU language allows it, this
4072 routine also defines a partial symbol for each nested subprogram
4073 that this subprogram contains.
6e70227d 4074
bc30ff58
JB
4075 DIE my also be a lexical block, in which case we simply search
4076 recursively for suprograms defined inside that lexical block.
4077 Again, this is only performed when the CU language allows this
4078 type of definitions. */
4079
4080static void
4081add_partial_subprogram (struct partial_die_info *pdi,
4082 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4083 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4084{
4085 if (pdi->tag == DW_TAG_subprogram)
4086 {
4087 if (pdi->has_pc_info)
4088 {
4089 if (pdi->lowpc < *lowpc)
4090 *lowpc = pdi->lowpc;
4091 if (pdi->highpc > *highpc)
4092 *highpc = pdi->highpc;
5734ee8b
DJ
4093 if (need_pc)
4094 {
4095 CORE_ADDR baseaddr;
4096 struct objfile *objfile = cu->objfile;
4097
4098 baseaddr = ANOFFSET (objfile->section_offsets,
4099 SECT_OFF_TEXT (objfile));
4100 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4101 pdi->lowpc + baseaddr,
4102 pdi->highpc - 1 + baseaddr,
9291a0cd 4103 cu->per_cu->v.psymtab);
5734ee8b 4104 }
bc30ff58 4105 if (!pdi->is_declaration)
e8d05480
JB
4106 /* Ignore subprogram DIEs that do not have a name, they are
4107 illegal. Do not emit a complaint at this point, we will
4108 do so when we convert this psymtab into a symtab. */
4109 if (pdi->name)
4110 add_partial_symbol (pdi, cu);
bc30ff58
JB
4111 }
4112 }
6e70227d 4113
bc30ff58
JB
4114 if (! pdi->has_children)
4115 return;
4116
4117 if (cu->language == language_ada)
4118 {
4119 pdi = pdi->die_child;
4120 while (pdi != NULL)
4121 {
4122 fixup_partial_die (pdi, cu);
4123 if (pdi->tag == DW_TAG_subprogram
4124 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4125 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4126 pdi = pdi->die_sibling;
4127 }
4128 }
4129}
4130
91c24f0a
DC
4131/* Read a partial die corresponding to an enumeration type. */
4132
72bf9492
DJ
4133static void
4134add_partial_enumeration (struct partial_die_info *enum_pdi,
4135 struct dwarf2_cu *cu)
91c24f0a 4136{
72bf9492 4137 struct partial_die_info *pdi;
91c24f0a
DC
4138
4139 if (enum_pdi->name != NULL)
72bf9492
DJ
4140 add_partial_symbol (enum_pdi, cu);
4141
4142 pdi = enum_pdi->die_child;
4143 while (pdi)
91c24f0a 4144 {
72bf9492 4145 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4146 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4147 else
72bf9492
DJ
4148 add_partial_symbol (pdi, cu);
4149 pdi = pdi->die_sibling;
91c24f0a 4150 }
91c24f0a
DC
4151}
4152
4bb7a0a7
DJ
4153/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4154 Return the corresponding abbrev, or NULL if the number is zero (indicating
4155 an empty DIE). In either case *BYTES_READ will be set to the length of
4156 the initial number. */
4157
4158static struct abbrev_info *
fe1b8b76 4159peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4160 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4161{
4162 bfd *abfd = cu->objfile->obfd;
4163 unsigned int abbrev_number;
4164 struct abbrev_info *abbrev;
4165
4166 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4167
4168 if (abbrev_number == 0)
4169 return NULL;
4170
4171 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4172 if (!abbrev)
4173 {
3e43a32a
MS
4174 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4175 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4176 }
4177
4178 return abbrev;
4179}
4180
93311388
DE
4181/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4182 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4183 DIE. Any children of the skipped DIEs will also be skipped. */
4184
fe1b8b76 4185static gdb_byte *
93311388 4186skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4187{
4188 struct abbrev_info *abbrev;
4189 unsigned int bytes_read;
4190
4191 while (1)
4192 {
4193 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4194 if (abbrev == NULL)
4195 return info_ptr + bytes_read;
4196 else
93311388 4197 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4198 }
4199}
4200
93311388
DE
4201/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4202 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4203 abbrev corresponding to that skipped uleb128 should be passed in
4204 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4205 children. */
4206
fe1b8b76 4207static gdb_byte *
93311388
DE
4208skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4209 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4210{
4211 unsigned int bytes_read;
4212 struct attribute attr;
4213 bfd *abfd = cu->objfile->obfd;
4214 unsigned int form, i;
4215
4216 for (i = 0; i < abbrev->num_attrs; i++)
4217 {
4218 /* The only abbrev we care about is DW_AT_sibling. */
4219 if (abbrev->attrs[i].name == DW_AT_sibling)
4220 {
4221 read_attribute (&attr, &abbrev->attrs[i],
4222 abfd, info_ptr, cu);
4223 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4224 complaint (&symfile_complaints,
4225 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4226 else
93311388 4227 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4228 }
4229
4230 /* If it isn't DW_AT_sibling, skip this attribute. */
4231 form = abbrev->attrs[i].form;
4232 skip_attribute:
4233 switch (form)
4234 {
4bb7a0a7 4235 case DW_FORM_ref_addr:
ae411497
TT
4236 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4237 and later it is offset sized. */
4238 if (cu->header.version == 2)
4239 info_ptr += cu->header.addr_size;
4240 else
4241 info_ptr += cu->header.offset_size;
4242 break;
4243 case DW_FORM_addr:
4bb7a0a7
DJ
4244 info_ptr += cu->header.addr_size;
4245 break;
4246 case DW_FORM_data1:
4247 case DW_FORM_ref1:
4248 case DW_FORM_flag:
4249 info_ptr += 1;
4250 break;
2dc7f7b3
TT
4251 case DW_FORM_flag_present:
4252 break;
4bb7a0a7
DJ
4253 case DW_FORM_data2:
4254 case DW_FORM_ref2:
4255 info_ptr += 2;
4256 break;
4257 case DW_FORM_data4:
4258 case DW_FORM_ref4:
4259 info_ptr += 4;
4260 break;
4261 case DW_FORM_data8:
4262 case DW_FORM_ref8:
55f1336d 4263 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4264 info_ptr += 8;
4265 break;
4266 case DW_FORM_string:
9b1c24c8 4267 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4268 info_ptr += bytes_read;
4269 break;
2dc7f7b3 4270 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4271 case DW_FORM_strp:
4272 info_ptr += cu->header.offset_size;
4273 break;
2dc7f7b3 4274 case DW_FORM_exprloc:
4bb7a0a7
DJ
4275 case DW_FORM_block:
4276 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4277 info_ptr += bytes_read;
4278 break;
4279 case DW_FORM_block1:
4280 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4281 break;
4282 case DW_FORM_block2:
4283 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4284 break;
4285 case DW_FORM_block4:
4286 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4287 break;
4288 case DW_FORM_sdata:
4289 case DW_FORM_udata:
4290 case DW_FORM_ref_udata:
4291 info_ptr = skip_leb128 (abfd, info_ptr);
4292 break;
4293 case DW_FORM_indirect:
4294 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4295 info_ptr += bytes_read;
4296 /* We need to continue parsing from here, so just go back to
4297 the top. */
4298 goto skip_attribute;
4299
4300 default:
3e43a32a
MS
4301 error (_("Dwarf Error: Cannot handle %s "
4302 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4303 dwarf_form_name (form),
4304 bfd_get_filename (abfd));
4305 }
4306 }
4307
4308 if (abbrev->has_children)
93311388 4309 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4310 else
4311 return info_ptr;
4312}
4313
93311388
DE
4314/* Locate ORIG_PDI's sibling.
4315 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4316 in BUFFER. */
91c24f0a 4317
fe1b8b76 4318static gdb_byte *
93311388
DE
4319locate_pdi_sibling (struct partial_die_info *orig_pdi,
4320 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4321 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4322{
4323 /* Do we know the sibling already? */
72bf9492 4324
91c24f0a
DC
4325 if (orig_pdi->sibling)
4326 return orig_pdi->sibling;
4327
4328 /* Are there any children to deal with? */
4329
4330 if (!orig_pdi->has_children)
4331 return info_ptr;
4332
4bb7a0a7 4333 /* Skip the children the long way. */
91c24f0a 4334
93311388 4335 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4336}
4337
c906108c
SS
4338/* Expand this partial symbol table into a full symbol table. */
4339
4340static void
fba45db2 4341dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4342{
c906108c
SS
4343 if (pst != NULL)
4344 {
4345 if (pst->readin)
4346 {
3e43a32a
MS
4347 warning (_("bug: psymtab for %s is already read in."),
4348 pst->filename);
c906108c
SS
4349 }
4350 else
4351 {
4352 if (info_verbose)
4353 {
3e43a32a
MS
4354 printf_filtered (_("Reading in symbols for %s..."),
4355 pst->filename);
c906108c
SS
4356 gdb_flush (gdb_stdout);
4357 }
4358
10b3939b
DJ
4359 /* Restore our global data. */
4360 dwarf2_per_objfile = objfile_data (pst->objfile,
4361 dwarf2_objfile_data_key);
4362
b2ab525c
KB
4363 /* If this psymtab is constructed from a debug-only objfile, the
4364 has_section_at_zero flag will not necessarily be correct. We
4365 can get the correct value for this flag by looking at the data
4366 associated with the (presumably stripped) associated objfile. */
4367 if (pst->objfile->separate_debug_objfile_backlink)
4368 {
4369 struct dwarf2_per_objfile *dpo_backlink
4370 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4371 dwarf2_objfile_data_key);
9a619af0 4372
b2ab525c
KB
4373 dwarf2_per_objfile->has_section_at_zero
4374 = dpo_backlink->has_section_at_zero;
4375 }
4376
98bfdba5
PA
4377 dwarf2_per_objfile->reading_partial_symbols = 0;
4378
c906108c
SS
4379 psymtab_to_symtab_1 (pst);
4380
4381 /* Finish up the debug error message. */
4382 if (info_verbose)
a3f17187 4383 printf_filtered (_("done.\n"));
c906108c
SS
4384 }
4385 }
4386}
4387
10b3939b
DJ
4388/* Add PER_CU to the queue. */
4389
4390static void
03dd20cc 4391queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4392{
4393 struct dwarf2_queue_item *item;
4394
4395 per_cu->queued = 1;
4396 item = xmalloc (sizeof (*item));
4397 item->per_cu = per_cu;
4398 item->next = NULL;
4399
4400 if (dwarf2_queue == NULL)
4401 dwarf2_queue = item;
4402 else
4403 dwarf2_queue_tail->next = item;
4404
4405 dwarf2_queue_tail = item;
4406}
4407
4408/* Process the queue. */
4409
4410static void
4411process_queue (struct objfile *objfile)
4412{
4413 struct dwarf2_queue_item *item, *next_item;
4414
03dd20cc
DJ
4415 /* The queue starts out with one item, but following a DIE reference
4416 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4417 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4418 {
9291a0cd
TT
4419 if (dwarf2_per_objfile->using_index
4420 ? !item->per_cu->v.quick->symtab
4421 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4422 process_full_comp_unit (item->per_cu);
4423
4424 item->per_cu->queued = 0;
4425 next_item = item->next;
4426 xfree (item);
4427 }
4428
4429 dwarf2_queue_tail = NULL;
4430}
4431
4432/* Free all allocated queue entries. This function only releases anything if
4433 an error was thrown; if the queue was processed then it would have been
4434 freed as we went along. */
4435
4436static void
4437dwarf2_release_queue (void *dummy)
4438{
4439 struct dwarf2_queue_item *item, *last;
4440
4441 item = dwarf2_queue;
4442 while (item)
4443 {
4444 /* Anything still marked queued is likely to be in an
4445 inconsistent state, so discard it. */
4446 if (item->per_cu->queued)
4447 {
4448 if (item->per_cu->cu != NULL)
4449 free_one_cached_comp_unit (item->per_cu->cu);
4450 item->per_cu->queued = 0;
4451 }
4452
4453 last = item;
4454 item = item->next;
4455 xfree (last);
4456 }
4457
4458 dwarf2_queue = dwarf2_queue_tail = NULL;
4459}
4460
4461/* Read in full symbols for PST, and anything it depends on. */
4462
c906108c 4463static void
fba45db2 4464psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4465{
10b3939b 4466 struct dwarf2_per_cu_data *per_cu;
c906108c 4467 struct cleanup *back_to;
aaa75496
JB
4468 int i;
4469
4470 for (i = 0; i < pst->number_of_dependencies; i++)
4471 if (!pst->dependencies[i]->readin)
4472 {
4473 /* Inform about additional files that need to be read in. */
4474 if (info_verbose)
4475 {
a3f17187 4476 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4477 fputs_filtered (" ", gdb_stdout);
4478 wrap_here ("");
4479 fputs_filtered ("and ", gdb_stdout);
4480 wrap_here ("");
4481 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4482 wrap_here (""); /* Flush output. */
aaa75496
JB
4483 gdb_flush (gdb_stdout);
4484 }
4485 psymtab_to_symtab_1 (pst->dependencies[i]);
4486 }
4487
e38df1d0 4488 per_cu = pst->read_symtab_private;
10b3939b
DJ
4489
4490 if (per_cu == NULL)
aaa75496
JB
4491 {
4492 /* It's an include file, no symbols to read for it.
4493 Everything is in the parent symtab. */
4494 pst->readin = 1;
4495 return;
4496 }
c906108c 4497
9291a0cd 4498 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4499}
4500
93311388 4501/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4502
93311388 4503static void
3e43a32a
MS
4504load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4505 struct objfile *objfile)
10b3939b 4506{
31ffec48 4507 bfd *abfd = objfile->obfd;
10b3939b 4508 struct dwarf2_cu *cu;
c764a876 4509 unsigned int offset;
93311388 4510 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4511 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4512 struct attribute *attr;
98bfdba5 4513 int read_cu = 0;
6502dd73 4514
348e048f
DE
4515 gdb_assert (! per_cu->from_debug_types);
4516
c906108c 4517 /* Set local variables from the partial symbol table info. */
10b3939b 4518 offset = per_cu->offset;
6502dd73 4519
be391dca 4520 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4521 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4522 beg_of_comp_unit = info_ptr;
63d06c5c 4523
98bfdba5
PA
4524 if (per_cu->cu == NULL)
4525 {
9816fde3
JK
4526 cu = xmalloc (sizeof (*cu));
4527 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4528
4529 read_cu = 1;
c906108c 4530
98bfdba5
PA
4531 /* If an error occurs while loading, release our storage. */
4532 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4533
98bfdba5
PA
4534 /* Read in the comp_unit header. */
4535 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4536
98bfdba5
PA
4537 /* Complete the cu_header. */
4538 cu->header.offset = offset;
4539 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4540
98bfdba5
PA
4541 /* Read the abbrevs for this compilation unit. */
4542 dwarf2_read_abbrevs (abfd, cu);
4543 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4544
98bfdba5
PA
4545 /* Link this compilation unit into the compilation unit tree. */
4546 per_cu->cu = cu;
4547 cu->per_cu = per_cu;
98bfdba5
PA
4548
4549 /* Link this CU into read_in_chain. */
4550 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4551 dwarf2_per_objfile->read_in_chain = per_cu;
4552 }
4553 else
4554 {
4555 cu = per_cu->cu;
4556 info_ptr += cu->header.first_die_offset;
4557 }
e142c38c 4558
93311388 4559 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4560
4561 /* We try not to read any attributes in this function, because not
4562 all objfiles needed for references have been loaded yet, and symbol
4563 table processing isn't initialized. But we have to set the CU language,
4564 or we won't be able to build types correctly. */
9816fde3 4565 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4566
a6c727b2
DJ
4567 /* Similarly, if we do not read the producer, we can not apply
4568 producer-specific interpretation. */
4569 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4570 if (attr)
4571 cu->producer = DW_STRING (attr);
4572
98bfdba5
PA
4573 if (read_cu)
4574 {
4575 do_cleanups (free_abbrevs_cleanup);
e142c38c 4576
98bfdba5
PA
4577 /* We've successfully allocated this compilation unit. Let our
4578 caller clean it up when finished with it. */
4579 discard_cleanups (free_cu_cleanup);
4580 }
10b3939b
DJ
4581}
4582
3da10d80
KS
4583/* Add a DIE to the delayed physname list. */
4584
4585static void
4586add_to_method_list (struct type *type, int fnfield_index, int index,
4587 const char *name, struct die_info *die,
4588 struct dwarf2_cu *cu)
4589{
4590 struct delayed_method_info mi;
4591 mi.type = type;
4592 mi.fnfield_index = fnfield_index;
4593 mi.index = index;
4594 mi.name = name;
4595 mi.die = die;
4596 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4597}
4598
4599/* A cleanup for freeing the delayed method list. */
4600
4601static void
4602free_delayed_list (void *ptr)
4603{
4604 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4605 if (cu->method_list != NULL)
4606 {
4607 VEC_free (delayed_method_info, cu->method_list);
4608 cu->method_list = NULL;
4609 }
4610}
4611
4612/* Compute the physnames of any methods on the CU's method list.
4613
4614 The computation of method physnames is delayed in order to avoid the
4615 (bad) condition that one of the method's formal parameters is of an as yet
4616 incomplete type. */
4617
4618static void
4619compute_delayed_physnames (struct dwarf2_cu *cu)
4620{
4621 int i;
4622 struct delayed_method_info *mi;
4623 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4624 {
1d06ead6 4625 const char *physname;
3da10d80
KS
4626 struct fn_fieldlist *fn_flp
4627 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4628 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4629 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4630 }
4631}
4632
8be455d7
JK
4633/* Check for GCC >= 4.0. */
4634
4635static int
4636producer_is_gcc_ge_4_0 (struct dwarf2_cu *cu)
4637{
4638 const char *cs;
4639 int major, minor;
4640
4641 if (cu->producer == NULL)
4642 {
4643 /* For unknown compilers expect their behavior is not compliant. For GCC
4644 this case can also happen for -gdwarf-4 type units supported since
4645 gcc-4.5. */
4646
4647 return 0;
4648 }
4649
4650 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
4651
4652 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
4653 {
4654 /* For non-GCC compilers expect their behavior is not compliant. */
4655
4656 return 0;
4657 }
4658 cs = &cu->producer[strlen ("GNU ")];
4659 while (*cs && !isdigit (*cs))
4660 cs++;
4661 if (sscanf (cs, "%d.%d", &major, &minor) != 2)
4662 {
4663 /* Not recognized as GCC. */
4664
4665 return 0;
4666 }
4667
4668 return major >= 4;
4669}
4670
10b3939b
DJ
4671/* Generate full symbol information for PST and CU, whose DIEs have
4672 already been loaded into memory. */
4673
4674static void
4675process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4676{
10b3939b 4677 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4678 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4679 CORE_ADDR lowpc, highpc;
4680 struct symtab *symtab;
3da10d80 4681 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4682 CORE_ADDR baseaddr;
4683
4684 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4685
10b3939b
DJ
4686 buildsym_init ();
4687 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4688 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4689
4690 cu->list_in_scope = &file_symbols;
c906108c 4691
d85a05f0 4692 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4693
c906108c 4694 /* Do line number decoding in read_file_scope () */
10b3939b 4695 process_die (cu->dies, cu);
c906108c 4696
3da10d80
KS
4697 /* Now that we have processed all the DIEs in the CU, all the types
4698 should be complete, and it should now be safe to compute all of the
4699 physnames. */
4700 compute_delayed_physnames (cu);
4701 do_cleanups (delayed_list_cleanup);
4702
fae299cd
DC
4703 /* Some compilers don't define a DW_AT_high_pc attribute for the
4704 compilation unit. If the DW_AT_high_pc is missing, synthesize
4705 it, by scanning the DIE's below the compilation unit. */
10b3939b 4706 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4707
613e1657 4708 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4709
8be455d7 4710 if (symtab != NULL)
c906108c 4711 {
8be455d7
JK
4712 /* Set symtab language to language from DW_AT_language. If the
4713 compilation is from a C file generated by language preprocessors, do
4714 not set the language if it was already deduced by start_subfile. */
4715 if (!(cu->language == language_c && symtab->language != language_c))
4716 symtab->language = cu->language;
4717
4718 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4719 produce DW_AT_location with location lists but it can be possibly
4720 invalid without -fvar-tracking.
4721
4722 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4723 needed, it would be wrong due to missing DW_AT_producer there.
4724
4725 Still one can confuse GDB by using non-standard GCC compilation
4726 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4727 */
4728 if (cu->has_loclist && producer_is_gcc_ge_4_0 (cu))
4729 symtab->locations_valid = 1;
c906108c 4730 }
9291a0cd
TT
4731
4732 if (dwarf2_per_objfile->using_index)
4733 per_cu->v.quick->symtab = symtab;
4734 else
4735 {
4736 struct partial_symtab *pst = per_cu->v.psymtab;
4737 pst->symtab = symtab;
4738 pst->readin = 1;
4739 }
c906108c
SS
4740
4741 do_cleanups (back_to);
4742}
4743
4744/* Process a die and its children. */
4745
4746static void
e7c27a73 4747process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4748{
4749 switch (die->tag)
4750 {
4751 case DW_TAG_padding:
4752 break;
4753 case DW_TAG_compile_unit:
e7c27a73 4754 read_file_scope (die, cu);
c906108c 4755 break;
348e048f
DE
4756 case DW_TAG_type_unit:
4757 read_type_unit_scope (die, cu);
4758 break;
c906108c 4759 case DW_TAG_subprogram:
c906108c 4760 case DW_TAG_inlined_subroutine:
edb3359d 4761 read_func_scope (die, cu);
c906108c
SS
4762 break;
4763 case DW_TAG_lexical_block:
14898363
L
4764 case DW_TAG_try_block:
4765 case DW_TAG_catch_block:
e7c27a73 4766 read_lexical_block_scope (die, cu);
c906108c
SS
4767 break;
4768 case DW_TAG_class_type:
680b30c7 4769 case DW_TAG_interface_type:
c906108c
SS
4770 case DW_TAG_structure_type:
4771 case DW_TAG_union_type:
134d01f1 4772 process_structure_scope (die, cu);
c906108c
SS
4773 break;
4774 case DW_TAG_enumeration_type:
134d01f1 4775 process_enumeration_scope (die, cu);
c906108c 4776 break;
134d01f1 4777
f792889a
DJ
4778 /* These dies have a type, but processing them does not create
4779 a symbol or recurse to process the children. Therefore we can
4780 read them on-demand through read_type_die. */
c906108c 4781 case DW_TAG_subroutine_type:
72019c9c 4782 case DW_TAG_set_type:
c906108c 4783 case DW_TAG_array_type:
c906108c 4784 case DW_TAG_pointer_type:
c906108c 4785 case DW_TAG_ptr_to_member_type:
c906108c 4786 case DW_TAG_reference_type:
c906108c 4787 case DW_TAG_string_type:
c906108c 4788 break;
134d01f1 4789
c906108c 4790 case DW_TAG_base_type:
a02abb62 4791 case DW_TAG_subrange_type:
cb249c71 4792 case DW_TAG_typedef:
134d01f1
DJ
4793 /* Add a typedef symbol for the type definition, if it has a
4794 DW_AT_name. */
f792889a 4795 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4796 break;
c906108c 4797 case DW_TAG_common_block:
e7c27a73 4798 read_common_block (die, cu);
c906108c
SS
4799 break;
4800 case DW_TAG_common_inclusion:
4801 break;
d9fa45fe 4802 case DW_TAG_namespace:
63d06c5c 4803 processing_has_namespace_info = 1;
e7c27a73 4804 read_namespace (die, cu);
d9fa45fe 4805 break;
5d7cb8df 4806 case DW_TAG_module:
f55ee35c 4807 processing_has_namespace_info = 1;
5d7cb8df
JK
4808 read_module (die, cu);
4809 break;
d9fa45fe
DC
4810 case DW_TAG_imported_declaration:
4811 case DW_TAG_imported_module:
63d06c5c 4812 processing_has_namespace_info = 1;
27aa8d6a
SW
4813 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4814 || cu->language != language_fortran))
4815 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4816 dwarf_tag_name (die->tag));
4817 read_import_statement (die, cu);
d9fa45fe 4818 break;
c906108c 4819 default:
e7c27a73 4820 new_symbol (die, NULL, cu);
c906108c
SS
4821 break;
4822 }
4823}
4824
94af9270
KS
4825/* A helper function for dwarf2_compute_name which determines whether DIE
4826 needs to have the name of the scope prepended to the name listed in the
4827 die. */
4828
4829static int
4830die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4831{
1c809c68
TT
4832 struct attribute *attr;
4833
94af9270
KS
4834 switch (die->tag)
4835 {
4836 case DW_TAG_namespace:
4837 case DW_TAG_typedef:
4838 case DW_TAG_class_type:
4839 case DW_TAG_interface_type:
4840 case DW_TAG_structure_type:
4841 case DW_TAG_union_type:
4842 case DW_TAG_enumeration_type:
4843 case DW_TAG_enumerator:
4844 case DW_TAG_subprogram:
4845 case DW_TAG_member:
4846 return 1;
4847
4848 case DW_TAG_variable:
c2b0a229 4849 case DW_TAG_constant:
94af9270
KS
4850 /* We only need to prefix "globally" visible variables. These include
4851 any variable marked with DW_AT_external or any variable that
4852 lives in a namespace. [Variables in anonymous namespaces
4853 require prefixing, but they are not DW_AT_external.] */
4854
4855 if (dwarf2_attr (die, DW_AT_specification, cu))
4856 {
4857 struct dwarf2_cu *spec_cu = cu;
9a619af0 4858
94af9270
KS
4859 return die_needs_namespace (die_specification (die, &spec_cu),
4860 spec_cu);
4861 }
4862
1c809c68 4863 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4864 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4865 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4866 return 0;
4867 /* A variable in a lexical block of some kind does not need a
4868 namespace, even though in C++ such variables may be external
4869 and have a mangled name. */
4870 if (die->parent->tag == DW_TAG_lexical_block
4871 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4872 || die->parent->tag == DW_TAG_catch_block
4873 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4874 return 0;
4875 return 1;
94af9270
KS
4876
4877 default:
4878 return 0;
4879 }
4880}
4881
98bfdba5
PA
4882/* Retrieve the last character from a mem_file. */
4883
4884static void
4885do_ui_file_peek_last (void *object, const char *buffer, long length)
4886{
4887 char *last_char_p = (char *) object;
4888
4889 if (length > 0)
4890 *last_char_p = buffer[length - 1];
4891}
4892
94af9270
KS
4893/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4894 compute the physname for the object, which include a method's
4895 formal parameters (C++/Java) and return type (Java).
4896
af6b7be1
JB
4897 For Ada, return the DIE's linkage name rather than the fully qualified
4898 name. PHYSNAME is ignored..
4899
94af9270
KS
4900 The result is allocated on the objfile_obstack and canonicalized. */
4901
4902static const char *
4903dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4904 int physname)
4905{
4906 if (name == NULL)
4907 name = dwarf2_name (die, cu);
4908
f55ee35c
JK
4909 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4910 compute it by typename_concat inside GDB. */
4911 if (cu->language == language_ada
4912 || (cu->language == language_fortran && physname))
4913 {
4914 /* For Ada unit, we prefer the linkage name over the name, as
4915 the former contains the exported name, which the user expects
4916 to be able to reference. Ideally, we want the user to be able
4917 to reference this entity using either natural or linkage name,
4918 but we haven't started looking at this enhancement yet. */
4919 struct attribute *attr;
4920
4921 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4922 if (attr == NULL)
4923 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4924 if (attr && DW_STRING (attr))
4925 return DW_STRING (attr);
4926 }
4927
94af9270
KS
4928 /* These are the only languages we know how to qualify names in. */
4929 if (name != NULL
f55ee35c
JK
4930 && (cu->language == language_cplus || cu->language == language_java
4931 || cu->language == language_fortran))
94af9270
KS
4932 {
4933 if (die_needs_namespace (die, cu))
4934 {
4935 long length;
4936 char *prefix;
4937 struct ui_file *buf;
4938
4939 prefix = determine_prefix (die, cu);
4940 buf = mem_fileopen ();
4941 if (*prefix != '\0')
4942 {
f55ee35c
JK
4943 char *prefixed_name = typename_concat (NULL, prefix, name,
4944 physname, cu);
9a619af0 4945
94af9270
KS
4946 fputs_unfiltered (prefixed_name, buf);
4947 xfree (prefixed_name);
4948 }
4949 else
62d5b8da 4950 fputs_unfiltered (name, buf);
94af9270 4951
98bfdba5
PA
4952 /* Template parameters may be specified in the DIE's DW_AT_name, or
4953 as children with DW_TAG_template_type_param or
4954 DW_TAG_value_type_param. If the latter, add them to the name
4955 here. If the name already has template parameters, then
4956 skip this step; some versions of GCC emit both, and
4957 it is more efficient to use the pre-computed name.
4958
4959 Something to keep in mind about this process: it is very
4960 unlikely, or in some cases downright impossible, to produce
4961 something that will match the mangled name of a function.
4962 If the definition of the function has the same debug info,
4963 we should be able to match up with it anyway. But fallbacks
4964 using the minimal symbol, for instance to find a method
4965 implemented in a stripped copy of libstdc++, will not work.
4966 If we do not have debug info for the definition, we will have to
4967 match them up some other way.
4968
4969 When we do name matching there is a related problem with function
4970 templates; two instantiated function templates are allowed to
4971 differ only by their return types, which we do not add here. */
4972
4973 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4974 {
4975 struct attribute *attr;
4976 struct die_info *child;
4977 int first = 1;
4978
4979 die->building_fullname = 1;
4980
4981 for (child = die->child; child != NULL; child = child->sibling)
4982 {
4983 struct type *type;
4984 long value;
4985 gdb_byte *bytes;
4986 struct dwarf2_locexpr_baton *baton;
4987 struct value *v;
4988
4989 if (child->tag != DW_TAG_template_type_param
4990 && child->tag != DW_TAG_template_value_param)
4991 continue;
4992
4993 if (first)
4994 {
4995 fputs_unfiltered ("<", buf);
4996 first = 0;
4997 }
4998 else
4999 fputs_unfiltered (", ", buf);
5000
5001 attr = dwarf2_attr (child, DW_AT_type, cu);
5002 if (attr == NULL)
5003 {
5004 complaint (&symfile_complaints,
5005 _("template parameter missing DW_AT_type"));
5006 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5007 continue;
5008 }
5009 type = die_type (child, cu);
5010
5011 if (child->tag == DW_TAG_template_type_param)
5012 {
5013 c_print_type (type, "", buf, -1, 0);
5014 continue;
5015 }
5016
5017 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5018 if (attr == NULL)
5019 {
5020 complaint (&symfile_complaints,
3e43a32a
MS
5021 _("template parameter missing "
5022 "DW_AT_const_value"));
98bfdba5
PA
5023 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5024 continue;
5025 }
5026
5027 dwarf2_const_value_attr (attr, type, name,
5028 &cu->comp_unit_obstack, cu,
5029 &value, &bytes, &baton);
5030
5031 if (TYPE_NOSIGN (type))
5032 /* GDB prints characters as NUMBER 'CHAR'. If that's
5033 changed, this can use value_print instead. */
5034 c_printchar (value, type, buf);
5035 else
5036 {
5037 struct value_print_options opts;
5038
5039 if (baton != NULL)
5040 v = dwarf2_evaluate_loc_desc (type, NULL,
5041 baton->data,
5042 baton->size,
5043 baton->per_cu);
5044 else if (bytes != NULL)
5045 {
5046 v = allocate_value (type);
5047 memcpy (value_contents_writeable (v), bytes,
5048 TYPE_LENGTH (type));
5049 }
5050 else
5051 v = value_from_longest (type, value);
5052
3e43a32a
MS
5053 /* Specify decimal so that we do not depend on
5054 the radix. */
98bfdba5
PA
5055 get_formatted_print_options (&opts, 'd');
5056 opts.raw = 1;
5057 value_print (v, buf, &opts);
5058 release_value (v);
5059 value_free (v);
5060 }
5061 }
5062
5063 die->building_fullname = 0;
5064
5065 if (!first)
5066 {
5067 /* Close the argument list, with a space if necessary
5068 (nested templates). */
5069 char last_char = '\0';
5070 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5071 if (last_char == '>')
5072 fputs_unfiltered (" >", buf);
5073 else
5074 fputs_unfiltered (">", buf);
5075 }
5076 }
5077
94af9270
KS
5078 /* For Java and C++ methods, append formal parameter type
5079 information, if PHYSNAME. */
6e70227d 5080
94af9270
KS
5081 if (physname && die->tag == DW_TAG_subprogram
5082 && (cu->language == language_cplus
5083 || cu->language == language_java))
5084 {
5085 struct type *type = read_type_die (die, cu);
5086
3167638f 5087 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5088
5089 if (cu->language == language_java)
5090 {
5091 /* For java, we must append the return type to method
0963b4bd 5092 names. */
94af9270
KS
5093 if (die->tag == DW_TAG_subprogram)
5094 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5095 0, 0);
5096 }
5097 else if (cu->language == language_cplus)
5098 {
60430eff
DJ
5099 /* Assume that an artificial first parameter is
5100 "this", but do not crash if it is not. RealView
5101 marks unnamed (and thus unused) parameters as
5102 artificial; there is no way to differentiate
5103 the two cases. */
94af9270
KS
5104 if (TYPE_NFIELDS (type) > 0
5105 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5106 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5107 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5108 0))))
94af9270
KS
5109 fputs_unfiltered (" const", buf);
5110 }
5111 }
5112
5113 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5114 &length);
5115 ui_file_delete (buf);
5116
5117 if (cu->language == language_cplus)
5118 {
5119 char *cname
5120 = dwarf2_canonicalize_name (name, cu,
5121 &cu->objfile->objfile_obstack);
9a619af0 5122
94af9270
KS
5123 if (cname != NULL)
5124 name = cname;
5125 }
5126 }
5127 }
5128
5129 return name;
5130}
5131
0114d602
DJ
5132/* Return the fully qualified name of DIE, based on its DW_AT_name.
5133 If scope qualifiers are appropriate they will be added. The result
5134 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5135 not have a name. NAME may either be from a previous call to
5136 dwarf2_name or NULL.
5137
0963b4bd 5138 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5139
5140static const char *
94af9270 5141dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5142{
94af9270
KS
5143 return dwarf2_compute_name (name, die, cu, 0);
5144}
0114d602 5145
94af9270
KS
5146/* Construct a physname for the given DIE in CU. NAME may either be
5147 from a previous call to dwarf2_name or NULL. The result will be
5148 allocated on the objfile_objstack or NULL if the DIE does not have a
5149 name.
0114d602 5150
94af9270 5151 The output string will be canonicalized (if C++/Java). */
0114d602 5152
94af9270
KS
5153static const char *
5154dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5155{
5156 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
5157}
5158
27aa8d6a
SW
5159/* Read the import statement specified by the given die and record it. */
5160
5161static void
5162read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5163{
5164 struct attribute *import_attr;
5165 struct die_info *imported_die;
de4affc9 5166 struct dwarf2_cu *imported_cu;
27aa8d6a 5167 const char *imported_name;
794684b6 5168 const char *imported_name_prefix;
13387711
SW
5169 const char *canonical_name;
5170 const char *import_alias;
5171 const char *imported_declaration = NULL;
794684b6 5172 const char *import_prefix;
13387711
SW
5173
5174 char *temp;
27aa8d6a
SW
5175
5176 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5177 if (import_attr == NULL)
5178 {
5179 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5180 dwarf_tag_name (die->tag));
5181 return;
5182 }
5183
de4affc9
CC
5184 imported_cu = cu;
5185 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5186 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5187 if (imported_name == NULL)
5188 {
5189 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5190
5191 The import in the following code:
5192 namespace A
5193 {
5194 typedef int B;
5195 }
5196
5197 int main ()
5198 {
5199 using A::B;
5200 B b;
5201 return b;
5202 }
5203
5204 ...
5205 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5206 <52> DW_AT_decl_file : 1
5207 <53> DW_AT_decl_line : 6
5208 <54> DW_AT_import : <0x75>
5209 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5210 <59> DW_AT_name : B
5211 <5b> DW_AT_decl_file : 1
5212 <5c> DW_AT_decl_line : 2
5213 <5d> DW_AT_type : <0x6e>
5214 ...
5215 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5216 <76> DW_AT_byte_size : 4
5217 <77> DW_AT_encoding : 5 (signed)
5218
5219 imports the wrong die ( 0x75 instead of 0x58 ).
5220 This case will be ignored until the gcc bug is fixed. */
5221 return;
5222 }
5223
82856980
SW
5224 /* Figure out the local name after import. */
5225 import_alias = dwarf2_name (die, cu);
27aa8d6a 5226
794684b6
SW
5227 /* Figure out where the statement is being imported to. */
5228 import_prefix = determine_prefix (die, cu);
5229
5230 /* Figure out what the scope of the imported die is and prepend it
5231 to the name of the imported die. */
de4affc9 5232 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5233
f55ee35c
JK
5234 if (imported_die->tag != DW_TAG_namespace
5235 && imported_die->tag != DW_TAG_module)
794684b6 5236 {
13387711
SW
5237 imported_declaration = imported_name;
5238 canonical_name = imported_name_prefix;
794684b6 5239 }
13387711 5240 else if (strlen (imported_name_prefix) > 0)
794684b6 5241 {
13387711
SW
5242 temp = alloca (strlen (imported_name_prefix)
5243 + 2 + strlen (imported_name) + 1);
5244 strcpy (temp, imported_name_prefix);
5245 strcat (temp, "::");
5246 strcat (temp, imported_name);
5247 canonical_name = temp;
794684b6 5248 }
13387711
SW
5249 else
5250 canonical_name = imported_name;
794684b6 5251
c0cc3a76
SW
5252 cp_add_using_directive (import_prefix,
5253 canonical_name,
5254 import_alias,
13387711 5255 imported_declaration,
c0cc3a76 5256 &cu->objfile->objfile_obstack);
27aa8d6a
SW
5257}
5258
5fb290d7 5259static void
e142c38c 5260initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5261{
e142c38c 5262 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5263}
5264
ae2de4f8
DE
5265/* Cleanup function for read_file_scope. */
5266
cb1df416
DJ
5267static void
5268free_cu_line_header (void *arg)
5269{
5270 struct dwarf2_cu *cu = arg;
5271
5272 free_line_header (cu->line_header);
5273 cu->line_header = NULL;
5274}
5275
9291a0cd
TT
5276static void
5277find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5278 char **name, char **comp_dir)
5279{
5280 struct attribute *attr;
5281
5282 *name = NULL;
5283 *comp_dir = NULL;
5284
5285 /* Find the filename. Do not use dwarf2_name here, since the filename
5286 is not a source language identifier. */
5287 attr = dwarf2_attr (die, DW_AT_name, cu);
5288 if (attr)
5289 {
5290 *name = DW_STRING (attr);
5291 }
5292
5293 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5294 if (attr)
5295 *comp_dir = DW_STRING (attr);
5296 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5297 {
5298 *comp_dir = ldirname (*name);
5299 if (*comp_dir != NULL)
5300 make_cleanup (xfree, *comp_dir);
5301 }
5302 if (*comp_dir != NULL)
5303 {
5304 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5305 directory, get rid of it. */
5306 char *cp = strchr (*comp_dir, ':');
5307
5308 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5309 *comp_dir = cp + 1;
5310 }
5311
5312 if (*name == NULL)
5313 *name = "<unknown>";
5314}
5315
ae2de4f8
DE
5316/* Process DW_TAG_compile_unit. */
5317
c906108c 5318static void
e7c27a73 5319read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5320{
e7c27a73 5321 struct objfile *objfile = cu->objfile;
debd256d 5322 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5323 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5324 CORE_ADDR highpc = ((CORE_ADDR) 0);
5325 struct attribute *attr;
e1024ff1 5326 char *name = NULL;
c906108c
SS
5327 char *comp_dir = NULL;
5328 struct die_info *child_die;
5329 bfd *abfd = objfile->obfd;
debd256d 5330 struct line_header *line_header = 0;
e142c38c 5331 CORE_ADDR baseaddr;
6e70227d 5332
e142c38c 5333 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5334
fae299cd 5335 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5336
5337 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5338 from finish_block. */
2acceee2 5339 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5340 lowpc = highpc;
5341 lowpc += baseaddr;
5342 highpc += baseaddr;
5343
9291a0cd 5344 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5345
e142c38c 5346 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5347 if (attr)
5348 {
e142c38c 5349 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5350 }
5351
b0f35d58 5352 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5353 if (attr)
b0f35d58 5354 cu->producer = DW_STRING (attr);
303b6f5d 5355
f4b8a18d
KW
5356 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5357 standardised yet. As a workaround for the language detection we fall
5358 back to the DW_AT_producer string. */
5359 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5360 cu->language = language_opencl;
5361
0963b4bd 5362 /* We assume that we're processing GCC output. */
c906108c 5363 processing_gcc_compilation = 2;
c906108c 5364
df8a16a1
DJ
5365 processing_has_namespace_info = 0;
5366
c906108c
SS
5367 start_symtab (name, comp_dir, lowpc);
5368 record_debugformat ("DWARF 2");
303b6f5d 5369 record_producer (cu->producer);
c906108c 5370
e142c38c 5371 initialize_cu_func_list (cu);
c906108c 5372
cb1df416
DJ
5373 /* Decode line number information if present. We do this before
5374 processing child DIEs, so that the line header table is available
5375 for DW_AT_decl_file. */
e142c38c 5376 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5377 if (attr)
5378 {
debd256d 5379 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5380 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5381 if (line_header)
5382 {
cb1df416
DJ
5383 cu->line_header = line_header;
5384 make_cleanup (free_cu_line_header, cu);
aaa75496 5385 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5386 }
5fb290d7 5387 }
debd256d 5388
cb1df416
DJ
5389 /* Process all dies in compilation unit. */
5390 if (die->child != NULL)
5391 {
5392 child_die = die->child;
5393 while (child_die && child_die->tag)
5394 {
5395 process_die (child_die, cu);
5396 child_die = sibling_die (child_die);
5397 }
5398 }
5399
2e276125
JB
5400 /* Decode macro information, if present. Dwarf 2 macro information
5401 refers to information in the line number info statement program
5402 header, so we can only read it if we've read the header
5403 successfully. */
e142c38c 5404 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5405 if (attr && line_header)
2e276125
JB
5406 {
5407 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5408
2e276125 5409 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5410 comp_dir, abfd, cu);
2e276125 5411 }
debd256d 5412 do_cleanups (back_to);
5fb290d7
DJ
5413}
5414
ae2de4f8
DE
5415/* Process DW_TAG_type_unit.
5416 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5417 actual type being defined by this TU. In this case the first top
5418 level sibling is there to provide context only. */
5419
5420static void
5421read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5422{
5423 struct objfile *objfile = cu->objfile;
5424 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5425 CORE_ADDR lowpc;
5426 struct attribute *attr;
5427 char *name = NULL;
5428 char *comp_dir = NULL;
5429 struct die_info *child_die;
5430 bfd *abfd = objfile->obfd;
348e048f
DE
5431
5432 /* start_symtab needs a low pc, but we don't really have one.
5433 Do what read_file_scope would do in the absence of such info. */
5434 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5435
5436 /* Find the filename. Do not use dwarf2_name here, since the filename
5437 is not a source language identifier. */
5438 attr = dwarf2_attr (die, DW_AT_name, cu);
5439 if (attr)
5440 name = DW_STRING (attr);
5441
5442 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5443 if (attr)
5444 comp_dir = DW_STRING (attr);
5445 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5446 {
5447 comp_dir = ldirname (name);
5448 if (comp_dir != NULL)
5449 make_cleanup (xfree, comp_dir);
5450 }
5451
5452 if (name == NULL)
5453 name = "<unknown>";
5454
5455 attr = dwarf2_attr (die, DW_AT_language, cu);
5456 if (attr)
5457 set_cu_language (DW_UNSND (attr), cu);
5458
5459 /* This isn't technically needed today. It is done for symmetry
5460 with read_file_scope. */
5461 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5462 if (attr)
348e048f
DE
5463 cu->producer = DW_STRING (attr);
5464
0963b4bd 5465 /* We assume that we're processing GCC output. */
348e048f
DE
5466 processing_gcc_compilation = 2;
5467
5468 processing_has_namespace_info = 0;
5469
5470 start_symtab (name, comp_dir, lowpc);
5471 record_debugformat ("DWARF 2");
5472 record_producer (cu->producer);
5473
5474 /* Process the dies in the type unit. */
5475 if (die->child == NULL)
5476 {
5477 dump_die_for_error (die);
5478 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5479 bfd_get_filename (abfd));
5480 }
5481
5482 child_die = die->child;
5483
5484 while (child_die && child_die->tag)
5485 {
5486 process_die (child_die, cu);
5487
5488 child_die = sibling_die (child_die);
5489 }
5490
5491 do_cleanups (back_to);
5492}
5493
5fb290d7 5494static void
e142c38c
DJ
5495add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5496 struct dwarf2_cu *cu)
5fb290d7
DJ
5497{
5498 struct function_range *thisfn;
5499
5500 thisfn = (struct function_range *)
7b5a2f43 5501 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5502 thisfn->name = name;
5503 thisfn->lowpc = lowpc;
5504 thisfn->highpc = highpc;
5505 thisfn->seen_line = 0;
5506 thisfn->next = NULL;
5507
e142c38c
DJ
5508 if (cu->last_fn == NULL)
5509 cu->first_fn = thisfn;
5fb290d7 5510 else
e142c38c 5511 cu->last_fn->next = thisfn;
5fb290d7 5512
e142c38c 5513 cu->last_fn = thisfn;
c906108c
SS
5514}
5515
d389af10
JK
5516/* qsort helper for inherit_abstract_dies. */
5517
5518static int
5519unsigned_int_compar (const void *ap, const void *bp)
5520{
5521 unsigned int a = *(unsigned int *) ap;
5522 unsigned int b = *(unsigned int *) bp;
5523
5524 return (a > b) - (b > a);
5525}
5526
5527/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5528 Inherit only the children of the DW_AT_abstract_origin DIE not being
5529 already referenced by DW_AT_abstract_origin from the children of the
5530 current DIE. */
d389af10
JK
5531
5532static void
5533inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5534{
5535 struct die_info *child_die;
5536 unsigned die_children_count;
5537 /* CU offsets which were referenced by children of the current DIE. */
5538 unsigned *offsets;
5539 unsigned *offsets_end, *offsetp;
5540 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5541 struct die_info *origin_die;
5542 /* Iterator of the ORIGIN_DIE children. */
5543 struct die_info *origin_child_die;
5544 struct cleanup *cleanups;
5545 struct attribute *attr;
cd02d79d
PA
5546 struct dwarf2_cu *origin_cu;
5547 struct pending **origin_previous_list_in_scope;
d389af10
JK
5548
5549 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5550 if (!attr)
5551 return;
5552
cd02d79d
PA
5553 /* Note that following die references may follow to a die in a
5554 different cu. */
5555
5556 origin_cu = cu;
5557 origin_die = follow_die_ref (die, attr, &origin_cu);
5558
5559 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5560 symbols in. */
5561 origin_previous_list_in_scope = origin_cu->list_in_scope;
5562 origin_cu->list_in_scope = cu->list_in_scope;
5563
edb3359d
DJ
5564 if (die->tag != origin_die->tag
5565 && !(die->tag == DW_TAG_inlined_subroutine
5566 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5567 complaint (&symfile_complaints,
5568 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5569 die->offset, origin_die->offset);
5570
5571 child_die = die->child;
5572 die_children_count = 0;
5573 while (child_die && child_die->tag)
5574 {
5575 child_die = sibling_die (child_die);
5576 die_children_count++;
5577 }
5578 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5579 cleanups = make_cleanup (xfree, offsets);
5580
5581 offsets_end = offsets;
5582 child_die = die->child;
5583 while (child_die && child_die->tag)
5584 {
c38f313d
DJ
5585 /* For each CHILD_DIE, find the corresponding child of
5586 ORIGIN_DIE. If there is more than one layer of
5587 DW_AT_abstract_origin, follow them all; there shouldn't be,
5588 but GCC versions at least through 4.4 generate this (GCC PR
5589 40573). */
5590 struct die_info *child_origin_die = child_die;
cd02d79d 5591 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5592
c38f313d
DJ
5593 while (1)
5594 {
cd02d79d
PA
5595 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5596 child_origin_cu);
c38f313d
DJ
5597 if (attr == NULL)
5598 break;
cd02d79d
PA
5599 child_origin_die = follow_die_ref (child_origin_die, attr,
5600 &child_origin_cu);
c38f313d
DJ
5601 }
5602
d389af10
JK
5603 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5604 counterpart may exist. */
c38f313d 5605 if (child_origin_die != child_die)
d389af10 5606 {
edb3359d
DJ
5607 if (child_die->tag != child_origin_die->tag
5608 && !(child_die->tag == DW_TAG_inlined_subroutine
5609 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5610 complaint (&symfile_complaints,
5611 _("Child DIE 0x%x and its abstract origin 0x%x have "
5612 "different tags"), child_die->offset,
5613 child_origin_die->offset);
c38f313d
DJ
5614 if (child_origin_die->parent != origin_die)
5615 complaint (&symfile_complaints,
5616 _("Child DIE 0x%x and its abstract origin 0x%x have "
5617 "different parents"), child_die->offset,
5618 child_origin_die->offset);
5619 else
5620 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5621 }
5622 child_die = sibling_die (child_die);
5623 }
5624 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5625 unsigned_int_compar);
5626 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5627 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5628 complaint (&symfile_complaints,
5629 _("Multiple children of DIE 0x%x refer "
5630 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5631 die->offset, *offsetp);
5632
5633 offsetp = offsets;
5634 origin_child_die = origin_die->child;
5635 while (origin_child_die && origin_child_die->tag)
5636 {
5637 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5638 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5639 offsetp++;
5640 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5641 {
5642 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5643 process_die (origin_child_die, origin_cu);
d389af10
JK
5644 }
5645 origin_child_die = sibling_die (origin_child_die);
5646 }
cd02d79d 5647 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5648
5649 do_cleanups (cleanups);
5650}
5651
c906108c 5652static void
e7c27a73 5653read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5654{
e7c27a73 5655 struct objfile *objfile = cu->objfile;
52f0bd74 5656 struct context_stack *new;
c906108c
SS
5657 CORE_ADDR lowpc;
5658 CORE_ADDR highpc;
5659 struct die_info *child_die;
edb3359d 5660 struct attribute *attr, *call_line, *call_file;
c906108c 5661 char *name;
e142c38c 5662 CORE_ADDR baseaddr;
801e3a5b 5663 struct block *block;
edb3359d 5664 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5665 VEC (symbolp) *template_args = NULL;
5666 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5667
5668 if (inlined_func)
5669 {
5670 /* If we do not have call site information, we can't show the
5671 caller of this inlined function. That's too confusing, so
5672 only use the scope for local variables. */
5673 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5674 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5675 if (call_line == NULL || call_file == NULL)
5676 {
5677 read_lexical_block_scope (die, cu);
5678 return;
5679 }
5680 }
c906108c 5681
e142c38c
DJ
5682 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5683
94af9270 5684 name = dwarf2_name (die, cu);
c906108c 5685
e8d05480
JB
5686 /* Ignore functions with missing or empty names. These are actually
5687 illegal according to the DWARF standard. */
5688 if (name == NULL)
5689 {
5690 complaint (&symfile_complaints,
5691 _("missing name for subprogram DIE at %d"), die->offset);
5692 return;
5693 }
5694
5695 /* Ignore functions with missing or invalid low and high pc attributes. */
5696 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5697 {
ae4d0c03
PM
5698 attr = dwarf2_attr (die, DW_AT_external, cu);
5699 if (!attr || !DW_UNSND (attr))
5700 complaint (&symfile_complaints,
3e43a32a
MS
5701 _("cannot get low and high bounds "
5702 "for subprogram DIE at %d"),
ae4d0c03 5703 die->offset);
e8d05480
JB
5704 return;
5705 }
c906108c
SS
5706
5707 lowpc += baseaddr;
5708 highpc += baseaddr;
5709
5fb290d7 5710 /* Record the function range for dwarf_decode_lines. */
e142c38c 5711 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5712
34eaf542
TT
5713 /* If we have any template arguments, then we must allocate a
5714 different sort of symbol. */
5715 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5716 {
5717 if (child_die->tag == DW_TAG_template_type_param
5718 || child_die->tag == DW_TAG_template_value_param)
5719 {
5720 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5721 struct template_symbol);
5722 templ_func->base.is_cplus_template_function = 1;
5723 break;
5724 }
5725 }
5726
c906108c 5727 new = push_context (0, lowpc);
34eaf542
TT
5728 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5729 (struct symbol *) templ_func);
4c2df51b 5730
4cecd739
DJ
5731 /* If there is a location expression for DW_AT_frame_base, record
5732 it. */
e142c38c 5733 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5734 if (attr)
c034e007
AC
5735 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5736 expression is being recorded directly in the function's symbol
5737 and not in a separate frame-base object. I guess this hack is
5738 to avoid adding some sort of frame-base adjunct/annex to the
5739 function's symbol :-(. The problem with doing this is that it
5740 results in a function symbol with a location expression that
5741 has nothing to do with the location of the function, ouch! The
5742 relationship should be: a function's symbol has-a frame base; a
5743 frame-base has-a location expression. */
e7c27a73 5744 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5745
e142c38c 5746 cu->list_in_scope = &local_symbols;
c906108c 5747
639d11d3 5748 if (die->child != NULL)
c906108c 5749 {
639d11d3 5750 child_die = die->child;
c906108c
SS
5751 while (child_die && child_die->tag)
5752 {
34eaf542
TT
5753 if (child_die->tag == DW_TAG_template_type_param
5754 || child_die->tag == DW_TAG_template_value_param)
5755 {
5756 struct symbol *arg = new_symbol (child_die, NULL, cu);
5757
f1078f66
DJ
5758 if (arg != NULL)
5759 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5760 }
5761 else
5762 process_die (child_die, cu);
c906108c
SS
5763 child_die = sibling_die (child_die);
5764 }
5765 }
5766
d389af10
JK
5767 inherit_abstract_dies (die, cu);
5768
4a811a97
UW
5769 /* If we have a DW_AT_specification, we might need to import using
5770 directives from the context of the specification DIE. See the
5771 comment in determine_prefix. */
5772 if (cu->language == language_cplus
5773 && dwarf2_attr (die, DW_AT_specification, cu))
5774 {
5775 struct dwarf2_cu *spec_cu = cu;
5776 struct die_info *spec_die = die_specification (die, &spec_cu);
5777
5778 while (spec_die)
5779 {
5780 child_die = spec_die->child;
5781 while (child_die && child_die->tag)
5782 {
5783 if (child_die->tag == DW_TAG_imported_module)
5784 process_die (child_die, spec_cu);
5785 child_die = sibling_die (child_die);
5786 }
5787
5788 /* In some cases, GCC generates specification DIEs that
5789 themselves contain DW_AT_specification attributes. */
5790 spec_die = die_specification (spec_die, &spec_cu);
5791 }
5792 }
5793
c906108c
SS
5794 new = pop_context ();
5795 /* Make a block for the local symbols within. */
801e3a5b
JB
5796 block = finish_block (new->name, &local_symbols, new->old_blocks,
5797 lowpc, highpc, objfile);
5798
df8a16a1 5799 /* For C++, set the block's scope. */
f55ee35c 5800 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5801 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5802 determine_prefix (die, cu),
df8a16a1
DJ
5803 processing_has_namespace_info);
5804
801e3a5b
JB
5805 /* If we have address ranges, record them. */
5806 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5807
34eaf542
TT
5808 /* Attach template arguments to function. */
5809 if (! VEC_empty (symbolp, template_args))
5810 {
5811 gdb_assert (templ_func != NULL);
5812
5813 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5814 templ_func->template_arguments
5815 = obstack_alloc (&objfile->objfile_obstack,
5816 (templ_func->n_template_arguments
5817 * sizeof (struct symbol *)));
5818 memcpy (templ_func->template_arguments,
5819 VEC_address (symbolp, template_args),
5820 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5821 VEC_free (symbolp, template_args);
5822 }
5823
208d8187
JB
5824 /* In C++, we can have functions nested inside functions (e.g., when
5825 a function declares a class that has methods). This means that
5826 when we finish processing a function scope, we may need to go
5827 back to building a containing block's symbol lists. */
5828 local_symbols = new->locals;
5829 param_symbols = new->params;
27aa8d6a 5830 using_directives = new->using_directives;
208d8187 5831
921e78cf
JB
5832 /* If we've finished processing a top-level function, subsequent
5833 symbols go in the file symbol list. */
5834 if (outermost_context_p ())
e142c38c 5835 cu->list_in_scope = &file_symbols;
c906108c
SS
5836}
5837
5838/* Process all the DIES contained within a lexical block scope. Start
5839 a new scope, process the dies, and then close the scope. */
5840
5841static void
e7c27a73 5842read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5843{
e7c27a73 5844 struct objfile *objfile = cu->objfile;
52f0bd74 5845 struct context_stack *new;
c906108c
SS
5846 CORE_ADDR lowpc, highpc;
5847 struct die_info *child_die;
e142c38c
DJ
5848 CORE_ADDR baseaddr;
5849
5850 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5851
5852 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5853 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5854 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5855 be nasty. Might be easier to properly extend generic blocks to
af34e669 5856 describe ranges. */
d85a05f0 5857 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5858 return;
5859 lowpc += baseaddr;
5860 highpc += baseaddr;
5861
5862 push_context (0, lowpc);
639d11d3 5863 if (die->child != NULL)
c906108c 5864 {
639d11d3 5865 child_die = die->child;
c906108c
SS
5866 while (child_die && child_die->tag)
5867 {
e7c27a73 5868 process_die (child_die, cu);
c906108c
SS
5869 child_die = sibling_die (child_die);
5870 }
5871 }
5872 new = pop_context ();
5873
8540c487 5874 if (local_symbols != NULL || using_directives != NULL)
c906108c 5875 {
801e3a5b
JB
5876 struct block *block
5877 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5878 highpc, objfile);
5879
5880 /* Note that recording ranges after traversing children, as we
5881 do here, means that recording a parent's ranges entails
5882 walking across all its children's ranges as they appear in
5883 the address map, which is quadratic behavior.
5884
5885 It would be nicer to record the parent's ranges before
5886 traversing its children, simply overriding whatever you find
5887 there. But since we don't even decide whether to create a
5888 block until after we've traversed its children, that's hard
5889 to do. */
5890 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5891 }
5892 local_symbols = new->locals;
27aa8d6a 5893 using_directives = new->using_directives;
c906108c
SS
5894}
5895
43039443 5896/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5897 Return 1 if the attributes are present and valid, otherwise, return 0.
5898 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5899
5900static int
5901dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5902 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5903 struct partial_symtab *ranges_pst)
43039443
JK
5904{
5905 struct objfile *objfile = cu->objfile;
5906 struct comp_unit_head *cu_header = &cu->header;
5907 bfd *obfd = objfile->obfd;
5908 unsigned int addr_size = cu_header->addr_size;
5909 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5910 /* Base address selection entry. */
5911 CORE_ADDR base;
5912 int found_base;
5913 unsigned int dummy;
5914 gdb_byte *buffer;
5915 CORE_ADDR marker;
5916 int low_set;
5917 CORE_ADDR low = 0;
5918 CORE_ADDR high = 0;
ff013f42 5919 CORE_ADDR baseaddr;
43039443 5920
d00adf39
DE
5921 found_base = cu->base_known;
5922 base = cu->base_address;
43039443 5923
be391dca 5924 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5925 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5926 {
5927 complaint (&symfile_complaints,
5928 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5929 offset);
5930 return 0;
5931 }
dce234bc 5932 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5933
5934 /* Read in the largest possible address. */
5935 marker = read_address (obfd, buffer, cu, &dummy);
5936 if ((marker & mask) == mask)
5937 {
5938 /* If we found the largest possible address, then
5939 read the base address. */
5940 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5941 buffer += 2 * addr_size;
5942 offset += 2 * addr_size;
5943 found_base = 1;
5944 }
5945
5946 low_set = 0;
5947
e7030f15 5948 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5949
43039443
JK
5950 while (1)
5951 {
5952 CORE_ADDR range_beginning, range_end;
5953
5954 range_beginning = read_address (obfd, buffer, cu, &dummy);
5955 buffer += addr_size;
5956 range_end = read_address (obfd, buffer, cu, &dummy);
5957 buffer += addr_size;
5958 offset += 2 * addr_size;
5959
5960 /* An end of list marker is a pair of zero addresses. */
5961 if (range_beginning == 0 && range_end == 0)
5962 /* Found the end of list entry. */
5963 break;
5964
5965 /* Each base address selection entry is a pair of 2 values.
5966 The first is the largest possible address, the second is
5967 the base address. Check for a base address here. */
5968 if ((range_beginning & mask) == mask)
5969 {
5970 /* If we found the largest possible address, then
5971 read the base address. */
5972 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5973 found_base = 1;
5974 continue;
5975 }
5976
5977 if (!found_base)
5978 {
5979 /* We have no valid base address for the ranges
5980 data. */
5981 complaint (&symfile_complaints,
5982 _("Invalid .debug_ranges data (no base address)"));
5983 return 0;
5984 }
5985
9277c30c
UW
5986 if (range_beginning > range_end)
5987 {
5988 /* Inverted range entries are invalid. */
5989 complaint (&symfile_complaints,
5990 _("Invalid .debug_ranges data (inverted range)"));
5991 return 0;
5992 }
5993
5994 /* Empty range entries have no effect. */
5995 if (range_beginning == range_end)
5996 continue;
5997
43039443
JK
5998 range_beginning += base;
5999 range_end += base;
6000
9277c30c 6001 if (ranges_pst != NULL)
ff013f42 6002 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6003 range_beginning + baseaddr,
6004 range_end - 1 + baseaddr,
ff013f42
JK
6005 ranges_pst);
6006
43039443
JK
6007 /* FIXME: This is recording everything as a low-high
6008 segment of consecutive addresses. We should have a
6009 data structure for discontiguous block ranges
6010 instead. */
6011 if (! low_set)
6012 {
6013 low = range_beginning;
6014 high = range_end;
6015 low_set = 1;
6016 }
6017 else
6018 {
6019 if (range_beginning < low)
6020 low = range_beginning;
6021 if (range_end > high)
6022 high = range_end;
6023 }
6024 }
6025
6026 if (! low_set)
6027 /* If the first entry is an end-of-list marker, the range
6028 describes an empty scope, i.e. no instructions. */
6029 return 0;
6030
6031 if (low_return)
6032 *low_return = low;
6033 if (high_return)
6034 *high_return = high;
6035 return 1;
6036}
6037
af34e669
DJ
6038/* Get low and high pc attributes from a die. Return 1 if the attributes
6039 are present and valid, otherwise, return 0. Return -1 if the range is
6040 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6041static int
af34e669 6042dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6043 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6044 struct partial_symtab *pst)
c906108c
SS
6045{
6046 struct attribute *attr;
af34e669
DJ
6047 CORE_ADDR low = 0;
6048 CORE_ADDR high = 0;
6049 int ret = 0;
c906108c 6050
e142c38c 6051 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6052 if (attr)
af34e669
DJ
6053 {
6054 high = DW_ADDR (attr);
e142c38c 6055 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6056 if (attr)
6057 low = DW_ADDR (attr);
6058 else
6059 /* Found high w/o low attribute. */
6060 return 0;
6061
6062 /* Found consecutive range of addresses. */
6063 ret = 1;
6064 }
c906108c 6065 else
af34e669 6066 {
e142c38c 6067 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6068 if (attr != NULL)
6069 {
af34e669 6070 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6071 .debug_ranges section. */
d85a05f0 6072 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6073 return 0;
43039443 6074 /* Found discontinuous range of addresses. */
af34e669
DJ
6075 ret = -1;
6076 }
6077 }
c906108c 6078
9373cf26
JK
6079 /* read_partial_die has also the strict LOW < HIGH requirement. */
6080 if (high <= low)
c906108c
SS
6081 return 0;
6082
6083 /* When using the GNU linker, .gnu.linkonce. sections are used to
6084 eliminate duplicate copies of functions and vtables and such.
6085 The linker will arbitrarily choose one and discard the others.
6086 The AT_*_pc values for such functions refer to local labels in
6087 these sections. If the section from that file was discarded, the
6088 labels are not in the output, so the relocs get a value of 0.
6089 If this is a discarded function, mark the pc bounds as invalid,
6090 so that GDB will ignore it. */
72dca2f5 6091 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6092 return 0;
6093
6094 *lowpc = low;
6095 *highpc = high;
af34e669 6096 return ret;
c906108c
SS
6097}
6098
b084d499
JB
6099/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6100 its low and high PC addresses. Do nothing if these addresses could not
6101 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6102 and HIGHPC to the high address if greater than HIGHPC. */
6103
6104static void
6105dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6106 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6107 struct dwarf2_cu *cu)
6108{
6109 CORE_ADDR low, high;
6110 struct die_info *child = die->child;
6111
d85a05f0 6112 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6113 {
6114 *lowpc = min (*lowpc, low);
6115 *highpc = max (*highpc, high);
6116 }
6117
6118 /* If the language does not allow nested subprograms (either inside
6119 subprograms or lexical blocks), we're done. */
6120 if (cu->language != language_ada)
6121 return;
6e70227d 6122
b084d499
JB
6123 /* Check all the children of the given DIE. If it contains nested
6124 subprograms, then check their pc bounds. Likewise, we need to
6125 check lexical blocks as well, as they may also contain subprogram
6126 definitions. */
6127 while (child && child->tag)
6128 {
6129 if (child->tag == DW_TAG_subprogram
6130 || child->tag == DW_TAG_lexical_block)
6131 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6132 child = sibling_die (child);
6133 }
6134}
6135
fae299cd
DC
6136/* Get the low and high pc's represented by the scope DIE, and store
6137 them in *LOWPC and *HIGHPC. If the correct values can't be
6138 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6139
6140static void
6141get_scope_pc_bounds (struct die_info *die,
6142 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6143 struct dwarf2_cu *cu)
6144{
6145 CORE_ADDR best_low = (CORE_ADDR) -1;
6146 CORE_ADDR best_high = (CORE_ADDR) 0;
6147 CORE_ADDR current_low, current_high;
6148
d85a05f0 6149 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6150 {
6151 best_low = current_low;
6152 best_high = current_high;
6153 }
6154 else
6155 {
6156 struct die_info *child = die->child;
6157
6158 while (child && child->tag)
6159 {
6160 switch (child->tag) {
6161 case DW_TAG_subprogram:
b084d499 6162 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6163 break;
6164 case DW_TAG_namespace:
f55ee35c 6165 case DW_TAG_module:
fae299cd
DC
6166 /* FIXME: carlton/2004-01-16: Should we do this for
6167 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6168 that current GCC's always emit the DIEs corresponding
6169 to definitions of methods of classes as children of a
6170 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6171 the DIEs giving the declarations, which could be
6172 anywhere). But I don't see any reason why the
6173 standards says that they have to be there. */
6174 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6175
6176 if (current_low != ((CORE_ADDR) -1))
6177 {
6178 best_low = min (best_low, current_low);
6179 best_high = max (best_high, current_high);
6180 }
6181 break;
6182 default:
0963b4bd 6183 /* Ignore. */
fae299cd
DC
6184 break;
6185 }
6186
6187 child = sibling_die (child);
6188 }
6189 }
6190
6191 *lowpc = best_low;
6192 *highpc = best_high;
6193}
6194
801e3a5b
JB
6195/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6196 in DIE. */
6197static void
6198dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6199 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6200{
6201 struct attribute *attr;
6202
6203 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6204 if (attr)
6205 {
6206 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6207
801e3a5b
JB
6208 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6209 if (attr)
6210 {
6211 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6212
801e3a5b
JB
6213 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6214 }
6215 }
6216
6217 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6218 if (attr)
6219 {
6220 bfd *obfd = cu->objfile->obfd;
6221
6222 /* The value of the DW_AT_ranges attribute is the offset of the
6223 address range list in the .debug_ranges section. */
6224 unsigned long offset = DW_UNSND (attr);
dce234bc 6225 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6226
6227 /* For some target architectures, but not others, the
6228 read_address function sign-extends the addresses it returns.
6229 To recognize base address selection entries, we need a
6230 mask. */
6231 unsigned int addr_size = cu->header.addr_size;
6232 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6233
6234 /* The base address, to which the next pair is relative. Note
6235 that this 'base' is a DWARF concept: most entries in a range
6236 list are relative, to reduce the number of relocs against the
6237 debugging information. This is separate from this function's
6238 'baseaddr' argument, which GDB uses to relocate debugging
6239 information from a shared library based on the address at
6240 which the library was loaded. */
d00adf39
DE
6241 CORE_ADDR base = cu->base_address;
6242 int base_known = cu->base_known;
801e3a5b 6243
be391dca 6244 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6245 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6246 {
6247 complaint (&symfile_complaints,
6248 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6249 offset);
6250 return;
6251 }
6252
6253 for (;;)
6254 {
6255 unsigned int bytes_read;
6256 CORE_ADDR start, end;
6257
6258 start = read_address (obfd, buffer, cu, &bytes_read);
6259 buffer += bytes_read;
6260 end = read_address (obfd, buffer, cu, &bytes_read);
6261 buffer += bytes_read;
6262
6263 /* Did we find the end of the range list? */
6264 if (start == 0 && end == 0)
6265 break;
6266
6267 /* Did we find a base address selection entry? */
6268 else if ((start & base_select_mask) == base_select_mask)
6269 {
6270 base = end;
6271 base_known = 1;
6272 }
6273
6274 /* We found an ordinary address range. */
6275 else
6276 {
6277 if (!base_known)
6278 {
6279 complaint (&symfile_complaints,
3e43a32a
MS
6280 _("Invalid .debug_ranges data "
6281 "(no base address)"));
801e3a5b
JB
6282 return;
6283 }
6284
9277c30c
UW
6285 if (start > end)
6286 {
6287 /* Inverted range entries are invalid. */
6288 complaint (&symfile_complaints,
6289 _("Invalid .debug_ranges data "
6290 "(inverted range)"));
6291 return;
6292 }
6293
6294 /* Empty range entries have no effect. */
6295 if (start == end)
6296 continue;
6297
6e70227d
DE
6298 record_block_range (block,
6299 baseaddr + base + start,
801e3a5b
JB
6300 baseaddr + base + end - 1);
6301 }
6302 }
6303 }
6304}
6305
60d5a603
JK
6306/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6307 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6308 during 4.6.0 experimental. */
6309
6310static int
6311producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6312{
6313 const char *cs;
6314 int major, minor, release;
6315
6316 if (cu->producer == NULL)
6317 {
6318 /* For unknown compilers expect their behavior is DWARF version
6319 compliant.
6320
6321 GCC started to support .debug_types sections by -gdwarf-4 since
6322 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6323 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6324 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6325 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6326
6327 return 0;
6328 }
6329
6330 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6331
6332 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6333 {
6334 /* For non-GCC compilers expect their behavior is DWARF version
6335 compliant. */
6336
6337 return 0;
6338 }
6339 cs = &cu->producer[strlen ("GNU ")];
6340 while (*cs && !isdigit (*cs))
6341 cs++;
6342 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6343 {
6344 /* Not recognized as GCC. */
6345
6346 return 0;
6347 }
6348
6349 return major < 4 || (major == 4 && minor < 6);
6350}
6351
6352/* Return the default accessibility type if it is not overriden by
6353 DW_AT_accessibility. */
6354
6355static enum dwarf_access_attribute
6356dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6357{
6358 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6359 {
6360 /* The default DWARF 2 accessibility for members is public, the default
6361 accessibility for inheritance is private. */
6362
6363 if (die->tag != DW_TAG_inheritance)
6364 return DW_ACCESS_public;
6365 else
6366 return DW_ACCESS_private;
6367 }
6368 else
6369 {
6370 /* DWARF 3+ defines the default accessibility a different way. The same
6371 rules apply now for DW_TAG_inheritance as for the members and it only
6372 depends on the container kind. */
6373
6374 if (die->parent->tag == DW_TAG_class_type)
6375 return DW_ACCESS_private;
6376 else
6377 return DW_ACCESS_public;
6378 }
6379}
6380
74ac6d43
TT
6381/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6382 offset. If the attribute was not found return 0, otherwise return
6383 1. If it was found but could not properly be handled, set *OFFSET
6384 to 0. */
6385
6386static int
6387handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6388 LONGEST *offset)
6389{
6390 struct attribute *attr;
6391
6392 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6393 if (attr != NULL)
6394 {
6395 *offset = 0;
6396
6397 /* Note that we do not check for a section offset first here.
6398 This is because DW_AT_data_member_location is new in DWARF 4,
6399 so if we see it, we can assume that a constant form is really
6400 a constant and not a section offset. */
6401 if (attr_form_is_constant (attr))
6402 *offset = dwarf2_get_attr_constant_value (attr, 0);
6403 else if (attr_form_is_section_offset (attr))
6404 dwarf2_complex_location_expr_complaint ();
6405 else if (attr_form_is_block (attr))
6406 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6407 else
6408 dwarf2_complex_location_expr_complaint ();
6409
6410 return 1;
6411 }
6412
6413 return 0;
6414}
6415
c906108c
SS
6416/* Add an aggregate field to the field list. */
6417
6418static void
107d2387 6419dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6420 struct dwarf2_cu *cu)
6e70227d 6421{
e7c27a73 6422 struct objfile *objfile = cu->objfile;
5e2b427d 6423 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6424 struct nextfield *new_field;
6425 struct attribute *attr;
6426 struct field *fp;
6427 char *fieldname = "";
6428
6429 /* Allocate a new field list entry and link it in. */
6430 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6431 make_cleanup (xfree, new_field);
c906108c 6432 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6433
6434 if (die->tag == DW_TAG_inheritance)
6435 {
6436 new_field->next = fip->baseclasses;
6437 fip->baseclasses = new_field;
6438 }
6439 else
6440 {
6441 new_field->next = fip->fields;
6442 fip->fields = new_field;
6443 }
c906108c
SS
6444 fip->nfields++;
6445
e142c38c 6446 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6447 if (attr)
6448 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
6449 else
6450 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
6451 if (new_field->accessibility != DW_ACCESS_public)
6452 fip->non_public_fields = 1;
60d5a603 6453
e142c38c 6454 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6455 if (attr)
6456 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
6457 else
6458 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
6459
6460 fp = &new_field->field;
a9a9bd0f 6461
e142c38c 6462 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6463 {
74ac6d43
TT
6464 LONGEST offset;
6465
a9a9bd0f 6466 /* Data member other than a C++ static data member. */
6e70227d 6467
c906108c 6468 /* Get type of field. */
e7c27a73 6469 fp->type = die_type (die, cu);
c906108c 6470
d6a843b5 6471 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6472
c906108c 6473 /* Get bit size of field (zero if none). */
e142c38c 6474 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6475 if (attr)
6476 {
6477 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6478 }
6479 else
6480 {
6481 FIELD_BITSIZE (*fp) = 0;
6482 }
6483
6484 /* Get bit offset of field. */
74ac6d43
TT
6485 if (handle_data_member_location (die, cu, &offset))
6486 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 6487 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6488 if (attr)
6489 {
5e2b427d 6490 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6491 {
6492 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6493 additional bit offset from the MSB of the containing
6494 anonymous object to the MSB of the field. We don't
6495 have to do anything special since we don't need to
6496 know the size of the anonymous object. */
c906108c
SS
6497 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6498 }
6499 else
6500 {
6501 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6502 MSB of the anonymous object, subtract off the number of
6503 bits from the MSB of the field to the MSB of the
6504 object, and then subtract off the number of bits of
6505 the field itself. The result is the bit offset of
6506 the LSB of the field. */
c906108c
SS
6507 int anonymous_size;
6508 int bit_offset = DW_UNSND (attr);
6509
e142c38c 6510 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6511 if (attr)
6512 {
6513 /* The size of the anonymous object containing
6514 the bit field is explicit, so use the
6515 indicated size (in bytes). */
6516 anonymous_size = DW_UNSND (attr);
6517 }
6518 else
6519 {
6520 /* The size of the anonymous object containing
6521 the bit field must be inferred from the type
6522 attribute of the data member containing the
6523 bit field. */
6524 anonymous_size = TYPE_LENGTH (fp->type);
6525 }
6526 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6527 - bit_offset - FIELD_BITSIZE (*fp);
6528 }
6529 }
6530
6531 /* Get name of field. */
39cbfefa
DJ
6532 fieldname = dwarf2_name (die, cu);
6533 if (fieldname == NULL)
6534 fieldname = "";
d8151005
DJ
6535
6536 /* The name is already allocated along with this objfile, so we don't
6537 need to duplicate it for the type. */
6538 fp->name = fieldname;
c906108c
SS
6539
6540 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6541 pointer or virtual base class pointer) to private. */
e142c38c 6542 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6543 {
d48cc9dd 6544 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6545 new_field->accessibility = DW_ACCESS_private;
6546 fip->non_public_fields = 1;
6547 }
6548 }
a9a9bd0f 6549 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6550 {
a9a9bd0f
DC
6551 /* C++ static member. */
6552
6553 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6554 is a declaration, but all versions of G++ as of this writing
6555 (so through at least 3.2.1) incorrectly generate
6556 DW_TAG_variable tags. */
6e70227d 6557
ff355380 6558 const char *physname;
c906108c 6559
a9a9bd0f 6560 /* Get name of field. */
39cbfefa
DJ
6561 fieldname = dwarf2_name (die, cu);
6562 if (fieldname == NULL)
c906108c
SS
6563 return;
6564
254e6b9e 6565 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6566 if (attr
6567 /* Only create a symbol if this is an external value.
6568 new_symbol checks this and puts the value in the global symbol
6569 table, which we want. If it is not external, new_symbol
6570 will try to put the value in cu->list_in_scope which is wrong. */
6571 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6572 {
6573 /* A static const member, not much different than an enum as far as
6574 we're concerned, except that we can support more types. */
6575 new_symbol (die, NULL, cu);
6576 }
6577
2df3850c 6578 /* Get physical name. */
ff355380 6579 physname = dwarf2_physname (fieldname, die, cu);
c906108c 6580
d8151005
DJ
6581 /* The name is already allocated along with this objfile, so we don't
6582 need to duplicate it for the type. */
6583 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6584 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6585 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6586 }
6587 else if (die->tag == DW_TAG_inheritance)
6588 {
74ac6d43 6589 LONGEST offset;
d4b96c9a 6590
74ac6d43
TT
6591 /* C++ base class field. */
6592 if (handle_data_member_location (die, cu, &offset))
6593 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 6594 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6595 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6596 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6597 fip->nbaseclasses++;
6598 }
6599}
6600
98751a41
JK
6601/* Add a typedef defined in the scope of the FIP's class. */
6602
6603static void
6604dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6605 struct dwarf2_cu *cu)
6e70227d 6606{
98751a41 6607 struct objfile *objfile = cu->objfile;
98751a41
JK
6608 struct typedef_field_list *new_field;
6609 struct attribute *attr;
6610 struct typedef_field *fp;
6611 char *fieldname = "";
6612
6613 /* Allocate a new field list entry and link it in. */
6614 new_field = xzalloc (sizeof (*new_field));
6615 make_cleanup (xfree, new_field);
6616
6617 gdb_assert (die->tag == DW_TAG_typedef);
6618
6619 fp = &new_field->field;
6620
6621 /* Get name of field. */
6622 fp->name = dwarf2_name (die, cu);
6623 if (fp->name == NULL)
6624 return;
6625
6626 fp->type = read_type_die (die, cu);
6627
6628 new_field->next = fip->typedef_field_list;
6629 fip->typedef_field_list = new_field;
6630 fip->typedef_field_list_count++;
6631}
6632
c906108c
SS
6633/* Create the vector of fields, and attach it to the type. */
6634
6635static void
fba45db2 6636dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6637 struct dwarf2_cu *cu)
c906108c
SS
6638{
6639 int nfields = fip->nfields;
6640
6641 /* Record the field count, allocate space for the array of fields,
6642 and create blank accessibility bitfields if necessary. */
6643 TYPE_NFIELDS (type) = nfields;
6644 TYPE_FIELDS (type) = (struct field *)
6645 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6646 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6647
b4ba55a1 6648 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6649 {
6650 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6651
6652 TYPE_FIELD_PRIVATE_BITS (type) =
6653 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6654 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6655
6656 TYPE_FIELD_PROTECTED_BITS (type) =
6657 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6658 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6659
774b6a14
TT
6660 TYPE_FIELD_IGNORE_BITS (type) =
6661 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6662 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6663 }
6664
6665 /* If the type has baseclasses, allocate and clear a bit vector for
6666 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6667 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6668 {
6669 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6670 unsigned char *pointer;
c906108c
SS
6671
6672 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6673 pointer = TYPE_ALLOC (type, num_bytes);
6674 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6675 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6676 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6677 }
6678
3e43a32a
MS
6679 /* Copy the saved-up fields into the field vector. Start from the head of
6680 the list, adding to the tail of the field array, so that they end up in
6681 the same order in the array in which they were added to the list. */
c906108c
SS
6682 while (nfields-- > 0)
6683 {
7d0ccb61
DJ
6684 struct nextfield *fieldp;
6685
6686 if (fip->fields)
6687 {
6688 fieldp = fip->fields;
6689 fip->fields = fieldp->next;
6690 }
6691 else
6692 {
6693 fieldp = fip->baseclasses;
6694 fip->baseclasses = fieldp->next;
6695 }
6696
6697 TYPE_FIELD (type, nfields) = fieldp->field;
6698 switch (fieldp->accessibility)
c906108c 6699 {
c5aa993b 6700 case DW_ACCESS_private:
b4ba55a1
JB
6701 if (cu->language != language_ada)
6702 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6703 break;
c906108c 6704
c5aa993b 6705 case DW_ACCESS_protected:
b4ba55a1
JB
6706 if (cu->language != language_ada)
6707 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6708 break;
c906108c 6709
c5aa993b
JM
6710 case DW_ACCESS_public:
6711 break;
c906108c 6712
c5aa993b
JM
6713 default:
6714 /* Unknown accessibility. Complain and treat it as public. */
6715 {
e2e0b3e5 6716 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6717 fieldp->accessibility);
c5aa993b
JM
6718 }
6719 break;
c906108c
SS
6720 }
6721 if (nfields < fip->nbaseclasses)
6722 {
7d0ccb61 6723 switch (fieldp->virtuality)
c906108c 6724 {
c5aa993b
JM
6725 case DW_VIRTUALITY_virtual:
6726 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 6727 if (cu->language == language_ada)
a73c6dcd 6728 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
6729 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6730 break;
c906108c
SS
6731 }
6732 }
c906108c
SS
6733 }
6734}
6735
c906108c
SS
6736/* Add a member function to the proper fieldlist. */
6737
6738static void
107d2387 6739dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6740 struct type *type, struct dwarf2_cu *cu)
c906108c 6741{
e7c27a73 6742 struct objfile *objfile = cu->objfile;
c906108c
SS
6743 struct attribute *attr;
6744 struct fnfieldlist *flp;
6745 int i;
6746 struct fn_field *fnp;
6747 char *fieldname;
c906108c 6748 struct nextfnfield *new_fnfield;
f792889a 6749 struct type *this_type;
60d5a603 6750 enum dwarf_access_attribute accessibility;
c906108c 6751
b4ba55a1 6752 if (cu->language == language_ada)
a73c6dcd 6753 error (_("unexpected member function in Ada type"));
b4ba55a1 6754
2df3850c 6755 /* Get name of member function. */
39cbfefa
DJ
6756 fieldname = dwarf2_name (die, cu);
6757 if (fieldname == NULL)
2df3850c 6758 return;
c906108c 6759
c906108c
SS
6760 /* Look up member function name in fieldlist. */
6761 for (i = 0; i < fip->nfnfields; i++)
6762 {
27bfe10e 6763 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6764 break;
6765 }
6766
6767 /* Create new list element if necessary. */
6768 if (i < fip->nfnfields)
6769 flp = &fip->fnfieldlists[i];
6770 else
6771 {
6772 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6773 {
6774 fip->fnfieldlists = (struct fnfieldlist *)
6775 xrealloc (fip->fnfieldlists,
6776 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6777 * sizeof (struct fnfieldlist));
c906108c 6778 if (fip->nfnfields == 0)
c13c43fd 6779 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6780 }
6781 flp = &fip->fnfieldlists[fip->nfnfields];
6782 flp->name = fieldname;
6783 flp->length = 0;
6784 flp->head = NULL;
3da10d80 6785 i = fip->nfnfields++;
c906108c
SS
6786 }
6787
6788 /* Create a new member function field and chain it to the field list
0963b4bd 6789 entry. */
c906108c 6790 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6791 make_cleanup (xfree, new_fnfield);
c906108c
SS
6792 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6793 new_fnfield->next = flp->head;
6794 flp->head = new_fnfield;
6795 flp->length++;
6796
6797 /* Fill in the member function field info. */
6798 fnp = &new_fnfield->fnfield;
3da10d80
KS
6799
6800 /* Delay processing of the physname until later. */
6801 if (cu->language == language_cplus || cu->language == language_java)
6802 {
6803 add_to_method_list (type, i, flp->length - 1, fieldname,
6804 die, cu);
6805 }
6806 else
6807 {
1d06ead6 6808 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
6809 fnp->physname = physname ? physname : "";
6810 }
6811
c906108c 6812 fnp->type = alloc_type (objfile);
f792889a
DJ
6813 this_type = read_type_die (die, cu);
6814 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6815 {
f792889a 6816 int nparams = TYPE_NFIELDS (this_type);
c906108c 6817
f792889a 6818 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6819 of the method itself (TYPE_CODE_METHOD). */
6820 smash_to_method_type (fnp->type, type,
f792889a
DJ
6821 TYPE_TARGET_TYPE (this_type),
6822 TYPE_FIELDS (this_type),
6823 TYPE_NFIELDS (this_type),
6824 TYPE_VARARGS (this_type));
c906108c
SS
6825
6826 /* Handle static member functions.
c5aa993b 6827 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
6828 member functions. G++ helps GDB by marking the first
6829 parameter for non-static member functions (which is the this
6830 pointer) as artificial. We obtain this information from
6831 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6832 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6833 fnp->voffset = VOFFSET_STATIC;
6834 }
6835 else
e2e0b3e5 6836 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6837 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6838
6839 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6840 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6841 fnp->fcontext = die_containing_type (die, cu);
c906108c 6842
3e43a32a
MS
6843 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6844 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
6845
6846 /* Get accessibility. */
e142c38c 6847 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 6848 if (attr)
60d5a603
JK
6849 accessibility = DW_UNSND (attr);
6850 else
6851 accessibility = dwarf2_default_access_attribute (die, cu);
6852 switch (accessibility)
c906108c 6853 {
60d5a603
JK
6854 case DW_ACCESS_private:
6855 fnp->is_private = 1;
6856 break;
6857 case DW_ACCESS_protected:
6858 fnp->is_protected = 1;
6859 break;
c906108c
SS
6860 }
6861
b02dede2 6862 /* Check for artificial methods. */
e142c38c 6863 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6864 if (attr && DW_UNSND (attr) != 0)
6865 fnp->is_artificial = 1;
6866
0d564a31 6867 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6868 function. For older versions of GCC, this is an offset in the
6869 appropriate virtual table, as specified by DW_AT_containing_type.
6870 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6871 to the object address. */
6872
e142c38c 6873 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6874 if (attr)
8e19ed76 6875 {
aec5aa8b 6876 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6877 {
aec5aa8b
TT
6878 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6879 {
6880 /* Old-style GCC. */
6881 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6882 }
6883 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6884 || (DW_BLOCK (attr)->size > 1
6885 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6886 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6887 {
6888 struct dwarf_block blk;
6889 int offset;
6890
6891 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6892 ? 1 : 2);
6893 blk.size = DW_BLOCK (attr)->size - offset;
6894 blk.data = DW_BLOCK (attr)->data + offset;
6895 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6896 if ((fnp->voffset % cu->header.addr_size) != 0)
6897 dwarf2_complex_location_expr_complaint ();
6898 else
6899 fnp->voffset /= cu->header.addr_size;
6900 fnp->voffset += 2;
6901 }
6902 else
6903 dwarf2_complex_location_expr_complaint ();
6904
6905 if (!fnp->fcontext)
6906 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6907 }
3690dd37 6908 else if (attr_form_is_section_offset (attr))
8e19ed76 6909 {
4d3c2250 6910 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6911 }
6912 else
6913 {
4d3c2250
KB
6914 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6915 fieldname);
8e19ed76 6916 }
0d564a31 6917 }
d48cc9dd
DJ
6918 else
6919 {
6920 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6921 if (attr && DW_UNSND (attr))
6922 {
6923 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6924 complaint (&symfile_complaints,
3e43a32a
MS
6925 _("Member function \"%s\" (offset %d) is virtual "
6926 "but the vtable offset is not specified"),
d48cc9dd 6927 fieldname, die->offset);
9655fd1a 6928 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6929 TYPE_CPLUS_DYNAMIC (type) = 1;
6930 }
6931 }
c906108c
SS
6932}
6933
6934/* Create the vector of member function fields, and attach it to the type. */
6935
6936static void
fba45db2 6937dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6938 struct dwarf2_cu *cu)
c906108c
SS
6939{
6940 struct fnfieldlist *flp;
6941 int total_length = 0;
6942 int i;
6943
b4ba55a1 6944 if (cu->language == language_ada)
a73c6dcd 6945 error (_("unexpected member functions in Ada type"));
b4ba55a1 6946
c906108c
SS
6947 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6948 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6949 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6950
6951 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6952 {
6953 struct nextfnfield *nfp = flp->head;
6954 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6955 int k;
6956
6957 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6958 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6959 fn_flp->fn_fields = (struct fn_field *)
6960 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6961 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6962 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6963
6964 total_length += flp->length;
6965 }
6966
6967 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6968 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6969}
6970
1168df01
JB
6971/* Returns non-zero if NAME is the name of a vtable member in CU's
6972 language, zero otherwise. */
6973static int
6974is_vtable_name (const char *name, struct dwarf2_cu *cu)
6975{
6976 static const char vptr[] = "_vptr";
987504bb 6977 static const char vtable[] = "vtable";
1168df01 6978
987504bb
JJ
6979 /* Look for the C++ and Java forms of the vtable. */
6980 if ((cu->language == language_java
6981 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6982 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6983 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6984 return 1;
6985
6986 return 0;
6987}
6988
c0dd20ea 6989/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6990 functions, with the ABI-specified layout. If TYPE describes
6991 such a structure, smash it into a member function type.
61049d3b
DJ
6992
6993 GCC shouldn't do this; it should just output pointer to member DIEs.
6994 This is GCC PR debug/28767. */
c0dd20ea 6995
0b92b5bb
TT
6996static void
6997quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6998{
0b92b5bb 6999 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7000
7001 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7002 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7003 return;
c0dd20ea
DJ
7004
7005 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7006 if (TYPE_FIELD_NAME (type, 0) == NULL
7007 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7008 || TYPE_FIELD_NAME (type, 1) == NULL
7009 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7010 return;
c0dd20ea
DJ
7011
7012 /* Find the type of the method. */
0b92b5bb 7013 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7014 if (pfn_type == NULL
7015 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7016 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7017 return;
c0dd20ea
DJ
7018
7019 /* Look for the "this" argument. */
7020 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7021 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7022 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7023 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7024 return;
c0dd20ea
DJ
7025
7026 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7027 new_type = alloc_type (objfile);
7028 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7029 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7030 TYPE_VARARGS (pfn_type));
0b92b5bb 7031 smash_to_methodptr_type (type, new_type);
c0dd20ea 7032}
1168df01 7033
c906108c 7034/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7035 (definition) to create a type for the structure or union. Fill in
7036 the type's name and general properties; the members will not be
7037 processed until process_structure_type.
c906108c 7038
c767944b
DJ
7039 NOTE: we need to call these functions regardless of whether or not the
7040 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7041 structure or union. This gets the type entered into our set of
7042 user defined types.
7043
7044 However, if the structure is incomplete (an opaque struct/union)
7045 then suppress creating a symbol table entry for it since gdb only
7046 wants to find the one with the complete definition. Note that if
7047 it is complete, we just call new_symbol, which does it's own
7048 checking about whether the struct/union is anonymous or not (and
7049 suppresses creating a symbol table entry itself). */
7050
f792889a 7051static struct type *
134d01f1 7052read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7053{
e7c27a73 7054 struct objfile *objfile = cu->objfile;
c906108c
SS
7055 struct type *type;
7056 struct attribute *attr;
39cbfefa 7057 char *name;
c906108c 7058
348e048f
DE
7059 /* If the definition of this type lives in .debug_types, read that type.
7060 Don't follow DW_AT_specification though, that will take us back up
7061 the chain and we want to go down. */
7062 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7063 if (attr)
7064 {
7065 struct dwarf2_cu *type_cu = cu;
7066 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7067
348e048f
DE
7068 /* We could just recurse on read_structure_type, but we need to call
7069 get_die_type to ensure only one type for this DIE is created.
7070 This is important, for example, because for c++ classes we need
7071 TYPE_NAME set which is only done by new_symbol. Blech. */
7072 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7073
7074 /* TYPE_CU may not be the same as CU.
7075 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7076 return set_die_type (die, type, cu);
7077 }
7078
c0dd20ea 7079 type = alloc_type (objfile);
c906108c 7080 INIT_CPLUS_SPECIFIC (type);
93311388 7081
39cbfefa
DJ
7082 name = dwarf2_name (die, cu);
7083 if (name != NULL)
c906108c 7084 {
987504bb
JJ
7085 if (cu->language == language_cplus
7086 || cu->language == language_java)
63d06c5c 7087 {
3da10d80
KS
7088 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7089
7090 /* dwarf2_full_name might have already finished building the DIE's
7091 type. If so, there is no need to continue. */
7092 if (get_die_type (die, cu) != NULL)
7093 return get_die_type (die, cu);
7094
7095 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7096 if (die->tag == DW_TAG_structure_type
7097 || die->tag == DW_TAG_class_type)
7098 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7099 }
7100 else
7101 {
d8151005
DJ
7102 /* The name is already allocated along with this objfile, so
7103 we don't need to duplicate it for the type. */
94af9270
KS
7104 TYPE_TAG_NAME (type) = (char *) name;
7105 if (die->tag == DW_TAG_class_type)
7106 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7107 }
c906108c
SS
7108 }
7109
7110 if (die->tag == DW_TAG_structure_type)
7111 {
7112 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7113 }
7114 else if (die->tag == DW_TAG_union_type)
7115 {
7116 TYPE_CODE (type) = TYPE_CODE_UNION;
7117 }
7118 else
7119 {
c906108c
SS
7120 TYPE_CODE (type) = TYPE_CODE_CLASS;
7121 }
7122
0cc2414c
TT
7123 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7124 TYPE_DECLARED_CLASS (type) = 1;
7125
e142c38c 7126 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7127 if (attr)
7128 {
7129 TYPE_LENGTH (type) = DW_UNSND (attr);
7130 }
7131 else
7132 {
7133 TYPE_LENGTH (type) = 0;
7134 }
7135
876cecd0 7136 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7137 if (die_is_declaration (die, cu))
876cecd0 7138 TYPE_STUB (type) = 1;
a6c727b2
DJ
7139 else if (attr == NULL && die->child == NULL
7140 && producer_is_realview (cu->producer))
7141 /* RealView does not output the required DW_AT_declaration
7142 on incomplete types. */
7143 TYPE_STUB (type) = 1;
dc718098 7144
c906108c
SS
7145 /* We need to add the type field to the die immediately so we don't
7146 infinitely recurse when dealing with pointers to the structure
0963b4bd 7147 type within the structure itself. */
1c379e20 7148 set_die_type (die, type, cu);
c906108c 7149
7e314c57
JK
7150 /* set_die_type should be already done. */
7151 set_descriptive_type (type, die, cu);
7152
c767944b
DJ
7153 return type;
7154}
7155
7156/* Finish creating a structure or union type, including filling in
7157 its members and creating a symbol for it. */
7158
7159static void
7160process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7161{
7162 struct objfile *objfile = cu->objfile;
7163 struct die_info *child_die = die->child;
7164 struct type *type;
7165
7166 type = get_die_type (die, cu);
7167 if (type == NULL)
7168 type = read_structure_type (die, cu);
7169
e142c38c 7170 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7171 {
7172 struct field_info fi;
7173 struct die_info *child_die;
34eaf542 7174 VEC (symbolp) *template_args = NULL;
c767944b 7175 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7176
7177 memset (&fi, 0, sizeof (struct field_info));
7178
639d11d3 7179 child_die = die->child;
c906108c
SS
7180
7181 while (child_die && child_die->tag)
7182 {
a9a9bd0f
DC
7183 if (child_die->tag == DW_TAG_member
7184 || child_die->tag == DW_TAG_variable)
c906108c 7185 {
a9a9bd0f
DC
7186 /* NOTE: carlton/2002-11-05: A C++ static data member
7187 should be a DW_TAG_member that is a declaration, but
7188 all versions of G++ as of this writing (so through at
7189 least 3.2.1) incorrectly generate DW_TAG_variable
7190 tags for them instead. */
e7c27a73 7191 dwarf2_add_field (&fi, child_die, cu);
c906108c 7192 }
8713b1b1 7193 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7194 {
0963b4bd 7195 /* C++ member function. */
e7c27a73 7196 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7197 }
7198 else if (child_die->tag == DW_TAG_inheritance)
7199 {
7200 /* C++ base class field. */
e7c27a73 7201 dwarf2_add_field (&fi, child_die, cu);
c906108c 7202 }
98751a41
JK
7203 else if (child_die->tag == DW_TAG_typedef)
7204 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7205 else if (child_die->tag == DW_TAG_template_type_param
7206 || child_die->tag == DW_TAG_template_value_param)
7207 {
7208 struct symbol *arg = new_symbol (child_die, NULL, cu);
7209
f1078f66
DJ
7210 if (arg != NULL)
7211 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7212 }
7213
c906108c
SS
7214 child_die = sibling_die (child_die);
7215 }
7216
34eaf542
TT
7217 /* Attach template arguments to type. */
7218 if (! VEC_empty (symbolp, template_args))
7219 {
7220 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7221 TYPE_N_TEMPLATE_ARGUMENTS (type)
7222 = VEC_length (symbolp, template_args);
7223 TYPE_TEMPLATE_ARGUMENTS (type)
7224 = obstack_alloc (&objfile->objfile_obstack,
7225 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7226 * sizeof (struct symbol *)));
7227 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7228 VEC_address (symbolp, template_args),
7229 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7230 * sizeof (struct symbol *)));
7231 VEC_free (symbolp, template_args);
7232 }
7233
c906108c
SS
7234 /* Attach fields and member functions to the type. */
7235 if (fi.nfields)
e7c27a73 7236 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7237 if (fi.nfnfields)
7238 {
e7c27a73 7239 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7240
c5aa993b 7241 /* Get the type which refers to the base class (possibly this
c906108c 7242 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7243 class from the DW_AT_containing_type attribute. This use of
7244 DW_AT_containing_type is a GNU extension. */
c906108c 7245
e142c38c 7246 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7247 {
e7c27a73 7248 struct type *t = die_containing_type (die, cu);
c906108c
SS
7249
7250 TYPE_VPTR_BASETYPE (type) = t;
7251 if (type == t)
7252 {
c906108c
SS
7253 int i;
7254
7255 /* Our own class provides vtbl ptr. */
7256 for (i = TYPE_NFIELDS (t) - 1;
7257 i >= TYPE_N_BASECLASSES (t);
7258 --i)
7259 {
7260 char *fieldname = TYPE_FIELD_NAME (t, i);
7261
1168df01 7262 if (is_vtable_name (fieldname, cu))
c906108c
SS
7263 {
7264 TYPE_VPTR_FIELDNO (type) = i;
7265 break;
7266 }
7267 }
7268
7269 /* Complain if virtual function table field not found. */
7270 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7271 complaint (&symfile_complaints,
3e43a32a
MS
7272 _("virtual function table pointer "
7273 "not found when defining class '%s'"),
4d3c2250
KB
7274 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7275 "");
c906108c
SS
7276 }
7277 else
7278 {
7279 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7280 }
7281 }
f6235d4c
EZ
7282 else if (cu->producer
7283 && strncmp (cu->producer,
7284 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7285 {
7286 /* The IBM XLC compiler does not provide direct indication
7287 of the containing type, but the vtable pointer is
7288 always named __vfp. */
7289
7290 int i;
7291
7292 for (i = TYPE_NFIELDS (type) - 1;
7293 i >= TYPE_N_BASECLASSES (type);
7294 --i)
7295 {
7296 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7297 {
7298 TYPE_VPTR_FIELDNO (type) = i;
7299 TYPE_VPTR_BASETYPE (type) = type;
7300 break;
7301 }
7302 }
7303 }
c906108c 7304 }
98751a41
JK
7305
7306 /* Copy fi.typedef_field_list linked list elements content into the
7307 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7308 if (fi.typedef_field_list)
7309 {
7310 int i = fi.typedef_field_list_count;
7311
a0d7a4ff 7312 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7313 TYPE_TYPEDEF_FIELD_ARRAY (type)
7314 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7315 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7316
7317 /* Reverse the list order to keep the debug info elements order. */
7318 while (--i >= 0)
7319 {
7320 struct typedef_field *dest, *src;
6e70227d 7321
98751a41
JK
7322 dest = &TYPE_TYPEDEF_FIELD (type, i);
7323 src = &fi.typedef_field_list->field;
7324 fi.typedef_field_list = fi.typedef_field_list->next;
7325 *dest = *src;
7326 }
7327 }
c767944b
DJ
7328
7329 do_cleanups (back_to);
c906108c 7330 }
63d06c5c 7331
0b92b5bb
TT
7332 quirk_gcc_member_function_pointer (type, cu->objfile);
7333
90aeadfc
DC
7334 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7335 snapshots) has been known to create a die giving a declaration
7336 for a class that has, as a child, a die giving a definition for a
7337 nested class. So we have to process our children even if the
7338 current die is a declaration. Normally, of course, a declaration
7339 won't have any children at all. */
134d01f1 7340
90aeadfc
DC
7341 while (child_die != NULL && child_die->tag)
7342 {
7343 if (child_die->tag == DW_TAG_member
7344 || child_die->tag == DW_TAG_variable
34eaf542
TT
7345 || child_die->tag == DW_TAG_inheritance
7346 || child_die->tag == DW_TAG_template_value_param
7347 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7348 {
90aeadfc 7349 /* Do nothing. */
134d01f1 7350 }
90aeadfc
DC
7351 else
7352 process_die (child_die, cu);
134d01f1 7353
90aeadfc 7354 child_die = sibling_die (child_die);
134d01f1
DJ
7355 }
7356
fa4028e9
JB
7357 /* Do not consider external references. According to the DWARF standard,
7358 these DIEs are identified by the fact that they have no byte_size
7359 attribute, and a declaration attribute. */
7360 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7361 || !die_is_declaration (die, cu))
c767944b 7362 new_symbol (die, type, cu);
134d01f1
DJ
7363}
7364
7365/* Given a DW_AT_enumeration_type die, set its type. We do not
7366 complete the type's fields yet, or create any symbols. */
c906108c 7367
f792889a 7368static struct type *
134d01f1 7369read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7370{
e7c27a73 7371 struct objfile *objfile = cu->objfile;
c906108c 7372 struct type *type;
c906108c 7373 struct attribute *attr;
0114d602 7374 const char *name;
134d01f1 7375
348e048f
DE
7376 /* If the definition of this type lives in .debug_types, read that type.
7377 Don't follow DW_AT_specification though, that will take us back up
7378 the chain and we want to go down. */
7379 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7380 if (attr)
7381 {
7382 struct dwarf2_cu *type_cu = cu;
7383 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7384
348e048f 7385 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7386
7387 /* TYPE_CU may not be the same as CU.
7388 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7389 return set_die_type (die, type, cu);
7390 }
7391
c906108c
SS
7392 type = alloc_type (objfile);
7393
7394 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7395 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7396 if (name != NULL)
0114d602 7397 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7398
e142c38c 7399 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7400 if (attr)
7401 {
7402 TYPE_LENGTH (type) = DW_UNSND (attr);
7403 }
7404 else
7405 {
7406 TYPE_LENGTH (type) = 0;
7407 }
7408
137033e9
JB
7409 /* The enumeration DIE can be incomplete. In Ada, any type can be
7410 declared as private in the package spec, and then defined only
7411 inside the package body. Such types are known as Taft Amendment
7412 Types. When another package uses such a type, an incomplete DIE
7413 may be generated by the compiler. */
02eb380e 7414 if (die_is_declaration (die, cu))
876cecd0 7415 TYPE_STUB (type) = 1;
02eb380e 7416
f792889a 7417 return set_die_type (die, type, cu);
134d01f1
DJ
7418}
7419
7420/* Given a pointer to a die which begins an enumeration, process all
7421 the dies that define the members of the enumeration, and create the
7422 symbol for the enumeration type.
7423
7424 NOTE: We reverse the order of the element list. */
7425
7426static void
7427process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7428{
f792889a 7429 struct type *this_type;
134d01f1 7430
f792889a
DJ
7431 this_type = get_die_type (die, cu);
7432 if (this_type == NULL)
7433 this_type = read_enumeration_type (die, cu);
9dc481d3 7434
639d11d3 7435 if (die->child != NULL)
c906108c 7436 {
9dc481d3
DE
7437 struct die_info *child_die;
7438 struct symbol *sym;
7439 struct field *fields = NULL;
7440 int num_fields = 0;
7441 int unsigned_enum = 1;
7442 char *name;
7443
639d11d3 7444 child_die = die->child;
c906108c
SS
7445 while (child_die && child_die->tag)
7446 {
7447 if (child_die->tag != DW_TAG_enumerator)
7448 {
e7c27a73 7449 process_die (child_die, cu);
c906108c
SS
7450 }
7451 else
7452 {
39cbfefa
DJ
7453 name = dwarf2_name (child_die, cu);
7454 if (name)
c906108c 7455 {
f792889a 7456 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7457 if (SYMBOL_VALUE (sym) < 0)
7458 unsigned_enum = 0;
7459
7460 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7461 {
7462 fields = (struct field *)
7463 xrealloc (fields,
7464 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7465 * sizeof (struct field));
c906108c
SS
7466 }
7467
3567439c 7468 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7469 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7470 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7471 FIELD_BITSIZE (fields[num_fields]) = 0;
7472
7473 num_fields++;
7474 }
7475 }
7476
7477 child_die = sibling_die (child_die);
7478 }
7479
7480 if (num_fields)
7481 {
f792889a
DJ
7482 TYPE_NFIELDS (this_type) = num_fields;
7483 TYPE_FIELDS (this_type) = (struct field *)
7484 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7485 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7486 sizeof (struct field) * num_fields);
b8c9b27d 7487 xfree (fields);
c906108c
SS
7488 }
7489 if (unsigned_enum)
876cecd0 7490 TYPE_UNSIGNED (this_type) = 1;
c906108c 7491 }
134d01f1 7492
f792889a 7493 new_symbol (die, this_type, cu);
c906108c
SS
7494}
7495
7496/* Extract all information from a DW_TAG_array_type DIE and put it in
7497 the DIE's type field. For now, this only handles one dimensional
7498 arrays. */
7499
f792889a 7500static struct type *
e7c27a73 7501read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7502{
e7c27a73 7503 struct objfile *objfile = cu->objfile;
c906108c 7504 struct die_info *child_die;
7e314c57 7505 struct type *type;
c906108c
SS
7506 struct type *element_type, *range_type, *index_type;
7507 struct type **range_types = NULL;
7508 struct attribute *attr;
7509 int ndim = 0;
7510 struct cleanup *back_to;
39cbfefa 7511 char *name;
c906108c 7512
e7c27a73 7513 element_type = die_type (die, cu);
c906108c 7514
7e314c57
JK
7515 /* The die_type call above may have already set the type for this DIE. */
7516 type = get_die_type (die, cu);
7517 if (type)
7518 return type;
7519
c906108c
SS
7520 /* Irix 6.2 native cc creates array types without children for
7521 arrays with unspecified length. */
639d11d3 7522 if (die->child == NULL)
c906108c 7523 {
46bf5051 7524 index_type = objfile_type (objfile)->builtin_int;
c906108c 7525 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7526 type = create_array_type (NULL, element_type, range_type);
7527 return set_die_type (die, type, cu);
c906108c
SS
7528 }
7529
7530 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7531 child_die = die->child;
c906108c
SS
7532 while (child_die && child_die->tag)
7533 {
7534 if (child_die->tag == DW_TAG_subrange_type)
7535 {
f792889a 7536 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7537
f792889a 7538 if (child_type != NULL)
a02abb62 7539 {
0963b4bd
MS
7540 /* The range type was succesfully read. Save it for the
7541 array type creation. */
a02abb62
JB
7542 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7543 {
7544 range_types = (struct type **)
7545 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7546 * sizeof (struct type *));
7547 if (ndim == 0)
7548 make_cleanup (free_current_contents, &range_types);
7549 }
f792889a 7550 range_types[ndim++] = child_type;
a02abb62 7551 }
c906108c
SS
7552 }
7553 child_die = sibling_die (child_die);
7554 }
7555
7556 /* Dwarf2 dimensions are output from left to right, create the
7557 necessary array types in backwards order. */
7ca2d3a3 7558
c906108c 7559 type = element_type;
7ca2d3a3
DL
7560
7561 if (read_array_order (die, cu) == DW_ORD_col_major)
7562 {
7563 int i = 0;
9a619af0 7564
7ca2d3a3
DL
7565 while (i < ndim)
7566 type = create_array_type (NULL, type, range_types[i++]);
7567 }
7568 else
7569 {
7570 while (ndim-- > 0)
7571 type = create_array_type (NULL, type, range_types[ndim]);
7572 }
c906108c 7573
f5f8a009
EZ
7574 /* Understand Dwarf2 support for vector types (like they occur on
7575 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7576 array type. This is not part of the Dwarf2/3 standard yet, but a
7577 custom vendor extension. The main difference between a regular
7578 array and the vector variant is that vectors are passed by value
7579 to functions. */
e142c38c 7580 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7581 if (attr)
ea37ba09 7582 make_vector_type (type);
f5f8a009 7583
dbc98a8b
KW
7584 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7585 implementation may choose to implement triple vectors using this
7586 attribute. */
7587 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7588 if (attr)
7589 {
7590 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7591 TYPE_LENGTH (type) = DW_UNSND (attr);
7592 else
3e43a32a
MS
7593 complaint (&symfile_complaints,
7594 _("DW_AT_byte_size for array type smaller "
7595 "than the total size of elements"));
dbc98a8b
KW
7596 }
7597
39cbfefa
DJ
7598 name = dwarf2_name (die, cu);
7599 if (name)
7600 TYPE_NAME (type) = name;
6e70227d 7601
0963b4bd 7602 /* Install the type in the die. */
7e314c57
JK
7603 set_die_type (die, type, cu);
7604
7605 /* set_die_type should be already done. */
b4ba55a1
JB
7606 set_descriptive_type (type, die, cu);
7607
c906108c
SS
7608 do_cleanups (back_to);
7609
7e314c57 7610 return type;
c906108c
SS
7611}
7612
7ca2d3a3 7613static enum dwarf_array_dim_ordering
6e70227d 7614read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7615{
7616 struct attribute *attr;
7617
7618 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7619
7620 if (attr) return DW_SND (attr);
7621
0963b4bd
MS
7622 /* GNU F77 is a special case, as at 08/2004 array type info is the
7623 opposite order to the dwarf2 specification, but data is still
7624 laid out as per normal fortran.
7ca2d3a3 7625
0963b4bd
MS
7626 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7627 version checking. */
7ca2d3a3 7628
905e0470
PM
7629 if (cu->language == language_fortran
7630 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7631 {
7632 return DW_ORD_row_major;
7633 }
7634
6e70227d 7635 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7636 {
7637 case array_column_major:
7638 return DW_ORD_col_major;
7639 case array_row_major:
7640 default:
7641 return DW_ORD_row_major;
7642 };
7643}
7644
72019c9c 7645/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 7646 the DIE's type field. */
72019c9c 7647
f792889a 7648static struct type *
72019c9c
GM
7649read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7650{
7e314c57
JK
7651 struct type *domain_type, *set_type;
7652 struct attribute *attr;
f792889a 7653
7e314c57
JK
7654 domain_type = die_type (die, cu);
7655
7656 /* The die_type call above may have already set the type for this DIE. */
7657 set_type = get_die_type (die, cu);
7658 if (set_type)
7659 return set_type;
7660
7661 set_type = create_set_type (NULL, domain_type);
7662
7663 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7664 if (attr)
7665 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7666
f792889a 7667 return set_die_type (die, set_type, cu);
72019c9c 7668}
7ca2d3a3 7669
c906108c
SS
7670/* First cut: install each common block member as a global variable. */
7671
7672static void
e7c27a73 7673read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7674{
7675 struct die_info *child_die;
7676 struct attribute *attr;
7677 struct symbol *sym;
7678 CORE_ADDR base = (CORE_ADDR) 0;
7679
e142c38c 7680 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7681 if (attr)
7682 {
0963b4bd 7683 /* Support the .debug_loc offsets. */
8e19ed76
PS
7684 if (attr_form_is_block (attr))
7685 {
e7c27a73 7686 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7687 }
3690dd37 7688 else if (attr_form_is_section_offset (attr))
8e19ed76 7689 {
4d3c2250 7690 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7691 }
7692 else
7693 {
4d3c2250
KB
7694 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7695 "common block member");
8e19ed76 7696 }
c906108c 7697 }
639d11d3 7698 if (die->child != NULL)
c906108c 7699 {
639d11d3 7700 child_die = die->child;
c906108c
SS
7701 while (child_die && child_die->tag)
7702 {
74ac6d43
TT
7703 LONGEST offset;
7704
e7c27a73 7705 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
7706 if (sym != NULL
7707 && handle_data_member_location (child_die, cu, &offset))
c906108c 7708 {
74ac6d43 7709 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
7710 add_symbol_to_list (sym, &global_symbols);
7711 }
7712 child_die = sibling_die (child_die);
7713 }
7714 }
7715}
7716
0114d602 7717/* Create a type for a C++ namespace. */
d9fa45fe 7718
0114d602
DJ
7719static struct type *
7720read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7721{
e7c27a73 7722 struct objfile *objfile = cu->objfile;
0114d602 7723 const char *previous_prefix, *name;
9219021c 7724 int is_anonymous;
0114d602
DJ
7725 struct type *type;
7726
7727 /* For extensions, reuse the type of the original namespace. */
7728 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7729 {
7730 struct die_info *ext_die;
7731 struct dwarf2_cu *ext_cu = cu;
9a619af0 7732
0114d602
DJ
7733 ext_die = dwarf2_extension (die, &ext_cu);
7734 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7735
7736 /* EXT_CU may not be the same as CU.
7737 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7738 return set_die_type (die, type, cu);
7739 }
9219021c 7740
e142c38c 7741 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7742
7743 /* Now build the name of the current namespace. */
7744
0114d602
DJ
7745 previous_prefix = determine_prefix (die, cu);
7746 if (previous_prefix[0] != '\0')
7747 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7748 previous_prefix, name, 0, cu);
0114d602
DJ
7749
7750 /* Create the type. */
7751 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7752 objfile);
7753 TYPE_NAME (type) = (char *) name;
7754 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7755
60531b24 7756 return set_die_type (die, type, cu);
0114d602
DJ
7757}
7758
7759/* Read a C++ namespace. */
7760
7761static void
7762read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7763{
7764 struct objfile *objfile = cu->objfile;
0114d602 7765 int is_anonymous;
9219021c 7766
5c4e30ca
DC
7767 /* Add a symbol associated to this if we haven't seen the namespace
7768 before. Also, add a using directive if it's an anonymous
7769 namespace. */
9219021c 7770
f2f0e013 7771 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7772 {
7773 struct type *type;
7774
0114d602 7775 type = read_type_die (die, cu);
e7c27a73 7776 new_symbol (die, type, cu);
5c4e30ca 7777
e8e80198 7778 namespace_name (die, &is_anonymous, cu);
5c4e30ca 7779 if (is_anonymous)
0114d602
DJ
7780 {
7781 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7782
c0cc3a76 7783 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7784 NULL, &objfile->objfile_obstack);
0114d602 7785 }
5c4e30ca 7786 }
9219021c 7787
639d11d3 7788 if (die->child != NULL)
d9fa45fe 7789 {
639d11d3 7790 struct die_info *child_die = die->child;
6e70227d 7791
d9fa45fe
DC
7792 while (child_die && child_die->tag)
7793 {
e7c27a73 7794 process_die (child_die, cu);
d9fa45fe
DC
7795 child_die = sibling_die (child_die);
7796 }
7797 }
38d518c9
EZ
7798}
7799
f55ee35c
JK
7800/* Read a Fortran module as type. This DIE can be only a declaration used for
7801 imported module. Still we need that type as local Fortran "use ... only"
7802 declaration imports depend on the created type in determine_prefix. */
7803
7804static struct type *
7805read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7806{
7807 struct objfile *objfile = cu->objfile;
7808 char *module_name;
7809 struct type *type;
7810
7811 module_name = dwarf2_name (die, cu);
7812 if (!module_name)
3e43a32a
MS
7813 complaint (&symfile_complaints,
7814 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
7815 die->offset);
7816 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7817
7818 /* determine_prefix uses TYPE_TAG_NAME. */
7819 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7820
7821 return set_die_type (die, type, cu);
7822}
7823
5d7cb8df
JK
7824/* Read a Fortran module. */
7825
7826static void
7827read_module (struct die_info *die, struct dwarf2_cu *cu)
7828{
7829 struct die_info *child_die = die->child;
7830
5d7cb8df
JK
7831 while (child_die && child_die->tag)
7832 {
7833 process_die (child_die, cu);
7834 child_die = sibling_die (child_die);
7835 }
7836}
7837
38d518c9
EZ
7838/* Return the name of the namespace represented by DIE. Set
7839 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7840 namespace. */
7841
7842static const char *
e142c38c 7843namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7844{
7845 struct die_info *current_die;
7846 const char *name = NULL;
7847
7848 /* Loop through the extensions until we find a name. */
7849
7850 for (current_die = die;
7851 current_die != NULL;
f2f0e013 7852 current_die = dwarf2_extension (die, &cu))
38d518c9 7853 {
e142c38c 7854 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7855 if (name != NULL)
7856 break;
7857 }
7858
7859 /* Is it an anonymous namespace? */
7860
7861 *is_anonymous = (name == NULL);
7862 if (*is_anonymous)
7863 name = "(anonymous namespace)";
7864
7865 return name;
d9fa45fe
DC
7866}
7867
c906108c
SS
7868/* Extract all information from a DW_TAG_pointer_type DIE and add to
7869 the user defined type vector. */
7870
f792889a 7871static struct type *
e7c27a73 7872read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7873{
5e2b427d 7874 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7875 struct comp_unit_head *cu_header = &cu->header;
c906108c 7876 struct type *type;
8b2dbe47
KB
7877 struct attribute *attr_byte_size;
7878 struct attribute *attr_address_class;
7879 int byte_size, addr_class;
7e314c57
JK
7880 struct type *target_type;
7881
7882 target_type = die_type (die, cu);
c906108c 7883
7e314c57
JK
7884 /* The die_type call above may have already set the type for this DIE. */
7885 type = get_die_type (die, cu);
7886 if (type)
7887 return type;
7888
7889 type = lookup_pointer_type (target_type);
8b2dbe47 7890
e142c38c 7891 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7892 if (attr_byte_size)
7893 byte_size = DW_UNSND (attr_byte_size);
c906108c 7894 else
8b2dbe47
KB
7895 byte_size = cu_header->addr_size;
7896
e142c38c 7897 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7898 if (attr_address_class)
7899 addr_class = DW_UNSND (attr_address_class);
7900 else
7901 addr_class = DW_ADDR_none;
7902
7903 /* If the pointer size or address class is different than the
7904 default, create a type variant marked as such and set the
7905 length accordingly. */
7906 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7907 {
5e2b427d 7908 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7909 {
7910 int type_flags;
7911
849957d9 7912 type_flags = gdbarch_address_class_type_flags
5e2b427d 7913 (gdbarch, byte_size, addr_class);
876cecd0
TT
7914 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7915 == 0);
8b2dbe47
KB
7916 type = make_type_with_address_space (type, type_flags);
7917 }
7918 else if (TYPE_LENGTH (type) != byte_size)
7919 {
3e43a32a
MS
7920 complaint (&symfile_complaints,
7921 _("invalid pointer size %d"), byte_size);
8b2dbe47 7922 }
6e70227d 7923 else
9a619af0
MS
7924 {
7925 /* Should we also complain about unhandled address classes? */
7926 }
c906108c 7927 }
8b2dbe47
KB
7928
7929 TYPE_LENGTH (type) = byte_size;
f792889a 7930 return set_die_type (die, type, cu);
c906108c
SS
7931}
7932
7933/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7934 the user defined type vector. */
7935
f792889a 7936static struct type *
e7c27a73 7937read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7938{
7939 struct type *type;
7940 struct type *to_type;
7941 struct type *domain;
7942
e7c27a73
DJ
7943 to_type = die_type (die, cu);
7944 domain = die_containing_type (die, cu);
0d5de010 7945
7e314c57
JK
7946 /* The calls above may have already set the type for this DIE. */
7947 type = get_die_type (die, cu);
7948 if (type)
7949 return type;
7950
0d5de010
DJ
7951 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7952 type = lookup_methodptr_type (to_type);
7953 else
7954 type = lookup_memberptr_type (to_type, domain);
c906108c 7955
f792889a 7956 return set_die_type (die, type, cu);
c906108c
SS
7957}
7958
7959/* Extract all information from a DW_TAG_reference_type DIE and add to
7960 the user defined type vector. */
7961
f792889a 7962static struct type *
e7c27a73 7963read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7964{
e7c27a73 7965 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7966 struct type *type, *target_type;
c906108c
SS
7967 struct attribute *attr;
7968
7e314c57
JK
7969 target_type = die_type (die, cu);
7970
7971 /* The die_type call above may have already set the type for this DIE. */
7972 type = get_die_type (die, cu);
7973 if (type)
7974 return type;
7975
7976 type = lookup_reference_type (target_type);
e142c38c 7977 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7978 if (attr)
7979 {
7980 TYPE_LENGTH (type) = DW_UNSND (attr);
7981 }
7982 else
7983 {
107d2387 7984 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7985 }
f792889a 7986 return set_die_type (die, type, cu);
c906108c
SS
7987}
7988
f792889a 7989static struct type *
e7c27a73 7990read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7991{
f792889a 7992 struct type *base_type, *cv_type;
c906108c 7993
e7c27a73 7994 base_type = die_type (die, cu);
7e314c57
JK
7995
7996 /* The die_type call above may have already set the type for this DIE. */
7997 cv_type = get_die_type (die, cu);
7998 if (cv_type)
7999 return cv_type;
8000
2f608a3a
KW
8001 /* In case the const qualifier is applied to an array type, the element type
8002 is so qualified, not the array type (section 6.7.3 of C99). */
8003 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8004 {
8005 struct type *el_type, *inner_array;
8006
8007 base_type = copy_type (base_type);
8008 inner_array = base_type;
8009
8010 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8011 {
8012 TYPE_TARGET_TYPE (inner_array) =
8013 copy_type (TYPE_TARGET_TYPE (inner_array));
8014 inner_array = TYPE_TARGET_TYPE (inner_array);
8015 }
8016
8017 el_type = TYPE_TARGET_TYPE (inner_array);
8018 TYPE_TARGET_TYPE (inner_array) =
8019 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8020
8021 return set_die_type (die, base_type, cu);
8022 }
8023
f792889a
DJ
8024 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8025 return set_die_type (die, cv_type, cu);
c906108c
SS
8026}
8027
f792889a 8028static struct type *
e7c27a73 8029read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8030{
f792889a 8031 struct type *base_type, *cv_type;
c906108c 8032
e7c27a73 8033 base_type = die_type (die, cu);
7e314c57
JK
8034
8035 /* The die_type call above may have already set the type for this DIE. */
8036 cv_type = get_die_type (die, cu);
8037 if (cv_type)
8038 return cv_type;
8039
f792889a
DJ
8040 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8041 return set_die_type (die, cv_type, cu);
c906108c
SS
8042}
8043
8044/* Extract all information from a DW_TAG_string_type DIE and add to
8045 the user defined type vector. It isn't really a user defined type,
8046 but it behaves like one, with other DIE's using an AT_user_def_type
8047 attribute to reference it. */
8048
f792889a 8049static struct type *
e7c27a73 8050read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8051{
e7c27a73 8052 struct objfile *objfile = cu->objfile;
3b7538c0 8053 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8054 struct type *type, *range_type, *index_type, *char_type;
8055 struct attribute *attr;
8056 unsigned int length;
8057
e142c38c 8058 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8059 if (attr)
8060 {
8061 length = DW_UNSND (attr);
8062 }
8063 else
8064 {
0963b4bd 8065 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8066 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8067 if (attr)
8068 {
8069 length = DW_UNSND (attr);
8070 }
8071 else
8072 {
8073 length = 1;
8074 }
c906108c 8075 }
6ccb9162 8076
46bf5051 8077 index_type = objfile_type (objfile)->builtin_int;
c906108c 8078 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8079 char_type = language_string_char_type (cu->language_defn, gdbarch);
8080 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8081
f792889a 8082 return set_die_type (die, type, cu);
c906108c
SS
8083}
8084
8085/* Handle DIES due to C code like:
8086
8087 struct foo
c5aa993b
JM
8088 {
8089 int (*funcp)(int a, long l);
8090 int b;
8091 };
c906108c 8092
0963b4bd 8093 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8094
f792889a 8095static struct type *
e7c27a73 8096read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8097{
0963b4bd
MS
8098 struct type *type; /* Type that this function returns. */
8099 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8100 struct attribute *attr;
8101
e7c27a73 8102 type = die_type (die, cu);
7e314c57
JK
8103
8104 /* The die_type call above may have already set the type for this DIE. */
8105 ftype = get_die_type (die, cu);
8106 if (ftype)
8107 return ftype;
8108
0c8b41f1 8109 ftype = lookup_function_type (type);
c906108c 8110
5b8101ae 8111 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8112 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8113 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8114 || cu->language == language_cplus
5b8101ae
PM
8115 || cu->language == language_java
8116 || cu->language == language_pascal)
876cecd0 8117 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8118 else if (producer_is_realview (cu->producer))
8119 /* RealView does not emit DW_AT_prototyped. We can not
8120 distinguish prototyped and unprototyped functions; default to
8121 prototyped, since that is more common in modern code (and
8122 RealView warns about unprototyped functions). */
8123 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8124
c055b101
CV
8125 /* Store the calling convention in the type if it's available in
8126 the subroutine die. Otherwise set the calling convention to
8127 the default value DW_CC_normal. */
8128 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8129 if (attr)
8130 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8131 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8132 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8133 else
8134 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8135
8136 /* We need to add the subroutine type to the die immediately so
8137 we don't infinitely recurse when dealing with parameters
0963b4bd 8138 declared as the same subroutine type. */
76c10ea2 8139 set_die_type (die, ftype, cu);
6e70227d 8140
639d11d3 8141 if (die->child != NULL)
c906108c 8142 {
8072405b 8143 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 8144 struct die_info *child_die;
8072405b 8145 int nparams, iparams;
c906108c
SS
8146
8147 /* Count the number of parameters.
8148 FIXME: GDB currently ignores vararg functions, but knows about
8149 vararg member functions. */
8072405b 8150 nparams = 0;
639d11d3 8151 child_die = die->child;
c906108c
SS
8152 while (child_die && child_die->tag)
8153 {
8154 if (child_die->tag == DW_TAG_formal_parameter)
8155 nparams++;
8156 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8157 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8158 child_die = sibling_die (child_die);
8159 }
8160
8161 /* Allocate storage for parameters and fill them in. */
8162 TYPE_NFIELDS (ftype) = nparams;
8163 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8164 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8165
8072405b
JK
8166 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8167 even if we error out during the parameters reading below. */
8168 for (iparams = 0; iparams < nparams; iparams++)
8169 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8170
8171 iparams = 0;
639d11d3 8172 child_die = die->child;
c906108c
SS
8173 while (child_die && child_die->tag)
8174 {
8175 if (child_die->tag == DW_TAG_formal_parameter)
8176 {
3ce3b1ba
PA
8177 struct type *arg_type;
8178
8179 /* DWARF version 2 has no clean way to discern C++
8180 static and non-static member functions. G++ helps
8181 GDB by marking the first parameter for non-static
8182 member functions (which is the this pointer) as
8183 artificial. We pass this information to
8184 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8185
8186 DWARF version 3 added DW_AT_object_pointer, which GCC
8187 4.5 does not yet generate. */
e142c38c 8188 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8189 if (attr)
8190 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8191 else
418835cc
KS
8192 {
8193 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8194
8195 /* GCC/43521: In java, the formal parameter
8196 "this" is sometimes not marked with DW_AT_artificial. */
8197 if (cu->language == language_java)
8198 {
8199 const char *name = dwarf2_name (child_die, cu);
9a619af0 8200
418835cc
KS
8201 if (name && !strcmp (name, "this"))
8202 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8203 }
8204 }
3ce3b1ba
PA
8205 arg_type = die_type (child_die, cu);
8206
8207 /* RealView does not mark THIS as const, which the testsuite
8208 expects. GCC marks THIS as const in method definitions,
8209 but not in the class specifications (GCC PR 43053). */
8210 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8211 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8212 {
8213 int is_this = 0;
8214 struct dwarf2_cu *arg_cu = cu;
8215 const char *name = dwarf2_name (child_die, cu);
8216
8217 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8218 if (attr)
8219 {
8220 /* If the compiler emits this, use it. */
8221 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8222 is_this = 1;
8223 }
8224 else if (name && strcmp (name, "this") == 0)
8225 /* Function definitions will have the argument names. */
8226 is_this = 1;
8227 else if (name == NULL && iparams == 0)
8228 /* Declarations may not have the names, so like
8229 elsewhere in GDB, assume an artificial first
8230 argument is "this". */
8231 is_this = 1;
8232
8233 if (is_this)
8234 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8235 arg_type, 0);
8236 }
8237
8238 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8239 iparams++;
8240 }
8241 child_die = sibling_die (child_die);
8242 }
8243 }
8244
76c10ea2 8245 return ftype;
c906108c
SS
8246}
8247
f792889a 8248static struct type *
e7c27a73 8249read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8250{
e7c27a73 8251 struct objfile *objfile = cu->objfile;
0114d602 8252 const char *name = NULL;
f792889a 8253 struct type *this_type;
c906108c 8254
94af9270 8255 name = dwarf2_full_name (NULL, die, cu);
f792889a 8256 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8257 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8258 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8259 set_die_type (die, this_type, cu);
8260 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8261 return this_type;
c906108c
SS
8262}
8263
8264/* Find a representation of a given base type and install
8265 it in the TYPE field of the die. */
8266
f792889a 8267static struct type *
e7c27a73 8268read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8269{
e7c27a73 8270 struct objfile *objfile = cu->objfile;
c906108c
SS
8271 struct type *type;
8272 struct attribute *attr;
8273 int encoding = 0, size = 0;
39cbfefa 8274 char *name;
6ccb9162
UW
8275 enum type_code code = TYPE_CODE_INT;
8276 int type_flags = 0;
8277 struct type *target_type = NULL;
c906108c 8278
e142c38c 8279 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8280 if (attr)
8281 {
8282 encoding = DW_UNSND (attr);
8283 }
e142c38c 8284 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8285 if (attr)
8286 {
8287 size = DW_UNSND (attr);
8288 }
39cbfefa 8289 name = dwarf2_name (die, cu);
6ccb9162 8290 if (!name)
c906108c 8291 {
6ccb9162
UW
8292 complaint (&symfile_complaints,
8293 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8294 }
6ccb9162
UW
8295
8296 switch (encoding)
c906108c 8297 {
6ccb9162
UW
8298 case DW_ATE_address:
8299 /* Turn DW_ATE_address into a void * pointer. */
8300 code = TYPE_CODE_PTR;
8301 type_flags |= TYPE_FLAG_UNSIGNED;
8302 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8303 break;
8304 case DW_ATE_boolean:
8305 code = TYPE_CODE_BOOL;
8306 type_flags |= TYPE_FLAG_UNSIGNED;
8307 break;
8308 case DW_ATE_complex_float:
8309 code = TYPE_CODE_COMPLEX;
8310 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8311 break;
8312 case DW_ATE_decimal_float:
8313 code = TYPE_CODE_DECFLOAT;
8314 break;
8315 case DW_ATE_float:
8316 code = TYPE_CODE_FLT;
8317 break;
8318 case DW_ATE_signed:
8319 break;
8320 case DW_ATE_unsigned:
8321 type_flags |= TYPE_FLAG_UNSIGNED;
8322 break;
8323 case DW_ATE_signed_char:
6e70227d 8324 if (cu->language == language_ada || cu->language == language_m2
868a0084 8325 || cu->language == language_pascal)
6ccb9162
UW
8326 code = TYPE_CODE_CHAR;
8327 break;
8328 case DW_ATE_unsigned_char:
868a0084
PM
8329 if (cu->language == language_ada || cu->language == language_m2
8330 || cu->language == language_pascal)
6ccb9162
UW
8331 code = TYPE_CODE_CHAR;
8332 type_flags |= TYPE_FLAG_UNSIGNED;
8333 break;
75079b2b
TT
8334 case DW_ATE_UTF:
8335 /* We just treat this as an integer and then recognize the
8336 type by name elsewhere. */
8337 break;
8338
6ccb9162
UW
8339 default:
8340 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8341 dwarf_type_encoding_name (encoding));
8342 break;
c906108c 8343 }
6ccb9162 8344
0114d602
DJ
8345 type = init_type (code, size, type_flags, NULL, objfile);
8346 TYPE_NAME (type) = name;
6ccb9162
UW
8347 TYPE_TARGET_TYPE (type) = target_type;
8348
0114d602 8349 if (name && strcmp (name, "char") == 0)
876cecd0 8350 TYPE_NOSIGN (type) = 1;
0114d602 8351
f792889a 8352 return set_die_type (die, type, cu);
c906108c
SS
8353}
8354
a02abb62
JB
8355/* Read the given DW_AT_subrange DIE. */
8356
f792889a 8357static struct type *
a02abb62
JB
8358read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8359{
8360 struct type *base_type;
8361 struct type *range_type;
8362 struct attribute *attr;
43bbcdc2
PH
8363 LONGEST low = 0;
8364 LONGEST high = -1;
39cbfefa 8365 char *name;
43bbcdc2 8366 LONGEST negative_mask;
e77813c8 8367
a02abb62 8368 base_type = die_type (die, cu);
953ac07e
JK
8369 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8370 check_typedef (base_type);
a02abb62 8371
7e314c57
JK
8372 /* The die_type call above may have already set the type for this DIE. */
8373 range_type = get_die_type (die, cu);
8374 if (range_type)
8375 return range_type;
8376
e142c38c 8377 if (cu->language == language_fortran)
6e70227d 8378 {
a02abb62
JB
8379 /* FORTRAN implies a lower bound of 1, if not given. */
8380 low = 1;
8381 }
8382
dd5e6932
DJ
8383 /* FIXME: For variable sized arrays either of these could be
8384 a variable rather than a constant value. We'll allow it,
8385 but we don't know how to handle it. */
e142c38c 8386 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8387 if (attr)
8388 low = dwarf2_get_attr_constant_value (attr, 0);
8389
e142c38c 8390 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8391 if (attr)
6e70227d 8392 {
e77813c8 8393 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8394 {
8395 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8396 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8397 FIXME: GDB does not yet know how to handle dynamic
8398 arrays properly, treat them as arrays with unspecified
8399 length for now.
8400
8401 FIXME: jimb/2003-09-22: GDB does not really know
8402 how to handle arrays of unspecified length
8403 either; we just represent them as zero-length
8404 arrays. Choose an appropriate upper bound given
8405 the lower bound we've computed above. */
8406 high = low - 1;
8407 }
8408 else
8409 high = dwarf2_get_attr_constant_value (attr, 1);
8410 }
e77813c8
PM
8411 else
8412 {
8413 attr = dwarf2_attr (die, DW_AT_count, cu);
8414 if (attr)
8415 {
8416 int count = dwarf2_get_attr_constant_value (attr, 1);
8417 high = low + count - 1;
8418 }
c2ff108b
JK
8419 else
8420 {
8421 /* Unspecified array length. */
8422 high = low - 1;
8423 }
e77813c8
PM
8424 }
8425
8426 /* Dwarf-2 specifications explicitly allows to create subrange types
8427 without specifying a base type.
8428 In that case, the base type must be set to the type of
8429 the lower bound, upper bound or count, in that order, if any of these
8430 three attributes references an object that has a type.
8431 If no base type is found, the Dwarf-2 specifications say that
8432 a signed integer type of size equal to the size of an address should
8433 be used.
8434 For the following C code: `extern char gdb_int [];'
8435 GCC produces an empty range DIE.
8436 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 8437 high bound or count are not yet handled by this code. */
e77813c8
PM
8438 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8439 {
8440 struct objfile *objfile = cu->objfile;
8441 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8442 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8443 struct type *int_type = objfile_type (objfile)->builtin_int;
8444
8445 /* Test "int", "long int", and "long long int" objfile types,
8446 and select the first one having a size above or equal to the
8447 architecture address size. */
8448 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8449 base_type = int_type;
8450 else
8451 {
8452 int_type = objfile_type (objfile)->builtin_long;
8453 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8454 base_type = int_type;
8455 else
8456 {
8457 int_type = objfile_type (objfile)->builtin_long_long;
8458 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8459 base_type = int_type;
8460 }
8461 }
8462 }
a02abb62 8463
6e70227d 8464 negative_mask =
43bbcdc2
PH
8465 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8466 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8467 low |= negative_mask;
8468 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8469 high |= negative_mask;
8470
a02abb62
JB
8471 range_type = create_range_type (NULL, base_type, low, high);
8472
bbb0eef6
JK
8473 /* Mark arrays with dynamic length at least as an array of unspecified
8474 length. GDB could check the boundary but before it gets implemented at
8475 least allow accessing the array elements. */
8476 if (attr && attr->form == DW_FORM_block1)
8477 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8478
c2ff108b
JK
8479 /* Ada expects an empty array on no boundary attributes. */
8480 if (attr == NULL && cu->language != language_ada)
8481 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8482
39cbfefa
DJ
8483 name = dwarf2_name (die, cu);
8484 if (name)
8485 TYPE_NAME (range_type) = name;
6e70227d 8486
e142c38c 8487 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8488 if (attr)
8489 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8490
7e314c57
JK
8491 set_die_type (die, range_type, cu);
8492
8493 /* set_die_type should be already done. */
b4ba55a1
JB
8494 set_descriptive_type (range_type, die, cu);
8495
7e314c57 8496 return range_type;
a02abb62 8497}
6e70227d 8498
f792889a 8499static struct type *
81a17f79
JB
8500read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8501{
8502 struct type *type;
81a17f79 8503
81a17f79
JB
8504 /* For now, we only support the C meaning of an unspecified type: void. */
8505
0114d602
DJ
8506 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8507 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8508
f792889a 8509 return set_die_type (die, type, cu);
81a17f79 8510}
a02abb62 8511
51545339
DJ
8512/* Trivial hash function for die_info: the hash value of a DIE
8513 is its offset in .debug_info for this objfile. */
8514
8515static hashval_t
8516die_hash (const void *item)
8517{
8518 const struct die_info *die = item;
9a619af0 8519
51545339
DJ
8520 return die->offset;
8521}
8522
8523/* Trivial comparison function for die_info structures: two DIEs
8524 are equal if they have the same offset. */
8525
8526static int
8527die_eq (const void *item_lhs, const void *item_rhs)
8528{
8529 const struct die_info *die_lhs = item_lhs;
8530 const struct die_info *die_rhs = item_rhs;
9a619af0 8531
51545339
DJ
8532 return die_lhs->offset == die_rhs->offset;
8533}
8534
c906108c
SS
8535/* Read a whole compilation unit into a linked list of dies. */
8536
f9aca02d 8537static struct die_info *
93311388 8538read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8539{
93311388 8540 struct die_reader_specs reader_specs;
98bfdba5 8541 int read_abbrevs = 0;
1d9ec526 8542 struct cleanup *back_to = NULL;
98bfdba5
PA
8543 struct die_info *die;
8544
8545 if (cu->dwarf2_abbrevs == NULL)
8546 {
8547 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8548 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8549 read_abbrevs = 1;
8550 }
93311388 8551
348e048f 8552 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8553 cu->die_hash
8554 = htab_create_alloc_ex (cu->header.length / 12,
8555 die_hash,
8556 die_eq,
8557 NULL,
8558 &cu->comp_unit_obstack,
8559 hashtab_obstack_allocate,
8560 dummy_obstack_deallocate);
8561
93311388
DE
8562 init_cu_die_reader (&reader_specs, cu);
8563
98bfdba5
PA
8564 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8565
8566 if (read_abbrevs)
8567 do_cleanups (back_to);
8568
8569 return die;
639d11d3
DC
8570}
8571
d97bc12b
DE
8572/* Main entry point for reading a DIE and all children.
8573 Read the DIE and dump it if requested. */
8574
8575static struct die_info *
93311388
DE
8576read_die_and_children (const struct die_reader_specs *reader,
8577 gdb_byte *info_ptr,
d97bc12b
DE
8578 gdb_byte **new_info_ptr,
8579 struct die_info *parent)
8580{
93311388 8581 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8582 new_info_ptr, parent);
8583
8584 if (dwarf2_die_debug)
8585 {
348e048f
DE
8586 fprintf_unfiltered (gdb_stdlog,
8587 "\nRead die from %s of %s:\n",
8588 reader->buffer == dwarf2_per_objfile->info.buffer
8589 ? ".debug_info"
8590 : reader->buffer == dwarf2_per_objfile->types.buffer
8591 ? ".debug_types"
8592 : "unknown section",
8593 reader->abfd->filename);
d97bc12b
DE
8594 dump_die (result, dwarf2_die_debug);
8595 }
8596
8597 return result;
8598}
8599
639d11d3
DC
8600/* Read a single die and all its descendents. Set the die's sibling
8601 field to NULL; set other fields in the die correctly, and set all
8602 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8603 location of the info_ptr after reading all of those dies. PARENT
8604 is the parent of the die in question. */
8605
8606static struct die_info *
93311388
DE
8607read_die_and_children_1 (const struct die_reader_specs *reader,
8608 gdb_byte *info_ptr,
d97bc12b
DE
8609 gdb_byte **new_info_ptr,
8610 struct die_info *parent)
639d11d3
DC
8611{
8612 struct die_info *die;
fe1b8b76 8613 gdb_byte *cur_ptr;
639d11d3
DC
8614 int has_children;
8615
93311388 8616 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8617 if (die == NULL)
8618 {
8619 *new_info_ptr = cur_ptr;
8620 return NULL;
8621 }
93311388 8622 store_in_ref_table (die, reader->cu);
639d11d3
DC
8623
8624 if (has_children)
348e048f 8625 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8626 else
8627 {
8628 die->child = NULL;
8629 *new_info_ptr = cur_ptr;
8630 }
8631
8632 die->sibling = NULL;
8633 die->parent = parent;
8634 return die;
8635}
8636
8637/* Read a die, all of its descendents, and all of its siblings; set
8638 all of the fields of all of the dies correctly. Arguments are as
8639 in read_die_and_children. */
8640
8641static struct die_info *
93311388
DE
8642read_die_and_siblings (const struct die_reader_specs *reader,
8643 gdb_byte *info_ptr,
fe1b8b76 8644 gdb_byte **new_info_ptr,
639d11d3
DC
8645 struct die_info *parent)
8646{
8647 struct die_info *first_die, *last_sibling;
fe1b8b76 8648 gdb_byte *cur_ptr;
639d11d3 8649
c906108c 8650 cur_ptr = info_ptr;
639d11d3
DC
8651 first_die = last_sibling = NULL;
8652
8653 while (1)
c906108c 8654 {
639d11d3 8655 struct die_info *die
93311388 8656 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8657
1d325ec1 8658 if (die == NULL)
c906108c 8659 {
639d11d3
DC
8660 *new_info_ptr = cur_ptr;
8661 return first_die;
c906108c 8662 }
1d325ec1
DJ
8663
8664 if (!first_die)
8665 first_die = die;
c906108c 8666 else
1d325ec1
DJ
8667 last_sibling->sibling = die;
8668
8669 last_sibling = die;
c906108c 8670 }
c906108c
SS
8671}
8672
93311388
DE
8673/* Read the die from the .debug_info section buffer. Set DIEP to
8674 point to a newly allocated die with its information, except for its
8675 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8676 whether the die has children or not. */
8677
8678static gdb_byte *
8679read_full_die (const struct die_reader_specs *reader,
8680 struct die_info **diep, gdb_byte *info_ptr,
8681 int *has_children)
8682{
8683 unsigned int abbrev_number, bytes_read, i, offset;
8684 struct abbrev_info *abbrev;
8685 struct die_info *die;
8686 struct dwarf2_cu *cu = reader->cu;
8687 bfd *abfd = reader->abfd;
8688
8689 offset = info_ptr - reader->buffer;
8690 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8691 info_ptr += bytes_read;
8692 if (!abbrev_number)
8693 {
8694 *diep = NULL;
8695 *has_children = 0;
8696 return info_ptr;
8697 }
8698
8699 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8700 if (!abbrev)
348e048f
DE
8701 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8702 abbrev_number,
8703 bfd_get_filename (abfd));
8704
93311388
DE
8705 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8706 die->offset = offset;
8707 die->tag = abbrev->tag;
8708 die->abbrev = abbrev_number;
8709
8710 die->num_attrs = abbrev->num_attrs;
8711
8712 for (i = 0; i < abbrev->num_attrs; ++i)
8713 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8714 abfd, info_ptr, cu);
8715
8716 *diep = die;
8717 *has_children = abbrev->has_children;
8718 return info_ptr;
8719}
8720
c906108c
SS
8721/* In DWARF version 2, the description of the debugging information is
8722 stored in a separate .debug_abbrev section. Before we read any
8723 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8724 in a hash table. This function also sets flags in CU describing
8725 the data found in the abbrev table. */
c906108c
SS
8726
8727static void
e7c27a73 8728dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8729{
e7c27a73 8730 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8731 gdb_byte *abbrev_ptr;
c906108c
SS
8732 struct abbrev_info *cur_abbrev;
8733 unsigned int abbrev_number, bytes_read, abbrev_name;
8734 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8735 struct attr_abbrev *cur_attrs;
8736 unsigned int allocated_attrs;
c906108c 8737
0963b4bd 8738 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
8739 obstack_init (&cu->abbrev_obstack);
8740 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8741 (ABBREV_HASH_SIZE
8742 * sizeof (struct abbrev_info *)));
8743 memset (cu->dwarf2_abbrevs, 0,
8744 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8745
be391dca
TT
8746 dwarf2_read_section (dwarf2_per_objfile->objfile,
8747 &dwarf2_per_objfile->abbrev);
dce234bc 8748 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8749 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8750 abbrev_ptr += bytes_read;
8751
f3dd6933
DJ
8752 allocated_attrs = ATTR_ALLOC_CHUNK;
8753 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8754
0963b4bd 8755 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
8756 while (abbrev_number)
8757 {
f3dd6933 8758 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8759
8760 /* read in abbrev header */
8761 cur_abbrev->number = abbrev_number;
8762 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8763 abbrev_ptr += bytes_read;
8764 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8765 abbrev_ptr += 1;
8766
72bf9492
DJ
8767 if (cur_abbrev->tag == DW_TAG_namespace)
8768 cu->has_namespace_info = 1;
8769
c906108c
SS
8770 /* now read in declarations */
8771 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8772 abbrev_ptr += bytes_read;
8773 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8774 abbrev_ptr += bytes_read;
8775 while (abbrev_name)
8776 {
f3dd6933 8777 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8778 {
f3dd6933
DJ
8779 allocated_attrs += ATTR_ALLOC_CHUNK;
8780 cur_attrs
8781 = xrealloc (cur_attrs, (allocated_attrs
8782 * sizeof (struct attr_abbrev)));
c906108c 8783 }
ae038cb0
DJ
8784
8785 /* Record whether this compilation unit might have
8786 inter-compilation-unit references. If we don't know what form
8787 this attribute will have, then it might potentially be a
8788 DW_FORM_ref_addr, so we conservatively expect inter-CU
8789 references. */
8790
8791 if (abbrev_form == DW_FORM_ref_addr
8792 || abbrev_form == DW_FORM_indirect)
8793 cu->has_form_ref_addr = 1;
8794
f3dd6933
DJ
8795 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8796 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8797 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8798 abbrev_ptr += bytes_read;
8799 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8800 abbrev_ptr += bytes_read;
8801 }
8802
f3dd6933
DJ
8803 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8804 (cur_abbrev->num_attrs
8805 * sizeof (struct attr_abbrev)));
8806 memcpy (cur_abbrev->attrs, cur_attrs,
8807 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8808
c906108c 8809 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8810 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8811 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8812
8813 /* Get next abbreviation.
8814 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8815 always properly terminated with an abbrev number of 0.
8816 Exit loop if we encounter an abbreviation which we have
8817 already read (which means we are about to read the abbreviations
8818 for the next compile unit) or if the end of the abbreviation
8819 table is reached. */
dce234bc
PP
8820 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8821 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8822 break;
8823 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8824 abbrev_ptr += bytes_read;
e7c27a73 8825 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8826 break;
8827 }
f3dd6933
DJ
8828
8829 xfree (cur_attrs);
c906108c
SS
8830}
8831
f3dd6933 8832/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8833
c906108c 8834static void
f3dd6933 8835dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8836{
f3dd6933 8837 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8838
f3dd6933
DJ
8839 obstack_free (&cu->abbrev_obstack, NULL);
8840 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8841}
8842
8843/* Lookup an abbrev_info structure in the abbrev hash table. */
8844
8845static struct abbrev_info *
e7c27a73 8846dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8847{
8848 unsigned int hash_number;
8849 struct abbrev_info *abbrev;
8850
8851 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8852 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8853
8854 while (abbrev)
8855 {
8856 if (abbrev->number == number)
8857 return abbrev;
8858 else
8859 abbrev = abbrev->next;
8860 }
8861 return NULL;
8862}
8863
72bf9492
DJ
8864/* Returns nonzero if TAG represents a type that we might generate a partial
8865 symbol for. */
8866
8867static int
8868is_type_tag_for_partial (int tag)
8869{
8870 switch (tag)
8871 {
8872#if 0
8873 /* Some types that would be reasonable to generate partial symbols for,
8874 that we don't at present. */
8875 case DW_TAG_array_type:
8876 case DW_TAG_file_type:
8877 case DW_TAG_ptr_to_member_type:
8878 case DW_TAG_set_type:
8879 case DW_TAG_string_type:
8880 case DW_TAG_subroutine_type:
8881#endif
8882 case DW_TAG_base_type:
8883 case DW_TAG_class_type:
680b30c7 8884 case DW_TAG_interface_type:
72bf9492
DJ
8885 case DW_TAG_enumeration_type:
8886 case DW_TAG_structure_type:
8887 case DW_TAG_subrange_type:
8888 case DW_TAG_typedef:
8889 case DW_TAG_union_type:
8890 return 1;
8891 default:
8892 return 0;
8893 }
8894}
8895
8896/* Load all DIEs that are interesting for partial symbols into memory. */
8897
8898static struct partial_die_info *
93311388
DE
8899load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8900 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8901{
8902 struct partial_die_info *part_die;
8903 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8904 struct abbrev_info *abbrev;
8905 unsigned int bytes_read;
5afb4e99 8906 unsigned int load_all = 0;
72bf9492
DJ
8907
8908 int nesting_level = 1;
8909
8910 parent_die = NULL;
8911 last_die = NULL;
8912
5afb4e99
DJ
8913 if (cu->per_cu && cu->per_cu->load_all_dies)
8914 load_all = 1;
8915
72bf9492
DJ
8916 cu->partial_dies
8917 = htab_create_alloc_ex (cu->header.length / 12,
8918 partial_die_hash,
8919 partial_die_eq,
8920 NULL,
8921 &cu->comp_unit_obstack,
8922 hashtab_obstack_allocate,
8923 dummy_obstack_deallocate);
8924
8925 part_die = obstack_alloc (&cu->comp_unit_obstack,
8926 sizeof (struct partial_die_info));
8927
8928 while (1)
8929 {
8930 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8931
8932 /* A NULL abbrev means the end of a series of children. */
8933 if (abbrev == NULL)
8934 {
8935 if (--nesting_level == 0)
8936 {
8937 /* PART_DIE was probably the last thing allocated on the
8938 comp_unit_obstack, so we could call obstack_free
8939 here. We don't do that because the waste is small,
8940 and will be cleaned up when we're done with this
8941 compilation unit. This way, we're also more robust
8942 against other users of the comp_unit_obstack. */
8943 return first_die;
8944 }
8945 info_ptr += bytes_read;
8946 last_die = parent_die;
8947 parent_die = parent_die->die_parent;
8948 continue;
8949 }
8950
98bfdba5
PA
8951 /* Check for template arguments. We never save these; if
8952 they're seen, we just mark the parent, and go on our way. */
8953 if (parent_die != NULL
8954 && cu->language == language_cplus
8955 && (abbrev->tag == DW_TAG_template_type_param
8956 || abbrev->tag == DW_TAG_template_value_param))
8957 {
8958 parent_die->has_template_arguments = 1;
8959
8960 if (!load_all)
8961 {
8962 /* We don't need a partial DIE for the template argument. */
8963 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8964 cu);
8965 continue;
8966 }
8967 }
8968
8969 /* We only recurse into subprograms looking for template arguments.
8970 Skip their other children. */
8971 if (!load_all
8972 && cu->language == language_cplus
8973 && parent_die != NULL
8974 && parent_die->tag == DW_TAG_subprogram)
8975 {
8976 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8977 continue;
8978 }
8979
5afb4e99
DJ
8980 /* Check whether this DIE is interesting enough to save. Normally
8981 we would not be interested in members here, but there may be
8982 later variables referencing them via DW_AT_specification (for
8983 static members). */
8984 if (!load_all
8985 && !is_type_tag_for_partial (abbrev->tag)
72929c62 8986 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
8987 && abbrev->tag != DW_TAG_enumerator
8988 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8989 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8990 && abbrev->tag != DW_TAG_variable
5afb4e99 8991 && abbrev->tag != DW_TAG_namespace
f55ee35c 8992 && abbrev->tag != DW_TAG_module
5afb4e99 8993 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8994 {
8995 /* Otherwise we skip to the next sibling, if any. */
93311388 8996 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8997 continue;
8998 }
8999
93311388
DE
9000 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9001 buffer, info_ptr, cu);
72bf9492
DJ
9002
9003 /* This two-pass algorithm for processing partial symbols has a
9004 high cost in cache pressure. Thus, handle some simple cases
9005 here which cover the majority of C partial symbols. DIEs
9006 which neither have specification tags in them, nor could have
9007 specification tags elsewhere pointing at them, can simply be
9008 processed and discarded.
9009
9010 This segment is also optional; scan_partial_symbols and
9011 add_partial_symbol will handle these DIEs if we chain
9012 them in normally. When compilers which do not emit large
9013 quantities of duplicate debug information are more common,
9014 this code can probably be removed. */
9015
9016 /* Any complete simple types at the top level (pretty much all
9017 of them, for a language without namespaces), can be processed
9018 directly. */
9019 if (parent_die == NULL
9020 && part_die->has_specification == 0
9021 && part_die->is_declaration == 0
d8228535 9022 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9023 || part_die->tag == DW_TAG_base_type
9024 || part_die->tag == DW_TAG_subrange_type))
9025 {
9026 if (building_psymtab && part_die->name != NULL)
04a679b8 9027 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
9028 VAR_DOMAIN, LOC_TYPEDEF,
9029 &cu->objfile->static_psymbols,
9030 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 9031 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9032 continue;
9033 }
9034
d8228535
JK
9035 /* The exception for DW_TAG_typedef with has_children above is
9036 a workaround of GCC PR debug/47510. In the case of this complaint
9037 type_name_no_tag_or_error will error on such types later.
9038
9039 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9040 it could not find the child DIEs referenced later, this is checked
9041 above. In correct DWARF DW_TAG_typedef should have no children. */
9042
9043 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9044 complaint (&symfile_complaints,
9045 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9046 "- DIE at 0x%x [in module %s]"),
9047 part_die->offset, cu->objfile->name);
9048
72bf9492
DJ
9049 /* If we're at the second level, and we're an enumerator, and
9050 our parent has no specification (meaning possibly lives in a
9051 namespace elsewhere), then we can add the partial symbol now
9052 instead of queueing it. */
9053 if (part_die->tag == DW_TAG_enumerator
9054 && parent_die != NULL
9055 && parent_die->die_parent == NULL
9056 && parent_die->tag == DW_TAG_enumeration_type
9057 && parent_die->has_specification == 0)
9058 {
9059 if (part_die->name == NULL)
3e43a32a
MS
9060 complaint (&symfile_complaints,
9061 _("malformed enumerator DIE ignored"));
72bf9492 9062 else if (building_psymtab)
04a679b8 9063 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9064 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9065 (cu->language == language_cplus
9066 || cu->language == language_java)
72bf9492
DJ
9067 ? &cu->objfile->global_psymbols
9068 : &cu->objfile->static_psymbols,
9069 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9070
93311388 9071 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9072 continue;
9073 }
9074
9075 /* We'll save this DIE so link it in. */
9076 part_die->die_parent = parent_die;
9077 part_die->die_sibling = NULL;
9078 part_die->die_child = NULL;
9079
9080 if (last_die && last_die == parent_die)
9081 last_die->die_child = part_die;
9082 else if (last_die)
9083 last_die->die_sibling = part_die;
9084
9085 last_die = part_die;
9086
9087 if (first_die == NULL)
9088 first_die = part_die;
9089
9090 /* Maybe add the DIE to the hash table. Not all DIEs that we
9091 find interesting need to be in the hash table, because we
9092 also have the parent/sibling/child chains; only those that we
9093 might refer to by offset later during partial symbol reading.
9094
9095 For now this means things that might have be the target of a
9096 DW_AT_specification, DW_AT_abstract_origin, or
9097 DW_AT_extension. DW_AT_extension will refer only to
9098 namespaces; DW_AT_abstract_origin refers to functions (and
9099 many things under the function DIE, but we do not recurse
9100 into function DIEs during partial symbol reading) and
9101 possibly variables as well; DW_AT_specification refers to
9102 declarations. Declarations ought to have the DW_AT_declaration
9103 flag. It happens that GCC forgets to put it in sometimes, but
9104 only for functions, not for types.
9105
9106 Adding more things than necessary to the hash table is harmless
9107 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9108 wasted time in find_partial_die, when we reread the compilation
9109 unit with load_all_dies set. */
72bf9492 9110
5afb4e99 9111 if (load_all
72929c62 9112 || abbrev->tag == DW_TAG_constant
5afb4e99 9113 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9114 || abbrev->tag == DW_TAG_variable
9115 || abbrev->tag == DW_TAG_namespace
9116 || part_die->is_declaration)
9117 {
9118 void **slot;
9119
9120 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9121 part_die->offset, INSERT);
9122 *slot = part_die;
9123 }
9124
9125 part_die = obstack_alloc (&cu->comp_unit_obstack,
9126 sizeof (struct partial_die_info));
9127
9128 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9129 we have no reason to follow the children of structures; for other
98bfdba5
PA
9130 languages we have to, so that we can get at method physnames
9131 to infer fully qualified class names, for DW_AT_specification,
9132 and for C++ template arguments. For C++, we also look one level
9133 inside functions to find template arguments (if the name of the
9134 function does not already contain the template arguments).
bc30ff58
JB
9135
9136 For Ada, we need to scan the children of subprograms and lexical
9137 blocks as well because Ada allows the definition of nested
9138 entities that could be interesting for the debugger, such as
9139 nested subprograms for instance. */
72bf9492 9140 if (last_die->has_children
5afb4e99
DJ
9141 && (load_all
9142 || last_die->tag == DW_TAG_namespace
f55ee35c 9143 || last_die->tag == DW_TAG_module
72bf9492 9144 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9145 || (cu->language == language_cplus
9146 && last_die->tag == DW_TAG_subprogram
9147 && (last_die->name == NULL
9148 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9149 || (cu->language != language_c
9150 && (last_die->tag == DW_TAG_class_type
680b30c7 9151 || last_die->tag == DW_TAG_interface_type
72bf9492 9152 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9153 || last_die->tag == DW_TAG_union_type))
9154 || (cu->language == language_ada
9155 && (last_die->tag == DW_TAG_subprogram
9156 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9157 {
9158 nesting_level++;
9159 parent_die = last_die;
9160 continue;
9161 }
9162
9163 /* Otherwise we skip to the next sibling, if any. */
93311388 9164 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9165
9166 /* Back to the top, do it again. */
9167 }
9168}
9169
c906108c
SS
9170/* Read a minimal amount of information into the minimal die structure. */
9171
fe1b8b76 9172static gdb_byte *
72bf9492
DJ
9173read_partial_die (struct partial_die_info *part_die,
9174 struct abbrev_info *abbrev,
9175 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9176 gdb_byte *buffer, gdb_byte *info_ptr,
9177 struct dwarf2_cu *cu)
c906108c 9178{
fa238c03 9179 unsigned int i;
c906108c 9180 struct attribute attr;
c5aa993b 9181 int has_low_pc_attr = 0;
c906108c
SS
9182 int has_high_pc_attr = 0;
9183
72bf9492 9184 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9185
93311388 9186 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9187
9188 info_ptr += abbrev_len;
9189
9190 if (abbrev == NULL)
9191 return info_ptr;
9192
c906108c
SS
9193 part_die->tag = abbrev->tag;
9194 part_die->has_children = abbrev->has_children;
c906108c
SS
9195
9196 for (i = 0; i < abbrev->num_attrs; ++i)
9197 {
e7c27a73 9198 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9199
9200 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9201 partial symbol table. */
c906108c
SS
9202 switch (attr.name)
9203 {
9204 case DW_AT_name:
71c25dea
TT
9205 switch (part_die->tag)
9206 {
9207 case DW_TAG_compile_unit:
348e048f 9208 case DW_TAG_type_unit:
71c25dea
TT
9209 /* Compilation units have a DW_AT_name that is a filename, not
9210 a source language identifier. */
9211 case DW_TAG_enumeration_type:
9212 case DW_TAG_enumerator:
9213 /* These tags always have simple identifiers already; no need
9214 to canonicalize them. */
9215 part_die->name = DW_STRING (&attr);
9216 break;
9217 default:
9218 part_die->name
9219 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9220 &cu->objfile->objfile_obstack);
71c25dea
TT
9221 break;
9222 }
c906108c 9223 break;
31ef98ae 9224 case DW_AT_linkage_name:
c906108c 9225 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9226 /* Note that both forms of linkage name might appear. We
9227 assume they will be the same, and we only store the last
9228 one we see. */
94af9270
KS
9229 if (cu->language == language_ada)
9230 part_die->name = DW_STRING (&attr);
abc72ce4 9231 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9232 break;
9233 case DW_AT_low_pc:
9234 has_low_pc_attr = 1;
9235 part_die->lowpc = DW_ADDR (&attr);
9236 break;
9237 case DW_AT_high_pc:
9238 has_high_pc_attr = 1;
9239 part_die->highpc = DW_ADDR (&attr);
9240 break;
9241 case DW_AT_location:
0963b4bd 9242 /* Support the .debug_loc offsets. */
8e19ed76
PS
9243 if (attr_form_is_block (&attr))
9244 {
9245 part_die->locdesc = DW_BLOCK (&attr);
9246 }
3690dd37 9247 else if (attr_form_is_section_offset (&attr))
8e19ed76 9248 {
4d3c2250 9249 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9250 }
9251 else
9252 {
4d3c2250
KB
9253 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9254 "partial symbol information");
8e19ed76 9255 }
c906108c 9256 break;
c906108c
SS
9257 case DW_AT_external:
9258 part_die->is_external = DW_UNSND (&attr);
9259 break;
9260 case DW_AT_declaration:
9261 part_die->is_declaration = DW_UNSND (&attr);
9262 break;
9263 case DW_AT_type:
9264 part_die->has_type = 1;
9265 break;
9266 case DW_AT_abstract_origin:
9267 case DW_AT_specification:
72bf9492
DJ
9268 case DW_AT_extension:
9269 part_die->has_specification = 1;
c764a876 9270 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9271 break;
9272 case DW_AT_sibling:
9273 /* Ignore absolute siblings, they might point outside of
9274 the current compile unit. */
9275 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9276 complaint (&symfile_complaints,
9277 _("ignoring absolute DW_AT_sibling"));
c906108c 9278 else
93311388 9279 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9280 break;
fa4028e9
JB
9281 case DW_AT_byte_size:
9282 part_die->has_byte_size = 1;
9283 break;
68511cec
CES
9284 case DW_AT_calling_convention:
9285 /* DWARF doesn't provide a way to identify a program's source-level
9286 entry point. DW_AT_calling_convention attributes are only meant
9287 to describe functions' calling conventions.
9288
9289 However, because it's a necessary piece of information in
9290 Fortran, and because DW_CC_program is the only piece of debugging
9291 information whose definition refers to a 'main program' at all,
9292 several compilers have begun marking Fortran main programs with
9293 DW_CC_program --- even when those functions use the standard
9294 calling conventions.
9295
9296 So until DWARF specifies a way to provide this information and
9297 compilers pick up the new representation, we'll support this
9298 practice. */
9299 if (DW_UNSND (&attr) == DW_CC_program
9300 && cu->language == language_fortran)
01f8c46d
JK
9301 {
9302 set_main_name (part_die->name);
9303
9304 /* As this DIE has a static linkage the name would be difficult
9305 to look up later. */
9306 language_of_main = language_fortran;
9307 }
68511cec 9308 break;
c906108c
SS
9309 default:
9310 break;
9311 }
9312 }
9313
9373cf26
JK
9314 if (has_low_pc_attr && has_high_pc_attr)
9315 {
9316 /* When using the GNU linker, .gnu.linkonce. sections are used to
9317 eliminate duplicate copies of functions and vtables and such.
9318 The linker will arbitrarily choose one and discard the others.
9319 The AT_*_pc values for such functions refer to local labels in
9320 these sections. If the section from that file was discarded, the
9321 labels are not in the output, so the relocs get a value of 0.
9322 If this is a discarded function, mark the pc bounds as invalid,
9323 so that GDB will ignore it. */
9324 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9325 {
9326 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9327
9328 complaint (&symfile_complaints,
9329 _("DW_AT_low_pc %s is zero "
9330 "for DIE at 0x%x [in module %s]"),
9331 paddress (gdbarch, part_die->lowpc),
9332 part_die->offset, cu->objfile->name);
9333 }
9334 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9335 else if (part_die->lowpc >= part_die->highpc)
9336 {
9337 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9338
9339 complaint (&symfile_complaints,
9340 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9341 "for DIE at 0x%x [in module %s]"),
9342 paddress (gdbarch, part_die->lowpc),
9343 paddress (gdbarch, part_die->highpc),
9344 part_die->offset, cu->objfile->name);
9345 }
9346 else
9347 part_die->has_pc_info = 1;
9348 }
85cbf3d3 9349
c906108c
SS
9350 return info_ptr;
9351}
9352
72bf9492
DJ
9353/* Find a cached partial DIE at OFFSET in CU. */
9354
9355static struct partial_die_info *
c764a876 9356find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9357{
9358 struct partial_die_info *lookup_die = NULL;
9359 struct partial_die_info part_die;
9360
9361 part_die.offset = offset;
9362 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9363
72bf9492
DJ
9364 return lookup_die;
9365}
9366
348e048f
DE
9367/* Find a partial DIE at OFFSET, which may or may not be in CU,
9368 except in the case of .debug_types DIEs which do not reference
9369 outside their CU (they do however referencing other types via
55f1336d 9370 DW_FORM_ref_sig8). */
72bf9492
DJ
9371
9372static struct partial_die_info *
c764a876 9373find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9374{
5afb4e99
DJ
9375 struct dwarf2_per_cu_data *per_cu = NULL;
9376 struct partial_die_info *pd = NULL;
72bf9492 9377
348e048f
DE
9378 if (cu->per_cu->from_debug_types)
9379 {
9380 pd = find_partial_die_in_comp_unit (offset, cu);
9381 if (pd != NULL)
9382 return pd;
9383 goto not_found;
9384 }
9385
45452591 9386 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9387 {
9388 pd = find_partial_die_in_comp_unit (offset, cu);
9389 if (pd != NULL)
9390 return pd;
9391 }
72bf9492 9392
ae038cb0
DJ
9393 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9394
98bfdba5
PA
9395 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9396 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9397
9398 per_cu->cu->last_used = 0;
5afb4e99
DJ
9399 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9400
9401 if (pd == NULL && per_cu->load_all_dies == 0)
9402 {
9403 struct cleanup *back_to;
9404 struct partial_die_info comp_unit_die;
9405 struct abbrev_info *abbrev;
9406 unsigned int bytes_read;
9407 char *info_ptr;
9408
9409 per_cu->load_all_dies = 1;
9410
9411 /* Re-read the DIEs. */
9412 back_to = make_cleanup (null_cleanup, 0);
9413 if (per_cu->cu->dwarf2_abbrevs == NULL)
9414 {
9415 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9416 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9417 }
dce234bc 9418 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9419 + per_cu->cu->header.offset
9420 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9421 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9422 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9423 per_cu->cu->objfile->obfd,
9424 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9425 per_cu->cu);
9426 if (comp_unit_die.has_children)
93311388
DE
9427 load_partial_dies (per_cu->cu->objfile->obfd,
9428 dwarf2_per_objfile->info.buffer, info_ptr,
9429 0, per_cu->cu);
5afb4e99
DJ
9430 do_cleanups (back_to);
9431
9432 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9433 }
9434
348e048f
DE
9435 not_found:
9436
5afb4e99
DJ
9437 if (pd == NULL)
9438 internal_error (__FILE__, __LINE__,
3e43a32a
MS
9439 _("could not find partial DIE 0x%x "
9440 "in cache [from module %s]\n"),
5afb4e99
DJ
9441 offset, bfd_get_filename (cu->objfile->obfd));
9442 return pd;
72bf9492
DJ
9443}
9444
abc72ce4
DE
9445/* See if we can figure out if the class lives in a namespace. We do
9446 this by looking for a member function; its demangled name will
9447 contain namespace info, if there is any. */
9448
9449static void
9450guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9451 struct dwarf2_cu *cu)
9452{
9453 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9454 what template types look like, because the demangler
9455 frequently doesn't give the same name as the debug info. We
9456 could fix this by only using the demangled name to get the
9457 prefix (but see comment in read_structure_type). */
9458
9459 struct partial_die_info *real_pdi;
9460 struct partial_die_info *child_pdi;
9461
9462 /* If this DIE (this DIE's specification, if any) has a parent, then
9463 we should not do this. We'll prepend the parent's fully qualified
9464 name when we create the partial symbol. */
9465
9466 real_pdi = struct_pdi;
9467 while (real_pdi->has_specification)
9468 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9469
9470 if (real_pdi->die_parent != NULL)
9471 return;
9472
9473 for (child_pdi = struct_pdi->die_child;
9474 child_pdi != NULL;
9475 child_pdi = child_pdi->die_sibling)
9476 {
9477 if (child_pdi->tag == DW_TAG_subprogram
9478 && child_pdi->linkage_name != NULL)
9479 {
9480 char *actual_class_name
9481 = language_class_name_from_physname (cu->language_defn,
9482 child_pdi->linkage_name);
9483 if (actual_class_name != NULL)
9484 {
9485 struct_pdi->name
9486 = obsavestring (actual_class_name,
9487 strlen (actual_class_name),
9488 &cu->objfile->objfile_obstack);
9489 xfree (actual_class_name);
9490 }
9491 break;
9492 }
9493 }
9494}
9495
72bf9492
DJ
9496/* Adjust PART_DIE before generating a symbol for it. This function
9497 may set the is_external flag or change the DIE's name. */
9498
9499static void
9500fixup_partial_die (struct partial_die_info *part_die,
9501 struct dwarf2_cu *cu)
9502{
abc72ce4
DE
9503 /* Once we've fixed up a die, there's no point in doing so again.
9504 This also avoids a memory leak if we were to call
9505 guess_partial_die_structure_name multiple times. */
9506 if (part_die->fixup_called)
9507 return;
9508
72bf9492
DJ
9509 /* If we found a reference attribute and the DIE has no name, try
9510 to find a name in the referred to DIE. */
9511
9512 if (part_die->name == NULL && part_die->has_specification)
9513 {
9514 struct partial_die_info *spec_die;
72bf9492 9515
10b3939b 9516 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9517
10b3939b 9518 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9519
9520 if (spec_die->name)
9521 {
9522 part_die->name = spec_die->name;
9523
9524 /* Copy DW_AT_external attribute if it is set. */
9525 if (spec_die->is_external)
9526 part_die->is_external = spec_die->is_external;
9527 }
9528 }
9529
9530 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9531
9532 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9533 part_die->name = "(anonymous namespace)";
9534
abc72ce4
DE
9535 /* If there is no parent die to provide a namespace, and there are
9536 children, see if we can determine the namespace from their linkage
9537 name.
9538 NOTE: We need to do this even if cu->has_namespace_info != 0.
9539 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9540 if (cu->language == language_cplus
9541 && dwarf2_per_objfile->types.asection != NULL
9542 && part_die->die_parent == NULL
9543 && part_die->has_children
9544 && (part_die->tag == DW_TAG_class_type
9545 || part_die->tag == DW_TAG_structure_type
9546 || part_die->tag == DW_TAG_union_type))
9547 guess_partial_die_structure_name (part_die, cu);
9548
53832f31
TT
9549 /* GCC might emit a nameless struct or union that has a linkage
9550 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
9551 if (part_die->name == NULL
9552 && (part_die->tag == DW_TAG_structure_type
9553 || part_die->tag == DW_TAG_union_type
9554 || part_die->tag == DW_TAG_class_type)
9555 && part_die->linkage_name != NULL)
9556 {
9557 char *demangled;
9558
9559 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9560 if (demangled)
9561 {
9562 part_die->name = obsavestring (demangled, strlen (demangled),
9563 &cu->objfile->objfile_obstack);
9564 xfree (demangled);
9565 }
9566 }
9567
abc72ce4 9568 part_die->fixup_called = 1;
72bf9492
DJ
9569}
9570
a8329558 9571/* Read an attribute value described by an attribute form. */
c906108c 9572
fe1b8b76 9573static gdb_byte *
a8329558 9574read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9575 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9576 struct dwarf2_cu *cu)
c906108c 9577{
e7c27a73 9578 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9579 unsigned int bytes_read;
9580 struct dwarf_block *blk;
9581
a8329558
KW
9582 attr->form = form;
9583 switch (form)
c906108c 9584 {
c906108c 9585 case DW_FORM_ref_addr:
ae411497
TT
9586 if (cu->header.version == 2)
9587 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9588 else
3e43a32a
MS
9589 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9590 &cu->header, &bytes_read);
ae411497
TT
9591 info_ptr += bytes_read;
9592 break;
9593 case DW_FORM_addr:
e7c27a73 9594 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9595 info_ptr += bytes_read;
c906108c
SS
9596 break;
9597 case DW_FORM_block2:
7b5a2f43 9598 blk = dwarf_alloc_block (cu);
c906108c
SS
9599 blk->size = read_2_bytes (abfd, info_ptr);
9600 info_ptr += 2;
9601 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9602 info_ptr += blk->size;
9603 DW_BLOCK (attr) = blk;
9604 break;
9605 case DW_FORM_block4:
7b5a2f43 9606 blk = dwarf_alloc_block (cu);
c906108c
SS
9607 blk->size = read_4_bytes (abfd, info_ptr);
9608 info_ptr += 4;
9609 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9610 info_ptr += blk->size;
9611 DW_BLOCK (attr) = blk;
9612 break;
9613 case DW_FORM_data2:
9614 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9615 info_ptr += 2;
9616 break;
9617 case DW_FORM_data4:
9618 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9619 info_ptr += 4;
9620 break;
9621 case DW_FORM_data8:
9622 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9623 info_ptr += 8;
9624 break;
2dc7f7b3
TT
9625 case DW_FORM_sec_offset:
9626 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9627 info_ptr += bytes_read;
9628 break;
c906108c 9629 case DW_FORM_string:
9b1c24c8 9630 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9631 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9632 info_ptr += bytes_read;
9633 break;
4bdf3d34
JJ
9634 case DW_FORM_strp:
9635 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9636 &bytes_read);
8285870a 9637 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9638 info_ptr += bytes_read;
9639 break;
2dc7f7b3 9640 case DW_FORM_exprloc:
c906108c 9641 case DW_FORM_block:
7b5a2f43 9642 blk = dwarf_alloc_block (cu);
c906108c
SS
9643 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9644 info_ptr += bytes_read;
9645 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9646 info_ptr += blk->size;
9647 DW_BLOCK (attr) = blk;
9648 break;
9649 case DW_FORM_block1:
7b5a2f43 9650 blk = dwarf_alloc_block (cu);
c906108c
SS
9651 blk->size = read_1_byte (abfd, info_ptr);
9652 info_ptr += 1;
9653 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9654 info_ptr += blk->size;
9655 DW_BLOCK (attr) = blk;
9656 break;
9657 case DW_FORM_data1:
9658 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9659 info_ptr += 1;
9660 break;
9661 case DW_FORM_flag:
9662 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9663 info_ptr += 1;
9664 break;
2dc7f7b3
TT
9665 case DW_FORM_flag_present:
9666 DW_UNSND (attr) = 1;
9667 break;
c906108c
SS
9668 case DW_FORM_sdata:
9669 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9670 info_ptr += bytes_read;
9671 break;
9672 case DW_FORM_udata:
9673 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9674 info_ptr += bytes_read;
9675 break;
9676 case DW_FORM_ref1:
10b3939b 9677 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9678 info_ptr += 1;
9679 break;
9680 case DW_FORM_ref2:
10b3939b 9681 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9682 info_ptr += 2;
9683 break;
9684 case DW_FORM_ref4:
10b3939b 9685 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9686 info_ptr += 4;
9687 break;
613e1657 9688 case DW_FORM_ref8:
10b3939b 9689 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9690 info_ptr += 8;
9691 break;
55f1336d 9692 case DW_FORM_ref_sig8:
348e048f
DE
9693 /* Convert the signature to something we can record in DW_UNSND
9694 for later lookup.
9695 NOTE: This is NULL if the type wasn't found. */
9696 DW_SIGNATURED_TYPE (attr) =
9697 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9698 info_ptr += 8;
9699 break;
c906108c 9700 case DW_FORM_ref_udata:
10b3939b
DJ
9701 DW_ADDR (attr) = (cu->header.offset
9702 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9703 info_ptr += bytes_read;
9704 break;
c906108c 9705 case DW_FORM_indirect:
a8329558
KW
9706 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9707 info_ptr += bytes_read;
e7c27a73 9708 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9709 break;
c906108c 9710 default:
8a3fe4f8 9711 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9712 dwarf_form_name (form),
9713 bfd_get_filename (abfd));
c906108c 9714 }
28e94949
JB
9715
9716 /* We have seen instances where the compiler tried to emit a byte
9717 size attribute of -1 which ended up being encoded as an unsigned
9718 0xffffffff. Although 0xffffffff is technically a valid size value,
9719 an object of this size seems pretty unlikely so we can relatively
9720 safely treat these cases as if the size attribute was invalid and
9721 treat them as zero by default. */
9722 if (attr->name == DW_AT_byte_size
9723 && form == DW_FORM_data4
9724 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9725 {
9726 complaint
9727 (&symfile_complaints,
43bbcdc2
PH
9728 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9729 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9730 DW_UNSND (attr) = 0;
9731 }
28e94949 9732
c906108c
SS
9733 return info_ptr;
9734}
9735
a8329558
KW
9736/* Read an attribute described by an abbreviated attribute. */
9737
fe1b8b76 9738static gdb_byte *
a8329558 9739read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9740 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9741{
9742 attr->name = abbrev->name;
e7c27a73 9743 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9744}
9745
0963b4bd 9746/* Read dwarf information from a buffer. */
c906108c
SS
9747
9748static unsigned int
fe1b8b76 9749read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9750{
fe1b8b76 9751 return bfd_get_8 (abfd, buf);
c906108c
SS
9752}
9753
9754static int
fe1b8b76 9755read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9756{
fe1b8b76 9757 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9758}
9759
9760static unsigned int
fe1b8b76 9761read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9762{
fe1b8b76 9763 return bfd_get_16 (abfd, buf);
c906108c
SS
9764}
9765
9766static int
fe1b8b76 9767read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9768{
fe1b8b76 9769 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9770}
9771
9772static unsigned int
fe1b8b76 9773read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9774{
fe1b8b76 9775 return bfd_get_32 (abfd, buf);
c906108c
SS
9776}
9777
9778static int
fe1b8b76 9779read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9780{
fe1b8b76 9781 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9782}
9783
93311388 9784static ULONGEST
fe1b8b76 9785read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9786{
fe1b8b76 9787 return bfd_get_64 (abfd, buf);
c906108c
SS
9788}
9789
9790static CORE_ADDR
fe1b8b76 9791read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9792 unsigned int *bytes_read)
c906108c 9793{
e7c27a73 9794 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9795 CORE_ADDR retval = 0;
9796
107d2387 9797 if (cu_header->signed_addr_p)
c906108c 9798 {
107d2387
AC
9799 switch (cu_header->addr_size)
9800 {
9801 case 2:
fe1b8b76 9802 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9803 break;
9804 case 4:
fe1b8b76 9805 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9806 break;
9807 case 8:
fe1b8b76 9808 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9809 break;
9810 default:
8e65ff28 9811 internal_error (__FILE__, __LINE__,
e2e0b3e5 9812 _("read_address: bad switch, signed [in module %s]"),
659b0389 9813 bfd_get_filename (abfd));
107d2387
AC
9814 }
9815 }
9816 else
9817 {
9818 switch (cu_header->addr_size)
9819 {
9820 case 2:
fe1b8b76 9821 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9822 break;
9823 case 4:
fe1b8b76 9824 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9825 break;
9826 case 8:
fe1b8b76 9827 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9828 break;
9829 default:
8e65ff28 9830 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
9831 _("read_address: bad switch, "
9832 "unsigned [in module %s]"),
659b0389 9833 bfd_get_filename (abfd));
107d2387 9834 }
c906108c 9835 }
64367e0a 9836
107d2387
AC
9837 *bytes_read = cu_header->addr_size;
9838 return retval;
c906108c
SS
9839}
9840
f7ef9339 9841/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9842 specification allows the initial length to take up either 4 bytes
9843 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9844 bytes describe the length and all offsets will be 8 bytes in length
9845 instead of 4.
9846
f7ef9339
KB
9847 An older, non-standard 64-bit format is also handled by this
9848 function. The older format in question stores the initial length
9849 as an 8-byte quantity without an escape value. Lengths greater
9850 than 2^32 aren't very common which means that the initial 4 bytes
9851 is almost always zero. Since a length value of zero doesn't make
9852 sense for the 32-bit format, this initial zero can be considered to
9853 be an escape value which indicates the presence of the older 64-bit
9854 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9855 greater than 4GB. If it becomes necessary to handle lengths
9856 somewhat larger than 4GB, we could allow other small values (such
9857 as the non-sensical values of 1, 2, and 3) to also be used as
9858 escape values indicating the presence of the old format.
f7ef9339 9859
917c78fc
MK
9860 The value returned via bytes_read should be used to increment the
9861 relevant pointer after calling read_initial_length().
c764a876 9862
613e1657
KB
9863 [ Note: read_initial_length() and read_offset() are based on the
9864 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9865 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9866 from:
9867
f7ef9339 9868 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9869
613e1657
KB
9870 This document is only a draft and is subject to change. (So beware.)
9871
f7ef9339 9872 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9873 determined empirically by examining 64-bit ELF files produced by
9874 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9875
9876 - Kevin, July 16, 2002
613e1657
KB
9877 ] */
9878
9879static LONGEST
c764a876 9880read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9881{
fe1b8b76 9882 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9883
dd373385 9884 if (length == 0xffffffff)
613e1657 9885 {
fe1b8b76 9886 length = bfd_get_64 (abfd, buf + 4);
613e1657 9887 *bytes_read = 12;
613e1657 9888 }
dd373385 9889 else if (length == 0)
f7ef9339 9890 {
dd373385 9891 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9892 length = bfd_get_64 (abfd, buf);
f7ef9339 9893 *bytes_read = 8;
f7ef9339 9894 }
613e1657
KB
9895 else
9896 {
9897 *bytes_read = 4;
613e1657
KB
9898 }
9899
c764a876
DE
9900 return length;
9901}
dd373385 9902
c764a876
DE
9903/* Cover function for read_initial_length.
9904 Returns the length of the object at BUF, and stores the size of the
9905 initial length in *BYTES_READ and stores the size that offsets will be in
9906 *OFFSET_SIZE.
9907 If the initial length size is not equivalent to that specified in
9908 CU_HEADER then issue a complaint.
9909 This is useful when reading non-comp-unit headers. */
dd373385 9910
c764a876
DE
9911static LONGEST
9912read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9913 const struct comp_unit_head *cu_header,
9914 unsigned int *bytes_read,
9915 unsigned int *offset_size)
9916{
9917 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9918
9919 gdb_assert (cu_header->initial_length_size == 4
9920 || cu_header->initial_length_size == 8
9921 || cu_header->initial_length_size == 12);
9922
9923 if (cu_header->initial_length_size != *bytes_read)
9924 complaint (&symfile_complaints,
9925 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9926
c764a876 9927 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9928 return length;
613e1657
KB
9929}
9930
9931/* Read an offset from the data stream. The size of the offset is
917c78fc 9932 given by cu_header->offset_size. */
613e1657
KB
9933
9934static LONGEST
fe1b8b76 9935read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9936 unsigned int *bytes_read)
c764a876
DE
9937{
9938 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9939
c764a876
DE
9940 *bytes_read = cu_header->offset_size;
9941 return offset;
9942}
9943
9944/* Read an offset from the data stream. */
9945
9946static LONGEST
9947read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9948{
9949 LONGEST retval = 0;
9950
c764a876 9951 switch (offset_size)
613e1657
KB
9952 {
9953 case 4:
fe1b8b76 9954 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9955 break;
9956 case 8:
fe1b8b76 9957 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9958 break;
9959 default:
8e65ff28 9960 internal_error (__FILE__, __LINE__,
c764a876 9961 _("read_offset_1: bad switch [in module %s]"),
659b0389 9962 bfd_get_filename (abfd));
613e1657
KB
9963 }
9964
917c78fc 9965 return retval;
613e1657
KB
9966}
9967
fe1b8b76
JB
9968static gdb_byte *
9969read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9970{
9971 /* If the size of a host char is 8 bits, we can return a pointer
9972 to the buffer, otherwise we have to copy the data to a buffer
9973 allocated on the temporary obstack. */
4bdf3d34 9974 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9975 return buf;
c906108c
SS
9976}
9977
9978static char *
9b1c24c8 9979read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9980{
9981 /* If the size of a host char is 8 bits, we can return a pointer
9982 to the string, otherwise we have to copy the string to a buffer
9983 allocated on the temporary obstack. */
4bdf3d34 9984 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9985 if (*buf == '\0')
9986 {
9987 *bytes_read_ptr = 1;
9988 return NULL;
9989 }
fe1b8b76
JB
9990 *bytes_read_ptr = strlen ((char *) buf) + 1;
9991 return (char *) buf;
4bdf3d34
JJ
9992}
9993
9994static char *
fe1b8b76 9995read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9996 const struct comp_unit_head *cu_header,
9997 unsigned int *bytes_read_ptr)
9998{
c764a876 9999 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 10000
be391dca 10001 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10002 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 10003 {
8a3fe4f8 10004 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 10005 bfd_get_filename (abfd));
4bdf3d34 10006 return NULL;
c906108c 10007 }
dce234bc 10008 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 10009 {
3e43a32a
MS
10010 error (_("DW_FORM_strp pointing outside of "
10011 ".debug_str section [in module %s]"),
10012 bfd_get_filename (abfd));
c906108c
SS
10013 return NULL;
10014 }
4bdf3d34 10015 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10016 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10017 return NULL;
dce234bc 10018 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10019}
10020
ce5d95e1 10021static unsigned long
fe1b8b76 10022read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10023{
ce5d95e1
JB
10024 unsigned long result;
10025 unsigned int num_read;
c906108c
SS
10026 int i, shift;
10027 unsigned char byte;
10028
10029 result = 0;
10030 shift = 0;
10031 num_read = 0;
10032 i = 0;
10033 while (1)
10034 {
fe1b8b76 10035 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10036 buf++;
10037 num_read++;
ce5d95e1 10038 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10039 if ((byte & 128) == 0)
10040 {
10041 break;
10042 }
10043 shift += 7;
10044 }
10045 *bytes_read_ptr = num_read;
10046 return result;
10047}
10048
ce5d95e1 10049static long
fe1b8b76 10050read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10051{
ce5d95e1 10052 long result;
77e0b926 10053 int i, shift, num_read;
c906108c
SS
10054 unsigned char byte;
10055
10056 result = 0;
10057 shift = 0;
c906108c
SS
10058 num_read = 0;
10059 i = 0;
10060 while (1)
10061 {
fe1b8b76 10062 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10063 buf++;
10064 num_read++;
ce5d95e1 10065 result |= ((long)(byte & 127) << shift);
c906108c
SS
10066 shift += 7;
10067 if ((byte & 128) == 0)
10068 {
10069 break;
10070 }
10071 }
77e0b926
DJ
10072 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10073 result |= -(((long)1) << shift);
c906108c
SS
10074 *bytes_read_ptr = num_read;
10075 return result;
10076}
10077
4bb7a0a7
DJ
10078/* Return a pointer to just past the end of an LEB128 number in BUF. */
10079
fe1b8b76
JB
10080static gdb_byte *
10081skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10082{
10083 int byte;
10084
10085 while (1)
10086 {
fe1b8b76 10087 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10088 buf++;
10089 if ((byte & 128) == 0)
10090 return buf;
10091 }
10092}
10093
c906108c 10094static void
e142c38c 10095set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10096{
10097 switch (lang)
10098 {
10099 case DW_LANG_C89:
76bee0cc 10100 case DW_LANG_C99:
c906108c 10101 case DW_LANG_C:
e142c38c 10102 cu->language = language_c;
c906108c
SS
10103 break;
10104 case DW_LANG_C_plus_plus:
e142c38c 10105 cu->language = language_cplus;
c906108c 10106 break;
6aecb9c2
JB
10107 case DW_LANG_D:
10108 cu->language = language_d;
10109 break;
c906108c
SS
10110 case DW_LANG_Fortran77:
10111 case DW_LANG_Fortran90:
b21b22e0 10112 case DW_LANG_Fortran95:
e142c38c 10113 cu->language = language_fortran;
c906108c
SS
10114 break;
10115 case DW_LANG_Mips_Assembler:
e142c38c 10116 cu->language = language_asm;
c906108c 10117 break;
bebd888e 10118 case DW_LANG_Java:
e142c38c 10119 cu->language = language_java;
bebd888e 10120 break;
c906108c 10121 case DW_LANG_Ada83:
8aaf0b47 10122 case DW_LANG_Ada95:
bc5f45f8
JB
10123 cu->language = language_ada;
10124 break;
72019c9c
GM
10125 case DW_LANG_Modula2:
10126 cu->language = language_m2;
10127 break;
fe8e67fd
PM
10128 case DW_LANG_Pascal83:
10129 cu->language = language_pascal;
10130 break;
22566fbd
DJ
10131 case DW_LANG_ObjC:
10132 cu->language = language_objc;
10133 break;
c906108c
SS
10134 case DW_LANG_Cobol74:
10135 case DW_LANG_Cobol85:
c906108c 10136 default:
e142c38c 10137 cu->language = language_minimal;
c906108c
SS
10138 break;
10139 }
e142c38c 10140 cu->language_defn = language_def (cu->language);
c906108c
SS
10141}
10142
10143/* Return the named attribute or NULL if not there. */
10144
10145static struct attribute *
e142c38c 10146dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10147{
10148 unsigned int i;
10149 struct attribute *spec = NULL;
10150
10151 for (i = 0; i < die->num_attrs; ++i)
10152 {
10153 if (die->attrs[i].name == name)
10b3939b 10154 return &die->attrs[i];
c906108c
SS
10155 if (die->attrs[i].name == DW_AT_specification
10156 || die->attrs[i].name == DW_AT_abstract_origin)
10157 spec = &die->attrs[i];
10158 }
c906108c 10159
10b3939b 10160 if (spec)
f2f0e013
DJ
10161 {
10162 die = follow_die_ref (die, spec, &cu);
10163 return dwarf2_attr (die, name, cu);
10164 }
c5aa993b 10165
c906108c
SS
10166 return NULL;
10167}
10168
348e048f
DE
10169/* Return the named attribute or NULL if not there,
10170 but do not follow DW_AT_specification, etc.
10171 This is for use in contexts where we're reading .debug_types dies.
10172 Following DW_AT_specification, DW_AT_abstract_origin will take us
10173 back up the chain, and we want to go down. */
10174
10175static struct attribute *
10176dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10177 struct dwarf2_cu *cu)
10178{
10179 unsigned int i;
10180
10181 for (i = 0; i < die->num_attrs; ++i)
10182 if (die->attrs[i].name == name)
10183 return &die->attrs[i];
10184
10185 return NULL;
10186}
10187
05cf31d1
JB
10188/* Return non-zero iff the attribute NAME is defined for the given DIE,
10189 and holds a non-zero value. This function should only be used for
2dc7f7b3 10190 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10191
10192static int
10193dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10194{
10195 struct attribute *attr = dwarf2_attr (die, name, cu);
10196
10197 return (attr && DW_UNSND (attr));
10198}
10199
3ca72b44 10200static int
e142c38c 10201die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10202{
05cf31d1
JB
10203 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10204 which value is non-zero. However, we have to be careful with
10205 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10206 (via dwarf2_flag_true_p) follows this attribute. So we may
10207 end up accidently finding a declaration attribute that belongs
10208 to a different DIE referenced by the specification attribute,
10209 even though the given DIE does not have a declaration attribute. */
10210 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10211 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10212}
10213
63d06c5c 10214/* Return the die giving the specification for DIE, if there is
f2f0e013 10215 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10216 containing the return value on output. If there is no
10217 specification, but there is an abstract origin, that is
10218 returned. */
63d06c5c
DC
10219
10220static struct die_info *
f2f0e013 10221die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10222{
f2f0e013
DJ
10223 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10224 *spec_cu);
63d06c5c 10225
edb3359d
DJ
10226 if (spec_attr == NULL)
10227 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10228
63d06c5c
DC
10229 if (spec_attr == NULL)
10230 return NULL;
10231 else
f2f0e013 10232 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10233}
c906108c 10234
debd256d 10235/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10236 refers to.
10237 NOTE: This is also used as a "cleanup" function. */
10238
debd256d
JB
10239static void
10240free_line_header (struct line_header *lh)
10241{
10242 if (lh->standard_opcode_lengths)
a8bc7b56 10243 xfree (lh->standard_opcode_lengths);
debd256d
JB
10244
10245 /* Remember that all the lh->file_names[i].name pointers are
10246 pointers into debug_line_buffer, and don't need to be freed. */
10247 if (lh->file_names)
a8bc7b56 10248 xfree (lh->file_names);
debd256d
JB
10249
10250 /* Similarly for the include directory names. */
10251 if (lh->include_dirs)
a8bc7b56 10252 xfree (lh->include_dirs);
debd256d 10253
a8bc7b56 10254 xfree (lh);
debd256d
JB
10255}
10256
debd256d 10257/* Add an entry to LH's include directory table. */
ae2de4f8 10258
debd256d
JB
10259static void
10260add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10261{
debd256d
JB
10262 /* Grow the array if necessary. */
10263 if (lh->include_dirs_size == 0)
c5aa993b 10264 {
debd256d
JB
10265 lh->include_dirs_size = 1; /* for testing */
10266 lh->include_dirs = xmalloc (lh->include_dirs_size
10267 * sizeof (*lh->include_dirs));
10268 }
10269 else if (lh->num_include_dirs >= lh->include_dirs_size)
10270 {
10271 lh->include_dirs_size *= 2;
10272 lh->include_dirs = xrealloc (lh->include_dirs,
10273 (lh->include_dirs_size
10274 * sizeof (*lh->include_dirs)));
c5aa993b 10275 }
c906108c 10276
debd256d
JB
10277 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10278}
6e70227d 10279
debd256d 10280/* Add an entry to LH's file name table. */
ae2de4f8 10281
debd256d
JB
10282static void
10283add_file_name (struct line_header *lh,
10284 char *name,
10285 unsigned int dir_index,
10286 unsigned int mod_time,
10287 unsigned int length)
10288{
10289 struct file_entry *fe;
10290
10291 /* Grow the array if necessary. */
10292 if (lh->file_names_size == 0)
10293 {
10294 lh->file_names_size = 1; /* for testing */
10295 lh->file_names = xmalloc (lh->file_names_size
10296 * sizeof (*lh->file_names));
10297 }
10298 else if (lh->num_file_names >= lh->file_names_size)
10299 {
10300 lh->file_names_size *= 2;
10301 lh->file_names = xrealloc (lh->file_names,
10302 (lh->file_names_size
10303 * sizeof (*lh->file_names)));
10304 }
10305
10306 fe = &lh->file_names[lh->num_file_names++];
10307 fe->name = name;
10308 fe->dir_index = dir_index;
10309 fe->mod_time = mod_time;
10310 fe->length = length;
aaa75496 10311 fe->included_p = 0;
cb1df416 10312 fe->symtab = NULL;
debd256d 10313}
6e70227d 10314
debd256d 10315/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10316 .debug_line, according to the endianness of ABFD. Return a pointer
10317 to a struct line_header, allocated using xmalloc.
debd256d
JB
10318
10319 NOTE: the strings in the include directory and file name tables of
10320 the returned object point into debug_line_buffer, and must not be
10321 freed. */
ae2de4f8 10322
debd256d
JB
10323static struct line_header *
10324dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10325 struct dwarf2_cu *cu)
debd256d
JB
10326{
10327 struct cleanup *back_to;
10328 struct line_header *lh;
fe1b8b76 10329 gdb_byte *line_ptr;
c764a876 10330 unsigned int bytes_read, offset_size;
debd256d
JB
10331 int i;
10332 char *cur_dir, *cur_file;
10333
be391dca 10334 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10335 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10336 {
e2e0b3e5 10337 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10338 return 0;
10339 }
10340
a738430d
MK
10341 /* Make sure that at least there's room for the total_length field.
10342 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10343 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10344 {
4d3c2250 10345 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10346 return 0;
10347 }
10348
10349 lh = xmalloc (sizeof (*lh));
10350 memset (lh, 0, sizeof (*lh));
10351 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10352 (void *) lh);
10353
dce234bc 10354 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10355
a738430d 10356 /* Read in the header. */
6e70227d 10357 lh->total_length =
c764a876
DE
10358 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10359 &bytes_read, &offset_size);
debd256d 10360 line_ptr += bytes_read;
dce234bc
PP
10361 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10362 + dwarf2_per_objfile->line.size))
debd256d 10363 {
4d3c2250 10364 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10365 return 0;
10366 }
10367 lh->statement_program_end = line_ptr + lh->total_length;
10368 lh->version = read_2_bytes (abfd, line_ptr);
10369 line_ptr += 2;
c764a876
DE
10370 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10371 line_ptr += offset_size;
debd256d
JB
10372 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10373 line_ptr += 1;
2dc7f7b3
TT
10374 if (lh->version >= 4)
10375 {
10376 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10377 line_ptr += 1;
10378 }
10379 else
10380 lh->maximum_ops_per_instruction = 1;
10381
10382 if (lh->maximum_ops_per_instruction == 0)
10383 {
10384 lh->maximum_ops_per_instruction = 1;
10385 complaint (&symfile_complaints,
3e43a32a
MS
10386 _("invalid maximum_ops_per_instruction "
10387 "in `.debug_line' section"));
2dc7f7b3
TT
10388 }
10389
debd256d
JB
10390 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10391 line_ptr += 1;
10392 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10393 line_ptr += 1;
10394 lh->line_range = read_1_byte (abfd, line_ptr);
10395 line_ptr += 1;
10396 lh->opcode_base = read_1_byte (abfd, line_ptr);
10397 line_ptr += 1;
10398 lh->standard_opcode_lengths
fe1b8b76 10399 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10400
10401 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10402 for (i = 1; i < lh->opcode_base; ++i)
10403 {
10404 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10405 line_ptr += 1;
10406 }
10407
a738430d 10408 /* Read directory table. */
9b1c24c8 10409 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10410 {
10411 line_ptr += bytes_read;
10412 add_include_dir (lh, cur_dir);
10413 }
10414 line_ptr += bytes_read;
10415
a738430d 10416 /* Read file name table. */
9b1c24c8 10417 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10418 {
10419 unsigned int dir_index, mod_time, length;
10420
10421 line_ptr += bytes_read;
10422 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10423 line_ptr += bytes_read;
10424 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10425 line_ptr += bytes_read;
10426 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10427 line_ptr += bytes_read;
10428
10429 add_file_name (lh, cur_file, dir_index, mod_time, length);
10430 }
10431 line_ptr += bytes_read;
6e70227d 10432 lh->statement_program_start = line_ptr;
debd256d 10433
dce234bc
PP
10434 if (line_ptr > (dwarf2_per_objfile->line.buffer
10435 + dwarf2_per_objfile->line.size))
4d3c2250 10436 complaint (&symfile_complaints,
3e43a32a
MS
10437 _("line number info header doesn't "
10438 "fit in `.debug_line' section"));
debd256d
JB
10439
10440 discard_cleanups (back_to);
10441 return lh;
10442}
c906108c 10443
5fb290d7
DJ
10444/* This function exists to work around a bug in certain compilers
10445 (particularly GCC 2.95), in which the first line number marker of a
10446 function does not show up until after the prologue, right before
10447 the second line number marker. This function shifts ADDRESS down
10448 to the beginning of the function if necessary, and is called on
10449 addresses passed to record_line. */
10450
10451static CORE_ADDR
e142c38c 10452check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10453{
10454 struct function_range *fn;
10455
10456 /* Find the function_range containing address. */
e142c38c 10457 if (!cu->first_fn)
5fb290d7
DJ
10458 return address;
10459
e142c38c
DJ
10460 if (!cu->cached_fn)
10461 cu->cached_fn = cu->first_fn;
5fb290d7 10462
e142c38c 10463 fn = cu->cached_fn;
5fb290d7
DJ
10464 while (fn)
10465 if (fn->lowpc <= address && fn->highpc > address)
10466 goto found;
10467 else
10468 fn = fn->next;
10469
e142c38c
DJ
10470 fn = cu->first_fn;
10471 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10472 if (fn->lowpc <= address && fn->highpc > address)
10473 goto found;
10474 else
10475 fn = fn->next;
10476
10477 return address;
10478
10479 found:
10480 if (fn->seen_line)
10481 return address;
10482 if (address != fn->lowpc)
4d3c2250 10483 complaint (&symfile_complaints,
e2e0b3e5 10484 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10485 (unsigned long) address, fn->name);
5fb290d7
DJ
10486 fn->seen_line = 1;
10487 return fn->lowpc;
10488}
10489
c6da4cef
DE
10490/* Subroutine of dwarf_decode_lines to simplify it.
10491 Return the file name of the psymtab for included file FILE_INDEX
10492 in line header LH of PST.
10493 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10494 If space for the result is malloc'd, it will be freed by a cleanup.
10495 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10496
10497static char *
10498psymtab_include_file_name (const struct line_header *lh, int file_index,
10499 const struct partial_symtab *pst,
10500 const char *comp_dir)
10501{
10502 const struct file_entry fe = lh->file_names [file_index];
10503 char *include_name = fe.name;
10504 char *include_name_to_compare = include_name;
10505 char *dir_name = NULL;
72b9f47f
TT
10506 const char *pst_filename;
10507 char *copied_name = NULL;
c6da4cef
DE
10508 int file_is_pst;
10509
10510 if (fe.dir_index)
10511 dir_name = lh->include_dirs[fe.dir_index - 1];
10512
10513 if (!IS_ABSOLUTE_PATH (include_name)
10514 && (dir_name != NULL || comp_dir != NULL))
10515 {
10516 /* Avoid creating a duplicate psymtab for PST.
10517 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10518 Before we do the comparison, however, we need to account
10519 for DIR_NAME and COMP_DIR.
10520 First prepend dir_name (if non-NULL). If we still don't
10521 have an absolute path prepend comp_dir (if non-NULL).
10522 However, the directory we record in the include-file's
10523 psymtab does not contain COMP_DIR (to match the
10524 corresponding symtab(s)).
10525
10526 Example:
10527
10528 bash$ cd /tmp
10529 bash$ gcc -g ./hello.c
10530 include_name = "hello.c"
10531 dir_name = "."
10532 DW_AT_comp_dir = comp_dir = "/tmp"
10533 DW_AT_name = "./hello.c" */
10534
10535 if (dir_name != NULL)
10536 {
10537 include_name = concat (dir_name, SLASH_STRING,
10538 include_name, (char *)NULL);
10539 include_name_to_compare = include_name;
10540 make_cleanup (xfree, include_name);
10541 }
10542 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10543 {
10544 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10545 include_name, (char *)NULL);
10546 }
10547 }
10548
10549 pst_filename = pst->filename;
10550 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10551 {
72b9f47f
TT
10552 copied_name = concat (pst->dirname, SLASH_STRING,
10553 pst_filename, (char *)NULL);
10554 pst_filename = copied_name;
c6da4cef
DE
10555 }
10556
1e3fad37 10557 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10558
10559 if (include_name_to_compare != include_name)
10560 xfree (include_name_to_compare);
72b9f47f
TT
10561 if (copied_name != NULL)
10562 xfree (copied_name);
c6da4cef
DE
10563
10564 if (file_is_pst)
10565 return NULL;
10566 return include_name;
10567}
10568
c91513d8
PP
10569/* Ignore this record_line request. */
10570
10571static void
10572noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10573{
10574 return;
10575}
10576
aaa75496
JB
10577/* Decode the Line Number Program (LNP) for the given line_header
10578 structure and CU. The actual information extracted and the type
10579 of structures created from the LNP depends on the value of PST.
10580
10581 1. If PST is NULL, then this procedure uses the data from the program
10582 to create all necessary symbol tables, and their linetables.
6e70227d 10583
aaa75496
JB
10584 2. If PST is not NULL, this procedure reads the program to determine
10585 the list of files included by the unit represented by PST, and
c6da4cef
DE
10586 builds all the associated partial symbol tables.
10587
10588 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10589 It is used for relative paths in the line table.
10590 NOTE: When processing partial symtabs (pst != NULL),
10591 comp_dir == pst->dirname.
10592
10593 NOTE: It is important that psymtabs have the same file name (via strcmp)
10594 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10595 symtab we don't use it in the name of the psymtabs we create.
10596 E.g. expand_line_sal requires this when finding psymtabs to expand.
10597 A good testcase for this is mb-inline.exp. */
debd256d 10598
c906108c 10599static void
72b9f47f 10600dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10601 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10602{
a8c50c1f 10603 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10604 gdb_byte *line_end;
a8c50c1f 10605 unsigned int bytes_read, extended_len;
c906108c 10606 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10607 CORE_ADDR baseaddr;
10608 struct objfile *objfile = cu->objfile;
fbf65064 10609 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10610 const int decode_for_pst_p = (pst != NULL);
cb1df416 10611 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
10612 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10613 = record_line;
e142c38c
DJ
10614
10615 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10616
debd256d
JB
10617 line_ptr = lh->statement_program_start;
10618 line_end = lh->statement_program_end;
c906108c
SS
10619
10620 /* Read the statement sequences until there's nothing left. */
10621 while (line_ptr < line_end)
10622 {
10623 /* state machine registers */
10624 CORE_ADDR address = 0;
10625 unsigned int file = 1;
10626 unsigned int line = 1;
10627 unsigned int column = 0;
debd256d 10628 int is_stmt = lh->default_is_stmt;
c906108c
SS
10629 int basic_block = 0;
10630 int end_sequence = 0;
fbf65064 10631 CORE_ADDR addr;
2dc7f7b3 10632 unsigned char op_index = 0;
c906108c 10633
aaa75496 10634 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10635 {
aaa75496 10636 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10637 /* lh->include_dirs and lh->file_names are 0-based, but the
10638 directory and file name numbers in the statement program
10639 are 1-based. */
10640 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10641 char *dir = NULL;
a738430d 10642
debd256d
JB
10643 if (fe->dir_index)
10644 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10645
10646 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10647 }
10648
a738430d 10649 /* Decode the table. */
c5aa993b 10650 while (!end_sequence)
c906108c
SS
10651 {
10652 op_code = read_1_byte (abfd, line_ptr);
10653 line_ptr += 1;
59205f5a
JB
10654 if (line_ptr > line_end)
10655 {
10656 dwarf2_debug_line_missing_end_sequence_complaint ();
10657 break;
10658 }
9aa1fe7e 10659
debd256d 10660 if (op_code >= lh->opcode_base)
6e70227d 10661 {
a738430d 10662 /* Special operand. */
debd256d 10663 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10664 address += (((op_index + (adj_opcode / lh->line_range))
10665 / lh->maximum_ops_per_instruction)
10666 * lh->minimum_instruction_length);
10667 op_index = ((op_index + (adj_opcode / lh->line_range))
10668 % lh->maximum_ops_per_instruction);
debd256d 10669 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10670 if (lh->num_file_names < file || file == 0)
25e43795 10671 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10672 /* For now we ignore lines not starting on an
10673 instruction boundary. */
10674 else if (op_index == 0)
25e43795
DJ
10675 {
10676 lh->file_names[file - 1].included_p = 1;
ca5f395d 10677 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10678 {
10679 if (last_subfile != current_subfile)
10680 {
10681 addr = gdbarch_addr_bits_remove (gdbarch, address);
10682 if (last_subfile)
c91513d8 10683 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10684 last_subfile = current_subfile;
10685 }
25e43795 10686 /* Append row to matrix using current values. */
fbf65064
UW
10687 addr = check_cu_functions (address, cu);
10688 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10689 (*p_record_line) (current_subfile, line, addr);
366da635 10690 }
25e43795 10691 }
ca5f395d 10692 basic_block = 0;
9aa1fe7e
GK
10693 }
10694 else switch (op_code)
c906108c
SS
10695 {
10696 case DW_LNS_extended_op:
3e43a32a
MS
10697 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10698 &bytes_read);
473b7be6 10699 line_ptr += bytes_read;
a8c50c1f 10700 extended_end = line_ptr + extended_len;
c906108c
SS
10701 extended_op = read_1_byte (abfd, line_ptr);
10702 line_ptr += 1;
10703 switch (extended_op)
10704 {
10705 case DW_LNE_end_sequence:
c91513d8 10706 p_record_line = record_line;
c906108c 10707 end_sequence = 1;
c906108c
SS
10708 break;
10709 case DW_LNE_set_address:
e7c27a73 10710 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
10711
10712 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10713 {
10714 /* This line table is for a function which has been
10715 GCd by the linker. Ignore it. PR gdb/12528 */
10716
10717 long line_offset
10718 = line_ptr - dwarf2_per_objfile->line.buffer;
10719
10720 complaint (&symfile_complaints,
10721 _(".debug_line address at offset 0x%lx is 0 "
10722 "[in module %s]"),
10723 line_offset, cu->objfile->name);
10724 p_record_line = noop_record_line;
10725 }
10726
2dc7f7b3 10727 op_index = 0;
107d2387
AC
10728 line_ptr += bytes_read;
10729 address += baseaddr;
c906108c
SS
10730 break;
10731 case DW_LNE_define_file:
debd256d
JB
10732 {
10733 char *cur_file;
10734 unsigned int dir_index, mod_time, length;
6e70227d 10735
3e43a32a
MS
10736 cur_file = read_direct_string (abfd, line_ptr,
10737 &bytes_read);
debd256d
JB
10738 line_ptr += bytes_read;
10739 dir_index =
10740 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10741 line_ptr += bytes_read;
10742 mod_time =
10743 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10744 line_ptr += bytes_read;
10745 length =
10746 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10747 line_ptr += bytes_read;
10748 add_file_name (lh, cur_file, dir_index, mod_time, length);
10749 }
c906108c 10750 break;
d0c6ba3d
CC
10751 case DW_LNE_set_discriminator:
10752 /* The discriminator is not interesting to the debugger;
10753 just ignore it. */
10754 line_ptr = extended_end;
10755 break;
c906108c 10756 default:
4d3c2250 10757 complaint (&symfile_complaints,
e2e0b3e5 10758 _("mangled .debug_line section"));
debd256d 10759 return;
c906108c 10760 }
a8c50c1f
DJ
10761 /* Make sure that we parsed the extended op correctly. If e.g.
10762 we expected a different address size than the producer used,
10763 we may have read the wrong number of bytes. */
10764 if (line_ptr != extended_end)
10765 {
10766 complaint (&symfile_complaints,
10767 _("mangled .debug_line section"));
10768 return;
10769 }
c906108c
SS
10770 break;
10771 case DW_LNS_copy:
59205f5a 10772 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10773 dwarf2_debug_line_missing_file_complaint ();
10774 else
366da635 10775 {
25e43795 10776 lh->file_names[file - 1].included_p = 1;
ca5f395d 10777 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10778 {
10779 if (last_subfile != current_subfile)
10780 {
10781 addr = gdbarch_addr_bits_remove (gdbarch, address);
10782 if (last_subfile)
c91513d8 10783 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10784 last_subfile = current_subfile;
10785 }
10786 addr = check_cu_functions (address, cu);
10787 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10788 (*p_record_line) (current_subfile, line, addr);
fbf65064 10789 }
366da635 10790 }
c906108c
SS
10791 basic_block = 0;
10792 break;
10793 case DW_LNS_advance_pc:
2dc7f7b3
TT
10794 {
10795 CORE_ADDR adjust
10796 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10797
10798 address += (((op_index + adjust)
10799 / lh->maximum_ops_per_instruction)
10800 * lh->minimum_instruction_length);
10801 op_index = ((op_index + adjust)
10802 % lh->maximum_ops_per_instruction);
10803 line_ptr += bytes_read;
10804 }
c906108c
SS
10805 break;
10806 case DW_LNS_advance_line:
10807 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10808 line_ptr += bytes_read;
10809 break;
10810 case DW_LNS_set_file:
debd256d 10811 {
a738430d
MK
10812 /* The arrays lh->include_dirs and lh->file_names are
10813 0-based, but the directory and file name numbers in
10814 the statement program are 1-based. */
debd256d 10815 struct file_entry *fe;
4f1520fb 10816 char *dir = NULL;
a738430d 10817
debd256d
JB
10818 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10819 line_ptr += bytes_read;
59205f5a 10820 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10821 dwarf2_debug_line_missing_file_complaint ();
10822 else
10823 {
10824 fe = &lh->file_names[file - 1];
10825 if (fe->dir_index)
10826 dir = lh->include_dirs[fe->dir_index - 1];
10827 if (!decode_for_pst_p)
10828 {
10829 last_subfile = current_subfile;
10830 dwarf2_start_subfile (fe->name, dir, comp_dir);
10831 }
10832 }
debd256d 10833 }
c906108c
SS
10834 break;
10835 case DW_LNS_set_column:
10836 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10837 line_ptr += bytes_read;
10838 break;
10839 case DW_LNS_negate_stmt:
10840 is_stmt = (!is_stmt);
10841 break;
10842 case DW_LNS_set_basic_block:
10843 basic_block = 1;
10844 break;
c2c6d25f
JM
10845 /* Add to the address register of the state machine the
10846 address increment value corresponding to special opcode
a738430d
MK
10847 255. I.e., this value is scaled by the minimum
10848 instruction length since special opcode 255 would have
b021a221 10849 scaled the increment. */
c906108c 10850 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10851 {
10852 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10853
10854 address += (((op_index + adjust)
10855 / lh->maximum_ops_per_instruction)
10856 * lh->minimum_instruction_length);
10857 op_index = ((op_index + adjust)
10858 % lh->maximum_ops_per_instruction);
10859 }
c906108c
SS
10860 break;
10861 case DW_LNS_fixed_advance_pc:
10862 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10863 op_index = 0;
c906108c
SS
10864 line_ptr += 2;
10865 break;
9aa1fe7e 10866 default:
a738430d
MK
10867 {
10868 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10869 int i;
a738430d 10870
debd256d 10871 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10872 {
10873 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10874 line_ptr += bytes_read;
10875 }
10876 }
c906108c
SS
10877 }
10878 }
59205f5a
JB
10879 if (lh->num_file_names < file || file == 0)
10880 dwarf2_debug_line_missing_file_complaint ();
10881 else
10882 {
10883 lh->file_names[file - 1].included_p = 1;
10884 if (!decode_for_pst_p)
fbf65064
UW
10885 {
10886 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 10887 (*p_record_line) (current_subfile, 0, addr);
fbf65064 10888 }
59205f5a 10889 }
c906108c 10890 }
aaa75496
JB
10891
10892 if (decode_for_pst_p)
10893 {
10894 int file_index;
10895
10896 /* Now that we're done scanning the Line Header Program, we can
10897 create the psymtab of each included file. */
10898 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10899 if (lh->file_names[file_index].included_p == 1)
10900 {
c6da4cef
DE
10901 char *include_name =
10902 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10903 if (include_name != NULL)
aaa75496
JB
10904 dwarf2_create_include_psymtab (include_name, pst, objfile);
10905 }
10906 }
cb1df416
DJ
10907 else
10908 {
10909 /* Make sure a symtab is created for every file, even files
10910 which contain only variables (i.e. no code with associated
10911 line numbers). */
10912
10913 int i;
10914 struct file_entry *fe;
10915
10916 for (i = 0; i < lh->num_file_names; i++)
10917 {
10918 char *dir = NULL;
9a619af0 10919
cb1df416
DJ
10920 fe = &lh->file_names[i];
10921 if (fe->dir_index)
10922 dir = lh->include_dirs[fe->dir_index - 1];
10923 dwarf2_start_subfile (fe->name, dir, comp_dir);
10924
10925 /* Skip the main file; we don't need it, and it must be
10926 allocated last, so that it will show up before the
10927 non-primary symtabs in the objfile's symtab list. */
10928 if (current_subfile == first_subfile)
10929 continue;
10930
10931 if (current_subfile->symtab == NULL)
10932 current_subfile->symtab = allocate_symtab (current_subfile->name,
10933 cu->objfile);
10934 fe->symtab = current_subfile->symtab;
10935 }
10936 }
c906108c
SS
10937}
10938
10939/* Start a subfile for DWARF. FILENAME is the name of the file and
10940 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10941 or NULL if not known. COMP_DIR is the compilation directory for the
10942 linetable's compilation unit or NULL if not known.
c906108c
SS
10943 This routine tries to keep line numbers from identical absolute and
10944 relative file names in a common subfile.
10945
10946 Using the `list' example from the GDB testsuite, which resides in
10947 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10948 of /srcdir/list0.c yields the following debugging information for list0.c:
10949
c5aa993b
JM
10950 DW_AT_name: /srcdir/list0.c
10951 DW_AT_comp_dir: /compdir
357e46e7 10952 files.files[0].name: list0.h
c5aa993b 10953 files.files[0].dir: /srcdir
357e46e7 10954 files.files[1].name: list0.c
c5aa993b 10955 files.files[1].dir: /srcdir
c906108c
SS
10956
10957 The line number information for list0.c has to end up in a single
4f1520fb
FR
10958 subfile, so that `break /srcdir/list0.c:1' works as expected.
10959 start_subfile will ensure that this happens provided that we pass the
10960 concatenation of files.files[1].dir and files.files[1].name as the
10961 subfile's name. */
c906108c
SS
10962
10963static void
3e43a32a
MS
10964dwarf2_start_subfile (char *filename, const char *dirname,
10965 const char *comp_dir)
c906108c 10966{
4f1520fb
FR
10967 char *fullname;
10968
10969 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10970 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10971 second argument to start_subfile. To be consistent, we do the
10972 same here. In order not to lose the line information directory,
10973 we concatenate it to the filename when it makes sense.
10974 Note that the Dwarf3 standard says (speaking of filenames in line
10975 information): ``The directory index is ignored for file names
10976 that represent full path names''. Thus ignoring dirname in the
10977 `else' branch below isn't an issue. */
c906108c 10978
d5166ae1 10979 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10980 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10981 else
10982 fullname = filename;
c906108c 10983
4f1520fb
FR
10984 start_subfile (fullname, comp_dir);
10985
10986 if (fullname != filename)
10987 xfree (fullname);
c906108c
SS
10988}
10989
4c2df51b
DJ
10990static void
10991var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10992 struct dwarf2_cu *cu)
4c2df51b 10993{
e7c27a73
DJ
10994 struct objfile *objfile = cu->objfile;
10995 struct comp_unit_head *cu_header = &cu->header;
10996
4c2df51b
DJ
10997 /* NOTE drow/2003-01-30: There used to be a comment and some special
10998 code here to turn a symbol with DW_AT_external and a
10999 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11000 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11001 with some versions of binutils) where shared libraries could have
11002 relocations against symbols in their debug information - the
11003 minimal symbol would have the right address, but the debug info
11004 would not. It's no longer necessary, because we will explicitly
11005 apply relocations when we read in the debug information now. */
11006
11007 /* A DW_AT_location attribute with no contents indicates that a
11008 variable has been optimized away. */
11009 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11010 {
11011 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11012 return;
11013 }
11014
11015 /* Handle one degenerate form of location expression specially, to
11016 preserve GDB's previous behavior when section offsets are
11017 specified. If this is just a DW_OP_addr then mark this symbol
11018 as LOC_STATIC. */
11019
11020 if (attr_form_is_block (attr)
11021 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11022 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11023 {
891d2f0b 11024 unsigned int dummy;
4c2df51b
DJ
11025
11026 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11027 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11028 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11029 fixup_symbol_section (sym, objfile);
11030 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11031 SYMBOL_SECTION (sym));
4c2df51b
DJ
11032 return;
11033 }
11034
11035 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11036 expression evaluator, and use LOC_COMPUTED only when necessary
11037 (i.e. when the value of a register or memory location is
11038 referenced, or a thread-local block, etc.). Then again, it might
11039 not be worthwhile. I'm assuming that it isn't unless performance
11040 or memory numbers show me otherwise. */
11041
e7c27a73 11042 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11043 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11044
11045 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11046 cu->has_loclist = 1;
4c2df51b
DJ
11047}
11048
c906108c
SS
11049/* Given a pointer to a DWARF information entry, figure out if we need
11050 to make a symbol table entry for it, and if so, create a new entry
11051 and return a pointer to it.
11052 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11053 used the passed type.
11054 If SPACE is not NULL, use it to hold the new symbol. If it is
11055 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11056
11057static struct symbol *
34eaf542
TT
11058new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11059 struct symbol *space)
c906108c 11060{
e7c27a73 11061 struct objfile *objfile = cu->objfile;
c906108c
SS
11062 struct symbol *sym = NULL;
11063 char *name;
11064 struct attribute *attr = NULL;
11065 struct attribute *attr2 = NULL;
e142c38c 11066 CORE_ADDR baseaddr;
e37fd15a
SW
11067 struct pending **list_to_add = NULL;
11068
edb3359d 11069 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11070
11071 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11072
94af9270 11073 name = dwarf2_name (die, cu);
c906108c
SS
11074 if (name)
11075 {
94af9270 11076 const char *linkagename;
34eaf542 11077 int suppress_add = 0;
94af9270 11078
34eaf542
TT
11079 if (space)
11080 sym = space;
11081 else
11082 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11083 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11084
11085 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11086 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11087 linkagename = dwarf2_physname (name, die, cu);
11088 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11089
f55ee35c
JK
11090 /* Fortran does not have mangling standard and the mangling does differ
11091 between gfortran, iFort etc. */
11092 if (cu->language == language_fortran
b250c185 11093 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11094 symbol_set_demangled_name (&(sym->ginfo),
11095 (char *) dwarf2_full_name (name, die, cu),
11096 NULL);
f55ee35c 11097
c906108c 11098 /* Default assumptions.
c5aa993b 11099 Use the passed type or decode it from the die. */
176620f1 11100 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11101 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11102 if (type != NULL)
11103 SYMBOL_TYPE (sym) = type;
11104 else
e7c27a73 11105 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11106 attr = dwarf2_attr (die,
11107 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11108 cu);
c906108c
SS
11109 if (attr)
11110 {
11111 SYMBOL_LINE (sym) = DW_UNSND (attr);
11112 }
cb1df416 11113
edb3359d
DJ
11114 attr = dwarf2_attr (die,
11115 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11116 cu);
cb1df416
DJ
11117 if (attr)
11118 {
11119 int file_index = DW_UNSND (attr);
9a619af0 11120
cb1df416
DJ
11121 if (cu->line_header == NULL
11122 || file_index > cu->line_header->num_file_names)
11123 complaint (&symfile_complaints,
11124 _("file index out of range"));
1c3d648d 11125 else if (file_index > 0)
cb1df416
DJ
11126 {
11127 struct file_entry *fe;
9a619af0 11128
cb1df416
DJ
11129 fe = &cu->line_header->file_names[file_index - 1];
11130 SYMBOL_SYMTAB (sym) = fe->symtab;
11131 }
11132 }
11133
c906108c
SS
11134 switch (die->tag)
11135 {
11136 case DW_TAG_label:
e142c38c 11137 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11138 if (attr)
11139 {
11140 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11141 }
0f5238ed
TT
11142 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11143 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11144 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11145 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11146 break;
11147 case DW_TAG_subprogram:
11148 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11149 finish_block. */
11150 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11151 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11152 if ((attr2 && (DW_UNSND (attr2) != 0))
11153 || cu->language == language_ada)
c906108c 11154 {
2cfa0c8d
JB
11155 /* Subprograms marked external are stored as a global symbol.
11156 Ada subprograms, whether marked external or not, are always
11157 stored as a global symbol, because we want to be able to
11158 access them globally. For instance, we want to be able
11159 to break on a nested subprogram without having to
11160 specify the context. */
e37fd15a 11161 list_to_add = &global_symbols;
c906108c
SS
11162 }
11163 else
11164 {
e37fd15a 11165 list_to_add = cu->list_in_scope;
c906108c
SS
11166 }
11167 break;
edb3359d
DJ
11168 case DW_TAG_inlined_subroutine:
11169 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11170 finish_block. */
11171 SYMBOL_CLASS (sym) = LOC_BLOCK;
11172 SYMBOL_INLINED (sym) = 1;
11173 /* Do not add the symbol to any lists. It will be found via
11174 BLOCK_FUNCTION from the blockvector. */
11175 break;
34eaf542
TT
11176 case DW_TAG_template_value_param:
11177 suppress_add = 1;
11178 /* Fall through. */
72929c62 11179 case DW_TAG_constant:
c906108c 11180 case DW_TAG_variable:
254e6b9e 11181 case DW_TAG_member:
0963b4bd
MS
11182 /* Compilation with minimal debug info may result in
11183 variables with missing type entries. Change the
11184 misleading `void' type to something sensible. */
c906108c 11185 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11186 SYMBOL_TYPE (sym)
46bf5051 11187 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11188
e142c38c 11189 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11190 /* In the case of DW_TAG_member, we should only be called for
11191 static const members. */
11192 if (die->tag == DW_TAG_member)
11193 {
3863f96c
DE
11194 /* dwarf2_add_field uses die_is_declaration,
11195 so we do the same. */
254e6b9e
DE
11196 gdb_assert (die_is_declaration (die, cu));
11197 gdb_assert (attr);
11198 }
c906108c
SS
11199 if (attr)
11200 {
e7c27a73 11201 dwarf2_const_value (attr, sym, cu);
e142c38c 11202 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11203 if (!suppress_add)
34eaf542
TT
11204 {
11205 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11206 list_to_add = &global_symbols;
34eaf542 11207 else
e37fd15a 11208 list_to_add = cu->list_in_scope;
34eaf542 11209 }
c906108c
SS
11210 break;
11211 }
e142c38c 11212 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11213 if (attr)
11214 {
e7c27a73 11215 var_decode_location (attr, sym, cu);
e142c38c 11216 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11217 if (SYMBOL_CLASS (sym) == LOC_STATIC
11218 && SYMBOL_VALUE_ADDRESS (sym) == 0
11219 && !dwarf2_per_objfile->has_section_at_zero)
11220 {
11221 /* When a static variable is eliminated by the linker,
11222 the corresponding debug information is not stripped
11223 out, but the variable address is set to null;
11224 do not add such variables into symbol table. */
11225 }
11226 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11227 {
f55ee35c
JK
11228 /* Workaround gfortran PR debug/40040 - it uses
11229 DW_AT_location for variables in -fPIC libraries which may
11230 get overriden by other libraries/executable and get
11231 a different address. Resolve it by the minimal symbol
11232 which may come from inferior's executable using copy
11233 relocation. Make this workaround only for gfortran as for
11234 other compilers GDB cannot guess the minimal symbol
11235 Fortran mangling kind. */
11236 if (cu->language == language_fortran && die->parent
11237 && die->parent->tag == DW_TAG_module
11238 && cu->producer
11239 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11240 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11241
1c809c68
TT
11242 /* A variable with DW_AT_external is never static,
11243 but it may be block-scoped. */
11244 list_to_add = (cu->list_in_scope == &file_symbols
11245 ? &global_symbols : cu->list_in_scope);
1c809c68 11246 }
c906108c 11247 else
e37fd15a 11248 list_to_add = cu->list_in_scope;
c906108c
SS
11249 }
11250 else
11251 {
11252 /* We do not know the address of this symbol.
c5aa993b
JM
11253 If it is an external symbol and we have type information
11254 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11255 The address of the variable will then be determined from
11256 the minimal symbol table whenever the variable is
11257 referenced. */
e142c38c 11258 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11259 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11260 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11261 {
0fe7935b
DJ
11262 /* A variable with DW_AT_external is never static, but it
11263 may be block-scoped. */
11264 list_to_add = (cu->list_in_scope == &file_symbols
11265 ? &global_symbols : cu->list_in_scope);
11266
c906108c 11267 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11268 }
442ddf59
JK
11269 else if (!die_is_declaration (die, cu))
11270 {
11271 /* Use the default LOC_OPTIMIZED_OUT class. */
11272 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11273 if (!suppress_add)
11274 list_to_add = cu->list_in_scope;
442ddf59 11275 }
c906108c
SS
11276 }
11277 break;
11278 case DW_TAG_formal_parameter:
edb3359d
DJ
11279 /* If we are inside a function, mark this as an argument. If
11280 not, we might be looking at an argument to an inlined function
11281 when we do not have enough information to show inlined frames;
11282 pretend it's a local variable in that case so that the user can
11283 still see it. */
11284 if (context_stack_depth > 0
11285 && context_stack[context_stack_depth - 1].name != NULL)
11286 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11287 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11288 if (attr)
11289 {
e7c27a73 11290 var_decode_location (attr, sym, cu);
c906108c 11291 }
e142c38c 11292 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11293 if (attr)
11294 {
e7c27a73 11295 dwarf2_const_value (attr, sym, cu);
c906108c 11296 }
f346a30d
PM
11297 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11298 if (attr && DW_UNSND (attr))
11299 {
11300 struct type *ref_type;
11301
11302 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11303 SYMBOL_TYPE (sym) = ref_type;
11304 }
11305
e37fd15a 11306 list_to_add = cu->list_in_scope;
c906108c
SS
11307 break;
11308 case DW_TAG_unspecified_parameters:
11309 /* From varargs functions; gdb doesn't seem to have any
11310 interest in this information, so just ignore it for now.
11311 (FIXME?) */
11312 break;
34eaf542
TT
11313 case DW_TAG_template_type_param:
11314 suppress_add = 1;
11315 /* Fall through. */
c906108c 11316 case DW_TAG_class_type:
680b30c7 11317 case DW_TAG_interface_type:
c906108c
SS
11318 case DW_TAG_structure_type:
11319 case DW_TAG_union_type:
72019c9c 11320 case DW_TAG_set_type:
c906108c
SS
11321 case DW_TAG_enumeration_type:
11322 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11323 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11324
63d06c5c 11325 {
987504bb 11326 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11327 really ever be static objects: otherwise, if you try
11328 to, say, break of a class's method and you're in a file
11329 which doesn't mention that class, it won't work unless
11330 the check for all static symbols in lookup_symbol_aux
11331 saves you. See the OtherFileClass tests in
11332 gdb.c++/namespace.exp. */
11333
e37fd15a 11334 if (!suppress_add)
34eaf542 11335 {
34eaf542
TT
11336 list_to_add = (cu->list_in_scope == &file_symbols
11337 && (cu->language == language_cplus
11338 || cu->language == language_java)
11339 ? &global_symbols : cu->list_in_scope);
63d06c5c 11340
64382290
TT
11341 /* The semantics of C++ state that "struct foo {
11342 ... }" also defines a typedef for "foo". A Java
11343 class declaration also defines a typedef for the
11344 class. */
11345 if (cu->language == language_cplus
11346 || cu->language == language_java
11347 || cu->language == language_ada)
11348 {
11349 /* The symbol's name is already allocated along
11350 with this objfile, so we don't need to
11351 duplicate it for the type. */
11352 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11353 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11354 }
63d06c5c
DC
11355 }
11356 }
c906108c
SS
11357 break;
11358 case DW_TAG_typedef:
63d06c5c
DC
11359 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11360 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11361 list_to_add = cu->list_in_scope;
63d06c5c 11362 break;
c906108c 11363 case DW_TAG_base_type:
a02abb62 11364 case DW_TAG_subrange_type:
c906108c 11365 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11366 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11367 list_to_add = cu->list_in_scope;
c906108c
SS
11368 break;
11369 case DW_TAG_enumerator:
e142c38c 11370 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11371 if (attr)
11372 {
e7c27a73 11373 dwarf2_const_value (attr, sym, cu);
c906108c 11374 }
63d06c5c
DC
11375 {
11376 /* NOTE: carlton/2003-11-10: See comment above in the
11377 DW_TAG_class_type, etc. block. */
11378
e142c38c 11379 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11380 && (cu->language == language_cplus
11381 || cu->language == language_java)
e142c38c 11382 ? &global_symbols : cu->list_in_scope);
63d06c5c 11383 }
c906108c 11384 break;
5c4e30ca
DC
11385 case DW_TAG_namespace:
11386 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11387 list_to_add = &global_symbols;
5c4e30ca 11388 break;
c906108c
SS
11389 default:
11390 /* Not a tag we recognize. Hopefully we aren't processing
11391 trash data, but since we must specifically ignore things
11392 we don't recognize, there is nothing else we should do at
0963b4bd 11393 this point. */
e2e0b3e5 11394 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11395 dwarf_tag_name (die->tag));
c906108c
SS
11396 break;
11397 }
df8a16a1 11398
e37fd15a
SW
11399 if (suppress_add)
11400 {
11401 sym->hash_next = objfile->template_symbols;
11402 objfile->template_symbols = sym;
11403 list_to_add = NULL;
11404 }
11405
11406 if (list_to_add != NULL)
11407 add_symbol_to_list (sym, list_to_add);
11408
df8a16a1
DJ
11409 /* For the benefit of old versions of GCC, check for anonymous
11410 namespaces based on the demangled name. */
11411 if (!processing_has_namespace_info
94af9270 11412 && cu->language == language_cplus)
df8a16a1 11413 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
11414 }
11415 return (sym);
11416}
11417
34eaf542
TT
11418/* A wrapper for new_symbol_full that always allocates a new symbol. */
11419
11420static struct symbol *
11421new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11422{
11423 return new_symbol_full (die, type, cu, NULL);
11424}
11425
98bfdba5
PA
11426/* Given an attr with a DW_FORM_dataN value in host byte order,
11427 zero-extend it as appropriate for the symbol's type. The DWARF
11428 standard (v4) is not entirely clear about the meaning of using
11429 DW_FORM_dataN for a constant with a signed type, where the type is
11430 wider than the data. The conclusion of a discussion on the DWARF
11431 list was that this is unspecified. We choose to always zero-extend
11432 because that is the interpretation long in use by GCC. */
c906108c 11433
98bfdba5
PA
11434static gdb_byte *
11435dwarf2_const_value_data (struct attribute *attr, struct type *type,
11436 const char *name, struct obstack *obstack,
11437 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11438{
e7c27a73 11439 struct objfile *objfile = cu->objfile;
e17a4113
UW
11440 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11441 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11442 LONGEST l = DW_UNSND (attr);
11443
11444 if (bits < sizeof (*value) * 8)
11445 {
11446 l &= ((LONGEST) 1 << bits) - 1;
11447 *value = l;
11448 }
11449 else if (bits == sizeof (*value) * 8)
11450 *value = l;
11451 else
11452 {
11453 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11454 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11455 return bytes;
11456 }
11457
11458 return NULL;
11459}
11460
11461/* Read a constant value from an attribute. Either set *VALUE, or if
11462 the value does not fit in *VALUE, set *BYTES - either already
11463 allocated on the objfile obstack, or newly allocated on OBSTACK,
11464 or, set *BATON, if we translated the constant to a location
11465 expression. */
11466
11467static void
11468dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11469 const char *name, struct obstack *obstack,
11470 struct dwarf2_cu *cu,
11471 long *value, gdb_byte **bytes,
11472 struct dwarf2_locexpr_baton **baton)
11473{
11474 struct objfile *objfile = cu->objfile;
11475 struct comp_unit_head *cu_header = &cu->header;
c906108c 11476 struct dwarf_block *blk;
98bfdba5
PA
11477 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11478 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11479
11480 *value = 0;
11481 *bytes = NULL;
11482 *baton = NULL;
c906108c
SS
11483
11484 switch (attr->form)
11485 {
11486 case DW_FORM_addr:
ac56253d 11487 {
ac56253d
TT
11488 gdb_byte *data;
11489
98bfdba5
PA
11490 if (TYPE_LENGTH (type) != cu_header->addr_size)
11491 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11492 cu_header->addr_size,
98bfdba5 11493 TYPE_LENGTH (type));
ac56253d
TT
11494 /* Symbols of this form are reasonably rare, so we just
11495 piggyback on the existing location code rather than writing
11496 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11497 *baton = obstack_alloc (&objfile->objfile_obstack,
11498 sizeof (struct dwarf2_locexpr_baton));
11499 (*baton)->per_cu = cu->per_cu;
11500 gdb_assert ((*baton)->per_cu);
ac56253d 11501
98bfdba5
PA
11502 (*baton)->size = 2 + cu_header->addr_size;
11503 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11504 (*baton)->data = data;
ac56253d
TT
11505
11506 data[0] = DW_OP_addr;
11507 store_unsigned_integer (&data[1], cu_header->addr_size,
11508 byte_order, DW_ADDR (attr));
11509 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11510 }
c906108c 11511 break;
4ac36638 11512 case DW_FORM_string:
93b5768b 11513 case DW_FORM_strp:
98bfdba5
PA
11514 /* DW_STRING is already allocated on the objfile obstack, point
11515 directly to it. */
11516 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11517 break;
c906108c
SS
11518 case DW_FORM_block1:
11519 case DW_FORM_block2:
11520 case DW_FORM_block4:
11521 case DW_FORM_block:
2dc7f7b3 11522 case DW_FORM_exprloc:
c906108c 11523 blk = DW_BLOCK (attr);
98bfdba5
PA
11524 if (TYPE_LENGTH (type) != blk->size)
11525 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11526 TYPE_LENGTH (type));
11527 *bytes = blk->data;
c906108c 11528 break;
2df3850c
JM
11529
11530 /* The DW_AT_const_value attributes are supposed to carry the
11531 symbol's value "represented as it would be on the target
11532 architecture." By the time we get here, it's already been
11533 converted to host endianness, so we just need to sign- or
11534 zero-extend it as appropriate. */
11535 case DW_FORM_data1:
3e43a32a
MS
11536 *bytes = dwarf2_const_value_data (attr, type, name,
11537 obstack, cu, value, 8);
2df3850c 11538 break;
c906108c 11539 case DW_FORM_data2:
3e43a32a
MS
11540 *bytes = dwarf2_const_value_data (attr, type, name,
11541 obstack, cu, value, 16);
2df3850c 11542 break;
c906108c 11543 case DW_FORM_data4:
3e43a32a
MS
11544 *bytes = dwarf2_const_value_data (attr, type, name,
11545 obstack, cu, value, 32);
2df3850c 11546 break;
c906108c 11547 case DW_FORM_data8:
3e43a32a
MS
11548 *bytes = dwarf2_const_value_data (attr, type, name,
11549 obstack, cu, value, 64);
2df3850c
JM
11550 break;
11551
c906108c 11552 case DW_FORM_sdata:
98bfdba5 11553 *value = DW_SND (attr);
2df3850c
JM
11554 break;
11555
c906108c 11556 case DW_FORM_udata:
98bfdba5 11557 *value = DW_UNSND (attr);
c906108c 11558 break;
2df3850c 11559
c906108c 11560 default:
4d3c2250 11561 complaint (&symfile_complaints,
e2e0b3e5 11562 _("unsupported const value attribute form: '%s'"),
4d3c2250 11563 dwarf_form_name (attr->form));
98bfdba5 11564 *value = 0;
c906108c
SS
11565 break;
11566 }
11567}
11568
2df3850c 11569
98bfdba5
PA
11570/* Copy constant value from an attribute to a symbol. */
11571
2df3850c 11572static void
98bfdba5
PA
11573dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11574 struct dwarf2_cu *cu)
2df3850c 11575{
98bfdba5
PA
11576 struct objfile *objfile = cu->objfile;
11577 struct comp_unit_head *cu_header = &cu->header;
11578 long value;
11579 gdb_byte *bytes;
11580 struct dwarf2_locexpr_baton *baton;
2df3850c 11581
98bfdba5
PA
11582 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11583 SYMBOL_PRINT_NAME (sym),
11584 &objfile->objfile_obstack, cu,
11585 &value, &bytes, &baton);
2df3850c 11586
98bfdba5
PA
11587 if (baton != NULL)
11588 {
11589 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11590 SYMBOL_LOCATION_BATON (sym) = baton;
11591 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11592 }
11593 else if (bytes != NULL)
11594 {
11595 SYMBOL_VALUE_BYTES (sym) = bytes;
11596 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11597 }
11598 else
11599 {
11600 SYMBOL_VALUE (sym) = value;
11601 SYMBOL_CLASS (sym) = LOC_CONST;
11602 }
2df3850c
JM
11603}
11604
c906108c
SS
11605/* Return the type of the die in question using its DW_AT_type attribute. */
11606
11607static struct type *
e7c27a73 11608die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11609{
c906108c 11610 struct attribute *type_attr;
c906108c 11611
e142c38c 11612 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11613 if (!type_attr)
11614 {
11615 /* A missing DW_AT_type represents a void type. */
46bf5051 11616 return objfile_type (cu->objfile)->builtin_void;
c906108c 11617 }
348e048f 11618
673bfd45 11619 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11620}
11621
b4ba55a1
JB
11622/* True iff CU's producer generates GNAT Ada auxiliary information
11623 that allows to find parallel types through that information instead
11624 of having to do expensive parallel lookups by type name. */
11625
11626static int
11627need_gnat_info (struct dwarf2_cu *cu)
11628{
11629 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11630 of GNAT produces this auxiliary information, without any indication
11631 that it is produced. Part of enhancing the FSF version of GNAT
11632 to produce that information will be to put in place an indicator
11633 that we can use in order to determine whether the descriptive type
11634 info is available or not. One suggestion that has been made is
11635 to use a new attribute, attached to the CU die. For now, assume
11636 that the descriptive type info is not available. */
11637 return 0;
11638}
11639
b4ba55a1
JB
11640/* Return the auxiliary type of the die in question using its
11641 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11642 attribute is not present. */
11643
11644static struct type *
11645die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11646{
b4ba55a1 11647 struct attribute *type_attr;
b4ba55a1
JB
11648
11649 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11650 if (!type_attr)
11651 return NULL;
11652
673bfd45 11653 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11654}
11655
11656/* If DIE has a descriptive_type attribute, then set the TYPE's
11657 descriptive type accordingly. */
11658
11659static void
11660set_descriptive_type (struct type *type, struct die_info *die,
11661 struct dwarf2_cu *cu)
11662{
11663 struct type *descriptive_type = die_descriptive_type (die, cu);
11664
11665 if (descriptive_type)
11666 {
11667 ALLOCATE_GNAT_AUX_TYPE (type);
11668 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11669 }
11670}
11671
c906108c
SS
11672/* Return the containing type of the die in question using its
11673 DW_AT_containing_type attribute. */
11674
11675static struct type *
e7c27a73 11676die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11677{
c906108c 11678 struct attribute *type_attr;
c906108c 11679
e142c38c 11680 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11681 if (!type_attr)
11682 error (_("Dwarf Error: Problem turning containing type into gdb type "
11683 "[in module %s]"), cu->objfile->name);
11684
673bfd45 11685 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11686}
11687
673bfd45
DE
11688/* Look up the type of DIE in CU using its type attribute ATTR.
11689 If there is no type substitute an error marker. */
11690
c906108c 11691static struct type *
673bfd45
DE
11692lookup_die_type (struct die_info *die, struct attribute *attr,
11693 struct dwarf2_cu *cu)
c906108c 11694{
f792889a
DJ
11695 struct type *this_type;
11696
673bfd45
DE
11697 /* First see if we have it cached. */
11698
11699 if (is_ref_attr (attr))
11700 {
11701 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11702
11703 this_type = get_die_type_at_offset (offset, cu->per_cu);
11704 }
55f1336d 11705 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
11706 {
11707 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11708 struct dwarf2_cu *sig_cu;
11709 unsigned int offset;
11710
11711 /* sig_type will be NULL if the signatured type is missing from
11712 the debug info. */
11713 if (sig_type == NULL)
11714 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11715 "at 0x%x [in module %s]"),
11716 die->offset, cu->objfile->name);
11717
11718 gdb_assert (sig_type->per_cu.from_debug_types);
b3c8eb43 11719 offset = sig_type->per_cu.offset + sig_type->type_offset;
673bfd45
DE
11720 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11721 }
11722 else
11723 {
11724 dump_die_for_error (die);
11725 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11726 dwarf_attr_name (attr->name), cu->objfile->name);
11727 }
11728
11729 /* If not cached we need to read it in. */
11730
11731 if (this_type == NULL)
11732 {
11733 struct die_info *type_die;
11734 struct dwarf2_cu *type_cu = cu;
11735
11736 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11737 /* If the type is cached, we should have found it above. */
11738 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11739 this_type = read_type_die_1 (type_die, type_cu);
11740 }
11741
11742 /* If we still don't have a type use an error marker. */
11743
11744 if (this_type == NULL)
c906108c 11745 {
b00fdb78
TT
11746 char *message, *saved;
11747
11748 /* read_type_die already issued a complaint. */
11749 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11750 cu->objfile->name,
11751 cu->header.offset,
11752 die->offset);
11753 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11754 message, strlen (message));
11755 xfree (message);
11756
11757 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11758 }
673bfd45 11759
f792889a 11760 return this_type;
c906108c
SS
11761}
11762
673bfd45
DE
11763/* Return the type in DIE, CU.
11764 Returns NULL for invalid types.
11765
11766 This first does a lookup in the appropriate type_hash table,
11767 and only reads the die in if necessary.
11768
11769 NOTE: This can be called when reading in partial or full symbols. */
11770
f792889a 11771static struct type *
e7c27a73 11772read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11773{
f792889a
DJ
11774 struct type *this_type;
11775
11776 this_type = get_die_type (die, cu);
11777 if (this_type)
11778 return this_type;
11779
673bfd45
DE
11780 return read_type_die_1 (die, cu);
11781}
11782
11783/* Read the type in DIE, CU.
11784 Returns NULL for invalid types. */
11785
11786static struct type *
11787read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11788{
11789 struct type *this_type = NULL;
11790
c906108c
SS
11791 switch (die->tag)
11792 {
11793 case DW_TAG_class_type:
680b30c7 11794 case DW_TAG_interface_type:
c906108c
SS
11795 case DW_TAG_structure_type:
11796 case DW_TAG_union_type:
f792889a 11797 this_type = read_structure_type (die, cu);
c906108c
SS
11798 break;
11799 case DW_TAG_enumeration_type:
f792889a 11800 this_type = read_enumeration_type (die, cu);
c906108c
SS
11801 break;
11802 case DW_TAG_subprogram:
11803 case DW_TAG_subroutine_type:
edb3359d 11804 case DW_TAG_inlined_subroutine:
f792889a 11805 this_type = read_subroutine_type (die, cu);
c906108c
SS
11806 break;
11807 case DW_TAG_array_type:
f792889a 11808 this_type = read_array_type (die, cu);
c906108c 11809 break;
72019c9c 11810 case DW_TAG_set_type:
f792889a 11811 this_type = read_set_type (die, cu);
72019c9c 11812 break;
c906108c 11813 case DW_TAG_pointer_type:
f792889a 11814 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11815 break;
11816 case DW_TAG_ptr_to_member_type:
f792889a 11817 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11818 break;
11819 case DW_TAG_reference_type:
f792889a 11820 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11821 break;
11822 case DW_TAG_const_type:
f792889a 11823 this_type = read_tag_const_type (die, cu);
c906108c
SS
11824 break;
11825 case DW_TAG_volatile_type:
f792889a 11826 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11827 break;
11828 case DW_TAG_string_type:
f792889a 11829 this_type = read_tag_string_type (die, cu);
c906108c
SS
11830 break;
11831 case DW_TAG_typedef:
f792889a 11832 this_type = read_typedef (die, cu);
c906108c 11833 break;
a02abb62 11834 case DW_TAG_subrange_type:
f792889a 11835 this_type = read_subrange_type (die, cu);
a02abb62 11836 break;
c906108c 11837 case DW_TAG_base_type:
f792889a 11838 this_type = read_base_type (die, cu);
c906108c 11839 break;
81a17f79 11840 case DW_TAG_unspecified_type:
f792889a 11841 this_type = read_unspecified_type (die, cu);
81a17f79 11842 break;
0114d602
DJ
11843 case DW_TAG_namespace:
11844 this_type = read_namespace_type (die, cu);
11845 break;
f55ee35c
JK
11846 case DW_TAG_module:
11847 this_type = read_module_type (die, cu);
11848 break;
c906108c 11849 default:
3e43a32a
MS
11850 complaint (&symfile_complaints,
11851 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11852 dwarf_tag_name (die->tag));
c906108c
SS
11853 break;
11854 }
63d06c5c 11855
f792889a 11856 return this_type;
63d06c5c
DC
11857}
11858
abc72ce4
DE
11859/* See if we can figure out if the class lives in a namespace. We do
11860 this by looking for a member function; its demangled name will
11861 contain namespace info, if there is any.
11862 Return the computed name or NULL.
11863 Space for the result is allocated on the objfile's obstack.
11864 This is the full-die version of guess_partial_die_structure_name.
11865 In this case we know DIE has no useful parent. */
11866
11867static char *
11868guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11869{
11870 struct die_info *spec_die;
11871 struct dwarf2_cu *spec_cu;
11872 struct die_info *child;
11873
11874 spec_cu = cu;
11875 spec_die = die_specification (die, &spec_cu);
11876 if (spec_die != NULL)
11877 {
11878 die = spec_die;
11879 cu = spec_cu;
11880 }
11881
11882 for (child = die->child;
11883 child != NULL;
11884 child = child->sibling)
11885 {
11886 if (child->tag == DW_TAG_subprogram)
11887 {
11888 struct attribute *attr;
11889
11890 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11891 if (attr == NULL)
11892 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11893 if (attr != NULL)
11894 {
11895 char *actual_name
11896 = language_class_name_from_physname (cu->language_defn,
11897 DW_STRING (attr));
11898 char *name = NULL;
11899
11900 if (actual_name != NULL)
11901 {
11902 char *die_name = dwarf2_name (die, cu);
11903
11904 if (die_name != NULL
11905 && strcmp (die_name, actual_name) != 0)
11906 {
11907 /* Strip off the class name from the full name.
11908 We want the prefix. */
11909 int die_name_len = strlen (die_name);
11910 int actual_name_len = strlen (actual_name);
11911
11912 /* Test for '::' as a sanity check. */
11913 if (actual_name_len > die_name_len + 2
3e43a32a
MS
11914 && actual_name[actual_name_len
11915 - die_name_len - 1] == ':')
abc72ce4
DE
11916 name =
11917 obsavestring (actual_name,
11918 actual_name_len - die_name_len - 2,
11919 &cu->objfile->objfile_obstack);
11920 }
11921 }
11922 xfree (actual_name);
11923 return name;
11924 }
11925 }
11926 }
11927
11928 return NULL;
11929}
11930
fdde2d81 11931/* Return the name of the namespace/class that DIE is defined within,
0114d602 11932 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11933
0114d602
DJ
11934 For example, if we're within the method foo() in the following
11935 code:
11936
11937 namespace N {
11938 class C {
11939 void foo () {
11940 }
11941 };
11942 }
11943
11944 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11945
11946static char *
e142c38c 11947determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11948{
0114d602
DJ
11949 struct die_info *parent, *spec_die;
11950 struct dwarf2_cu *spec_cu;
11951 struct type *parent_type;
63d06c5c 11952
f55ee35c
JK
11953 if (cu->language != language_cplus && cu->language != language_java
11954 && cu->language != language_fortran)
0114d602
DJ
11955 return "";
11956
11957 /* We have to be careful in the presence of DW_AT_specification.
11958 For example, with GCC 3.4, given the code
11959
11960 namespace N {
11961 void foo() {
11962 // Definition of N::foo.
11963 }
11964 }
11965
11966 then we'll have a tree of DIEs like this:
11967
11968 1: DW_TAG_compile_unit
11969 2: DW_TAG_namespace // N
11970 3: DW_TAG_subprogram // declaration of N::foo
11971 4: DW_TAG_subprogram // definition of N::foo
11972 DW_AT_specification // refers to die #3
11973
11974 Thus, when processing die #4, we have to pretend that we're in
11975 the context of its DW_AT_specification, namely the contex of die
11976 #3. */
11977 spec_cu = cu;
11978 spec_die = die_specification (die, &spec_cu);
11979 if (spec_die == NULL)
11980 parent = die->parent;
11981 else
63d06c5c 11982 {
0114d602
DJ
11983 parent = spec_die->parent;
11984 cu = spec_cu;
63d06c5c 11985 }
0114d602
DJ
11986
11987 if (parent == NULL)
11988 return "";
98bfdba5
PA
11989 else if (parent->building_fullname)
11990 {
11991 const char *name;
11992 const char *parent_name;
11993
11994 /* It has been seen on RealView 2.2 built binaries,
11995 DW_TAG_template_type_param types actually _defined_ as
11996 children of the parent class:
11997
11998 enum E {};
11999 template class <class Enum> Class{};
12000 Class<enum E> class_e;
12001
12002 1: DW_TAG_class_type (Class)
12003 2: DW_TAG_enumeration_type (E)
12004 3: DW_TAG_enumerator (enum1:0)
12005 3: DW_TAG_enumerator (enum2:1)
12006 ...
12007 2: DW_TAG_template_type_param
12008 DW_AT_type DW_FORM_ref_udata (E)
12009
12010 Besides being broken debug info, it can put GDB into an
12011 infinite loop. Consider:
12012
12013 When we're building the full name for Class<E>, we'll start
12014 at Class, and go look over its template type parameters,
12015 finding E. We'll then try to build the full name of E, and
12016 reach here. We're now trying to build the full name of E,
12017 and look over the parent DIE for containing scope. In the
12018 broken case, if we followed the parent DIE of E, we'd again
12019 find Class, and once again go look at its template type
12020 arguments, etc., etc. Simply don't consider such parent die
12021 as source-level parent of this die (it can't be, the language
12022 doesn't allow it), and break the loop here. */
12023 name = dwarf2_name (die, cu);
12024 parent_name = dwarf2_name (parent, cu);
12025 complaint (&symfile_complaints,
12026 _("template param type '%s' defined within parent '%s'"),
12027 name ? name : "<unknown>",
12028 parent_name ? parent_name : "<unknown>");
12029 return "";
12030 }
63d06c5c 12031 else
0114d602
DJ
12032 switch (parent->tag)
12033 {
63d06c5c 12034 case DW_TAG_namespace:
0114d602 12035 parent_type = read_type_die (parent, cu);
acebe513
UW
12036 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12037 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12038 Work around this problem here. */
12039 if (cu->language == language_cplus
12040 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12041 return "";
0114d602
DJ
12042 /* We give a name to even anonymous namespaces. */
12043 return TYPE_TAG_NAME (parent_type);
63d06c5c 12044 case DW_TAG_class_type:
680b30c7 12045 case DW_TAG_interface_type:
63d06c5c 12046 case DW_TAG_structure_type:
0114d602 12047 case DW_TAG_union_type:
f55ee35c 12048 case DW_TAG_module:
0114d602
DJ
12049 parent_type = read_type_die (parent, cu);
12050 if (TYPE_TAG_NAME (parent_type) != NULL)
12051 return TYPE_TAG_NAME (parent_type);
12052 else
12053 /* An anonymous structure is only allowed non-static data
12054 members; no typedefs, no member functions, et cetera.
12055 So it does not need a prefix. */
12056 return "";
abc72ce4
DE
12057 case DW_TAG_compile_unit:
12058 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12059 if (cu->language == language_cplus
12060 && dwarf2_per_objfile->types.asection != NULL
12061 && die->child != NULL
12062 && (die->tag == DW_TAG_class_type
12063 || die->tag == DW_TAG_structure_type
12064 || die->tag == DW_TAG_union_type))
12065 {
12066 char *name = guess_full_die_structure_name (die, cu);
12067 if (name != NULL)
12068 return name;
12069 }
12070 return "";
63d06c5c 12071 default:
8176b9b8 12072 return determine_prefix (parent, cu);
63d06c5c 12073 }
63d06c5c
DC
12074}
12075
3e43a32a
MS
12076/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12077 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12078 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12079 an obconcat, otherwise allocate storage for the result. The CU argument is
12080 used to determine the language and hence, the appropriate separator. */
987504bb 12081
f55ee35c 12082#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12083
12084static char *
f55ee35c
JK
12085typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12086 int physname, struct dwarf2_cu *cu)
63d06c5c 12087{
f55ee35c 12088 const char *lead = "";
5c315b68 12089 const char *sep;
63d06c5c 12090
3e43a32a
MS
12091 if (suffix == NULL || suffix[0] == '\0'
12092 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12093 sep = "";
12094 else if (cu->language == language_java)
12095 sep = ".";
f55ee35c
JK
12096 else if (cu->language == language_fortran && physname)
12097 {
12098 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12099 DW_AT_MIPS_linkage_name is preferred and used instead. */
12100
12101 lead = "__";
12102 sep = "_MOD_";
12103 }
987504bb
JJ
12104 else
12105 sep = "::";
63d06c5c 12106
6dd47d34
DE
12107 if (prefix == NULL)
12108 prefix = "";
12109 if (suffix == NULL)
12110 suffix = "";
12111
987504bb
JJ
12112 if (obs == NULL)
12113 {
3e43a32a
MS
12114 char *retval
12115 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12116
f55ee35c
JK
12117 strcpy (retval, lead);
12118 strcat (retval, prefix);
6dd47d34
DE
12119 strcat (retval, sep);
12120 strcat (retval, suffix);
63d06c5c
DC
12121 return retval;
12122 }
987504bb
JJ
12123 else
12124 {
12125 /* We have an obstack. */
f55ee35c 12126 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12127 }
63d06c5c
DC
12128}
12129
c906108c
SS
12130/* Return sibling of die, NULL if no sibling. */
12131
f9aca02d 12132static struct die_info *
fba45db2 12133sibling_die (struct die_info *die)
c906108c 12134{
639d11d3 12135 return die->sibling;
c906108c
SS
12136}
12137
71c25dea
TT
12138/* Get name of a die, return NULL if not found. */
12139
12140static char *
12141dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12142 struct obstack *obstack)
12143{
12144 if (name && cu->language == language_cplus)
12145 {
12146 char *canon_name = cp_canonicalize_string (name);
12147
12148 if (canon_name != NULL)
12149 {
12150 if (strcmp (canon_name, name) != 0)
12151 name = obsavestring (canon_name, strlen (canon_name),
12152 obstack);
12153 xfree (canon_name);
12154 }
12155 }
12156
12157 return name;
c906108c
SS
12158}
12159
9219021c
DC
12160/* Get name of a die, return NULL if not found. */
12161
12162static char *
e142c38c 12163dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12164{
12165 struct attribute *attr;
12166
e142c38c 12167 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12168 if ((!attr || !DW_STRING (attr))
12169 && die->tag != DW_TAG_class_type
12170 && die->tag != DW_TAG_interface_type
12171 && die->tag != DW_TAG_structure_type
12172 && die->tag != DW_TAG_union_type)
71c25dea
TT
12173 return NULL;
12174
12175 switch (die->tag)
12176 {
12177 case DW_TAG_compile_unit:
12178 /* Compilation units have a DW_AT_name that is a filename, not
12179 a source language identifier. */
12180 case DW_TAG_enumeration_type:
12181 case DW_TAG_enumerator:
12182 /* These tags always have simple identifiers already; no need
12183 to canonicalize them. */
12184 return DW_STRING (attr);
907af001 12185
418835cc
KS
12186 case DW_TAG_subprogram:
12187 /* Java constructors will all be named "<init>", so return
12188 the class name when we see this special case. */
12189 if (cu->language == language_java
12190 && DW_STRING (attr) != NULL
12191 && strcmp (DW_STRING (attr), "<init>") == 0)
12192 {
12193 struct dwarf2_cu *spec_cu = cu;
12194 struct die_info *spec_die;
12195
12196 /* GCJ will output '<init>' for Java constructor names.
12197 For this special case, return the name of the parent class. */
12198
12199 /* GCJ may output suprogram DIEs with AT_specification set.
12200 If so, use the name of the specified DIE. */
12201 spec_die = die_specification (die, &spec_cu);
12202 if (spec_die != NULL)
12203 return dwarf2_name (spec_die, spec_cu);
12204
12205 do
12206 {
12207 die = die->parent;
12208 if (die->tag == DW_TAG_class_type)
12209 return dwarf2_name (die, cu);
12210 }
12211 while (die->tag != DW_TAG_compile_unit);
12212 }
907af001
UW
12213 break;
12214
12215 case DW_TAG_class_type:
12216 case DW_TAG_interface_type:
12217 case DW_TAG_structure_type:
12218 case DW_TAG_union_type:
12219 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12220 structures or unions. These were of the form "._%d" in GCC 4.1,
12221 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12222 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12223 if (attr && DW_STRING (attr)
12224 && (strncmp (DW_STRING (attr), "._", 2) == 0
12225 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12226 return NULL;
53832f31
TT
12227
12228 /* GCC might emit a nameless typedef that has a linkage name. See
12229 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12230 if (!attr || DW_STRING (attr) == NULL)
12231 {
df5c6c50 12232 char *demangled = NULL;
53832f31
TT
12233
12234 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12235 if (attr == NULL)
12236 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12237
12238 if (attr == NULL || DW_STRING (attr) == NULL)
12239 return NULL;
12240
df5c6c50
JK
12241 /* Avoid demangling DW_STRING (attr) the second time on a second
12242 call for the same DIE. */
12243 if (!DW_STRING_IS_CANONICAL (attr))
12244 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12245
12246 if (demangled)
12247 {
12248 /* FIXME: we already did this for the partial symbol... */
12249 DW_STRING (attr)
12250 = obsavestring (demangled, strlen (demangled),
12251 &cu->objfile->objfile_obstack);
12252 DW_STRING_IS_CANONICAL (attr) = 1;
12253 xfree (demangled);
12254 }
12255 }
907af001
UW
12256 break;
12257
71c25dea 12258 default:
907af001
UW
12259 break;
12260 }
12261
12262 if (!DW_STRING_IS_CANONICAL (attr))
12263 {
12264 DW_STRING (attr)
12265 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12266 &cu->objfile->objfile_obstack);
12267 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12268 }
907af001 12269 return DW_STRING (attr);
9219021c
DC
12270}
12271
12272/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12273 is none. *EXT_CU is the CU containing DIE on input, and the CU
12274 containing the return value on output. */
9219021c
DC
12275
12276static struct die_info *
f2f0e013 12277dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12278{
12279 struct attribute *attr;
9219021c 12280
f2f0e013 12281 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12282 if (attr == NULL)
12283 return NULL;
12284
f2f0e013 12285 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12286}
12287
c906108c
SS
12288/* Convert a DIE tag into its string name. */
12289
12290static char *
aa1ee363 12291dwarf_tag_name (unsigned tag)
c906108c
SS
12292{
12293 switch (tag)
12294 {
12295 case DW_TAG_padding:
12296 return "DW_TAG_padding";
12297 case DW_TAG_array_type:
12298 return "DW_TAG_array_type";
12299 case DW_TAG_class_type:
12300 return "DW_TAG_class_type";
12301 case DW_TAG_entry_point:
12302 return "DW_TAG_entry_point";
12303 case DW_TAG_enumeration_type:
12304 return "DW_TAG_enumeration_type";
12305 case DW_TAG_formal_parameter:
12306 return "DW_TAG_formal_parameter";
12307 case DW_TAG_imported_declaration:
12308 return "DW_TAG_imported_declaration";
12309 case DW_TAG_label:
12310 return "DW_TAG_label";
12311 case DW_TAG_lexical_block:
12312 return "DW_TAG_lexical_block";
12313 case DW_TAG_member:
12314 return "DW_TAG_member";
12315 case DW_TAG_pointer_type:
12316 return "DW_TAG_pointer_type";
12317 case DW_TAG_reference_type:
12318 return "DW_TAG_reference_type";
12319 case DW_TAG_compile_unit:
12320 return "DW_TAG_compile_unit";
12321 case DW_TAG_string_type:
12322 return "DW_TAG_string_type";
12323 case DW_TAG_structure_type:
12324 return "DW_TAG_structure_type";
12325 case DW_TAG_subroutine_type:
12326 return "DW_TAG_subroutine_type";
12327 case DW_TAG_typedef:
12328 return "DW_TAG_typedef";
12329 case DW_TAG_union_type:
12330 return "DW_TAG_union_type";
12331 case DW_TAG_unspecified_parameters:
12332 return "DW_TAG_unspecified_parameters";
12333 case DW_TAG_variant:
12334 return "DW_TAG_variant";
12335 case DW_TAG_common_block:
12336 return "DW_TAG_common_block";
12337 case DW_TAG_common_inclusion:
12338 return "DW_TAG_common_inclusion";
12339 case DW_TAG_inheritance:
12340 return "DW_TAG_inheritance";
12341 case DW_TAG_inlined_subroutine:
12342 return "DW_TAG_inlined_subroutine";
12343 case DW_TAG_module:
12344 return "DW_TAG_module";
12345 case DW_TAG_ptr_to_member_type:
12346 return "DW_TAG_ptr_to_member_type";
12347 case DW_TAG_set_type:
12348 return "DW_TAG_set_type";
12349 case DW_TAG_subrange_type:
12350 return "DW_TAG_subrange_type";
12351 case DW_TAG_with_stmt:
12352 return "DW_TAG_with_stmt";
12353 case DW_TAG_access_declaration:
12354 return "DW_TAG_access_declaration";
12355 case DW_TAG_base_type:
12356 return "DW_TAG_base_type";
12357 case DW_TAG_catch_block:
12358 return "DW_TAG_catch_block";
12359 case DW_TAG_const_type:
12360 return "DW_TAG_const_type";
12361 case DW_TAG_constant:
12362 return "DW_TAG_constant";
12363 case DW_TAG_enumerator:
12364 return "DW_TAG_enumerator";
12365 case DW_TAG_file_type:
12366 return "DW_TAG_file_type";
12367 case DW_TAG_friend:
12368 return "DW_TAG_friend";
12369 case DW_TAG_namelist:
12370 return "DW_TAG_namelist";
12371 case DW_TAG_namelist_item:
12372 return "DW_TAG_namelist_item";
12373 case DW_TAG_packed_type:
12374 return "DW_TAG_packed_type";
12375 case DW_TAG_subprogram:
12376 return "DW_TAG_subprogram";
12377 case DW_TAG_template_type_param:
12378 return "DW_TAG_template_type_param";
12379 case DW_TAG_template_value_param:
12380 return "DW_TAG_template_value_param";
12381 case DW_TAG_thrown_type:
12382 return "DW_TAG_thrown_type";
12383 case DW_TAG_try_block:
12384 return "DW_TAG_try_block";
12385 case DW_TAG_variant_part:
12386 return "DW_TAG_variant_part";
12387 case DW_TAG_variable:
12388 return "DW_TAG_variable";
12389 case DW_TAG_volatile_type:
12390 return "DW_TAG_volatile_type";
d9fa45fe
DC
12391 case DW_TAG_dwarf_procedure:
12392 return "DW_TAG_dwarf_procedure";
12393 case DW_TAG_restrict_type:
12394 return "DW_TAG_restrict_type";
12395 case DW_TAG_interface_type:
12396 return "DW_TAG_interface_type";
12397 case DW_TAG_namespace:
12398 return "DW_TAG_namespace";
12399 case DW_TAG_imported_module:
12400 return "DW_TAG_imported_module";
12401 case DW_TAG_unspecified_type:
12402 return "DW_TAG_unspecified_type";
12403 case DW_TAG_partial_unit:
12404 return "DW_TAG_partial_unit";
12405 case DW_TAG_imported_unit:
12406 return "DW_TAG_imported_unit";
b7619582
GF
12407 case DW_TAG_condition:
12408 return "DW_TAG_condition";
12409 case DW_TAG_shared_type:
12410 return "DW_TAG_shared_type";
348e048f
DE
12411 case DW_TAG_type_unit:
12412 return "DW_TAG_type_unit";
c906108c
SS
12413 case DW_TAG_MIPS_loop:
12414 return "DW_TAG_MIPS_loop";
b7619582
GF
12415 case DW_TAG_HP_array_descriptor:
12416 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12417 case DW_TAG_format_label:
12418 return "DW_TAG_format_label";
12419 case DW_TAG_function_template:
12420 return "DW_TAG_function_template";
12421 case DW_TAG_class_template:
12422 return "DW_TAG_class_template";
b7619582
GF
12423 case DW_TAG_GNU_BINCL:
12424 return "DW_TAG_GNU_BINCL";
12425 case DW_TAG_GNU_EINCL:
12426 return "DW_TAG_GNU_EINCL";
12427 case DW_TAG_upc_shared_type:
12428 return "DW_TAG_upc_shared_type";
12429 case DW_TAG_upc_strict_type:
12430 return "DW_TAG_upc_strict_type";
12431 case DW_TAG_upc_relaxed_type:
12432 return "DW_TAG_upc_relaxed_type";
12433 case DW_TAG_PGI_kanji_type:
12434 return "DW_TAG_PGI_kanji_type";
12435 case DW_TAG_PGI_interface_block:
12436 return "DW_TAG_PGI_interface_block";
c906108c
SS
12437 default:
12438 return "DW_TAG_<unknown>";
12439 }
12440}
12441
12442/* Convert a DWARF attribute code into its string name. */
12443
12444static char *
aa1ee363 12445dwarf_attr_name (unsigned attr)
c906108c
SS
12446{
12447 switch (attr)
12448 {
12449 case DW_AT_sibling:
12450 return "DW_AT_sibling";
12451 case DW_AT_location:
12452 return "DW_AT_location";
12453 case DW_AT_name:
12454 return "DW_AT_name";
12455 case DW_AT_ordering:
12456 return "DW_AT_ordering";
12457 case DW_AT_subscr_data:
12458 return "DW_AT_subscr_data";
12459 case DW_AT_byte_size:
12460 return "DW_AT_byte_size";
12461 case DW_AT_bit_offset:
12462 return "DW_AT_bit_offset";
12463 case DW_AT_bit_size:
12464 return "DW_AT_bit_size";
12465 case DW_AT_element_list:
12466 return "DW_AT_element_list";
12467 case DW_AT_stmt_list:
12468 return "DW_AT_stmt_list";
12469 case DW_AT_low_pc:
12470 return "DW_AT_low_pc";
12471 case DW_AT_high_pc:
12472 return "DW_AT_high_pc";
12473 case DW_AT_language:
12474 return "DW_AT_language";
12475 case DW_AT_member:
12476 return "DW_AT_member";
12477 case DW_AT_discr:
12478 return "DW_AT_discr";
12479 case DW_AT_discr_value:
12480 return "DW_AT_discr_value";
12481 case DW_AT_visibility:
12482 return "DW_AT_visibility";
12483 case DW_AT_import:
12484 return "DW_AT_import";
12485 case DW_AT_string_length:
12486 return "DW_AT_string_length";
12487 case DW_AT_common_reference:
12488 return "DW_AT_common_reference";
12489 case DW_AT_comp_dir:
12490 return "DW_AT_comp_dir";
12491 case DW_AT_const_value:
12492 return "DW_AT_const_value";
12493 case DW_AT_containing_type:
12494 return "DW_AT_containing_type";
12495 case DW_AT_default_value:
12496 return "DW_AT_default_value";
12497 case DW_AT_inline:
12498 return "DW_AT_inline";
12499 case DW_AT_is_optional:
12500 return "DW_AT_is_optional";
12501 case DW_AT_lower_bound:
12502 return "DW_AT_lower_bound";
12503 case DW_AT_producer:
12504 return "DW_AT_producer";
12505 case DW_AT_prototyped:
12506 return "DW_AT_prototyped";
12507 case DW_AT_return_addr:
12508 return "DW_AT_return_addr";
12509 case DW_AT_start_scope:
12510 return "DW_AT_start_scope";
09fa0d7c
JK
12511 case DW_AT_bit_stride:
12512 return "DW_AT_bit_stride";
c906108c
SS
12513 case DW_AT_upper_bound:
12514 return "DW_AT_upper_bound";
12515 case DW_AT_abstract_origin:
12516 return "DW_AT_abstract_origin";
12517 case DW_AT_accessibility:
12518 return "DW_AT_accessibility";
12519 case DW_AT_address_class:
12520 return "DW_AT_address_class";
12521 case DW_AT_artificial:
12522 return "DW_AT_artificial";
12523 case DW_AT_base_types:
12524 return "DW_AT_base_types";
12525 case DW_AT_calling_convention:
12526 return "DW_AT_calling_convention";
12527 case DW_AT_count:
12528 return "DW_AT_count";
12529 case DW_AT_data_member_location:
12530 return "DW_AT_data_member_location";
12531 case DW_AT_decl_column:
12532 return "DW_AT_decl_column";
12533 case DW_AT_decl_file:
12534 return "DW_AT_decl_file";
12535 case DW_AT_decl_line:
12536 return "DW_AT_decl_line";
12537 case DW_AT_declaration:
12538 return "DW_AT_declaration";
12539 case DW_AT_discr_list:
12540 return "DW_AT_discr_list";
12541 case DW_AT_encoding:
12542 return "DW_AT_encoding";
12543 case DW_AT_external:
12544 return "DW_AT_external";
12545 case DW_AT_frame_base:
12546 return "DW_AT_frame_base";
12547 case DW_AT_friend:
12548 return "DW_AT_friend";
12549 case DW_AT_identifier_case:
12550 return "DW_AT_identifier_case";
12551 case DW_AT_macro_info:
12552 return "DW_AT_macro_info";
12553 case DW_AT_namelist_items:
12554 return "DW_AT_namelist_items";
12555 case DW_AT_priority:
12556 return "DW_AT_priority";
12557 case DW_AT_segment:
12558 return "DW_AT_segment";
12559 case DW_AT_specification:
12560 return "DW_AT_specification";
12561 case DW_AT_static_link:
12562 return "DW_AT_static_link";
12563 case DW_AT_type:
12564 return "DW_AT_type";
12565 case DW_AT_use_location:
12566 return "DW_AT_use_location";
12567 case DW_AT_variable_parameter:
12568 return "DW_AT_variable_parameter";
12569 case DW_AT_virtuality:
12570 return "DW_AT_virtuality";
12571 case DW_AT_vtable_elem_location:
12572 return "DW_AT_vtable_elem_location";
b7619582 12573 /* DWARF 3 values. */
d9fa45fe
DC
12574 case DW_AT_allocated:
12575 return "DW_AT_allocated";
12576 case DW_AT_associated:
12577 return "DW_AT_associated";
12578 case DW_AT_data_location:
12579 return "DW_AT_data_location";
09fa0d7c
JK
12580 case DW_AT_byte_stride:
12581 return "DW_AT_byte_stride";
d9fa45fe
DC
12582 case DW_AT_entry_pc:
12583 return "DW_AT_entry_pc";
12584 case DW_AT_use_UTF8:
12585 return "DW_AT_use_UTF8";
12586 case DW_AT_extension:
12587 return "DW_AT_extension";
12588 case DW_AT_ranges:
12589 return "DW_AT_ranges";
12590 case DW_AT_trampoline:
12591 return "DW_AT_trampoline";
12592 case DW_AT_call_column:
12593 return "DW_AT_call_column";
12594 case DW_AT_call_file:
12595 return "DW_AT_call_file";
12596 case DW_AT_call_line:
12597 return "DW_AT_call_line";
b7619582
GF
12598 case DW_AT_description:
12599 return "DW_AT_description";
12600 case DW_AT_binary_scale:
12601 return "DW_AT_binary_scale";
12602 case DW_AT_decimal_scale:
12603 return "DW_AT_decimal_scale";
12604 case DW_AT_small:
12605 return "DW_AT_small";
12606 case DW_AT_decimal_sign:
12607 return "DW_AT_decimal_sign";
12608 case DW_AT_digit_count:
12609 return "DW_AT_digit_count";
12610 case DW_AT_picture_string:
12611 return "DW_AT_picture_string";
12612 case DW_AT_mutable:
12613 return "DW_AT_mutable";
12614 case DW_AT_threads_scaled:
12615 return "DW_AT_threads_scaled";
12616 case DW_AT_explicit:
12617 return "DW_AT_explicit";
12618 case DW_AT_object_pointer:
12619 return "DW_AT_object_pointer";
12620 case DW_AT_endianity:
12621 return "DW_AT_endianity";
12622 case DW_AT_elemental:
12623 return "DW_AT_elemental";
12624 case DW_AT_pure:
12625 return "DW_AT_pure";
12626 case DW_AT_recursive:
12627 return "DW_AT_recursive";
348e048f
DE
12628 /* DWARF 4 values. */
12629 case DW_AT_signature:
12630 return "DW_AT_signature";
31ef98ae
TT
12631 case DW_AT_linkage_name:
12632 return "DW_AT_linkage_name";
b7619582 12633 /* SGI/MIPS extensions. */
c764a876 12634#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12635 case DW_AT_MIPS_fde:
12636 return "DW_AT_MIPS_fde";
c764a876 12637#endif
c906108c
SS
12638 case DW_AT_MIPS_loop_begin:
12639 return "DW_AT_MIPS_loop_begin";
12640 case DW_AT_MIPS_tail_loop_begin:
12641 return "DW_AT_MIPS_tail_loop_begin";
12642 case DW_AT_MIPS_epilog_begin:
12643 return "DW_AT_MIPS_epilog_begin";
12644 case DW_AT_MIPS_loop_unroll_factor:
12645 return "DW_AT_MIPS_loop_unroll_factor";
12646 case DW_AT_MIPS_software_pipeline_depth:
12647 return "DW_AT_MIPS_software_pipeline_depth";
12648 case DW_AT_MIPS_linkage_name:
12649 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12650 case DW_AT_MIPS_stride:
12651 return "DW_AT_MIPS_stride";
12652 case DW_AT_MIPS_abstract_name:
12653 return "DW_AT_MIPS_abstract_name";
12654 case DW_AT_MIPS_clone_origin:
12655 return "DW_AT_MIPS_clone_origin";
12656 case DW_AT_MIPS_has_inlines:
12657 return "DW_AT_MIPS_has_inlines";
b7619582 12658 /* HP extensions. */
c764a876 12659#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12660 case DW_AT_HP_block_index:
12661 return "DW_AT_HP_block_index";
c764a876 12662#endif
b7619582
GF
12663 case DW_AT_HP_unmodifiable:
12664 return "DW_AT_HP_unmodifiable";
12665 case DW_AT_HP_actuals_stmt_list:
12666 return "DW_AT_HP_actuals_stmt_list";
12667 case DW_AT_HP_proc_per_section:
12668 return "DW_AT_HP_proc_per_section";
12669 case DW_AT_HP_raw_data_ptr:
12670 return "DW_AT_HP_raw_data_ptr";
12671 case DW_AT_HP_pass_by_reference:
12672 return "DW_AT_HP_pass_by_reference";
12673 case DW_AT_HP_opt_level:
12674 return "DW_AT_HP_opt_level";
12675 case DW_AT_HP_prof_version_id:
12676 return "DW_AT_HP_prof_version_id";
12677 case DW_AT_HP_opt_flags:
12678 return "DW_AT_HP_opt_flags";
12679 case DW_AT_HP_cold_region_low_pc:
12680 return "DW_AT_HP_cold_region_low_pc";
12681 case DW_AT_HP_cold_region_high_pc:
12682 return "DW_AT_HP_cold_region_high_pc";
12683 case DW_AT_HP_all_variables_modifiable:
12684 return "DW_AT_HP_all_variables_modifiable";
12685 case DW_AT_HP_linkage_name:
12686 return "DW_AT_HP_linkage_name";
12687 case DW_AT_HP_prof_flags:
12688 return "DW_AT_HP_prof_flags";
12689 /* GNU extensions. */
c906108c
SS
12690 case DW_AT_sf_names:
12691 return "DW_AT_sf_names";
12692 case DW_AT_src_info:
12693 return "DW_AT_src_info";
12694 case DW_AT_mac_info:
12695 return "DW_AT_mac_info";
12696 case DW_AT_src_coords:
12697 return "DW_AT_src_coords";
12698 case DW_AT_body_begin:
12699 return "DW_AT_body_begin";
12700 case DW_AT_body_end:
12701 return "DW_AT_body_end";
f5f8a009
EZ
12702 case DW_AT_GNU_vector:
12703 return "DW_AT_GNU_vector";
2de00c64
DE
12704 case DW_AT_GNU_odr_signature:
12705 return "DW_AT_GNU_odr_signature";
b7619582
GF
12706 /* VMS extensions. */
12707 case DW_AT_VMS_rtnbeg_pd_address:
12708 return "DW_AT_VMS_rtnbeg_pd_address";
12709 /* UPC extension. */
12710 case DW_AT_upc_threads_scaled:
12711 return "DW_AT_upc_threads_scaled";
12712 /* PGI (STMicroelectronics) extensions. */
12713 case DW_AT_PGI_lbase:
12714 return "DW_AT_PGI_lbase";
12715 case DW_AT_PGI_soffset:
12716 return "DW_AT_PGI_soffset";
12717 case DW_AT_PGI_lstride:
12718 return "DW_AT_PGI_lstride";
c906108c
SS
12719 default:
12720 return "DW_AT_<unknown>";
12721 }
12722}
12723
12724/* Convert a DWARF value form code into its string name. */
12725
12726static char *
aa1ee363 12727dwarf_form_name (unsigned form)
c906108c
SS
12728{
12729 switch (form)
12730 {
12731 case DW_FORM_addr:
12732 return "DW_FORM_addr";
12733 case DW_FORM_block2:
12734 return "DW_FORM_block2";
12735 case DW_FORM_block4:
12736 return "DW_FORM_block4";
12737 case DW_FORM_data2:
12738 return "DW_FORM_data2";
12739 case DW_FORM_data4:
12740 return "DW_FORM_data4";
12741 case DW_FORM_data8:
12742 return "DW_FORM_data8";
12743 case DW_FORM_string:
12744 return "DW_FORM_string";
12745 case DW_FORM_block:
12746 return "DW_FORM_block";
12747 case DW_FORM_block1:
12748 return "DW_FORM_block1";
12749 case DW_FORM_data1:
12750 return "DW_FORM_data1";
12751 case DW_FORM_flag:
12752 return "DW_FORM_flag";
12753 case DW_FORM_sdata:
12754 return "DW_FORM_sdata";
12755 case DW_FORM_strp:
12756 return "DW_FORM_strp";
12757 case DW_FORM_udata:
12758 return "DW_FORM_udata";
12759 case DW_FORM_ref_addr:
12760 return "DW_FORM_ref_addr";
12761 case DW_FORM_ref1:
12762 return "DW_FORM_ref1";
12763 case DW_FORM_ref2:
12764 return "DW_FORM_ref2";
12765 case DW_FORM_ref4:
12766 return "DW_FORM_ref4";
12767 case DW_FORM_ref8:
12768 return "DW_FORM_ref8";
12769 case DW_FORM_ref_udata:
12770 return "DW_FORM_ref_udata";
12771 case DW_FORM_indirect:
12772 return "DW_FORM_indirect";
348e048f
DE
12773 case DW_FORM_sec_offset:
12774 return "DW_FORM_sec_offset";
12775 case DW_FORM_exprloc:
12776 return "DW_FORM_exprloc";
12777 case DW_FORM_flag_present:
12778 return "DW_FORM_flag_present";
55f1336d
TT
12779 case DW_FORM_ref_sig8:
12780 return "DW_FORM_ref_sig8";
c906108c
SS
12781 default:
12782 return "DW_FORM_<unknown>";
12783 }
12784}
12785
12786/* Convert a DWARF stack opcode into its string name. */
12787
9eae7c52 12788const char *
b1bfef65 12789dwarf_stack_op_name (unsigned op)
c906108c
SS
12790{
12791 switch (op)
12792 {
12793 case DW_OP_addr:
12794 return "DW_OP_addr";
12795 case DW_OP_deref:
12796 return "DW_OP_deref";
12797 case DW_OP_const1u:
12798 return "DW_OP_const1u";
12799 case DW_OP_const1s:
12800 return "DW_OP_const1s";
12801 case DW_OP_const2u:
12802 return "DW_OP_const2u";
12803 case DW_OP_const2s:
12804 return "DW_OP_const2s";
12805 case DW_OP_const4u:
12806 return "DW_OP_const4u";
12807 case DW_OP_const4s:
12808 return "DW_OP_const4s";
12809 case DW_OP_const8u:
12810 return "DW_OP_const8u";
12811 case DW_OP_const8s:
12812 return "DW_OP_const8s";
12813 case DW_OP_constu:
12814 return "DW_OP_constu";
12815 case DW_OP_consts:
12816 return "DW_OP_consts";
12817 case DW_OP_dup:
12818 return "DW_OP_dup";
12819 case DW_OP_drop:
12820 return "DW_OP_drop";
12821 case DW_OP_over:
12822 return "DW_OP_over";
12823 case DW_OP_pick:
12824 return "DW_OP_pick";
12825 case DW_OP_swap:
12826 return "DW_OP_swap";
12827 case DW_OP_rot:
12828 return "DW_OP_rot";
12829 case DW_OP_xderef:
12830 return "DW_OP_xderef";
12831 case DW_OP_abs:
12832 return "DW_OP_abs";
12833 case DW_OP_and:
12834 return "DW_OP_and";
12835 case DW_OP_div:
12836 return "DW_OP_div";
12837 case DW_OP_minus:
12838 return "DW_OP_minus";
12839 case DW_OP_mod:
12840 return "DW_OP_mod";
12841 case DW_OP_mul:
12842 return "DW_OP_mul";
12843 case DW_OP_neg:
12844 return "DW_OP_neg";
12845 case DW_OP_not:
12846 return "DW_OP_not";
12847 case DW_OP_or:
12848 return "DW_OP_or";
12849 case DW_OP_plus:
12850 return "DW_OP_plus";
12851 case DW_OP_plus_uconst:
12852 return "DW_OP_plus_uconst";
12853 case DW_OP_shl:
12854 return "DW_OP_shl";
12855 case DW_OP_shr:
12856 return "DW_OP_shr";
12857 case DW_OP_shra:
12858 return "DW_OP_shra";
12859 case DW_OP_xor:
12860 return "DW_OP_xor";
12861 case DW_OP_bra:
12862 return "DW_OP_bra";
12863 case DW_OP_eq:
12864 return "DW_OP_eq";
12865 case DW_OP_ge:
12866 return "DW_OP_ge";
12867 case DW_OP_gt:
12868 return "DW_OP_gt";
12869 case DW_OP_le:
12870 return "DW_OP_le";
12871 case DW_OP_lt:
12872 return "DW_OP_lt";
12873 case DW_OP_ne:
12874 return "DW_OP_ne";
12875 case DW_OP_skip:
12876 return "DW_OP_skip";
12877 case DW_OP_lit0:
12878 return "DW_OP_lit0";
12879 case DW_OP_lit1:
12880 return "DW_OP_lit1";
12881 case DW_OP_lit2:
12882 return "DW_OP_lit2";
12883 case DW_OP_lit3:
12884 return "DW_OP_lit3";
12885 case DW_OP_lit4:
12886 return "DW_OP_lit4";
12887 case DW_OP_lit5:
12888 return "DW_OP_lit5";
12889 case DW_OP_lit6:
12890 return "DW_OP_lit6";
12891 case DW_OP_lit7:
12892 return "DW_OP_lit7";
12893 case DW_OP_lit8:
12894 return "DW_OP_lit8";
12895 case DW_OP_lit9:
12896 return "DW_OP_lit9";
12897 case DW_OP_lit10:
12898 return "DW_OP_lit10";
12899 case DW_OP_lit11:
12900 return "DW_OP_lit11";
12901 case DW_OP_lit12:
12902 return "DW_OP_lit12";
12903 case DW_OP_lit13:
12904 return "DW_OP_lit13";
12905 case DW_OP_lit14:
12906 return "DW_OP_lit14";
12907 case DW_OP_lit15:
12908 return "DW_OP_lit15";
12909 case DW_OP_lit16:
12910 return "DW_OP_lit16";
12911 case DW_OP_lit17:
12912 return "DW_OP_lit17";
12913 case DW_OP_lit18:
12914 return "DW_OP_lit18";
12915 case DW_OP_lit19:
12916 return "DW_OP_lit19";
12917 case DW_OP_lit20:
12918 return "DW_OP_lit20";
12919 case DW_OP_lit21:
12920 return "DW_OP_lit21";
12921 case DW_OP_lit22:
12922 return "DW_OP_lit22";
12923 case DW_OP_lit23:
12924 return "DW_OP_lit23";
12925 case DW_OP_lit24:
12926 return "DW_OP_lit24";
12927 case DW_OP_lit25:
12928 return "DW_OP_lit25";
12929 case DW_OP_lit26:
12930 return "DW_OP_lit26";
12931 case DW_OP_lit27:
12932 return "DW_OP_lit27";
12933 case DW_OP_lit28:
12934 return "DW_OP_lit28";
12935 case DW_OP_lit29:
12936 return "DW_OP_lit29";
12937 case DW_OP_lit30:
12938 return "DW_OP_lit30";
12939 case DW_OP_lit31:
12940 return "DW_OP_lit31";
12941 case DW_OP_reg0:
12942 return "DW_OP_reg0";
12943 case DW_OP_reg1:
12944 return "DW_OP_reg1";
12945 case DW_OP_reg2:
12946 return "DW_OP_reg2";
12947 case DW_OP_reg3:
12948 return "DW_OP_reg3";
12949 case DW_OP_reg4:
12950 return "DW_OP_reg4";
12951 case DW_OP_reg5:
12952 return "DW_OP_reg5";
12953 case DW_OP_reg6:
12954 return "DW_OP_reg6";
12955 case DW_OP_reg7:
12956 return "DW_OP_reg7";
12957 case DW_OP_reg8:
12958 return "DW_OP_reg8";
12959 case DW_OP_reg9:
12960 return "DW_OP_reg9";
12961 case DW_OP_reg10:
12962 return "DW_OP_reg10";
12963 case DW_OP_reg11:
12964 return "DW_OP_reg11";
12965 case DW_OP_reg12:
12966 return "DW_OP_reg12";
12967 case DW_OP_reg13:
12968 return "DW_OP_reg13";
12969 case DW_OP_reg14:
12970 return "DW_OP_reg14";
12971 case DW_OP_reg15:
12972 return "DW_OP_reg15";
12973 case DW_OP_reg16:
12974 return "DW_OP_reg16";
12975 case DW_OP_reg17:
12976 return "DW_OP_reg17";
12977 case DW_OP_reg18:
12978 return "DW_OP_reg18";
12979 case DW_OP_reg19:
12980 return "DW_OP_reg19";
12981 case DW_OP_reg20:
12982 return "DW_OP_reg20";
12983 case DW_OP_reg21:
12984 return "DW_OP_reg21";
12985 case DW_OP_reg22:
12986 return "DW_OP_reg22";
12987 case DW_OP_reg23:
12988 return "DW_OP_reg23";
12989 case DW_OP_reg24:
12990 return "DW_OP_reg24";
12991 case DW_OP_reg25:
12992 return "DW_OP_reg25";
12993 case DW_OP_reg26:
12994 return "DW_OP_reg26";
12995 case DW_OP_reg27:
12996 return "DW_OP_reg27";
12997 case DW_OP_reg28:
12998 return "DW_OP_reg28";
12999 case DW_OP_reg29:
13000 return "DW_OP_reg29";
13001 case DW_OP_reg30:
13002 return "DW_OP_reg30";
13003 case DW_OP_reg31:
13004 return "DW_OP_reg31";
13005 case DW_OP_breg0:
13006 return "DW_OP_breg0";
13007 case DW_OP_breg1:
13008 return "DW_OP_breg1";
13009 case DW_OP_breg2:
13010 return "DW_OP_breg2";
13011 case DW_OP_breg3:
13012 return "DW_OP_breg3";
13013 case DW_OP_breg4:
13014 return "DW_OP_breg4";
13015 case DW_OP_breg5:
13016 return "DW_OP_breg5";
13017 case DW_OP_breg6:
13018 return "DW_OP_breg6";
13019 case DW_OP_breg7:
13020 return "DW_OP_breg7";
13021 case DW_OP_breg8:
13022 return "DW_OP_breg8";
13023 case DW_OP_breg9:
13024 return "DW_OP_breg9";
13025 case DW_OP_breg10:
13026 return "DW_OP_breg10";
13027 case DW_OP_breg11:
13028 return "DW_OP_breg11";
13029 case DW_OP_breg12:
13030 return "DW_OP_breg12";
13031 case DW_OP_breg13:
13032 return "DW_OP_breg13";
13033 case DW_OP_breg14:
13034 return "DW_OP_breg14";
13035 case DW_OP_breg15:
13036 return "DW_OP_breg15";
13037 case DW_OP_breg16:
13038 return "DW_OP_breg16";
13039 case DW_OP_breg17:
13040 return "DW_OP_breg17";
13041 case DW_OP_breg18:
13042 return "DW_OP_breg18";
13043 case DW_OP_breg19:
13044 return "DW_OP_breg19";
13045 case DW_OP_breg20:
13046 return "DW_OP_breg20";
13047 case DW_OP_breg21:
13048 return "DW_OP_breg21";
13049 case DW_OP_breg22:
13050 return "DW_OP_breg22";
13051 case DW_OP_breg23:
13052 return "DW_OP_breg23";
13053 case DW_OP_breg24:
13054 return "DW_OP_breg24";
13055 case DW_OP_breg25:
13056 return "DW_OP_breg25";
13057 case DW_OP_breg26:
13058 return "DW_OP_breg26";
13059 case DW_OP_breg27:
13060 return "DW_OP_breg27";
13061 case DW_OP_breg28:
13062 return "DW_OP_breg28";
13063 case DW_OP_breg29:
13064 return "DW_OP_breg29";
13065 case DW_OP_breg30:
13066 return "DW_OP_breg30";
13067 case DW_OP_breg31:
13068 return "DW_OP_breg31";
13069 case DW_OP_regx:
13070 return "DW_OP_regx";
13071 case DW_OP_fbreg:
13072 return "DW_OP_fbreg";
13073 case DW_OP_bregx:
13074 return "DW_OP_bregx";
13075 case DW_OP_piece:
13076 return "DW_OP_piece";
13077 case DW_OP_deref_size:
13078 return "DW_OP_deref_size";
13079 case DW_OP_xderef_size:
13080 return "DW_OP_xderef_size";
13081 case DW_OP_nop:
13082 return "DW_OP_nop";
b7619582 13083 /* DWARF 3 extensions. */
ed348acc
EZ
13084 case DW_OP_push_object_address:
13085 return "DW_OP_push_object_address";
13086 case DW_OP_call2:
13087 return "DW_OP_call2";
13088 case DW_OP_call4:
13089 return "DW_OP_call4";
13090 case DW_OP_call_ref:
13091 return "DW_OP_call_ref";
b7619582
GF
13092 case DW_OP_form_tls_address:
13093 return "DW_OP_form_tls_address";
13094 case DW_OP_call_frame_cfa:
13095 return "DW_OP_call_frame_cfa";
13096 case DW_OP_bit_piece:
13097 return "DW_OP_bit_piece";
9eae7c52
TT
13098 /* DWARF 4 extensions. */
13099 case DW_OP_implicit_value:
13100 return "DW_OP_implicit_value";
13101 case DW_OP_stack_value:
13102 return "DW_OP_stack_value";
13103 /* GNU extensions. */
ed348acc
EZ
13104 case DW_OP_GNU_push_tls_address:
13105 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13106 case DW_OP_GNU_uninit:
13107 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13108 case DW_OP_GNU_implicit_pointer:
13109 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13110 case DW_OP_GNU_entry_value:
13111 return "DW_OP_GNU_entry_value";
13112 case DW_OP_GNU_const_type:
13113 return "DW_OP_GNU_const_type";
13114 case DW_OP_GNU_regval_type:
13115 return "DW_OP_GNU_regval_type";
13116 case DW_OP_GNU_deref_type:
13117 return "DW_OP_GNU_deref_type";
13118 case DW_OP_GNU_convert:
13119 return "DW_OP_GNU_convert";
13120 case DW_OP_GNU_reinterpret:
13121 return "DW_OP_GNU_reinterpret";
c906108c 13122 default:
b1bfef65 13123 return NULL;
c906108c
SS
13124 }
13125}
13126
13127static char *
fba45db2 13128dwarf_bool_name (unsigned mybool)
c906108c
SS
13129{
13130 if (mybool)
13131 return "TRUE";
13132 else
13133 return "FALSE";
13134}
13135
13136/* Convert a DWARF type code into its string name. */
13137
13138static char *
aa1ee363 13139dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13140{
13141 switch (enc)
13142 {
b7619582
GF
13143 case DW_ATE_void:
13144 return "DW_ATE_void";
c906108c
SS
13145 case DW_ATE_address:
13146 return "DW_ATE_address";
13147 case DW_ATE_boolean:
13148 return "DW_ATE_boolean";
13149 case DW_ATE_complex_float:
13150 return "DW_ATE_complex_float";
13151 case DW_ATE_float:
13152 return "DW_ATE_float";
13153 case DW_ATE_signed:
13154 return "DW_ATE_signed";
13155 case DW_ATE_signed_char:
13156 return "DW_ATE_signed_char";
13157 case DW_ATE_unsigned:
13158 return "DW_ATE_unsigned";
13159 case DW_ATE_unsigned_char:
13160 return "DW_ATE_unsigned_char";
b7619582 13161 /* DWARF 3. */
d9fa45fe
DC
13162 case DW_ATE_imaginary_float:
13163 return "DW_ATE_imaginary_float";
b7619582
GF
13164 case DW_ATE_packed_decimal:
13165 return "DW_ATE_packed_decimal";
13166 case DW_ATE_numeric_string:
13167 return "DW_ATE_numeric_string";
13168 case DW_ATE_edited:
13169 return "DW_ATE_edited";
13170 case DW_ATE_signed_fixed:
13171 return "DW_ATE_signed_fixed";
13172 case DW_ATE_unsigned_fixed:
13173 return "DW_ATE_unsigned_fixed";
13174 case DW_ATE_decimal_float:
13175 return "DW_ATE_decimal_float";
75079b2b
TT
13176 /* DWARF 4. */
13177 case DW_ATE_UTF:
13178 return "DW_ATE_UTF";
b7619582
GF
13179 /* HP extensions. */
13180 case DW_ATE_HP_float80:
13181 return "DW_ATE_HP_float80";
13182 case DW_ATE_HP_complex_float80:
13183 return "DW_ATE_HP_complex_float80";
13184 case DW_ATE_HP_float128:
13185 return "DW_ATE_HP_float128";
13186 case DW_ATE_HP_complex_float128:
13187 return "DW_ATE_HP_complex_float128";
13188 case DW_ATE_HP_floathpintel:
13189 return "DW_ATE_HP_floathpintel";
13190 case DW_ATE_HP_imaginary_float80:
13191 return "DW_ATE_HP_imaginary_float80";
13192 case DW_ATE_HP_imaginary_float128:
13193 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13194 default:
13195 return "DW_ATE_<unknown>";
13196 }
13197}
13198
0963b4bd 13199/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13200
13201#if 0
13202static char *
aa1ee363 13203dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13204{
13205 switch (cfi_opc)
13206 {
13207 case DW_CFA_advance_loc:
13208 return "DW_CFA_advance_loc";
13209 case DW_CFA_offset:
13210 return "DW_CFA_offset";
13211 case DW_CFA_restore:
13212 return "DW_CFA_restore";
13213 case DW_CFA_nop:
13214 return "DW_CFA_nop";
13215 case DW_CFA_set_loc:
13216 return "DW_CFA_set_loc";
13217 case DW_CFA_advance_loc1:
13218 return "DW_CFA_advance_loc1";
13219 case DW_CFA_advance_loc2:
13220 return "DW_CFA_advance_loc2";
13221 case DW_CFA_advance_loc4:
13222 return "DW_CFA_advance_loc4";
13223 case DW_CFA_offset_extended:
13224 return "DW_CFA_offset_extended";
13225 case DW_CFA_restore_extended:
13226 return "DW_CFA_restore_extended";
13227 case DW_CFA_undefined:
13228 return "DW_CFA_undefined";
13229 case DW_CFA_same_value:
13230 return "DW_CFA_same_value";
13231 case DW_CFA_register:
13232 return "DW_CFA_register";
13233 case DW_CFA_remember_state:
13234 return "DW_CFA_remember_state";
13235 case DW_CFA_restore_state:
13236 return "DW_CFA_restore_state";
13237 case DW_CFA_def_cfa:
13238 return "DW_CFA_def_cfa";
13239 case DW_CFA_def_cfa_register:
13240 return "DW_CFA_def_cfa_register";
13241 case DW_CFA_def_cfa_offset:
13242 return "DW_CFA_def_cfa_offset";
b7619582 13243 /* DWARF 3. */
985cb1a3
JM
13244 case DW_CFA_def_cfa_expression:
13245 return "DW_CFA_def_cfa_expression";
13246 case DW_CFA_expression:
13247 return "DW_CFA_expression";
13248 case DW_CFA_offset_extended_sf:
13249 return "DW_CFA_offset_extended_sf";
13250 case DW_CFA_def_cfa_sf:
13251 return "DW_CFA_def_cfa_sf";
13252 case DW_CFA_def_cfa_offset_sf:
13253 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13254 case DW_CFA_val_offset:
13255 return "DW_CFA_val_offset";
13256 case DW_CFA_val_offset_sf:
13257 return "DW_CFA_val_offset_sf";
13258 case DW_CFA_val_expression:
13259 return "DW_CFA_val_expression";
13260 /* SGI/MIPS specific. */
c906108c
SS
13261 case DW_CFA_MIPS_advance_loc8:
13262 return "DW_CFA_MIPS_advance_loc8";
b7619582 13263 /* GNU extensions. */
985cb1a3
JM
13264 case DW_CFA_GNU_window_save:
13265 return "DW_CFA_GNU_window_save";
13266 case DW_CFA_GNU_args_size:
13267 return "DW_CFA_GNU_args_size";
13268 case DW_CFA_GNU_negative_offset_extended:
13269 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13270 default:
13271 return "DW_CFA_<unknown>";
13272 }
13273}
13274#endif
13275
f9aca02d 13276static void
d97bc12b 13277dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13278{
13279 unsigned int i;
13280
d97bc12b
DE
13281 print_spaces (indent, f);
13282 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13283 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13284
13285 if (die->parent != NULL)
13286 {
13287 print_spaces (indent, f);
13288 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13289 die->parent->offset);
13290 }
13291
13292 print_spaces (indent, f);
13293 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13294 dwarf_bool_name (die->child != NULL));
c906108c 13295
d97bc12b
DE
13296 print_spaces (indent, f);
13297 fprintf_unfiltered (f, " attributes:\n");
13298
c906108c
SS
13299 for (i = 0; i < die->num_attrs; ++i)
13300 {
d97bc12b
DE
13301 print_spaces (indent, f);
13302 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13303 dwarf_attr_name (die->attrs[i].name),
13304 dwarf_form_name (die->attrs[i].form));
d97bc12b 13305
c906108c
SS
13306 switch (die->attrs[i].form)
13307 {
13308 case DW_FORM_ref_addr:
13309 case DW_FORM_addr:
d97bc12b 13310 fprintf_unfiltered (f, "address: ");
5af949e3 13311 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13312 break;
13313 case DW_FORM_block2:
13314 case DW_FORM_block4:
13315 case DW_FORM_block:
13316 case DW_FORM_block1:
3e43a32a
MS
13317 fprintf_unfiltered (f, "block: size %d",
13318 DW_BLOCK (&die->attrs[i])->size);
c906108c 13319 break;
2dc7f7b3
TT
13320 case DW_FORM_exprloc:
13321 fprintf_unfiltered (f, "expression: size %u",
13322 DW_BLOCK (&die->attrs[i])->size);
13323 break;
10b3939b
DJ
13324 case DW_FORM_ref1:
13325 case DW_FORM_ref2:
13326 case DW_FORM_ref4:
d97bc12b 13327 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13328 (long) (DW_ADDR (&die->attrs[i])));
13329 break;
c906108c
SS
13330 case DW_FORM_data1:
13331 case DW_FORM_data2:
13332 case DW_FORM_data4:
ce5d95e1 13333 case DW_FORM_data8:
c906108c
SS
13334 case DW_FORM_udata:
13335 case DW_FORM_sdata:
43bbcdc2
PH
13336 fprintf_unfiltered (f, "constant: %s",
13337 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13338 break;
2dc7f7b3
TT
13339 case DW_FORM_sec_offset:
13340 fprintf_unfiltered (f, "section offset: %s",
13341 pulongest (DW_UNSND (&die->attrs[i])));
13342 break;
55f1336d 13343 case DW_FORM_ref_sig8:
348e048f
DE
13344 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13345 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b3c8eb43 13346 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
348e048f
DE
13347 else
13348 fprintf_unfiltered (f, "signatured type, offset: unknown");
13349 break;
c906108c 13350 case DW_FORM_string:
4bdf3d34 13351 case DW_FORM_strp:
8285870a 13352 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13353 DW_STRING (&die->attrs[i])
8285870a
JK
13354 ? DW_STRING (&die->attrs[i]) : "",
13355 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13356 break;
13357 case DW_FORM_flag:
13358 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13359 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13360 else
d97bc12b 13361 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13362 break;
2dc7f7b3
TT
13363 case DW_FORM_flag_present:
13364 fprintf_unfiltered (f, "flag: TRUE");
13365 break;
a8329558 13366 case DW_FORM_indirect:
0963b4bd
MS
13367 /* The reader will have reduced the indirect form to
13368 the "base form" so this form should not occur. */
3e43a32a
MS
13369 fprintf_unfiltered (f,
13370 "unexpected attribute form: DW_FORM_indirect");
a8329558 13371 break;
c906108c 13372 default:
d97bc12b 13373 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13374 die->attrs[i].form);
d97bc12b 13375 break;
c906108c 13376 }
d97bc12b 13377 fprintf_unfiltered (f, "\n");
c906108c
SS
13378 }
13379}
13380
f9aca02d 13381static void
d97bc12b 13382dump_die_for_error (struct die_info *die)
c906108c 13383{
d97bc12b
DE
13384 dump_die_shallow (gdb_stderr, 0, die);
13385}
13386
13387static void
13388dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13389{
13390 int indent = level * 4;
13391
13392 gdb_assert (die != NULL);
13393
13394 if (level >= max_level)
13395 return;
13396
13397 dump_die_shallow (f, indent, die);
13398
13399 if (die->child != NULL)
c906108c 13400 {
d97bc12b
DE
13401 print_spaces (indent, f);
13402 fprintf_unfiltered (f, " Children:");
13403 if (level + 1 < max_level)
13404 {
13405 fprintf_unfiltered (f, "\n");
13406 dump_die_1 (f, level + 1, max_level, die->child);
13407 }
13408 else
13409 {
3e43a32a
MS
13410 fprintf_unfiltered (f,
13411 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13412 }
13413 }
13414
13415 if (die->sibling != NULL && level > 0)
13416 {
13417 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13418 }
13419}
13420
d97bc12b
DE
13421/* This is called from the pdie macro in gdbinit.in.
13422 It's not static so gcc will keep a copy callable from gdb. */
13423
13424void
13425dump_die (struct die_info *die, int max_level)
13426{
13427 dump_die_1 (gdb_stdlog, 0, max_level, die);
13428}
13429
f9aca02d 13430static void
51545339 13431store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13432{
51545339 13433 void **slot;
c906108c 13434
51545339
DJ
13435 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13436
13437 *slot = die;
c906108c
SS
13438}
13439
93311388
DE
13440static int
13441is_ref_attr (struct attribute *attr)
c906108c 13442{
c906108c
SS
13443 switch (attr->form)
13444 {
13445 case DW_FORM_ref_addr:
c906108c
SS
13446 case DW_FORM_ref1:
13447 case DW_FORM_ref2:
13448 case DW_FORM_ref4:
613e1657 13449 case DW_FORM_ref8:
c906108c 13450 case DW_FORM_ref_udata:
93311388 13451 return 1;
c906108c 13452 default:
93311388 13453 return 0;
c906108c 13454 }
93311388
DE
13455}
13456
13457static unsigned int
13458dwarf2_get_ref_die_offset (struct attribute *attr)
13459{
13460 if (is_ref_attr (attr))
13461 return DW_ADDR (attr);
13462
13463 complaint (&symfile_complaints,
13464 _("unsupported die ref attribute form: '%s'"),
13465 dwarf_form_name (attr->form));
13466 return 0;
c906108c
SS
13467}
13468
43bbcdc2
PH
13469/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13470 * the value held by the attribute is not constant. */
a02abb62 13471
43bbcdc2 13472static LONGEST
a02abb62
JB
13473dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13474{
13475 if (attr->form == DW_FORM_sdata)
13476 return DW_SND (attr);
13477 else if (attr->form == DW_FORM_udata
13478 || attr->form == DW_FORM_data1
13479 || attr->form == DW_FORM_data2
13480 || attr->form == DW_FORM_data4
13481 || attr->form == DW_FORM_data8)
13482 return DW_UNSND (attr);
13483 else
13484 {
3e43a32a
MS
13485 complaint (&symfile_complaints,
13486 _("Attribute value is not a constant (%s)"),
a02abb62
JB
13487 dwarf_form_name (attr->form));
13488 return default_value;
13489 }
13490}
13491
03dd20cc 13492/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
13493 unit and add it to our queue.
13494 The result is non-zero if PER_CU was queued, otherwise the result is zero
13495 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 13496
348e048f 13497static int
03dd20cc
DJ
13498maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13499 struct dwarf2_per_cu_data *per_cu)
13500{
98bfdba5
PA
13501 /* We may arrive here during partial symbol reading, if we need full
13502 DIEs to process an unusual case (e.g. template arguments). Do
13503 not queue PER_CU, just tell our caller to load its DIEs. */
13504 if (dwarf2_per_objfile->reading_partial_symbols)
13505 {
13506 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13507 return 1;
13508 return 0;
13509 }
13510
03dd20cc
DJ
13511 /* Mark the dependence relation so that we don't flush PER_CU
13512 too early. */
13513 dwarf2_add_dependence (this_cu, per_cu);
13514
13515 /* If it's already on the queue, we have nothing to do. */
13516 if (per_cu->queued)
348e048f 13517 return 0;
03dd20cc
DJ
13518
13519 /* If the compilation unit is already loaded, just mark it as
13520 used. */
13521 if (per_cu->cu != NULL)
13522 {
13523 per_cu->cu->last_used = 0;
348e048f 13524 return 0;
03dd20cc
DJ
13525 }
13526
13527 /* Add it to the queue. */
13528 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
13529
13530 return 1;
13531}
13532
13533/* Follow reference or signature attribute ATTR of SRC_DIE.
13534 On entry *REF_CU is the CU of SRC_DIE.
13535 On exit *REF_CU is the CU of the result. */
13536
13537static struct die_info *
13538follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13539 struct dwarf2_cu **ref_cu)
13540{
13541 struct die_info *die;
13542
13543 if (is_ref_attr (attr))
13544 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 13545 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
13546 die = follow_die_sig (src_die, attr, ref_cu);
13547 else
13548 {
13549 dump_die_for_error (src_die);
13550 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13551 (*ref_cu)->objfile->name);
13552 }
13553
13554 return die;
03dd20cc
DJ
13555}
13556
5c631832 13557/* Follow reference OFFSET.
673bfd45
DE
13558 On entry *REF_CU is the CU of the source die referencing OFFSET.
13559 On exit *REF_CU is the CU of the result.
13560 Returns NULL if OFFSET is invalid. */
f504f079 13561
f9aca02d 13562static struct die_info *
5c631832 13563follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13564{
10b3939b 13565 struct die_info temp_die;
f2f0e013 13566 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13567
348e048f
DE
13568 gdb_assert (cu->per_cu != NULL);
13569
98bfdba5
PA
13570 target_cu = cu;
13571
348e048f
DE
13572 if (cu->per_cu->from_debug_types)
13573 {
13574 /* .debug_types CUs cannot reference anything outside their CU.
13575 If they need to, they have to reference a signatured type via
55f1336d 13576 DW_FORM_ref_sig8. */
348e048f 13577 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13578 return NULL;
348e048f
DE
13579 }
13580 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13581 {
13582 struct dwarf2_per_cu_data *per_cu;
9a619af0 13583
45452591 13584 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13585
13586 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13587 if (maybe_queue_comp_unit (cu, per_cu))
13588 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13589
10b3939b
DJ
13590 target_cu = per_cu->cu;
13591 }
98bfdba5
PA
13592 else if (cu->dies == NULL)
13593 {
13594 /* We're loading full DIEs during partial symbol reading. */
13595 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13596 load_full_comp_unit (cu->per_cu, cu->objfile);
13597 }
c906108c 13598
f2f0e013 13599 *ref_cu = target_cu;
51545339 13600 temp_die.offset = offset;
5c631832
JK
13601 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13602}
10b3939b 13603
5c631832
JK
13604/* Follow reference attribute ATTR of SRC_DIE.
13605 On entry *REF_CU is the CU of SRC_DIE.
13606 On exit *REF_CU is the CU of the result. */
13607
13608static struct die_info *
13609follow_die_ref (struct die_info *src_die, struct attribute *attr,
13610 struct dwarf2_cu **ref_cu)
13611{
13612 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13613 struct dwarf2_cu *cu = *ref_cu;
13614 struct die_info *die;
13615
13616 die = follow_die_offset (offset, ref_cu);
13617 if (!die)
13618 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13619 "at 0x%x [in module %s]"),
13620 offset, src_die->offset, cu->objfile->name);
348e048f 13621
5c631832
JK
13622 return die;
13623}
13624
13625/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13626 value is intended for DW_OP_call*. */
13627
13628struct dwarf2_locexpr_baton
13629dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
13630 struct dwarf2_per_cu_data *per_cu,
13631 CORE_ADDR (*get_frame_pc) (void *baton),
13632 void *baton)
5c631832
JK
13633{
13634 struct dwarf2_cu *cu = per_cu->cu;
13635 struct die_info *die;
13636 struct attribute *attr;
13637 struct dwarf2_locexpr_baton retval;
13638
8cf6f0b1
TT
13639 dw2_setup (per_cu->objfile);
13640
5c631832
JK
13641 die = follow_die_offset (offset, &cu);
13642 if (!die)
13643 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13644 offset, per_cu->cu->objfile->name);
13645
13646 attr = dwarf2_attr (die, DW_AT_location, cu);
13647 if (!attr)
13648 {
13649 /* DWARF: "If there is no such attribute, then there is no effect.". */
13650
13651 retval.data = NULL;
13652 retval.size = 0;
13653 }
8cf6f0b1
TT
13654 else if (attr_form_is_section_offset (attr))
13655 {
13656 struct dwarf2_loclist_baton loclist_baton;
13657 CORE_ADDR pc = (*get_frame_pc) (baton);
13658 size_t size;
13659
13660 fill_in_loclist_baton (cu, &loclist_baton, attr);
13661
13662 retval.data = dwarf2_find_location_expression (&loclist_baton,
13663 &size, pc);
13664 retval.size = size;
13665 }
5c631832
JK
13666 else
13667 {
13668 if (!attr_form_is_block (attr))
13669 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13670 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13671 offset, per_cu->cu->objfile->name);
13672
13673 retval.data = DW_BLOCK (attr)->data;
13674 retval.size = DW_BLOCK (attr)->size;
13675 }
13676 retval.per_cu = cu->per_cu;
13677 return retval;
348e048f
DE
13678}
13679
8a9b8146
TT
13680/* Return the type of the DIE at DIE_OFFSET in the CU named by
13681 PER_CU. */
13682
13683struct type *
13684dwarf2_get_die_type (unsigned int die_offset,
13685 struct dwarf2_per_cu_data *per_cu)
13686{
8a9b8146 13687 dw2_setup (per_cu->objfile);
9ff3b74f 13688 return get_die_type_at_offset (die_offset, per_cu);
8a9b8146
TT
13689}
13690
348e048f
DE
13691/* Follow the signature attribute ATTR in SRC_DIE.
13692 On entry *REF_CU is the CU of SRC_DIE.
13693 On exit *REF_CU is the CU of the result. */
13694
13695static struct die_info *
13696follow_die_sig (struct die_info *src_die, struct attribute *attr,
13697 struct dwarf2_cu **ref_cu)
13698{
13699 struct objfile *objfile = (*ref_cu)->objfile;
13700 struct die_info temp_die;
13701 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13702 struct dwarf2_cu *sig_cu;
13703 struct die_info *die;
13704
13705 /* sig_type will be NULL if the signatured type is missing from
13706 the debug info. */
13707 if (sig_type == NULL)
13708 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13709 "at 0x%x [in module %s]"),
13710 src_die->offset, objfile->name);
13711
13712 /* If necessary, add it to the queue and load its DIEs. */
13713
13714 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13715 read_signatured_type (objfile, sig_type);
13716
13717 gdb_assert (sig_type->per_cu.cu != NULL);
13718
13719 sig_cu = sig_type->per_cu.cu;
13720 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13721 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13722 if (die)
13723 {
13724 *ref_cu = sig_cu;
13725 return die;
13726 }
13727
3e43a32a
MS
13728 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13729 "from DIE at 0x%x [in module %s]"),
348e048f
DE
13730 sig_type->type_offset, src_die->offset, objfile->name);
13731}
13732
13733/* Given an offset of a signatured type, return its signatured_type. */
13734
13735static struct signatured_type *
13736lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13737{
13738 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13739 unsigned int length, initial_length_size;
13740 unsigned int sig_offset;
13741 struct signatured_type find_entry, *type_sig;
13742
13743 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13744 sig_offset = (initial_length_size
13745 + 2 /*version*/
13746 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13747 + 1 /*address_size*/);
13748 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13749 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13750
13751 /* This is only used to lookup previously recorded types.
13752 If we didn't find it, it's our bug. */
13753 gdb_assert (type_sig != NULL);
b3c8eb43 13754 gdb_assert (offset == type_sig->per_cu.offset);
348e048f
DE
13755
13756 return type_sig;
13757}
13758
13759/* Read in signatured type at OFFSET and build its CU and die(s). */
13760
13761static void
13762read_signatured_type_at_offset (struct objfile *objfile,
13763 unsigned int offset)
13764{
13765 struct signatured_type *type_sig;
13766
be391dca
TT
13767 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13768
348e048f
DE
13769 /* We have the section offset, but we need the signature to do the
13770 hash table lookup. */
13771 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13772
13773 gdb_assert (type_sig->per_cu.cu == NULL);
13774
13775 read_signatured_type (objfile, type_sig);
13776
13777 gdb_assert (type_sig->per_cu.cu != NULL);
13778}
13779
13780/* Read in a signatured type and build its CU and DIEs. */
13781
13782static void
13783read_signatured_type (struct objfile *objfile,
13784 struct signatured_type *type_sig)
13785{
1fd400ff 13786 gdb_byte *types_ptr;
348e048f
DE
13787 struct die_reader_specs reader_specs;
13788 struct dwarf2_cu *cu;
13789 ULONGEST signature;
13790 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13791
1fd400ff 13792 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
b3c8eb43 13793 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->per_cu.offset;
1fd400ff 13794
348e048f
DE
13795 gdb_assert (type_sig->per_cu.cu == NULL);
13796
9816fde3
JK
13797 cu = xmalloc (sizeof (*cu));
13798 init_one_comp_unit (cu, objfile);
13799
348e048f
DE
13800 type_sig->per_cu.cu = cu;
13801 cu->per_cu = &type_sig->per_cu;
13802
13803 /* If an error occurs while loading, release our storage. */
13804 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13805
13806 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13807 types_ptr, objfile->obfd);
13808 gdb_assert (signature == type_sig->signature);
13809
13810 cu->die_hash
13811 = htab_create_alloc_ex (cu->header.length / 12,
13812 die_hash,
13813 die_eq,
13814 NULL,
13815 &cu->comp_unit_obstack,
13816 hashtab_obstack_allocate,
13817 dummy_obstack_deallocate);
13818
13819 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13820 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13821
13822 init_cu_die_reader (&reader_specs, cu);
13823
13824 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13825 NULL /*parent*/);
13826
13827 /* We try not to read any attributes in this function, because not
13828 all objfiles needed for references have been loaded yet, and symbol
13829 table processing isn't initialized. But we have to set the CU language,
13830 or we won't be able to build types correctly. */
9816fde3 13831 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
13832
13833 do_cleanups (back_to);
13834
13835 /* We've successfully allocated this compilation unit. Let our caller
13836 clean it up when finished with it. */
13837 discard_cleanups (free_cu_cleanup);
13838
13839 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13840 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13841}
13842
c906108c
SS
13843/* Decode simple location descriptions.
13844 Given a pointer to a dwarf block that defines a location, compute
13845 the location and return the value.
13846
4cecd739
DJ
13847 NOTE drow/2003-11-18: This function is called in two situations
13848 now: for the address of static or global variables (partial symbols
13849 only) and for offsets into structures which are expected to be
13850 (more or less) constant. The partial symbol case should go away,
13851 and only the constant case should remain. That will let this
13852 function complain more accurately. A few special modes are allowed
13853 without complaint for global variables (for instance, global
13854 register values and thread-local values).
c906108c
SS
13855
13856 A location description containing no operations indicates that the
4cecd739 13857 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13858 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13859 callers will only want a very basic result and this can become a
13860 complaint.
c906108c 13861
d53d4ac5 13862 Note that stack[0] is unused except as a default error return. */
c906108c
SS
13863
13864static CORE_ADDR
e7c27a73 13865decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13866{
e7c27a73 13867 struct objfile *objfile = cu->objfile;
c906108c
SS
13868 int i;
13869 int size = blk->size;
fe1b8b76 13870 gdb_byte *data = blk->data;
c906108c
SS
13871 CORE_ADDR stack[64];
13872 int stacki;
13873 unsigned int bytes_read, unsnd;
fe1b8b76 13874 gdb_byte op;
c906108c
SS
13875
13876 i = 0;
13877 stacki = 0;
13878 stack[stacki] = 0;
d53d4ac5 13879 stack[++stacki] = 0;
c906108c
SS
13880
13881 while (i < size)
13882 {
c906108c
SS
13883 op = data[i++];
13884 switch (op)
13885 {
f1bea926
JM
13886 case DW_OP_lit0:
13887 case DW_OP_lit1:
13888 case DW_OP_lit2:
13889 case DW_OP_lit3:
13890 case DW_OP_lit4:
13891 case DW_OP_lit5:
13892 case DW_OP_lit6:
13893 case DW_OP_lit7:
13894 case DW_OP_lit8:
13895 case DW_OP_lit9:
13896 case DW_OP_lit10:
13897 case DW_OP_lit11:
13898 case DW_OP_lit12:
13899 case DW_OP_lit13:
13900 case DW_OP_lit14:
13901 case DW_OP_lit15:
13902 case DW_OP_lit16:
13903 case DW_OP_lit17:
13904 case DW_OP_lit18:
13905 case DW_OP_lit19:
13906 case DW_OP_lit20:
13907 case DW_OP_lit21:
13908 case DW_OP_lit22:
13909 case DW_OP_lit23:
13910 case DW_OP_lit24:
13911 case DW_OP_lit25:
13912 case DW_OP_lit26:
13913 case DW_OP_lit27:
13914 case DW_OP_lit28:
13915 case DW_OP_lit29:
13916 case DW_OP_lit30:
13917 case DW_OP_lit31:
13918 stack[++stacki] = op - DW_OP_lit0;
13919 break;
13920
c906108c
SS
13921 case DW_OP_reg0:
13922 case DW_OP_reg1:
13923 case DW_OP_reg2:
13924 case DW_OP_reg3:
13925 case DW_OP_reg4:
13926 case DW_OP_reg5:
13927 case DW_OP_reg6:
13928 case DW_OP_reg7:
13929 case DW_OP_reg8:
13930 case DW_OP_reg9:
13931 case DW_OP_reg10:
13932 case DW_OP_reg11:
13933 case DW_OP_reg12:
13934 case DW_OP_reg13:
13935 case DW_OP_reg14:
13936 case DW_OP_reg15:
13937 case DW_OP_reg16:
13938 case DW_OP_reg17:
13939 case DW_OP_reg18:
13940 case DW_OP_reg19:
13941 case DW_OP_reg20:
13942 case DW_OP_reg21:
13943 case DW_OP_reg22:
13944 case DW_OP_reg23:
13945 case DW_OP_reg24:
13946 case DW_OP_reg25:
13947 case DW_OP_reg26:
13948 case DW_OP_reg27:
13949 case DW_OP_reg28:
13950 case DW_OP_reg29:
13951 case DW_OP_reg30:
13952 case DW_OP_reg31:
c906108c 13953 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13954 if (i < size)
13955 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13956 break;
13957
13958 case DW_OP_regx:
c906108c
SS
13959 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13960 i += bytes_read;
c906108c 13961 stack[++stacki] = unsnd;
4cecd739
DJ
13962 if (i < size)
13963 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13964 break;
13965
13966 case DW_OP_addr:
107d2387 13967 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13968 cu, &bytes_read);
107d2387 13969 i += bytes_read;
c906108c
SS
13970 break;
13971
13972 case DW_OP_const1u:
13973 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13974 i += 1;
13975 break;
13976
13977 case DW_OP_const1s:
13978 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13979 i += 1;
13980 break;
13981
13982 case DW_OP_const2u:
13983 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13984 i += 2;
13985 break;
13986
13987 case DW_OP_const2s:
13988 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13989 i += 2;
13990 break;
13991
13992 case DW_OP_const4u:
13993 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13994 i += 4;
13995 break;
13996
13997 case DW_OP_const4s:
13998 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13999 i += 4;
14000 break;
14001
14002 case DW_OP_constu:
14003 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 14004 &bytes_read);
c906108c
SS
14005 i += bytes_read;
14006 break;
14007
14008 case DW_OP_consts:
14009 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14010 i += bytes_read;
14011 break;
14012
f1bea926
JM
14013 case DW_OP_dup:
14014 stack[stacki + 1] = stack[stacki];
14015 stacki++;
14016 break;
14017
c906108c
SS
14018 case DW_OP_plus:
14019 stack[stacki - 1] += stack[stacki];
14020 stacki--;
14021 break;
14022
14023 case DW_OP_plus_uconst:
3e43a32a
MS
14024 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14025 &bytes_read);
c906108c
SS
14026 i += bytes_read;
14027 break;
14028
14029 case DW_OP_minus:
f1bea926 14030 stack[stacki - 1] -= stack[stacki];
c906108c
SS
14031 stacki--;
14032 break;
14033
7a292a7a 14034 case DW_OP_deref:
7a292a7a 14035 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
14036 this using GDB's address_class enum. This is valid for partial
14037 global symbols, although the variable's address will be bogus
14038 in the psymtab. */
7a292a7a 14039 if (i < size)
4d3c2250 14040 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
14041 break;
14042
9d774e44 14043 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
14044 /* The top of the stack has the offset from the beginning
14045 of the thread control block at which the variable is located. */
14046 /* Nothing should follow this operator, so the top of stack would
14047 be returned. */
4cecd739
DJ
14048 /* This is valid for partial global symbols, but the variable's
14049 address will be bogus in the psymtab. */
9d774e44 14050 if (i < size)
4d3c2250 14051 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
14052 break;
14053
42be36b3
CT
14054 case DW_OP_GNU_uninit:
14055 break;
14056
c906108c 14057 default:
b1bfef65
TT
14058 {
14059 const char *name = dwarf_stack_op_name (op);
14060
14061 if (name)
14062 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14063 name);
14064 else
14065 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14066 op);
14067 }
14068
c906108c
SS
14069 return (stack[stacki]);
14070 }
d53d4ac5
TT
14071
14072 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14073 outside of the allocated space. Also enforce minimum>0. */
14074 if (stacki >= ARRAY_SIZE (stack) - 1)
14075 {
14076 complaint (&symfile_complaints,
14077 _("location description stack overflow"));
14078 return 0;
14079 }
14080
14081 if (stacki <= 0)
14082 {
14083 complaint (&symfile_complaints,
14084 _("location description stack underflow"));
14085 return 0;
14086 }
c906108c
SS
14087 }
14088 return (stack[stacki]);
14089}
14090
14091/* memory allocation interface */
14092
c906108c 14093static struct dwarf_block *
7b5a2f43 14094dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14095{
14096 struct dwarf_block *blk;
14097
14098 blk = (struct dwarf_block *)
7b5a2f43 14099 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14100 return (blk);
14101}
14102
14103static struct abbrev_info *
f3dd6933 14104dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14105{
14106 struct abbrev_info *abbrev;
14107
f3dd6933
DJ
14108 abbrev = (struct abbrev_info *)
14109 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14110 memset (abbrev, 0, sizeof (struct abbrev_info));
14111 return (abbrev);
14112}
14113
14114static struct die_info *
b60c80d6 14115dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14116{
14117 struct die_info *die;
b60c80d6
DJ
14118 size_t size = sizeof (struct die_info);
14119
14120 if (num_attrs > 1)
14121 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14122
b60c80d6 14123 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14124 memset (die, 0, sizeof (struct die_info));
14125 return (die);
14126}
2e276125
JB
14127
14128\f
14129/* Macro support. */
14130
2e276125
JB
14131/* Return the full name of file number I in *LH's file name table.
14132 Use COMP_DIR as the name of the current directory of the
14133 compilation. The result is allocated using xmalloc; the caller is
14134 responsible for freeing it. */
14135static char *
14136file_full_name (int file, struct line_header *lh, const char *comp_dir)
14137{
6a83a1e6
EZ
14138 /* Is the file number a valid index into the line header's file name
14139 table? Remember that file numbers start with one, not zero. */
14140 if (1 <= file && file <= lh->num_file_names)
14141 {
14142 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14143
6a83a1e6
EZ
14144 if (IS_ABSOLUTE_PATH (fe->name))
14145 return xstrdup (fe->name);
14146 else
14147 {
14148 const char *dir;
14149 int dir_len;
14150 char *full_name;
14151
14152 if (fe->dir_index)
14153 dir = lh->include_dirs[fe->dir_index - 1];
14154 else
14155 dir = comp_dir;
14156
14157 if (dir)
14158 {
14159 dir_len = strlen (dir);
14160 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14161 strcpy (full_name, dir);
14162 full_name[dir_len] = '/';
14163 strcpy (full_name + dir_len + 1, fe->name);
14164 return full_name;
14165 }
14166 else
14167 return xstrdup (fe->name);
14168 }
14169 }
2e276125
JB
14170 else
14171 {
6a83a1e6
EZ
14172 /* The compiler produced a bogus file number. We can at least
14173 record the macro definitions made in the file, even if we
14174 won't be able to find the file by name. */
14175 char fake_name[80];
9a619af0 14176
6a83a1e6 14177 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14178
6e70227d 14179 complaint (&symfile_complaints,
6a83a1e6
EZ
14180 _("bad file number in macro information (%d)"),
14181 file);
2e276125 14182
6a83a1e6 14183 return xstrdup (fake_name);
2e276125
JB
14184 }
14185}
14186
14187
14188static struct macro_source_file *
14189macro_start_file (int file, int line,
14190 struct macro_source_file *current_file,
14191 const char *comp_dir,
14192 struct line_header *lh, struct objfile *objfile)
14193{
14194 /* The full name of this source file. */
14195 char *full_name = file_full_name (file, lh, comp_dir);
14196
14197 /* We don't create a macro table for this compilation unit
14198 at all until we actually get a filename. */
14199 if (! pending_macros)
4a146b47 14200 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14201 objfile->macro_cache);
2e276125
JB
14202
14203 if (! current_file)
14204 /* If we have no current file, then this must be the start_file
14205 directive for the compilation unit's main source file. */
14206 current_file = macro_set_main (pending_macros, full_name);
14207 else
14208 current_file = macro_include (current_file, line, full_name);
14209
14210 xfree (full_name);
6e70227d 14211
2e276125
JB
14212 return current_file;
14213}
14214
14215
14216/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14217 followed by a null byte. */
14218static char *
14219copy_string (const char *buf, int len)
14220{
14221 char *s = xmalloc (len + 1);
9a619af0 14222
2e276125
JB
14223 memcpy (s, buf, len);
14224 s[len] = '\0';
2e276125
JB
14225 return s;
14226}
14227
14228
14229static const char *
14230consume_improper_spaces (const char *p, const char *body)
14231{
14232 if (*p == ' ')
14233 {
4d3c2250 14234 complaint (&symfile_complaints,
3e43a32a
MS
14235 _("macro definition contains spaces "
14236 "in formal argument list:\n`%s'"),
4d3c2250 14237 body);
2e276125
JB
14238
14239 while (*p == ' ')
14240 p++;
14241 }
14242
14243 return p;
14244}
14245
14246
14247static void
14248parse_macro_definition (struct macro_source_file *file, int line,
14249 const char *body)
14250{
14251 const char *p;
14252
14253 /* The body string takes one of two forms. For object-like macro
14254 definitions, it should be:
14255
14256 <macro name> " " <definition>
14257
14258 For function-like macro definitions, it should be:
14259
14260 <macro name> "() " <definition>
14261 or
14262 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14263
14264 Spaces may appear only where explicitly indicated, and in the
14265 <definition>.
14266
14267 The Dwarf 2 spec says that an object-like macro's name is always
14268 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14269 the space when the macro's definition is the empty string.
2e276125
JB
14270
14271 The Dwarf 2 spec says that there should be no spaces between the
14272 formal arguments in a function-like macro's formal argument list,
14273 but versions of GCC around March 2002 include spaces after the
14274 commas. */
14275
14276
14277 /* Find the extent of the macro name. The macro name is terminated
14278 by either a space or null character (for an object-like macro) or
14279 an opening paren (for a function-like macro). */
14280 for (p = body; *p; p++)
14281 if (*p == ' ' || *p == '(')
14282 break;
14283
14284 if (*p == ' ' || *p == '\0')
14285 {
14286 /* It's an object-like macro. */
14287 int name_len = p - body;
14288 char *name = copy_string (body, name_len);
14289 const char *replacement;
14290
14291 if (*p == ' ')
14292 replacement = body + name_len + 1;
14293 else
14294 {
4d3c2250 14295 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14296 replacement = body + name_len;
14297 }
6e70227d 14298
2e276125
JB
14299 macro_define_object (file, line, name, replacement);
14300
14301 xfree (name);
14302 }
14303 else if (*p == '(')
14304 {
14305 /* It's a function-like macro. */
14306 char *name = copy_string (body, p - body);
14307 int argc = 0;
14308 int argv_size = 1;
14309 char **argv = xmalloc (argv_size * sizeof (*argv));
14310
14311 p++;
14312
14313 p = consume_improper_spaces (p, body);
14314
14315 /* Parse the formal argument list. */
14316 while (*p && *p != ')')
14317 {
14318 /* Find the extent of the current argument name. */
14319 const char *arg_start = p;
14320
14321 while (*p && *p != ',' && *p != ')' && *p != ' ')
14322 p++;
14323
14324 if (! *p || p == arg_start)
4d3c2250 14325 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14326 else
14327 {
14328 /* Make sure argv has room for the new argument. */
14329 if (argc >= argv_size)
14330 {
14331 argv_size *= 2;
14332 argv = xrealloc (argv, argv_size * sizeof (*argv));
14333 }
14334
14335 argv[argc++] = copy_string (arg_start, p - arg_start);
14336 }
14337
14338 p = consume_improper_spaces (p, body);
14339
14340 /* Consume the comma, if present. */
14341 if (*p == ',')
14342 {
14343 p++;
14344
14345 p = consume_improper_spaces (p, body);
14346 }
14347 }
14348
14349 if (*p == ')')
14350 {
14351 p++;
14352
14353 if (*p == ' ')
14354 /* Perfectly formed definition, no complaints. */
14355 macro_define_function (file, line, name,
6e70227d 14356 argc, (const char **) argv,
2e276125
JB
14357 p + 1);
14358 else if (*p == '\0')
14359 {
14360 /* Complain, but do define it. */
4d3c2250 14361 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14362 macro_define_function (file, line, name,
6e70227d 14363 argc, (const char **) argv,
2e276125
JB
14364 p);
14365 }
14366 else
14367 /* Just complain. */
4d3c2250 14368 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14369 }
14370 else
14371 /* Just complain. */
4d3c2250 14372 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14373
14374 xfree (name);
14375 {
14376 int i;
14377
14378 for (i = 0; i < argc; i++)
14379 xfree (argv[i]);
14380 }
14381 xfree (argv);
14382 }
14383 else
4d3c2250 14384 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14385}
14386
14387
14388static void
14389dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14390 char *comp_dir, bfd *abfd,
e7c27a73 14391 struct dwarf2_cu *cu)
2e276125 14392{
fe1b8b76 14393 gdb_byte *mac_ptr, *mac_end;
2e276125 14394 struct macro_source_file *current_file = 0;
757a13d0
JK
14395 enum dwarf_macinfo_record_type macinfo_type;
14396 int at_commandline;
2e276125 14397
be391dca
TT
14398 dwarf2_read_section (dwarf2_per_objfile->objfile,
14399 &dwarf2_per_objfile->macinfo);
dce234bc 14400 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 14401 {
e2e0b3e5 14402 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
14403 return;
14404 }
14405
757a13d0
JK
14406 /* First pass: Find the name of the base filename.
14407 This filename is needed in order to process all macros whose definition
14408 (or undefinition) comes from the command line. These macros are defined
14409 before the first DW_MACINFO_start_file entry, and yet still need to be
14410 associated to the base file.
14411
14412 To determine the base file name, we scan the macro definitions until we
14413 reach the first DW_MACINFO_start_file entry. We then initialize
14414 CURRENT_FILE accordingly so that any macro definition found before the
14415 first DW_MACINFO_start_file can still be associated to the base file. */
14416
dce234bc
PP
14417 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14418 mac_end = dwarf2_per_objfile->macinfo.buffer
14419 + dwarf2_per_objfile->macinfo.size;
2e276125 14420
757a13d0 14421 do
2e276125 14422 {
2e276125
JB
14423 /* Do we at least have room for a macinfo type byte? */
14424 if (mac_ptr >= mac_end)
14425 {
757a13d0 14426 /* Complaint is printed during the second pass as GDB will probably
3e43a32a
MS
14427 stop the first pass earlier upon finding
14428 DW_MACINFO_start_file. */
757a13d0 14429 break;
2e276125
JB
14430 }
14431
14432 macinfo_type = read_1_byte (abfd, mac_ptr);
14433 mac_ptr++;
14434
14435 switch (macinfo_type)
14436 {
14437 /* A zero macinfo type indicates the end of the macro
14438 information. */
14439 case 0:
757a13d0
JK
14440 break;
14441
14442 case DW_MACINFO_define:
14443 case DW_MACINFO_undef:
14444 /* Only skip the data by MAC_PTR. */
14445 {
14446 unsigned int bytes_read;
14447
14448 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14449 mac_ptr += bytes_read;
9b1c24c8 14450 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14451 mac_ptr += bytes_read;
14452 }
14453 break;
14454
14455 case DW_MACINFO_start_file:
14456 {
14457 unsigned int bytes_read;
14458 int line, file;
14459
14460 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14461 mac_ptr += bytes_read;
14462 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14463 mac_ptr += bytes_read;
14464
3e43a32a
MS
14465 current_file = macro_start_file (file, line, current_file,
14466 comp_dir, lh, cu->objfile);
757a13d0
JK
14467 }
14468 break;
14469
14470 case DW_MACINFO_end_file:
14471 /* No data to skip by MAC_PTR. */
14472 break;
14473
14474 case DW_MACINFO_vendor_ext:
14475 /* Only skip the data by MAC_PTR. */
14476 {
14477 unsigned int bytes_read;
14478
14479 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14480 mac_ptr += bytes_read;
9b1c24c8 14481 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14482 mac_ptr += bytes_read;
14483 }
14484 break;
14485
14486 default:
14487 break;
14488 }
14489 } while (macinfo_type != 0 && current_file == NULL);
14490
14491 /* Second pass: Process all entries.
14492
14493 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14494 command-line macro definitions/undefinitions. This flag is unset when we
14495 reach the first DW_MACINFO_start_file entry. */
14496
dce234bc 14497 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
14498
14499 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14500 GDB is still reading the definitions from command line. First
14501 DW_MACINFO_start_file will need to be ignored as it was already executed
14502 to create CURRENT_FILE for the main source holding also the command line
14503 definitions. On first met DW_MACINFO_start_file this flag is reset to
14504 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14505
14506 at_commandline = 1;
14507
14508 do
14509 {
14510 /* Do we at least have room for a macinfo type byte? */
14511 if (mac_ptr >= mac_end)
14512 {
14513 dwarf2_macros_too_long_complaint ();
14514 break;
14515 }
14516
14517 macinfo_type = read_1_byte (abfd, mac_ptr);
14518 mac_ptr++;
14519
14520 switch (macinfo_type)
14521 {
14522 /* A zero macinfo type indicates the end of the macro
14523 information. */
14524 case 0:
14525 break;
2e276125
JB
14526
14527 case DW_MACINFO_define:
14528 case DW_MACINFO_undef:
14529 {
891d2f0b 14530 unsigned int bytes_read;
2e276125
JB
14531 int line;
14532 char *body;
14533
14534 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14535 mac_ptr += bytes_read;
9b1c24c8 14536 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14537 mac_ptr += bytes_read;
14538
14539 if (! current_file)
757a13d0
JK
14540 {
14541 /* DWARF violation as no main source is present. */
14542 complaint (&symfile_complaints,
14543 _("debug info with no main source gives macro %s "
14544 "on line %d: %s"),
6e70227d
DE
14545 macinfo_type == DW_MACINFO_define ?
14546 _("definition") :
905e0470
PM
14547 macinfo_type == DW_MACINFO_undef ?
14548 _("undefinition") :
14549 _("something-or-other"), line, body);
757a13d0
JK
14550 break;
14551 }
3e43a32a
MS
14552 if ((line == 0 && !at_commandline)
14553 || (line != 0 && at_commandline))
4d3c2250 14554 complaint (&symfile_complaints,
757a13d0
JK
14555 _("debug info gives %s macro %s with %s line %d: %s"),
14556 at_commandline ? _("command-line") : _("in-file"),
905e0470 14557 macinfo_type == DW_MACINFO_define ?
6e70227d 14558 _("definition") :
905e0470
PM
14559 macinfo_type == DW_MACINFO_undef ?
14560 _("undefinition") :
14561 _("something-or-other"),
757a13d0
JK
14562 line == 0 ? _("zero") : _("non-zero"), line, body);
14563
14564 if (macinfo_type == DW_MACINFO_define)
14565 parse_macro_definition (current_file, line, body);
14566 else if (macinfo_type == DW_MACINFO_undef)
14567 macro_undef (current_file, line, body);
2e276125
JB
14568 }
14569 break;
14570
14571 case DW_MACINFO_start_file:
14572 {
891d2f0b 14573 unsigned int bytes_read;
2e276125
JB
14574 int line, file;
14575
14576 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14577 mac_ptr += bytes_read;
14578 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14579 mac_ptr += bytes_read;
14580
3e43a32a
MS
14581 if ((line == 0 && !at_commandline)
14582 || (line != 0 && at_commandline))
757a13d0
JK
14583 complaint (&symfile_complaints,
14584 _("debug info gives source %d included "
14585 "from %s at %s line %d"),
14586 file, at_commandline ? _("command-line") : _("file"),
14587 line == 0 ? _("zero") : _("non-zero"), line);
14588
14589 if (at_commandline)
14590 {
14591 /* This DW_MACINFO_start_file was executed in the pass one. */
14592 at_commandline = 0;
14593 }
14594 else
14595 current_file = macro_start_file (file, line,
14596 current_file, comp_dir,
14597 lh, cu->objfile);
2e276125
JB
14598 }
14599 break;
14600
14601 case DW_MACINFO_end_file:
14602 if (! current_file)
4d3c2250 14603 complaint (&symfile_complaints,
3e43a32a
MS
14604 _("macro debug info has an unmatched "
14605 "`close_file' directive"));
2e276125
JB
14606 else
14607 {
14608 current_file = current_file->included_by;
14609 if (! current_file)
14610 {
14611 enum dwarf_macinfo_record_type next_type;
14612
14613 /* GCC circa March 2002 doesn't produce the zero
14614 type byte marking the end of the compilation
14615 unit. Complain if it's not there, but exit no
14616 matter what. */
14617
14618 /* Do we at least have room for a macinfo type byte? */
14619 if (mac_ptr >= mac_end)
14620 {
4d3c2250 14621 dwarf2_macros_too_long_complaint ();
2e276125
JB
14622 return;
14623 }
14624
14625 /* We don't increment mac_ptr here, so this is just
14626 a look-ahead. */
14627 next_type = read_1_byte (abfd, mac_ptr);
14628 if (next_type != 0)
4d3c2250 14629 complaint (&symfile_complaints,
3e43a32a
MS
14630 _("no terminating 0-type entry for "
14631 "macros in `.debug_macinfo' section"));
2e276125
JB
14632
14633 return;
14634 }
14635 }
14636 break;
14637
14638 case DW_MACINFO_vendor_ext:
14639 {
891d2f0b 14640 unsigned int bytes_read;
2e276125 14641 int constant;
2e276125
JB
14642
14643 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14644 mac_ptr += bytes_read;
e8e80198 14645 read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14646 mac_ptr += bytes_read;
14647
14648 /* We don't recognize any vendor extensions. */
14649 }
14650 break;
14651 }
757a13d0 14652 } while (macinfo_type != 0);
2e276125 14653}
8e19ed76
PS
14654
14655/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 14656 if so return true else false. */
8e19ed76
PS
14657static int
14658attr_form_is_block (struct attribute *attr)
14659{
14660 return (attr == NULL ? 0 :
14661 attr->form == DW_FORM_block1
14662 || attr->form == DW_FORM_block2
14663 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14664 || attr->form == DW_FORM_block
14665 || attr->form == DW_FORM_exprloc);
8e19ed76 14666}
4c2df51b 14667
c6a0999f
JB
14668/* Return non-zero if ATTR's value is a section offset --- classes
14669 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14670 You may use DW_UNSND (attr) to retrieve such offsets.
14671
14672 Section 7.5.4, "Attribute Encodings", explains that no attribute
14673 may have a value that belongs to more than one of these classes; it
14674 would be ambiguous if we did, because we use the same forms for all
14675 of them. */
3690dd37
JB
14676static int
14677attr_form_is_section_offset (struct attribute *attr)
14678{
14679 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14680 || attr->form == DW_FORM_data8
14681 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14682}
14683
14684
14685/* Return non-zero if ATTR's value falls in the 'constant' class, or
14686 zero otherwise. When this function returns true, you can apply
14687 dwarf2_get_attr_constant_value to it.
14688
14689 However, note that for some attributes you must check
14690 attr_form_is_section_offset before using this test. DW_FORM_data4
14691 and DW_FORM_data8 are members of both the constant class, and of
14692 the classes that contain offsets into other debug sections
14693 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14694 that, if an attribute's can be either a constant or one of the
14695 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14696 taken as section offsets, not constants. */
14697static int
14698attr_form_is_constant (struct attribute *attr)
14699{
14700 switch (attr->form)
14701 {
14702 case DW_FORM_sdata:
14703 case DW_FORM_udata:
14704 case DW_FORM_data1:
14705 case DW_FORM_data2:
14706 case DW_FORM_data4:
14707 case DW_FORM_data8:
14708 return 1;
14709 default:
14710 return 0;
14711 }
14712}
14713
8cf6f0b1
TT
14714/* A helper function that fills in a dwarf2_loclist_baton. */
14715
14716static void
14717fill_in_loclist_baton (struct dwarf2_cu *cu,
14718 struct dwarf2_loclist_baton *baton,
14719 struct attribute *attr)
14720{
14721 dwarf2_read_section (dwarf2_per_objfile->objfile,
14722 &dwarf2_per_objfile->loc);
14723
14724 baton->per_cu = cu->per_cu;
14725 gdb_assert (baton->per_cu);
14726 /* We don't know how long the location list is, but make sure we
14727 don't run off the edge of the section. */
14728 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14729 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14730 baton->base_address = cu->base_address;
14731}
14732
4c2df51b
DJ
14733static void
14734dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14735 struct dwarf2_cu *cu)
4c2df51b 14736{
3690dd37 14737 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14738 /* ".debug_loc" may not exist at all, or the offset may be outside
14739 the section. If so, fall through to the complaint in the
14740 other branch. */
9e0ac564
TT
14741 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14742 &dwarf2_per_objfile->loc))
4c2df51b 14743 {
0d53c4c4 14744 struct dwarf2_loclist_baton *baton;
4c2df51b 14745
4a146b47 14746 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14747 sizeof (struct dwarf2_loclist_baton));
4c2df51b 14748
8cf6f0b1 14749 fill_in_loclist_baton (cu, baton, attr);
be391dca 14750
d00adf39 14751 if (cu->base_known == 0)
0d53c4c4 14752 complaint (&symfile_complaints,
3e43a32a
MS
14753 _("Location list used without "
14754 "specifying the CU base address."));
4c2df51b 14755
768a979c 14756 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14757 SYMBOL_LOCATION_BATON (sym) = baton;
14758 }
14759 else
14760 {
14761 struct dwarf2_locexpr_baton *baton;
14762
4a146b47 14763 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14764 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14765 baton->per_cu = cu->per_cu;
14766 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14767
14768 if (attr_form_is_block (attr))
14769 {
14770 /* Note that we're just copying the block's data pointer
14771 here, not the actual data. We're still pointing into the
6502dd73
DJ
14772 info_buffer for SYM's objfile; right now we never release
14773 that buffer, but when we do clean up properly this may
14774 need to change. */
0d53c4c4
DJ
14775 baton->size = DW_BLOCK (attr)->size;
14776 baton->data = DW_BLOCK (attr)->data;
14777 }
14778 else
14779 {
14780 dwarf2_invalid_attrib_class_complaint ("location description",
14781 SYMBOL_NATURAL_NAME (sym));
14782 baton->size = 0;
14783 baton->data = NULL;
14784 }
6e70227d 14785
768a979c 14786 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14787 SYMBOL_LOCATION_BATON (sym) = baton;
14788 }
4c2df51b 14789}
6502dd73 14790
9aa1f1e3
TT
14791/* Return the OBJFILE associated with the compilation unit CU. If CU
14792 came from a separate debuginfo file, then the master objfile is
14793 returned. */
ae0d2f24
UW
14794
14795struct objfile *
14796dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14797{
9291a0cd 14798 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14799
14800 /* Return the master objfile, so that we can report and look up the
14801 correct file containing this variable. */
14802 if (objfile->separate_debug_objfile_backlink)
14803 objfile = objfile->separate_debug_objfile_backlink;
14804
14805 return objfile;
14806}
14807
14808/* Return the address size given in the compilation unit header for CU. */
14809
14810CORE_ADDR
14811dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14812{
14813 if (per_cu->cu)
14814 return per_cu->cu->header.addr_size;
14815 else
14816 {
14817 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14818 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14819 struct dwarf2_per_objfile *per_objfile
14820 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14821 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14822 struct comp_unit_head cu_header;
9a619af0 14823
ae0d2f24
UW
14824 memset (&cu_header, 0, sizeof cu_header);
14825 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14826 return cu_header.addr_size;
14827 }
14828}
14829
9eae7c52
TT
14830/* Return the offset size given in the compilation unit header for CU. */
14831
14832int
14833dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14834{
14835 if (per_cu->cu)
14836 return per_cu->cu->header.offset_size;
14837 else
14838 {
14839 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14840 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14841 struct dwarf2_per_objfile *per_objfile
14842 = objfile_data (objfile, dwarf2_objfile_data_key);
14843 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14844 struct comp_unit_head cu_header;
14845
14846 memset (&cu_header, 0, sizeof cu_header);
14847 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14848 return cu_header.offset_size;
14849 }
14850}
14851
9aa1f1e3
TT
14852/* Return the text offset of the CU. The returned offset comes from
14853 this CU's objfile. If this objfile came from a separate debuginfo
14854 file, then the offset may be different from the corresponding
14855 offset in the parent objfile. */
14856
14857CORE_ADDR
14858dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14859{
bb3fa9d0 14860 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14861
14862 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14863}
14864
348e048f
DE
14865/* Locate the .debug_info compilation unit from CU's objfile which contains
14866 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14867
14868static struct dwarf2_per_cu_data *
c764a876 14869dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14870 struct objfile *objfile)
14871{
14872 struct dwarf2_per_cu_data *this_cu;
14873 int low, high;
14874
ae038cb0
DJ
14875 low = 0;
14876 high = dwarf2_per_objfile->n_comp_units - 1;
14877 while (high > low)
14878 {
14879 int mid = low + (high - low) / 2;
9a619af0 14880
ae038cb0
DJ
14881 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14882 high = mid;
14883 else
14884 low = mid + 1;
14885 }
14886 gdb_assert (low == high);
14887 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14888 {
10b3939b 14889 if (low == 0)
8a3fe4f8
AC
14890 error (_("Dwarf Error: could not find partial DIE containing "
14891 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14892 (long) offset, bfd_get_filename (objfile->obfd));
14893
ae038cb0
DJ
14894 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14895 return dwarf2_per_objfile->all_comp_units[low-1];
14896 }
14897 else
14898 {
14899 this_cu = dwarf2_per_objfile->all_comp_units[low];
14900 if (low == dwarf2_per_objfile->n_comp_units - 1
14901 && offset >= this_cu->offset + this_cu->length)
c764a876 14902 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14903 gdb_assert (offset < this_cu->offset + this_cu->length);
14904 return this_cu;
14905 }
14906}
14907
10b3939b
DJ
14908/* Locate the compilation unit from OBJFILE which is located at exactly
14909 OFFSET. Raises an error on failure. */
14910
ae038cb0 14911static struct dwarf2_per_cu_data *
c764a876 14912dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14913{
14914 struct dwarf2_per_cu_data *this_cu;
9a619af0 14915
ae038cb0
DJ
14916 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14917 if (this_cu->offset != offset)
c764a876 14918 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14919 return this_cu;
14920}
14921
9816fde3 14922/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 14923
9816fde3
JK
14924static void
14925init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 14926{
9816fde3 14927 memset (cu, 0, sizeof (*cu));
93311388
DE
14928 cu->objfile = objfile;
14929 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
14930}
14931
14932/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14933
14934static void
14935prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14936{
14937 struct attribute *attr;
14938
14939 /* Set the language we're debugging. */
14940 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14941 if (attr)
14942 set_cu_language (DW_UNSND (attr), cu);
14943 else
9cded63f
TT
14944 {
14945 cu->language = language_minimal;
14946 cu->language_defn = language_def (cu->language);
14947 }
93311388
DE
14948}
14949
ae038cb0
DJ
14950/* Release one cached compilation unit, CU. We unlink it from the tree
14951 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14952 the caller is responsible for that.
14953 NOTE: DATA is a void * because this function is also used as a
14954 cleanup routine. */
ae038cb0
DJ
14955
14956static void
14957free_one_comp_unit (void *data)
14958{
14959 struct dwarf2_cu *cu = data;
14960
14961 if (cu->per_cu != NULL)
14962 cu->per_cu->cu = NULL;
14963 cu->per_cu = NULL;
14964
14965 obstack_free (&cu->comp_unit_obstack, NULL);
14966
14967 xfree (cu);
14968}
14969
72bf9492 14970/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14971 when we're finished with it. We can't free the pointer itself, but be
14972 sure to unlink it from the cache. Also release any associated storage
14973 and perform cache maintenance.
72bf9492
DJ
14974
14975 Only used during partial symbol parsing. */
14976
14977static void
14978free_stack_comp_unit (void *data)
14979{
14980 struct dwarf2_cu *cu = data;
14981
14982 obstack_free (&cu->comp_unit_obstack, NULL);
14983 cu->partial_dies = NULL;
ae038cb0
DJ
14984
14985 if (cu->per_cu != NULL)
14986 {
14987 /* This compilation unit is on the stack in our caller, so we
14988 should not xfree it. Just unlink it. */
14989 cu->per_cu->cu = NULL;
14990 cu->per_cu = NULL;
14991
14992 /* If we had a per-cu pointer, then we may have other compilation
14993 units loaded, so age them now. */
14994 age_cached_comp_units ();
14995 }
14996}
14997
14998/* Free all cached compilation units. */
14999
15000static void
15001free_cached_comp_units (void *data)
15002{
15003 struct dwarf2_per_cu_data *per_cu, **last_chain;
15004
15005 per_cu = dwarf2_per_objfile->read_in_chain;
15006 last_chain = &dwarf2_per_objfile->read_in_chain;
15007 while (per_cu != NULL)
15008 {
15009 struct dwarf2_per_cu_data *next_cu;
15010
15011 next_cu = per_cu->cu->read_in_chain;
15012
15013 free_one_comp_unit (per_cu->cu);
15014 *last_chain = next_cu;
15015
15016 per_cu = next_cu;
15017 }
15018}
15019
15020/* Increase the age counter on each cached compilation unit, and free
15021 any that are too old. */
15022
15023static void
15024age_cached_comp_units (void)
15025{
15026 struct dwarf2_per_cu_data *per_cu, **last_chain;
15027
15028 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15029 per_cu = dwarf2_per_objfile->read_in_chain;
15030 while (per_cu != NULL)
15031 {
15032 per_cu->cu->last_used ++;
15033 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15034 dwarf2_mark (per_cu->cu);
15035 per_cu = per_cu->cu->read_in_chain;
15036 }
15037
15038 per_cu = dwarf2_per_objfile->read_in_chain;
15039 last_chain = &dwarf2_per_objfile->read_in_chain;
15040 while (per_cu != NULL)
15041 {
15042 struct dwarf2_per_cu_data *next_cu;
15043
15044 next_cu = per_cu->cu->read_in_chain;
15045
15046 if (!per_cu->cu->mark)
15047 {
15048 free_one_comp_unit (per_cu->cu);
15049 *last_chain = next_cu;
15050 }
15051 else
15052 last_chain = &per_cu->cu->read_in_chain;
15053
15054 per_cu = next_cu;
15055 }
15056}
15057
15058/* Remove a single compilation unit from the cache. */
15059
15060static void
15061free_one_cached_comp_unit (void *target_cu)
15062{
15063 struct dwarf2_per_cu_data *per_cu, **last_chain;
15064
15065 per_cu = dwarf2_per_objfile->read_in_chain;
15066 last_chain = &dwarf2_per_objfile->read_in_chain;
15067 while (per_cu != NULL)
15068 {
15069 struct dwarf2_per_cu_data *next_cu;
15070
15071 next_cu = per_cu->cu->read_in_chain;
15072
15073 if (per_cu->cu == target_cu)
15074 {
15075 free_one_comp_unit (per_cu->cu);
15076 *last_chain = next_cu;
15077 break;
15078 }
15079 else
15080 last_chain = &per_cu->cu->read_in_chain;
15081
15082 per_cu = next_cu;
15083 }
15084}
15085
fe3e1990
DJ
15086/* Release all extra memory associated with OBJFILE. */
15087
15088void
15089dwarf2_free_objfile (struct objfile *objfile)
15090{
15091 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15092
15093 if (dwarf2_per_objfile == NULL)
15094 return;
15095
15096 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15097 free_cached_comp_units (NULL);
15098
7b9f3c50
DE
15099 if (dwarf2_per_objfile->quick_file_names_table)
15100 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 15101
fe3e1990
DJ
15102 /* Everything else should be on the objfile obstack. */
15103}
15104
1c379e20
DJ
15105/* A pair of DIE offset and GDB type pointer. We store these
15106 in a hash table separate from the DIEs, and preserve them
15107 when the DIEs are flushed out of cache. */
15108
15109struct dwarf2_offset_and_type
15110{
15111 unsigned int offset;
15112 struct type *type;
15113};
15114
15115/* Hash function for a dwarf2_offset_and_type. */
15116
15117static hashval_t
15118offset_and_type_hash (const void *item)
15119{
15120 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 15121
1c379e20
DJ
15122 return ofs->offset;
15123}
15124
15125/* Equality function for a dwarf2_offset_and_type. */
15126
15127static int
15128offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15129{
15130 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15131 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 15132
1c379e20
DJ
15133 return ofs_lhs->offset == ofs_rhs->offset;
15134}
15135
15136/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
15137 table if necessary. For convenience, return TYPE.
15138
15139 The DIEs reading must have careful ordering to:
15140 * Not cause infite loops trying to read in DIEs as a prerequisite for
15141 reading current DIE.
15142 * Not trying to dereference contents of still incompletely read in types
15143 while reading in other DIEs.
15144 * Enable referencing still incompletely read in types just by a pointer to
15145 the type without accessing its fields.
15146
15147 Therefore caller should follow these rules:
15148 * Try to fetch any prerequisite types we may need to build this DIE type
15149 before building the type and calling set_die_type.
e71ec853 15150 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
15151 possible before fetching more types to complete the current type.
15152 * Make the type as complete as possible before fetching more types. */
1c379e20 15153
f792889a 15154static struct type *
1c379e20
DJ
15155set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15156{
15157 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
15158 struct objfile *objfile = cu->objfile;
15159 htab_t *type_hash_ptr;
1c379e20 15160
b4ba55a1
JB
15161 /* For Ada types, make sure that the gnat-specific data is always
15162 initialized (if not already set). There are a few types where
15163 we should not be doing so, because the type-specific area is
15164 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15165 where the type-specific area is used to store the floatformat).
15166 But this is not a problem, because the gnat-specific information
15167 is actually not needed for these types. */
15168 if (need_gnat_info (cu)
15169 && TYPE_CODE (type) != TYPE_CODE_FUNC
15170 && TYPE_CODE (type) != TYPE_CODE_FLT
15171 && !HAVE_GNAT_AUX_INFO (type))
15172 INIT_GNAT_SPECIFIC (type);
15173
673bfd45
DE
15174 if (cu->per_cu->from_debug_types)
15175 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15176 else
15177 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15178
15179 if (*type_hash_ptr == NULL)
f792889a 15180 {
673bfd45
DE
15181 *type_hash_ptr
15182 = htab_create_alloc_ex (127,
f792889a
DJ
15183 offset_and_type_hash,
15184 offset_and_type_eq,
15185 NULL,
673bfd45 15186 &objfile->objfile_obstack,
f792889a
DJ
15187 hashtab_obstack_allocate,
15188 dummy_obstack_deallocate);
f792889a 15189 }
1c379e20
DJ
15190
15191 ofs.offset = die->offset;
15192 ofs.type = type;
15193 slot = (struct dwarf2_offset_and_type **)
673bfd45 15194 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
15195 if (*slot)
15196 complaint (&symfile_complaints,
15197 _("A problem internal to GDB: DIE 0x%x has type already set"),
15198 die->offset);
673bfd45 15199 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 15200 **slot = ofs;
f792889a 15201 return type;
1c379e20
DJ
15202}
15203
673bfd45
DE
15204/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15205 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
15206
15207static struct type *
673bfd45
DE
15208get_die_type_at_offset (unsigned int offset,
15209 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
15210{
15211 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 15212 htab_t type_hash;
f792889a 15213
673bfd45
DE
15214 if (per_cu->from_debug_types)
15215 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15216 else
15217 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
15218 if (type_hash == NULL)
15219 return NULL;
1c379e20 15220
673bfd45 15221 ofs.offset = offset;
1c379e20
DJ
15222 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15223 if (slot)
15224 return slot->type;
15225 else
15226 return NULL;
15227}
15228
673bfd45
DE
15229/* Look up the type for DIE in the appropriate type_hash table,
15230 or return NULL if DIE does not have a saved type. */
15231
15232static struct type *
15233get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15234{
15235 return get_die_type_at_offset (die->offset, cu->per_cu);
15236}
15237
10b3939b
DJ
15238/* Add a dependence relationship from CU to REF_PER_CU. */
15239
15240static void
15241dwarf2_add_dependence (struct dwarf2_cu *cu,
15242 struct dwarf2_per_cu_data *ref_per_cu)
15243{
15244 void **slot;
15245
15246 if (cu->dependencies == NULL)
15247 cu->dependencies
15248 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15249 NULL, &cu->comp_unit_obstack,
15250 hashtab_obstack_allocate,
15251 dummy_obstack_deallocate);
15252
15253 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15254 if (*slot == NULL)
15255 *slot = ref_per_cu;
15256}
1c379e20 15257
f504f079
DE
15258/* Subroutine of dwarf2_mark to pass to htab_traverse.
15259 Set the mark field in every compilation unit in the
ae038cb0
DJ
15260 cache that we must keep because we are keeping CU. */
15261
10b3939b
DJ
15262static int
15263dwarf2_mark_helper (void **slot, void *data)
15264{
15265 struct dwarf2_per_cu_data *per_cu;
15266
15267 per_cu = (struct dwarf2_per_cu_data *) *slot;
15268 if (per_cu->cu->mark)
15269 return 1;
15270 per_cu->cu->mark = 1;
15271
15272 if (per_cu->cu->dependencies != NULL)
15273 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15274
15275 return 1;
15276}
15277
f504f079
DE
15278/* Set the mark field in CU and in every other compilation unit in the
15279 cache that we must keep because we are keeping CU. */
15280
ae038cb0
DJ
15281static void
15282dwarf2_mark (struct dwarf2_cu *cu)
15283{
15284 if (cu->mark)
15285 return;
15286 cu->mark = 1;
10b3939b
DJ
15287 if (cu->dependencies != NULL)
15288 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
15289}
15290
15291static void
15292dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15293{
15294 while (per_cu)
15295 {
15296 per_cu->cu->mark = 0;
15297 per_cu = per_cu->cu->read_in_chain;
15298 }
72bf9492
DJ
15299}
15300
72bf9492
DJ
15301/* Trivial hash function for partial_die_info: the hash value of a DIE
15302 is its offset in .debug_info for this objfile. */
15303
15304static hashval_t
15305partial_die_hash (const void *item)
15306{
15307 const struct partial_die_info *part_die = item;
9a619af0 15308
72bf9492
DJ
15309 return part_die->offset;
15310}
15311
15312/* Trivial comparison function for partial_die_info structures: two DIEs
15313 are equal if they have the same offset. */
15314
15315static int
15316partial_die_eq (const void *item_lhs, const void *item_rhs)
15317{
15318 const struct partial_die_info *part_die_lhs = item_lhs;
15319 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 15320
72bf9492
DJ
15321 return part_die_lhs->offset == part_die_rhs->offset;
15322}
15323
ae038cb0
DJ
15324static struct cmd_list_element *set_dwarf2_cmdlist;
15325static struct cmd_list_element *show_dwarf2_cmdlist;
15326
15327static void
15328set_dwarf2_cmd (char *args, int from_tty)
15329{
15330 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15331}
15332
15333static void
15334show_dwarf2_cmd (char *args, int from_tty)
6e70227d 15335{
ae038cb0
DJ
15336 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15337}
15338
dce234bc
PP
15339/* If section described by INFO was mmapped, munmap it now. */
15340
15341static void
15342munmap_section_buffer (struct dwarf2_section_info *info)
15343{
15344 if (info->was_mmapped)
15345 {
15346#ifdef HAVE_MMAP
15347 intptr_t begin = (intptr_t) info->buffer;
15348 intptr_t map_begin = begin & ~(pagesize - 1);
15349 size_t map_length = info->size + begin - map_begin;
9a619af0 15350
dce234bc
PP
15351 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15352#else
15353 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 15354 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
15355#endif
15356 }
15357}
15358
15359/* munmap debug sections for OBJFILE, if necessary. */
15360
15361static void
c1bd65d0 15362dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
15363{
15364 struct dwarf2_per_objfile *data = d;
9a619af0 15365
16be1145
DE
15366 /* This is sorted according to the order they're defined in to make it easier
15367 to keep in sync. */
dce234bc
PP
15368 munmap_section_buffer (&data->info);
15369 munmap_section_buffer (&data->abbrev);
15370 munmap_section_buffer (&data->line);
16be1145 15371 munmap_section_buffer (&data->loc);
dce234bc 15372 munmap_section_buffer (&data->macinfo);
16be1145 15373 munmap_section_buffer (&data->str);
dce234bc 15374 munmap_section_buffer (&data->ranges);
16be1145 15375 munmap_section_buffer (&data->types);
dce234bc
PP
15376 munmap_section_buffer (&data->frame);
15377 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
15378 munmap_section_buffer (&data->gdb_index);
15379}
15380
15381\f
ae2de4f8 15382/* The "save gdb-index" command. */
9291a0cd
TT
15383
15384/* The contents of the hash table we create when building the string
15385 table. */
15386struct strtab_entry
15387{
15388 offset_type offset;
15389 const char *str;
15390};
15391
559a7a62
JK
15392/* Hash function for a strtab_entry.
15393
15394 Function is used only during write_hash_table so no index format backward
15395 compatibility is needed. */
b89be57b 15396
9291a0cd
TT
15397static hashval_t
15398hash_strtab_entry (const void *e)
15399{
15400 const struct strtab_entry *entry = e;
559a7a62 15401 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
15402}
15403
15404/* Equality function for a strtab_entry. */
b89be57b 15405
9291a0cd
TT
15406static int
15407eq_strtab_entry (const void *a, const void *b)
15408{
15409 const struct strtab_entry *ea = a;
15410 const struct strtab_entry *eb = b;
15411 return !strcmp (ea->str, eb->str);
15412}
15413
15414/* Create a strtab_entry hash table. */
b89be57b 15415
9291a0cd
TT
15416static htab_t
15417create_strtab (void)
15418{
15419 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15420 xfree, xcalloc, xfree);
15421}
15422
15423/* Add a string to the constant pool. Return the string's offset in
15424 host order. */
b89be57b 15425
9291a0cd
TT
15426static offset_type
15427add_string (htab_t table, struct obstack *cpool, const char *str)
15428{
15429 void **slot;
15430 struct strtab_entry entry;
15431 struct strtab_entry *result;
15432
15433 entry.str = str;
15434 slot = htab_find_slot (table, &entry, INSERT);
15435 if (*slot)
15436 result = *slot;
15437 else
15438 {
15439 result = XNEW (struct strtab_entry);
15440 result->offset = obstack_object_size (cpool);
15441 result->str = str;
15442 obstack_grow_str0 (cpool, str);
15443 *slot = result;
15444 }
15445 return result->offset;
15446}
15447
15448/* An entry in the symbol table. */
15449struct symtab_index_entry
15450{
15451 /* The name of the symbol. */
15452 const char *name;
15453 /* The offset of the name in the constant pool. */
15454 offset_type index_offset;
15455 /* A sorted vector of the indices of all the CUs that hold an object
15456 of this name. */
15457 VEC (offset_type) *cu_indices;
15458};
15459
15460/* The symbol table. This is a power-of-2-sized hash table. */
15461struct mapped_symtab
15462{
15463 offset_type n_elements;
15464 offset_type size;
15465 struct symtab_index_entry **data;
15466};
15467
15468/* Hash function for a symtab_index_entry. */
b89be57b 15469
9291a0cd
TT
15470static hashval_t
15471hash_symtab_entry (const void *e)
15472{
15473 const struct symtab_index_entry *entry = e;
15474 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15475 sizeof (offset_type) * VEC_length (offset_type,
15476 entry->cu_indices),
15477 0);
15478}
15479
15480/* Equality function for a symtab_index_entry. */
b89be57b 15481
9291a0cd
TT
15482static int
15483eq_symtab_entry (const void *a, const void *b)
15484{
15485 const struct symtab_index_entry *ea = a;
15486 const struct symtab_index_entry *eb = b;
15487 int len = VEC_length (offset_type, ea->cu_indices);
15488 if (len != VEC_length (offset_type, eb->cu_indices))
15489 return 0;
15490 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15491 VEC_address (offset_type, eb->cu_indices),
15492 sizeof (offset_type) * len);
15493}
15494
15495/* Destroy a symtab_index_entry. */
b89be57b 15496
9291a0cd
TT
15497static void
15498delete_symtab_entry (void *p)
15499{
15500 struct symtab_index_entry *entry = p;
15501 VEC_free (offset_type, entry->cu_indices);
15502 xfree (entry);
15503}
15504
15505/* Create a hash table holding symtab_index_entry objects. */
b89be57b 15506
9291a0cd 15507static htab_t
3876f04e 15508create_symbol_hash_table (void)
9291a0cd
TT
15509{
15510 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15511 delete_symtab_entry, xcalloc, xfree);
15512}
15513
15514/* Create a new mapped symtab object. */
b89be57b 15515
9291a0cd
TT
15516static struct mapped_symtab *
15517create_mapped_symtab (void)
15518{
15519 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15520 symtab->n_elements = 0;
15521 symtab->size = 1024;
15522 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15523 return symtab;
15524}
15525
15526/* Destroy a mapped_symtab. */
b89be57b 15527
9291a0cd
TT
15528static void
15529cleanup_mapped_symtab (void *p)
15530{
15531 struct mapped_symtab *symtab = p;
15532 /* The contents of the array are freed when the other hash table is
15533 destroyed. */
15534 xfree (symtab->data);
15535 xfree (symtab);
15536}
15537
15538/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
15539 the slot.
15540
15541 Function is used only during write_hash_table so no index format backward
15542 compatibility is needed. */
b89be57b 15543
9291a0cd
TT
15544static struct symtab_index_entry **
15545find_slot (struct mapped_symtab *symtab, const char *name)
15546{
559a7a62 15547 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
15548
15549 index = hash & (symtab->size - 1);
15550 step = ((hash * 17) & (symtab->size - 1)) | 1;
15551
15552 for (;;)
15553 {
15554 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15555 return &symtab->data[index];
15556 index = (index + step) & (symtab->size - 1);
15557 }
15558}
15559
15560/* Expand SYMTAB's hash table. */
b89be57b 15561
9291a0cd
TT
15562static void
15563hash_expand (struct mapped_symtab *symtab)
15564{
15565 offset_type old_size = symtab->size;
15566 offset_type i;
15567 struct symtab_index_entry **old_entries = symtab->data;
15568
15569 symtab->size *= 2;
15570 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15571
15572 for (i = 0; i < old_size; ++i)
15573 {
15574 if (old_entries[i])
15575 {
15576 struct symtab_index_entry **slot = find_slot (symtab,
15577 old_entries[i]->name);
15578 *slot = old_entries[i];
15579 }
15580 }
15581
15582 xfree (old_entries);
15583}
15584
15585/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15586 is the index of the CU in which the symbol appears. */
b89be57b 15587
9291a0cd
TT
15588static void
15589add_index_entry (struct mapped_symtab *symtab, const char *name,
15590 offset_type cu_index)
15591{
15592 struct symtab_index_entry **slot;
15593
15594 ++symtab->n_elements;
15595 if (4 * symtab->n_elements / 3 >= symtab->size)
15596 hash_expand (symtab);
15597
15598 slot = find_slot (symtab, name);
15599 if (!*slot)
15600 {
15601 *slot = XNEW (struct symtab_index_entry);
15602 (*slot)->name = name;
15603 (*slot)->cu_indices = NULL;
15604 }
15605 /* Don't push an index twice. Due to how we add entries we only
15606 have to check the last one. */
15607 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 15608 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
15609 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15610}
15611
15612/* Add a vector of indices to the constant pool. */
b89be57b 15613
9291a0cd 15614static offset_type
3876f04e 15615add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15616 struct symtab_index_entry *entry)
15617{
15618 void **slot;
15619
3876f04e 15620 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15621 if (!*slot)
15622 {
15623 offset_type len = VEC_length (offset_type, entry->cu_indices);
15624 offset_type val = MAYBE_SWAP (len);
15625 offset_type iter;
15626 int i;
15627
15628 *slot = entry;
15629 entry->index_offset = obstack_object_size (cpool);
15630
15631 obstack_grow (cpool, &val, sizeof (val));
15632 for (i = 0;
15633 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15634 ++i)
15635 {
15636 val = MAYBE_SWAP (iter);
15637 obstack_grow (cpool, &val, sizeof (val));
15638 }
15639 }
15640 else
15641 {
15642 struct symtab_index_entry *old_entry = *slot;
15643 entry->index_offset = old_entry->index_offset;
15644 entry = old_entry;
15645 }
15646 return entry->index_offset;
15647}
15648
15649/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15650 constant pool entries going into the obstack CPOOL. */
b89be57b 15651
9291a0cd
TT
15652static void
15653write_hash_table (struct mapped_symtab *symtab,
15654 struct obstack *output, struct obstack *cpool)
15655{
15656 offset_type i;
3876f04e 15657 htab_t symbol_hash_table;
9291a0cd
TT
15658 htab_t str_table;
15659
3876f04e 15660 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15661 str_table = create_strtab ();
3876f04e 15662
9291a0cd
TT
15663 /* We add all the index vectors to the constant pool first, to
15664 ensure alignment is ok. */
15665 for (i = 0; i < symtab->size; ++i)
15666 {
15667 if (symtab->data[i])
3876f04e 15668 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15669 }
15670
15671 /* Now write out the hash table. */
15672 for (i = 0; i < symtab->size; ++i)
15673 {
15674 offset_type str_off, vec_off;
15675
15676 if (symtab->data[i])
15677 {
15678 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15679 vec_off = symtab->data[i]->index_offset;
15680 }
15681 else
15682 {
15683 /* While 0 is a valid constant pool index, it is not valid
15684 to have 0 for both offsets. */
15685 str_off = 0;
15686 vec_off = 0;
15687 }
15688
15689 str_off = MAYBE_SWAP (str_off);
15690 vec_off = MAYBE_SWAP (vec_off);
15691
15692 obstack_grow (output, &str_off, sizeof (str_off));
15693 obstack_grow (output, &vec_off, sizeof (vec_off));
15694 }
15695
15696 htab_delete (str_table);
3876f04e 15697 htab_delete (symbol_hash_table);
9291a0cd
TT
15698}
15699
0a5429f6
DE
15700/* Struct to map psymtab to CU index in the index file. */
15701struct psymtab_cu_index_map
15702{
15703 struct partial_symtab *psymtab;
15704 unsigned int cu_index;
15705};
15706
15707static hashval_t
15708hash_psymtab_cu_index (const void *item)
15709{
15710 const struct psymtab_cu_index_map *map = item;
15711
15712 return htab_hash_pointer (map->psymtab);
15713}
15714
15715static int
15716eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15717{
15718 const struct psymtab_cu_index_map *lhs = item_lhs;
15719 const struct psymtab_cu_index_map *rhs = item_rhs;
15720
15721 return lhs->psymtab == rhs->psymtab;
15722}
15723
15724/* Helper struct for building the address table. */
15725struct addrmap_index_data
15726{
15727 struct objfile *objfile;
15728 struct obstack *addr_obstack;
15729 htab_t cu_index_htab;
15730
15731 /* Non-zero if the previous_* fields are valid.
15732 We can't write an entry until we see the next entry (since it is only then
15733 that we know the end of the entry). */
15734 int previous_valid;
15735 /* Index of the CU in the table of all CUs in the index file. */
15736 unsigned int previous_cu_index;
0963b4bd 15737 /* Start address of the CU. */
0a5429f6
DE
15738 CORE_ADDR previous_cu_start;
15739};
15740
15741/* Write an address entry to OBSTACK. */
b89be57b 15742
9291a0cd 15743static void
0a5429f6
DE
15744add_address_entry (struct objfile *objfile, struct obstack *obstack,
15745 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 15746{
0a5429f6 15747 offset_type cu_index_to_write;
9291a0cd
TT
15748 char addr[8];
15749 CORE_ADDR baseaddr;
15750
15751 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15752
0a5429f6
DE
15753 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15754 obstack_grow (obstack, addr, 8);
15755 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15756 obstack_grow (obstack, addr, 8);
15757 cu_index_to_write = MAYBE_SWAP (cu_index);
15758 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15759}
15760
15761/* Worker function for traversing an addrmap to build the address table. */
15762
15763static int
15764add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15765{
15766 struct addrmap_index_data *data = datap;
15767 struct partial_symtab *pst = obj;
15768 offset_type cu_index;
15769 void **slot;
15770
15771 if (data->previous_valid)
15772 add_address_entry (data->objfile, data->addr_obstack,
15773 data->previous_cu_start, start_addr,
15774 data->previous_cu_index);
15775
15776 data->previous_cu_start = start_addr;
15777 if (pst != NULL)
15778 {
15779 struct psymtab_cu_index_map find_map, *map;
15780 find_map.psymtab = pst;
15781 map = htab_find (data->cu_index_htab, &find_map);
15782 gdb_assert (map != NULL);
15783 data->previous_cu_index = map->cu_index;
15784 data->previous_valid = 1;
15785 }
15786 else
15787 data->previous_valid = 0;
15788
15789 return 0;
15790}
15791
15792/* Write OBJFILE's address map to OBSTACK.
15793 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15794 in the index file. */
15795
15796static void
15797write_address_map (struct objfile *objfile, struct obstack *obstack,
15798 htab_t cu_index_htab)
15799{
15800 struct addrmap_index_data addrmap_index_data;
15801
15802 /* When writing the address table, we have to cope with the fact that
15803 the addrmap iterator only provides the start of a region; we have to
15804 wait until the next invocation to get the start of the next region. */
15805
15806 addrmap_index_data.objfile = objfile;
15807 addrmap_index_data.addr_obstack = obstack;
15808 addrmap_index_data.cu_index_htab = cu_index_htab;
15809 addrmap_index_data.previous_valid = 0;
15810
15811 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15812 &addrmap_index_data);
15813
15814 /* It's highly unlikely the last entry (end address = 0xff...ff)
15815 is valid, but we should still handle it.
15816 The end address is recorded as the start of the next region, but that
15817 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15818 anyway. */
15819 if (addrmap_index_data.previous_valid)
15820 add_address_entry (objfile, obstack,
15821 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15822 addrmap_index_data.previous_cu_index);
9291a0cd
TT
15823}
15824
15825/* Add a list of partial symbols to SYMTAB. */
b89be57b 15826
9291a0cd
TT
15827static void
15828write_psymbols (struct mapped_symtab *symtab,
987d643c 15829 htab_t psyms_seen,
9291a0cd
TT
15830 struct partial_symbol **psymp,
15831 int count,
987d643c
TT
15832 offset_type cu_index,
15833 int is_static)
9291a0cd
TT
15834{
15835 for (; count-- > 0; ++psymp)
15836 {
987d643c
TT
15837 void **slot, *lookup;
15838
9291a0cd
TT
15839 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15840 error (_("Ada is not currently supported by the index"));
987d643c
TT
15841
15842 /* We only want to add a given psymbol once. However, we also
15843 want to account for whether it is global or static. So, we
15844 may add it twice, using slightly different values. */
15845 if (is_static)
15846 {
15847 uintptr_t val = 1 | (uintptr_t) *psymp;
15848
15849 lookup = (void *) val;
15850 }
15851 else
15852 lookup = *psymp;
15853
15854 /* Only add a given psymbol once. */
15855 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15856 if (!*slot)
15857 {
15858 *slot = lookup;
15859 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15860 }
9291a0cd
TT
15861 }
15862}
15863
15864/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15865 exception if there is an error. */
b89be57b 15866
9291a0cd
TT
15867static void
15868write_obstack (FILE *file, struct obstack *obstack)
15869{
15870 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15871 file)
15872 != obstack_object_size (obstack))
15873 error (_("couldn't data write to file"));
15874}
15875
15876/* Unlink a file if the argument is not NULL. */
b89be57b 15877
9291a0cd
TT
15878static void
15879unlink_if_set (void *p)
15880{
15881 char **filename = p;
15882 if (*filename)
15883 unlink (*filename);
15884}
15885
1fd400ff
TT
15886/* A helper struct used when iterating over debug_types. */
15887struct signatured_type_index_data
15888{
15889 struct objfile *objfile;
15890 struct mapped_symtab *symtab;
15891 struct obstack *types_list;
987d643c 15892 htab_t psyms_seen;
1fd400ff
TT
15893 int cu_index;
15894};
15895
15896/* A helper function that writes a single signatured_type to an
15897 obstack. */
b89be57b 15898
1fd400ff
TT
15899static int
15900write_one_signatured_type (void **slot, void *d)
15901{
15902 struct signatured_type_index_data *info = d;
15903 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15904 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15905 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15906 gdb_byte val[8];
15907
15908 write_psymbols (info->symtab,
987d643c 15909 info->psyms_seen,
3e43a32a
MS
15910 info->objfile->global_psymbols.list
15911 + psymtab->globals_offset,
987d643c
TT
15912 psymtab->n_global_syms, info->cu_index,
15913 0);
1fd400ff 15914 write_psymbols (info->symtab,
987d643c 15915 info->psyms_seen,
3e43a32a
MS
15916 info->objfile->static_psymbols.list
15917 + psymtab->statics_offset,
987d643c
TT
15918 psymtab->n_static_syms, info->cu_index,
15919 1);
1fd400ff 15920
b3c8eb43 15921 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
1fd400ff
TT
15922 obstack_grow (info->types_list, val, 8);
15923 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15924 obstack_grow (info->types_list, val, 8);
15925 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15926 obstack_grow (info->types_list, val, 8);
15927
15928 ++info->cu_index;
15929
15930 return 1;
15931}
15932
987d643c
TT
15933/* A cleanup function for an htab_t. */
15934
15935static void
15936cleanup_htab (void *arg)
15937{
15938 htab_delete (arg);
15939}
15940
9291a0cd 15941/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 15942
9291a0cd
TT
15943static void
15944write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15945{
15946 struct cleanup *cleanup;
15947 char *filename, *cleanup_filename;
1fd400ff
TT
15948 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15949 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15950 int i;
15951 FILE *out_file;
15952 struct mapped_symtab *symtab;
15953 offset_type val, size_of_contents, total_len;
15954 struct stat st;
15955 char buf[8];
987d643c 15956 htab_t psyms_seen;
0a5429f6
DE
15957 htab_t cu_index_htab;
15958 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 15959
b4f2f049 15960 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 15961 return;
b4f2f049 15962
9291a0cd
TT
15963 if (dwarf2_per_objfile->using_index)
15964 error (_("Cannot use an index to create the index"));
15965
15966 if (stat (objfile->name, &st) < 0)
7e17e088 15967 perror_with_name (objfile->name);
9291a0cd
TT
15968
15969 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15970 INDEX_SUFFIX, (char *) NULL);
15971 cleanup = make_cleanup (xfree, filename);
15972
15973 out_file = fopen (filename, "wb");
15974 if (!out_file)
15975 error (_("Can't open `%s' for writing"), filename);
15976
15977 cleanup_filename = filename;
15978 make_cleanup (unlink_if_set, &cleanup_filename);
15979
15980 symtab = create_mapped_symtab ();
15981 make_cleanup (cleanup_mapped_symtab, symtab);
15982
15983 obstack_init (&addr_obstack);
15984 make_cleanup_obstack_free (&addr_obstack);
15985
15986 obstack_init (&cu_list);
15987 make_cleanup_obstack_free (&cu_list);
15988
1fd400ff
TT
15989 obstack_init (&types_cu_list);
15990 make_cleanup_obstack_free (&types_cu_list);
15991
987d643c
TT
15992 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15993 NULL, xcalloc, xfree);
15994 make_cleanup (cleanup_htab, psyms_seen);
15995
0a5429f6
DE
15996 /* While we're scanning CU's create a table that maps a psymtab pointer
15997 (which is what addrmap records) to its index (which is what is recorded
15998 in the index file). This will later be needed to write the address
15999 table. */
16000 cu_index_htab = htab_create_alloc (100,
16001 hash_psymtab_cu_index,
16002 eq_psymtab_cu_index,
16003 NULL, xcalloc, xfree);
16004 make_cleanup (cleanup_htab, cu_index_htab);
16005 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16006 xmalloc (sizeof (struct psymtab_cu_index_map)
16007 * dwarf2_per_objfile->n_comp_units);
16008 make_cleanup (xfree, psymtab_cu_index_map);
16009
16010 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
16011 work here. Also, the debug_types entries do not appear in
16012 all_comp_units, but only in their own hash table. */
9291a0cd
TT
16013 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16014 {
3e43a32a
MS
16015 struct dwarf2_per_cu_data *per_cu
16016 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 16017 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 16018 gdb_byte val[8];
0a5429f6
DE
16019 struct psymtab_cu_index_map *map;
16020 void **slot;
9291a0cd
TT
16021
16022 write_psymbols (symtab,
987d643c 16023 psyms_seen,
9291a0cd 16024 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
16025 psymtab->n_global_syms, i,
16026 0);
9291a0cd 16027 write_psymbols (symtab,
987d643c 16028 psyms_seen,
9291a0cd 16029 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
16030 psymtab->n_static_syms, i,
16031 1);
9291a0cd 16032
0a5429f6
DE
16033 map = &psymtab_cu_index_map[i];
16034 map->psymtab = psymtab;
16035 map->cu_index = i;
16036 slot = htab_find_slot (cu_index_htab, map, INSERT);
16037 gdb_assert (slot != NULL);
16038 gdb_assert (*slot == NULL);
16039 *slot = map;
9291a0cd 16040
e254ef6a 16041 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 16042 obstack_grow (&cu_list, val, 8);
e254ef6a 16043 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
16044 obstack_grow (&cu_list, val, 8);
16045 }
16046
0a5429f6
DE
16047 /* Dump the address map. */
16048 write_address_map (objfile, &addr_obstack, cu_index_htab);
16049
1fd400ff
TT
16050 /* Write out the .debug_type entries, if any. */
16051 if (dwarf2_per_objfile->signatured_types)
16052 {
16053 struct signatured_type_index_data sig_data;
16054
16055 sig_data.objfile = objfile;
16056 sig_data.symtab = symtab;
16057 sig_data.types_list = &types_cu_list;
987d643c 16058 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
16059 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16060 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16061 write_one_signatured_type, &sig_data);
16062 }
16063
9291a0cd
TT
16064 obstack_init (&constant_pool);
16065 make_cleanup_obstack_free (&constant_pool);
16066 obstack_init (&symtab_obstack);
16067 make_cleanup_obstack_free (&symtab_obstack);
16068 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16069
16070 obstack_init (&contents);
16071 make_cleanup_obstack_free (&contents);
1fd400ff 16072 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
16073 total_len = size_of_contents;
16074
16075 /* The version number. */
559a7a62 16076 val = MAYBE_SWAP (5);
9291a0cd
TT
16077 obstack_grow (&contents, &val, sizeof (val));
16078
16079 /* The offset of the CU list from the start of the file. */
16080 val = MAYBE_SWAP (total_len);
16081 obstack_grow (&contents, &val, sizeof (val));
16082 total_len += obstack_object_size (&cu_list);
16083
1fd400ff
TT
16084 /* The offset of the types CU list from the start of the file. */
16085 val = MAYBE_SWAP (total_len);
16086 obstack_grow (&contents, &val, sizeof (val));
16087 total_len += obstack_object_size (&types_cu_list);
16088
9291a0cd
TT
16089 /* The offset of the address table from the start of the file. */
16090 val = MAYBE_SWAP (total_len);
16091 obstack_grow (&contents, &val, sizeof (val));
16092 total_len += obstack_object_size (&addr_obstack);
16093
16094 /* The offset of the symbol table from the start of the file. */
16095 val = MAYBE_SWAP (total_len);
16096 obstack_grow (&contents, &val, sizeof (val));
16097 total_len += obstack_object_size (&symtab_obstack);
16098
16099 /* The offset of the constant pool from the start of the file. */
16100 val = MAYBE_SWAP (total_len);
16101 obstack_grow (&contents, &val, sizeof (val));
16102 total_len += obstack_object_size (&constant_pool);
16103
16104 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16105
16106 write_obstack (out_file, &contents);
16107 write_obstack (out_file, &cu_list);
1fd400ff 16108 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
16109 write_obstack (out_file, &addr_obstack);
16110 write_obstack (out_file, &symtab_obstack);
16111 write_obstack (out_file, &constant_pool);
16112
16113 fclose (out_file);
16114
16115 /* We want to keep the file, so we set cleanup_filename to NULL
16116 here. See unlink_if_set. */
16117 cleanup_filename = NULL;
16118
16119 do_cleanups (cleanup);
16120}
16121
90476074
TT
16122/* Implementation of the `save gdb-index' command.
16123
16124 Note that the file format used by this command is documented in the
16125 GDB manual. Any changes here must be documented there. */
11570e71 16126
9291a0cd
TT
16127static void
16128save_gdb_index_command (char *arg, int from_tty)
16129{
16130 struct objfile *objfile;
16131
16132 if (!arg || !*arg)
96d19272 16133 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
16134
16135 ALL_OBJFILES (objfile)
16136 {
16137 struct stat st;
16138
16139 /* If the objfile does not correspond to an actual file, skip it. */
16140 if (stat (objfile->name, &st) < 0)
16141 continue;
16142
16143 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16144 if (dwarf2_per_objfile)
16145 {
16146 volatile struct gdb_exception except;
16147
16148 TRY_CATCH (except, RETURN_MASK_ERROR)
16149 {
16150 write_psymtabs_to_index (objfile, arg);
16151 }
16152 if (except.reason < 0)
16153 exception_fprintf (gdb_stderr, except,
16154 _("Error while writing index for `%s': "),
16155 objfile->name);
16156 }
16157 }
dce234bc
PP
16158}
16159
9291a0cd
TT
16160\f
16161
9eae7c52
TT
16162int dwarf2_always_disassemble;
16163
16164static void
16165show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16166 struct cmd_list_element *c, const char *value)
16167{
3e43a32a
MS
16168 fprintf_filtered (file,
16169 _("Whether to always disassemble "
16170 "DWARF expressions is %s.\n"),
9eae7c52
TT
16171 value);
16172}
16173
6502dd73
DJ
16174void _initialize_dwarf2_read (void);
16175
16176void
16177_initialize_dwarf2_read (void)
16178{
96d19272
JK
16179 struct cmd_list_element *c;
16180
dce234bc 16181 dwarf2_objfile_data_key
c1bd65d0 16182 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 16183
1bedd215
AC
16184 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16185Set DWARF 2 specific variables.\n\
16186Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16187 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16188 0/*allow-unknown*/, &maintenance_set_cmdlist);
16189
1bedd215
AC
16190 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16191Show DWARF 2 specific variables\n\
16192Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16193 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16194 0/*allow-unknown*/, &maintenance_show_cmdlist);
16195
16196 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
16197 &dwarf2_max_cache_age, _("\
16198Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16199Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16200A higher limit means that cached compilation units will be stored\n\
16201in memory longer, and more total memory will be used. Zero disables\n\
16202caching, which can slow down startup."),
2c5b56ce 16203 NULL,
920d2a44 16204 show_dwarf2_max_cache_age,
2c5b56ce 16205 &set_dwarf2_cmdlist,
ae038cb0 16206 &show_dwarf2_cmdlist);
d97bc12b 16207
9eae7c52
TT
16208 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16209 &dwarf2_always_disassemble, _("\
16210Set whether `info address' always disassembles DWARF expressions."), _("\
16211Show whether `info address' always disassembles DWARF expressions."), _("\
16212When enabled, DWARF expressions are always printed in an assembly-like\n\
16213syntax. When disabled, expressions will be printed in a more\n\
16214conversational style, when possible."),
16215 NULL,
16216 show_dwarf2_always_disassemble,
16217 &set_dwarf2_cmdlist,
16218 &show_dwarf2_cmdlist);
16219
d97bc12b
DE
16220 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16221Set debugging of the dwarf2 DIE reader."), _("\
16222Show debugging of the dwarf2 DIE reader."), _("\
16223When enabled (non-zero), DIEs are dumped after they are read in.\n\
16224The value is the maximum depth to print."),
16225 NULL,
16226 NULL,
16227 &setdebuglist, &showdebuglist);
9291a0cd 16228
96d19272 16229 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 16230 _("\
fc1a9d6e 16231Save a gdb-index file.\n\
11570e71 16232Usage: save gdb-index DIRECTORY"),
96d19272
JK
16233 &save_cmdlist);
16234 set_cmd_completer (c, filename_completer);
6502dd73 16235}
This page took 2.486457 seconds and 4 git commands to generate.