2011-06-27 Tristan Gingold <gingold@adacore.com>
[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 245
251d32d9 246/* Default names of the debugging sections. */
c906108c 247
233a11ab
CS
248/* Note that if the debugging section has been compressed, it might
249 have a name like .zdebug_info. */
250
251d32d9
TG
251static const struct dwarf2_debug_sections dwarf2_elf_names = {
252 { ".debug_info", ".zdebug_info" },
253 { ".debug_abbrev", ".zdebug_abbrev" },
254 { ".debug_line", ".zdebug_line" },
255 { ".debug_loc", ".zdebug_loc" },
256 { ".debug_macinfo", ".zdebug_macinfo" },
257 { ".debug_str", ".zdebug_str" },
258 { ".debug_ranges", ".zdebug_ranges" },
259 { ".debug_types", ".zdebug_types" },
260 { ".debug_frame", ".zdebug_frame" },
261 { ".eh_frame", NULL },
262 { ".gdb_index", ".zgdb_index" }
263};
c906108c
SS
264
265/* local data types */
266
0963b4bd 267/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
268#ifndef ABBREV_HASH_SIZE
269#define ABBREV_HASH_SIZE 121
270#endif
271
107d2387
AC
272/* The data in a compilation unit header, after target2host
273 translation, looks like this. */
c906108c 274struct comp_unit_head
a738430d 275{
c764a876 276 unsigned int length;
a738430d 277 short version;
a738430d
MK
278 unsigned char addr_size;
279 unsigned char signed_addr_p;
9cbfa09e 280 unsigned int abbrev_offset;
57349743 281
a738430d
MK
282 /* Size of file offsets; either 4 or 8. */
283 unsigned int offset_size;
57349743 284
a738430d
MK
285 /* Size of the length field; either 4 or 12. */
286 unsigned int initial_length_size;
57349743 287
a738430d
MK
288 /* Offset to the first byte of this compilation unit header in the
289 .debug_info section, for resolving relative reference dies. */
290 unsigned int offset;
57349743 291
d00adf39
DE
292 /* Offset to first die in this cu from the start of the cu.
293 This will be the first byte following the compilation unit header. */
294 unsigned int first_die_offset;
a738430d 295};
c906108c 296
3da10d80
KS
297/* Type used for delaying computation of method physnames.
298 See comments for compute_delayed_physnames. */
299struct delayed_method_info
300{
301 /* The type to which the method is attached, i.e., its parent class. */
302 struct type *type;
303
304 /* The index of the method in the type's function fieldlists. */
305 int fnfield_index;
306
307 /* The index of the method in the fieldlist. */
308 int index;
309
310 /* The name of the DIE. */
311 const char *name;
312
313 /* The DIE associated with this method. */
314 struct die_info *die;
315};
316
317typedef struct delayed_method_info delayed_method_info;
318DEF_VEC_O (delayed_method_info);
319
e7c27a73
DJ
320/* Internal state when decoding a particular compilation unit. */
321struct dwarf2_cu
322{
323 /* The objfile containing this compilation unit. */
324 struct objfile *objfile;
325
d00adf39 326 /* The header of the compilation unit. */
e7c27a73 327 struct comp_unit_head header;
e142c38c 328
d00adf39
DE
329 /* Base address of this compilation unit. */
330 CORE_ADDR base_address;
331
332 /* Non-zero if base_address has been set. */
333 int base_known;
334
e142c38c
DJ
335 struct function_range *first_fn, *last_fn, *cached_fn;
336
337 /* The language we are debugging. */
338 enum language language;
339 const struct language_defn *language_defn;
340
b0f35d58
DL
341 const char *producer;
342
e142c38c
DJ
343 /* The generic symbol table building routines have separate lists for
344 file scope symbols and all all other scopes (local scopes). So
345 we need to select the right one to pass to add_symbol_to_list().
346 We do it by keeping a pointer to the correct list in list_in_scope.
347
348 FIXME: The original dwarf code just treated the file scope as the
349 first local scope, and all other local scopes as nested local
350 scopes, and worked fine. Check to see if we really need to
351 distinguish these in buildsym.c. */
352 struct pending **list_in_scope;
353
f3dd6933
DJ
354 /* DWARF abbreviation table associated with this compilation unit. */
355 struct abbrev_info **dwarf2_abbrevs;
356
357 /* Storage for the abbrev table. */
358 struct obstack abbrev_obstack;
72bf9492
DJ
359
360 /* Hash table holding all the loaded partial DIEs. */
361 htab_t partial_dies;
362
363 /* Storage for things with the same lifetime as this read-in compilation
364 unit, including partial DIEs. */
365 struct obstack comp_unit_obstack;
366
ae038cb0
DJ
367 /* When multiple dwarf2_cu structures are living in memory, this field
368 chains them all together, so that they can be released efficiently.
369 We will probably also want a generation counter so that most-recently-used
370 compilation units are cached... */
371 struct dwarf2_per_cu_data *read_in_chain;
372
373 /* Backchain to our per_cu entry if the tree has been built. */
374 struct dwarf2_per_cu_data *per_cu;
375
376 /* How many compilation units ago was this CU last referenced? */
377 int last_used;
378
10b3939b 379 /* A hash table of die offsets for following references. */
51545339 380 htab_t die_hash;
10b3939b
DJ
381
382 /* Full DIEs if read in. */
383 struct die_info *dies;
384
385 /* A set of pointers to dwarf2_per_cu_data objects for compilation
386 units referenced by this one. Only set during full symbol processing;
387 partial symbol tables do not have dependencies. */
388 htab_t dependencies;
389
cb1df416
DJ
390 /* Header data from the line table, during full symbol processing. */
391 struct line_header *line_header;
392
3da10d80
KS
393 /* A list of methods which need to have physnames computed
394 after all type information has been read. */
395 VEC (delayed_method_info) *method_list;
396
ae038cb0
DJ
397 /* Mark used when releasing cached dies. */
398 unsigned int mark : 1;
399
400 /* This flag will be set if this compilation unit might include
401 inter-compilation-unit references. */
402 unsigned int has_form_ref_addr : 1;
403
72bf9492
DJ
404 /* This flag will be set if this compilation unit includes any
405 DW_TAG_namespace DIEs. If we know that there are explicit
406 DIEs for namespaces, we don't need to try to infer them
407 from mangled names. */
408 unsigned int has_namespace_info : 1;
8be455d7
JK
409
410 /* This CU references .debug_loc. See the symtab->locations_valid field.
411 This test is imperfect as there may exist optimized debug code not using
412 any location list and still facing inlining issues if handled as
413 unoptimized code. For a future better test see GCC PR other/32998. */
414
415 unsigned int has_loclist : 1;
e7c27a73
DJ
416};
417
10b3939b
DJ
418/* Persistent data held for a compilation unit, even when not
419 processing it. We put a pointer to this structure in the
420 read_symtab_private field of the psymtab. If we encounter
421 inter-compilation-unit references, we also maintain a sorted
422 list of all compilation units. */
423
ae038cb0
DJ
424struct dwarf2_per_cu_data
425{
348e048f 426 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 427 bytes should suffice to store the length of any compilation unit
45452591
DE
428 - if it doesn't, GDB will fall over anyway.
429 NOTE: Unlike comp_unit_head.length, this length includes
430 initial_length_size. */
c764a876 431 unsigned int offset;
348e048f 432 unsigned int length : 29;
ae038cb0
DJ
433
434 /* Flag indicating this compilation unit will be read in before
435 any of the current compilation units are processed. */
c764a876 436 unsigned int queued : 1;
ae038cb0 437
5afb4e99
DJ
438 /* This flag will be set if we need to load absolutely all DIEs
439 for this compilation unit, instead of just the ones we think
440 are interesting. It gets set if we look for a DIE in the
441 hash table and don't find it. */
442 unsigned int load_all_dies : 1;
443
348e048f
DE
444 /* Non-zero if this CU is from .debug_types.
445 Otherwise it's from .debug_info. */
446 unsigned int from_debug_types : 1;
447
17ea53c3
JK
448 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
449 of the CU cache it gets reset to NULL again. */
ae038cb0 450 struct dwarf2_cu *cu;
1c379e20 451
9291a0cd
TT
452 /* The corresponding objfile. */
453 struct objfile *objfile;
454
455 /* When using partial symbol tables, the 'psymtab' field is active.
456 Otherwise the 'quick' field is active. */
457 union
458 {
459 /* The partial symbol table associated with this compilation unit,
460 or NULL for partial units (which do not have an associated
461 symtab). */
462 struct partial_symtab *psymtab;
463
464 /* Data needed by the "quick" functions. */
465 struct dwarf2_per_cu_quick_data *quick;
466 } v;
ae038cb0
DJ
467};
468
348e048f
DE
469/* Entry in the signatured_types hash table. */
470
471struct signatured_type
472{
473 ULONGEST signature;
474
348e048f
DE
475 /* Offset in .debug_types of the type defined by this TU. */
476 unsigned int type_offset;
477
478 /* The CU(/TU) of this type. */
479 struct dwarf2_per_cu_data per_cu;
480};
481
0963b4bd
MS
482/* Struct used to pass misc. parameters to read_die_and_children, et
483 al. which are used for both .debug_info and .debug_types dies.
484 All parameters here are unchanging for the life of the call. This
485 struct exists to abstract away the constant parameters of die
486 reading. */
93311388
DE
487
488struct die_reader_specs
489{
490 /* The bfd of this objfile. */
491 bfd* abfd;
492
493 /* The CU of the DIE we are parsing. */
494 struct dwarf2_cu *cu;
495
496 /* Pointer to start of section buffer.
497 This is either the start of .debug_info or .debug_types. */
498 const gdb_byte *buffer;
499};
500
debd256d
JB
501/* The line number information for a compilation unit (found in the
502 .debug_line section) begins with a "statement program header",
503 which contains the following information. */
504struct line_header
505{
506 unsigned int total_length;
507 unsigned short version;
508 unsigned int header_length;
509 unsigned char minimum_instruction_length;
2dc7f7b3 510 unsigned char maximum_ops_per_instruction;
debd256d
JB
511 unsigned char default_is_stmt;
512 int line_base;
513 unsigned char line_range;
514 unsigned char opcode_base;
515
516 /* standard_opcode_lengths[i] is the number of operands for the
517 standard opcode whose value is i. This means that
518 standard_opcode_lengths[0] is unused, and the last meaningful
519 element is standard_opcode_lengths[opcode_base - 1]. */
520 unsigned char *standard_opcode_lengths;
521
522 /* The include_directories table. NOTE! These strings are not
523 allocated with xmalloc; instead, they are pointers into
524 debug_line_buffer. If you try to free them, `free' will get
525 indigestion. */
526 unsigned int num_include_dirs, include_dirs_size;
527 char **include_dirs;
528
529 /* The file_names table. NOTE! These strings are not allocated
530 with xmalloc; instead, they are pointers into debug_line_buffer.
531 Don't try to free them directly. */
532 unsigned int num_file_names, file_names_size;
533 struct file_entry
c906108c 534 {
debd256d
JB
535 char *name;
536 unsigned int dir_index;
537 unsigned int mod_time;
538 unsigned int length;
aaa75496 539 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 540 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
541 } *file_names;
542
543 /* The start and end of the statement program following this
6502dd73 544 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 545 gdb_byte *statement_program_start, *statement_program_end;
debd256d 546};
c906108c
SS
547
548/* When we construct a partial symbol table entry we only
0963b4bd 549 need this much information. */
c906108c
SS
550struct partial_die_info
551 {
72bf9492 552 /* Offset of this DIE. */
c906108c 553 unsigned int offset;
72bf9492
DJ
554
555 /* DWARF-2 tag for this DIE. */
556 ENUM_BITFIELD(dwarf_tag) tag : 16;
557
72bf9492
DJ
558 /* Assorted flags describing the data found in this DIE. */
559 unsigned int has_children : 1;
560 unsigned int is_external : 1;
561 unsigned int is_declaration : 1;
562 unsigned int has_type : 1;
563 unsigned int has_specification : 1;
564 unsigned int has_pc_info : 1;
565
566 /* Flag set if the SCOPE field of this structure has been
567 computed. */
568 unsigned int scope_set : 1;
569
fa4028e9
JB
570 /* Flag set if the DIE has a byte_size attribute. */
571 unsigned int has_byte_size : 1;
572
98bfdba5
PA
573 /* Flag set if any of the DIE's children are template arguments. */
574 unsigned int has_template_arguments : 1;
575
abc72ce4
DE
576 /* Flag set if fixup_partial_die has been called on this die. */
577 unsigned int fixup_called : 1;
578
72bf9492 579 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 580 sometimes a default name for unnamed DIEs. */
c906108c 581 char *name;
72bf9492 582
abc72ce4
DE
583 /* The linkage name, if present. */
584 const char *linkage_name;
585
72bf9492
DJ
586 /* The scope to prepend to our children. This is generally
587 allocated on the comp_unit_obstack, so will disappear
588 when this compilation unit leaves the cache. */
589 char *scope;
590
591 /* The location description associated with this DIE, if any. */
592 struct dwarf_block *locdesc;
593
594 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
595 CORE_ADDR lowpc;
596 CORE_ADDR highpc;
72bf9492 597
93311388 598 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 599 DW_AT_sibling, if any. */
abc72ce4
DE
600 /* NOTE: This member isn't strictly necessary, read_partial_die could
601 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 602 gdb_byte *sibling;
72bf9492
DJ
603
604 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
605 DW_AT_specification (or DW_AT_abstract_origin or
606 DW_AT_extension). */
607 unsigned int spec_offset;
608
609 /* Pointers to this DIE's parent, first child, and next sibling,
610 if any. */
611 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
612 };
613
0963b4bd 614/* This data structure holds the information of an abbrev. */
c906108c
SS
615struct abbrev_info
616 {
617 unsigned int number; /* number identifying abbrev */
618 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
619 unsigned short has_children; /* boolean */
620 unsigned short num_attrs; /* number of attributes */
c906108c
SS
621 struct attr_abbrev *attrs; /* an array of attribute descriptions */
622 struct abbrev_info *next; /* next in chain */
623 };
624
625struct attr_abbrev
626 {
9d25dd43
DE
627 ENUM_BITFIELD(dwarf_attribute) name : 16;
628 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
629 };
630
0963b4bd 631/* Attributes have a name and a value. */
b60c80d6
DJ
632struct attribute
633 {
9d25dd43 634 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
635 ENUM_BITFIELD(dwarf_form) form : 15;
636
637 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
638 field should be in u.str (existing only for DW_STRING) but it is kept
639 here for better struct attribute alignment. */
640 unsigned int string_is_canonical : 1;
641
b60c80d6
DJ
642 union
643 {
644 char *str;
645 struct dwarf_block *blk;
43bbcdc2
PH
646 ULONGEST unsnd;
647 LONGEST snd;
b60c80d6 648 CORE_ADDR addr;
348e048f 649 struct signatured_type *signatured_type;
b60c80d6
DJ
650 }
651 u;
652 };
653
0963b4bd 654/* This data structure holds a complete die structure. */
c906108c
SS
655struct die_info
656 {
76815b17
DE
657 /* DWARF-2 tag for this DIE. */
658 ENUM_BITFIELD(dwarf_tag) tag : 16;
659
660 /* Number of attributes */
98bfdba5
PA
661 unsigned char num_attrs;
662
663 /* True if we're presently building the full type name for the
664 type derived from this DIE. */
665 unsigned char building_fullname : 1;
76815b17
DE
666
667 /* Abbrev number */
668 unsigned int abbrev;
669
93311388 670 /* Offset in .debug_info or .debug_types section. */
76815b17 671 unsigned int offset;
78ba4af6
JB
672
673 /* The dies in a compilation unit form an n-ary tree. PARENT
674 points to this die's parent; CHILD points to the first child of
675 this node; and all the children of a given node are chained
4950bc1c 676 together via their SIBLING fields. */
639d11d3
DC
677 struct die_info *child; /* Its first child, if any. */
678 struct die_info *sibling; /* Its next sibling, if any. */
679 struct die_info *parent; /* Its parent, if any. */
c906108c 680
b60c80d6
DJ
681 /* An array of attributes, with NUM_ATTRS elements. There may be
682 zero, but it's not common and zero-sized arrays are not
683 sufficiently portable C. */
684 struct attribute attrs[1];
c906108c
SS
685 };
686
5fb290d7
DJ
687struct function_range
688{
689 const char *name;
690 CORE_ADDR lowpc, highpc;
691 int seen_line;
692 struct function_range *next;
693};
694
0963b4bd 695/* Get at parts of an attribute structure. */
c906108c
SS
696
697#define DW_STRING(attr) ((attr)->u.str)
8285870a 698#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
699#define DW_UNSND(attr) ((attr)->u.unsnd)
700#define DW_BLOCK(attr) ((attr)->u.blk)
701#define DW_SND(attr) ((attr)->u.snd)
702#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 703#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 704
0963b4bd 705/* Blocks are a bunch of untyped bytes. */
c906108c
SS
706struct dwarf_block
707 {
708 unsigned int size;
fe1b8b76 709 gdb_byte *data;
c906108c
SS
710 };
711
c906108c
SS
712#ifndef ATTR_ALLOC_CHUNK
713#define ATTR_ALLOC_CHUNK 4
714#endif
715
c906108c
SS
716/* Allocate fields for structs, unions and enums in this size. */
717#ifndef DW_FIELD_ALLOC_CHUNK
718#define DW_FIELD_ALLOC_CHUNK 4
719#endif
720
c906108c
SS
721/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
722 but this would require a corresponding change in unpack_field_as_long
723 and friends. */
724static int bits_per_byte = 8;
725
726/* The routines that read and process dies for a C struct or C++ class
727 pass lists of data member fields and lists of member function fields
728 in an instance of a field_info structure, as defined below. */
729struct field_info
c5aa993b 730 {
0963b4bd 731 /* List of data member and baseclasses fields. */
c5aa993b
JM
732 struct nextfield
733 {
734 struct nextfield *next;
735 int accessibility;
736 int virtuality;
737 struct field field;
738 }
7d0ccb61 739 *fields, *baseclasses;
c906108c 740
7d0ccb61 741 /* Number of fields (including baseclasses). */
c5aa993b 742 int nfields;
c906108c 743
c5aa993b
JM
744 /* Number of baseclasses. */
745 int nbaseclasses;
c906108c 746
c5aa993b
JM
747 /* Set if the accesibility of one of the fields is not public. */
748 int non_public_fields;
c906108c 749
c5aa993b
JM
750 /* Member function fields array, entries are allocated in the order they
751 are encountered in the object file. */
752 struct nextfnfield
753 {
754 struct nextfnfield *next;
755 struct fn_field fnfield;
756 }
757 *fnfields;
c906108c 758
c5aa993b
JM
759 /* Member function fieldlist array, contains name of possibly overloaded
760 member function, number of overloaded member functions and a pointer
761 to the head of the member function field chain. */
762 struct fnfieldlist
763 {
764 char *name;
765 int length;
766 struct nextfnfield *head;
767 }
768 *fnfieldlists;
c906108c 769
c5aa993b
JM
770 /* Number of entries in the fnfieldlists array. */
771 int nfnfields;
98751a41
JK
772
773 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
774 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
775 struct typedef_field_list
776 {
777 struct typedef_field field;
778 struct typedef_field_list *next;
779 }
780 *typedef_field_list;
781 unsigned typedef_field_list_count;
c5aa993b 782 };
c906108c 783
10b3939b
DJ
784/* One item on the queue of compilation units to read in full symbols
785 for. */
786struct dwarf2_queue_item
787{
788 struct dwarf2_per_cu_data *per_cu;
789 struct dwarf2_queue_item *next;
790};
791
792/* The current queue. */
793static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
794
ae038cb0
DJ
795/* Loaded secondary compilation units are kept in memory until they
796 have not been referenced for the processing of this many
797 compilation units. Set this to zero to disable caching. Cache
798 sizes of up to at least twenty will improve startup time for
799 typical inter-CU-reference binaries, at an obvious memory cost. */
800static int dwarf2_max_cache_age = 5;
920d2a44
AC
801static void
802show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
803 struct cmd_list_element *c, const char *value)
804{
3e43a32a
MS
805 fprintf_filtered (file, _("The upper bound on the age of cached "
806 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
807 value);
808}
809
ae038cb0 810
0963b4bd 811/* Various complaints about symbol reading that don't abort the process. */
c906108c 812
4d3c2250
KB
813static void
814dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 815{
4d3c2250 816 complaint (&symfile_complaints,
e2e0b3e5 817 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
818}
819
25e43795
DJ
820static void
821dwarf2_debug_line_missing_file_complaint (void)
822{
823 complaint (&symfile_complaints,
824 _(".debug_line section has line data without a file"));
825}
826
59205f5a
JB
827static void
828dwarf2_debug_line_missing_end_sequence_complaint (void)
829{
830 complaint (&symfile_complaints,
3e43a32a
MS
831 _(".debug_line section has line "
832 "program sequence without an end"));
59205f5a
JB
833}
834
4d3c2250
KB
835static void
836dwarf2_complex_location_expr_complaint (void)
2e276125 837{
e2e0b3e5 838 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
839}
840
4d3c2250
KB
841static void
842dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
843 int arg3)
2e276125 844{
4d3c2250 845 complaint (&symfile_complaints,
3e43a32a
MS
846 _("const value length mismatch for '%s', got %d, expected %d"),
847 arg1, arg2, arg3);
4d3c2250
KB
848}
849
850static void
851dwarf2_macros_too_long_complaint (void)
2e276125 852{
4d3c2250 853 complaint (&symfile_complaints,
e2e0b3e5 854 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
855}
856
857static void
858dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 859{
4d3c2250 860 complaint (&symfile_complaints,
3e43a32a
MS
861 _("macro debug info contains a "
862 "malformed macro definition:\n`%s'"),
4d3c2250
KB
863 arg1);
864}
865
866static void
867dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 868{
4d3c2250 869 complaint (&symfile_complaints,
3e43a32a
MS
870 _("invalid attribute class or form for '%s' in '%s'"),
871 arg1, arg2);
4d3c2250 872}
c906108c 873
c906108c
SS
874/* local function prototypes */
875
4efb68b1 876static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 877
aaa75496
JB
878static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
879 struct objfile *);
880
c67a9c90 881static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 882
72bf9492
DJ
883static void scan_partial_symbols (struct partial_die_info *,
884 CORE_ADDR *, CORE_ADDR *,
5734ee8b 885 int, struct dwarf2_cu *);
c906108c 886
72bf9492
DJ
887static void add_partial_symbol (struct partial_die_info *,
888 struct dwarf2_cu *);
63d06c5c 889
72bf9492
DJ
890static void add_partial_namespace (struct partial_die_info *pdi,
891 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 892 int need_pc, struct dwarf2_cu *cu);
63d06c5c 893
5d7cb8df
JK
894static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
895 CORE_ADDR *highpc, int need_pc,
896 struct dwarf2_cu *cu);
897
72bf9492
DJ
898static void add_partial_enumeration (struct partial_die_info *enum_pdi,
899 struct dwarf2_cu *cu);
91c24f0a 900
bc30ff58
JB
901static void add_partial_subprogram (struct partial_die_info *pdi,
902 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 903 int need_pc, struct dwarf2_cu *cu);
bc30ff58 904
fe1b8b76 905static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
906 gdb_byte *buffer, gdb_byte *info_ptr,
907 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 908
a14ed312 909static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 910
a14ed312 911static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 912
e7c27a73 913static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 914
f3dd6933 915static void dwarf2_free_abbrev_table (void *);
c906108c 916
fe1b8b76 917static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 918 struct dwarf2_cu *);
72bf9492 919
57349743 920static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 921 struct dwarf2_cu *);
c906108c 922
93311388
DE
923static struct partial_die_info *load_partial_dies (bfd *,
924 gdb_byte *, gdb_byte *,
925 int, struct dwarf2_cu *);
72bf9492 926
fe1b8b76 927static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
928 struct abbrev_info *abbrev,
929 unsigned int, bfd *,
930 gdb_byte *, gdb_byte *,
931 struct dwarf2_cu *);
c906108c 932
c764a876 933static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 934 struct dwarf2_cu *);
72bf9492
DJ
935
936static void fixup_partial_die (struct partial_die_info *,
937 struct dwarf2_cu *);
938
fe1b8b76
JB
939static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
940 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 941
fe1b8b76
JB
942static gdb_byte *read_attribute_value (struct attribute *, unsigned,
943 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 944
fe1b8b76 945static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 946
fe1b8b76 947static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 948
fe1b8b76 949static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 950
fe1b8b76 951static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 952
93311388 953static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 954
fe1b8b76 955static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 956 unsigned int *);
c906108c 957
c764a876
DE
958static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
959
960static LONGEST read_checked_initial_length_and_offset
961 (bfd *, gdb_byte *, const struct comp_unit_head *,
962 unsigned int *, unsigned int *);
613e1657 963
fe1b8b76 964static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
965 unsigned int *);
966
967static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 968
fe1b8b76 969static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 970
9b1c24c8 971static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 972
fe1b8b76
JB
973static char *read_indirect_string (bfd *, gdb_byte *,
974 const struct comp_unit_head *,
975 unsigned int *);
4bdf3d34 976
fe1b8b76 977static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 978
fe1b8b76 979static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 980
fe1b8b76 981static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 982
e142c38c 983static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 984
e142c38c
DJ
985static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
986 struct dwarf2_cu *);
c906108c 987
348e048f
DE
988static struct attribute *dwarf2_attr_no_follow (struct die_info *,
989 unsigned int,
990 struct dwarf2_cu *);
991
05cf31d1
JB
992static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
993 struct dwarf2_cu *cu);
994
e142c38c 995static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 996
e142c38c 997static struct die_info *die_specification (struct die_info *die,
f2f0e013 998 struct dwarf2_cu **);
63d06c5c 999
debd256d
JB
1000static void free_line_header (struct line_header *lh);
1001
aaa75496
JB
1002static void add_file_name (struct line_header *, char *, unsigned int,
1003 unsigned int, unsigned int);
1004
debd256d
JB
1005static struct line_header *(dwarf_decode_line_header
1006 (unsigned int offset,
e7c27a73 1007 bfd *abfd, struct dwarf2_cu *cu));
debd256d 1008
72b9f47f 1009static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 1010 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1011
72b9f47f 1012static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1013
a14ed312 1014static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1015 struct dwarf2_cu *);
c906108c 1016
34eaf542
TT
1017static struct symbol *new_symbol_full (struct die_info *, struct type *,
1018 struct dwarf2_cu *, struct symbol *);
1019
a14ed312 1020static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1021 struct dwarf2_cu *);
c906108c 1022
98bfdba5
PA
1023static void dwarf2_const_value_attr (struct attribute *attr,
1024 struct type *type,
1025 const char *name,
1026 struct obstack *obstack,
1027 struct dwarf2_cu *cu, long *value,
1028 gdb_byte **bytes,
1029 struct dwarf2_locexpr_baton **baton);
2df3850c 1030
e7c27a73 1031static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1032
b4ba55a1
JB
1033static int need_gnat_info (struct dwarf2_cu *);
1034
3e43a32a
MS
1035static struct type *die_descriptive_type (struct die_info *,
1036 struct dwarf2_cu *);
b4ba55a1
JB
1037
1038static void set_descriptive_type (struct type *, struct die_info *,
1039 struct dwarf2_cu *);
1040
e7c27a73
DJ
1041static struct type *die_containing_type (struct die_info *,
1042 struct dwarf2_cu *);
c906108c 1043
673bfd45
DE
1044static struct type *lookup_die_type (struct die_info *, struct attribute *,
1045 struct dwarf2_cu *);
c906108c 1046
f792889a 1047static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1048
673bfd45
DE
1049static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1050
086ed43d 1051static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1052
6e70227d 1053static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1054 const char *suffix, int physname,
1055 struct dwarf2_cu *cu);
63d06c5c 1056
e7c27a73 1057static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1058
348e048f
DE
1059static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1060
e7c27a73 1061static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1062
e7c27a73 1063static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1064
ff013f42
JK
1065static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1066 struct dwarf2_cu *, struct partial_symtab *);
1067
a14ed312 1068static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1069 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1070 struct partial_symtab *);
c906108c 1071
fae299cd
DC
1072static void get_scope_pc_bounds (struct die_info *,
1073 CORE_ADDR *, CORE_ADDR *,
1074 struct dwarf2_cu *);
1075
801e3a5b
JB
1076static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1077 CORE_ADDR, struct dwarf2_cu *);
1078
a14ed312 1079static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1080 struct dwarf2_cu *);
c906108c 1081
a14ed312 1082static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1083 struct type *, struct dwarf2_cu *);
c906108c 1084
a14ed312 1085static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1086 struct die_info *, struct type *,
e7c27a73 1087 struct dwarf2_cu *);
c906108c 1088
a14ed312 1089static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1090 struct type *,
1091 struct dwarf2_cu *);
c906108c 1092
134d01f1 1093static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1094
e7c27a73 1095static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1096
e7c27a73 1097static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1098
5d7cb8df
JK
1099static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1100
27aa8d6a
SW
1101static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1102
f55ee35c
JK
1103static struct type *read_module_type (struct die_info *die,
1104 struct dwarf2_cu *cu);
1105
38d518c9 1106static const char *namespace_name (struct die_info *die,
e142c38c 1107 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1108
134d01f1 1109static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1110
e7c27a73 1111static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1112
6e70227d 1113static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1114 struct dwarf2_cu *);
1115
93311388 1116static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1117
93311388
DE
1118static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1119 gdb_byte *info_ptr,
d97bc12b
DE
1120 gdb_byte **new_info_ptr,
1121 struct die_info *parent);
1122
93311388
DE
1123static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1124 gdb_byte *info_ptr,
fe1b8b76 1125 gdb_byte **new_info_ptr,
639d11d3
DC
1126 struct die_info *parent);
1127
93311388
DE
1128static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1129 gdb_byte *info_ptr,
fe1b8b76 1130 gdb_byte **new_info_ptr,
639d11d3
DC
1131 struct die_info *parent);
1132
93311388
DE
1133static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1134 struct die_info **, gdb_byte *,
1135 int *);
1136
e7c27a73 1137static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1138
71c25dea
TT
1139static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1140 struct obstack *);
1141
e142c38c 1142static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1143
98bfdba5
PA
1144static const char *dwarf2_full_name (char *name,
1145 struct die_info *die,
1146 struct dwarf2_cu *cu);
1147
e142c38c 1148static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1149 struct dwarf2_cu **);
9219021c 1150
a14ed312 1151static char *dwarf_tag_name (unsigned int);
c906108c 1152
a14ed312 1153static char *dwarf_attr_name (unsigned int);
c906108c 1154
a14ed312 1155static char *dwarf_form_name (unsigned int);
c906108c 1156
a14ed312 1157static char *dwarf_bool_name (unsigned int);
c906108c 1158
a14ed312 1159static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1160
1161#if 0
a14ed312 1162static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1163#endif
1164
f9aca02d 1165static struct die_info *sibling_die (struct die_info *);
c906108c 1166
d97bc12b
DE
1167static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1168
1169static void dump_die_for_error (struct die_info *);
1170
1171static void dump_die_1 (struct ui_file *, int level, int max_level,
1172 struct die_info *);
c906108c 1173
d97bc12b 1174/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1175
51545339 1176static void store_in_ref_table (struct die_info *,
10b3939b 1177 struct dwarf2_cu *);
c906108c 1178
93311388
DE
1179static int is_ref_attr (struct attribute *);
1180
c764a876 1181static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1182
43bbcdc2 1183static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1184
348e048f
DE
1185static struct die_info *follow_die_ref_or_sig (struct die_info *,
1186 struct attribute *,
1187 struct dwarf2_cu **);
1188
10b3939b
DJ
1189static struct die_info *follow_die_ref (struct die_info *,
1190 struct attribute *,
f2f0e013 1191 struct dwarf2_cu **);
c906108c 1192
348e048f
DE
1193static struct die_info *follow_die_sig (struct die_info *,
1194 struct attribute *,
1195 struct dwarf2_cu **);
1196
1197static void read_signatured_type_at_offset (struct objfile *objfile,
1198 unsigned int offset);
1199
1200static void read_signatured_type (struct objfile *,
1201 struct signatured_type *type_sig);
1202
c906108c
SS
1203/* memory allocation interface */
1204
7b5a2f43 1205static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1206
f3dd6933 1207static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1208
b60c80d6 1209static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1210
e142c38c 1211static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1212
e142c38c
DJ
1213static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1214 struct dwarf2_cu *);
5fb290d7 1215
2e276125 1216static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1217 char *, bfd *, struct dwarf2_cu *);
2e276125 1218
8e19ed76
PS
1219static int attr_form_is_block (struct attribute *);
1220
3690dd37
JB
1221static int attr_form_is_section_offset (struct attribute *);
1222
1223static int attr_form_is_constant (struct attribute *);
1224
8cf6f0b1
TT
1225static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1226 struct dwarf2_loclist_baton *baton,
1227 struct attribute *attr);
1228
93e7bd98
DJ
1229static void dwarf2_symbol_mark_computed (struct attribute *attr,
1230 struct symbol *sym,
1231 struct dwarf2_cu *cu);
4c2df51b 1232
93311388
DE
1233static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1234 struct abbrev_info *abbrev,
1235 struct dwarf2_cu *cu);
4bb7a0a7 1236
72bf9492
DJ
1237static void free_stack_comp_unit (void *);
1238
72bf9492
DJ
1239static hashval_t partial_die_hash (const void *item);
1240
1241static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1242
ae038cb0 1243static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1244 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1245
1246static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1247 (unsigned int offset, struct objfile *objfile);
ae038cb0 1248
9816fde3
JK
1249static void init_one_comp_unit (struct dwarf2_cu *cu,
1250 struct objfile *objfile);
1251
1252static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1253 struct die_info *comp_unit_die);
93311388 1254
ae038cb0
DJ
1255static void free_one_comp_unit (void *);
1256
1257static void free_cached_comp_units (void *);
1258
1259static void age_cached_comp_units (void);
1260
1261static void free_one_cached_comp_unit (void *);
1262
f792889a
DJ
1263static struct type *set_die_type (struct die_info *, struct type *,
1264 struct dwarf2_cu *);
1c379e20 1265
ae038cb0
DJ
1266static void create_all_comp_units (struct objfile *);
1267
1fd400ff
TT
1268static int create_debug_types_hash_table (struct objfile *objfile);
1269
93311388
DE
1270static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1271 struct objfile *);
10b3939b
DJ
1272
1273static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1274
1275static void dwarf2_add_dependence (struct dwarf2_cu *,
1276 struct dwarf2_per_cu_data *);
1277
ae038cb0
DJ
1278static void dwarf2_mark (struct dwarf2_cu *);
1279
1280static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1281
673bfd45
DE
1282static struct type *get_die_type_at_offset (unsigned int,
1283 struct dwarf2_per_cu_data *per_cu);
1284
f792889a 1285static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1286
9291a0cd
TT
1287static void dwarf2_release_queue (void *dummy);
1288
1289static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1290 struct objfile *objfile);
1291
1292static void process_queue (struct objfile *objfile);
1293
1294static void find_file_and_directory (struct die_info *die,
1295 struct dwarf2_cu *cu,
1296 char **name, char **comp_dir);
1297
1298static char *file_full_name (int file, struct line_header *lh,
1299 const char *comp_dir);
1300
1301static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1302 gdb_byte *info_ptr,
1303 gdb_byte *buffer,
1304 unsigned int buffer_size,
1305 bfd *abfd);
1306
1307static void init_cu_die_reader (struct die_reader_specs *reader,
1308 struct dwarf2_cu *cu);
1309
673bfd45 1310static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1311
9291a0cd
TT
1312#if WORDS_BIGENDIAN
1313
1314/* Convert VALUE between big- and little-endian. */
1315static offset_type
1316byte_swap (offset_type value)
1317{
1318 offset_type result;
1319
1320 result = (value & 0xff) << 24;
1321 result |= (value & 0xff00) << 8;
1322 result |= (value & 0xff0000) >> 8;
1323 result |= (value & 0xff000000) >> 24;
1324 return result;
1325}
1326
1327#define MAYBE_SWAP(V) byte_swap (V)
1328
1329#else
1330#define MAYBE_SWAP(V) (V)
1331#endif /* WORDS_BIGENDIAN */
1332
1333/* The suffix for an index file. */
1334#define INDEX_SUFFIX ".gdb-index"
1335
3da10d80
KS
1336static const char *dwarf2_physname (char *name, struct die_info *die,
1337 struct dwarf2_cu *cu);
1338
c906108c 1339/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1340 information and return true if we have enough to do something.
1341 NAMES points to the dwarf2 section names, or is NULL if the standard
1342 ELF names are used. */
c906108c
SS
1343
1344int
251d32d9
TG
1345dwarf2_has_info (struct objfile *objfile,
1346 const struct dwarf2_debug_sections *names)
c906108c 1347{
be391dca
TT
1348 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1349 if (!dwarf2_per_objfile)
1350 {
1351 /* Initialize per-objfile state. */
1352 struct dwarf2_per_objfile *data
1353 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1354
be391dca
TT
1355 memset (data, 0, sizeof (*data));
1356 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1357 dwarf2_per_objfile = data;
6502dd73 1358
251d32d9
TG
1359 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1360 (void *) names);
be391dca
TT
1361 dwarf2_per_objfile->objfile = objfile;
1362 }
1363 return (dwarf2_per_objfile->info.asection != NULL
1364 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1365}
1366
251d32d9
TG
1367/* When loading sections, we look either for uncompressed section or for
1368 compressed section names. */
233a11ab
CS
1369
1370static int
251d32d9
TG
1371section_is_p (const char *section_name,
1372 const struct dwarf2_section_names *names)
233a11ab 1373{
251d32d9
TG
1374 if (names->normal != NULL
1375 && strcmp (section_name, names->normal) == 0)
1376 return 1;
1377 if (names->compressed != NULL
1378 && strcmp (section_name, names->compressed) == 0)
1379 return 1;
1380 return 0;
233a11ab
CS
1381}
1382
c906108c
SS
1383/* This function is mapped across the sections and remembers the
1384 offset and size of each of the debugging sections we are interested
1385 in. */
1386
1387static void
251d32d9 1388dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1389{
251d32d9
TG
1390 const struct dwarf2_debug_sections *names;
1391
1392 if (vnames == NULL)
1393 names = &dwarf2_elf_names;
1394 else
1395 names = (const struct dwarf2_debug_sections *) vnames;
1396
1397 if (section_is_p (sectp->name, &names->info))
c906108c 1398 {
dce234bc
PP
1399 dwarf2_per_objfile->info.asection = sectp;
1400 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1401 }
251d32d9 1402 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1403 {
dce234bc
PP
1404 dwarf2_per_objfile->abbrev.asection = sectp;
1405 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1406 }
251d32d9 1407 else if (section_is_p (sectp->name, &names->line))
c906108c 1408 {
dce234bc
PP
1409 dwarf2_per_objfile->line.asection = sectp;
1410 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1411 }
251d32d9 1412 else if (section_is_p (sectp->name, &names->loc))
c906108c 1413 {
dce234bc
PP
1414 dwarf2_per_objfile->loc.asection = sectp;
1415 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1416 }
251d32d9 1417 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1418 {
dce234bc
PP
1419 dwarf2_per_objfile->macinfo.asection = sectp;
1420 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1421 }
251d32d9 1422 else if (section_is_p (sectp->name, &names->str))
c906108c 1423 {
dce234bc
PP
1424 dwarf2_per_objfile->str.asection = sectp;
1425 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1426 }
251d32d9 1427 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1428 {
dce234bc
PP
1429 dwarf2_per_objfile->frame.asection = sectp;
1430 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1431 }
251d32d9 1432 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1433 {
3799ccc6 1434 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1435
3799ccc6
EZ
1436 if (aflag & SEC_HAS_CONTENTS)
1437 {
dce234bc
PP
1438 dwarf2_per_objfile->eh_frame.asection = sectp;
1439 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1440 }
b6af0555 1441 }
251d32d9 1442 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1443 {
dce234bc
PP
1444 dwarf2_per_objfile->ranges.asection = sectp;
1445 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1446 }
251d32d9 1447 else if (section_is_p (sectp->name, &names->types))
348e048f
DE
1448 {
1449 dwarf2_per_objfile->types.asection = sectp;
1450 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1451 }
251d32d9 1452 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1453 {
1454 dwarf2_per_objfile->gdb_index.asection = sectp;
1455 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1456 }
dce234bc 1457
72dca2f5
FR
1458 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1459 && bfd_section_vma (abfd, sectp) == 0)
1460 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1461}
1462
dce234bc
PP
1463/* Decompress a section that was compressed using zlib. Store the
1464 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1465
1466static void
dce234bc
PP
1467zlib_decompress_section (struct objfile *objfile, asection *sectp,
1468 gdb_byte **outbuf, bfd_size_type *outsize)
1469{
1470 bfd *abfd = objfile->obfd;
1471#ifndef HAVE_ZLIB_H
1472 error (_("Support for zlib-compressed DWARF data (from '%s') "
1473 "is disabled in this copy of GDB"),
1474 bfd_get_filename (abfd));
1475#else
1476 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1477 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1478 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1479 bfd_size_type uncompressed_size;
1480 gdb_byte *uncompressed_buffer;
1481 z_stream strm;
1482 int rc;
1483 int header_size = 12;
1484
1485 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1486 || bfd_bread (compressed_buffer,
1487 compressed_size, abfd) != compressed_size)
dce234bc
PP
1488 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1489 bfd_get_filename (abfd));
1490
1491 /* Read the zlib header. In this case, it should be "ZLIB" followed
1492 by the uncompressed section size, 8 bytes in big-endian order. */
1493 if (compressed_size < header_size
1494 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1495 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1496 bfd_get_filename (abfd));
1497 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1498 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1499 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1500 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1501 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1502 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1503 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1504 uncompressed_size += compressed_buffer[11];
1505
1506 /* It is possible the section consists of several compressed
1507 buffers concatenated together, so we uncompress in a loop. */
1508 strm.zalloc = NULL;
1509 strm.zfree = NULL;
1510 strm.opaque = NULL;
1511 strm.avail_in = compressed_size - header_size;
1512 strm.next_in = (Bytef*) compressed_buffer + header_size;
1513 strm.avail_out = uncompressed_size;
1514 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1515 uncompressed_size);
1516 rc = inflateInit (&strm);
1517 while (strm.avail_in > 0)
1518 {
1519 if (rc != Z_OK)
1520 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1521 bfd_get_filename (abfd), rc);
1522 strm.next_out = ((Bytef*) uncompressed_buffer
1523 + (uncompressed_size - strm.avail_out));
1524 rc = inflate (&strm, Z_FINISH);
1525 if (rc != Z_STREAM_END)
1526 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1527 bfd_get_filename (abfd), rc);
1528 rc = inflateReset (&strm);
1529 }
1530 rc = inflateEnd (&strm);
1531 if (rc != Z_OK
1532 || strm.avail_out != 0)
1533 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1534 bfd_get_filename (abfd), rc);
1535
affddf13 1536 do_cleanups (cleanup);
dce234bc
PP
1537 *outbuf = uncompressed_buffer;
1538 *outsize = uncompressed_size;
1539#endif
233a11ab
CS
1540}
1541
9e0ac564
TT
1542/* A helper function that decides whether a section is empty. */
1543
1544static int
1545dwarf2_section_empty_p (struct dwarf2_section_info *info)
1546{
1547 return info->asection == NULL || info->size == 0;
1548}
1549
dce234bc
PP
1550/* Read the contents of the section SECTP from object file specified by
1551 OBJFILE, store info about the section into INFO.
1552 If the section is compressed, uncompress it before returning. */
c906108c 1553
dce234bc
PP
1554static void
1555dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1556{
dce234bc
PP
1557 bfd *abfd = objfile->obfd;
1558 asection *sectp = info->asection;
1559 gdb_byte *buf, *retbuf;
1560 unsigned char header[4];
c906108c 1561
be391dca
TT
1562 if (info->readin)
1563 return;
dce234bc
PP
1564 info->buffer = NULL;
1565 info->was_mmapped = 0;
be391dca 1566 info->readin = 1;
188dd5d6 1567
9e0ac564 1568 if (dwarf2_section_empty_p (info))
dce234bc 1569 return;
c906108c 1570
dce234bc
PP
1571 /* Check if the file has a 4-byte header indicating compression. */
1572 if (info->size > sizeof (header)
1573 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1574 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1575 {
1576 /* Upon decompression, update the buffer and its size. */
1577 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1578 {
1579 zlib_decompress_section (objfile, sectp, &info->buffer,
1580 &info->size);
1581 return;
1582 }
1583 }
4bdf3d34 1584
dce234bc
PP
1585#ifdef HAVE_MMAP
1586 if (pagesize == 0)
1587 pagesize = getpagesize ();
2e276125 1588
dce234bc
PP
1589 /* Only try to mmap sections which are large enough: we don't want to
1590 waste space due to fragmentation. Also, only try mmap for sections
1591 without relocations. */
1592
1593 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1594 {
1595 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1596 size_t map_length = info->size + sectp->filepos - pg_offset;
1597 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1598 MAP_PRIVATE, pg_offset);
1599
1600 if (retbuf != MAP_FAILED)
1601 {
1602 info->was_mmapped = 1;
1603 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1604#if HAVE_POSIX_MADVISE
1605 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1606#endif
dce234bc
PP
1607 return;
1608 }
1609 }
1610#endif
1611
1612 /* If we get here, we are a normal, not-compressed section. */
1613 info->buffer = buf
1614 = obstack_alloc (&objfile->objfile_obstack, info->size);
1615
1616 /* When debugging .o files, we may need to apply relocations; see
1617 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1618 We never compress sections in .o files, so we only need to
1619 try this when the section is not compressed. */
ac8035ab 1620 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1621 if (retbuf != NULL)
1622 {
1623 info->buffer = retbuf;
1624 return;
1625 }
1626
1627 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1628 || bfd_bread (buf, info->size, abfd) != info->size)
1629 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1630 bfd_get_filename (abfd));
1631}
1632
9e0ac564
TT
1633/* A helper function that returns the size of a section in a safe way.
1634 If you are positive that the section has been read before using the
1635 size, then it is safe to refer to the dwarf2_section_info object's
1636 "size" field directly. In other cases, you must call this
1637 function, because for compressed sections the size field is not set
1638 correctly until the section has been read. */
1639
1640static bfd_size_type
1641dwarf2_section_size (struct objfile *objfile,
1642 struct dwarf2_section_info *info)
1643{
1644 if (!info->readin)
1645 dwarf2_read_section (objfile, info);
1646 return info->size;
1647}
1648
dce234bc 1649/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1650 SECTION_NAME. */
af34e669 1651
dce234bc 1652void
3017a003
TG
1653dwarf2_get_section_info (struct objfile *objfile,
1654 enum dwarf2_section_enum sect,
dce234bc
PP
1655 asection **sectp, gdb_byte **bufp,
1656 bfd_size_type *sizep)
1657{
1658 struct dwarf2_per_objfile *data
1659 = objfile_data (objfile, dwarf2_objfile_data_key);
1660 struct dwarf2_section_info *info;
a3b2a86b
TT
1661
1662 /* We may see an objfile without any DWARF, in which case we just
1663 return nothing. */
1664 if (data == NULL)
1665 {
1666 *sectp = NULL;
1667 *bufp = NULL;
1668 *sizep = 0;
1669 return;
1670 }
3017a003
TG
1671 switch (sect)
1672 {
1673 case DWARF2_DEBUG_FRAME:
1674 info = &data->frame;
1675 break;
1676 case DWARF2_EH_FRAME:
1677 info = &data->eh_frame;
1678 break;
1679 default:
1680 gdb_assert_not_reached ("unexpected section");
1681 }
dce234bc 1682
9e0ac564 1683 dwarf2_read_section (objfile, info);
dce234bc
PP
1684
1685 *sectp = info->asection;
1686 *bufp = info->buffer;
1687 *sizep = info->size;
1688}
1689
9291a0cd 1690\f
7b9f3c50
DE
1691/* DWARF quick_symbols_functions support. */
1692
1693/* TUs can share .debug_line entries, and there can be a lot more TUs than
1694 unique line tables, so we maintain a separate table of all .debug_line
1695 derived entries to support the sharing.
1696 All the quick functions need is the list of file names. We discard the
1697 line_header when we're done and don't need to record it here. */
1698struct quick_file_names
1699{
1700 /* The offset in .debug_line of the line table. We hash on this. */
1701 unsigned int offset;
1702
1703 /* The number of entries in file_names, real_names. */
1704 unsigned int num_file_names;
1705
1706 /* The file names from the line table, after being run through
1707 file_full_name. */
1708 const char **file_names;
1709
1710 /* The file names from the line table after being run through
1711 gdb_realpath. These are computed lazily. */
1712 const char **real_names;
1713};
1714
1715/* When using the index (and thus not using psymtabs), each CU has an
1716 object of this type. This is used to hold information needed by
1717 the various "quick" methods. */
1718struct dwarf2_per_cu_quick_data
1719{
1720 /* The file table. This can be NULL if there was no file table
1721 or it's currently not read in.
1722 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1723 struct quick_file_names *file_names;
1724
1725 /* The corresponding symbol table. This is NULL if symbols for this
1726 CU have not yet been read. */
1727 struct symtab *symtab;
1728
1729 /* A temporary mark bit used when iterating over all CUs in
1730 expand_symtabs_matching. */
1731 unsigned int mark : 1;
1732
1733 /* True if we've tried to read the file table and found there isn't one.
1734 There will be no point in trying to read it again next time. */
1735 unsigned int no_file_data : 1;
1736};
1737
1738/* Hash function for a quick_file_names. */
1739
1740static hashval_t
1741hash_file_name_entry (const void *e)
1742{
1743 const struct quick_file_names *file_data = e;
1744
1745 return file_data->offset;
1746}
1747
1748/* Equality function for a quick_file_names. */
1749
1750static int
1751eq_file_name_entry (const void *a, const void *b)
1752{
1753 const struct quick_file_names *ea = a;
1754 const struct quick_file_names *eb = b;
1755
1756 return ea->offset == eb->offset;
1757}
1758
1759/* Delete function for a quick_file_names. */
1760
1761static void
1762delete_file_name_entry (void *e)
1763{
1764 struct quick_file_names *file_data = e;
1765 int i;
1766
1767 for (i = 0; i < file_data->num_file_names; ++i)
1768 {
1769 xfree ((void*) file_data->file_names[i]);
1770 if (file_data->real_names)
1771 xfree ((void*) file_data->real_names[i]);
1772 }
1773
1774 /* The space for the struct itself lives on objfile_obstack,
1775 so we don't free it here. */
1776}
1777
1778/* Create a quick_file_names hash table. */
1779
1780static htab_t
1781create_quick_file_names_table (unsigned int nr_initial_entries)
1782{
1783 return htab_create_alloc (nr_initial_entries,
1784 hash_file_name_entry, eq_file_name_entry,
1785 delete_file_name_entry, xcalloc, xfree);
1786}
9291a0cd
TT
1787
1788/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1789 this CU came. */
2fdf6df6 1790
9291a0cd
TT
1791static void
1792dw2_do_instantiate_symtab (struct objfile *objfile,
1793 struct dwarf2_per_cu_data *per_cu)
1794{
1795 struct cleanup *back_to;
1796
1797 back_to = make_cleanup (dwarf2_release_queue, NULL);
1798
1799 queue_comp_unit (per_cu, objfile);
1800
1801 if (per_cu->from_debug_types)
1802 read_signatured_type_at_offset (objfile, per_cu->offset);
1803 else
1804 load_full_comp_unit (per_cu, objfile);
1805
1806 process_queue (objfile);
1807
1808 /* Age the cache, releasing compilation units that have not
1809 been used recently. */
1810 age_cached_comp_units ();
1811
1812 do_cleanups (back_to);
1813}
1814
1815/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1816 the objfile from which this CU came. Returns the resulting symbol
1817 table. */
2fdf6df6 1818
9291a0cd
TT
1819static struct symtab *
1820dw2_instantiate_symtab (struct objfile *objfile,
1821 struct dwarf2_per_cu_data *per_cu)
1822{
1823 if (!per_cu->v.quick->symtab)
1824 {
1825 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1826 increment_reading_symtab ();
1827 dw2_do_instantiate_symtab (objfile, per_cu);
1828 do_cleanups (back_to);
1829 }
1830 return per_cu->v.quick->symtab;
1831}
1832
1fd400ff 1833/* Return the CU given its index. */
2fdf6df6 1834
1fd400ff
TT
1835static struct dwarf2_per_cu_data *
1836dw2_get_cu (int index)
1837{
1838 if (index >= dwarf2_per_objfile->n_comp_units)
1839 {
1840 index -= dwarf2_per_objfile->n_comp_units;
1841 return dwarf2_per_objfile->type_comp_units[index];
1842 }
1843 return dwarf2_per_objfile->all_comp_units[index];
1844}
1845
9291a0cd
TT
1846/* A helper function that knows how to read a 64-bit value in a way
1847 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1848 otherwise. */
2fdf6df6 1849
9291a0cd
TT
1850static int
1851extract_cu_value (const char *bytes, ULONGEST *result)
1852{
1853 if (sizeof (ULONGEST) < 8)
1854 {
1855 int i;
1856
1857 /* Ignore the upper 4 bytes if they are all zero. */
1858 for (i = 0; i < 4; ++i)
1859 if (bytes[i + 4] != 0)
1860 return 0;
1861
1862 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1863 }
1864 else
1865 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1866 return 1;
1867}
1868
1869/* Read the CU list from the mapped index, and use it to create all
1870 the CU objects for this objfile. Return 0 if something went wrong,
1871 1 if everything went ok. */
2fdf6df6 1872
9291a0cd 1873static int
1fd400ff
TT
1874create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1875 offset_type cu_list_elements)
9291a0cd
TT
1876{
1877 offset_type i;
9291a0cd
TT
1878
1879 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1880 dwarf2_per_objfile->all_comp_units
1881 = obstack_alloc (&objfile->objfile_obstack,
1882 dwarf2_per_objfile->n_comp_units
1883 * sizeof (struct dwarf2_per_cu_data *));
1884
1885 for (i = 0; i < cu_list_elements; i += 2)
1886 {
1887 struct dwarf2_per_cu_data *the_cu;
1888 ULONGEST offset, length;
1889
1890 if (!extract_cu_value (cu_list, &offset)
1891 || !extract_cu_value (cu_list + 8, &length))
1892 return 0;
1893 cu_list += 2 * 8;
1894
1895 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1896 struct dwarf2_per_cu_data);
1897 the_cu->offset = offset;
1898 the_cu->length = length;
1899 the_cu->objfile = objfile;
1900 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1901 struct dwarf2_per_cu_quick_data);
1902 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1903 }
1904
1905 return 1;
1906}
1907
1fd400ff 1908/* Create the signatured type hash table from the index. */
673bfd45 1909
1fd400ff 1910static int
673bfd45
DE
1911create_signatured_type_table_from_index (struct objfile *objfile,
1912 const gdb_byte *bytes,
1913 offset_type elements)
1fd400ff
TT
1914{
1915 offset_type i;
673bfd45 1916 htab_t sig_types_hash;
1fd400ff
TT
1917
1918 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1919 dwarf2_per_objfile->type_comp_units
1920 = obstack_alloc (&objfile->objfile_obstack,
1921 dwarf2_per_objfile->n_type_comp_units
1922 * sizeof (struct dwarf2_per_cu_data *));
1923
673bfd45 1924 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1925
1926 for (i = 0; i < elements; i += 3)
1927 {
1928 struct signatured_type *type_sig;
1929 ULONGEST offset, type_offset, signature;
1930 void **slot;
1931
1932 if (!extract_cu_value (bytes, &offset)
1933 || !extract_cu_value (bytes + 8, &type_offset))
1934 return 0;
1935 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1936 bytes += 3 * 8;
1937
1938 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1939 struct signatured_type);
1940 type_sig->signature = signature;
1fd400ff
TT
1941 type_sig->type_offset = type_offset;
1942 type_sig->per_cu.from_debug_types = 1;
1943 type_sig->per_cu.offset = offset;
1944 type_sig->per_cu.objfile = objfile;
1945 type_sig->per_cu.v.quick
1946 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1947 struct dwarf2_per_cu_quick_data);
1948
673bfd45 1949 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1950 *slot = type_sig;
1951
1952 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1953 }
1954
673bfd45 1955 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1956
1957 return 1;
1958}
1959
9291a0cd
TT
1960/* Read the address map data from the mapped index, and use it to
1961 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1962
9291a0cd
TT
1963static void
1964create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1965{
1966 const gdb_byte *iter, *end;
1967 struct obstack temp_obstack;
1968 struct addrmap *mutable_map;
1969 struct cleanup *cleanup;
1970 CORE_ADDR baseaddr;
1971
1972 obstack_init (&temp_obstack);
1973 cleanup = make_cleanup_obstack_free (&temp_obstack);
1974 mutable_map = addrmap_create_mutable (&temp_obstack);
1975
1976 iter = index->address_table;
1977 end = iter + index->address_table_size;
1978
1979 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1980
1981 while (iter < end)
1982 {
1983 ULONGEST hi, lo, cu_index;
1984 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1985 iter += 8;
1986 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1987 iter += 8;
1988 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1989 iter += 4;
1990
1991 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1992 dw2_get_cu (cu_index));
9291a0cd
TT
1993 }
1994
1995 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1996 &objfile->objfile_obstack);
1997 do_cleanups (cleanup);
1998}
1999
59d7bcaf
JK
2000/* The hash function for strings in the mapped index. This is the same as
2001 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2002 implementation. This is necessary because the hash function is tied to the
2003 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2004 SYMBOL_HASH_NEXT.
2005
2006 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2007
9291a0cd 2008static hashval_t
559a7a62 2009mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2010{
2011 const unsigned char *str = (const unsigned char *) p;
2012 hashval_t r = 0;
2013 unsigned char c;
2014
2015 while ((c = *str++) != 0)
559a7a62
JK
2016 {
2017 if (index_version >= 5)
2018 c = tolower (c);
2019 r = r * 67 + c - 113;
2020 }
9291a0cd
TT
2021
2022 return r;
2023}
2024
2025/* Find a slot in the mapped index INDEX for the object named NAME.
2026 If NAME is found, set *VEC_OUT to point to the CU vector in the
2027 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2028
9291a0cd
TT
2029static int
2030find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2031 offset_type **vec_out)
2032{
0cf03b49
JK
2033 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2034 offset_type hash;
9291a0cd 2035 offset_type slot, step;
559a7a62 2036 int (*cmp) (const char *, const char *);
9291a0cd 2037
0cf03b49
JK
2038 if (current_language->la_language == language_cplus
2039 || current_language->la_language == language_java
2040 || current_language->la_language == language_fortran)
2041 {
2042 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2043 not contain any. */
2044 const char *paren = strchr (name, '(');
2045
2046 if (paren)
2047 {
2048 char *dup;
2049
2050 dup = xmalloc (paren - name + 1);
2051 memcpy (dup, name, paren - name);
2052 dup[paren - name] = 0;
2053
2054 make_cleanup (xfree, dup);
2055 name = dup;
2056 }
2057 }
2058
559a7a62
JK
2059 /* Index version 4 did not support case insensitive searches. But the
2060 indexes for case insensitive languages are built in lowercase, therefore
2061 simulate our NAME being searched is also lowercased. */
2062 hash = mapped_index_string_hash ((index->version == 4
2063 && case_sensitivity == case_sensitive_off
2064 ? 5 : index->version),
2065 name);
2066
3876f04e
DE
2067 slot = hash & (index->symbol_table_slots - 1);
2068 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2069 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2070
2071 for (;;)
2072 {
2073 /* Convert a slot number to an offset into the table. */
2074 offset_type i = 2 * slot;
2075 const char *str;
3876f04e 2076 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2077 {
2078 do_cleanups (back_to);
2079 return 0;
2080 }
9291a0cd 2081
3876f04e 2082 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2083 if (!cmp (name, str))
9291a0cd
TT
2084 {
2085 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2086 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2087 do_cleanups (back_to);
9291a0cd
TT
2088 return 1;
2089 }
2090
3876f04e 2091 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2092 }
2093}
2094
2095/* Read the index file. If everything went ok, initialize the "quick"
2096 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2097
9291a0cd
TT
2098static int
2099dwarf2_read_index (struct objfile *objfile)
2100{
9291a0cd
TT
2101 char *addr;
2102 struct mapped_index *map;
b3b272e1 2103 offset_type *metadata;
ac0b195c
KW
2104 const gdb_byte *cu_list;
2105 const gdb_byte *types_list = NULL;
2106 offset_type version, cu_list_elements;
2107 offset_type types_list_elements = 0;
1fd400ff 2108 int i;
9291a0cd 2109
9e0ac564 2110 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2111 return 0;
82430852
JK
2112
2113 /* Older elfutils strip versions could keep the section in the main
2114 executable while splitting it for the separate debug info file. */
2115 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2116 & SEC_HAS_CONTENTS) == 0)
2117 return 0;
2118
9291a0cd
TT
2119 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2120
2121 addr = dwarf2_per_objfile->gdb_index.buffer;
2122 /* Version check. */
1fd400ff 2123 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2124 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2125 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2126 contained incomplete addrmap. So, it seems better to just ignore such
559a7a62
JK
2127 indices. Index version 4 uses a different hash function than index
2128 version 5 and later. */
831adc1f 2129 if (version < 4)
9291a0cd 2130 return 0;
594e8718
JK
2131 /* Indexes with higher version than the one supported by GDB may be no
2132 longer backward compatible. */
559a7a62 2133 if (version > 5)
594e8718 2134 return 0;
9291a0cd
TT
2135
2136 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2137 map->version = version;
b3b272e1 2138 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2139
2140 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2141
2142 i = 0;
2143 cu_list = addr + MAYBE_SWAP (metadata[i]);
2144 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2145 / 8);
1fd400ff
TT
2146 ++i;
2147
987d643c
TT
2148 types_list = addr + MAYBE_SWAP (metadata[i]);
2149 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2150 - MAYBE_SWAP (metadata[i]))
2151 / 8);
2152 ++i;
1fd400ff
TT
2153
2154 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2155 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2156 - MAYBE_SWAP (metadata[i]));
2157 ++i;
2158
3876f04e
DE
2159 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2160 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2161 - MAYBE_SWAP (metadata[i]))
2162 / (2 * sizeof (offset_type)));
1fd400ff 2163 ++i;
9291a0cd 2164
1fd400ff
TT
2165 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2166
2167 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2168 return 0;
2169
987d643c 2170 if (types_list_elements
673bfd45
DE
2171 && !create_signatured_type_table_from_index (objfile, types_list,
2172 types_list_elements))
9291a0cd
TT
2173 return 0;
2174
2175 create_addrmap_from_index (objfile, map);
2176
2177 dwarf2_per_objfile->index_table = map;
2178 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2179 dwarf2_per_objfile->quick_file_names_table =
2180 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2181
2182 return 1;
2183}
2184
2185/* A helper for the "quick" functions which sets the global
2186 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2187
9291a0cd
TT
2188static void
2189dw2_setup (struct objfile *objfile)
2190{
2191 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2192 gdb_assert (dwarf2_per_objfile);
2193}
2194
2195/* A helper for the "quick" functions which attempts to read the line
2196 table for THIS_CU. */
2fdf6df6 2197
7b9f3c50
DE
2198static struct quick_file_names *
2199dw2_get_file_names (struct objfile *objfile,
2200 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2201{
2202 bfd *abfd = objfile->obfd;
7b9f3c50 2203 struct line_header *lh;
9291a0cd
TT
2204 struct attribute *attr;
2205 struct cleanup *cleanups;
2206 struct die_info *comp_unit_die;
36374493 2207 struct dwarf2_section_info* sec;
9291a0cd
TT
2208 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2209 int has_children, i;
2210 struct dwarf2_cu cu;
2211 unsigned int bytes_read, buffer_size;
2212 struct die_reader_specs reader_specs;
2213 char *name, *comp_dir;
7b9f3c50
DE
2214 void **slot;
2215 struct quick_file_names *qfn;
2216 unsigned int line_offset;
9291a0cd 2217
7b9f3c50
DE
2218 if (this_cu->v.quick->file_names != NULL)
2219 return this_cu->v.quick->file_names;
2220 /* If we know there is no line data, no point in looking again. */
2221 if (this_cu->v.quick->no_file_data)
2222 return NULL;
9291a0cd 2223
9816fde3 2224 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2225 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2226
36374493
DE
2227 if (this_cu->from_debug_types)
2228 sec = &dwarf2_per_objfile->types;
2229 else
2230 sec = &dwarf2_per_objfile->info;
2231 dwarf2_read_section (objfile, sec);
2232 buffer_size = sec->size;
2233 buffer = sec->buffer;
9291a0cd
TT
2234 info_ptr = buffer + this_cu->offset;
2235 beg_of_comp_unit = info_ptr;
2236
2237 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2238 buffer, buffer_size,
2239 abfd);
2240
2241 /* Complete the cu_header. */
2242 cu.header.offset = beg_of_comp_unit - buffer;
2243 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2244
2245 this_cu->cu = &cu;
2246 cu.per_cu = this_cu;
2247
2248 dwarf2_read_abbrevs (abfd, &cu);
2249 make_cleanup (dwarf2_free_abbrev_table, &cu);
2250
2251 if (this_cu->from_debug_types)
2252 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2253 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2254 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2255 &has_children);
9291a0cd 2256
7b9f3c50
DE
2257 lh = NULL;
2258 slot = NULL;
2259 line_offset = 0;
9291a0cd
TT
2260 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2261 if (attr)
2262 {
7b9f3c50
DE
2263 struct quick_file_names find_entry;
2264
2265 line_offset = DW_UNSND (attr);
2266
2267 /* We may have already read in this line header (TU line header sharing).
2268 If we have we're done. */
2269 find_entry.offset = line_offset;
2270 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2271 &find_entry, INSERT);
2272 if (*slot != NULL)
2273 {
2274 do_cleanups (cleanups);
2275 this_cu->v.quick->file_names = *slot;
2276 return *slot;
2277 }
2278
9291a0cd
TT
2279 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2280 }
2281 if (lh == NULL)
2282 {
2283 do_cleanups (cleanups);
7b9f3c50
DE
2284 this_cu->v.quick->no_file_data = 1;
2285 return NULL;
9291a0cd
TT
2286 }
2287
7b9f3c50
DE
2288 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2289 qfn->offset = line_offset;
2290 gdb_assert (slot != NULL);
2291 *slot = qfn;
9291a0cd 2292
7b9f3c50 2293 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2294
7b9f3c50
DE
2295 qfn->num_file_names = lh->num_file_names;
2296 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2297 lh->num_file_names * sizeof (char *));
9291a0cd 2298 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2299 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2300 qfn->real_names = NULL;
9291a0cd 2301
7b9f3c50 2302 free_line_header (lh);
9291a0cd 2303 do_cleanups (cleanups);
7b9f3c50
DE
2304
2305 this_cu->v.quick->file_names = qfn;
2306 return qfn;
9291a0cd
TT
2307}
2308
2309/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2310 real path for a given file name from the line table. */
2fdf6df6 2311
9291a0cd 2312static const char *
7b9f3c50
DE
2313dw2_get_real_path (struct objfile *objfile,
2314 struct quick_file_names *qfn, int index)
9291a0cd 2315{
7b9f3c50
DE
2316 if (qfn->real_names == NULL)
2317 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2318 qfn->num_file_names, sizeof (char *));
9291a0cd 2319
7b9f3c50
DE
2320 if (qfn->real_names[index] == NULL)
2321 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2322
7b9f3c50 2323 return qfn->real_names[index];
9291a0cd
TT
2324}
2325
2326static struct symtab *
2327dw2_find_last_source_symtab (struct objfile *objfile)
2328{
2329 int index;
ae2de4f8 2330
9291a0cd
TT
2331 dw2_setup (objfile);
2332 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2333 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2334}
2335
7b9f3c50
DE
2336/* Traversal function for dw2_forget_cached_source_info. */
2337
2338static int
2339dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2340{
7b9f3c50 2341 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2342
7b9f3c50 2343 if (file_data->real_names)
9291a0cd 2344 {
7b9f3c50 2345 int i;
9291a0cd 2346
7b9f3c50 2347 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2348 {
7b9f3c50
DE
2349 xfree ((void*) file_data->real_names[i]);
2350 file_data->real_names[i] = NULL;
9291a0cd
TT
2351 }
2352 }
7b9f3c50
DE
2353
2354 return 1;
2355}
2356
2357static void
2358dw2_forget_cached_source_info (struct objfile *objfile)
2359{
2360 dw2_setup (objfile);
2361
2362 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2363 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2364}
2365
2366static int
2367dw2_lookup_symtab (struct objfile *objfile, const char *name,
2368 const char *full_path, const char *real_path,
2369 struct symtab **result)
2370{
2371 int i;
2372 int check_basename = lbasename (name) == name;
2373 struct dwarf2_per_cu_data *base_cu = NULL;
2374
2375 dw2_setup (objfile);
ae2de4f8 2376
1fd400ff
TT
2377 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2378 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2379 {
2380 int j;
e254ef6a 2381 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2382 struct quick_file_names *file_data;
9291a0cd 2383
e254ef6a 2384 if (per_cu->v.quick->symtab)
9291a0cd
TT
2385 continue;
2386
7b9f3c50
DE
2387 file_data = dw2_get_file_names (objfile, per_cu);
2388 if (file_data == NULL)
9291a0cd
TT
2389 continue;
2390
7b9f3c50 2391 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2392 {
7b9f3c50 2393 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2394
2395 if (FILENAME_CMP (name, this_name) == 0)
2396 {
e254ef6a 2397 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2398 return 1;
2399 }
2400
2401 if (check_basename && ! base_cu
2402 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2403 base_cu = per_cu;
9291a0cd
TT
2404
2405 if (full_path != NULL)
2406 {
7b9f3c50
DE
2407 const char *this_real_name = dw2_get_real_path (objfile,
2408 file_data, j);
9291a0cd 2409
7b9f3c50
DE
2410 if (this_real_name != NULL
2411 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2412 {
e254ef6a 2413 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2414 return 1;
2415 }
2416 }
2417
2418 if (real_path != NULL)
2419 {
7b9f3c50
DE
2420 const char *this_real_name = dw2_get_real_path (objfile,
2421 file_data, j);
9291a0cd 2422
7b9f3c50
DE
2423 if (this_real_name != NULL
2424 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2425 {
74dd2ca6
DE
2426 *result = dw2_instantiate_symtab (objfile, per_cu);
2427 return 1;
9291a0cd
TT
2428 }
2429 }
2430 }
2431 }
2432
2433 if (base_cu)
2434 {
2435 *result = dw2_instantiate_symtab (objfile, base_cu);
2436 return 1;
2437 }
2438
2439 return 0;
2440}
2441
2442static struct symtab *
2443dw2_lookup_symbol (struct objfile *objfile, int block_index,
2444 const char *name, domain_enum domain)
2445{
774b6a14 2446 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2447 instead. */
2448 return NULL;
2449}
2450
2451/* A helper function that expands all symtabs that hold an object
2452 named NAME. */
2fdf6df6 2453
9291a0cd
TT
2454static void
2455dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2456{
2457 dw2_setup (objfile);
2458
ae2de4f8 2459 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2460 if (dwarf2_per_objfile->index_table)
2461 {
2462 offset_type *vec;
2463
2464 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2465 name, &vec))
2466 {
2467 offset_type i, len = MAYBE_SWAP (*vec);
2468 for (i = 0; i < len; ++i)
2469 {
2470 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2471 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2472
e254ef6a 2473 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2474 }
2475 }
2476 }
2477}
2478
774b6a14
TT
2479static void
2480dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2481 enum block_enum block_kind, const char *name,
774b6a14 2482 domain_enum domain)
9291a0cd 2483{
774b6a14 2484 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2485}
2486
2487static void
2488dw2_print_stats (struct objfile *objfile)
2489{
2490 int i, count;
2491
2492 dw2_setup (objfile);
2493 count = 0;
1fd400ff
TT
2494 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2495 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2496 {
e254ef6a 2497 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2498
e254ef6a 2499 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2500 ++count;
2501 }
2502 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2503}
2504
2505static void
2506dw2_dump (struct objfile *objfile)
2507{
2508 /* Nothing worth printing. */
2509}
2510
2511static void
2512dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2513 struct section_offsets *delta)
2514{
2515 /* There's nothing to relocate here. */
2516}
2517
2518static void
2519dw2_expand_symtabs_for_function (struct objfile *objfile,
2520 const char *func_name)
2521{
2522 dw2_do_expand_symtabs_matching (objfile, func_name);
2523}
2524
2525static void
2526dw2_expand_all_symtabs (struct objfile *objfile)
2527{
2528 int i;
2529
2530 dw2_setup (objfile);
1fd400ff
TT
2531
2532 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2533 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2534 {
e254ef6a 2535 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2536
e254ef6a 2537 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2538 }
2539}
2540
2541static void
2542dw2_expand_symtabs_with_filename (struct objfile *objfile,
2543 const char *filename)
2544{
2545 int i;
2546
2547 dw2_setup (objfile);
d4637a04
DE
2548
2549 /* We don't need to consider type units here.
2550 This is only called for examining code, e.g. expand_line_sal.
2551 There can be an order of magnitude (or more) more type units
2552 than comp units, and we avoid them if we can. */
2553
2554 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2555 {
2556 int j;
e254ef6a 2557 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2558 struct quick_file_names *file_data;
9291a0cd 2559
e254ef6a 2560 if (per_cu->v.quick->symtab)
9291a0cd
TT
2561 continue;
2562
7b9f3c50
DE
2563 file_data = dw2_get_file_names (objfile, per_cu);
2564 if (file_data == NULL)
9291a0cd
TT
2565 continue;
2566
7b9f3c50 2567 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2568 {
7b9f3c50 2569 const char *this_name = file_data->file_names[j];
1ef75ecc 2570 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2571 {
e254ef6a 2572 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2573 break;
2574 }
2575 }
2576 }
2577}
2578
dd786858 2579static const char *
9291a0cd
TT
2580dw2_find_symbol_file (struct objfile *objfile, const char *name)
2581{
e254ef6a 2582 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2583 offset_type *vec;
7b9f3c50 2584 struct quick_file_names *file_data;
9291a0cd
TT
2585
2586 dw2_setup (objfile);
2587
ae2de4f8 2588 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2589 if (!dwarf2_per_objfile->index_table)
2590 return NULL;
2591
2592 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2593 name, &vec))
2594 return NULL;
2595
2596 /* Note that this just looks at the very first one named NAME -- but
2597 actually we are looking for a function. find_main_filename
2598 should be rewritten so that it doesn't require a custom hook. It
2599 could just use the ordinary symbol tables. */
2600 /* vec[0] is the length, which must always be >0. */
e254ef6a 2601 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2602
7b9f3c50
DE
2603 file_data = dw2_get_file_names (objfile, per_cu);
2604 if (file_data == NULL)
9291a0cd
TT
2605 return NULL;
2606
7b9f3c50 2607 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2608}
2609
2610static void
40658b94
PH
2611dw2_map_matching_symbols (const char * name, domain_enum namespace,
2612 struct objfile *objfile, int global,
2613 int (*callback) (struct block *,
2614 struct symbol *, void *),
2edb89d3
JK
2615 void *data, symbol_compare_ftype *match,
2616 symbol_compare_ftype *ordered_compare)
9291a0cd 2617{
40658b94 2618 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2619 current language is Ada for a non-Ada objfile using GNU index. As Ada
2620 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2621}
2622
2623static void
2624dw2_expand_symtabs_matching (struct objfile *objfile,
2625 int (*file_matcher) (const char *, void *),
2626 int (*name_matcher) (const char *, void *),
8903c50d 2627 enum search_domain kind,
9291a0cd
TT
2628 void *data)
2629{
2630 int i;
2631 offset_type iter;
4b5246aa 2632 struct mapped_index *index;
9291a0cd
TT
2633
2634 dw2_setup (objfile);
ae2de4f8
DE
2635
2636 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2637 if (!dwarf2_per_objfile->index_table)
2638 return;
4b5246aa 2639 index = dwarf2_per_objfile->index_table;
9291a0cd 2640
7b08b9eb
JK
2641 if (file_matcher != NULL)
2642 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2643 + dwarf2_per_objfile->n_type_comp_units); ++i)
2644 {
2645 int j;
2646 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2647 struct quick_file_names *file_data;
2648
2649 per_cu->v.quick->mark = 0;
2650 if (per_cu->v.quick->symtab)
2651 continue;
2652
2653 file_data = dw2_get_file_names (objfile, per_cu);
2654 if (file_data == NULL)
2655 continue;
2656
2657 for (j = 0; j < file_data->num_file_names; ++j)
2658 {
2659 if (file_matcher (file_data->file_names[j], data))
2660 {
2661 per_cu->v.quick->mark = 1;
2662 break;
2663 }
2664 }
2665 }
9291a0cd 2666
3876f04e 2667 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2668 {
2669 offset_type idx = 2 * iter;
2670 const char *name;
2671 offset_type *vec, vec_len, vec_idx;
2672
3876f04e 2673 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2674 continue;
2675
3876f04e 2676 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2677
2678 if (! (*name_matcher) (name, data))
2679 continue;
2680
2681 /* The name was matched, now expand corresponding CUs that were
2682 marked. */
4b5246aa 2683 vec = (offset_type *) (index->constant_pool
3876f04e 2684 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2685 vec_len = MAYBE_SWAP (vec[0]);
2686 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2687 {
e254ef6a 2688 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2689
e254ef6a 2690 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2691 if (file_matcher == NULL || per_cu->v.quick->mark)
e254ef6a 2692 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2693 }
2694 }
2695}
2696
2697static struct symtab *
2698dw2_find_pc_sect_symtab (struct objfile *objfile,
2699 struct minimal_symbol *msymbol,
2700 CORE_ADDR pc,
2701 struct obj_section *section,
2702 int warn_if_readin)
2703{
2704 struct dwarf2_per_cu_data *data;
2705
2706 dw2_setup (objfile);
2707
2708 if (!objfile->psymtabs_addrmap)
2709 return NULL;
2710
2711 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2712 if (!data)
2713 return NULL;
2714
2715 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2716 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2717 paddress (get_objfile_arch (objfile), pc));
2718
2719 return dw2_instantiate_symtab (objfile, data);
2720}
2721
9291a0cd 2722static void
44b13c5a 2723dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
9291a0cd
TT
2724 void *data)
2725{
2726 int i;
2727
2728 dw2_setup (objfile);
ae2de4f8 2729
1fd400ff
TT
2730 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2731 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2732 {
2733 int j;
e254ef6a 2734 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2735 struct quick_file_names *file_data;
9291a0cd 2736
e254ef6a 2737 if (per_cu->v.quick->symtab)
9291a0cd
TT
2738 continue;
2739
7b9f3c50
DE
2740 file_data = dw2_get_file_names (objfile, per_cu);
2741 if (file_data == NULL)
9291a0cd
TT
2742 continue;
2743
7b9f3c50 2744 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2745 {
7b9f3c50
DE
2746 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2747 j);
2748 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2749 }
2750 }
2751}
2752
2753static int
2754dw2_has_symbols (struct objfile *objfile)
2755{
2756 return 1;
2757}
2758
2759const struct quick_symbol_functions dwarf2_gdb_index_functions =
2760{
2761 dw2_has_symbols,
2762 dw2_find_last_source_symtab,
2763 dw2_forget_cached_source_info,
2764 dw2_lookup_symtab,
2765 dw2_lookup_symbol,
774b6a14 2766 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2767 dw2_print_stats,
2768 dw2_dump,
2769 dw2_relocate,
2770 dw2_expand_symtabs_for_function,
2771 dw2_expand_all_symtabs,
2772 dw2_expand_symtabs_with_filename,
2773 dw2_find_symbol_file,
40658b94 2774 dw2_map_matching_symbols,
9291a0cd
TT
2775 dw2_expand_symtabs_matching,
2776 dw2_find_pc_sect_symtab,
9291a0cd
TT
2777 dw2_map_symbol_filenames
2778};
2779
2780/* Initialize for reading DWARF for this objfile. Return 0 if this
2781 file will use psymtabs, or 1 if using the GNU index. */
2782
2783int
2784dwarf2_initialize_objfile (struct objfile *objfile)
2785{
2786 /* If we're about to read full symbols, don't bother with the
2787 indices. In this case we also don't care if some other debug
2788 format is making psymtabs, because they are all about to be
2789 expanded anyway. */
2790 if ((objfile->flags & OBJF_READNOW))
2791 {
2792 int i;
2793
2794 dwarf2_per_objfile->using_index = 1;
2795 create_all_comp_units (objfile);
1fd400ff 2796 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2797 dwarf2_per_objfile->quick_file_names_table =
2798 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2799
1fd400ff
TT
2800 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2801 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2802 {
e254ef6a 2803 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2804
e254ef6a
DE
2805 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2806 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2807 }
2808
2809 /* Return 1 so that gdb sees the "quick" functions. However,
2810 these functions will be no-ops because we will have expanded
2811 all symtabs. */
2812 return 1;
2813 }
2814
2815 if (dwarf2_read_index (objfile))
2816 return 1;
2817
9291a0cd
TT
2818 return 0;
2819}
2820
2821\f
2822
dce234bc
PP
2823/* Build a partial symbol table. */
2824
2825void
f29dff0a 2826dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2827{
f29dff0a 2828 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2829 {
2830 init_psymbol_list (objfile, 1024);
2831 }
2832
d146bf1e 2833 dwarf2_build_psymtabs_hard (objfile);
c906108c 2834}
c906108c 2835
45452591
DE
2836/* Return TRUE if OFFSET is within CU_HEADER. */
2837
2838static inline int
2839offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2840{
2841 unsigned int bottom = cu_header->offset;
2842 unsigned int top = (cu_header->offset
2843 + cu_header->length
2844 + cu_header->initial_length_size);
9a619af0 2845
45452591
DE
2846 return (offset >= bottom && offset < top);
2847}
2848
93311388
DE
2849/* Read in the comp unit header information from the debug_info at info_ptr.
2850 NOTE: This leaves members offset, first_die_offset to be filled in
2851 by the caller. */
107d2387 2852
fe1b8b76 2853static gdb_byte *
107d2387 2854read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2855 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2856{
2857 int signed_addr;
891d2f0b 2858 unsigned int bytes_read;
c764a876
DE
2859
2860 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2861 cu_header->initial_length_size = bytes_read;
2862 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2863 info_ptr += bytes_read;
107d2387
AC
2864 cu_header->version = read_2_bytes (abfd, info_ptr);
2865 info_ptr += 2;
613e1657 2866 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2867 &bytes_read);
613e1657 2868 info_ptr += bytes_read;
107d2387
AC
2869 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2870 info_ptr += 1;
2871 signed_addr = bfd_get_sign_extend_vma (abfd);
2872 if (signed_addr < 0)
8e65ff28 2873 internal_error (__FILE__, __LINE__,
e2e0b3e5 2874 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2875 cu_header->signed_addr_p = signed_addr;
c764a876 2876
107d2387
AC
2877 return info_ptr;
2878}
2879
fe1b8b76
JB
2880static gdb_byte *
2881partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2882 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2883 bfd *abfd)
2884{
fe1b8b76 2885 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2886
2887 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2888
2dc7f7b3 2889 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2890 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2891 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2892 bfd_get_filename (abfd));
72bf9492 2893
9e0ac564
TT
2894 if (header->abbrev_offset
2895 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2896 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
2897 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2898 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2899 (long) header->abbrev_offset,
93311388 2900 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2901 bfd_get_filename (abfd));
2902
2903 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2904 > buffer + buffer_size)
8a3fe4f8
AC
2905 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2906 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2907 (long) header->length,
93311388 2908 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2909 bfd_get_filename (abfd));
2910
2911 return info_ptr;
2912}
2913
348e048f
DE
2914/* Read in the types comp unit header information from .debug_types entry at
2915 types_ptr. The result is a pointer to one past the end of the header. */
2916
2917static gdb_byte *
2918read_type_comp_unit_head (struct comp_unit_head *cu_header,
2919 ULONGEST *signature,
2920 gdb_byte *types_ptr, bfd *abfd)
2921{
348e048f
DE
2922 gdb_byte *initial_types_ptr = types_ptr;
2923
6e70227d 2924 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2925 &dwarf2_per_objfile->types);
348e048f
DE
2926 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2927
2928 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2929
2930 *signature = read_8_bytes (abfd, types_ptr);
2931 types_ptr += 8;
2932 types_ptr += cu_header->offset_size;
2933 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2934
2935 return types_ptr;
2936}
2937
aaa75496
JB
2938/* Allocate a new partial symtab for file named NAME and mark this new
2939 partial symtab as being an include of PST. */
2940
2941static void
2942dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2943 struct objfile *objfile)
2944{
2945 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2946
2947 subpst->section_offsets = pst->section_offsets;
2948 subpst->textlow = 0;
2949 subpst->texthigh = 0;
2950
2951 subpst->dependencies = (struct partial_symtab **)
2952 obstack_alloc (&objfile->objfile_obstack,
2953 sizeof (struct partial_symtab *));
2954 subpst->dependencies[0] = pst;
2955 subpst->number_of_dependencies = 1;
2956
2957 subpst->globals_offset = 0;
2958 subpst->n_global_syms = 0;
2959 subpst->statics_offset = 0;
2960 subpst->n_static_syms = 0;
2961 subpst->symtab = NULL;
2962 subpst->read_symtab = pst->read_symtab;
2963 subpst->readin = 0;
2964
2965 /* No private part is necessary for include psymtabs. This property
2966 can be used to differentiate between such include psymtabs and
10b3939b 2967 the regular ones. */
58a9656e 2968 subpst->read_symtab_private = NULL;
aaa75496
JB
2969}
2970
2971/* Read the Line Number Program data and extract the list of files
2972 included by the source file represented by PST. Build an include
d85a05f0 2973 partial symtab for each of these included files. */
aaa75496
JB
2974
2975static void
2976dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2977 struct die_info *die,
aaa75496
JB
2978 struct partial_symtab *pst)
2979{
2980 struct objfile *objfile = cu->objfile;
2981 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2982 struct line_header *lh = NULL;
2983 struct attribute *attr;
aaa75496 2984
d85a05f0
DJ
2985 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2986 if (attr)
2987 {
2988 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2989
d85a05f0
DJ
2990 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2991 }
aaa75496
JB
2992 if (lh == NULL)
2993 return; /* No linetable, so no includes. */
2994
c6da4cef
DE
2995 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2996 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2997
2998 free_line_header (lh);
2999}
3000
348e048f
DE
3001static hashval_t
3002hash_type_signature (const void *item)
3003{
3004 const struct signatured_type *type_sig = item;
9a619af0 3005
348e048f
DE
3006 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3007 return type_sig->signature;
3008}
3009
3010static int
3011eq_type_signature (const void *item_lhs, const void *item_rhs)
3012{
3013 const struct signatured_type *lhs = item_lhs;
3014 const struct signatured_type *rhs = item_rhs;
9a619af0 3015
348e048f
DE
3016 return lhs->signature == rhs->signature;
3017}
3018
1fd400ff
TT
3019/* Allocate a hash table for signatured types. */
3020
3021static htab_t
673bfd45 3022allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3023{
3024 return htab_create_alloc_ex (41,
3025 hash_type_signature,
3026 eq_type_signature,
3027 NULL,
3028 &objfile->objfile_obstack,
3029 hashtab_obstack_allocate,
3030 dummy_obstack_deallocate);
3031}
3032
3033/* A helper function to add a signatured type CU to a list. */
3034
3035static int
3036add_signatured_type_cu_to_list (void **slot, void *datum)
3037{
3038 struct signatured_type *sigt = *slot;
3039 struct dwarf2_per_cu_data ***datap = datum;
3040
3041 **datap = &sigt->per_cu;
3042 ++*datap;
3043
3044 return 1;
3045}
3046
348e048f
DE
3047/* Create the hash table of all entries in the .debug_types section.
3048 The result is zero if there is an error (e.g. missing .debug_types section),
3049 otherwise non-zero. */
3050
3051static int
3052create_debug_types_hash_table (struct objfile *objfile)
3053{
be391dca 3054 gdb_byte *info_ptr;
348e048f 3055 htab_t types_htab;
1fd400ff 3056 struct dwarf2_per_cu_data **iter;
348e048f 3057
be391dca
TT
3058 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3059 info_ptr = dwarf2_per_objfile->types.buffer;
3060
348e048f
DE
3061 if (info_ptr == NULL)
3062 {
3063 dwarf2_per_objfile->signatured_types = NULL;
3064 return 0;
3065 }
3066
673bfd45 3067 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
3068
3069 if (dwarf2_die_debug)
3070 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3071
3e43a32a
MS
3072 while (info_ptr < dwarf2_per_objfile->types.buffer
3073 + dwarf2_per_objfile->types.size)
348e048f
DE
3074 {
3075 unsigned int offset;
3076 unsigned int offset_size;
3077 unsigned int type_offset;
3078 unsigned int length, initial_length_size;
3079 unsigned short version;
3080 ULONGEST signature;
3081 struct signatured_type *type_sig;
3082 void **slot;
3083 gdb_byte *ptr = info_ptr;
3084
3085 offset = ptr - dwarf2_per_objfile->types.buffer;
3086
3087 /* We need to read the type's signature in order to build the hash
3088 table, but we don't need to read anything else just yet. */
3089
3090 /* Sanity check to ensure entire cu is present. */
3091 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3092 if (ptr + length + initial_length_size
3093 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3094 {
3095 complaint (&symfile_complaints,
3e43a32a
MS
3096 _("debug type entry runs off end "
3097 "of `.debug_types' section, ignored"));
348e048f
DE
3098 break;
3099 }
3100
3101 offset_size = initial_length_size == 4 ? 4 : 8;
3102 ptr += initial_length_size;
3103 version = bfd_get_16 (objfile->obfd, ptr);
3104 ptr += 2;
3105 ptr += offset_size; /* abbrev offset */
3106 ptr += 1; /* address size */
3107 signature = bfd_get_64 (objfile->obfd, ptr);
3108 ptr += 8;
3109 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3110
3111 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3112 memset (type_sig, 0, sizeof (*type_sig));
3113 type_sig->signature = signature;
348e048f 3114 type_sig->type_offset = type_offset;
ca1f3406 3115 type_sig->per_cu.objfile = objfile;
1fd400ff 3116 type_sig->per_cu.from_debug_types = 1;
b3c8eb43 3117 type_sig->per_cu.offset = offset;
348e048f
DE
3118
3119 slot = htab_find_slot (types_htab, type_sig, INSERT);
3120 gdb_assert (slot != NULL);
b3c8eb43
JK
3121 if (*slot != NULL)
3122 {
3123 const struct signatured_type *dup_sig = *slot;
3124
3125 complaint (&symfile_complaints,
3126 _("debug type entry at offset 0x%x is duplicate to the "
3127 "entry at offset 0x%x, signature 0x%s"),
3128 offset, dup_sig->per_cu.offset,
3129 phex (signature, sizeof (signature)));
3130 gdb_assert (signature == dup_sig->signature);
3131 }
348e048f
DE
3132 *slot = type_sig;
3133
3134 if (dwarf2_die_debug)
3135 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3136 offset, phex (signature, sizeof (signature)));
3137
3138 info_ptr = info_ptr + initial_length_size + length;
3139 }
3140
3141 dwarf2_per_objfile->signatured_types = types_htab;
3142
1fd400ff
TT
3143 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3144 dwarf2_per_objfile->type_comp_units
3145 = obstack_alloc (&objfile->objfile_obstack,
3146 dwarf2_per_objfile->n_type_comp_units
3147 * sizeof (struct dwarf2_per_cu_data *));
3148 iter = &dwarf2_per_objfile->type_comp_units[0];
3149 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3150 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3151 == dwarf2_per_objfile->n_type_comp_units);
3152
348e048f
DE
3153 return 1;
3154}
3155
3156/* Lookup a signature based type.
3157 Returns NULL if SIG is not present in the table. */
3158
3159static struct signatured_type *
3160lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3161{
3162 struct signatured_type find_entry, *entry;
3163
3164 if (dwarf2_per_objfile->signatured_types == NULL)
3165 {
3166 complaint (&symfile_complaints,
55f1336d 3167 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3168 return 0;
3169 }
3170
3171 find_entry.signature = sig;
3172 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3173 return entry;
3174}
3175
d85a05f0
DJ
3176/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3177
3178static void
3179init_cu_die_reader (struct die_reader_specs *reader,
3180 struct dwarf2_cu *cu)
3181{
3182 reader->abfd = cu->objfile->obfd;
3183 reader->cu = cu;
3184 if (cu->per_cu->from_debug_types)
be391dca
TT
3185 {
3186 gdb_assert (dwarf2_per_objfile->types.readin);
3187 reader->buffer = dwarf2_per_objfile->types.buffer;
3188 }
d85a05f0 3189 else
be391dca
TT
3190 {
3191 gdb_assert (dwarf2_per_objfile->info.readin);
3192 reader->buffer = dwarf2_per_objfile->info.buffer;
3193 }
d85a05f0
DJ
3194}
3195
3196/* Find the base address of the compilation unit for range lists and
3197 location lists. It will normally be specified by DW_AT_low_pc.
3198 In DWARF-3 draft 4, the base address could be overridden by
3199 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3200 compilation units with discontinuous ranges. */
3201
3202static void
3203dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3204{
3205 struct attribute *attr;
3206
3207 cu->base_known = 0;
3208 cu->base_address = 0;
3209
3210 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3211 if (attr)
3212 {
3213 cu->base_address = DW_ADDR (attr);
3214 cu->base_known = 1;
3215 }
3216 else
3217 {
3218 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3219 if (attr)
3220 {
3221 cu->base_address = DW_ADDR (attr);
3222 cu->base_known = 1;
3223 }
3224 }
3225}
3226
348e048f
DE
3227/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3228 to combine the common parts.
93311388 3229 Process a compilation unit for a psymtab.
348e048f
DE
3230 BUFFER is a pointer to the beginning of the dwarf section buffer,
3231 either .debug_info or debug_types.
93311388
DE
3232 INFO_PTR is a pointer to the start of the CU.
3233 Returns a pointer to the next CU. */
aaa75496 3234
93311388
DE
3235static gdb_byte *
3236process_psymtab_comp_unit (struct objfile *objfile,
3237 struct dwarf2_per_cu_data *this_cu,
3238 gdb_byte *buffer, gdb_byte *info_ptr,
3239 unsigned int buffer_size)
c906108c 3240{
c906108c 3241 bfd *abfd = objfile->obfd;
93311388 3242 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3243 struct die_info *comp_unit_die;
c906108c 3244 struct partial_symtab *pst;
5734ee8b 3245 CORE_ADDR baseaddr;
93311388
DE
3246 struct cleanup *back_to_inner;
3247 struct dwarf2_cu cu;
d85a05f0
DJ
3248 int has_children, has_pc_info;
3249 struct attribute *attr;
d85a05f0
DJ
3250 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3251 struct die_reader_specs reader_specs;
3e2a0cee 3252 const char *filename;
c906108c 3253
9816fde3 3254 init_one_comp_unit (&cu, objfile);
93311388 3255 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3256
93311388
DE
3257 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3258 buffer, buffer_size,
3259 abfd);
10b3939b 3260
93311388
DE
3261 /* Complete the cu_header. */
3262 cu.header.offset = beg_of_comp_unit - buffer;
3263 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3264
93311388 3265 cu.list_in_scope = &file_symbols;
af703f96 3266
328c9494
DJ
3267 /* If this compilation unit was already read in, free the
3268 cached copy in order to read it in again. This is
3269 necessary because we skipped some symbols when we first
3270 read in the compilation unit (see load_partial_dies).
3271 This problem could be avoided, but the benefit is
3272 unclear. */
3273 if (this_cu->cu != NULL)
3274 free_one_cached_comp_unit (this_cu->cu);
3275
3276 /* Note that this is a pointer to our stack frame, being
3277 added to a global data structure. It will be cleaned up
3278 in free_stack_comp_unit when we finish with this
3279 compilation unit. */
3280 this_cu->cu = &cu;
d85a05f0
DJ
3281 cu.per_cu = this_cu;
3282
93311388
DE
3283 /* Read the abbrevs for this compilation unit into a table. */
3284 dwarf2_read_abbrevs (abfd, &cu);
3285 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3286
93311388 3287 /* Read the compilation unit die. */
348e048f
DE
3288 if (this_cu->from_debug_types)
3289 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3290 init_cu_die_reader (&reader_specs, &cu);
3291 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3292 &has_children);
93311388 3293
348e048f
DE
3294 if (this_cu->from_debug_types)
3295 {
b3c8eb43
JK
3296 /* LENGTH has not been set yet for type units. */
3297 gdb_assert (this_cu->offset == cu.header.offset);
348e048f
DE
3298 this_cu->length = cu.header.length + cu.header.initial_length_size;
3299 }
d85a05f0 3300 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3301 {
93311388
DE
3302 info_ptr = (beg_of_comp_unit + cu.header.length
3303 + cu.header.initial_length_size);
3304 do_cleanups (back_to_inner);
3305 return info_ptr;
3306 }
72bf9492 3307
9816fde3 3308 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3309
93311388 3310 /* Allocate a new partial symbol table structure. */
d85a05f0 3311 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3312 if (attr == NULL || !DW_STRING (attr))
3313 filename = "";
3314 else
3315 filename = DW_STRING (attr);
93311388 3316 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3317 filename,
93311388
DE
3318 /* TEXTLOW and TEXTHIGH are set below. */
3319 0,
3320 objfile->global_psymbols.next,
3321 objfile->static_psymbols.next);
72bf9492 3322
d85a05f0
DJ
3323 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3324 if (attr != NULL)
3325 pst->dirname = DW_STRING (attr);
72bf9492 3326
e38df1d0 3327 pst->read_symtab_private = this_cu;
72bf9492 3328
93311388 3329 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3330
0963b4bd 3331 /* Store the function that reads in the rest of the symbol table. */
93311388 3332 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3333
9291a0cd 3334 this_cu->v.psymtab = pst;
c906108c 3335
d85a05f0
DJ
3336 dwarf2_find_base_address (comp_unit_die, &cu);
3337
93311388
DE
3338 /* Possibly set the default values of LOWPC and HIGHPC from
3339 `DW_AT_ranges'. */
d85a05f0
DJ
3340 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3341 &best_highpc, &cu, pst);
3342 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3343 /* Store the contiguous range if it is not empty; it can be empty for
3344 CUs with no code. */
3345 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3346 best_lowpc + baseaddr,
3347 best_highpc + baseaddr - 1, pst);
93311388
DE
3348
3349 /* Check if comp unit has_children.
3350 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3351 If not, there's no more debug_info for this comp unit. */
d85a05f0 3352 if (has_children)
93311388
DE
3353 {
3354 struct partial_die_info *first_die;
3355 CORE_ADDR lowpc, highpc;
31ffec48 3356
93311388
DE
3357 lowpc = ((CORE_ADDR) -1);
3358 highpc = ((CORE_ADDR) 0);
c906108c 3359
93311388 3360 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3361
93311388 3362 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3363 ! has_pc_info, &cu);
57c22c6c 3364
93311388
DE
3365 /* If we didn't find a lowpc, set it to highpc to avoid
3366 complaints from `maint check'. */
3367 if (lowpc == ((CORE_ADDR) -1))
3368 lowpc = highpc;
10b3939b 3369
93311388
DE
3370 /* If the compilation unit didn't have an explicit address range,
3371 then use the information extracted from its child dies. */
d85a05f0 3372 if (! has_pc_info)
93311388 3373 {
d85a05f0
DJ
3374 best_lowpc = lowpc;
3375 best_highpc = highpc;
93311388
DE
3376 }
3377 }
d85a05f0
DJ
3378 pst->textlow = best_lowpc + baseaddr;
3379 pst->texthigh = best_highpc + baseaddr;
c906108c 3380
93311388
DE
3381 pst->n_global_syms = objfile->global_psymbols.next -
3382 (objfile->global_psymbols.list + pst->globals_offset);
3383 pst->n_static_syms = objfile->static_psymbols.next -
3384 (objfile->static_psymbols.list + pst->statics_offset);
3385 sort_pst_symbols (pst);
c906108c 3386
93311388
DE
3387 info_ptr = (beg_of_comp_unit + cu.header.length
3388 + cu.header.initial_length_size);
ae038cb0 3389
348e048f
DE
3390 if (this_cu->from_debug_types)
3391 {
3392 /* It's not clear we want to do anything with stmt lists here.
3393 Waiting to see what gcc ultimately does. */
3394 }
d85a05f0 3395 else
93311388
DE
3396 {
3397 /* Get the list of files included in the current compilation unit,
3398 and build a psymtab for each of them. */
d85a05f0 3399 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3400 }
ae038cb0 3401
93311388 3402 do_cleanups (back_to_inner);
ae038cb0 3403
93311388
DE
3404 return info_ptr;
3405}
ff013f42 3406
348e048f
DE
3407/* Traversal function for htab_traverse_noresize.
3408 Process one .debug_types comp-unit. */
3409
3410static int
3411process_type_comp_unit (void **slot, void *info)
3412{
3413 struct signatured_type *entry = (struct signatured_type *) *slot;
3414 struct objfile *objfile = (struct objfile *) info;
3415 struct dwarf2_per_cu_data *this_cu;
3416
3417 this_cu = &entry->per_cu;
348e048f 3418
be391dca 3419 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3420 process_psymtab_comp_unit (objfile, this_cu,
3421 dwarf2_per_objfile->types.buffer,
b3c8eb43 3422 dwarf2_per_objfile->types.buffer + this_cu->offset,
348e048f
DE
3423 dwarf2_per_objfile->types.size);
3424
3425 return 1;
3426}
3427
3428/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3429 Build partial symbol tables for the .debug_types comp-units. */
3430
3431static void
3432build_type_psymtabs (struct objfile *objfile)
3433{
3434 if (! create_debug_types_hash_table (objfile))
3435 return;
3436
3437 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3438 process_type_comp_unit, objfile);
3439}
3440
60606b2c
TT
3441/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3442
3443static void
3444psymtabs_addrmap_cleanup (void *o)
3445{
3446 struct objfile *objfile = o;
ec61707d 3447
60606b2c
TT
3448 objfile->psymtabs_addrmap = NULL;
3449}
3450
93311388
DE
3451/* Build the partial symbol table by doing a quick pass through the
3452 .debug_info and .debug_abbrev sections. */
72bf9492 3453
93311388 3454static void
c67a9c90 3455dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3456{
93311388 3457 gdb_byte *info_ptr;
60606b2c
TT
3458 struct cleanup *back_to, *addrmap_cleanup;
3459 struct obstack temp_obstack;
93311388 3460
98bfdba5
PA
3461 dwarf2_per_objfile->reading_partial_symbols = 1;
3462
be391dca 3463 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3464 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3465
93311388
DE
3466 /* Any cached compilation units will be linked by the per-objfile
3467 read_in_chain. Make sure to free them when we're done. */
3468 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3469
348e048f
DE
3470 build_type_psymtabs (objfile);
3471
93311388 3472 create_all_comp_units (objfile);
c906108c 3473
60606b2c
TT
3474 /* Create a temporary address map on a temporary obstack. We later
3475 copy this to the final obstack. */
3476 obstack_init (&temp_obstack);
3477 make_cleanup_obstack_free (&temp_obstack);
3478 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3479 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3480
93311388
DE
3481 /* Since the objects we're extracting from .debug_info vary in
3482 length, only the individual functions to extract them (like
3483 read_comp_unit_head and load_partial_die) can really know whether
3484 the buffer is large enough to hold another complete object.
c906108c 3485
93311388
DE
3486 At the moment, they don't actually check that. If .debug_info
3487 holds just one extra byte after the last compilation unit's dies,
3488 then read_comp_unit_head will happily read off the end of the
3489 buffer. read_partial_die is similarly casual. Those functions
3490 should be fixed.
c906108c 3491
93311388
DE
3492 For this loop condition, simply checking whether there's any data
3493 left at all should be sufficient. */
c906108c 3494
93311388
DE
3495 while (info_ptr < (dwarf2_per_objfile->info.buffer
3496 + dwarf2_per_objfile->info.size))
3497 {
3498 struct dwarf2_per_cu_data *this_cu;
dd373385 3499
3e43a32a
MS
3500 this_cu = dwarf2_find_comp_unit (info_ptr
3501 - dwarf2_per_objfile->info.buffer,
93311388 3502 objfile);
aaa75496 3503
93311388
DE
3504 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3505 dwarf2_per_objfile->info.buffer,
3506 info_ptr,
3507 dwarf2_per_objfile->info.size);
c906108c 3508 }
ff013f42
JK
3509
3510 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3511 &objfile->objfile_obstack);
60606b2c 3512 discard_cleanups (addrmap_cleanup);
ff013f42 3513
ae038cb0
DJ
3514 do_cleanups (back_to);
3515}
3516
93311388 3517/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3518
3519static void
93311388
DE
3520load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3521 struct objfile *objfile)
ae038cb0
DJ
3522{
3523 bfd *abfd = objfile->obfd;
fe1b8b76 3524 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3525 struct die_info *comp_unit_die;
ae038cb0 3526 struct dwarf2_cu *cu;
1d9ec526 3527 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3528 int has_children;
3529 struct die_reader_specs reader_specs;
98bfdba5 3530 int read_cu = 0;
ae038cb0 3531
348e048f
DE
3532 gdb_assert (! this_cu->from_debug_types);
3533
be391dca 3534 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3535 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3536 beg_of_comp_unit = info_ptr;
3537
98bfdba5
PA
3538 if (this_cu->cu == NULL)
3539 {
9816fde3
JK
3540 cu = xmalloc (sizeof (*cu));
3541 init_one_comp_unit (cu, objfile);
ae038cb0 3542
98bfdba5 3543 read_cu = 1;
ae038cb0 3544
98bfdba5
PA
3545 /* If an error occurs while loading, release our storage. */
3546 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3547
98bfdba5
PA
3548 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3549 dwarf2_per_objfile->info.buffer,
3550 dwarf2_per_objfile->info.size,
3551 abfd);
ae038cb0 3552
98bfdba5
PA
3553 /* Complete the cu_header. */
3554 cu->header.offset = this_cu->offset;
3555 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3556
3557 /* Link this compilation unit into the compilation unit tree. */
3558 this_cu->cu = cu;
3559 cu->per_cu = this_cu;
98bfdba5
PA
3560
3561 /* Link this CU into read_in_chain. */
3562 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3563 dwarf2_per_objfile->read_in_chain = this_cu;
3564 }
3565 else
3566 {
3567 cu = this_cu->cu;
3568 info_ptr += cu->header.first_die_offset;
3569 }
ae038cb0
DJ
3570
3571 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3572 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3573 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3574 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3575
3576 /* Read the compilation unit die. */
d85a05f0
DJ
3577 init_cu_die_reader (&reader_specs, cu);
3578 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3579 &has_children);
ae038cb0 3580
9816fde3 3581 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3582
ae038cb0
DJ
3583 /* Check if comp unit has_children.
3584 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3585 If not, there's no more debug_info for this comp unit. */
d85a05f0 3586 if (has_children)
93311388 3587 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3588
98bfdba5
PA
3589 do_cleanups (free_abbrevs_cleanup);
3590
3591 if (read_cu)
3592 {
3593 /* We've successfully allocated this compilation unit. Let our
3594 caller clean it up when finished with it. */
3595 discard_cleanups (free_cu_cleanup);
3596 }
ae038cb0
DJ
3597}
3598
3599/* Create a list of all compilation units in OBJFILE. We do this only
3600 if an inter-comp-unit reference is found; presumably if there is one,
3601 there will be many, and one will occur early in the .debug_info section.
3602 So there's no point in building this list incrementally. */
3603
3604static void
3605create_all_comp_units (struct objfile *objfile)
3606{
3607 int n_allocated;
3608 int n_comp_units;
3609 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3610 gdb_byte *info_ptr;
3611
3612 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3613 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3614
3615 n_comp_units = 0;
3616 n_allocated = 10;
3617 all_comp_units = xmalloc (n_allocated
3618 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3619
3e43a32a
MS
3620 while (info_ptr < dwarf2_per_objfile->info.buffer
3621 + dwarf2_per_objfile->info.size)
ae038cb0 3622 {
c764a876 3623 unsigned int length, initial_length_size;
ae038cb0 3624 struct dwarf2_per_cu_data *this_cu;
c764a876 3625 unsigned int offset;
ae038cb0 3626
dce234bc 3627 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3628
3629 /* Read just enough information to find out where the next
3630 compilation unit is. */
c764a876
DE
3631 length = read_initial_length (objfile->obfd, info_ptr,
3632 &initial_length_size);
ae038cb0
DJ
3633
3634 /* Save the compilation unit for later lookup. */
3635 this_cu = obstack_alloc (&objfile->objfile_obstack,
3636 sizeof (struct dwarf2_per_cu_data));
3637 memset (this_cu, 0, sizeof (*this_cu));
3638 this_cu->offset = offset;
c764a876 3639 this_cu->length = length + initial_length_size;
9291a0cd 3640 this_cu->objfile = objfile;
ae038cb0
DJ
3641
3642 if (n_comp_units == n_allocated)
3643 {
3644 n_allocated *= 2;
3645 all_comp_units = xrealloc (all_comp_units,
3646 n_allocated
3647 * sizeof (struct dwarf2_per_cu_data *));
3648 }
3649 all_comp_units[n_comp_units++] = this_cu;
3650
3651 info_ptr = info_ptr + this_cu->length;
3652 }
3653
3654 dwarf2_per_objfile->all_comp_units
3655 = obstack_alloc (&objfile->objfile_obstack,
3656 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3657 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3658 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3659 xfree (all_comp_units);
3660 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3661}
3662
5734ee8b
DJ
3663/* Process all loaded DIEs for compilation unit CU, starting at
3664 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3665 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3666 DW_AT_ranges). If NEED_PC is set, then this function will set
3667 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3668 and record the covered ranges in the addrmap. */
c906108c 3669
72bf9492
DJ
3670static void
3671scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3672 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3673{
72bf9492 3674 struct partial_die_info *pdi;
c906108c 3675
91c24f0a
DC
3676 /* Now, march along the PDI's, descending into ones which have
3677 interesting children but skipping the children of the other ones,
3678 until we reach the end of the compilation unit. */
c906108c 3679
72bf9492 3680 pdi = first_die;
91c24f0a 3681
72bf9492
DJ
3682 while (pdi != NULL)
3683 {
3684 fixup_partial_die (pdi, cu);
c906108c 3685
f55ee35c 3686 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3687 children, so we need to look at them. Ditto for anonymous
3688 enums. */
933c6fe4 3689
72bf9492 3690 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3691 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3692 {
72bf9492 3693 switch (pdi->tag)
c906108c
SS
3694 {
3695 case DW_TAG_subprogram:
5734ee8b 3696 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3697 break;
72929c62 3698 case DW_TAG_constant:
c906108c
SS
3699 case DW_TAG_variable:
3700 case DW_TAG_typedef:
91c24f0a 3701 case DW_TAG_union_type:
72bf9492 3702 if (!pdi->is_declaration)
63d06c5c 3703 {
72bf9492 3704 add_partial_symbol (pdi, cu);
63d06c5c
DC
3705 }
3706 break;
c906108c 3707 case DW_TAG_class_type:
680b30c7 3708 case DW_TAG_interface_type:
c906108c 3709 case DW_TAG_structure_type:
72bf9492 3710 if (!pdi->is_declaration)
c906108c 3711 {
72bf9492 3712 add_partial_symbol (pdi, cu);
c906108c
SS
3713 }
3714 break;
91c24f0a 3715 case DW_TAG_enumeration_type:
72bf9492
DJ
3716 if (!pdi->is_declaration)
3717 add_partial_enumeration (pdi, cu);
c906108c
SS
3718 break;
3719 case DW_TAG_base_type:
a02abb62 3720 case DW_TAG_subrange_type:
c906108c 3721 /* File scope base type definitions are added to the partial
c5aa993b 3722 symbol table. */
72bf9492 3723 add_partial_symbol (pdi, cu);
c906108c 3724 break;
d9fa45fe 3725 case DW_TAG_namespace:
5734ee8b 3726 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3727 break;
5d7cb8df
JK
3728 case DW_TAG_module:
3729 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3730 break;
c906108c
SS
3731 default:
3732 break;
3733 }
3734 }
3735
72bf9492
DJ
3736 /* If the die has a sibling, skip to the sibling. */
3737
3738 pdi = pdi->die_sibling;
3739 }
3740}
3741
3742/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3743
72bf9492 3744 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3745 name is concatenated with "::" and the partial DIE's name. For
3746 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3747 Enumerators are an exception; they use the scope of their parent
3748 enumeration type, i.e. the name of the enumeration type is not
3749 prepended to the enumerator.
91c24f0a 3750
72bf9492
DJ
3751 There are two complexities. One is DW_AT_specification; in this
3752 case "parent" means the parent of the target of the specification,
3753 instead of the direct parent of the DIE. The other is compilers
3754 which do not emit DW_TAG_namespace; in this case we try to guess
3755 the fully qualified name of structure types from their members'
3756 linkage names. This must be done using the DIE's children rather
3757 than the children of any DW_AT_specification target. We only need
3758 to do this for structures at the top level, i.e. if the target of
3759 any DW_AT_specification (if any; otherwise the DIE itself) does not
3760 have a parent. */
3761
3762/* Compute the scope prefix associated with PDI's parent, in
3763 compilation unit CU. The result will be allocated on CU's
3764 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3765 field. NULL is returned if no prefix is necessary. */
3766static char *
3767partial_die_parent_scope (struct partial_die_info *pdi,
3768 struct dwarf2_cu *cu)
3769{
3770 char *grandparent_scope;
3771 struct partial_die_info *parent, *real_pdi;
91c24f0a 3772
72bf9492
DJ
3773 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3774 then this means the parent of the specification DIE. */
3775
3776 real_pdi = pdi;
72bf9492 3777 while (real_pdi->has_specification)
10b3939b 3778 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3779
3780 parent = real_pdi->die_parent;
3781 if (parent == NULL)
3782 return NULL;
3783
3784 if (parent->scope_set)
3785 return parent->scope;
3786
3787 fixup_partial_die (parent, cu);
3788
10b3939b 3789 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3790
acebe513
UW
3791 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3792 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3793 Work around this problem here. */
3794 if (cu->language == language_cplus
6e70227d 3795 && parent->tag == DW_TAG_namespace
acebe513
UW
3796 && strcmp (parent->name, "::") == 0
3797 && grandparent_scope == NULL)
3798 {
3799 parent->scope = NULL;
3800 parent->scope_set = 1;
3801 return NULL;
3802 }
3803
72bf9492 3804 if (parent->tag == DW_TAG_namespace
f55ee35c 3805 || parent->tag == DW_TAG_module
72bf9492
DJ
3806 || parent->tag == DW_TAG_structure_type
3807 || parent->tag == DW_TAG_class_type
680b30c7 3808 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3809 || parent->tag == DW_TAG_union_type
3810 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3811 {
3812 if (grandparent_scope == NULL)
3813 parent->scope = parent->name;
3814 else
3e43a32a
MS
3815 parent->scope = typename_concat (&cu->comp_unit_obstack,
3816 grandparent_scope,
f55ee35c 3817 parent->name, 0, cu);
72bf9492 3818 }
ceeb3d5a 3819 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3820 /* Enumerators should not get the name of the enumeration as a prefix. */
3821 parent->scope = grandparent_scope;
3822 else
3823 {
3824 /* FIXME drow/2004-04-01: What should we be doing with
3825 function-local names? For partial symbols, we should probably be
3826 ignoring them. */
3827 complaint (&symfile_complaints,
e2e0b3e5 3828 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3829 parent->tag, pdi->offset);
3830 parent->scope = grandparent_scope;
c906108c
SS
3831 }
3832
72bf9492
DJ
3833 parent->scope_set = 1;
3834 return parent->scope;
3835}
3836
3837/* Return the fully scoped name associated with PDI, from compilation unit
3838 CU. The result will be allocated with malloc. */
3839static char *
3840partial_die_full_name (struct partial_die_info *pdi,
3841 struct dwarf2_cu *cu)
3842{
3843 char *parent_scope;
3844
98bfdba5
PA
3845 /* If this is a template instantiation, we can not work out the
3846 template arguments from partial DIEs. So, unfortunately, we have
3847 to go through the full DIEs. At least any work we do building
3848 types here will be reused if full symbols are loaded later. */
3849 if (pdi->has_template_arguments)
3850 {
3851 fixup_partial_die (pdi, cu);
3852
3853 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3854 {
3855 struct die_info *die;
3856 struct attribute attr;
3857 struct dwarf2_cu *ref_cu = cu;
3858
3859 attr.name = 0;
3860 attr.form = DW_FORM_ref_addr;
3861 attr.u.addr = pdi->offset;
3862 die = follow_die_ref (NULL, &attr, &ref_cu);
3863
3864 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3865 }
3866 }
3867
72bf9492
DJ
3868 parent_scope = partial_die_parent_scope (pdi, cu);
3869 if (parent_scope == NULL)
3870 return NULL;
3871 else
f55ee35c 3872 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3873}
3874
3875static void
72bf9492 3876add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3877{
e7c27a73 3878 struct objfile *objfile = cu->objfile;
c906108c 3879 CORE_ADDR addr = 0;
decbce07 3880 char *actual_name = NULL;
5c4e30ca 3881 const struct partial_symbol *psym = NULL;
e142c38c 3882 CORE_ADDR baseaddr;
72bf9492 3883 int built_actual_name = 0;
e142c38c
DJ
3884
3885 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3886
94af9270
KS
3887 actual_name = partial_die_full_name (pdi, cu);
3888 if (actual_name)
3889 built_actual_name = 1;
63d06c5c 3890
72bf9492
DJ
3891 if (actual_name == NULL)
3892 actual_name = pdi->name;
3893
c906108c
SS
3894 switch (pdi->tag)
3895 {
3896 case DW_TAG_subprogram:
2cfa0c8d 3897 if (pdi->is_external || cu->language == language_ada)
c906108c 3898 {
2cfa0c8d
JB
3899 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3900 of the global scope. But in Ada, we want to be able to access
3901 nested procedures globally. So all Ada subprograms are stored
3902 in the global scope. */
f47fb265 3903 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3904 mst_text, objfile); */
f47fb265
MS
3905 add_psymbol_to_list (actual_name, strlen (actual_name),
3906 built_actual_name,
3907 VAR_DOMAIN, LOC_BLOCK,
3908 &objfile->global_psymbols,
3909 0, pdi->lowpc + baseaddr,
3910 cu->language, objfile);
c906108c
SS
3911 }
3912 else
3913 {
f47fb265 3914 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3915 mst_file_text, objfile); */
f47fb265
MS
3916 add_psymbol_to_list (actual_name, strlen (actual_name),
3917 built_actual_name,
3918 VAR_DOMAIN, LOC_BLOCK,
3919 &objfile->static_psymbols,
3920 0, pdi->lowpc + baseaddr,
3921 cu->language, objfile);
c906108c
SS
3922 }
3923 break;
72929c62
JB
3924 case DW_TAG_constant:
3925 {
3926 struct psymbol_allocation_list *list;
3927
3928 if (pdi->is_external)
3929 list = &objfile->global_psymbols;
3930 else
3931 list = &objfile->static_psymbols;
f47fb265
MS
3932 add_psymbol_to_list (actual_name, strlen (actual_name),
3933 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3934 list, 0, 0, cu->language, objfile);
72929c62
JB
3935 }
3936 break;
c906108c 3937 case DW_TAG_variable:
caac4577
JG
3938 if (pdi->locdesc)
3939 addr = decode_locdesc (pdi->locdesc, cu);
3940
3941 if (pdi->locdesc
3942 && addr == 0
3943 && !dwarf2_per_objfile->has_section_at_zero)
3944 {
3945 /* A global or static variable may also have been stripped
3946 out by the linker if unused, in which case its address
3947 will be nullified; do not add such variables into partial
3948 symbol table then. */
3949 }
3950 else if (pdi->is_external)
c906108c
SS
3951 {
3952 /* Global Variable.
3953 Don't enter into the minimal symbol tables as there is
3954 a minimal symbol table entry from the ELF symbols already.
3955 Enter into partial symbol table if it has a location
3956 descriptor or a type.
3957 If the location descriptor is missing, new_symbol will create
3958 a LOC_UNRESOLVED symbol, the address of the variable will then
3959 be determined from the minimal symbol table whenever the variable
3960 is referenced.
3961 The address for the partial symbol table entry is not
3962 used by GDB, but it comes in handy for debugging partial symbol
3963 table building. */
3964
c906108c 3965 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
3966 add_psymbol_to_list (actual_name, strlen (actual_name),
3967 built_actual_name,
3968 VAR_DOMAIN, LOC_STATIC,
3969 &objfile->global_psymbols,
3970 0, addr + baseaddr,
3971 cu->language, objfile);
c906108c
SS
3972 }
3973 else
3974 {
0963b4bd 3975 /* Static Variable. Skip symbols without location descriptors. */
c906108c 3976 if (pdi->locdesc == NULL)
decbce07
MS
3977 {
3978 if (built_actual_name)
3979 xfree (actual_name);
3980 return;
3981 }
f47fb265 3982 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3983 mst_file_data, objfile); */
f47fb265
MS
3984 add_psymbol_to_list (actual_name, strlen (actual_name),
3985 built_actual_name,
3986 VAR_DOMAIN, LOC_STATIC,
3987 &objfile->static_psymbols,
3988 0, addr + baseaddr,
3989 cu->language, objfile);
c906108c
SS
3990 }
3991 break;
3992 case DW_TAG_typedef:
3993 case DW_TAG_base_type:
a02abb62 3994 case DW_TAG_subrange_type:
38d518c9 3995 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3996 built_actual_name,
176620f1 3997 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3998 &objfile->static_psymbols,
e142c38c 3999 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4000 break;
72bf9492
DJ
4001 case DW_TAG_namespace:
4002 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4003 built_actual_name,
72bf9492
DJ
4004 VAR_DOMAIN, LOC_TYPEDEF,
4005 &objfile->global_psymbols,
4006 0, (CORE_ADDR) 0, cu->language, objfile);
4007 break;
c906108c 4008 case DW_TAG_class_type:
680b30c7 4009 case DW_TAG_interface_type:
c906108c
SS
4010 case DW_TAG_structure_type:
4011 case DW_TAG_union_type:
4012 case DW_TAG_enumeration_type:
fa4028e9
JB
4013 /* Skip external references. The DWARF standard says in the section
4014 about "Structure, Union, and Class Type Entries": "An incomplete
4015 structure, union or class type is represented by a structure,
4016 union or class entry that does not have a byte size attribute
4017 and that has a DW_AT_declaration attribute." */
4018 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4019 {
4020 if (built_actual_name)
4021 xfree (actual_name);
4022 return;
4023 }
fa4028e9 4024
63d06c5c
DC
4025 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4026 static vs. global. */
38d518c9 4027 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4028 built_actual_name,
176620f1 4029 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4030 (cu->language == language_cplus
4031 || cu->language == language_java)
63d06c5c
DC
4032 ? &objfile->global_psymbols
4033 : &objfile->static_psymbols,
e142c38c 4034 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4035
c906108c
SS
4036 break;
4037 case DW_TAG_enumerator:
38d518c9 4038 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4039 built_actual_name,
176620f1 4040 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4041 (cu->language == language_cplus
4042 || cu->language == language_java)
f6fe98ef
DJ
4043 ? &objfile->global_psymbols
4044 : &objfile->static_psymbols,
e142c38c 4045 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4046 break;
4047 default:
4048 break;
4049 }
5c4e30ca 4050
72bf9492
DJ
4051 if (built_actual_name)
4052 xfree (actual_name);
c906108c
SS
4053}
4054
5c4e30ca
DC
4055/* Read a partial die corresponding to a namespace; also, add a symbol
4056 corresponding to that namespace to the symbol table. NAMESPACE is
4057 the name of the enclosing namespace. */
91c24f0a 4058
72bf9492
DJ
4059static void
4060add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4061 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4062 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4063{
72bf9492 4064 /* Add a symbol for the namespace. */
e7c27a73 4065
72bf9492 4066 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4067
4068 /* Now scan partial symbols in that namespace. */
4069
91c24f0a 4070 if (pdi->has_children)
5734ee8b 4071 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4072}
4073
5d7cb8df
JK
4074/* Read a partial die corresponding to a Fortran module. */
4075
4076static void
4077add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4078 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4079{
f55ee35c 4080 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4081
4082 if (pdi->has_children)
4083 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4084}
4085
bc30ff58
JB
4086/* Read a partial die corresponding to a subprogram and create a partial
4087 symbol for that subprogram. When the CU language allows it, this
4088 routine also defines a partial symbol for each nested subprogram
4089 that this subprogram contains.
6e70227d 4090
bc30ff58
JB
4091 DIE my also be a lexical block, in which case we simply search
4092 recursively for suprograms defined inside that lexical block.
4093 Again, this is only performed when the CU language allows this
4094 type of definitions. */
4095
4096static void
4097add_partial_subprogram (struct partial_die_info *pdi,
4098 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4099 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4100{
4101 if (pdi->tag == DW_TAG_subprogram)
4102 {
4103 if (pdi->has_pc_info)
4104 {
4105 if (pdi->lowpc < *lowpc)
4106 *lowpc = pdi->lowpc;
4107 if (pdi->highpc > *highpc)
4108 *highpc = pdi->highpc;
5734ee8b
DJ
4109 if (need_pc)
4110 {
4111 CORE_ADDR baseaddr;
4112 struct objfile *objfile = cu->objfile;
4113
4114 baseaddr = ANOFFSET (objfile->section_offsets,
4115 SECT_OFF_TEXT (objfile));
4116 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4117 pdi->lowpc + baseaddr,
4118 pdi->highpc - 1 + baseaddr,
9291a0cd 4119 cu->per_cu->v.psymtab);
5734ee8b 4120 }
bc30ff58 4121 if (!pdi->is_declaration)
e8d05480
JB
4122 /* Ignore subprogram DIEs that do not have a name, they are
4123 illegal. Do not emit a complaint at this point, we will
4124 do so when we convert this psymtab into a symtab. */
4125 if (pdi->name)
4126 add_partial_symbol (pdi, cu);
bc30ff58
JB
4127 }
4128 }
6e70227d 4129
bc30ff58
JB
4130 if (! pdi->has_children)
4131 return;
4132
4133 if (cu->language == language_ada)
4134 {
4135 pdi = pdi->die_child;
4136 while (pdi != NULL)
4137 {
4138 fixup_partial_die (pdi, cu);
4139 if (pdi->tag == DW_TAG_subprogram
4140 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4141 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4142 pdi = pdi->die_sibling;
4143 }
4144 }
4145}
4146
91c24f0a
DC
4147/* Read a partial die corresponding to an enumeration type. */
4148
72bf9492
DJ
4149static void
4150add_partial_enumeration (struct partial_die_info *enum_pdi,
4151 struct dwarf2_cu *cu)
91c24f0a 4152{
72bf9492 4153 struct partial_die_info *pdi;
91c24f0a
DC
4154
4155 if (enum_pdi->name != NULL)
72bf9492
DJ
4156 add_partial_symbol (enum_pdi, cu);
4157
4158 pdi = enum_pdi->die_child;
4159 while (pdi)
91c24f0a 4160 {
72bf9492 4161 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4162 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4163 else
72bf9492
DJ
4164 add_partial_symbol (pdi, cu);
4165 pdi = pdi->die_sibling;
91c24f0a 4166 }
91c24f0a
DC
4167}
4168
4bb7a0a7
DJ
4169/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4170 Return the corresponding abbrev, or NULL if the number is zero (indicating
4171 an empty DIE). In either case *BYTES_READ will be set to the length of
4172 the initial number. */
4173
4174static struct abbrev_info *
fe1b8b76 4175peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4176 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4177{
4178 bfd *abfd = cu->objfile->obfd;
4179 unsigned int abbrev_number;
4180 struct abbrev_info *abbrev;
4181
4182 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4183
4184 if (abbrev_number == 0)
4185 return NULL;
4186
4187 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4188 if (!abbrev)
4189 {
3e43a32a
MS
4190 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4191 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4192 }
4193
4194 return abbrev;
4195}
4196
93311388
DE
4197/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4198 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4199 DIE. Any children of the skipped DIEs will also be skipped. */
4200
fe1b8b76 4201static gdb_byte *
93311388 4202skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4203{
4204 struct abbrev_info *abbrev;
4205 unsigned int bytes_read;
4206
4207 while (1)
4208 {
4209 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4210 if (abbrev == NULL)
4211 return info_ptr + bytes_read;
4212 else
93311388 4213 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4214 }
4215}
4216
93311388
DE
4217/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4218 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4219 abbrev corresponding to that skipped uleb128 should be passed in
4220 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4221 children. */
4222
fe1b8b76 4223static gdb_byte *
93311388
DE
4224skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4225 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4226{
4227 unsigned int bytes_read;
4228 struct attribute attr;
4229 bfd *abfd = cu->objfile->obfd;
4230 unsigned int form, i;
4231
4232 for (i = 0; i < abbrev->num_attrs; i++)
4233 {
4234 /* The only abbrev we care about is DW_AT_sibling. */
4235 if (abbrev->attrs[i].name == DW_AT_sibling)
4236 {
4237 read_attribute (&attr, &abbrev->attrs[i],
4238 abfd, info_ptr, cu);
4239 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4240 complaint (&symfile_complaints,
4241 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4242 else
93311388 4243 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4244 }
4245
4246 /* If it isn't DW_AT_sibling, skip this attribute. */
4247 form = abbrev->attrs[i].form;
4248 skip_attribute:
4249 switch (form)
4250 {
4bb7a0a7 4251 case DW_FORM_ref_addr:
ae411497
TT
4252 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4253 and later it is offset sized. */
4254 if (cu->header.version == 2)
4255 info_ptr += cu->header.addr_size;
4256 else
4257 info_ptr += cu->header.offset_size;
4258 break;
4259 case DW_FORM_addr:
4bb7a0a7
DJ
4260 info_ptr += cu->header.addr_size;
4261 break;
4262 case DW_FORM_data1:
4263 case DW_FORM_ref1:
4264 case DW_FORM_flag:
4265 info_ptr += 1;
4266 break;
2dc7f7b3
TT
4267 case DW_FORM_flag_present:
4268 break;
4bb7a0a7
DJ
4269 case DW_FORM_data2:
4270 case DW_FORM_ref2:
4271 info_ptr += 2;
4272 break;
4273 case DW_FORM_data4:
4274 case DW_FORM_ref4:
4275 info_ptr += 4;
4276 break;
4277 case DW_FORM_data8:
4278 case DW_FORM_ref8:
55f1336d 4279 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4280 info_ptr += 8;
4281 break;
4282 case DW_FORM_string:
9b1c24c8 4283 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4284 info_ptr += bytes_read;
4285 break;
2dc7f7b3 4286 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4287 case DW_FORM_strp:
4288 info_ptr += cu->header.offset_size;
4289 break;
2dc7f7b3 4290 case DW_FORM_exprloc:
4bb7a0a7
DJ
4291 case DW_FORM_block:
4292 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4293 info_ptr += bytes_read;
4294 break;
4295 case DW_FORM_block1:
4296 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4297 break;
4298 case DW_FORM_block2:
4299 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4300 break;
4301 case DW_FORM_block4:
4302 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4303 break;
4304 case DW_FORM_sdata:
4305 case DW_FORM_udata:
4306 case DW_FORM_ref_udata:
4307 info_ptr = skip_leb128 (abfd, info_ptr);
4308 break;
4309 case DW_FORM_indirect:
4310 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4311 info_ptr += bytes_read;
4312 /* We need to continue parsing from here, so just go back to
4313 the top. */
4314 goto skip_attribute;
4315
4316 default:
3e43a32a
MS
4317 error (_("Dwarf Error: Cannot handle %s "
4318 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4319 dwarf_form_name (form),
4320 bfd_get_filename (abfd));
4321 }
4322 }
4323
4324 if (abbrev->has_children)
93311388 4325 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4326 else
4327 return info_ptr;
4328}
4329
93311388
DE
4330/* Locate ORIG_PDI's sibling.
4331 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4332 in BUFFER. */
91c24f0a 4333
fe1b8b76 4334static gdb_byte *
93311388
DE
4335locate_pdi_sibling (struct partial_die_info *orig_pdi,
4336 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4337 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4338{
4339 /* Do we know the sibling already? */
72bf9492 4340
91c24f0a
DC
4341 if (orig_pdi->sibling)
4342 return orig_pdi->sibling;
4343
4344 /* Are there any children to deal with? */
4345
4346 if (!orig_pdi->has_children)
4347 return info_ptr;
4348
4bb7a0a7 4349 /* Skip the children the long way. */
91c24f0a 4350
93311388 4351 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4352}
4353
c906108c
SS
4354/* Expand this partial symbol table into a full symbol table. */
4355
4356static void
fba45db2 4357dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4358{
c906108c
SS
4359 if (pst != NULL)
4360 {
4361 if (pst->readin)
4362 {
3e43a32a
MS
4363 warning (_("bug: psymtab for %s is already read in."),
4364 pst->filename);
c906108c
SS
4365 }
4366 else
4367 {
4368 if (info_verbose)
4369 {
3e43a32a
MS
4370 printf_filtered (_("Reading in symbols for %s..."),
4371 pst->filename);
c906108c
SS
4372 gdb_flush (gdb_stdout);
4373 }
4374
10b3939b
DJ
4375 /* Restore our global data. */
4376 dwarf2_per_objfile = objfile_data (pst->objfile,
4377 dwarf2_objfile_data_key);
4378
b2ab525c
KB
4379 /* If this psymtab is constructed from a debug-only objfile, the
4380 has_section_at_zero flag will not necessarily be correct. We
4381 can get the correct value for this flag by looking at the data
4382 associated with the (presumably stripped) associated objfile. */
4383 if (pst->objfile->separate_debug_objfile_backlink)
4384 {
4385 struct dwarf2_per_objfile *dpo_backlink
4386 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4387 dwarf2_objfile_data_key);
9a619af0 4388
b2ab525c
KB
4389 dwarf2_per_objfile->has_section_at_zero
4390 = dpo_backlink->has_section_at_zero;
4391 }
4392
98bfdba5
PA
4393 dwarf2_per_objfile->reading_partial_symbols = 0;
4394
c906108c
SS
4395 psymtab_to_symtab_1 (pst);
4396
4397 /* Finish up the debug error message. */
4398 if (info_verbose)
a3f17187 4399 printf_filtered (_("done.\n"));
c906108c
SS
4400 }
4401 }
4402}
4403
10b3939b
DJ
4404/* Add PER_CU to the queue. */
4405
4406static void
03dd20cc 4407queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4408{
4409 struct dwarf2_queue_item *item;
4410
4411 per_cu->queued = 1;
4412 item = xmalloc (sizeof (*item));
4413 item->per_cu = per_cu;
4414 item->next = NULL;
4415
4416 if (dwarf2_queue == NULL)
4417 dwarf2_queue = item;
4418 else
4419 dwarf2_queue_tail->next = item;
4420
4421 dwarf2_queue_tail = item;
4422}
4423
4424/* Process the queue. */
4425
4426static void
4427process_queue (struct objfile *objfile)
4428{
4429 struct dwarf2_queue_item *item, *next_item;
4430
03dd20cc
DJ
4431 /* The queue starts out with one item, but following a DIE reference
4432 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4433 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4434 {
9291a0cd
TT
4435 if (dwarf2_per_objfile->using_index
4436 ? !item->per_cu->v.quick->symtab
4437 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4438 process_full_comp_unit (item->per_cu);
4439
4440 item->per_cu->queued = 0;
4441 next_item = item->next;
4442 xfree (item);
4443 }
4444
4445 dwarf2_queue_tail = NULL;
4446}
4447
4448/* Free all allocated queue entries. This function only releases anything if
4449 an error was thrown; if the queue was processed then it would have been
4450 freed as we went along. */
4451
4452static void
4453dwarf2_release_queue (void *dummy)
4454{
4455 struct dwarf2_queue_item *item, *last;
4456
4457 item = dwarf2_queue;
4458 while (item)
4459 {
4460 /* Anything still marked queued is likely to be in an
4461 inconsistent state, so discard it. */
4462 if (item->per_cu->queued)
4463 {
4464 if (item->per_cu->cu != NULL)
4465 free_one_cached_comp_unit (item->per_cu->cu);
4466 item->per_cu->queued = 0;
4467 }
4468
4469 last = item;
4470 item = item->next;
4471 xfree (last);
4472 }
4473
4474 dwarf2_queue = dwarf2_queue_tail = NULL;
4475}
4476
4477/* Read in full symbols for PST, and anything it depends on. */
4478
c906108c 4479static void
fba45db2 4480psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4481{
10b3939b 4482 struct dwarf2_per_cu_data *per_cu;
c906108c 4483 struct cleanup *back_to;
aaa75496
JB
4484 int i;
4485
4486 for (i = 0; i < pst->number_of_dependencies; i++)
4487 if (!pst->dependencies[i]->readin)
4488 {
4489 /* Inform about additional files that need to be read in. */
4490 if (info_verbose)
4491 {
a3f17187 4492 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4493 fputs_filtered (" ", gdb_stdout);
4494 wrap_here ("");
4495 fputs_filtered ("and ", gdb_stdout);
4496 wrap_here ("");
4497 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4498 wrap_here (""); /* Flush output. */
aaa75496
JB
4499 gdb_flush (gdb_stdout);
4500 }
4501 psymtab_to_symtab_1 (pst->dependencies[i]);
4502 }
4503
e38df1d0 4504 per_cu = pst->read_symtab_private;
10b3939b
DJ
4505
4506 if (per_cu == NULL)
aaa75496
JB
4507 {
4508 /* It's an include file, no symbols to read for it.
4509 Everything is in the parent symtab. */
4510 pst->readin = 1;
4511 return;
4512 }
c906108c 4513
9291a0cd 4514 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4515}
4516
93311388 4517/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4518
93311388 4519static void
3e43a32a
MS
4520load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4521 struct objfile *objfile)
10b3939b 4522{
31ffec48 4523 bfd *abfd = objfile->obfd;
10b3939b 4524 struct dwarf2_cu *cu;
c764a876 4525 unsigned int offset;
93311388 4526 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4527 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4528 struct attribute *attr;
98bfdba5 4529 int read_cu = 0;
6502dd73 4530
348e048f
DE
4531 gdb_assert (! per_cu->from_debug_types);
4532
c906108c 4533 /* Set local variables from the partial symbol table info. */
10b3939b 4534 offset = per_cu->offset;
6502dd73 4535
be391dca 4536 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4537 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4538 beg_of_comp_unit = info_ptr;
63d06c5c 4539
98bfdba5
PA
4540 if (per_cu->cu == NULL)
4541 {
9816fde3
JK
4542 cu = xmalloc (sizeof (*cu));
4543 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4544
4545 read_cu = 1;
c906108c 4546
98bfdba5
PA
4547 /* If an error occurs while loading, release our storage. */
4548 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4549
98bfdba5
PA
4550 /* Read in the comp_unit header. */
4551 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4552
98bfdba5
PA
4553 /* Complete the cu_header. */
4554 cu->header.offset = offset;
4555 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4556
98bfdba5
PA
4557 /* Read the abbrevs for this compilation unit. */
4558 dwarf2_read_abbrevs (abfd, cu);
4559 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4560
98bfdba5
PA
4561 /* Link this compilation unit into the compilation unit tree. */
4562 per_cu->cu = cu;
4563 cu->per_cu = per_cu;
98bfdba5
PA
4564
4565 /* Link this CU into read_in_chain. */
4566 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4567 dwarf2_per_objfile->read_in_chain = per_cu;
4568 }
4569 else
4570 {
4571 cu = per_cu->cu;
4572 info_ptr += cu->header.first_die_offset;
4573 }
e142c38c 4574
93311388 4575 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4576
4577 /* We try not to read any attributes in this function, because not
4578 all objfiles needed for references have been loaded yet, and symbol
4579 table processing isn't initialized. But we have to set the CU language,
4580 or we won't be able to build types correctly. */
9816fde3 4581 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4582
a6c727b2
DJ
4583 /* Similarly, if we do not read the producer, we can not apply
4584 producer-specific interpretation. */
4585 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4586 if (attr)
4587 cu->producer = DW_STRING (attr);
4588
98bfdba5
PA
4589 if (read_cu)
4590 {
4591 do_cleanups (free_abbrevs_cleanup);
e142c38c 4592
98bfdba5
PA
4593 /* We've successfully allocated this compilation unit. Let our
4594 caller clean it up when finished with it. */
4595 discard_cleanups (free_cu_cleanup);
4596 }
10b3939b
DJ
4597}
4598
3da10d80
KS
4599/* Add a DIE to the delayed physname list. */
4600
4601static void
4602add_to_method_list (struct type *type, int fnfield_index, int index,
4603 const char *name, struct die_info *die,
4604 struct dwarf2_cu *cu)
4605{
4606 struct delayed_method_info mi;
4607 mi.type = type;
4608 mi.fnfield_index = fnfield_index;
4609 mi.index = index;
4610 mi.name = name;
4611 mi.die = die;
4612 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4613}
4614
4615/* A cleanup for freeing the delayed method list. */
4616
4617static void
4618free_delayed_list (void *ptr)
4619{
4620 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4621 if (cu->method_list != NULL)
4622 {
4623 VEC_free (delayed_method_info, cu->method_list);
4624 cu->method_list = NULL;
4625 }
4626}
4627
4628/* Compute the physnames of any methods on the CU's method list.
4629
4630 The computation of method physnames is delayed in order to avoid the
4631 (bad) condition that one of the method's formal parameters is of an as yet
4632 incomplete type. */
4633
4634static void
4635compute_delayed_physnames (struct dwarf2_cu *cu)
4636{
4637 int i;
4638 struct delayed_method_info *mi;
4639 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4640 {
1d06ead6 4641 const char *physname;
3da10d80
KS
4642 struct fn_fieldlist *fn_flp
4643 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4644 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4645 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4646 }
4647}
4648
8be455d7
JK
4649/* Check for GCC >= 4.0. */
4650
4651static int
4652producer_is_gcc_ge_4_0 (struct dwarf2_cu *cu)
4653{
4654 const char *cs;
4655 int major, minor;
4656
4657 if (cu->producer == NULL)
4658 {
4659 /* For unknown compilers expect their behavior is not compliant. For GCC
4660 this case can also happen for -gdwarf-4 type units supported since
4661 gcc-4.5. */
4662
4663 return 0;
4664 }
4665
4666 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
4667
4668 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
4669 {
4670 /* For non-GCC compilers expect their behavior is not compliant. */
4671
4672 return 0;
4673 }
4674 cs = &cu->producer[strlen ("GNU ")];
4675 while (*cs && !isdigit (*cs))
4676 cs++;
4677 if (sscanf (cs, "%d.%d", &major, &minor) != 2)
4678 {
4679 /* Not recognized as GCC. */
4680
4681 return 0;
4682 }
4683
4684 return major >= 4;
4685}
4686
10b3939b
DJ
4687/* Generate full symbol information for PST and CU, whose DIEs have
4688 already been loaded into memory. */
4689
4690static void
4691process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4692{
10b3939b 4693 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4694 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4695 CORE_ADDR lowpc, highpc;
4696 struct symtab *symtab;
3da10d80 4697 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4698 CORE_ADDR baseaddr;
4699
4700 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4701
10b3939b
DJ
4702 buildsym_init ();
4703 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4704 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4705
4706 cu->list_in_scope = &file_symbols;
c906108c 4707
d85a05f0 4708 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4709
c906108c 4710 /* Do line number decoding in read_file_scope () */
10b3939b 4711 process_die (cu->dies, cu);
c906108c 4712
3da10d80
KS
4713 /* Now that we have processed all the DIEs in the CU, all the types
4714 should be complete, and it should now be safe to compute all of the
4715 physnames. */
4716 compute_delayed_physnames (cu);
4717 do_cleanups (delayed_list_cleanup);
4718
fae299cd
DC
4719 /* Some compilers don't define a DW_AT_high_pc attribute for the
4720 compilation unit. If the DW_AT_high_pc is missing, synthesize
4721 it, by scanning the DIE's below the compilation unit. */
10b3939b 4722 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4723
613e1657 4724 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4725
8be455d7 4726 if (symtab != NULL)
c906108c 4727 {
8be455d7
JK
4728 /* Set symtab language to language from DW_AT_language. If the
4729 compilation is from a C file generated by language preprocessors, do
4730 not set the language if it was already deduced by start_subfile. */
4731 if (!(cu->language == language_c && symtab->language != language_c))
4732 symtab->language = cu->language;
4733
4734 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4735 produce DW_AT_location with location lists but it can be possibly
4736 invalid without -fvar-tracking.
4737
4738 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4739 needed, it would be wrong due to missing DW_AT_producer there.
4740
4741 Still one can confuse GDB by using non-standard GCC compilation
4742 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4743 */
4744 if (cu->has_loclist && producer_is_gcc_ge_4_0 (cu))
4745 symtab->locations_valid = 1;
c906108c 4746 }
9291a0cd
TT
4747
4748 if (dwarf2_per_objfile->using_index)
4749 per_cu->v.quick->symtab = symtab;
4750 else
4751 {
4752 struct partial_symtab *pst = per_cu->v.psymtab;
4753 pst->symtab = symtab;
4754 pst->readin = 1;
4755 }
c906108c
SS
4756
4757 do_cleanups (back_to);
4758}
4759
4760/* Process a die and its children. */
4761
4762static void
e7c27a73 4763process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4764{
4765 switch (die->tag)
4766 {
4767 case DW_TAG_padding:
4768 break;
4769 case DW_TAG_compile_unit:
e7c27a73 4770 read_file_scope (die, cu);
c906108c 4771 break;
348e048f
DE
4772 case DW_TAG_type_unit:
4773 read_type_unit_scope (die, cu);
4774 break;
c906108c 4775 case DW_TAG_subprogram:
c906108c 4776 case DW_TAG_inlined_subroutine:
edb3359d 4777 read_func_scope (die, cu);
c906108c
SS
4778 break;
4779 case DW_TAG_lexical_block:
14898363
L
4780 case DW_TAG_try_block:
4781 case DW_TAG_catch_block:
e7c27a73 4782 read_lexical_block_scope (die, cu);
c906108c
SS
4783 break;
4784 case DW_TAG_class_type:
680b30c7 4785 case DW_TAG_interface_type:
c906108c
SS
4786 case DW_TAG_structure_type:
4787 case DW_TAG_union_type:
134d01f1 4788 process_structure_scope (die, cu);
c906108c
SS
4789 break;
4790 case DW_TAG_enumeration_type:
134d01f1 4791 process_enumeration_scope (die, cu);
c906108c 4792 break;
134d01f1 4793
f792889a
DJ
4794 /* These dies have a type, but processing them does not create
4795 a symbol or recurse to process the children. Therefore we can
4796 read them on-demand through read_type_die. */
c906108c 4797 case DW_TAG_subroutine_type:
72019c9c 4798 case DW_TAG_set_type:
c906108c 4799 case DW_TAG_array_type:
c906108c 4800 case DW_TAG_pointer_type:
c906108c 4801 case DW_TAG_ptr_to_member_type:
c906108c 4802 case DW_TAG_reference_type:
c906108c 4803 case DW_TAG_string_type:
c906108c 4804 break;
134d01f1 4805
c906108c 4806 case DW_TAG_base_type:
a02abb62 4807 case DW_TAG_subrange_type:
cb249c71 4808 case DW_TAG_typedef:
134d01f1
DJ
4809 /* Add a typedef symbol for the type definition, if it has a
4810 DW_AT_name. */
f792889a 4811 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4812 break;
c906108c 4813 case DW_TAG_common_block:
e7c27a73 4814 read_common_block (die, cu);
c906108c
SS
4815 break;
4816 case DW_TAG_common_inclusion:
4817 break;
d9fa45fe 4818 case DW_TAG_namespace:
63d06c5c 4819 processing_has_namespace_info = 1;
e7c27a73 4820 read_namespace (die, cu);
d9fa45fe 4821 break;
5d7cb8df 4822 case DW_TAG_module:
f55ee35c 4823 processing_has_namespace_info = 1;
5d7cb8df
JK
4824 read_module (die, cu);
4825 break;
d9fa45fe
DC
4826 case DW_TAG_imported_declaration:
4827 case DW_TAG_imported_module:
63d06c5c 4828 processing_has_namespace_info = 1;
27aa8d6a
SW
4829 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4830 || cu->language != language_fortran))
4831 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4832 dwarf_tag_name (die->tag));
4833 read_import_statement (die, cu);
d9fa45fe 4834 break;
c906108c 4835 default:
e7c27a73 4836 new_symbol (die, NULL, cu);
c906108c
SS
4837 break;
4838 }
4839}
4840
94af9270
KS
4841/* A helper function for dwarf2_compute_name which determines whether DIE
4842 needs to have the name of the scope prepended to the name listed in the
4843 die. */
4844
4845static int
4846die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4847{
1c809c68
TT
4848 struct attribute *attr;
4849
94af9270
KS
4850 switch (die->tag)
4851 {
4852 case DW_TAG_namespace:
4853 case DW_TAG_typedef:
4854 case DW_TAG_class_type:
4855 case DW_TAG_interface_type:
4856 case DW_TAG_structure_type:
4857 case DW_TAG_union_type:
4858 case DW_TAG_enumeration_type:
4859 case DW_TAG_enumerator:
4860 case DW_TAG_subprogram:
4861 case DW_TAG_member:
4862 return 1;
4863
4864 case DW_TAG_variable:
c2b0a229 4865 case DW_TAG_constant:
94af9270
KS
4866 /* We only need to prefix "globally" visible variables. These include
4867 any variable marked with DW_AT_external or any variable that
4868 lives in a namespace. [Variables in anonymous namespaces
4869 require prefixing, but they are not DW_AT_external.] */
4870
4871 if (dwarf2_attr (die, DW_AT_specification, cu))
4872 {
4873 struct dwarf2_cu *spec_cu = cu;
9a619af0 4874
94af9270
KS
4875 return die_needs_namespace (die_specification (die, &spec_cu),
4876 spec_cu);
4877 }
4878
1c809c68 4879 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4880 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4881 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4882 return 0;
4883 /* A variable in a lexical block of some kind does not need a
4884 namespace, even though in C++ such variables may be external
4885 and have a mangled name. */
4886 if (die->parent->tag == DW_TAG_lexical_block
4887 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4888 || die->parent->tag == DW_TAG_catch_block
4889 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4890 return 0;
4891 return 1;
94af9270
KS
4892
4893 default:
4894 return 0;
4895 }
4896}
4897
98bfdba5
PA
4898/* Retrieve the last character from a mem_file. */
4899
4900static void
4901do_ui_file_peek_last (void *object, const char *buffer, long length)
4902{
4903 char *last_char_p = (char *) object;
4904
4905 if (length > 0)
4906 *last_char_p = buffer[length - 1];
4907}
4908
94af9270
KS
4909/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4910 compute the physname for the object, which include a method's
4911 formal parameters (C++/Java) and return type (Java).
4912
af6b7be1
JB
4913 For Ada, return the DIE's linkage name rather than the fully qualified
4914 name. PHYSNAME is ignored..
4915
94af9270
KS
4916 The result is allocated on the objfile_obstack and canonicalized. */
4917
4918static const char *
4919dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4920 int physname)
4921{
4922 if (name == NULL)
4923 name = dwarf2_name (die, cu);
4924
f55ee35c
JK
4925 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4926 compute it by typename_concat inside GDB. */
4927 if (cu->language == language_ada
4928 || (cu->language == language_fortran && physname))
4929 {
4930 /* For Ada unit, we prefer the linkage name over the name, as
4931 the former contains the exported name, which the user expects
4932 to be able to reference. Ideally, we want the user to be able
4933 to reference this entity using either natural or linkage name,
4934 but we haven't started looking at this enhancement yet. */
4935 struct attribute *attr;
4936
4937 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4938 if (attr == NULL)
4939 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4940 if (attr && DW_STRING (attr))
4941 return DW_STRING (attr);
4942 }
4943
94af9270
KS
4944 /* These are the only languages we know how to qualify names in. */
4945 if (name != NULL
f55ee35c
JK
4946 && (cu->language == language_cplus || cu->language == language_java
4947 || cu->language == language_fortran))
94af9270
KS
4948 {
4949 if (die_needs_namespace (die, cu))
4950 {
4951 long length;
4952 char *prefix;
4953 struct ui_file *buf;
4954
4955 prefix = determine_prefix (die, cu);
4956 buf = mem_fileopen ();
4957 if (*prefix != '\0')
4958 {
f55ee35c
JK
4959 char *prefixed_name = typename_concat (NULL, prefix, name,
4960 physname, cu);
9a619af0 4961
94af9270
KS
4962 fputs_unfiltered (prefixed_name, buf);
4963 xfree (prefixed_name);
4964 }
4965 else
62d5b8da 4966 fputs_unfiltered (name, buf);
94af9270 4967
98bfdba5
PA
4968 /* Template parameters may be specified in the DIE's DW_AT_name, or
4969 as children with DW_TAG_template_type_param or
4970 DW_TAG_value_type_param. If the latter, add them to the name
4971 here. If the name already has template parameters, then
4972 skip this step; some versions of GCC emit both, and
4973 it is more efficient to use the pre-computed name.
4974
4975 Something to keep in mind about this process: it is very
4976 unlikely, or in some cases downright impossible, to produce
4977 something that will match the mangled name of a function.
4978 If the definition of the function has the same debug info,
4979 we should be able to match up with it anyway. But fallbacks
4980 using the minimal symbol, for instance to find a method
4981 implemented in a stripped copy of libstdc++, will not work.
4982 If we do not have debug info for the definition, we will have to
4983 match them up some other way.
4984
4985 When we do name matching there is a related problem with function
4986 templates; two instantiated function templates are allowed to
4987 differ only by their return types, which we do not add here. */
4988
4989 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4990 {
4991 struct attribute *attr;
4992 struct die_info *child;
4993 int first = 1;
4994
4995 die->building_fullname = 1;
4996
4997 for (child = die->child; child != NULL; child = child->sibling)
4998 {
4999 struct type *type;
5000 long value;
5001 gdb_byte *bytes;
5002 struct dwarf2_locexpr_baton *baton;
5003 struct value *v;
5004
5005 if (child->tag != DW_TAG_template_type_param
5006 && child->tag != DW_TAG_template_value_param)
5007 continue;
5008
5009 if (first)
5010 {
5011 fputs_unfiltered ("<", buf);
5012 first = 0;
5013 }
5014 else
5015 fputs_unfiltered (", ", buf);
5016
5017 attr = dwarf2_attr (child, DW_AT_type, cu);
5018 if (attr == NULL)
5019 {
5020 complaint (&symfile_complaints,
5021 _("template parameter missing DW_AT_type"));
5022 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5023 continue;
5024 }
5025 type = die_type (child, cu);
5026
5027 if (child->tag == DW_TAG_template_type_param)
5028 {
5029 c_print_type (type, "", buf, -1, 0);
5030 continue;
5031 }
5032
5033 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5034 if (attr == NULL)
5035 {
5036 complaint (&symfile_complaints,
3e43a32a
MS
5037 _("template parameter missing "
5038 "DW_AT_const_value"));
98bfdba5
PA
5039 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5040 continue;
5041 }
5042
5043 dwarf2_const_value_attr (attr, type, name,
5044 &cu->comp_unit_obstack, cu,
5045 &value, &bytes, &baton);
5046
5047 if (TYPE_NOSIGN (type))
5048 /* GDB prints characters as NUMBER 'CHAR'. If that's
5049 changed, this can use value_print instead. */
5050 c_printchar (value, type, buf);
5051 else
5052 {
5053 struct value_print_options opts;
5054
5055 if (baton != NULL)
5056 v = dwarf2_evaluate_loc_desc (type, NULL,
5057 baton->data,
5058 baton->size,
5059 baton->per_cu);
5060 else if (bytes != NULL)
5061 {
5062 v = allocate_value (type);
5063 memcpy (value_contents_writeable (v), bytes,
5064 TYPE_LENGTH (type));
5065 }
5066 else
5067 v = value_from_longest (type, value);
5068
3e43a32a
MS
5069 /* Specify decimal so that we do not depend on
5070 the radix. */
98bfdba5
PA
5071 get_formatted_print_options (&opts, 'd');
5072 opts.raw = 1;
5073 value_print (v, buf, &opts);
5074 release_value (v);
5075 value_free (v);
5076 }
5077 }
5078
5079 die->building_fullname = 0;
5080
5081 if (!first)
5082 {
5083 /* Close the argument list, with a space if necessary
5084 (nested templates). */
5085 char last_char = '\0';
5086 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5087 if (last_char == '>')
5088 fputs_unfiltered (" >", buf);
5089 else
5090 fputs_unfiltered (">", buf);
5091 }
5092 }
5093
94af9270
KS
5094 /* For Java and C++ methods, append formal parameter type
5095 information, if PHYSNAME. */
6e70227d 5096
94af9270
KS
5097 if (physname && die->tag == DW_TAG_subprogram
5098 && (cu->language == language_cplus
5099 || cu->language == language_java))
5100 {
5101 struct type *type = read_type_die (die, cu);
5102
3167638f 5103 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5104
5105 if (cu->language == language_java)
5106 {
5107 /* For java, we must append the return type to method
0963b4bd 5108 names. */
94af9270
KS
5109 if (die->tag == DW_TAG_subprogram)
5110 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5111 0, 0);
5112 }
5113 else if (cu->language == language_cplus)
5114 {
60430eff
DJ
5115 /* Assume that an artificial first parameter is
5116 "this", but do not crash if it is not. RealView
5117 marks unnamed (and thus unused) parameters as
5118 artificial; there is no way to differentiate
5119 the two cases. */
94af9270
KS
5120 if (TYPE_NFIELDS (type) > 0
5121 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5122 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5123 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5124 0))))
94af9270
KS
5125 fputs_unfiltered (" const", buf);
5126 }
5127 }
5128
5129 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5130 &length);
5131 ui_file_delete (buf);
5132
5133 if (cu->language == language_cplus)
5134 {
5135 char *cname
5136 = dwarf2_canonicalize_name (name, cu,
5137 &cu->objfile->objfile_obstack);
9a619af0 5138
94af9270
KS
5139 if (cname != NULL)
5140 name = cname;
5141 }
5142 }
5143 }
5144
5145 return name;
5146}
5147
0114d602
DJ
5148/* Return the fully qualified name of DIE, based on its DW_AT_name.
5149 If scope qualifiers are appropriate they will be added. The result
5150 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5151 not have a name. NAME may either be from a previous call to
5152 dwarf2_name or NULL.
5153
0963b4bd 5154 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5155
5156static const char *
94af9270 5157dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5158{
94af9270
KS
5159 return dwarf2_compute_name (name, die, cu, 0);
5160}
0114d602 5161
94af9270
KS
5162/* Construct a physname for the given DIE in CU. NAME may either be
5163 from a previous call to dwarf2_name or NULL. The result will be
5164 allocated on the objfile_objstack or NULL if the DIE does not have a
5165 name.
0114d602 5166
94af9270 5167 The output string will be canonicalized (if C++/Java). */
0114d602 5168
94af9270
KS
5169static const char *
5170dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5171{
5172 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
5173}
5174
27aa8d6a
SW
5175/* Read the import statement specified by the given die and record it. */
5176
5177static void
5178read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5179{
5180 struct attribute *import_attr;
5181 struct die_info *imported_die;
de4affc9 5182 struct dwarf2_cu *imported_cu;
27aa8d6a 5183 const char *imported_name;
794684b6 5184 const char *imported_name_prefix;
13387711
SW
5185 const char *canonical_name;
5186 const char *import_alias;
5187 const char *imported_declaration = NULL;
794684b6 5188 const char *import_prefix;
13387711
SW
5189
5190 char *temp;
27aa8d6a
SW
5191
5192 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5193 if (import_attr == NULL)
5194 {
5195 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5196 dwarf_tag_name (die->tag));
5197 return;
5198 }
5199
de4affc9
CC
5200 imported_cu = cu;
5201 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5202 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5203 if (imported_name == NULL)
5204 {
5205 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5206
5207 The import in the following code:
5208 namespace A
5209 {
5210 typedef int B;
5211 }
5212
5213 int main ()
5214 {
5215 using A::B;
5216 B b;
5217 return b;
5218 }
5219
5220 ...
5221 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5222 <52> DW_AT_decl_file : 1
5223 <53> DW_AT_decl_line : 6
5224 <54> DW_AT_import : <0x75>
5225 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5226 <59> DW_AT_name : B
5227 <5b> DW_AT_decl_file : 1
5228 <5c> DW_AT_decl_line : 2
5229 <5d> DW_AT_type : <0x6e>
5230 ...
5231 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5232 <76> DW_AT_byte_size : 4
5233 <77> DW_AT_encoding : 5 (signed)
5234
5235 imports the wrong die ( 0x75 instead of 0x58 ).
5236 This case will be ignored until the gcc bug is fixed. */
5237 return;
5238 }
5239
82856980
SW
5240 /* Figure out the local name after import. */
5241 import_alias = dwarf2_name (die, cu);
27aa8d6a 5242
794684b6
SW
5243 /* Figure out where the statement is being imported to. */
5244 import_prefix = determine_prefix (die, cu);
5245
5246 /* Figure out what the scope of the imported die is and prepend it
5247 to the name of the imported die. */
de4affc9 5248 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5249
f55ee35c
JK
5250 if (imported_die->tag != DW_TAG_namespace
5251 && imported_die->tag != DW_TAG_module)
794684b6 5252 {
13387711
SW
5253 imported_declaration = imported_name;
5254 canonical_name = imported_name_prefix;
794684b6 5255 }
13387711 5256 else if (strlen (imported_name_prefix) > 0)
794684b6 5257 {
13387711
SW
5258 temp = alloca (strlen (imported_name_prefix)
5259 + 2 + strlen (imported_name) + 1);
5260 strcpy (temp, imported_name_prefix);
5261 strcat (temp, "::");
5262 strcat (temp, imported_name);
5263 canonical_name = temp;
794684b6 5264 }
13387711
SW
5265 else
5266 canonical_name = imported_name;
794684b6 5267
c0cc3a76
SW
5268 cp_add_using_directive (import_prefix,
5269 canonical_name,
5270 import_alias,
13387711 5271 imported_declaration,
c0cc3a76 5272 &cu->objfile->objfile_obstack);
27aa8d6a
SW
5273}
5274
5fb290d7 5275static void
e142c38c 5276initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5277{
e142c38c 5278 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5279}
5280
ae2de4f8
DE
5281/* Cleanup function for read_file_scope. */
5282
cb1df416
DJ
5283static void
5284free_cu_line_header (void *arg)
5285{
5286 struct dwarf2_cu *cu = arg;
5287
5288 free_line_header (cu->line_header);
5289 cu->line_header = NULL;
5290}
5291
9291a0cd
TT
5292static void
5293find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5294 char **name, char **comp_dir)
5295{
5296 struct attribute *attr;
5297
5298 *name = NULL;
5299 *comp_dir = NULL;
5300
5301 /* Find the filename. Do not use dwarf2_name here, since the filename
5302 is not a source language identifier. */
5303 attr = dwarf2_attr (die, DW_AT_name, cu);
5304 if (attr)
5305 {
5306 *name = DW_STRING (attr);
5307 }
5308
5309 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5310 if (attr)
5311 *comp_dir = DW_STRING (attr);
5312 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5313 {
5314 *comp_dir = ldirname (*name);
5315 if (*comp_dir != NULL)
5316 make_cleanup (xfree, *comp_dir);
5317 }
5318 if (*comp_dir != NULL)
5319 {
5320 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5321 directory, get rid of it. */
5322 char *cp = strchr (*comp_dir, ':');
5323
5324 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5325 *comp_dir = cp + 1;
5326 }
5327
5328 if (*name == NULL)
5329 *name = "<unknown>";
5330}
5331
ae2de4f8
DE
5332/* Process DW_TAG_compile_unit. */
5333
c906108c 5334static void
e7c27a73 5335read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5336{
e7c27a73 5337 struct objfile *objfile = cu->objfile;
debd256d 5338 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5339 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5340 CORE_ADDR highpc = ((CORE_ADDR) 0);
5341 struct attribute *attr;
e1024ff1 5342 char *name = NULL;
c906108c
SS
5343 char *comp_dir = NULL;
5344 struct die_info *child_die;
5345 bfd *abfd = objfile->obfd;
debd256d 5346 struct line_header *line_header = 0;
e142c38c 5347 CORE_ADDR baseaddr;
6e70227d 5348
e142c38c 5349 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5350
fae299cd 5351 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5352
5353 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5354 from finish_block. */
2acceee2 5355 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5356 lowpc = highpc;
5357 lowpc += baseaddr;
5358 highpc += baseaddr;
5359
9291a0cd 5360 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5361
e142c38c 5362 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5363 if (attr)
5364 {
e142c38c 5365 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5366 }
5367
b0f35d58 5368 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5369 if (attr)
b0f35d58 5370 cu->producer = DW_STRING (attr);
303b6f5d 5371
f4b8a18d
KW
5372 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5373 standardised yet. As a workaround for the language detection we fall
5374 back to the DW_AT_producer string. */
5375 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5376 cu->language = language_opencl;
5377
0963b4bd 5378 /* We assume that we're processing GCC output. */
c906108c 5379 processing_gcc_compilation = 2;
c906108c 5380
df8a16a1
DJ
5381 processing_has_namespace_info = 0;
5382
c906108c
SS
5383 start_symtab (name, comp_dir, lowpc);
5384 record_debugformat ("DWARF 2");
303b6f5d 5385 record_producer (cu->producer);
c906108c 5386
e142c38c 5387 initialize_cu_func_list (cu);
c906108c 5388
cb1df416
DJ
5389 /* Decode line number information if present. We do this before
5390 processing child DIEs, so that the line header table is available
5391 for DW_AT_decl_file. */
e142c38c 5392 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5393 if (attr)
5394 {
debd256d 5395 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5396 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5397 if (line_header)
5398 {
cb1df416
DJ
5399 cu->line_header = line_header;
5400 make_cleanup (free_cu_line_header, cu);
aaa75496 5401 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5402 }
5fb290d7 5403 }
debd256d 5404
cb1df416
DJ
5405 /* Process all dies in compilation unit. */
5406 if (die->child != NULL)
5407 {
5408 child_die = die->child;
5409 while (child_die && child_die->tag)
5410 {
5411 process_die (child_die, cu);
5412 child_die = sibling_die (child_die);
5413 }
5414 }
5415
2e276125
JB
5416 /* Decode macro information, if present. Dwarf 2 macro information
5417 refers to information in the line number info statement program
5418 header, so we can only read it if we've read the header
5419 successfully. */
e142c38c 5420 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5421 if (attr && line_header)
2e276125
JB
5422 {
5423 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5424
2e276125 5425 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5426 comp_dir, abfd, cu);
2e276125 5427 }
debd256d 5428 do_cleanups (back_to);
5fb290d7
DJ
5429}
5430
ae2de4f8
DE
5431/* Process DW_TAG_type_unit.
5432 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5433 actual type being defined by this TU. In this case the first top
5434 level sibling is there to provide context only. */
5435
5436static void
5437read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5438{
5439 struct objfile *objfile = cu->objfile;
5440 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5441 CORE_ADDR lowpc;
5442 struct attribute *attr;
5443 char *name = NULL;
5444 char *comp_dir = NULL;
5445 struct die_info *child_die;
5446 bfd *abfd = objfile->obfd;
348e048f
DE
5447
5448 /* start_symtab needs a low pc, but we don't really have one.
5449 Do what read_file_scope would do in the absence of such info. */
5450 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5451
5452 /* Find the filename. Do not use dwarf2_name here, since the filename
5453 is not a source language identifier. */
5454 attr = dwarf2_attr (die, DW_AT_name, cu);
5455 if (attr)
5456 name = DW_STRING (attr);
5457
5458 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5459 if (attr)
5460 comp_dir = DW_STRING (attr);
5461 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5462 {
5463 comp_dir = ldirname (name);
5464 if (comp_dir != NULL)
5465 make_cleanup (xfree, comp_dir);
5466 }
5467
5468 if (name == NULL)
5469 name = "<unknown>";
5470
5471 attr = dwarf2_attr (die, DW_AT_language, cu);
5472 if (attr)
5473 set_cu_language (DW_UNSND (attr), cu);
5474
5475 /* This isn't technically needed today. It is done for symmetry
5476 with read_file_scope. */
5477 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5478 if (attr)
348e048f
DE
5479 cu->producer = DW_STRING (attr);
5480
0963b4bd 5481 /* We assume that we're processing GCC output. */
348e048f
DE
5482 processing_gcc_compilation = 2;
5483
5484 processing_has_namespace_info = 0;
5485
5486 start_symtab (name, comp_dir, lowpc);
5487 record_debugformat ("DWARF 2");
5488 record_producer (cu->producer);
5489
5490 /* Process the dies in the type unit. */
5491 if (die->child == NULL)
5492 {
5493 dump_die_for_error (die);
5494 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5495 bfd_get_filename (abfd));
5496 }
5497
5498 child_die = die->child;
5499
5500 while (child_die && child_die->tag)
5501 {
5502 process_die (child_die, cu);
5503
5504 child_die = sibling_die (child_die);
5505 }
5506
5507 do_cleanups (back_to);
5508}
5509
5fb290d7 5510static void
e142c38c
DJ
5511add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5512 struct dwarf2_cu *cu)
5fb290d7
DJ
5513{
5514 struct function_range *thisfn;
5515
5516 thisfn = (struct function_range *)
7b5a2f43 5517 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5518 thisfn->name = name;
5519 thisfn->lowpc = lowpc;
5520 thisfn->highpc = highpc;
5521 thisfn->seen_line = 0;
5522 thisfn->next = NULL;
5523
e142c38c
DJ
5524 if (cu->last_fn == NULL)
5525 cu->first_fn = thisfn;
5fb290d7 5526 else
e142c38c 5527 cu->last_fn->next = thisfn;
5fb290d7 5528
e142c38c 5529 cu->last_fn = thisfn;
c906108c
SS
5530}
5531
d389af10
JK
5532/* qsort helper for inherit_abstract_dies. */
5533
5534static int
5535unsigned_int_compar (const void *ap, const void *bp)
5536{
5537 unsigned int a = *(unsigned int *) ap;
5538 unsigned int b = *(unsigned int *) bp;
5539
5540 return (a > b) - (b > a);
5541}
5542
5543/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5544 Inherit only the children of the DW_AT_abstract_origin DIE not being
5545 already referenced by DW_AT_abstract_origin from the children of the
5546 current DIE. */
d389af10
JK
5547
5548static void
5549inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5550{
5551 struct die_info *child_die;
5552 unsigned die_children_count;
5553 /* CU offsets which were referenced by children of the current DIE. */
5554 unsigned *offsets;
5555 unsigned *offsets_end, *offsetp;
5556 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5557 struct die_info *origin_die;
5558 /* Iterator of the ORIGIN_DIE children. */
5559 struct die_info *origin_child_die;
5560 struct cleanup *cleanups;
5561 struct attribute *attr;
cd02d79d
PA
5562 struct dwarf2_cu *origin_cu;
5563 struct pending **origin_previous_list_in_scope;
d389af10
JK
5564
5565 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5566 if (!attr)
5567 return;
5568
cd02d79d
PA
5569 /* Note that following die references may follow to a die in a
5570 different cu. */
5571
5572 origin_cu = cu;
5573 origin_die = follow_die_ref (die, attr, &origin_cu);
5574
5575 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5576 symbols in. */
5577 origin_previous_list_in_scope = origin_cu->list_in_scope;
5578 origin_cu->list_in_scope = cu->list_in_scope;
5579
edb3359d
DJ
5580 if (die->tag != origin_die->tag
5581 && !(die->tag == DW_TAG_inlined_subroutine
5582 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5583 complaint (&symfile_complaints,
5584 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5585 die->offset, origin_die->offset);
5586
5587 child_die = die->child;
5588 die_children_count = 0;
5589 while (child_die && child_die->tag)
5590 {
5591 child_die = sibling_die (child_die);
5592 die_children_count++;
5593 }
5594 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5595 cleanups = make_cleanup (xfree, offsets);
5596
5597 offsets_end = offsets;
5598 child_die = die->child;
5599 while (child_die && child_die->tag)
5600 {
c38f313d
DJ
5601 /* For each CHILD_DIE, find the corresponding child of
5602 ORIGIN_DIE. If there is more than one layer of
5603 DW_AT_abstract_origin, follow them all; there shouldn't be,
5604 but GCC versions at least through 4.4 generate this (GCC PR
5605 40573). */
5606 struct die_info *child_origin_die = child_die;
cd02d79d 5607 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5608
c38f313d
DJ
5609 while (1)
5610 {
cd02d79d
PA
5611 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5612 child_origin_cu);
c38f313d
DJ
5613 if (attr == NULL)
5614 break;
cd02d79d
PA
5615 child_origin_die = follow_die_ref (child_origin_die, attr,
5616 &child_origin_cu);
c38f313d
DJ
5617 }
5618
d389af10
JK
5619 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5620 counterpart may exist. */
c38f313d 5621 if (child_origin_die != child_die)
d389af10 5622 {
edb3359d
DJ
5623 if (child_die->tag != child_origin_die->tag
5624 && !(child_die->tag == DW_TAG_inlined_subroutine
5625 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5626 complaint (&symfile_complaints,
5627 _("Child DIE 0x%x and its abstract origin 0x%x have "
5628 "different tags"), child_die->offset,
5629 child_origin_die->offset);
c38f313d
DJ
5630 if (child_origin_die->parent != origin_die)
5631 complaint (&symfile_complaints,
5632 _("Child DIE 0x%x and its abstract origin 0x%x have "
5633 "different parents"), child_die->offset,
5634 child_origin_die->offset);
5635 else
5636 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5637 }
5638 child_die = sibling_die (child_die);
5639 }
5640 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5641 unsigned_int_compar);
5642 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5643 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5644 complaint (&symfile_complaints,
5645 _("Multiple children of DIE 0x%x refer "
5646 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5647 die->offset, *offsetp);
5648
5649 offsetp = offsets;
5650 origin_child_die = origin_die->child;
5651 while (origin_child_die && origin_child_die->tag)
5652 {
5653 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5654 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5655 offsetp++;
5656 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5657 {
5658 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5659 process_die (origin_child_die, origin_cu);
d389af10
JK
5660 }
5661 origin_child_die = sibling_die (origin_child_die);
5662 }
cd02d79d 5663 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5664
5665 do_cleanups (cleanups);
5666}
5667
c906108c 5668static void
e7c27a73 5669read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5670{
e7c27a73 5671 struct objfile *objfile = cu->objfile;
52f0bd74 5672 struct context_stack *new;
c906108c
SS
5673 CORE_ADDR lowpc;
5674 CORE_ADDR highpc;
5675 struct die_info *child_die;
edb3359d 5676 struct attribute *attr, *call_line, *call_file;
c906108c 5677 char *name;
e142c38c 5678 CORE_ADDR baseaddr;
801e3a5b 5679 struct block *block;
edb3359d 5680 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5681 VEC (symbolp) *template_args = NULL;
5682 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5683
5684 if (inlined_func)
5685 {
5686 /* If we do not have call site information, we can't show the
5687 caller of this inlined function. That's too confusing, so
5688 only use the scope for local variables. */
5689 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5690 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5691 if (call_line == NULL || call_file == NULL)
5692 {
5693 read_lexical_block_scope (die, cu);
5694 return;
5695 }
5696 }
c906108c 5697
e142c38c
DJ
5698 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5699
94af9270 5700 name = dwarf2_name (die, cu);
c906108c 5701
e8d05480
JB
5702 /* Ignore functions with missing or empty names. These are actually
5703 illegal according to the DWARF standard. */
5704 if (name == NULL)
5705 {
5706 complaint (&symfile_complaints,
5707 _("missing name for subprogram DIE at %d"), die->offset);
5708 return;
5709 }
5710
5711 /* Ignore functions with missing or invalid low and high pc attributes. */
5712 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5713 {
ae4d0c03
PM
5714 attr = dwarf2_attr (die, DW_AT_external, cu);
5715 if (!attr || !DW_UNSND (attr))
5716 complaint (&symfile_complaints,
3e43a32a
MS
5717 _("cannot get low and high bounds "
5718 "for subprogram DIE at %d"),
ae4d0c03 5719 die->offset);
e8d05480
JB
5720 return;
5721 }
c906108c
SS
5722
5723 lowpc += baseaddr;
5724 highpc += baseaddr;
5725
5fb290d7 5726 /* Record the function range for dwarf_decode_lines. */
e142c38c 5727 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5728
34eaf542
TT
5729 /* If we have any template arguments, then we must allocate a
5730 different sort of symbol. */
5731 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5732 {
5733 if (child_die->tag == DW_TAG_template_type_param
5734 || child_die->tag == DW_TAG_template_value_param)
5735 {
5736 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5737 struct template_symbol);
5738 templ_func->base.is_cplus_template_function = 1;
5739 break;
5740 }
5741 }
5742
c906108c 5743 new = push_context (0, lowpc);
34eaf542
TT
5744 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5745 (struct symbol *) templ_func);
4c2df51b 5746
4cecd739
DJ
5747 /* If there is a location expression for DW_AT_frame_base, record
5748 it. */
e142c38c 5749 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5750 if (attr)
c034e007
AC
5751 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5752 expression is being recorded directly in the function's symbol
5753 and not in a separate frame-base object. I guess this hack is
5754 to avoid adding some sort of frame-base adjunct/annex to the
5755 function's symbol :-(. The problem with doing this is that it
5756 results in a function symbol with a location expression that
5757 has nothing to do with the location of the function, ouch! The
5758 relationship should be: a function's symbol has-a frame base; a
5759 frame-base has-a location expression. */
e7c27a73 5760 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5761
e142c38c 5762 cu->list_in_scope = &local_symbols;
c906108c 5763
639d11d3 5764 if (die->child != NULL)
c906108c 5765 {
639d11d3 5766 child_die = die->child;
c906108c
SS
5767 while (child_die && child_die->tag)
5768 {
34eaf542
TT
5769 if (child_die->tag == DW_TAG_template_type_param
5770 || child_die->tag == DW_TAG_template_value_param)
5771 {
5772 struct symbol *arg = new_symbol (child_die, NULL, cu);
5773
f1078f66
DJ
5774 if (arg != NULL)
5775 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5776 }
5777 else
5778 process_die (child_die, cu);
c906108c
SS
5779 child_die = sibling_die (child_die);
5780 }
5781 }
5782
d389af10
JK
5783 inherit_abstract_dies (die, cu);
5784
4a811a97
UW
5785 /* If we have a DW_AT_specification, we might need to import using
5786 directives from the context of the specification DIE. See the
5787 comment in determine_prefix. */
5788 if (cu->language == language_cplus
5789 && dwarf2_attr (die, DW_AT_specification, cu))
5790 {
5791 struct dwarf2_cu *spec_cu = cu;
5792 struct die_info *spec_die = die_specification (die, &spec_cu);
5793
5794 while (spec_die)
5795 {
5796 child_die = spec_die->child;
5797 while (child_die && child_die->tag)
5798 {
5799 if (child_die->tag == DW_TAG_imported_module)
5800 process_die (child_die, spec_cu);
5801 child_die = sibling_die (child_die);
5802 }
5803
5804 /* In some cases, GCC generates specification DIEs that
5805 themselves contain DW_AT_specification attributes. */
5806 spec_die = die_specification (spec_die, &spec_cu);
5807 }
5808 }
5809
c906108c
SS
5810 new = pop_context ();
5811 /* Make a block for the local symbols within. */
801e3a5b
JB
5812 block = finish_block (new->name, &local_symbols, new->old_blocks,
5813 lowpc, highpc, objfile);
5814
df8a16a1 5815 /* For C++, set the block's scope. */
f55ee35c 5816 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5817 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5818 determine_prefix (die, cu),
df8a16a1
DJ
5819 processing_has_namespace_info);
5820
801e3a5b
JB
5821 /* If we have address ranges, record them. */
5822 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5823
34eaf542
TT
5824 /* Attach template arguments to function. */
5825 if (! VEC_empty (symbolp, template_args))
5826 {
5827 gdb_assert (templ_func != NULL);
5828
5829 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5830 templ_func->template_arguments
5831 = obstack_alloc (&objfile->objfile_obstack,
5832 (templ_func->n_template_arguments
5833 * sizeof (struct symbol *)));
5834 memcpy (templ_func->template_arguments,
5835 VEC_address (symbolp, template_args),
5836 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5837 VEC_free (symbolp, template_args);
5838 }
5839
208d8187
JB
5840 /* In C++, we can have functions nested inside functions (e.g., when
5841 a function declares a class that has methods). This means that
5842 when we finish processing a function scope, we may need to go
5843 back to building a containing block's symbol lists. */
5844 local_symbols = new->locals;
5845 param_symbols = new->params;
27aa8d6a 5846 using_directives = new->using_directives;
208d8187 5847
921e78cf
JB
5848 /* If we've finished processing a top-level function, subsequent
5849 symbols go in the file symbol list. */
5850 if (outermost_context_p ())
e142c38c 5851 cu->list_in_scope = &file_symbols;
c906108c
SS
5852}
5853
5854/* Process all the DIES contained within a lexical block scope. Start
5855 a new scope, process the dies, and then close the scope. */
5856
5857static void
e7c27a73 5858read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5859{
e7c27a73 5860 struct objfile *objfile = cu->objfile;
52f0bd74 5861 struct context_stack *new;
c906108c
SS
5862 CORE_ADDR lowpc, highpc;
5863 struct die_info *child_die;
e142c38c
DJ
5864 CORE_ADDR baseaddr;
5865
5866 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5867
5868 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5869 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5870 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5871 be nasty. Might be easier to properly extend generic blocks to
af34e669 5872 describe ranges. */
d85a05f0 5873 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5874 return;
5875 lowpc += baseaddr;
5876 highpc += baseaddr;
5877
5878 push_context (0, lowpc);
639d11d3 5879 if (die->child != NULL)
c906108c 5880 {
639d11d3 5881 child_die = die->child;
c906108c
SS
5882 while (child_die && child_die->tag)
5883 {
e7c27a73 5884 process_die (child_die, cu);
c906108c
SS
5885 child_die = sibling_die (child_die);
5886 }
5887 }
5888 new = pop_context ();
5889
8540c487 5890 if (local_symbols != NULL || using_directives != NULL)
c906108c 5891 {
801e3a5b
JB
5892 struct block *block
5893 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5894 highpc, objfile);
5895
5896 /* Note that recording ranges after traversing children, as we
5897 do here, means that recording a parent's ranges entails
5898 walking across all its children's ranges as they appear in
5899 the address map, which is quadratic behavior.
5900
5901 It would be nicer to record the parent's ranges before
5902 traversing its children, simply overriding whatever you find
5903 there. But since we don't even decide whether to create a
5904 block until after we've traversed its children, that's hard
5905 to do. */
5906 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5907 }
5908 local_symbols = new->locals;
27aa8d6a 5909 using_directives = new->using_directives;
c906108c
SS
5910}
5911
43039443 5912/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5913 Return 1 if the attributes are present and valid, otherwise, return 0.
5914 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5915
5916static int
5917dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5918 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5919 struct partial_symtab *ranges_pst)
43039443
JK
5920{
5921 struct objfile *objfile = cu->objfile;
5922 struct comp_unit_head *cu_header = &cu->header;
5923 bfd *obfd = objfile->obfd;
5924 unsigned int addr_size = cu_header->addr_size;
5925 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5926 /* Base address selection entry. */
5927 CORE_ADDR base;
5928 int found_base;
5929 unsigned int dummy;
5930 gdb_byte *buffer;
5931 CORE_ADDR marker;
5932 int low_set;
5933 CORE_ADDR low = 0;
5934 CORE_ADDR high = 0;
ff013f42 5935 CORE_ADDR baseaddr;
43039443 5936
d00adf39
DE
5937 found_base = cu->base_known;
5938 base = cu->base_address;
43039443 5939
be391dca 5940 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5941 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5942 {
5943 complaint (&symfile_complaints,
5944 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5945 offset);
5946 return 0;
5947 }
dce234bc 5948 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5949
5950 /* Read in the largest possible address. */
5951 marker = read_address (obfd, buffer, cu, &dummy);
5952 if ((marker & mask) == mask)
5953 {
5954 /* If we found the largest possible address, then
5955 read the base address. */
5956 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5957 buffer += 2 * addr_size;
5958 offset += 2 * addr_size;
5959 found_base = 1;
5960 }
5961
5962 low_set = 0;
5963
e7030f15 5964 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5965
43039443
JK
5966 while (1)
5967 {
5968 CORE_ADDR range_beginning, range_end;
5969
5970 range_beginning = read_address (obfd, buffer, cu, &dummy);
5971 buffer += addr_size;
5972 range_end = read_address (obfd, buffer, cu, &dummy);
5973 buffer += addr_size;
5974 offset += 2 * addr_size;
5975
5976 /* An end of list marker is a pair of zero addresses. */
5977 if (range_beginning == 0 && range_end == 0)
5978 /* Found the end of list entry. */
5979 break;
5980
5981 /* Each base address selection entry is a pair of 2 values.
5982 The first is the largest possible address, the second is
5983 the base address. Check for a base address here. */
5984 if ((range_beginning & mask) == mask)
5985 {
5986 /* If we found the largest possible address, then
5987 read the base address. */
5988 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5989 found_base = 1;
5990 continue;
5991 }
5992
5993 if (!found_base)
5994 {
5995 /* We have no valid base address for the ranges
5996 data. */
5997 complaint (&symfile_complaints,
5998 _("Invalid .debug_ranges data (no base address)"));
5999 return 0;
6000 }
6001
9277c30c
UW
6002 if (range_beginning > range_end)
6003 {
6004 /* Inverted range entries are invalid. */
6005 complaint (&symfile_complaints,
6006 _("Invalid .debug_ranges data (inverted range)"));
6007 return 0;
6008 }
6009
6010 /* Empty range entries have no effect. */
6011 if (range_beginning == range_end)
6012 continue;
6013
43039443
JK
6014 range_beginning += base;
6015 range_end += base;
6016
9277c30c 6017 if (ranges_pst != NULL)
ff013f42 6018 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6019 range_beginning + baseaddr,
6020 range_end - 1 + baseaddr,
ff013f42
JK
6021 ranges_pst);
6022
43039443
JK
6023 /* FIXME: This is recording everything as a low-high
6024 segment of consecutive addresses. We should have a
6025 data structure for discontiguous block ranges
6026 instead. */
6027 if (! low_set)
6028 {
6029 low = range_beginning;
6030 high = range_end;
6031 low_set = 1;
6032 }
6033 else
6034 {
6035 if (range_beginning < low)
6036 low = range_beginning;
6037 if (range_end > high)
6038 high = range_end;
6039 }
6040 }
6041
6042 if (! low_set)
6043 /* If the first entry is an end-of-list marker, the range
6044 describes an empty scope, i.e. no instructions. */
6045 return 0;
6046
6047 if (low_return)
6048 *low_return = low;
6049 if (high_return)
6050 *high_return = high;
6051 return 1;
6052}
6053
af34e669
DJ
6054/* Get low and high pc attributes from a die. Return 1 if the attributes
6055 are present and valid, otherwise, return 0. Return -1 if the range is
6056 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6057static int
af34e669 6058dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6059 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6060 struct partial_symtab *pst)
c906108c
SS
6061{
6062 struct attribute *attr;
af34e669
DJ
6063 CORE_ADDR low = 0;
6064 CORE_ADDR high = 0;
6065 int ret = 0;
c906108c 6066
e142c38c 6067 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6068 if (attr)
af34e669
DJ
6069 {
6070 high = DW_ADDR (attr);
e142c38c 6071 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6072 if (attr)
6073 low = DW_ADDR (attr);
6074 else
6075 /* Found high w/o low attribute. */
6076 return 0;
6077
6078 /* Found consecutive range of addresses. */
6079 ret = 1;
6080 }
c906108c 6081 else
af34e669 6082 {
e142c38c 6083 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6084 if (attr != NULL)
6085 {
af34e669 6086 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6087 .debug_ranges section. */
d85a05f0 6088 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6089 return 0;
43039443 6090 /* Found discontinuous range of addresses. */
af34e669
DJ
6091 ret = -1;
6092 }
6093 }
c906108c 6094
9373cf26
JK
6095 /* read_partial_die has also the strict LOW < HIGH requirement. */
6096 if (high <= low)
c906108c
SS
6097 return 0;
6098
6099 /* When using the GNU linker, .gnu.linkonce. sections are used to
6100 eliminate duplicate copies of functions and vtables and such.
6101 The linker will arbitrarily choose one and discard the others.
6102 The AT_*_pc values for such functions refer to local labels in
6103 these sections. If the section from that file was discarded, the
6104 labels are not in the output, so the relocs get a value of 0.
6105 If this is a discarded function, mark the pc bounds as invalid,
6106 so that GDB will ignore it. */
72dca2f5 6107 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6108 return 0;
6109
6110 *lowpc = low;
6111 *highpc = high;
af34e669 6112 return ret;
c906108c
SS
6113}
6114
b084d499
JB
6115/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6116 its low and high PC addresses. Do nothing if these addresses could not
6117 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6118 and HIGHPC to the high address if greater than HIGHPC. */
6119
6120static void
6121dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6122 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6123 struct dwarf2_cu *cu)
6124{
6125 CORE_ADDR low, high;
6126 struct die_info *child = die->child;
6127
d85a05f0 6128 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6129 {
6130 *lowpc = min (*lowpc, low);
6131 *highpc = max (*highpc, high);
6132 }
6133
6134 /* If the language does not allow nested subprograms (either inside
6135 subprograms or lexical blocks), we're done. */
6136 if (cu->language != language_ada)
6137 return;
6e70227d 6138
b084d499
JB
6139 /* Check all the children of the given DIE. If it contains nested
6140 subprograms, then check their pc bounds. Likewise, we need to
6141 check lexical blocks as well, as they may also contain subprogram
6142 definitions. */
6143 while (child && child->tag)
6144 {
6145 if (child->tag == DW_TAG_subprogram
6146 || child->tag == DW_TAG_lexical_block)
6147 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6148 child = sibling_die (child);
6149 }
6150}
6151
fae299cd
DC
6152/* Get the low and high pc's represented by the scope DIE, and store
6153 them in *LOWPC and *HIGHPC. If the correct values can't be
6154 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6155
6156static void
6157get_scope_pc_bounds (struct die_info *die,
6158 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6159 struct dwarf2_cu *cu)
6160{
6161 CORE_ADDR best_low = (CORE_ADDR) -1;
6162 CORE_ADDR best_high = (CORE_ADDR) 0;
6163 CORE_ADDR current_low, current_high;
6164
d85a05f0 6165 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6166 {
6167 best_low = current_low;
6168 best_high = current_high;
6169 }
6170 else
6171 {
6172 struct die_info *child = die->child;
6173
6174 while (child && child->tag)
6175 {
6176 switch (child->tag) {
6177 case DW_TAG_subprogram:
b084d499 6178 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6179 break;
6180 case DW_TAG_namespace:
f55ee35c 6181 case DW_TAG_module:
fae299cd
DC
6182 /* FIXME: carlton/2004-01-16: Should we do this for
6183 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6184 that current GCC's always emit the DIEs corresponding
6185 to definitions of methods of classes as children of a
6186 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6187 the DIEs giving the declarations, which could be
6188 anywhere). But I don't see any reason why the
6189 standards says that they have to be there. */
6190 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6191
6192 if (current_low != ((CORE_ADDR) -1))
6193 {
6194 best_low = min (best_low, current_low);
6195 best_high = max (best_high, current_high);
6196 }
6197 break;
6198 default:
0963b4bd 6199 /* Ignore. */
fae299cd
DC
6200 break;
6201 }
6202
6203 child = sibling_die (child);
6204 }
6205 }
6206
6207 *lowpc = best_low;
6208 *highpc = best_high;
6209}
6210
801e3a5b
JB
6211/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6212 in DIE. */
6213static void
6214dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6215 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6216{
6217 struct attribute *attr;
6218
6219 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6220 if (attr)
6221 {
6222 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6223
801e3a5b
JB
6224 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6225 if (attr)
6226 {
6227 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6228
801e3a5b
JB
6229 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6230 }
6231 }
6232
6233 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6234 if (attr)
6235 {
6236 bfd *obfd = cu->objfile->obfd;
6237
6238 /* The value of the DW_AT_ranges attribute is the offset of the
6239 address range list in the .debug_ranges section. */
6240 unsigned long offset = DW_UNSND (attr);
dce234bc 6241 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6242
6243 /* For some target architectures, but not others, the
6244 read_address function sign-extends the addresses it returns.
6245 To recognize base address selection entries, we need a
6246 mask. */
6247 unsigned int addr_size = cu->header.addr_size;
6248 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6249
6250 /* The base address, to which the next pair is relative. Note
6251 that this 'base' is a DWARF concept: most entries in a range
6252 list are relative, to reduce the number of relocs against the
6253 debugging information. This is separate from this function's
6254 'baseaddr' argument, which GDB uses to relocate debugging
6255 information from a shared library based on the address at
6256 which the library was loaded. */
d00adf39
DE
6257 CORE_ADDR base = cu->base_address;
6258 int base_known = cu->base_known;
801e3a5b 6259
be391dca 6260 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6261 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6262 {
6263 complaint (&symfile_complaints,
6264 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6265 offset);
6266 return;
6267 }
6268
6269 for (;;)
6270 {
6271 unsigned int bytes_read;
6272 CORE_ADDR start, end;
6273
6274 start = read_address (obfd, buffer, cu, &bytes_read);
6275 buffer += bytes_read;
6276 end = read_address (obfd, buffer, cu, &bytes_read);
6277 buffer += bytes_read;
6278
6279 /* Did we find the end of the range list? */
6280 if (start == 0 && end == 0)
6281 break;
6282
6283 /* Did we find a base address selection entry? */
6284 else if ((start & base_select_mask) == base_select_mask)
6285 {
6286 base = end;
6287 base_known = 1;
6288 }
6289
6290 /* We found an ordinary address range. */
6291 else
6292 {
6293 if (!base_known)
6294 {
6295 complaint (&symfile_complaints,
3e43a32a
MS
6296 _("Invalid .debug_ranges data "
6297 "(no base address)"));
801e3a5b
JB
6298 return;
6299 }
6300
9277c30c
UW
6301 if (start > end)
6302 {
6303 /* Inverted range entries are invalid. */
6304 complaint (&symfile_complaints,
6305 _("Invalid .debug_ranges data "
6306 "(inverted range)"));
6307 return;
6308 }
6309
6310 /* Empty range entries have no effect. */
6311 if (start == end)
6312 continue;
6313
6e70227d
DE
6314 record_block_range (block,
6315 baseaddr + base + start,
801e3a5b
JB
6316 baseaddr + base + end - 1);
6317 }
6318 }
6319 }
6320}
6321
60d5a603
JK
6322/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6323 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6324 during 4.6.0 experimental. */
6325
6326static int
6327producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6328{
6329 const char *cs;
6330 int major, minor, release;
6331
6332 if (cu->producer == NULL)
6333 {
6334 /* For unknown compilers expect their behavior is DWARF version
6335 compliant.
6336
6337 GCC started to support .debug_types sections by -gdwarf-4 since
6338 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6339 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6340 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6341 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6342
6343 return 0;
6344 }
6345
6346 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6347
6348 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6349 {
6350 /* For non-GCC compilers expect their behavior is DWARF version
6351 compliant. */
6352
6353 return 0;
6354 }
6355 cs = &cu->producer[strlen ("GNU ")];
6356 while (*cs && !isdigit (*cs))
6357 cs++;
6358 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6359 {
6360 /* Not recognized as GCC. */
6361
6362 return 0;
6363 }
6364
6365 return major < 4 || (major == 4 && minor < 6);
6366}
6367
6368/* Return the default accessibility type if it is not overriden by
6369 DW_AT_accessibility. */
6370
6371static enum dwarf_access_attribute
6372dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6373{
6374 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6375 {
6376 /* The default DWARF 2 accessibility for members is public, the default
6377 accessibility for inheritance is private. */
6378
6379 if (die->tag != DW_TAG_inheritance)
6380 return DW_ACCESS_public;
6381 else
6382 return DW_ACCESS_private;
6383 }
6384 else
6385 {
6386 /* DWARF 3+ defines the default accessibility a different way. The same
6387 rules apply now for DW_TAG_inheritance as for the members and it only
6388 depends on the container kind. */
6389
6390 if (die->parent->tag == DW_TAG_class_type)
6391 return DW_ACCESS_private;
6392 else
6393 return DW_ACCESS_public;
6394 }
6395}
6396
74ac6d43
TT
6397/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6398 offset. If the attribute was not found return 0, otherwise return
6399 1. If it was found but could not properly be handled, set *OFFSET
6400 to 0. */
6401
6402static int
6403handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6404 LONGEST *offset)
6405{
6406 struct attribute *attr;
6407
6408 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6409 if (attr != NULL)
6410 {
6411 *offset = 0;
6412
6413 /* Note that we do not check for a section offset first here.
6414 This is because DW_AT_data_member_location is new in DWARF 4,
6415 so if we see it, we can assume that a constant form is really
6416 a constant and not a section offset. */
6417 if (attr_form_is_constant (attr))
6418 *offset = dwarf2_get_attr_constant_value (attr, 0);
6419 else if (attr_form_is_section_offset (attr))
6420 dwarf2_complex_location_expr_complaint ();
6421 else if (attr_form_is_block (attr))
6422 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6423 else
6424 dwarf2_complex_location_expr_complaint ();
6425
6426 return 1;
6427 }
6428
6429 return 0;
6430}
6431
c906108c
SS
6432/* Add an aggregate field to the field list. */
6433
6434static void
107d2387 6435dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6436 struct dwarf2_cu *cu)
6e70227d 6437{
e7c27a73 6438 struct objfile *objfile = cu->objfile;
5e2b427d 6439 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6440 struct nextfield *new_field;
6441 struct attribute *attr;
6442 struct field *fp;
6443 char *fieldname = "";
6444
6445 /* Allocate a new field list entry and link it in. */
6446 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6447 make_cleanup (xfree, new_field);
c906108c 6448 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6449
6450 if (die->tag == DW_TAG_inheritance)
6451 {
6452 new_field->next = fip->baseclasses;
6453 fip->baseclasses = new_field;
6454 }
6455 else
6456 {
6457 new_field->next = fip->fields;
6458 fip->fields = new_field;
6459 }
c906108c
SS
6460 fip->nfields++;
6461
e142c38c 6462 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6463 if (attr)
6464 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
6465 else
6466 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
6467 if (new_field->accessibility != DW_ACCESS_public)
6468 fip->non_public_fields = 1;
60d5a603 6469
e142c38c 6470 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6471 if (attr)
6472 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
6473 else
6474 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
6475
6476 fp = &new_field->field;
a9a9bd0f 6477
e142c38c 6478 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6479 {
74ac6d43
TT
6480 LONGEST offset;
6481
a9a9bd0f 6482 /* Data member other than a C++ static data member. */
6e70227d 6483
c906108c 6484 /* Get type of field. */
e7c27a73 6485 fp->type = die_type (die, cu);
c906108c 6486
d6a843b5 6487 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6488
c906108c 6489 /* Get bit size of field (zero if none). */
e142c38c 6490 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6491 if (attr)
6492 {
6493 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6494 }
6495 else
6496 {
6497 FIELD_BITSIZE (*fp) = 0;
6498 }
6499
6500 /* Get bit offset of field. */
74ac6d43
TT
6501 if (handle_data_member_location (die, cu, &offset))
6502 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 6503 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6504 if (attr)
6505 {
5e2b427d 6506 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6507 {
6508 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6509 additional bit offset from the MSB of the containing
6510 anonymous object to the MSB of the field. We don't
6511 have to do anything special since we don't need to
6512 know the size of the anonymous object. */
c906108c
SS
6513 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6514 }
6515 else
6516 {
6517 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6518 MSB of the anonymous object, subtract off the number of
6519 bits from the MSB of the field to the MSB of the
6520 object, and then subtract off the number of bits of
6521 the field itself. The result is the bit offset of
6522 the LSB of the field. */
c906108c
SS
6523 int anonymous_size;
6524 int bit_offset = DW_UNSND (attr);
6525
e142c38c 6526 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6527 if (attr)
6528 {
6529 /* The size of the anonymous object containing
6530 the bit field is explicit, so use the
6531 indicated size (in bytes). */
6532 anonymous_size = DW_UNSND (attr);
6533 }
6534 else
6535 {
6536 /* The size of the anonymous object containing
6537 the bit field must be inferred from the type
6538 attribute of the data member containing the
6539 bit field. */
6540 anonymous_size = TYPE_LENGTH (fp->type);
6541 }
6542 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6543 - bit_offset - FIELD_BITSIZE (*fp);
6544 }
6545 }
6546
6547 /* Get name of field. */
39cbfefa
DJ
6548 fieldname = dwarf2_name (die, cu);
6549 if (fieldname == NULL)
6550 fieldname = "";
d8151005
DJ
6551
6552 /* The name is already allocated along with this objfile, so we don't
6553 need to duplicate it for the type. */
6554 fp->name = fieldname;
c906108c
SS
6555
6556 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6557 pointer or virtual base class pointer) to private. */
e142c38c 6558 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6559 {
d48cc9dd 6560 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6561 new_field->accessibility = DW_ACCESS_private;
6562 fip->non_public_fields = 1;
6563 }
6564 }
a9a9bd0f 6565 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6566 {
a9a9bd0f
DC
6567 /* C++ static member. */
6568
6569 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6570 is a declaration, but all versions of G++ as of this writing
6571 (so through at least 3.2.1) incorrectly generate
6572 DW_TAG_variable tags. */
6e70227d 6573
ff355380 6574 const char *physname;
c906108c 6575
a9a9bd0f 6576 /* Get name of field. */
39cbfefa
DJ
6577 fieldname = dwarf2_name (die, cu);
6578 if (fieldname == NULL)
c906108c
SS
6579 return;
6580
254e6b9e 6581 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6582 if (attr
6583 /* Only create a symbol if this is an external value.
6584 new_symbol checks this and puts the value in the global symbol
6585 table, which we want. If it is not external, new_symbol
6586 will try to put the value in cu->list_in_scope which is wrong. */
6587 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6588 {
6589 /* A static const member, not much different than an enum as far as
6590 we're concerned, except that we can support more types. */
6591 new_symbol (die, NULL, cu);
6592 }
6593
2df3850c 6594 /* Get physical name. */
ff355380 6595 physname = dwarf2_physname (fieldname, die, cu);
c906108c 6596
d8151005
DJ
6597 /* The name is already allocated along with this objfile, so we don't
6598 need to duplicate it for the type. */
6599 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6600 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6601 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6602 }
6603 else if (die->tag == DW_TAG_inheritance)
6604 {
74ac6d43 6605 LONGEST offset;
d4b96c9a 6606
74ac6d43
TT
6607 /* C++ base class field. */
6608 if (handle_data_member_location (die, cu, &offset))
6609 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 6610 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6611 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6612 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6613 fip->nbaseclasses++;
6614 }
6615}
6616
98751a41
JK
6617/* Add a typedef defined in the scope of the FIP's class. */
6618
6619static void
6620dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6621 struct dwarf2_cu *cu)
6e70227d 6622{
98751a41 6623 struct objfile *objfile = cu->objfile;
98751a41
JK
6624 struct typedef_field_list *new_field;
6625 struct attribute *attr;
6626 struct typedef_field *fp;
6627 char *fieldname = "";
6628
6629 /* Allocate a new field list entry and link it in. */
6630 new_field = xzalloc (sizeof (*new_field));
6631 make_cleanup (xfree, new_field);
6632
6633 gdb_assert (die->tag == DW_TAG_typedef);
6634
6635 fp = &new_field->field;
6636
6637 /* Get name of field. */
6638 fp->name = dwarf2_name (die, cu);
6639 if (fp->name == NULL)
6640 return;
6641
6642 fp->type = read_type_die (die, cu);
6643
6644 new_field->next = fip->typedef_field_list;
6645 fip->typedef_field_list = new_field;
6646 fip->typedef_field_list_count++;
6647}
6648
c906108c
SS
6649/* Create the vector of fields, and attach it to the type. */
6650
6651static void
fba45db2 6652dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6653 struct dwarf2_cu *cu)
c906108c
SS
6654{
6655 int nfields = fip->nfields;
6656
6657 /* Record the field count, allocate space for the array of fields,
6658 and create blank accessibility bitfields if necessary. */
6659 TYPE_NFIELDS (type) = nfields;
6660 TYPE_FIELDS (type) = (struct field *)
6661 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6662 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6663
b4ba55a1 6664 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6665 {
6666 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6667
6668 TYPE_FIELD_PRIVATE_BITS (type) =
6669 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6670 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6671
6672 TYPE_FIELD_PROTECTED_BITS (type) =
6673 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6674 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6675
774b6a14
TT
6676 TYPE_FIELD_IGNORE_BITS (type) =
6677 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6678 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6679 }
6680
6681 /* If the type has baseclasses, allocate and clear a bit vector for
6682 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6683 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6684 {
6685 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6686 unsigned char *pointer;
c906108c
SS
6687
6688 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6689 pointer = TYPE_ALLOC (type, num_bytes);
6690 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6691 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6692 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6693 }
6694
3e43a32a
MS
6695 /* Copy the saved-up fields into the field vector. Start from the head of
6696 the list, adding to the tail of the field array, so that they end up in
6697 the same order in the array in which they were added to the list. */
c906108c
SS
6698 while (nfields-- > 0)
6699 {
7d0ccb61
DJ
6700 struct nextfield *fieldp;
6701
6702 if (fip->fields)
6703 {
6704 fieldp = fip->fields;
6705 fip->fields = fieldp->next;
6706 }
6707 else
6708 {
6709 fieldp = fip->baseclasses;
6710 fip->baseclasses = fieldp->next;
6711 }
6712
6713 TYPE_FIELD (type, nfields) = fieldp->field;
6714 switch (fieldp->accessibility)
c906108c 6715 {
c5aa993b 6716 case DW_ACCESS_private:
b4ba55a1
JB
6717 if (cu->language != language_ada)
6718 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6719 break;
c906108c 6720
c5aa993b 6721 case DW_ACCESS_protected:
b4ba55a1
JB
6722 if (cu->language != language_ada)
6723 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6724 break;
c906108c 6725
c5aa993b
JM
6726 case DW_ACCESS_public:
6727 break;
c906108c 6728
c5aa993b
JM
6729 default:
6730 /* Unknown accessibility. Complain and treat it as public. */
6731 {
e2e0b3e5 6732 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6733 fieldp->accessibility);
c5aa993b
JM
6734 }
6735 break;
c906108c
SS
6736 }
6737 if (nfields < fip->nbaseclasses)
6738 {
7d0ccb61 6739 switch (fieldp->virtuality)
c906108c 6740 {
c5aa993b
JM
6741 case DW_VIRTUALITY_virtual:
6742 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 6743 if (cu->language == language_ada)
a73c6dcd 6744 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
6745 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6746 break;
c906108c
SS
6747 }
6748 }
c906108c
SS
6749 }
6750}
6751
c906108c
SS
6752/* Add a member function to the proper fieldlist. */
6753
6754static void
107d2387 6755dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6756 struct type *type, struct dwarf2_cu *cu)
c906108c 6757{
e7c27a73 6758 struct objfile *objfile = cu->objfile;
c906108c
SS
6759 struct attribute *attr;
6760 struct fnfieldlist *flp;
6761 int i;
6762 struct fn_field *fnp;
6763 char *fieldname;
c906108c 6764 struct nextfnfield *new_fnfield;
f792889a 6765 struct type *this_type;
60d5a603 6766 enum dwarf_access_attribute accessibility;
c906108c 6767
b4ba55a1 6768 if (cu->language == language_ada)
a73c6dcd 6769 error (_("unexpected member function in Ada type"));
b4ba55a1 6770
2df3850c 6771 /* Get name of member function. */
39cbfefa
DJ
6772 fieldname = dwarf2_name (die, cu);
6773 if (fieldname == NULL)
2df3850c 6774 return;
c906108c 6775
c906108c
SS
6776 /* Look up member function name in fieldlist. */
6777 for (i = 0; i < fip->nfnfields; i++)
6778 {
27bfe10e 6779 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6780 break;
6781 }
6782
6783 /* Create new list element if necessary. */
6784 if (i < fip->nfnfields)
6785 flp = &fip->fnfieldlists[i];
6786 else
6787 {
6788 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6789 {
6790 fip->fnfieldlists = (struct fnfieldlist *)
6791 xrealloc (fip->fnfieldlists,
6792 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6793 * sizeof (struct fnfieldlist));
c906108c 6794 if (fip->nfnfields == 0)
c13c43fd 6795 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6796 }
6797 flp = &fip->fnfieldlists[fip->nfnfields];
6798 flp->name = fieldname;
6799 flp->length = 0;
6800 flp->head = NULL;
3da10d80 6801 i = fip->nfnfields++;
c906108c
SS
6802 }
6803
6804 /* Create a new member function field and chain it to the field list
0963b4bd 6805 entry. */
c906108c 6806 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6807 make_cleanup (xfree, new_fnfield);
c906108c
SS
6808 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6809 new_fnfield->next = flp->head;
6810 flp->head = new_fnfield;
6811 flp->length++;
6812
6813 /* Fill in the member function field info. */
6814 fnp = &new_fnfield->fnfield;
3da10d80
KS
6815
6816 /* Delay processing of the physname until later. */
6817 if (cu->language == language_cplus || cu->language == language_java)
6818 {
6819 add_to_method_list (type, i, flp->length - 1, fieldname,
6820 die, cu);
6821 }
6822 else
6823 {
1d06ead6 6824 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
6825 fnp->physname = physname ? physname : "";
6826 }
6827
c906108c 6828 fnp->type = alloc_type (objfile);
f792889a
DJ
6829 this_type = read_type_die (die, cu);
6830 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6831 {
f792889a 6832 int nparams = TYPE_NFIELDS (this_type);
c906108c 6833
f792889a 6834 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6835 of the method itself (TYPE_CODE_METHOD). */
6836 smash_to_method_type (fnp->type, type,
f792889a
DJ
6837 TYPE_TARGET_TYPE (this_type),
6838 TYPE_FIELDS (this_type),
6839 TYPE_NFIELDS (this_type),
6840 TYPE_VARARGS (this_type));
c906108c
SS
6841
6842 /* Handle static member functions.
c5aa993b 6843 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
6844 member functions. G++ helps GDB by marking the first
6845 parameter for non-static member functions (which is the this
6846 pointer) as artificial. We obtain this information from
6847 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6848 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6849 fnp->voffset = VOFFSET_STATIC;
6850 }
6851 else
e2e0b3e5 6852 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6853 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6854
6855 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6856 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6857 fnp->fcontext = die_containing_type (die, cu);
c906108c 6858
3e43a32a
MS
6859 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6860 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
6861
6862 /* Get accessibility. */
e142c38c 6863 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 6864 if (attr)
60d5a603
JK
6865 accessibility = DW_UNSND (attr);
6866 else
6867 accessibility = dwarf2_default_access_attribute (die, cu);
6868 switch (accessibility)
c906108c 6869 {
60d5a603
JK
6870 case DW_ACCESS_private:
6871 fnp->is_private = 1;
6872 break;
6873 case DW_ACCESS_protected:
6874 fnp->is_protected = 1;
6875 break;
c906108c
SS
6876 }
6877
b02dede2 6878 /* Check for artificial methods. */
e142c38c 6879 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6880 if (attr && DW_UNSND (attr) != 0)
6881 fnp->is_artificial = 1;
6882
0d564a31 6883 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6884 function. For older versions of GCC, this is an offset in the
6885 appropriate virtual table, as specified by DW_AT_containing_type.
6886 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6887 to the object address. */
6888
e142c38c 6889 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6890 if (attr)
8e19ed76 6891 {
aec5aa8b 6892 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6893 {
aec5aa8b
TT
6894 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6895 {
6896 /* Old-style GCC. */
6897 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6898 }
6899 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6900 || (DW_BLOCK (attr)->size > 1
6901 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6902 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6903 {
6904 struct dwarf_block blk;
6905 int offset;
6906
6907 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6908 ? 1 : 2);
6909 blk.size = DW_BLOCK (attr)->size - offset;
6910 blk.data = DW_BLOCK (attr)->data + offset;
6911 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6912 if ((fnp->voffset % cu->header.addr_size) != 0)
6913 dwarf2_complex_location_expr_complaint ();
6914 else
6915 fnp->voffset /= cu->header.addr_size;
6916 fnp->voffset += 2;
6917 }
6918 else
6919 dwarf2_complex_location_expr_complaint ();
6920
6921 if (!fnp->fcontext)
6922 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6923 }
3690dd37 6924 else if (attr_form_is_section_offset (attr))
8e19ed76 6925 {
4d3c2250 6926 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6927 }
6928 else
6929 {
4d3c2250
KB
6930 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6931 fieldname);
8e19ed76 6932 }
0d564a31 6933 }
d48cc9dd
DJ
6934 else
6935 {
6936 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6937 if (attr && DW_UNSND (attr))
6938 {
6939 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6940 complaint (&symfile_complaints,
3e43a32a
MS
6941 _("Member function \"%s\" (offset %d) is virtual "
6942 "but the vtable offset is not specified"),
d48cc9dd 6943 fieldname, die->offset);
9655fd1a 6944 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6945 TYPE_CPLUS_DYNAMIC (type) = 1;
6946 }
6947 }
c906108c
SS
6948}
6949
6950/* Create the vector of member function fields, and attach it to the type. */
6951
6952static void
fba45db2 6953dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6954 struct dwarf2_cu *cu)
c906108c
SS
6955{
6956 struct fnfieldlist *flp;
6957 int total_length = 0;
6958 int i;
6959
b4ba55a1 6960 if (cu->language == language_ada)
a73c6dcd 6961 error (_("unexpected member functions in Ada type"));
b4ba55a1 6962
c906108c
SS
6963 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6964 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6965 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6966
6967 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6968 {
6969 struct nextfnfield *nfp = flp->head;
6970 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6971 int k;
6972
6973 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6974 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6975 fn_flp->fn_fields = (struct fn_field *)
6976 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6977 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6978 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6979
6980 total_length += flp->length;
6981 }
6982
6983 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6984 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6985}
6986
1168df01
JB
6987/* Returns non-zero if NAME is the name of a vtable member in CU's
6988 language, zero otherwise. */
6989static int
6990is_vtable_name (const char *name, struct dwarf2_cu *cu)
6991{
6992 static const char vptr[] = "_vptr";
987504bb 6993 static const char vtable[] = "vtable";
1168df01 6994
987504bb
JJ
6995 /* Look for the C++ and Java forms of the vtable. */
6996 if ((cu->language == language_java
6997 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6998 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6999 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7000 return 1;
7001
7002 return 0;
7003}
7004
c0dd20ea 7005/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7006 functions, with the ABI-specified layout. If TYPE describes
7007 such a structure, smash it into a member function type.
61049d3b
DJ
7008
7009 GCC shouldn't do this; it should just output pointer to member DIEs.
7010 This is GCC PR debug/28767. */
c0dd20ea 7011
0b92b5bb
TT
7012static void
7013quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7014{
0b92b5bb 7015 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7016
7017 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7018 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7019 return;
c0dd20ea
DJ
7020
7021 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7022 if (TYPE_FIELD_NAME (type, 0) == NULL
7023 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7024 || TYPE_FIELD_NAME (type, 1) == NULL
7025 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7026 return;
c0dd20ea
DJ
7027
7028 /* Find the type of the method. */
0b92b5bb 7029 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7030 if (pfn_type == NULL
7031 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7032 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7033 return;
c0dd20ea
DJ
7034
7035 /* Look for the "this" argument. */
7036 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7037 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7038 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7039 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7040 return;
c0dd20ea
DJ
7041
7042 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7043 new_type = alloc_type (objfile);
7044 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7045 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7046 TYPE_VARARGS (pfn_type));
0b92b5bb 7047 smash_to_methodptr_type (type, new_type);
c0dd20ea 7048}
1168df01 7049
c906108c 7050/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7051 (definition) to create a type for the structure or union. Fill in
7052 the type's name and general properties; the members will not be
7053 processed until process_structure_type.
c906108c 7054
c767944b
DJ
7055 NOTE: we need to call these functions regardless of whether or not the
7056 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7057 structure or union. This gets the type entered into our set of
7058 user defined types.
7059
7060 However, if the structure is incomplete (an opaque struct/union)
7061 then suppress creating a symbol table entry for it since gdb only
7062 wants to find the one with the complete definition. Note that if
7063 it is complete, we just call new_symbol, which does it's own
7064 checking about whether the struct/union is anonymous or not (and
7065 suppresses creating a symbol table entry itself). */
7066
f792889a 7067static struct type *
134d01f1 7068read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7069{
e7c27a73 7070 struct objfile *objfile = cu->objfile;
c906108c
SS
7071 struct type *type;
7072 struct attribute *attr;
39cbfefa 7073 char *name;
c906108c 7074
348e048f
DE
7075 /* If the definition of this type lives in .debug_types, read that type.
7076 Don't follow DW_AT_specification though, that will take us back up
7077 the chain and we want to go down. */
7078 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7079 if (attr)
7080 {
7081 struct dwarf2_cu *type_cu = cu;
7082 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7083
348e048f
DE
7084 /* We could just recurse on read_structure_type, but we need to call
7085 get_die_type to ensure only one type for this DIE is created.
7086 This is important, for example, because for c++ classes we need
7087 TYPE_NAME set which is only done by new_symbol. Blech. */
7088 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7089
7090 /* TYPE_CU may not be the same as CU.
7091 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7092 return set_die_type (die, type, cu);
7093 }
7094
c0dd20ea 7095 type = alloc_type (objfile);
c906108c 7096 INIT_CPLUS_SPECIFIC (type);
93311388 7097
39cbfefa
DJ
7098 name = dwarf2_name (die, cu);
7099 if (name != NULL)
c906108c 7100 {
987504bb
JJ
7101 if (cu->language == language_cplus
7102 || cu->language == language_java)
63d06c5c 7103 {
3da10d80
KS
7104 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7105
7106 /* dwarf2_full_name might have already finished building the DIE's
7107 type. If so, there is no need to continue. */
7108 if (get_die_type (die, cu) != NULL)
7109 return get_die_type (die, cu);
7110
7111 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7112 if (die->tag == DW_TAG_structure_type
7113 || die->tag == DW_TAG_class_type)
7114 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7115 }
7116 else
7117 {
d8151005
DJ
7118 /* The name is already allocated along with this objfile, so
7119 we don't need to duplicate it for the type. */
94af9270
KS
7120 TYPE_TAG_NAME (type) = (char *) name;
7121 if (die->tag == DW_TAG_class_type)
7122 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7123 }
c906108c
SS
7124 }
7125
7126 if (die->tag == DW_TAG_structure_type)
7127 {
7128 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7129 }
7130 else if (die->tag == DW_TAG_union_type)
7131 {
7132 TYPE_CODE (type) = TYPE_CODE_UNION;
7133 }
7134 else
7135 {
c906108c
SS
7136 TYPE_CODE (type) = TYPE_CODE_CLASS;
7137 }
7138
0cc2414c
TT
7139 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7140 TYPE_DECLARED_CLASS (type) = 1;
7141
e142c38c 7142 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7143 if (attr)
7144 {
7145 TYPE_LENGTH (type) = DW_UNSND (attr);
7146 }
7147 else
7148 {
7149 TYPE_LENGTH (type) = 0;
7150 }
7151
876cecd0 7152 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7153 if (die_is_declaration (die, cu))
876cecd0 7154 TYPE_STUB (type) = 1;
a6c727b2
DJ
7155 else if (attr == NULL && die->child == NULL
7156 && producer_is_realview (cu->producer))
7157 /* RealView does not output the required DW_AT_declaration
7158 on incomplete types. */
7159 TYPE_STUB (type) = 1;
dc718098 7160
c906108c
SS
7161 /* We need to add the type field to the die immediately so we don't
7162 infinitely recurse when dealing with pointers to the structure
0963b4bd 7163 type within the structure itself. */
1c379e20 7164 set_die_type (die, type, cu);
c906108c 7165
7e314c57
JK
7166 /* set_die_type should be already done. */
7167 set_descriptive_type (type, die, cu);
7168
c767944b
DJ
7169 return type;
7170}
7171
7172/* Finish creating a structure or union type, including filling in
7173 its members and creating a symbol for it. */
7174
7175static void
7176process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7177{
7178 struct objfile *objfile = cu->objfile;
7179 struct die_info *child_die = die->child;
7180 struct type *type;
7181
7182 type = get_die_type (die, cu);
7183 if (type == NULL)
7184 type = read_structure_type (die, cu);
7185
e142c38c 7186 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7187 {
7188 struct field_info fi;
7189 struct die_info *child_die;
34eaf542 7190 VEC (symbolp) *template_args = NULL;
c767944b 7191 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7192
7193 memset (&fi, 0, sizeof (struct field_info));
7194
639d11d3 7195 child_die = die->child;
c906108c
SS
7196
7197 while (child_die && child_die->tag)
7198 {
a9a9bd0f
DC
7199 if (child_die->tag == DW_TAG_member
7200 || child_die->tag == DW_TAG_variable)
c906108c 7201 {
a9a9bd0f
DC
7202 /* NOTE: carlton/2002-11-05: A C++ static data member
7203 should be a DW_TAG_member that is a declaration, but
7204 all versions of G++ as of this writing (so through at
7205 least 3.2.1) incorrectly generate DW_TAG_variable
7206 tags for them instead. */
e7c27a73 7207 dwarf2_add_field (&fi, child_die, cu);
c906108c 7208 }
8713b1b1 7209 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7210 {
0963b4bd 7211 /* C++ member function. */
e7c27a73 7212 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7213 }
7214 else if (child_die->tag == DW_TAG_inheritance)
7215 {
7216 /* C++ base class field. */
e7c27a73 7217 dwarf2_add_field (&fi, child_die, cu);
c906108c 7218 }
98751a41
JK
7219 else if (child_die->tag == DW_TAG_typedef)
7220 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7221 else if (child_die->tag == DW_TAG_template_type_param
7222 || child_die->tag == DW_TAG_template_value_param)
7223 {
7224 struct symbol *arg = new_symbol (child_die, NULL, cu);
7225
f1078f66
DJ
7226 if (arg != NULL)
7227 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7228 }
7229
c906108c
SS
7230 child_die = sibling_die (child_die);
7231 }
7232
34eaf542
TT
7233 /* Attach template arguments to type. */
7234 if (! VEC_empty (symbolp, template_args))
7235 {
7236 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7237 TYPE_N_TEMPLATE_ARGUMENTS (type)
7238 = VEC_length (symbolp, template_args);
7239 TYPE_TEMPLATE_ARGUMENTS (type)
7240 = obstack_alloc (&objfile->objfile_obstack,
7241 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7242 * sizeof (struct symbol *)));
7243 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7244 VEC_address (symbolp, template_args),
7245 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7246 * sizeof (struct symbol *)));
7247 VEC_free (symbolp, template_args);
7248 }
7249
c906108c
SS
7250 /* Attach fields and member functions to the type. */
7251 if (fi.nfields)
e7c27a73 7252 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7253 if (fi.nfnfields)
7254 {
e7c27a73 7255 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7256
c5aa993b 7257 /* Get the type which refers to the base class (possibly this
c906108c 7258 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7259 class from the DW_AT_containing_type attribute. This use of
7260 DW_AT_containing_type is a GNU extension. */
c906108c 7261
e142c38c 7262 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7263 {
e7c27a73 7264 struct type *t = die_containing_type (die, cu);
c906108c
SS
7265
7266 TYPE_VPTR_BASETYPE (type) = t;
7267 if (type == t)
7268 {
c906108c
SS
7269 int i;
7270
7271 /* Our own class provides vtbl ptr. */
7272 for (i = TYPE_NFIELDS (t) - 1;
7273 i >= TYPE_N_BASECLASSES (t);
7274 --i)
7275 {
7276 char *fieldname = TYPE_FIELD_NAME (t, i);
7277
1168df01 7278 if (is_vtable_name (fieldname, cu))
c906108c
SS
7279 {
7280 TYPE_VPTR_FIELDNO (type) = i;
7281 break;
7282 }
7283 }
7284
7285 /* Complain if virtual function table field not found. */
7286 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7287 complaint (&symfile_complaints,
3e43a32a
MS
7288 _("virtual function table pointer "
7289 "not found when defining class '%s'"),
4d3c2250
KB
7290 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7291 "");
c906108c
SS
7292 }
7293 else
7294 {
7295 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7296 }
7297 }
f6235d4c
EZ
7298 else if (cu->producer
7299 && strncmp (cu->producer,
7300 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7301 {
7302 /* The IBM XLC compiler does not provide direct indication
7303 of the containing type, but the vtable pointer is
7304 always named __vfp. */
7305
7306 int i;
7307
7308 for (i = TYPE_NFIELDS (type) - 1;
7309 i >= TYPE_N_BASECLASSES (type);
7310 --i)
7311 {
7312 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7313 {
7314 TYPE_VPTR_FIELDNO (type) = i;
7315 TYPE_VPTR_BASETYPE (type) = type;
7316 break;
7317 }
7318 }
7319 }
c906108c 7320 }
98751a41
JK
7321
7322 /* Copy fi.typedef_field_list linked list elements content into the
7323 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7324 if (fi.typedef_field_list)
7325 {
7326 int i = fi.typedef_field_list_count;
7327
a0d7a4ff 7328 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7329 TYPE_TYPEDEF_FIELD_ARRAY (type)
7330 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7331 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7332
7333 /* Reverse the list order to keep the debug info elements order. */
7334 while (--i >= 0)
7335 {
7336 struct typedef_field *dest, *src;
6e70227d 7337
98751a41
JK
7338 dest = &TYPE_TYPEDEF_FIELD (type, i);
7339 src = &fi.typedef_field_list->field;
7340 fi.typedef_field_list = fi.typedef_field_list->next;
7341 *dest = *src;
7342 }
7343 }
c767944b
DJ
7344
7345 do_cleanups (back_to);
c906108c 7346 }
63d06c5c 7347
0b92b5bb
TT
7348 quirk_gcc_member_function_pointer (type, cu->objfile);
7349
90aeadfc
DC
7350 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7351 snapshots) has been known to create a die giving a declaration
7352 for a class that has, as a child, a die giving a definition for a
7353 nested class. So we have to process our children even if the
7354 current die is a declaration. Normally, of course, a declaration
7355 won't have any children at all. */
134d01f1 7356
90aeadfc
DC
7357 while (child_die != NULL && child_die->tag)
7358 {
7359 if (child_die->tag == DW_TAG_member
7360 || child_die->tag == DW_TAG_variable
34eaf542
TT
7361 || child_die->tag == DW_TAG_inheritance
7362 || child_die->tag == DW_TAG_template_value_param
7363 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7364 {
90aeadfc 7365 /* Do nothing. */
134d01f1 7366 }
90aeadfc
DC
7367 else
7368 process_die (child_die, cu);
134d01f1 7369
90aeadfc 7370 child_die = sibling_die (child_die);
134d01f1
DJ
7371 }
7372
fa4028e9
JB
7373 /* Do not consider external references. According to the DWARF standard,
7374 these DIEs are identified by the fact that they have no byte_size
7375 attribute, and a declaration attribute. */
7376 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7377 || !die_is_declaration (die, cu))
c767944b 7378 new_symbol (die, type, cu);
134d01f1
DJ
7379}
7380
7381/* Given a DW_AT_enumeration_type die, set its type. We do not
7382 complete the type's fields yet, or create any symbols. */
c906108c 7383
f792889a 7384static struct type *
134d01f1 7385read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7386{
e7c27a73 7387 struct objfile *objfile = cu->objfile;
c906108c 7388 struct type *type;
c906108c 7389 struct attribute *attr;
0114d602 7390 const char *name;
134d01f1 7391
348e048f
DE
7392 /* If the definition of this type lives in .debug_types, read that type.
7393 Don't follow DW_AT_specification though, that will take us back up
7394 the chain and we want to go down. */
7395 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7396 if (attr)
7397 {
7398 struct dwarf2_cu *type_cu = cu;
7399 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7400
348e048f 7401 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7402
7403 /* TYPE_CU may not be the same as CU.
7404 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7405 return set_die_type (die, type, cu);
7406 }
7407
c906108c
SS
7408 type = alloc_type (objfile);
7409
7410 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7411 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7412 if (name != NULL)
0114d602 7413 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7414
e142c38c 7415 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7416 if (attr)
7417 {
7418 TYPE_LENGTH (type) = DW_UNSND (attr);
7419 }
7420 else
7421 {
7422 TYPE_LENGTH (type) = 0;
7423 }
7424
137033e9
JB
7425 /* The enumeration DIE can be incomplete. In Ada, any type can be
7426 declared as private in the package spec, and then defined only
7427 inside the package body. Such types are known as Taft Amendment
7428 Types. When another package uses such a type, an incomplete DIE
7429 may be generated by the compiler. */
02eb380e 7430 if (die_is_declaration (die, cu))
876cecd0 7431 TYPE_STUB (type) = 1;
02eb380e 7432
f792889a 7433 return set_die_type (die, type, cu);
134d01f1
DJ
7434}
7435
7436/* Given a pointer to a die which begins an enumeration, process all
7437 the dies that define the members of the enumeration, and create the
7438 symbol for the enumeration type.
7439
7440 NOTE: We reverse the order of the element list. */
7441
7442static void
7443process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7444{
f792889a 7445 struct type *this_type;
134d01f1 7446
f792889a
DJ
7447 this_type = get_die_type (die, cu);
7448 if (this_type == NULL)
7449 this_type = read_enumeration_type (die, cu);
9dc481d3 7450
639d11d3 7451 if (die->child != NULL)
c906108c 7452 {
9dc481d3
DE
7453 struct die_info *child_die;
7454 struct symbol *sym;
7455 struct field *fields = NULL;
7456 int num_fields = 0;
7457 int unsigned_enum = 1;
7458 char *name;
7459
639d11d3 7460 child_die = die->child;
c906108c
SS
7461 while (child_die && child_die->tag)
7462 {
7463 if (child_die->tag != DW_TAG_enumerator)
7464 {
e7c27a73 7465 process_die (child_die, cu);
c906108c
SS
7466 }
7467 else
7468 {
39cbfefa
DJ
7469 name = dwarf2_name (child_die, cu);
7470 if (name)
c906108c 7471 {
f792889a 7472 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7473 if (SYMBOL_VALUE (sym) < 0)
7474 unsigned_enum = 0;
7475
7476 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7477 {
7478 fields = (struct field *)
7479 xrealloc (fields,
7480 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7481 * sizeof (struct field));
c906108c
SS
7482 }
7483
3567439c 7484 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7485 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7486 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7487 FIELD_BITSIZE (fields[num_fields]) = 0;
7488
7489 num_fields++;
7490 }
7491 }
7492
7493 child_die = sibling_die (child_die);
7494 }
7495
7496 if (num_fields)
7497 {
f792889a
DJ
7498 TYPE_NFIELDS (this_type) = num_fields;
7499 TYPE_FIELDS (this_type) = (struct field *)
7500 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7501 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7502 sizeof (struct field) * num_fields);
b8c9b27d 7503 xfree (fields);
c906108c
SS
7504 }
7505 if (unsigned_enum)
876cecd0 7506 TYPE_UNSIGNED (this_type) = 1;
c906108c 7507 }
134d01f1 7508
f792889a 7509 new_symbol (die, this_type, cu);
c906108c
SS
7510}
7511
7512/* Extract all information from a DW_TAG_array_type DIE and put it in
7513 the DIE's type field. For now, this only handles one dimensional
7514 arrays. */
7515
f792889a 7516static struct type *
e7c27a73 7517read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7518{
e7c27a73 7519 struct objfile *objfile = cu->objfile;
c906108c 7520 struct die_info *child_die;
7e314c57 7521 struct type *type;
c906108c
SS
7522 struct type *element_type, *range_type, *index_type;
7523 struct type **range_types = NULL;
7524 struct attribute *attr;
7525 int ndim = 0;
7526 struct cleanup *back_to;
39cbfefa 7527 char *name;
c906108c 7528
e7c27a73 7529 element_type = die_type (die, cu);
c906108c 7530
7e314c57
JK
7531 /* The die_type call above may have already set the type for this DIE. */
7532 type = get_die_type (die, cu);
7533 if (type)
7534 return type;
7535
c906108c
SS
7536 /* Irix 6.2 native cc creates array types without children for
7537 arrays with unspecified length. */
639d11d3 7538 if (die->child == NULL)
c906108c 7539 {
46bf5051 7540 index_type = objfile_type (objfile)->builtin_int;
c906108c 7541 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7542 type = create_array_type (NULL, element_type, range_type);
7543 return set_die_type (die, type, cu);
c906108c
SS
7544 }
7545
7546 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7547 child_die = die->child;
c906108c
SS
7548 while (child_die && child_die->tag)
7549 {
7550 if (child_die->tag == DW_TAG_subrange_type)
7551 {
f792889a 7552 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7553
f792889a 7554 if (child_type != NULL)
a02abb62 7555 {
0963b4bd
MS
7556 /* The range type was succesfully read. Save it for the
7557 array type creation. */
a02abb62
JB
7558 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7559 {
7560 range_types = (struct type **)
7561 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7562 * sizeof (struct type *));
7563 if (ndim == 0)
7564 make_cleanup (free_current_contents, &range_types);
7565 }
f792889a 7566 range_types[ndim++] = child_type;
a02abb62 7567 }
c906108c
SS
7568 }
7569 child_die = sibling_die (child_die);
7570 }
7571
7572 /* Dwarf2 dimensions are output from left to right, create the
7573 necessary array types in backwards order. */
7ca2d3a3 7574
c906108c 7575 type = element_type;
7ca2d3a3
DL
7576
7577 if (read_array_order (die, cu) == DW_ORD_col_major)
7578 {
7579 int i = 0;
9a619af0 7580
7ca2d3a3
DL
7581 while (i < ndim)
7582 type = create_array_type (NULL, type, range_types[i++]);
7583 }
7584 else
7585 {
7586 while (ndim-- > 0)
7587 type = create_array_type (NULL, type, range_types[ndim]);
7588 }
c906108c 7589
f5f8a009
EZ
7590 /* Understand Dwarf2 support for vector types (like they occur on
7591 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7592 array type. This is not part of the Dwarf2/3 standard yet, but a
7593 custom vendor extension. The main difference between a regular
7594 array and the vector variant is that vectors are passed by value
7595 to functions. */
e142c38c 7596 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7597 if (attr)
ea37ba09 7598 make_vector_type (type);
f5f8a009 7599
dbc98a8b
KW
7600 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7601 implementation may choose to implement triple vectors using this
7602 attribute. */
7603 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7604 if (attr)
7605 {
7606 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7607 TYPE_LENGTH (type) = DW_UNSND (attr);
7608 else
3e43a32a
MS
7609 complaint (&symfile_complaints,
7610 _("DW_AT_byte_size for array type smaller "
7611 "than the total size of elements"));
dbc98a8b
KW
7612 }
7613
39cbfefa
DJ
7614 name = dwarf2_name (die, cu);
7615 if (name)
7616 TYPE_NAME (type) = name;
6e70227d 7617
0963b4bd 7618 /* Install the type in the die. */
7e314c57
JK
7619 set_die_type (die, type, cu);
7620
7621 /* set_die_type should be already done. */
b4ba55a1
JB
7622 set_descriptive_type (type, die, cu);
7623
c906108c
SS
7624 do_cleanups (back_to);
7625
7e314c57 7626 return type;
c906108c
SS
7627}
7628
7ca2d3a3 7629static enum dwarf_array_dim_ordering
6e70227d 7630read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7631{
7632 struct attribute *attr;
7633
7634 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7635
7636 if (attr) return DW_SND (attr);
7637
0963b4bd
MS
7638 /* GNU F77 is a special case, as at 08/2004 array type info is the
7639 opposite order to the dwarf2 specification, but data is still
7640 laid out as per normal fortran.
7ca2d3a3 7641
0963b4bd
MS
7642 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7643 version checking. */
7ca2d3a3 7644
905e0470
PM
7645 if (cu->language == language_fortran
7646 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7647 {
7648 return DW_ORD_row_major;
7649 }
7650
6e70227d 7651 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7652 {
7653 case array_column_major:
7654 return DW_ORD_col_major;
7655 case array_row_major:
7656 default:
7657 return DW_ORD_row_major;
7658 };
7659}
7660
72019c9c 7661/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 7662 the DIE's type field. */
72019c9c 7663
f792889a 7664static struct type *
72019c9c
GM
7665read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7666{
7e314c57
JK
7667 struct type *domain_type, *set_type;
7668 struct attribute *attr;
f792889a 7669
7e314c57
JK
7670 domain_type = die_type (die, cu);
7671
7672 /* The die_type call above may have already set the type for this DIE. */
7673 set_type = get_die_type (die, cu);
7674 if (set_type)
7675 return set_type;
7676
7677 set_type = create_set_type (NULL, domain_type);
7678
7679 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7680 if (attr)
7681 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7682
f792889a 7683 return set_die_type (die, set_type, cu);
72019c9c 7684}
7ca2d3a3 7685
c906108c
SS
7686/* First cut: install each common block member as a global variable. */
7687
7688static void
e7c27a73 7689read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7690{
7691 struct die_info *child_die;
7692 struct attribute *attr;
7693 struct symbol *sym;
7694 CORE_ADDR base = (CORE_ADDR) 0;
7695
e142c38c 7696 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7697 if (attr)
7698 {
0963b4bd 7699 /* Support the .debug_loc offsets. */
8e19ed76
PS
7700 if (attr_form_is_block (attr))
7701 {
e7c27a73 7702 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7703 }
3690dd37 7704 else if (attr_form_is_section_offset (attr))
8e19ed76 7705 {
4d3c2250 7706 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7707 }
7708 else
7709 {
4d3c2250
KB
7710 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7711 "common block member");
8e19ed76 7712 }
c906108c 7713 }
639d11d3 7714 if (die->child != NULL)
c906108c 7715 {
639d11d3 7716 child_die = die->child;
c906108c
SS
7717 while (child_die && child_die->tag)
7718 {
74ac6d43
TT
7719 LONGEST offset;
7720
e7c27a73 7721 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
7722 if (sym != NULL
7723 && handle_data_member_location (child_die, cu, &offset))
c906108c 7724 {
74ac6d43 7725 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
7726 add_symbol_to_list (sym, &global_symbols);
7727 }
7728 child_die = sibling_die (child_die);
7729 }
7730 }
7731}
7732
0114d602 7733/* Create a type for a C++ namespace. */
d9fa45fe 7734
0114d602
DJ
7735static struct type *
7736read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7737{
e7c27a73 7738 struct objfile *objfile = cu->objfile;
0114d602 7739 const char *previous_prefix, *name;
9219021c 7740 int is_anonymous;
0114d602
DJ
7741 struct type *type;
7742
7743 /* For extensions, reuse the type of the original namespace. */
7744 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7745 {
7746 struct die_info *ext_die;
7747 struct dwarf2_cu *ext_cu = cu;
9a619af0 7748
0114d602
DJ
7749 ext_die = dwarf2_extension (die, &ext_cu);
7750 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7751
7752 /* EXT_CU may not be the same as CU.
7753 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7754 return set_die_type (die, type, cu);
7755 }
9219021c 7756
e142c38c 7757 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7758
7759 /* Now build the name of the current namespace. */
7760
0114d602
DJ
7761 previous_prefix = determine_prefix (die, cu);
7762 if (previous_prefix[0] != '\0')
7763 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7764 previous_prefix, name, 0, cu);
0114d602
DJ
7765
7766 /* Create the type. */
7767 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7768 objfile);
7769 TYPE_NAME (type) = (char *) name;
7770 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7771
60531b24 7772 return set_die_type (die, type, cu);
0114d602
DJ
7773}
7774
7775/* Read a C++ namespace. */
7776
7777static void
7778read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7779{
7780 struct objfile *objfile = cu->objfile;
0114d602 7781 int is_anonymous;
9219021c 7782
5c4e30ca
DC
7783 /* Add a symbol associated to this if we haven't seen the namespace
7784 before. Also, add a using directive if it's an anonymous
7785 namespace. */
9219021c 7786
f2f0e013 7787 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7788 {
7789 struct type *type;
7790
0114d602 7791 type = read_type_die (die, cu);
e7c27a73 7792 new_symbol (die, type, cu);
5c4e30ca 7793
e8e80198 7794 namespace_name (die, &is_anonymous, cu);
5c4e30ca 7795 if (is_anonymous)
0114d602
DJ
7796 {
7797 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7798
c0cc3a76 7799 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7800 NULL, &objfile->objfile_obstack);
0114d602 7801 }
5c4e30ca 7802 }
9219021c 7803
639d11d3 7804 if (die->child != NULL)
d9fa45fe 7805 {
639d11d3 7806 struct die_info *child_die = die->child;
6e70227d 7807
d9fa45fe
DC
7808 while (child_die && child_die->tag)
7809 {
e7c27a73 7810 process_die (child_die, cu);
d9fa45fe
DC
7811 child_die = sibling_die (child_die);
7812 }
7813 }
38d518c9
EZ
7814}
7815
f55ee35c
JK
7816/* Read a Fortran module as type. This DIE can be only a declaration used for
7817 imported module. Still we need that type as local Fortran "use ... only"
7818 declaration imports depend on the created type in determine_prefix. */
7819
7820static struct type *
7821read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7822{
7823 struct objfile *objfile = cu->objfile;
7824 char *module_name;
7825 struct type *type;
7826
7827 module_name = dwarf2_name (die, cu);
7828 if (!module_name)
3e43a32a
MS
7829 complaint (&symfile_complaints,
7830 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
7831 die->offset);
7832 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7833
7834 /* determine_prefix uses TYPE_TAG_NAME. */
7835 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7836
7837 return set_die_type (die, type, cu);
7838}
7839
5d7cb8df
JK
7840/* Read a Fortran module. */
7841
7842static void
7843read_module (struct die_info *die, struct dwarf2_cu *cu)
7844{
7845 struct die_info *child_die = die->child;
7846
5d7cb8df
JK
7847 while (child_die && child_die->tag)
7848 {
7849 process_die (child_die, cu);
7850 child_die = sibling_die (child_die);
7851 }
7852}
7853
38d518c9
EZ
7854/* Return the name of the namespace represented by DIE. Set
7855 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7856 namespace. */
7857
7858static const char *
e142c38c 7859namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7860{
7861 struct die_info *current_die;
7862 const char *name = NULL;
7863
7864 /* Loop through the extensions until we find a name. */
7865
7866 for (current_die = die;
7867 current_die != NULL;
f2f0e013 7868 current_die = dwarf2_extension (die, &cu))
38d518c9 7869 {
e142c38c 7870 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7871 if (name != NULL)
7872 break;
7873 }
7874
7875 /* Is it an anonymous namespace? */
7876
7877 *is_anonymous = (name == NULL);
7878 if (*is_anonymous)
2b1dbab0 7879 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
7880
7881 return name;
d9fa45fe
DC
7882}
7883
c906108c
SS
7884/* Extract all information from a DW_TAG_pointer_type DIE and add to
7885 the user defined type vector. */
7886
f792889a 7887static struct type *
e7c27a73 7888read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7889{
5e2b427d 7890 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7891 struct comp_unit_head *cu_header = &cu->header;
c906108c 7892 struct type *type;
8b2dbe47
KB
7893 struct attribute *attr_byte_size;
7894 struct attribute *attr_address_class;
7895 int byte_size, addr_class;
7e314c57
JK
7896 struct type *target_type;
7897
7898 target_type = die_type (die, cu);
c906108c 7899
7e314c57
JK
7900 /* The die_type call above may have already set the type for this DIE. */
7901 type = get_die_type (die, cu);
7902 if (type)
7903 return type;
7904
7905 type = lookup_pointer_type (target_type);
8b2dbe47 7906
e142c38c 7907 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7908 if (attr_byte_size)
7909 byte_size = DW_UNSND (attr_byte_size);
c906108c 7910 else
8b2dbe47
KB
7911 byte_size = cu_header->addr_size;
7912
e142c38c 7913 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7914 if (attr_address_class)
7915 addr_class = DW_UNSND (attr_address_class);
7916 else
7917 addr_class = DW_ADDR_none;
7918
7919 /* If the pointer size or address class is different than the
7920 default, create a type variant marked as such and set the
7921 length accordingly. */
7922 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7923 {
5e2b427d 7924 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7925 {
7926 int type_flags;
7927
849957d9 7928 type_flags = gdbarch_address_class_type_flags
5e2b427d 7929 (gdbarch, byte_size, addr_class);
876cecd0
TT
7930 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7931 == 0);
8b2dbe47
KB
7932 type = make_type_with_address_space (type, type_flags);
7933 }
7934 else if (TYPE_LENGTH (type) != byte_size)
7935 {
3e43a32a
MS
7936 complaint (&symfile_complaints,
7937 _("invalid pointer size %d"), byte_size);
8b2dbe47 7938 }
6e70227d 7939 else
9a619af0
MS
7940 {
7941 /* Should we also complain about unhandled address classes? */
7942 }
c906108c 7943 }
8b2dbe47
KB
7944
7945 TYPE_LENGTH (type) = byte_size;
f792889a 7946 return set_die_type (die, type, cu);
c906108c
SS
7947}
7948
7949/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7950 the user defined type vector. */
7951
f792889a 7952static struct type *
e7c27a73 7953read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7954{
7955 struct type *type;
7956 struct type *to_type;
7957 struct type *domain;
7958
e7c27a73
DJ
7959 to_type = die_type (die, cu);
7960 domain = die_containing_type (die, cu);
0d5de010 7961
7e314c57
JK
7962 /* The calls above may have already set the type for this DIE. */
7963 type = get_die_type (die, cu);
7964 if (type)
7965 return type;
7966
0d5de010
DJ
7967 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7968 type = lookup_methodptr_type (to_type);
7969 else
7970 type = lookup_memberptr_type (to_type, domain);
c906108c 7971
f792889a 7972 return set_die_type (die, type, cu);
c906108c
SS
7973}
7974
7975/* Extract all information from a DW_TAG_reference_type DIE and add to
7976 the user defined type vector. */
7977
f792889a 7978static struct type *
e7c27a73 7979read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7980{
e7c27a73 7981 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7982 struct type *type, *target_type;
c906108c
SS
7983 struct attribute *attr;
7984
7e314c57
JK
7985 target_type = die_type (die, cu);
7986
7987 /* The die_type call above may have already set the type for this DIE. */
7988 type = get_die_type (die, cu);
7989 if (type)
7990 return type;
7991
7992 type = lookup_reference_type (target_type);
e142c38c 7993 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7994 if (attr)
7995 {
7996 TYPE_LENGTH (type) = DW_UNSND (attr);
7997 }
7998 else
7999 {
107d2387 8000 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8001 }
f792889a 8002 return set_die_type (die, type, cu);
c906108c
SS
8003}
8004
f792889a 8005static struct type *
e7c27a73 8006read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8007{
f792889a 8008 struct type *base_type, *cv_type;
c906108c 8009
e7c27a73 8010 base_type = die_type (die, cu);
7e314c57
JK
8011
8012 /* The die_type call above may have already set the type for this DIE. */
8013 cv_type = get_die_type (die, cu);
8014 if (cv_type)
8015 return cv_type;
8016
2f608a3a
KW
8017 /* In case the const qualifier is applied to an array type, the element type
8018 is so qualified, not the array type (section 6.7.3 of C99). */
8019 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8020 {
8021 struct type *el_type, *inner_array;
8022
8023 base_type = copy_type (base_type);
8024 inner_array = base_type;
8025
8026 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8027 {
8028 TYPE_TARGET_TYPE (inner_array) =
8029 copy_type (TYPE_TARGET_TYPE (inner_array));
8030 inner_array = TYPE_TARGET_TYPE (inner_array);
8031 }
8032
8033 el_type = TYPE_TARGET_TYPE (inner_array);
8034 TYPE_TARGET_TYPE (inner_array) =
8035 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8036
8037 return set_die_type (die, base_type, cu);
8038 }
8039
f792889a
DJ
8040 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8041 return set_die_type (die, cv_type, cu);
c906108c
SS
8042}
8043
f792889a 8044static struct type *
e7c27a73 8045read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8046{
f792889a 8047 struct type *base_type, *cv_type;
c906108c 8048
e7c27a73 8049 base_type = die_type (die, cu);
7e314c57
JK
8050
8051 /* The die_type call above may have already set the type for this DIE. */
8052 cv_type = get_die_type (die, cu);
8053 if (cv_type)
8054 return cv_type;
8055
f792889a
DJ
8056 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8057 return set_die_type (die, cv_type, cu);
c906108c
SS
8058}
8059
8060/* Extract all information from a DW_TAG_string_type DIE and add to
8061 the user defined type vector. It isn't really a user defined type,
8062 but it behaves like one, with other DIE's using an AT_user_def_type
8063 attribute to reference it. */
8064
f792889a 8065static struct type *
e7c27a73 8066read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8067{
e7c27a73 8068 struct objfile *objfile = cu->objfile;
3b7538c0 8069 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8070 struct type *type, *range_type, *index_type, *char_type;
8071 struct attribute *attr;
8072 unsigned int length;
8073
e142c38c 8074 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8075 if (attr)
8076 {
8077 length = DW_UNSND (attr);
8078 }
8079 else
8080 {
0963b4bd 8081 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8082 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8083 if (attr)
8084 {
8085 length = DW_UNSND (attr);
8086 }
8087 else
8088 {
8089 length = 1;
8090 }
c906108c 8091 }
6ccb9162 8092
46bf5051 8093 index_type = objfile_type (objfile)->builtin_int;
c906108c 8094 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8095 char_type = language_string_char_type (cu->language_defn, gdbarch);
8096 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8097
f792889a 8098 return set_die_type (die, type, cu);
c906108c
SS
8099}
8100
8101/* Handle DIES due to C code like:
8102
8103 struct foo
c5aa993b
JM
8104 {
8105 int (*funcp)(int a, long l);
8106 int b;
8107 };
c906108c 8108
0963b4bd 8109 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8110
f792889a 8111static struct type *
e7c27a73 8112read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8113{
0963b4bd
MS
8114 struct type *type; /* Type that this function returns. */
8115 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8116 struct attribute *attr;
8117
e7c27a73 8118 type = die_type (die, cu);
7e314c57
JK
8119
8120 /* The die_type call above may have already set the type for this DIE. */
8121 ftype = get_die_type (die, cu);
8122 if (ftype)
8123 return ftype;
8124
0c8b41f1 8125 ftype = lookup_function_type (type);
c906108c 8126
5b8101ae 8127 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8128 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8129 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8130 || cu->language == language_cplus
5b8101ae
PM
8131 || cu->language == language_java
8132 || cu->language == language_pascal)
876cecd0 8133 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8134 else if (producer_is_realview (cu->producer))
8135 /* RealView does not emit DW_AT_prototyped. We can not
8136 distinguish prototyped and unprototyped functions; default to
8137 prototyped, since that is more common in modern code (and
8138 RealView warns about unprototyped functions). */
8139 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8140
c055b101
CV
8141 /* Store the calling convention in the type if it's available in
8142 the subroutine die. Otherwise set the calling convention to
8143 the default value DW_CC_normal. */
8144 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8145 if (attr)
8146 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8147 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8148 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8149 else
8150 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8151
8152 /* We need to add the subroutine type to the die immediately so
8153 we don't infinitely recurse when dealing with parameters
0963b4bd 8154 declared as the same subroutine type. */
76c10ea2 8155 set_die_type (die, ftype, cu);
6e70227d 8156
639d11d3 8157 if (die->child != NULL)
c906108c 8158 {
8072405b 8159 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 8160 struct die_info *child_die;
8072405b 8161 int nparams, iparams;
c906108c
SS
8162
8163 /* Count the number of parameters.
8164 FIXME: GDB currently ignores vararg functions, but knows about
8165 vararg member functions. */
8072405b 8166 nparams = 0;
639d11d3 8167 child_die = die->child;
c906108c
SS
8168 while (child_die && child_die->tag)
8169 {
8170 if (child_die->tag == DW_TAG_formal_parameter)
8171 nparams++;
8172 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8173 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8174 child_die = sibling_die (child_die);
8175 }
8176
8177 /* Allocate storage for parameters and fill them in. */
8178 TYPE_NFIELDS (ftype) = nparams;
8179 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8180 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8181
8072405b
JK
8182 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8183 even if we error out during the parameters reading below. */
8184 for (iparams = 0; iparams < nparams; iparams++)
8185 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8186
8187 iparams = 0;
639d11d3 8188 child_die = die->child;
c906108c
SS
8189 while (child_die && child_die->tag)
8190 {
8191 if (child_die->tag == DW_TAG_formal_parameter)
8192 {
3ce3b1ba
PA
8193 struct type *arg_type;
8194
8195 /* DWARF version 2 has no clean way to discern C++
8196 static and non-static member functions. G++ helps
8197 GDB by marking the first parameter for non-static
8198 member functions (which is the this pointer) as
8199 artificial. We pass this information to
8200 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8201
8202 DWARF version 3 added DW_AT_object_pointer, which GCC
8203 4.5 does not yet generate. */
e142c38c 8204 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8205 if (attr)
8206 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8207 else
418835cc
KS
8208 {
8209 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8210
8211 /* GCC/43521: In java, the formal parameter
8212 "this" is sometimes not marked with DW_AT_artificial. */
8213 if (cu->language == language_java)
8214 {
8215 const char *name = dwarf2_name (child_die, cu);
9a619af0 8216
418835cc
KS
8217 if (name && !strcmp (name, "this"))
8218 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8219 }
8220 }
3ce3b1ba
PA
8221 arg_type = die_type (child_die, cu);
8222
8223 /* RealView does not mark THIS as const, which the testsuite
8224 expects. GCC marks THIS as const in method definitions,
8225 but not in the class specifications (GCC PR 43053). */
8226 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8227 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8228 {
8229 int is_this = 0;
8230 struct dwarf2_cu *arg_cu = cu;
8231 const char *name = dwarf2_name (child_die, cu);
8232
8233 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8234 if (attr)
8235 {
8236 /* If the compiler emits this, use it. */
8237 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8238 is_this = 1;
8239 }
8240 else if (name && strcmp (name, "this") == 0)
8241 /* Function definitions will have the argument names. */
8242 is_this = 1;
8243 else if (name == NULL && iparams == 0)
8244 /* Declarations may not have the names, so like
8245 elsewhere in GDB, assume an artificial first
8246 argument is "this". */
8247 is_this = 1;
8248
8249 if (is_this)
8250 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8251 arg_type, 0);
8252 }
8253
8254 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8255 iparams++;
8256 }
8257 child_die = sibling_die (child_die);
8258 }
8259 }
8260
76c10ea2 8261 return ftype;
c906108c
SS
8262}
8263
f792889a 8264static struct type *
e7c27a73 8265read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8266{
e7c27a73 8267 struct objfile *objfile = cu->objfile;
0114d602 8268 const char *name = NULL;
f792889a 8269 struct type *this_type;
c906108c 8270
94af9270 8271 name = dwarf2_full_name (NULL, die, cu);
f792889a 8272 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8273 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8274 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8275 set_die_type (die, this_type, cu);
8276 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8277 return this_type;
c906108c
SS
8278}
8279
8280/* Find a representation of a given base type and install
8281 it in the TYPE field of the die. */
8282
f792889a 8283static struct type *
e7c27a73 8284read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8285{
e7c27a73 8286 struct objfile *objfile = cu->objfile;
c906108c
SS
8287 struct type *type;
8288 struct attribute *attr;
8289 int encoding = 0, size = 0;
39cbfefa 8290 char *name;
6ccb9162
UW
8291 enum type_code code = TYPE_CODE_INT;
8292 int type_flags = 0;
8293 struct type *target_type = NULL;
c906108c 8294
e142c38c 8295 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8296 if (attr)
8297 {
8298 encoding = DW_UNSND (attr);
8299 }
e142c38c 8300 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8301 if (attr)
8302 {
8303 size = DW_UNSND (attr);
8304 }
39cbfefa 8305 name = dwarf2_name (die, cu);
6ccb9162 8306 if (!name)
c906108c 8307 {
6ccb9162
UW
8308 complaint (&symfile_complaints,
8309 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8310 }
6ccb9162
UW
8311
8312 switch (encoding)
c906108c 8313 {
6ccb9162
UW
8314 case DW_ATE_address:
8315 /* Turn DW_ATE_address into a void * pointer. */
8316 code = TYPE_CODE_PTR;
8317 type_flags |= TYPE_FLAG_UNSIGNED;
8318 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8319 break;
8320 case DW_ATE_boolean:
8321 code = TYPE_CODE_BOOL;
8322 type_flags |= TYPE_FLAG_UNSIGNED;
8323 break;
8324 case DW_ATE_complex_float:
8325 code = TYPE_CODE_COMPLEX;
8326 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8327 break;
8328 case DW_ATE_decimal_float:
8329 code = TYPE_CODE_DECFLOAT;
8330 break;
8331 case DW_ATE_float:
8332 code = TYPE_CODE_FLT;
8333 break;
8334 case DW_ATE_signed:
8335 break;
8336 case DW_ATE_unsigned:
8337 type_flags |= TYPE_FLAG_UNSIGNED;
8338 break;
8339 case DW_ATE_signed_char:
6e70227d 8340 if (cu->language == language_ada || cu->language == language_m2
868a0084 8341 || cu->language == language_pascal)
6ccb9162
UW
8342 code = TYPE_CODE_CHAR;
8343 break;
8344 case DW_ATE_unsigned_char:
868a0084
PM
8345 if (cu->language == language_ada || cu->language == language_m2
8346 || cu->language == language_pascal)
6ccb9162
UW
8347 code = TYPE_CODE_CHAR;
8348 type_flags |= TYPE_FLAG_UNSIGNED;
8349 break;
75079b2b
TT
8350 case DW_ATE_UTF:
8351 /* We just treat this as an integer and then recognize the
8352 type by name elsewhere. */
8353 break;
8354
6ccb9162
UW
8355 default:
8356 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8357 dwarf_type_encoding_name (encoding));
8358 break;
c906108c 8359 }
6ccb9162 8360
0114d602
DJ
8361 type = init_type (code, size, type_flags, NULL, objfile);
8362 TYPE_NAME (type) = name;
6ccb9162
UW
8363 TYPE_TARGET_TYPE (type) = target_type;
8364
0114d602 8365 if (name && strcmp (name, "char") == 0)
876cecd0 8366 TYPE_NOSIGN (type) = 1;
0114d602 8367
f792889a 8368 return set_die_type (die, type, cu);
c906108c
SS
8369}
8370
a02abb62
JB
8371/* Read the given DW_AT_subrange DIE. */
8372
f792889a 8373static struct type *
a02abb62
JB
8374read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8375{
8376 struct type *base_type;
8377 struct type *range_type;
8378 struct attribute *attr;
43bbcdc2
PH
8379 LONGEST low = 0;
8380 LONGEST high = -1;
39cbfefa 8381 char *name;
43bbcdc2 8382 LONGEST negative_mask;
e77813c8 8383
a02abb62 8384 base_type = die_type (die, cu);
953ac07e
JK
8385 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8386 check_typedef (base_type);
a02abb62 8387
7e314c57
JK
8388 /* The die_type call above may have already set the type for this DIE. */
8389 range_type = get_die_type (die, cu);
8390 if (range_type)
8391 return range_type;
8392
e142c38c 8393 if (cu->language == language_fortran)
6e70227d 8394 {
a02abb62
JB
8395 /* FORTRAN implies a lower bound of 1, if not given. */
8396 low = 1;
8397 }
8398
dd5e6932
DJ
8399 /* FIXME: For variable sized arrays either of these could be
8400 a variable rather than a constant value. We'll allow it,
8401 but we don't know how to handle it. */
e142c38c 8402 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8403 if (attr)
8404 low = dwarf2_get_attr_constant_value (attr, 0);
8405
e142c38c 8406 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8407 if (attr)
6e70227d 8408 {
e77813c8 8409 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8410 {
8411 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8412 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8413 FIXME: GDB does not yet know how to handle dynamic
8414 arrays properly, treat them as arrays with unspecified
8415 length for now.
8416
8417 FIXME: jimb/2003-09-22: GDB does not really know
8418 how to handle arrays of unspecified length
8419 either; we just represent them as zero-length
8420 arrays. Choose an appropriate upper bound given
8421 the lower bound we've computed above. */
8422 high = low - 1;
8423 }
8424 else
8425 high = dwarf2_get_attr_constant_value (attr, 1);
8426 }
e77813c8
PM
8427 else
8428 {
8429 attr = dwarf2_attr (die, DW_AT_count, cu);
8430 if (attr)
8431 {
8432 int count = dwarf2_get_attr_constant_value (attr, 1);
8433 high = low + count - 1;
8434 }
c2ff108b
JK
8435 else
8436 {
8437 /* Unspecified array length. */
8438 high = low - 1;
8439 }
e77813c8
PM
8440 }
8441
8442 /* Dwarf-2 specifications explicitly allows to create subrange types
8443 without specifying a base type.
8444 In that case, the base type must be set to the type of
8445 the lower bound, upper bound or count, in that order, if any of these
8446 three attributes references an object that has a type.
8447 If no base type is found, the Dwarf-2 specifications say that
8448 a signed integer type of size equal to the size of an address should
8449 be used.
8450 For the following C code: `extern char gdb_int [];'
8451 GCC produces an empty range DIE.
8452 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 8453 high bound or count are not yet handled by this code. */
e77813c8
PM
8454 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8455 {
8456 struct objfile *objfile = cu->objfile;
8457 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8458 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8459 struct type *int_type = objfile_type (objfile)->builtin_int;
8460
8461 /* Test "int", "long int", and "long long int" objfile types,
8462 and select the first one having a size above or equal to the
8463 architecture address size. */
8464 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8465 base_type = int_type;
8466 else
8467 {
8468 int_type = objfile_type (objfile)->builtin_long;
8469 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8470 base_type = int_type;
8471 else
8472 {
8473 int_type = objfile_type (objfile)->builtin_long_long;
8474 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8475 base_type = int_type;
8476 }
8477 }
8478 }
a02abb62 8479
6e70227d 8480 negative_mask =
43bbcdc2
PH
8481 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8482 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8483 low |= negative_mask;
8484 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8485 high |= negative_mask;
8486
a02abb62
JB
8487 range_type = create_range_type (NULL, base_type, low, high);
8488
bbb0eef6
JK
8489 /* Mark arrays with dynamic length at least as an array of unspecified
8490 length. GDB could check the boundary but before it gets implemented at
8491 least allow accessing the array elements. */
8492 if (attr && attr->form == DW_FORM_block1)
8493 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8494
c2ff108b
JK
8495 /* Ada expects an empty array on no boundary attributes. */
8496 if (attr == NULL && cu->language != language_ada)
8497 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8498
39cbfefa
DJ
8499 name = dwarf2_name (die, cu);
8500 if (name)
8501 TYPE_NAME (range_type) = name;
6e70227d 8502
e142c38c 8503 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8504 if (attr)
8505 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8506
7e314c57
JK
8507 set_die_type (die, range_type, cu);
8508
8509 /* set_die_type should be already done. */
b4ba55a1
JB
8510 set_descriptive_type (range_type, die, cu);
8511
7e314c57 8512 return range_type;
a02abb62 8513}
6e70227d 8514
f792889a 8515static struct type *
81a17f79
JB
8516read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8517{
8518 struct type *type;
81a17f79 8519
81a17f79
JB
8520 /* For now, we only support the C meaning of an unspecified type: void. */
8521
0114d602
DJ
8522 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8523 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8524
f792889a 8525 return set_die_type (die, type, cu);
81a17f79 8526}
a02abb62 8527
51545339
DJ
8528/* Trivial hash function for die_info: the hash value of a DIE
8529 is its offset in .debug_info for this objfile. */
8530
8531static hashval_t
8532die_hash (const void *item)
8533{
8534 const struct die_info *die = item;
9a619af0 8535
51545339
DJ
8536 return die->offset;
8537}
8538
8539/* Trivial comparison function for die_info structures: two DIEs
8540 are equal if they have the same offset. */
8541
8542static int
8543die_eq (const void *item_lhs, const void *item_rhs)
8544{
8545 const struct die_info *die_lhs = item_lhs;
8546 const struct die_info *die_rhs = item_rhs;
9a619af0 8547
51545339
DJ
8548 return die_lhs->offset == die_rhs->offset;
8549}
8550
c906108c
SS
8551/* Read a whole compilation unit into a linked list of dies. */
8552
f9aca02d 8553static struct die_info *
93311388 8554read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8555{
93311388 8556 struct die_reader_specs reader_specs;
98bfdba5 8557 int read_abbrevs = 0;
1d9ec526 8558 struct cleanup *back_to = NULL;
98bfdba5
PA
8559 struct die_info *die;
8560
8561 if (cu->dwarf2_abbrevs == NULL)
8562 {
8563 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8564 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8565 read_abbrevs = 1;
8566 }
93311388 8567
348e048f 8568 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8569 cu->die_hash
8570 = htab_create_alloc_ex (cu->header.length / 12,
8571 die_hash,
8572 die_eq,
8573 NULL,
8574 &cu->comp_unit_obstack,
8575 hashtab_obstack_allocate,
8576 dummy_obstack_deallocate);
8577
93311388
DE
8578 init_cu_die_reader (&reader_specs, cu);
8579
98bfdba5
PA
8580 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8581
8582 if (read_abbrevs)
8583 do_cleanups (back_to);
8584
8585 return die;
639d11d3
DC
8586}
8587
d97bc12b
DE
8588/* Main entry point for reading a DIE and all children.
8589 Read the DIE and dump it if requested. */
8590
8591static struct die_info *
93311388
DE
8592read_die_and_children (const struct die_reader_specs *reader,
8593 gdb_byte *info_ptr,
d97bc12b
DE
8594 gdb_byte **new_info_ptr,
8595 struct die_info *parent)
8596{
93311388 8597 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8598 new_info_ptr, parent);
8599
8600 if (dwarf2_die_debug)
8601 {
348e048f
DE
8602 fprintf_unfiltered (gdb_stdlog,
8603 "\nRead die from %s of %s:\n",
8604 reader->buffer == dwarf2_per_objfile->info.buffer
8605 ? ".debug_info"
8606 : reader->buffer == dwarf2_per_objfile->types.buffer
8607 ? ".debug_types"
8608 : "unknown section",
8609 reader->abfd->filename);
d97bc12b
DE
8610 dump_die (result, dwarf2_die_debug);
8611 }
8612
8613 return result;
8614}
8615
639d11d3
DC
8616/* Read a single die and all its descendents. Set the die's sibling
8617 field to NULL; set other fields in the die correctly, and set all
8618 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8619 location of the info_ptr after reading all of those dies. PARENT
8620 is the parent of the die in question. */
8621
8622static struct die_info *
93311388
DE
8623read_die_and_children_1 (const struct die_reader_specs *reader,
8624 gdb_byte *info_ptr,
d97bc12b
DE
8625 gdb_byte **new_info_ptr,
8626 struct die_info *parent)
639d11d3
DC
8627{
8628 struct die_info *die;
fe1b8b76 8629 gdb_byte *cur_ptr;
639d11d3
DC
8630 int has_children;
8631
93311388 8632 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8633 if (die == NULL)
8634 {
8635 *new_info_ptr = cur_ptr;
8636 return NULL;
8637 }
93311388 8638 store_in_ref_table (die, reader->cu);
639d11d3
DC
8639
8640 if (has_children)
348e048f 8641 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8642 else
8643 {
8644 die->child = NULL;
8645 *new_info_ptr = cur_ptr;
8646 }
8647
8648 die->sibling = NULL;
8649 die->parent = parent;
8650 return die;
8651}
8652
8653/* Read a die, all of its descendents, and all of its siblings; set
8654 all of the fields of all of the dies correctly. Arguments are as
8655 in read_die_and_children. */
8656
8657static struct die_info *
93311388
DE
8658read_die_and_siblings (const struct die_reader_specs *reader,
8659 gdb_byte *info_ptr,
fe1b8b76 8660 gdb_byte **new_info_ptr,
639d11d3
DC
8661 struct die_info *parent)
8662{
8663 struct die_info *first_die, *last_sibling;
fe1b8b76 8664 gdb_byte *cur_ptr;
639d11d3 8665
c906108c 8666 cur_ptr = info_ptr;
639d11d3
DC
8667 first_die = last_sibling = NULL;
8668
8669 while (1)
c906108c 8670 {
639d11d3 8671 struct die_info *die
93311388 8672 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8673
1d325ec1 8674 if (die == NULL)
c906108c 8675 {
639d11d3
DC
8676 *new_info_ptr = cur_ptr;
8677 return first_die;
c906108c 8678 }
1d325ec1
DJ
8679
8680 if (!first_die)
8681 first_die = die;
c906108c 8682 else
1d325ec1
DJ
8683 last_sibling->sibling = die;
8684
8685 last_sibling = die;
c906108c 8686 }
c906108c
SS
8687}
8688
93311388
DE
8689/* Read the die from the .debug_info section buffer. Set DIEP to
8690 point to a newly allocated die with its information, except for its
8691 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8692 whether the die has children or not. */
8693
8694static gdb_byte *
8695read_full_die (const struct die_reader_specs *reader,
8696 struct die_info **diep, gdb_byte *info_ptr,
8697 int *has_children)
8698{
8699 unsigned int abbrev_number, bytes_read, i, offset;
8700 struct abbrev_info *abbrev;
8701 struct die_info *die;
8702 struct dwarf2_cu *cu = reader->cu;
8703 bfd *abfd = reader->abfd;
8704
8705 offset = info_ptr - reader->buffer;
8706 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8707 info_ptr += bytes_read;
8708 if (!abbrev_number)
8709 {
8710 *diep = NULL;
8711 *has_children = 0;
8712 return info_ptr;
8713 }
8714
8715 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8716 if (!abbrev)
348e048f
DE
8717 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8718 abbrev_number,
8719 bfd_get_filename (abfd));
8720
93311388
DE
8721 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8722 die->offset = offset;
8723 die->tag = abbrev->tag;
8724 die->abbrev = abbrev_number;
8725
8726 die->num_attrs = abbrev->num_attrs;
8727
8728 for (i = 0; i < abbrev->num_attrs; ++i)
8729 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8730 abfd, info_ptr, cu);
8731
8732 *diep = die;
8733 *has_children = abbrev->has_children;
8734 return info_ptr;
8735}
8736
c906108c
SS
8737/* In DWARF version 2, the description of the debugging information is
8738 stored in a separate .debug_abbrev section. Before we read any
8739 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8740 in a hash table. This function also sets flags in CU describing
8741 the data found in the abbrev table. */
c906108c
SS
8742
8743static void
e7c27a73 8744dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8745{
e7c27a73 8746 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8747 gdb_byte *abbrev_ptr;
c906108c
SS
8748 struct abbrev_info *cur_abbrev;
8749 unsigned int abbrev_number, bytes_read, abbrev_name;
8750 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8751 struct attr_abbrev *cur_attrs;
8752 unsigned int allocated_attrs;
c906108c 8753
0963b4bd 8754 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
8755 obstack_init (&cu->abbrev_obstack);
8756 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8757 (ABBREV_HASH_SIZE
8758 * sizeof (struct abbrev_info *)));
8759 memset (cu->dwarf2_abbrevs, 0,
8760 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8761
be391dca
TT
8762 dwarf2_read_section (dwarf2_per_objfile->objfile,
8763 &dwarf2_per_objfile->abbrev);
dce234bc 8764 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8765 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8766 abbrev_ptr += bytes_read;
8767
f3dd6933
DJ
8768 allocated_attrs = ATTR_ALLOC_CHUNK;
8769 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8770
0963b4bd 8771 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
8772 while (abbrev_number)
8773 {
f3dd6933 8774 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8775
8776 /* read in abbrev header */
8777 cur_abbrev->number = abbrev_number;
8778 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8779 abbrev_ptr += bytes_read;
8780 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8781 abbrev_ptr += 1;
8782
72bf9492
DJ
8783 if (cur_abbrev->tag == DW_TAG_namespace)
8784 cu->has_namespace_info = 1;
8785
c906108c
SS
8786 /* now read in declarations */
8787 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8788 abbrev_ptr += bytes_read;
8789 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8790 abbrev_ptr += bytes_read;
8791 while (abbrev_name)
8792 {
f3dd6933 8793 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8794 {
f3dd6933
DJ
8795 allocated_attrs += ATTR_ALLOC_CHUNK;
8796 cur_attrs
8797 = xrealloc (cur_attrs, (allocated_attrs
8798 * sizeof (struct attr_abbrev)));
c906108c 8799 }
ae038cb0
DJ
8800
8801 /* Record whether this compilation unit might have
8802 inter-compilation-unit references. If we don't know what form
8803 this attribute will have, then it might potentially be a
8804 DW_FORM_ref_addr, so we conservatively expect inter-CU
8805 references. */
8806
8807 if (abbrev_form == DW_FORM_ref_addr
8808 || abbrev_form == DW_FORM_indirect)
8809 cu->has_form_ref_addr = 1;
8810
f3dd6933
DJ
8811 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8812 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8813 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8814 abbrev_ptr += bytes_read;
8815 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8816 abbrev_ptr += bytes_read;
8817 }
8818
f3dd6933
DJ
8819 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8820 (cur_abbrev->num_attrs
8821 * sizeof (struct attr_abbrev)));
8822 memcpy (cur_abbrev->attrs, cur_attrs,
8823 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8824
c906108c 8825 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8826 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8827 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8828
8829 /* Get next abbreviation.
8830 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8831 always properly terminated with an abbrev number of 0.
8832 Exit loop if we encounter an abbreviation which we have
8833 already read (which means we are about to read the abbreviations
8834 for the next compile unit) or if the end of the abbreviation
8835 table is reached. */
dce234bc
PP
8836 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8837 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8838 break;
8839 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8840 abbrev_ptr += bytes_read;
e7c27a73 8841 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8842 break;
8843 }
f3dd6933
DJ
8844
8845 xfree (cur_attrs);
c906108c
SS
8846}
8847
f3dd6933 8848/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8849
c906108c 8850static void
f3dd6933 8851dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8852{
f3dd6933 8853 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8854
f3dd6933
DJ
8855 obstack_free (&cu->abbrev_obstack, NULL);
8856 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8857}
8858
8859/* Lookup an abbrev_info structure in the abbrev hash table. */
8860
8861static struct abbrev_info *
e7c27a73 8862dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8863{
8864 unsigned int hash_number;
8865 struct abbrev_info *abbrev;
8866
8867 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8868 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8869
8870 while (abbrev)
8871 {
8872 if (abbrev->number == number)
8873 return abbrev;
8874 else
8875 abbrev = abbrev->next;
8876 }
8877 return NULL;
8878}
8879
72bf9492
DJ
8880/* Returns nonzero if TAG represents a type that we might generate a partial
8881 symbol for. */
8882
8883static int
8884is_type_tag_for_partial (int tag)
8885{
8886 switch (tag)
8887 {
8888#if 0
8889 /* Some types that would be reasonable to generate partial symbols for,
8890 that we don't at present. */
8891 case DW_TAG_array_type:
8892 case DW_TAG_file_type:
8893 case DW_TAG_ptr_to_member_type:
8894 case DW_TAG_set_type:
8895 case DW_TAG_string_type:
8896 case DW_TAG_subroutine_type:
8897#endif
8898 case DW_TAG_base_type:
8899 case DW_TAG_class_type:
680b30c7 8900 case DW_TAG_interface_type:
72bf9492
DJ
8901 case DW_TAG_enumeration_type:
8902 case DW_TAG_structure_type:
8903 case DW_TAG_subrange_type:
8904 case DW_TAG_typedef:
8905 case DW_TAG_union_type:
8906 return 1;
8907 default:
8908 return 0;
8909 }
8910}
8911
8912/* Load all DIEs that are interesting for partial symbols into memory. */
8913
8914static struct partial_die_info *
93311388
DE
8915load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8916 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8917{
8918 struct partial_die_info *part_die;
8919 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8920 struct abbrev_info *abbrev;
8921 unsigned int bytes_read;
5afb4e99 8922 unsigned int load_all = 0;
72bf9492
DJ
8923
8924 int nesting_level = 1;
8925
8926 parent_die = NULL;
8927 last_die = NULL;
8928
5afb4e99
DJ
8929 if (cu->per_cu && cu->per_cu->load_all_dies)
8930 load_all = 1;
8931
72bf9492
DJ
8932 cu->partial_dies
8933 = htab_create_alloc_ex (cu->header.length / 12,
8934 partial_die_hash,
8935 partial_die_eq,
8936 NULL,
8937 &cu->comp_unit_obstack,
8938 hashtab_obstack_allocate,
8939 dummy_obstack_deallocate);
8940
8941 part_die = obstack_alloc (&cu->comp_unit_obstack,
8942 sizeof (struct partial_die_info));
8943
8944 while (1)
8945 {
8946 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8947
8948 /* A NULL abbrev means the end of a series of children. */
8949 if (abbrev == NULL)
8950 {
8951 if (--nesting_level == 0)
8952 {
8953 /* PART_DIE was probably the last thing allocated on the
8954 comp_unit_obstack, so we could call obstack_free
8955 here. We don't do that because the waste is small,
8956 and will be cleaned up when we're done with this
8957 compilation unit. This way, we're also more robust
8958 against other users of the comp_unit_obstack. */
8959 return first_die;
8960 }
8961 info_ptr += bytes_read;
8962 last_die = parent_die;
8963 parent_die = parent_die->die_parent;
8964 continue;
8965 }
8966
98bfdba5
PA
8967 /* Check for template arguments. We never save these; if
8968 they're seen, we just mark the parent, and go on our way. */
8969 if (parent_die != NULL
8970 && cu->language == language_cplus
8971 && (abbrev->tag == DW_TAG_template_type_param
8972 || abbrev->tag == DW_TAG_template_value_param))
8973 {
8974 parent_die->has_template_arguments = 1;
8975
8976 if (!load_all)
8977 {
8978 /* We don't need a partial DIE for the template argument. */
8979 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8980 cu);
8981 continue;
8982 }
8983 }
8984
8985 /* We only recurse into subprograms looking for template arguments.
8986 Skip their other children. */
8987 if (!load_all
8988 && cu->language == language_cplus
8989 && parent_die != NULL
8990 && parent_die->tag == DW_TAG_subprogram)
8991 {
8992 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8993 continue;
8994 }
8995
5afb4e99
DJ
8996 /* Check whether this DIE is interesting enough to save. Normally
8997 we would not be interested in members here, but there may be
8998 later variables referencing them via DW_AT_specification (for
8999 static members). */
9000 if (!load_all
9001 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9002 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9003 && abbrev->tag != DW_TAG_enumerator
9004 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9005 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9006 && abbrev->tag != DW_TAG_variable
5afb4e99 9007 && abbrev->tag != DW_TAG_namespace
f55ee35c 9008 && abbrev->tag != DW_TAG_module
5afb4e99 9009 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9010 {
9011 /* Otherwise we skip to the next sibling, if any. */
93311388 9012 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9013 continue;
9014 }
9015
93311388
DE
9016 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9017 buffer, info_ptr, cu);
72bf9492
DJ
9018
9019 /* This two-pass algorithm for processing partial symbols has a
9020 high cost in cache pressure. Thus, handle some simple cases
9021 here which cover the majority of C partial symbols. DIEs
9022 which neither have specification tags in them, nor could have
9023 specification tags elsewhere pointing at them, can simply be
9024 processed and discarded.
9025
9026 This segment is also optional; scan_partial_symbols and
9027 add_partial_symbol will handle these DIEs if we chain
9028 them in normally. When compilers which do not emit large
9029 quantities of duplicate debug information are more common,
9030 this code can probably be removed. */
9031
9032 /* Any complete simple types at the top level (pretty much all
9033 of them, for a language without namespaces), can be processed
9034 directly. */
9035 if (parent_die == NULL
9036 && part_die->has_specification == 0
9037 && part_die->is_declaration == 0
d8228535 9038 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9039 || part_die->tag == DW_TAG_base_type
9040 || part_die->tag == DW_TAG_subrange_type))
9041 {
9042 if (building_psymtab && part_die->name != NULL)
04a679b8 9043 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
9044 VAR_DOMAIN, LOC_TYPEDEF,
9045 &cu->objfile->static_psymbols,
9046 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 9047 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9048 continue;
9049 }
9050
d8228535
JK
9051 /* The exception for DW_TAG_typedef with has_children above is
9052 a workaround of GCC PR debug/47510. In the case of this complaint
9053 type_name_no_tag_or_error will error on such types later.
9054
9055 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9056 it could not find the child DIEs referenced later, this is checked
9057 above. In correct DWARF DW_TAG_typedef should have no children. */
9058
9059 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9060 complaint (&symfile_complaints,
9061 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9062 "- DIE at 0x%x [in module %s]"),
9063 part_die->offset, cu->objfile->name);
9064
72bf9492
DJ
9065 /* If we're at the second level, and we're an enumerator, and
9066 our parent has no specification (meaning possibly lives in a
9067 namespace elsewhere), then we can add the partial symbol now
9068 instead of queueing it. */
9069 if (part_die->tag == DW_TAG_enumerator
9070 && parent_die != NULL
9071 && parent_die->die_parent == NULL
9072 && parent_die->tag == DW_TAG_enumeration_type
9073 && parent_die->has_specification == 0)
9074 {
9075 if (part_die->name == NULL)
3e43a32a
MS
9076 complaint (&symfile_complaints,
9077 _("malformed enumerator DIE ignored"));
72bf9492 9078 else if (building_psymtab)
04a679b8 9079 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9080 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9081 (cu->language == language_cplus
9082 || cu->language == language_java)
72bf9492
DJ
9083 ? &cu->objfile->global_psymbols
9084 : &cu->objfile->static_psymbols,
9085 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9086
93311388 9087 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9088 continue;
9089 }
9090
9091 /* We'll save this DIE so link it in. */
9092 part_die->die_parent = parent_die;
9093 part_die->die_sibling = NULL;
9094 part_die->die_child = NULL;
9095
9096 if (last_die && last_die == parent_die)
9097 last_die->die_child = part_die;
9098 else if (last_die)
9099 last_die->die_sibling = part_die;
9100
9101 last_die = part_die;
9102
9103 if (first_die == NULL)
9104 first_die = part_die;
9105
9106 /* Maybe add the DIE to the hash table. Not all DIEs that we
9107 find interesting need to be in the hash table, because we
9108 also have the parent/sibling/child chains; only those that we
9109 might refer to by offset later during partial symbol reading.
9110
9111 For now this means things that might have be the target of a
9112 DW_AT_specification, DW_AT_abstract_origin, or
9113 DW_AT_extension. DW_AT_extension will refer only to
9114 namespaces; DW_AT_abstract_origin refers to functions (and
9115 many things under the function DIE, but we do not recurse
9116 into function DIEs during partial symbol reading) and
9117 possibly variables as well; DW_AT_specification refers to
9118 declarations. Declarations ought to have the DW_AT_declaration
9119 flag. It happens that GCC forgets to put it in sometimes, but
9120 only for functions, not for types.
9121
9122 Adding more things than necessary to the hash table is harmless
9123 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9124 wasted time in find_partial_die, when we reread the compilation
9125 unit with load_all_dies set. */
72bf9492 9126
5afb4e99 9127 if (load_all
72929c62 9128 || abbrev->tag == DW_TAG_constant
5afb4e99 9129 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9130 || abbrev->tag == DW_TAG_variable
9131 || abbrev->tag == DW_TAG_namespace
9132 || part_die->is_declaration)
9133 {
9134 void **slot;
9135
9136 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9137 part_die->offset, INSERT);
9138 *slot = part_die;
9139 }
9140
9141 part_die = obstack_alloc (&cu->comp_unit_obstack,
9142 sizeof (struct partial_die_info));
9143
9144 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9145 we have no reason to follow the children of structures; for other
98bfdba5
PA
9146 languages we have to, so that we can get at method physnames
9147 to infer fully qualified class names, for DW_AT_specification,
9148 and for C++ template arguments. For C++, we also look one level
9149 inside functions to find template arguments (if the name of the
9150 function does not already contain the template arguments).
bc30ff58
JB
9151
9152 For Ada, we need to scan the children of subprograms and lexical
9153 blocks as well because Ada allows the definition of nested
9154 entities that could be interesting for the debugger, such as
9155 nested subprograms for instance. */
72bf9492 9156 if (last_die->has_children
5afb4e99
DJ
9157 && (load_all
9158 || last_die->tag == DW_TAG_namespace
f55ee35c 9159 || last_die->tag == DW_TAG_module
72bf9492 9160 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9161 || (cu->language == language_cplus
9162 && last_die->tag == DW_TAG_subprogram
9163 && (last_die->name == NULL
9164 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9165 || (cu->language != language_c
9166 && (last_die->tag == DW_TAG_class_type
680b30c7 9167 || last_die->tag == DW_TAG_interface_type
72bf9492 9168 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9169 || last_die->tag == DW_TAG_union_type))
9170 || (cu->language == language_ada
9171 && (last_die->tag == DW_TAG_subprogram
9172 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9173 {
9174 nesting_level++;
9175 parent_die = last_die;
9176 continue;
9177 }
9178
9179 /* Otherwise we skip to the next sibling, if any. */
93311388 9180 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9181
9182 /* Back to the top, do it again. */
9183 }
9184}
9185
c906108c
SS
9186/* Read a minimal amount of information into the minimal die structure. */
9187
fe1b8b76 9188static gdb_byte *
72bf9492
DJ
9189read_partial_die (struct partial_die_info *part_die,
9190 struct abbrev_info *abbrev,
9191 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9192 gdb_byte *buffer, gdb_byte *info_ptr,
9193 struct dwarf2_cu *cu)
c906108c 9194{
fa238c03 9195 unsigned int i;
c906108c 9196 struct attribute attr;
c5aa993b 9197 int has_low_pc_attr = 0;
c906108c
SS
9198 int has_high_pc_attr = 0;
9199
72bf9492 9200 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9201
93311388 9202 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9203
9204 info_ptr += abbrev_len;
9205
9206 if (abbrev == NULL)
9207 return info_ptr;
9208
c906108c
SS
9209 part_die->tag = abbrev->tag;
9210 part_die->has_children = abbrev->has_children;
c906108c
SS
9211
9212 for (i = 0; i < abbrev->num_attrs; ++i)
9213 {
e7c27a73 9214 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9215
9216 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9217 partial symbol table. */
c906108c
SS
9218 switch (attr.name)
9219 {
9220 case DW_AT_name:
71c25dea
TT
9221 switch (part_die->tag)
9222 {
9223 case DW_TAG_compile_unit:
348e048f 9224 case DW_TAG_type_unit:
71c25dea
TT
9225 /* Compilation units have a DW_AT_name that is a filename, not
9226 a source language identifier. */
9227 case DW_TAG_enumeration_type:
9228 case DW_TAG_enumerator:
9229 /* These tags always have simple identifiers already; no need
9230 to canonicalize them. */
9231 part_die->name = DW_STRING (&attr);
9232 break;
9233 default:
9234 part_die->name
9235 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9236 &cu->objfile->objfile_obstack);
71c25dea
TT
9237 break;
9238 }
c906108c 9239 break;
31ef98ae 9240 case DW_AT_linkage_name:
c906108c 9241 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9242 /* Note that both forms of linkage name might appear. We
9243 assume they will be the same, and we only store the last
9244 one we see. */
94af9270
KS
9245 if (cu->language == language_ada)
9246 part_die->name = DW_STRING (&attr);
abc72ce4 9247 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9248 break;
9249 case DW_AT_low_pc:
9250 has_low_pc_attr = 1;
9251 part_die->lowpc = DW_ADDR (&attr);
9252 break;
9253 case DW_AT_high_pc:
9254 has_high_pc_attr = 1;
9255 part_die->highpc = DW_ADDR (&attr);
9256 break;
9257 case DW_AT_location:
0963b4bd 9258 /* Support the .debug_loc offsets. */
8e19ed76
PS
9259 if (attr_form_is_block (&attr))
9260 {
9261 part_die->locdesc = DW_BLOCK (&attr);
9262 }
3690dd37 9263 else if (attr_form_is_section_offset (&attr))
8e19ed76 9264 {
4d3c2250 9265 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9266 }
9267 else
9268 {
4d3c2250
KB
9269 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9270 "partial symbol information");
8e19ed76 9271 }
c906108c 9272 break;
c906108c
SS
9273 case DW_AT_external:
9274 part_die->is_external = DW_UNSND (&attr);
9275 break;
9276 case DW_AT_declaration:
9277 part_die->is_declaration = DW_UNSND (&attr);
9278 break;
9279 case DW_AT_type:
9280 part_die->has_type = 1;
9281 break;
9282 case DW_AT_abstract_origin:
9283 case DW_AT_specification:
72bf9492
DJ
9284 case DW_AT_extension:
9285 part_die->has_specification = 1;
c764a876 9286 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9287 break;
9288 case DW_AT_sibling:
9289 /* Ignore absolute siblings, they might point outside of
9290 the current compile unit. */
9291 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9292 complaint (&symfile_complaints,
9293 _("ignoring absolute DW_AT_sibling"));
c906108c 9294 else
93311388 9295 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9296 break;
fa4028e9
JB
9297 case DW_AT_byte_size:
9298 part_die->has_byte_size = 1;
9299 break;
68511cec
CES
9300 case DW_AT_calling_convention:
9301 /* DWARF doesn't provide a way to identify a program's source-level
9302 entry point. DW_AT_calling_convention attributes are only meant
9303 to describe functions' calling conventions.
9304
9305 However, because it's a necessary piece of information in
9306 Fortran, and because DW_CC_program is the only piece of debugging
9307 information whose definition refers to a 'main program' at all,
9308 several compilers have begun marking Fortran main programs with
9309 DW_CC_program --- even when those functions use the standard
9310 calling conventions.
9311
9312 So until DWARF specifies a way to provide this information and
9313 compilers pick up the new representation, we'll support this
9314 practice. */
9315 if (DW_UNSND (&attr) == DW_CC_program
9316 && cu->language == language_fortran)
01f8c46d
JK
9317 {
9318 set_main_name (part_die->name);
9319
9320 /* As this DIE has a static linkage the name would be difficult
9321 to look up later. */
9322 language_of_main = language_fortran;
9323 }
68511cec 9324 break;
c906108c
SS
9325 default:
9326 break;
9327 }
9328 }
9329
9373cf26
JK
9330 if (has_low_pc_attr && has_high_pc_attr)
9331 {
9332 /* When using the GNU linker, .gnu.linkonce. sections are used to
9333 eliminate duplicate copies of functions and vtables and such.
9334 The linker will arbitrarily choose one and discard the others.
9335 The AT_*_pc values for such functions refer to local labels in
9336 these sections. If the section from that file was discarded, the
9337 labels are not in the output, so the relocs get a value of 0.
9338 If this is a discarded function, mark the pc bounds as invalid,
9339 so that GDB will ignore it. */
9340 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9341 {
9342 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9343
9344 complaint (&symfile_complaints,
9345 _("DW_AT_low_pc %s is zero "
9346 "for DIE at 0x%x [in module %s]"),
9347 paddress (gdbarch, part_die->lowpc),
9348 part_die->offset, cu->objfile->name);
9349 }
9350 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9351 else if (part_die->lowpc >= part_die->highpc)
9352 {
9353 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9354
9355 complaint (&symfile_complaints,
9356 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9357 "for DIE at 0x%x [in module %s]"),
9358 paddress (gdbarch, part_die->lowpc),
9359 paddress (gdbarch, part_die->highpc),
9360 part_die->offset, cu->objfile->name);
9361 }
9362 else
9363 part_die->has_pc_info = 1;
9364 }
85cbf3d3 9365
c906108c
SS
9366 return info_ptr;
9367}
9368
72bf9492
DJ
9369/* Find a cached partial DIE at OFFSET in CU. */
9370
9371static struct partial_die_info *
c764a876 9372find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9373{
9374 struct partial_die_info *lookup_die = NULL;
9375 struct partial_die_info part_die;
9376
9377 part_die.offset = offset;
9378 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9379
72bf9492
DJ
9380 return lookup_die;
9381}
9382
348e048f
DE
9383/* Find a partial DIE at OFFSET, which may or may not be in CU,
9384 except in the case of .debug_types DIEs which do not reference
9385 outside their CU (they do however referencing other types via
55f1336d 9386 DW_FORM_ref_sig8). */
72bf9492
DJ
9387
9388static struct partial_die_info *
c764a876 9389find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9390{
5afb4e99
DJ
9391 struct dwarf2_per_cu_data *per_cu = NULL;
9392 struct partial_die_info *pd = NULL;
72bf9492 9393
348e048f
DE
9394 if (cu->per_cu->from_debug_types)
9395 {
9396 pd = find_partial_die_in_comp_unit (offset, cu);
9397 if (pd != NULL)
9398 return pd;
9399 goto not_found;
9400 }
9401
45452591 9402 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9403 {
9404 pd = find_partial_die_in_comp_unit (offset, cu);
9405 if (pd != NULL)
9406 return pd;
9407 }
72bf9492 9408
ae038cb0
DJ
9409 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9410
98bfdba5
PA
9411 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9412 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9413
9414 per_cu->cu->last_used = 0;
5afb4e99
DJ
9415 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9416
9417 if (pd == NULL && per_cu->load_all_dies == 0)
9418 {
9419 struct cleanup *back_to;
9420 struct partial_die_info comp_unit_die;
9421 struct abbrev_info *abbrev;
9422 unsigned int bytes_read;
9423 char *info_ptr;
9424
9425 per_cu->load_all_dies = 1;
9426
9427 /* Re-read the DIEs. */
9428 back_to = make_cleanup (null_cleanup, 0);
9429 if (per_cu->cu->dwarf2_abbrevs == NULL)
9430 {
9431 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9432 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9433 }
dce234bc 9434 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9435 + per_cu->cu->header.offset
9436 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9437 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9438 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9439 per_cu->cu->objfile->obfd,
9440 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9441 per_cu->cu);
9442 if (comp_unit_die.has_children)
93311388
DE
9443 load_partial_dies (per_cu->cu->objfile->obfd,
9444 dwarf2_per_objfile->info.buffer, info_ptr,
9445 0, per_cu->cu);
5afb4e99
DJ
9446 do_cleanups (back_to);
9447
9448 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9449 }
9450
348e048f
DE
9451 not_found:
9452
5afb4e99
DJ
9453 if (pd == NULL)
9454 internal_error (__FILE__, __LINE__,
3e43a32a
MS
9455 _("could not find partial DIE 0x%x "
9456 "in cache [from module %s]\n"),
5afb4e99
DJ
9457 offset, bfd_get_filename (cu->objfile->obfd));
9458 return pd;
72bf9492
DJ
9459}
9460
abc72ce4
DE
9461/* See if we can figure out if the class lives in a namespace. We do
9462 this by looking for a member function; its demangled name will
9463 contain namespace info, if there is any. */
9464
9465static void
9466guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9467 struct dwarf2_cu *cu)
9468{
9469 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9470 what template types look like, because the demangler
9471 frequently doesn't give the same name as the debug info. We
9472 could fix this by only using the demangled name to get the
9473 prefix (but see comment in read_structure_type). */
9474
9475 struct partial_die_info *real_pdi;
9476 struct partial_die_info *child_pdi;
9477
9478 /* If this DIE (this DIE's specification, if any) has a parent, then
9479 we should not do this. We'll prepend the parent's fully qualified
9480 name when we create the partial symbol. */
9481
9482 real_pdi = struct_pdi;
9483 while (real_pdi->has_specification)
9484 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9485
9486 if (real_pdi->die_parent != NULL)
9487 return;
9488
9489 for (child_pdi = struct_pdi->die_child;
9490 child_pdi != NULL;
9491 child_pdi = child_pdi->die_sibling)
9492 {
9493 if (child_pdi->tag == DW_TAG_subprogram
9494 && child_pdi->linkage_name != NULL)
9495 {
9496 char *actual_class_name
9497 = language_class_name_from_physname (cu->language_defn,
9498 child_pdi->linkage_name);
9499 if (actual_class_name != NULL)
9500 {
9501 struct_pdi->name
9502 = obsavestring (actual_class_name,
9503 strlen (actual_class_name),
9504 &cu->objfile->objfile_obstack);
9505 xfree (actual_class_name);
9506 }
9507 break;
9508 }
9509 }
9510}
9511
72bf9492
DJ
9512/* Adjust PART_DIE before generating a symbol for it. This function
9513 may set the is_external flag or change the DIE's name. */
9514
9515static void
9516fixup_partial_die (struct partial_die_info *part_die,
9517 struct dwarf2_cu *cu)
9518{
abc72ce4
DE
9519 /* Once we've fixed up a die, there's no point in doing so again.
9520 This also avoids a memory leak if we were to call
9521 guess_partial_die_structure_name multiple times. */
9522 if (part_die->fixup_called)
9523 return;
9524
72bf9492
DJ
9525 /* If we found a reference attribute and the DIE has no name, try
9526 to find a name in the referred to DIE. */
9527
9528 if (part_die->name == NULL && part_die->has_specification)
9529 {
9530 struct partial_die_info *spec_die;
72bf9492 9531
10b3939b 9532 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9533
10b3939b 9534 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9535
9536 if (spec_die->name)
9537 {
9538 part_die->name = spec_die->name;
9539
9540 /* Copy DW_AT_external attribute if it is set. */
9541 if (spec_die->is_external)
9542 part_die->is_external = spec_die->is_external;
9543 }
9544 }
9545
9546 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9547
9548 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 9549 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 9550
abc72ce4
DE
9551 /* If there is no parent die to provide a namespace, and there are
9552 children, see if we can determine the namespace from their linkage
9553 name.
9554 NOTE: We need to do this even if cu->has_namespace_info != 0.
9555 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9556 if (cu->language == language_cplus
9557 && dwarf2_per_objfile->types.asection != NULL
9558 && part_die->die_parent == NULL
9559 && part_die->has_children
9560 && (part_die->tag == DW_TAG_class_type
9561 || part_die->tag == DW_TAG_structure_type
9562 || part_die->tag == DW_TAG_union_type))
9563 guess_partial_die_structure_name (part_die, cu);
9564
53832f31
TT
9565 /* GCC might emit a nameless struct or union that has a linkage
9566 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
9567 if (part_die->name == NULL
9568 && (part_die->tag == DW_TAG_structure_type
9569 || part_die->tag == DW_TAG_union_type
9570 || part_die->tag == DW_TAG_class_type)
9571 && part_die->linkage_name != NULL)
9572 {
9573 char *demangled;
9574
9575 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9576 if (demangled)
9577 {
9578 part_die->name = obsavestring (demangled, strlen (demangled),
9579 &cu->objfile->objfile_obstack);
9580 xfree (demangled);
9581 }
9582 }
9583
abc72ce4 9584 part_die->fixup_called = 1;
72bf9492
DJ
9585}
9586
a8329558 9587/* Read an attribute value described by an attribute form. */
c906108c 9588
fe1b8b76 9589static gdb_byte *
a8329558 9590read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9591 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9592 struct dwarf2_cu *cu)
c906108c 9593{
e7c27a73 9594 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9595 unsigned int bytes_read;
9596 struct dwarf_block *blk;
9597
a8329558
KW
9598 attr->form = form;
9599 switch (form)
c906108c 9600 {
c906108c 9601 case DW_FORM_ref_addr:
ae411497
TT
9602 if (cu->header.version == 2)
9603 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9604 else
3e43a32a
MS
9605 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9606 &cu->header, &bytes_read);
ae411497
TT
9607 info_ptr += bytes_read;
9608 break;
9609 case DW_FORM_addr:
e7c27a73 9610 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9611 info_ptr += bytes_read;
c906108c
SS
9612 break;
9613 case DW_FORM_block2:
7b5a2f43 9614 blk = dwarf_alloc_block (cu);
c906108c
SS
9615 blk->size = read_2_bytes (abfd, info_ptr);
9616 info_ptr += 2;
9617 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9618 info_ptr += blk->size;
9619 DW_BLOCK (attr) = blk;
9620 break;
9621 case DW_FORM_block4:
7b5a2f43 9622 blk = dwarf_alloc_block (cu);
c906108c
SS
9623 blk->size = read_4_bytes (abfd, info_ptr);
9624 info_ptr += 4;
9625 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9626 info_ptr += blk->size;
9627 DW_BLOCK (attr) = blk;
9628 break;
9629 case DW_FORM_data2:
9630 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9631 info_ptr += 2;
9632 break;
9633 case DW_FORM_data4:
9634 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9635 info_ptr += 4;
9636 break;
9637 case DW_FORM_data8:
9638 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9639 info_ptr += 8;
9640 break;
2dc7f7b3
TT
9641 case DW_FORM_sec_offset:
9642 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9643 info_ptr += bytes_read;
9644 break;
c906108c 9645 case DW_FORM_string:
9b1c24c8 9646 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9647 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9648 info_ptr += bytes_read;
9649 break;
4bdf3d34
JJ
9650 case DW_FORM_strp:
9651 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9652 &bytes_read);
8285870a 9653 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9654 info_ptr += bytes_read;
9655 break;
2dc7f7b3 9656 case DW_FORM_exprloc:
c906108c 9657 case DW_FORM_block:
7b5a2f43 9658 blk = dwarf_alloc_block (cu);
c906108c
SS
9659 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9660 info_ptr += bytes_read;
9661 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9662 info_ptr += blk->size;
9663 DW_BLOCK (attr) = blk;
9664 break;
9665 case DW_FORM_block1:
7b5a2f43 9666 blk = dwarf_alloc_block (cu);
c906108c
SS
9667 blk->size = read_1_byte (abfd, info_ptr);
9668 info_ptr += 1;
9669 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9670 info_ptr += blk->size;
9671 DW_BLOCK (attr) = blk;
9672 break;
9673 case DW_FORM_data1:
9674 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9675 info_ptr += 1;
9676 break;
9677 case DW_FORM_flag:
9678 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9679 info_ptr += 1;
9680 break;
2dc7f7b3
TT
9681 case DW_FORM_flag_present:
9682 DW_UNSND (attr) = 1;
9683 break;
c906108c
SS
9684 case DW_FORM_sdata:
9685 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9686 info_ptr += bytes_read;
9687 break;
9688 case DW_FORM_udata:
9689 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9690 info_ptr += bytes_read;
9691 break;
9692 case DW_FORM_ref1:
10b3939b 9693 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9694 info_ptr += 1;
9695 break;
9696 case DW_FORM_ref2:
10b3939b 9697 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9698 info_ptr += 2;
9699 break;
9700 case DW_FORM_ref4:
10b3939b 9701 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9702 info_ptr += 4;
9703 break;
613e1657 9704 case DW_FORM_ref8:
10b3939b 9705 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9706 info_ptr += 8;
9707 break;
55f1336d 9708 case DW_FORM_ref_sig8:
348e048f
DE
9709 /* Convert the signature to something we can record in DW_UNSND
9710 for later lookup.
9711 NOTE: This is NULL if the type wasn't found. */
9712 DW_SIGNATURED_TYPE (attr) =
9713 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9714 info_ptr += 8;
9715 break;
c906108c 9716 case DW_FORM_ref_udata:
10b3939b
DJ
9717 DW_ADDR (attr) = (cu->header.offset
9718 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9719 info_ptr += bytes_read;
9720 break;
c906108c 9721 case DW_FORM_indirect:
a8329558
KW
9722 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9723 info_ptr += bytes_read;
e7c27a73 9724 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9725 break;
c906108c 9726 default:
8a3fe4f8 9727 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9728 dwarf_form_name (form),
9729 bfd_get_filename (abfd));
c906108c 9730 }
28e94949
JB
9731
9732 /* We have seen instances where the compiler tried to emit a byte
9733 size attribute of -1 which ended up being encoded as an unsigned
9734 0xffffffff. Although 0xffffffff is technically a valid size value,
9735 an object of this size seems pretty unlikely so we can relatively
9736 safely treat these cases as if the size attribute was invalid and
9737 treat them as zero by default. */
9738 if (attr->name == DW_AT_byte_size
9739 && form == DW_FORM_data4
9740 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9741 {
9742 complaint
9743 (&symfile_complaints,
43bbcdc2
PH
9744 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9745 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9746 DW_UNSND (attr) = 0;
9747 }
28e94949 9748
c906108c
SS
9749 return info_ptr;
9750}
9751
a8329558
KW
9752/* Read an attribute described by an abbreviated attribute. */
9753
fe1b8b76 9754static gdb_byte *
a8329558 9755read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9756 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9757{
9758 attr->name = abbrev->name;
e7c27a73 9759 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9760}
9761
0963b4bd 9762/* Read dwarf information from a buffer. */
c906108c
SS
9763
9764static unsigned int
fe1b8b76 9765read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9766{
fe1b8b76 9767 return bfd_get_8 (abfd, buf);
c906108c
SS
9768}
9769
9770static int
fe1b8b76 9771read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9772{
fe1b8b76 9773 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9774}
9775
9776static unsigned int
fe1b8b76 9777read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9778{
fe1b8b76 9779 return bfd_get_16 (abfd, buf);
c906108c
SS
9780}
9781
9782static int
fe1b8b76 9783read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9784{
fe1b8b76 9785 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9786}
9787
9788static unsigned int
fe1b8b76 9789read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9790{
fe1b8b76 9791 return bfd_get_32 (abfd, buf);
c906108c
SS
9792}
9793
9794static int
fe1b8b76 9795read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9796{
fe1b8b76 9797 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9798}
9799
93311388 9800static ULONGEST
fe1b8b76 9801read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9802{
fe1b8b76 9803 return bfd_get_64 (abfd, buf);
c906108c
SS
9804}
9805
9806static CORE_ADDR
fe1b8b76 9807read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9808 unsigned int *bytes_read)
c906108c 9809{
e7c27a73 9810 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9811 CORE_ADDR retval = 0;
9812
107d2387 9813 if (cu_header->signed_addr_p)
c906108c 9814 {
107d2387
AC
9815 switch (cu_header->addr_size)
9816 {
9817 case 2:
fe1b8b76 9818 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9819 break;
9820 case 4:
fe1b8b76 9821 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9822 break;
9823 case 8:
fe1b8b76 9824 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9825 break;
9826 default:
8e65ff28 9827 internal_error (__FILE__, __LINE__,
e2e0b3e5 9828 _("read_address: bad switch, signed [in module %s]"),
659b0389 9829 bfd_get_filename (abfd));
107d2387
AC
9830 }
9831 }
9832 else
9833 {
9834 switch (cu_header->addr_size)
9835 {
9836 case 2:
fe1b8b76 9837 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9838 break;
9839 case 4:
fe1b8b76 9840 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9841 break;
9842 case 8:
fe1b8b76 9843 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9844 break;
9845 default:
8e65ff28 9846 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
9847 _("read_address: bad switch, "
9848 "unsigned [in module %s]"),
659b0389 9849 bfd_get_filename (abfd));
107d2387 9850 }
c906108c 9851 }
64367e0a 9852
107d2387
AC
9853 *bytes_read = cu_header->addr_size;
9854 return retval;
c906108c
SS
9855}
9856
f7ef9339 9857/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9858 specification allows the initial length to take up either 4 bytes
9859 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9860 bytes describe the length and all offsets will be 8 bytes in length
9861 instead of 4.
9862
f7ef9339
KB
9863 An older, non-standard 64-bit format is also handled by this
9864 function. The older format in question stores the initial length
9865 as an 8-byte quantity without an escape value. Lengths greater
9866 than 2^32 aren't very common which means that the initial 4 bytes
9867 is almost always zero. Since a length value of zero doesn't make
9868 sense for the 32-bit format, this initial zero can be considered to
9869 be an escape value which indicates the presence of the older 64-bit
9870 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9871 greater than 4GB. If it becomes necessary to handle lengths
9872 somewhat larger than 4GB, we could allow other small values (such
9873 as the non-sensical values of 1, 2, and 3) to also be used as
9874 escape values indicating the presence of the old format.
f7ef9339 9875
917c78fc
MK
9876 The value returned via bytes_read should be used to increment the
9877 relevant pointer after calling read_initial_length().
c764a876 9878
613e1657
KB
9879 [ Note: read_initial_length() and read_offset() are based on the
9880 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9881 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9882 from:
9883
f7ef9339 9884 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9885
613e1657
KB
9886 This document is only a draft and is subject to change. (So beware.)
9887
f7ef9339 9888 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9889 determined empirically by examining 64-bit ELF files produced by
9890 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9891
9892 - Kevin, July 16, 2002
613e1657
KB
9893 ] */
9894
9895static LONGEST
c764a876 9896read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9897{
fe1b8b76 9898 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9899
dd373385 9900 if (length == 0xffffffff)
613e1657 9901 {
fe1b8b76 9902 length = bfd_get_64 (abfd, buf + 4);
613e1657 9903 *bytes_read = 12;
613e1657 9904 }
dd373385 9905 else if (length == 0)
f7ef9339 9906 {
dd373385 9907 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9908 length = bfd_get_64 (abfd, buf);
f7ef9339 9909 *bytes_read = 8;
f7ef9339 9910 }
613e1657
KB
9911 else
9912 {
9913 *bytes_read = 4;
613e1657
KB
9914 }
9915
c764a876
DE
9916 return length;
9917}
dd373385 9918
c764a876
DE
9919/* Cover function for read_initial_length.
9920 Returns the length of the object at BUF, and stores the size of the
9921 initial length in *BYTES_READ and stores the size that offsets will be in
9922 *OFFSET_SIZE.
9923 If the initial length size is not equivalent to that specified in
9924 CU_HEADER then issue a complaint.
9925 This is useful when reading non-comp-unit headers. */
dd373385 9926
c764a876
DE
9927static LONGEST
9928read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9929 const struct comp_unit_head *cu_header,
9930 unsigned int *bytes_read,
9931 unsigned int *offset_size)
9932{
9933 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9934
9935 gdb_assert (cu_header->initial_length_size == 4
9936 || cu_header->initial_length_size == 8
9937 || cu_header->initial_length_size == 12);
9938
9939 if (cu_header->initial_length_size != *bytes_read)
9940 complaint (&symfile_complaints,
9941 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9942
c764a876 9943 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9944 return length;
613e1657
KB
9945}
9946
9947/* Read an offset from the data stream. The size of the offset is
917c78fc 9948 given by cu_header->offset_size. */
613e1657
KB
9949
9950static LONGEST
fe1b8b76 9951read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9952 unsigned int *bytes_read)
c764a876
DE
9953{
9954 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9955
c764a876
DE
9956 *bytes_read = cu_header->offset_size;
9957 return offset;
9958}
9959
9960/* Read an offset from the data stream. */
9961
9962static LONGEST
9963read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9964{
9965 LONGEST retval = 0;
9966
c764a876 9967 switch (offset_size)
613e1657
KB
9968 {
9969 case 4:
fe1b8b76 9970 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9971 break;
9972 case 8:
fe1b8b76 9973 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9974 break;
9975 default:
8e65ff28 9976 internal_error (__FILE__, __LINE__,
c764a876 9977 _("read_offset_1: bad switch [in module %s]"),
659b0389 9978 bfd_get_filename (abfd));
613e1657
KB
9979 }
9980
917c78fc 9981 return retval;
613e1657
KB
9982}
9983
fe1b8b76
JB
9984static gdb_byte *
9985read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9986{
9987 /* If the size of a host char is 8 bits, we can return a pointer
9988 to the buffer, otherwise we have to copy the data to a buffer
9989 allocated on the temporary obstack. */
4bdf3d34 9990 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9991 return buf;
c906108c
SS
9992}
9993
9994static char *
9b1c24c8 9995read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9996{
9997 /* If the size of a host char is 8 bits, we can return a pointer
9998 to the string, otherwise we have to copy the string to a buffer
9999 allocated on the temporary obstack. */
4bdf3d34 10000 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10001 if (*buf == '\0')
10002 {
10003 *bytes_read_ptr = 1;
10004 return NULL;
10005 }
fe1b8b76
JB
10006 *bytes_read_ptr = strlen ((char *) buf) + 1;
10007 return (char *) buf;
4bdf3d34
JJ
10008}
10009
10010static char *
fe1b8b76 10011read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
10012 const struct comp_unit_head *cu_header,
10013 unsigned int *bytes_read_ptr)
10014{
c764a876 10015 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 10016
be391dca 10017 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10018 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 10019 {
8a3fe4f8 10020 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 10021 bfd_get_filename (abfd));
4bdf3d34 10022 return NULL;
c906108c 10023 }
dce234bc 10024 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 10025 {
3e43a32a
MS
10026 error (_("DW_FORM_strp pointing outside of "
10027 ".debug_str section [in module %s]"),
10028 bfd_get_filename (abfd));
c906108c
SS
10029 return NULL;
10030 }
4bdf3d34 10031 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10032 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10033 return NULL;
dce234bc 10034 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10035}
10036
ce5d95e1 10037static unsigned long
fe1b8b76 10038read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10039{
ce5d95e1
JB
10040 unsigned long result;
10041 unsigned int num_read;
c906108c
SS
10042 int i, shift;
10043 unsigned char byte;
10044
10045 result = 0;
10046 shift = 0;
10047 num_read = 0;
10048 i = 0;
10049 while (1)
10050 {
fe1b8b76 10051 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10052 buf++;
10053 num_read++;
ce5d95e1 10054 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10055 if ((byte & 128) == 0)
10056 {
10057 break;
10058 }
10059 shift += 7;
10060 }
10061 *bytes_read_ptr = num_read;
10062 return result;
10063}
10064
ce5d95e1 10065static long
fe1b8b76 10066read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10067{
ce5d95e1 10068 long result;
77e0b926 10069 int i, shift, num_read;
c906108c
SS
10070 unsigned char byte;
10071
10072 result = 0;
10073 shift = 0;
c906108c
SS
10074 num_read = 0;
10075 i = 0;
10076 while (1)
10077 {
fe1b8b76 10078 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10079 buf++;
10080 num_read++;
ce5d95e1 10081 result |= ((long)(byte & 127) << shift);
c906108c
SS
10082 shift += 7;
10083 if ((byte & 128) == 0)
10084 {
10085 break;
10086 }
10087 }
77e0b926
DJ
10088 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10089 result |= -(((long)1) << shift);
c906108c
SS
10090 *bytes_read_ptr = num_read;
10091 return result;
10092}
10093
4bb7a0a7
DJ
10094/* Return a pointer to just past the end of an LEB128 number in BUF. */
10095
fe1b8b76
JB
10096static gdb_byte *
10097skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10098{
10099 int byte;
10100
10101 while (1)
10102 {
fe1b8b76 10103 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10104 buf++;
10105 if ((byte & 128) == 0)
10106 return buf;
10107 }
10108}
10109
c906108c 10110static void
e142c38c 10111set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10112{
10113 switch (lang)
10114 {
10115 case DW_LANG_C89:
76bee0cc 10116 case DW_LANG_C99:
c906108c 10117 case DW_LANG_C:
e142c38c 10118 cu->language = language_c;
c906108c
SS
10119 break;
10120 case DW_LANG_C_plus_plus:
e142c38c 10121 cu->language = language_cplus;
c906108c 10122 break;
6aecb9c2
JB
10123 case DW_LANG_D:
10124 cu->language = language_d;
10125 break;
c906108c
SS
10126 case DW_LANG_Fortran77:
10127 case DW_LANG_Fortran90:
b21b22e0 10128 case DW_LANG_Fortran95:
e142c38c 10129 cu->language = language_fortran;
c906108c
SS
10130 break;
10131 case DW_LANG_Mips_Assembler:
e142c38c 10132 cu->language = language_asm;
c906108c 10133 break;
bebd888e 10134 case DW_LANG_Java:
e142c38c 10135 cu->language = language_java;
bebd888e 10136 break;
c906108c 10137 case DW_LANG_Ada83:
8aaf0b47 10138 case DW_LANG_Ada95:
bc5f45f8
JB
10139 cu->language = language_ada;
10140 break;
72019c9c
GM
10141 case DW_LANG_Modula2:
10142 cu->language = language_m2;
10143 break;
fe8e67fd
PM
10144 case DW_LANG_Pascal83:
10145 cu->language = language_pascal;
10146 break;
22566fbd
DJ
10147 case DW_LANG_ObjC:
10148 cu->language = language_objc;
10149 break;
c906108c
SS
10150 case DW_LANG_Cobol74:
10151 case DW_LANG_Cobol85:
c906108c 10152 default:
e142c38c 10153 cu->language = language_minimal;
c906108c
SS
10154 break;
10155 }
e142c38c 10156 cu->language_defn = language_def (cu->language);
c906108c
SS
10157}
10158
10159/* Return the named attribute or NULL if not there. */
10160
10161static struct attribute *
e142c38c 10162dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10163{
10164 unsigned int i;
10165 struct attribute *spec = NULL;
10166
10167 for (i = 0; i < die->num_attrs; ++i)
10168 {
10169 if (die->attrs[i].name == name)
10b3939b 10170 return &die->attrs[i];
c906108c
SS
10171 if (die->attrs[i].name == DW_AT_specification
10172 || die->attrs[i].name == DW_AT_abstract_origin)
10173 spec = &die->attrs[i];
10174 }
c906108c 10175
10b3939b 10176 if (spec)
f2f0e013
DJ
10177 {
10178 die = follow_die_ref (die, spec, &cu);
10179 return dwarf2_attr (die, name, cu);
10180 }
c5aa993b 10181
c906108c
SS
10182 return NULL;
10183}
10184
348e048f
DE
10185/* Return the named attribute or NULL if not there,
10186 but do not follow DW_AT_specification, etc.
10187 This is for use in contexts where we're reading .debug_types dies.
10188 Following DW_AT_specification, DW_AT_abstract_origin will take us
10189 back up the chain, and we want to go down. */
10190
10191static struct attribute *
10192dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10193 struct dwarf2_cu *cu)
10194{
10195 unsigned int i;
10196
10197 for (i = 0; i < die->num_attrs; ++i)
10198 if (die->attrs[i].name == name)
10199 return &die->attrs[i];
10200
10201 return NULL;
10202}
10203
05cf31d1
JB
10204/* Return non-zero iff the attribute NAME is defined for the given DIE,
10205 and holds a non-zero value. This function should only be used for
2dc7f7b3 10206 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10207
10208static int
10209dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10210{
10211 struct attribute *attr = dwarf2_attr (die, name, cu);
10212
10213 return (attr && DW_UNSND (attr));
10214}
10215
3ca72b44 10216static int
e142c38c 10217die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10218{
05cf31d1
JB
10219 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10220 which value is non-zero. However, we have to be careful with
10221 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10222 (via dwarf2_flag_true_p) follows this attribute. So we may
10223 end up accidently finding a declaration attribute that belongs
10224 to a different DIE referenced by the specification attribute,
10225 even though the given DIE does not have a declaration attribute. */
10226 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10227 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10228}
10229
63d06c5c 10230/* Return the die giving the specification for DIE, if there is
f2f0e013 10231 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10232 containing the return value on output. If there is no
10233 specification, but there is an abstract origin, that is
10234 returned. */
63d06c5c
DC
10235
10236static struct die_info *
f2f0e013 10237die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10238{
f2f0e013
DJ
10239 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10240 *spec_cu);
63d06c5c 10241
edb3359d
DJ
10242 if (spec_attr == NULL)
10243 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10244
63d06c5c
DC
10245 if (spec_attr == NULL)
10246 return NULL;
10247 else
f2f0e013 10248 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10249}
c906108c 10250
debd256d 10251/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10252 refers to.
10253 NOTE: This is also used as a "cleanup" function. */
10254
debd256d
JB
10255static void
10256free_line_header (struct line_header *lh)
10257{
10258 if (lh->standard_opcode_lengths)
a8bc7b56 10259 xfree (lh->standard_opcode_lengths);
debd256d
JB
10260
10261 /* Remember that all the lh->file_names[i].name pointers are
10262 pointers into debug_line_buffer, and don't need to be freed. */
10263 if (lh->file_names)
a8bc7b56 10264 xfree (lh->file_names);
debd256d
JB
10265
10266 /* Similarly for the include directory names. */
10267 if (lh->include_dirs)
a8bc7b56 10268 xfree (lh->include_dirs);
debd256d 10269
a8bc7b56 10270 xfree (lh);
debd256d
JB
10271}
10272
debd256d 10273/* Add an entry to LH's include directory table. */
ae2de4f8 10274
debd256d
JB
10275static void
10276add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10277{
debd256d
JB
10278 /* Grow the array if necessary. */
10279 if (lh->include_dirs_size == 0)
c5aa993b 10280 {
debd256d
JB
10281 lh->include_dirs_size = 1; /* for testing */
10282 lh->include_dirs = xmalloc (lh->include_dirs_size
10283 * sizeof (*lh->include_dirs));
10284 }
10285 else if (lh->num_include_dirs >= lh->include_dirs_size)
10286 {
10287 lh->include_dirs_size *= 2;
10288 lh->include_dirs = xrealloc (lh->include_dirs,
10289 (lh->include_dirs_size
10290 * sizeof (*lh->include_dirs)));
c5aa993b 10291 }
c906108c 10292
debd256d
JB
10293 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10294}
6e70227d 10295
debd256d 10296/* Add an entry to LH's file name table. */
ae2de4f8 10297
debd256d
JB
10298static void
10299add_file_name (struct line_header *lh,
10300 char *name,
10301 unsigned int dir_index,
10302 unsigned int mod_time,
10303 unsigned int length)
10304{
10305 struct file_entry *fe;
10306
10307 /* Grow the array if necessary. */
10308 if (lh->file_names_size == 0)
10309 {
10310 lh->file_names_size = 1; /* for testing */
10311 lh->file_names = xmalloc (lh->file_names_size
10312 * sizeof (*lh->file_names));
10313 }
10314 else if (lh->num_file_names >= lh->file_names_size)
10315 {
10316 lh->file_names_size *= 2;
10317 lh->file_names = xrealloc (lh->file_names,
10318 (lh->file_names_size
10319 * sizeof (*lh->file_names)));
10320 }
10321
10322 fe = &lh->file_names[lh->num_file_names++];
10323 fe->name = name;
10324 fe->dir_index = dir_index;
10325 fe->mod_time = mod_time;
10326 fe->length = length;
aaa75496 10327 fe->included_p = 0;
cb1df416 10328 fe->symtab = NULL;
debd256d 10329}
6e70227d 10330
debd256d 10331/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10332 .debug_line, according to the endianness of ABFD. Return a pointer
10333 to a struct line_header, allocated using xmalloc.
debd256d
JB
10334
10335 NOTE: the strings in the include directory and file name tables of
10336 the returned object point into debug_line_buffer, and must not be
10337 freed. */
ae2de4f8 10338
debd256d
JB
10339static struct line_header *
10340dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10341 struct dwarf2_cu *cu)
debd256d
JB
10342{
10343 struct cleanup *back_to;
10344 struct line_header *lh;
fe1b8b76 10345 gdb_byte *line_ptr;
c764a876 10346 unsigned int bytes_read, offset_size;
debd256d
JB
10347 int i;
10348 char *cur_dir, *cur_file;
10349
be391dca 10350 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10351 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10352 {
e2e0b3e5 10353 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10354 return 0;
10355 }
10356
a738430d
MK
10357 /* Make sure that at least there's room for the total_length field.
10358 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10359 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10360 {
4d3c2250 10361 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10362 return 0;
10363 }
10364
10365 lh = xmalloc (sizeof (*lh));
10366 memset (lh, 0, sizeof (*lh));
10367 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10368 (void *) lh);
10369
dce234bc 10370 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10371
a738430d 10372 /* Read in the header. */
6e70227d 10373 lh->total_length =
c764a876
DE
10374 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10375 &bytes_read, &offset_size);
debd256d 10376 line_ptr += bytes_read;
dce234bc
PP
10377 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10378 + dwarf2_per_objfile->line.size))
debd256d 10379 {
4d3c2250 10380 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10381 return 0;
10382 }
10383 lh->statement_program_end = line_ptr + lh->total_length;
10384 lh->version = read_2_bytes (abfd, line_ptr);
10385 line_ptr += 2;
c764a876
DE
10386 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10387 line_ptr += offset_size;
debd256d
JB
10388 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10389 line_ptr += 1;
2dc7f7b3
TT
10390 if (lh->version >= 4)
10391 {
10392 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10393 line_ptr += 1;
10394 }
10395 else
10396 lh->maximum_ops_per_instruction = 1;
10397
10398 if (lh->maximum_ops_per_instruction == 0)
10399 {
10400 lh->maximum_ops_per_instruction = 1;
10401 complaint (&symfile_complaints,
3e43a32a
MS
10402 _("invalid maximum_ops_per_instruction "
10403 "in `.debug_line' section"));
2dc7f7b3
TT
10404 }
10405
debd256d
JB
10406 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10407 line_ptr += 1;
10408 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10409 line_ptr += 1;
10410 lh->line_range = read_1_byte (abfd, line_ptr);
10411 line_ptr += 1;
10412 lh->opcode_base = read_1_byte (abfd, line_ptr);
10413 line_ptr += 1;
10414 lh->standard_opcode_lengths
fe1b8b76 10415 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10416
10417 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10418 for (i = 1; i < lh->opcode_base; ++i)
10419 {
10420 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10421 line_ptr += 1;
10422 }
10423
a738430d 10424 /* Read directory table. */
9b1c24c8 10425 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10426 {
10427 line_ptr += bytes_read;
10428 add_include_dir (lh, cur_dir);
10429 }
10430 line_ptr += bytes_read;
10431
a738430d 10432 /* Read file name table. */
9b1c24c8 10433 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10434 {
10435 unsigned int dir_index, mod_time, length;
10436
10437 line_ptr += bytes_read;
10438 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10439 line_ptr += bytes_read;
10440 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10441 line_ptr += bytes_read;
10442 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10443 line_ptr += bytes_read;
10444
10445 add_file_name (lh, cur_file, dir_index, mod_time, length);
10446 }
10447 line_ptr += bytes_read;
6e70227d 10448 lh->statement_program_start = line_ptr;
debd256d 10449
dce234bc
PP
10450 if (line_ptr > (dwarf2_per_objfile->line.buffer
10451 + dwarf2_per_objfile->line.size))
4d3c2250 10452 complaint (&symfile_complaints,
3e43a32a
MS
10453 _("line number info header doesn't "
10454 "fit in `.debug_line' section"));
debd256d
JB
10455
10456 discard_cleanups (back_to);
10457 return lh;
10458}
c906108c 10459
5fb290d7
DJ
10460/* This function exists to work around a bug in certain compilers
10461 (particularly GCC 2.95), in which the first line number marker of a
10462 function does not show up until after the prologue, right before
10463 the second line number marker. This function shifts ADDRESS down
10464 to the beginning of the function if necessary, and is called on
10465 addresses passed to record_line. */
10466
10467static CORE_ADDR
e142c38c 10468check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10469{
10470 struct function_range *fn;
10471
10472 /* Find the function_range containing address. */
e142c38c 10473 if (!cu->first_fn)
5fb290d7
DJ
10474 return address;
10475
e142c38c
DJ
10476 if (!cu->cached_fn)
10477 cu->cached_fn = cu->first_fn;
5fb290d7 10478
e142c38c 10479 fn = cu->cached_fn;
5fb290d7
DJ
10480 while (fn)
10481 if (fn->lowpc <= address && fn->highpc > address)
10482 goto found;
10483 else
10484 fn = fn->next;
10485
e142c38c
DJ
10486 fn = cu->first_fn;
10487 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10488 if (fn->lowpc <= address && fn->highpc > address)
10489 goto found;
10490 else
10491 fn = fn->next;
10492
10493 return address;
10494
10495 found:
10496 if (fn->seen_line)
10497 return address;
10498 if (address != fn->lowpc)
4d3c2250 10499 complaint (&symfile_complaints,
e2e0b3e5 10500 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10501 (unsigned long) address, fn->name);
5fb290d7
DJ
10502 fn->seen_line = 1;
10503 return fn->lowpc;
10504}
10505
c6da4cef
DE
10506/* Subroutine of dwarf_decode_lines to simplify it.
10507 Return the file name of the psymtab for included file FILE_INDEX
10508 in line header LH of PST.
10509 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10510 If space for the result is malloc'd, it will be freed by a cleanup.
10511 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10512
10513static char *
10514psymtab_include_file_name (const struct line_header *lh, int file_index,
10515 const struct partial_symtab *pst,
10516 const char *comp_dir)
10517{
10518 const struct file_entry fe = lh->file_names [file_index];
10519 char *include_name = fe.name;
10520 char *include_name_to_compare = include_name;
10521 char *dir_name = NULL;
72b9f47f
TT
10522 const char *pst_filename;
10523 char *copied_name = NULL;
c6da4cef
DE
10524 int file_is_pst;
10525
10526 if (fe.dir_index)
10527 dir_name = lh->include_dirs[fe.dir_index - 1];
10528
10529 if (!IS_ABSOLUTE_PATH (include_name)
10530 && (dir_name != NULL || comp_dir != NULL))
10531 {
10532 /* Avoid creating a duplicate psymtab for PST.
10533 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10534 Before we do the comparison, however, we need to account
10535 for DIR_NAME and COMP_DIR.
10536 First prepend dir_name (if non-NULL). If we still don't
10537 have an absolute path prepend comp_dir (if non-NULL).
10538 However, the directory we record in the include-file's
10539 psymtab does not contain COMP_DIR (to match the
10540 corresponding symtab(s)).
10541
10542 Example:
10543
10544 bash$ cd /tmp
10545 bash$ gcc -g ./hello.c
10546 include_name = "hello.c"
10547 dir_name = "."
10548 DW_AT_comp_dir = comp_dir = "/tmp"
10549 DW_AT_name = "./hello.c" */
10550
10551 if (dir_name != NULL)
10552 {
10553 include_name = concat (dir_name, SLASH_STRING,
10554 include_name, (char *)NULL);
10555 include_name_to_compare = include_name;
10556 make_cleanup (xfree, include_name);
10557 }
10558 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10559 {
10560 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10561 include_name, (char *)NULL);
10562 }
10563 }
10564
10565 pst_filename = pst->filename;
10566 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10567 {
72b9f47f
TT
10568 copied_name = concat (pst->dirname, SLASH_STRING,
10569 pst_filename, (char *)NULL);
10570 pst_filename = copied_name;
c6da4cef
DE
10571 }
10572
1e3fad37 10573 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10574
10575 if (include_name_to_compare != include_name)
10576 xfree (include_name_to_compare);
72b9f47f
TT
10577 if (copied_name != NULL)
10578 xfree (copied_name);
c6da4cef
DE
10579
10580 if (file_is_pst)
10581 return NULL;
10582 return include_name;
10583}
10584
c91513d8
PP
10585/* Ignore this record_line request. */
10586
10587static void
10588noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10589{
10590 return;
10591}
10592
aaa75496
JB
10593/* Decode the Line Number Program (LNP) for the given line_header
10594 structure and CU. The actual information extracted and the type
10595 of structures created from the LNP depends on the value of PST.
10596
10597 1. If PST is NULL, then this procedure uses the data from the program
10598 to create all necessary symbol tables, and their linetables.
6e70227d 10599
aaa75496
JB
10600 2. If PST is not NULL, this procedure reads the program to determine
10601 the list of files included by the unit represented by PST, and
c6da4cef
DE
10602 builds all the associated partial symbol tables.
10603
10604 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10605 It is used for relative paths in the line table.
10606 NOTE: When processing partial symtabs (pst != NULL),
10607 comp_dir == pst->dirname.
10608
10609 NOTE: It is important that psymtabs have the same file name (via strcmp)
10610 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10611 symtab we don't use it in the name of the psymtabs we create.
10612 E.g. expand_line_sal requires this when finding psymtabs to expand.
10613 A good testcase for this is mb-inline.exp. */
debd256d 10614
c906108c 10615static void
72b9f47f 10616dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10617 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10618{
a8c50c1f 10619 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10620 gdb_byte *line_end;
a8c50c1f 10621 unsigned int bytes_read, extended_len;
c906108c 10622 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10623 CORE_ADDR baseaddr;
10624 struct objfile *objfile = cu->objfile;
fbf65064 10625 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10626 const int decode_for_pst_p = (pst != NULL);
cb1df416 10627 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
10628 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10629 = record_line;
e142c38c
DJ
10630
10631 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10632
debd256d
JB
10633 line_ptr = lh->statement_program_start;
10634 line_end = lh->statement_program_end;
c906108c
SS
10635
10636 /* Read the statement sequences until there's nothing left. */
10637 while (line_ptr < line_end)
10638 {
10639 /* state machine registers */
10640 CORE_ADDR address = 0;
10641 unsigned int file = 1;
10642 unsigned int line = 1;
10643 unsigned int column = 0;
debd256d 10644 int is_stmt = lh->default_is_stmt;
c906108c
SS
10645 int basic_block = 0;
10646 int end_sequence = 0;
fbf65064 10647 CORE_ADDR addr;
2dc7f7b3 10648 unsigned char op_index = 0;
c906108c 10649
aaa75496 10650 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10651 {
aaa75496 10652 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10653 /* lh->include_dirs and lh->file_names are 0-based, but the
10654 directory and file name numbers in the statement program
10655 are 1-based. */
10656 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10657 char *dir = NULL;
a738430d 10658
debd256d
JB
10659 if (fe->dir_index)
10660 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10661
10662 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10663 }
10664
a738430d 10665 /* Decode the table. */
c5aa993b 10666 while (!end_sequence)
c906108c
SS
10667 {
10668 op_code = read_1_byte (abfd, line_ptr);
10669 line_ptr += 1;
59205f5a
JB
10670 if (line_ptr > line_end)
10671 {
10672 dwarf2_debug_line_missing_end_sequence_complaint ();
10673 break;
10674 }
9aa1fe7e 10675
debd256d 10676 if (op_code >= lh->opcode_base)
6e70227d 10677 {
a738430d 10678 /* Special operand. */
debd256d 10679 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10680 address += (((op_index + (adj_opcode / lh->line_range))
10681 / lh->maximum_ops_per_instruction)
10682 * lh->minimum_instruction_length);
10683 op_index = ((op_index + (adj_opcode / lh->line_range))
10684 % lh->maximum_ops_per_instruction);
debd256d 10685 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10686 if (lh->num_file_names < file || file == 0)
25e43795 10687 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10688 /* For now we ignore lines not starting on an
10689 instruction boundary. */
10690 else if (op_index == 0)
25e43795
DJ
10691 {
10692 lh->file_names[file - 1].included_p = 1;
ca5f395d 10693 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10694 {
10695 if (last_subfile != current_subfile)
10696 {
10697 addr = gdbarch_addr_bits_remove (gdbarch, address);
10698 if (last_subfile)
c91513d8 10699 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10700 last_subfile = current_subfile;
10701 }
25e43795 10702 /* Append row to matrix using current values. */
fbf65064
UW
10703 addr = check_cu_functions (address, cu);
10704 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10705 (*p_record_line) (current_subfile, line, addr);
366da635 10706 }
25e43795 10707 }
ca5f395d 10708 basic_block = 0;
9aa1fe7e
GK
10709 }
10710 else switch (op_code)
c906108c
SS
10711 {
10712 case DW_LNS_extended_op:
3e43a32a
MS
10713 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10714 &bytes_read);
473b7be6 10715 line_ptr += bytes_read;
a8c50c1f 10716 extended_end = line_ptr + extended_len;
c906108c
SS
10717 extended_op = read_1_byte (abfd, line_ptr);
10718 line_ptr += 1;
10719 switch (extended_op)
10720 {
10721 case DW_LNE_end_sequence:
c91513d8 10722 p_record_line = record_line;
c906108c 10723 end_sequence = 1;
c906108c
SS
10724 break;
10725 case DW_LNE_set_address:
e7c27a73 10726 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
10727
10728 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10729 {
10730 /* This line table is for a function which has been
10731 GCd by the linker. Ignore it. PR gdb/12528 */
10732
10733 long line_offset
10734 = line_ptr - dwarf2_per_objfile->line.buffer;
10735
10736 complaint (&symfile_complaints,
10737 _(".debug_line address at offset 0x%lx is 0 "
10738 "[in module %s]"),
10739 line_offset, cu->objfile->name);
10740 p_record_line = noop_record_line;
10741 }
10742
2dc7f7b3 10743 op_index = 0;
107d2387
AC
10744 line_ptr += bytes_read;
10745 address += baseaddr;
c906108c
SS
10746 break;
10747 case DW_LNE_define_file:
debd256d
JB
10748 {
10749 char *cur_file;
10750 unsigned int dir_index, mod_time, length;
6e70227d 10751
3e43a32a
MS
10752 cur_file = read_direct_string (abfd, line_ptr,
10753 &bytes_read);
debd256d
JB
10754 line_ptr += bytes_read;
10755 dir_index =
10756 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10757 line_ptr += bytes_read;
10758 mod_time =
10759 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10760 line_ptr += bytes_read;
10761 length =
10762 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10763 line_ptr += bytes_read;
10764 add_file_name (lh, cur_file, dir_index, mod_time, length);
10765 }
c906108c 10766 break;
d0c6ba3d
CC
10767 case DW_LNE_set_discriminator:
10768 /* The discriminator is not interesting to the debugger;
10769 just ignore it. */
10770 line_ptr = extended_end;
10771 break;
c906108c 10772 default:
4d3c2250 10773 complaint (&symfile_complaints,
e2e0b3e5 10774 _("mangled .debug_line section"));
debd256d 10775 return;
c906108c 10776 }
a8c50c1f
DJ
10777 /* Make sure that we parsed the extended op correctly. If e.g.
10778 we expected a different address size than the producer used,
10779 we may have read the wrong number of bytes. */
10780 if (line_ptr != extended_end)
10781 {
10782 complaint (&symfile_complaints,
10783 _("mangled .debug_line section"));
10784 return;
10785 }
c906108c
SS
10786 break;
10787 case DW_LNS_copy:
59205f5a 10788 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10789 dwarf2_debug_line_missing_file_complaint ();
10790 else
366da635 10791 {
25e43795 10792 lh->file_names[file - 1].included_p = 1;
ca5f395d 10793 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10794 {
10795 if (last_subfile != current_subfile)
10796 {
10797 addr = gdbarch_addr_bits_remove (gdbarch, address);
10798 if (last_subfile)
c91513d8 10799 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10800 last_subfile = current_subfile;
10801 }
10802 addr = check_cu_functions (address, cu);
10803 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10804 (*p_record_line) (current_subfile, line, addr);
fbf65064 10805 }
366da635 10806 }
c906108c
SS
10807 basic_block = 0;
10808 break;
10809 case DW_LNS_advance_pc:
2dc7f7b3
TT
10810 {
10811 CORE_ADDR adjust
10812 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10813
10814 address += (((op_index + adjust)
10815 / lh->maximum_ops_per_instruction)
10816 * lh->minimum_instruction_length);
10817 op_index = ((op_index + adjust)
10818 % lh->maximum_ops_per_instruction);
10819 line_ptr += bytes_read;
10820 }
c906108c
SS
10821 break;
10822 case DW_LNS_advance_line:
10823 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10824 line_ptr += bytes_read;
10825 break;
10826 case DW_LNS_set_file:
debd256d 10827 {
a738430d
MK
10828 /* The arrays lh->include_dirs and lh->file_names are
10829 0-based, but the directory and file name numbers in
10830 the statement program are 1-based. */
debd256d 10831 struct file_entry *fe;
4f1520fb 10832 char *dir = NULL;
a738430d 10833
debd256d
JB
10834 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10835 line_ptr += bytes_read;
59205f5a 10836 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10837 dwarf2_debug_line_missing_file_complaint ();
10838 else
10839 {
10840 fe = &lh->file_names[file - 1];
10841 if (fe->dir_index)
10842 dir = lh->include_dirs[fe->dir_index - 1];
10843 if (!decode_for_pst_p)
10844 {
10845 last_subfile = current_subfile;
10846 dwarf2_start_subfile (fe->name, dir, comp_dir);
10847 }
10848 }
debd256d 10849 }
c906108c
SS
10850 break;
10851 case DW_LNS_set_column:
10852 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10853 line_ptr += bytes_read;
10854 break;
10855 case DW_LNS_negate_stmt:
10856 is_stmt = (!is_stmt);
10857 break;
10858 case DW_LNS_set_basic_block:
10859 basic_block = 1;
10860 break;
c2c6d25f
JM
10861 /* Add to the address register of the state machine the
10862 address increment value corresponding to special opcode
a738430d
MK
10863 255. I.e., this value is scaled by the minimum
10864 instruction length since special opcode 255 would have
b021a221 10865 scaled the increment. */
c906108c 10866 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10867 {
10868 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10869
10870 address += (((op_index + adjust)
10871 / lh->maximum_ops_per_instruction)
10872 * lh->minimum_instruction_length);
10873 op_index = ((op_index + adjust)
10874 % lh->maximum_ops_per_instruction);
10875 }
c906108c
SS
10876 break;
10877 case DW_LNS_fixed_advance_pc:
10878 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10879 op_index = 0;
c906108c
SS
10880 line_ptr += 2;
10881 break;
9aa1fe7e 10882 default:
a738430d
MK
10883 {
10884 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10885 int i;
a738430d 10886
debd256d 10887 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10888 {
10889 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10890 line_ptr += bytes_read;
10891 }
10892 }
c906108c
SS
10893 }
10894 }
59205f5a
JB
10895 if (lh->num_file_names < file || file == 0)
10896 dwarf2_debug_line_missing_file_complaint ();
10897 else
10898 {
10899 lh->file_names[file - 1].included_p = 1;
10900 if (!decode_for_pst_p)
fbf65064
UW
10901 {
10902 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 10903 (*p_record_line) (current_subfile, 0, addr);
fbf65064 10904 }
59205f5a 10905 }
c906108c 10906 }
aaa75496
JB
10907
10908 if (decode_for_pst_p)
10909 {
10910 int file_index;
10911
10912 /* Now that we're done scanning the Line Header Program, we can
10913 create the psymtab of each included file. */
10914 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10915 if (lh->file_names[file_index].included_p == 1)
10916 {
c6da4cef
DE
10917 char *include_name =
10918 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10919 if (include_name != NULL)
aaa75496
JB
10920 dwarf2_create_include_psymtab (include_name, pst, objfile);
10921 }
10922 }
cb1df416
DJ
10923 else
10924 {
10925 /* Make sure a symtab is created for every file, even files
10926 which contain only variables (i.e. no code with associated
10927 line numbers). */
10928
10929 int i;
10930 struct file_entry *fe;
10931
10932 for (i = 0; i < lh->num_file_names; i++)
10933 {
10934 char *dir = NULL;
9a619af0 10935
cb1df416
DJ
10936 fe = &lh->file_names[i];
10937 if (fe->dir_index)
10938 dir = lh->include_dirs[fe->dir_index - 1];
10939 dwarf2_start_subfile (fe->name, dir, comp_dir);
10940
10941 /* Skip the main file; we don't need it, and it must be
10942 allocated last, so that it will show up before the
10943 non-primary symtabs in the objfile's symtab list. */
10944 if (current_subfile == first_subfile)
10945 continue;
10946
10947 if (current_subfile->symtab == NULL)
10948 current_subfile->symtab = allocate_symtab (current_subfile->name,
10949 cu->objfile);
10950 fe->symtab = current_subfile->symtab;
10951 }
10952 }
c906108c
SS
10953}
10954
10955/* Start a subfile for DWARF. FILENAME is the name of the file and
10956 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10957 or NULL if not known. COMP_DIR is the compilation directory for the
10958 linetable's compilation unit or NULL if not known.
c906108c
SS
10959 This routine tries to keep line numbers from identical absolute and
10960 relative file names in a common subfile.
10961
10962 Using the `list' example from the GDB testsuite, which resides in
10963 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10964 of /srcdir/list0.c yields the following debugging information for list0.c:
10965
c5aa993b
JM
10966 DW_AT_name: /srcdir/list0.c
10967 DW_AT_comp_dir: /compdir
357e46e7 10968 files.files[0].name: list0.h
c5aa993b 10969 files.files[0].dir: /srcdir
357e46e7 10970 files.files[1].name: list0.c
c5aa993b 10971 files.files[1].dir: /srcdir
c906108c
SS
10972
10973 The line number information for list0.c has to end up in a single
4f1520fb
FR
10974 subfile, so that `break /srcdir/list0.c:1' works as expected.
10975 start_subfile will ensure that this happens provided that we pass the
10976 concatenation of files.files[1].dir and files.files[1].name as the
10977 subfile's name. */
c906108c
SS
10978
10979static void
3e43a32a
MS
10980dwarf2_start_subfile (char *filename, const char *dirname,
10981 const char *comp_dir)
c906108c 10982{
4f1520fb
FR
10983 char *fullname;
10984
10985 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10986 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10987 second argument to start_subfile. To be consistent, we do the
10988 same here. In order not to lose the line information directory,
10989 we concatenate it to the filename when it makes sense.
10990 Note that the Dwarf3 standard says (speaking of filenames in line
10991 information): ``The directory index is ignored for file names
10992 that represent full path names''. Thus ignoring dirname in the
10993 `else' branch below isn't an issue. */
c906108c 10994
d5166ae1 10995 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10996 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10997 else
10998 fullname = filename;
c906108c 10999
4f1520fb
FR
11000 start_subfile (fullname, comp_dir);
11001
11002 if (fullname != filename)
11003 xfree (fullname);
c906108c
SS
11004}
11005
4c2df51b
DJ
11006static void
11007var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11008 struct dwarf2_cu *cu)
4c2df51b 11009{
e7c27a73
DJ
11010 struct objfile *objfile = cu->objfile;
11011 struct comp_unit_head *cu_header = &cu->header;
11012
4c2df51b
DJ
11013 /* NOTE drow/2003-01-30: There used to be a comment and some special
11014 code here to turn a symbol with DW_AT_external and a
11015 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11016 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11017 with some versions of binutils) where shared libraries could have
11018 relocations against symbols in their debug information - the
11019 minimal symbol would have the right address, but the debug info
11020 would not. It's no longer necessary, because we will explicitly
11021 apply relocations when we read in the debug information now. */
11022
11023 /* A DW_AT_location attribute with no contents indicates that a
11024 variable has been optimized away. */
11025 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11026 {
11027 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11028 return;
11029 }
11030
11031 /* Handle one degenerate form of location expression specially, to
11032 preserve GDB's previous behavior when section offsets are
11033 specified. If this is just a DW_OP_addr then mark this symbol
11034 as LOC_STATIC. */
11035
11036 if (attr_form_is_block (attr)
11037 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11038 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11039 {
891d2f0b 11040 unsigned int dummy;
4c2df51b
DJ
11041
11042 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11043 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11044 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11045 fixup_symbol_section (sym, objfile);
11046 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11047 SYMBOL_SECTION (sym));
4c2df51b
DJ
11048 return;
11049 }
11050
11051 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11052 expression evaluator, and use LOC_COMPUTED only when necessary
11053 (i.e. when the value of a register or memory location is
11054 referenced, or a thread-local block, etc.). Then again, it might
11055 not be worthwhile. I'm assuming that it isn't unless performance
11056 or memory numbers show me otherwise. */
11057
e7c27a73 11058 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11059 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11060
11061 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11062 cu->has_loclist = 1;
4c2df51b
DJ
11063}
11064
c906108c
SS
11065/* Given a pointer to a DWARF information entry, figure out if we need
11066 to make a symbol table entry for it, and if so, create a new entry
11067 and return a pointer to it.
11068 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11069 used the passed type.
11070 If SPACE is not NULL, use it to hold the new symbol. If it is
11071 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11072
11073static struct symbol *
34eaf542
TT
11074new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11075 struct symbol *space)
c906108c 11076{
e7c27a73 11077 struct objfile *objfile = cu->objfile;
c906108c
SS
11078 struct symbol *sym = NULL;
11079 char *name;
11080 struct attribute *attr = NULL;
11081 struct attribute *attr2 = NULL;
e142c38c 11082 CORE_ADDR baseaddr;
e37fd15a
SW
11083 struct pending **list_to_add = NULL;
11084
edb3359d 11085 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11086
11087 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11088
94af9270 11089 name = dwarf2_name (die, cu);
c906108c
SS
11090 if (name)
11091 {
94af9270 11092 const char *linkagename;
34eaf542 11093 int suppress_add = 0;
94af9270 11094
34eaf542
TT
11095 if (space)
11096 sym = space;
11097 else
11098 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11099 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11100
11101 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11102 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11103 linkagename = dwarf2_physname (name, die, cu);
11104 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11105
f55ee35c
JK
11106 /* Fortran does not have mangling standard and the mangling does differ
11107 between gfortran, iFort etc. */
11108 if (cu->language == language_fortran
b250c185 11109 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11110 symbol_set_demangled_name (&(sym->ginfo),
11111 (char *) dwarf2_full_name (name, die, cu),
11112 NULL);
f55ee35c 11113
c906108c 11114 /* Default assumptions.
c5aa993b 11115 Use the passed type or decode it from the die. */
176620f1 11116 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11117 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11118 if (type != NULL)
11119 SYMBOL_TYPE (sym) = type;
11120 else
e7c27a73 11121 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11122 attr = dwarf2_attr (die,
11123 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11124 cu);
c906108c
SS
11125 if (attr)
11126 {
11127 SYMBOL_LINE (sym) = DW_UNSND (attr);
11128 }
cb1df416 11129
edb3359d
DJ
11130 attr = dwarf2_attr (die,
11131 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11132 cu);
cb1df416
DJ
11133 if (attr)
11134 {
11135 int file_index = DW_UNSND (attr);
9a619af0 11136
cb1df416
DJ
11137 if (cu->line_header == NULL
11138 || file_index > cu->line_header->num_file_names)
11139 complaint (&symfile_complaints,
11140 _("file index out of range"));
1c3d648d 11141 else if (file_index > 0)
cb1df416
DJ
11142 {
11143 struct file_entry *fe;
9a619af0 11144
cb1df416
DJ
11145 fe = &cu->line_header->file_names[file_index - 1];
11146 SYMBOL_SYMTAB (sym) = fe->symtab;
11147 }
11148 }
11149
c906108c
SS
11150 switch (die->tag)
11151 {
11152 case DW_TAG_label:
e142c38c 11153 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11154 if (attr)
11155 {
11156 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11157 }
0f5238ed
TT
11158 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11159 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11160 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11161 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11162 break;
11163 case DW_TAG_subprogram:
11164 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11165 finish_block. */
11166 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11167 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11168 if ((attr2 && (DW_UNSND (attr2) != 0))
11169 || cu->language == language_ada)
c906108c 11170 {
2cfa0c8d
JB
11171 /* Subprograms marked external are stored as a global symbol.
11172 Ada subprograms, whether marked external or not, are always
11173 stored as a global symbol, because we want to be able to
11174 access them globally. For instance, we want to be able
11175 to break on a nested subprogram without having to
11176 specify the context. */
e37fd15a 11177 list_to_add = &global_symbols;
c906108c
SS
11178 }
11179 else
11180 {
e37fd15a 11181 list_to_add = cu->list_in_scope;
c906108c
SS
11182 }
11183 break;
edb3359d
DJ
11184 case DW_TAG_inlined_subroutine:
11185 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11186 finish_block. */
11187 SYMBOL_CLASS (sym) = LOC_BLOCK;
11188 SYMBOL_INLINED (sym) = 1;
11189 /* Do not add the symbol to any lists. It will be found via
11190 BLOCK_FUNCTION from the blockvector. */
11191 break;
34eaf542
TT
11192 case DW_TAG_template_value_param:
11193 suppress_add = 1;
11194 /* Fall through. */
72929c62 11195 case DW_TAG_constant:
c906108c 11196 case DW_TAG_variable:
254e6b9e 11197 case DW_TAG_member:
0963b4bd
MS
11198 /* Compilation with minimal debug info may result in
11199 variables with missing type entries. Change the
11200 misleading `void' type to something sensible. */
c906108c 11201 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11202 SYMBOL_TYPE (sym)
46bf5051 11203 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11204
e142c38c 11205 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11206 /* In the case of DW_TAG_member, we should only be called for
11207 static const members. */
11208 if (die->tag == DW_TAG_member)
11209 {
3863f96c
DE
11210 /* dwarf2_add_field uses die_is_declaration,
11211 so we do the same. */
254e6b9e
DE
11212 gdb_assert (die_is_declaration (die, cu));
11213 gdb_assert (attr);
11214 }
c906108c
SS
11215 if (attr)
11216 {
e7c27a73 11217 dwarf2_const_value (attr, sym, cu);
e142c38c 11218 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11219 if (!suppress_add)
34eaf542
TT
11220 {
11221 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11222 list_to_add = &global_symbols;
34eaf542 11223 else
e37fd15a 11224 list_to_add = cu->list_in_scope;
34eaf542 11225 }
c906108c
SS
11226 break;
11227 }
e142c38c 11228 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11229 if (attr)
11230 {
e7c27a73 11231 var_decode_location (attr, sym, cu);
e142c38c 11232 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11233 if (SYMBOL_CLASS (sym) == LOC_STATIC
11234 && SYMBOL_VALUE_ADDRESS (sym) == 0
11235 && !dwarf2_per_objfile->has_section_at_zero)
11236 {
11237 /* When a static variable is eliminated by the linker,
11238 the corresponding debug information is not stripped
11239 out, but the variable address is set to null;
11240 do not add such variables into symbol table. */
11241 }
11242 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11243 {
f55ee35c
JK
11244 /* Workaround gfortran PR debug/40040 - it uses
11245 DW_AT_location for variables in -fPIC libraries which may
11246 get overriden by other libraries/executable and get
11247 a different address. Resolve it by the minimal symbol
11248 which may come from inferior's executable using copy
11249 relocation. Make this workaround only for gfortran as for
11250 other compilers GDB cannot guess the minimal symbol
11251 Fortran mangling kind. */
11252 if (cu->language == language_fortran && die->parent
11253 && die->parent->tag == DW_TAG_module
11254 && cu->producer
11255 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11256 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11257
1c809c68
TT
11258 /* A variable with DW_AT_external is never static,
11259 but it may be block-scoped. */
11260 list_to_add = (cu->list_in_scope == &file_symbols
11261 ? &global_symbols : cu->list_in_scope);
1c809c68 11262 }
c906108c 11263 else
e37fd15a 11264 list_to_add = cu->list_in_scope;
c906108c
SS
11265 }
11266 else
11267 {
11268 /* We do not know the address of this symbol.
c5aa993b
JM
11269 If it is an external symbol and we have type information
11270 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11271 The address of the variable will then be determined from
11272 the minimal symbol table whenever the variable is
11273 referenced. */
e142c38c 11274 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11275 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11276 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11277 {
0fe7935b
DJ
11278 /* A variable with DW_AT_external is never static, but it
11279 may be block-scoped. */
11280 list_to_add = (cu->list_in_scope == &file_symbols
11281 ? &global_symbols : cu->list_in_scope);
11282
c906108c 11283 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11284 }
442ddf59
JK
11285 else if (!die_is_declaration (die, cu))
11286 {
11287 /* Use the default LOC_OPTIMIZED_OUT class. */
11288 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11289 if (!suppress_add)
11290 list_to_add = cu->list_in_scope;
442ddf59 11291 }
c906108c
SS
11292 }
11293 break;
11294 case DW_TAG_formal_parameter:
edb3359d
DJ
11295 /* If we are inside a function, mark this as an argument. If
11296 not, we might be looking at an argument to an inlined function
11297 when we do not have enough information to show inlined frames;
11298 pretend it's a local variable in that case so that the user can
11299 still see it. */
11300 if (context_stack_depth > 0
11301 && context_stack[context_stack_depth - 1].name != NULL)
11302 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11303 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11304 if (attr)
11305 {
e7c27a73 11306 var_decode_location (attr, sym, cu);
c906108c 11307 }
e142c38c 11308 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11309 if (attr)
11310 {
e7c27a73 11311 dwarf2_const_value (attr, sym, cu);
c906108c 11312 }
f346a30d
PM
11313 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11314 if (attr && DW_UNSND (attr))
11315 {
11316 struct type *ref_type;
11317
11318 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11319 SYMBOL_TYPE (sym) = ref_type;
11320 }
11321
e37fd15a 11322 list_to_add = cu->list_in_scope;
c906108c
SS
11323 break;
11324 case DW_TAG_unspecified_parameters:
11325 /* From varargs functions; gdb doesn't seem to have any
11326 interest in this information, so just ignore it for now.
11327 (FIXME?) */
11328 break;
34eaf542
TT
11329 case DW_TAG_template_type_param:
11330 suppress_add = 1;
11331 /* Fall through. */
c906108c 11332 case DW_TAG_class_type:
680b30c7 11333 case DW_TAG_interface_type:
c906108c
SS
11334 case DW_TAG_structure_type:
11335 case DW_TAG_union_type:
72019c9c 11336 case DW_TAG_set_type:
c906108c
SS
11337 case DW_TAG_enumeration_type:
11338 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11339 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11340
63d06c5c 11341 {
987504bb 11342 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11343 really ever be static objects: otherwise, if you try
11344 to, say, break of a class's method and you're in a file
11345 which doesn't mention that class, it won't work unless
11346 the check for all static symbols in lookup_symbol_aux
11347 saves you. See the OtherFileClass tests in
11348 gdb.c++/namespace.exp. */
11349
e37fd15a 11350 if (!suppress_add)
34eaf542 11351 {
34eaf542
TT
11352 list_to_add = (cu->list_in_scope == &file_symbols
11353 && (cu->language == language_cplus
11354 || cu->language == language_java)
11355 ? &global_symbols : cu->list_in_scope);
63d06c5c 11356
64382290
TT
11357 /* The semantics of C++ state that "struct foo {
11358 ... }" also defines a typedef for "foo". A Java
11359 class declaration also defines a typedef for the
11360 class. */
11361 if (cu->language == language_cplus
11362 || cu->language == language_java
11363 || cu->language == language_ada)
11364 {
11365 /* The symbol's name is already allocated along
11366 with this objfile, so we don't need to
11367 duplicate it for the type. */
11368 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11369 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11370 }
63d06c5c
DC
11371 }
11372 }
c906108c
SS
11373 break;
11374 case DW_TAG_typedef:
63d06c5c
DC
11375 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11376 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11377 list_to_add = cu->list_in_scope;
63d06c5c 11378 break;
c906108c 11379 case DW_TAG_base_type:
a02abb62 11380 case DW_TAG_subrange_type:
c906108c 11381 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11382 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11383 list_to_add = cu->list_in_scope;
c906108c
SS
11384 break;
11385 case DW_TAG_enumerator:
e142c38c 11386 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11387 if (attr)
11388 {
e7c27a73 11389 dwarf2_const_value (attr, sym, cu);
c906108c 11390 }
63d06c5c
DC
11391 {
11392 /* NOTE: carlton/2003-11-10: See comment above in the
11393 DW_TAG_class_type, etc. block. */
11394
e142c38c 11395 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11396 && (cu->language == language_cplus
11397 || cu->language == language_java)
e142c38c 11398 ? &global_symbols : cu->list_in_scope);
63d06c5c 11399 }
c906108c 11400 break;
5c4e30ca
DC
11401 case DW_TAG_namespace:
11402 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11403 list_to_add = &global_symbols;
5c4e30ca 11404 break;
c906108c
SS
11405 default:
11406 /* Not a tag we recognize. Hopefully we aren't processing
11407 trash data, but since we must specifically ignore things
11408 we don't recognize, there is nothing else we should do at
0963b4bd 11409 this point. */
e2e0b3e5 11410 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11411 dwarf_tag_name (die->tag));
c906108c
SS
11412 break;
11413 }
df8a16a1 11414
e37fd15a
SW
11415 if (suppress_add)
11416 {
11417 sym->hash_next = objfile->template_symbols;
11418 objfile->template_symbols = sym;
11419 list_to_add = NULL;
11420 }
11421
11422 if (list_to_add != NULL)
11423 add_symbol_to_list (sym, list_to_add);
11424
df8a16a1
DJ
11425 /* For the benefit of old versions of GCC, check for anonymous
11426 namespaces based on the demangled name. */
11427 if (!processing_has_namespace_info
94af9270 11428 && cu->language == language_cplus)
df8a16a1 11429 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
11430 }
11431 return (sym);
11432}
11433
34eaf542
TT
11434/* A wrapper for new_symbol_full that always allocates a new symbol. */
11435
11436static struct symbol *
11437new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11438{
11439 return new_symbol_full (die, type, cu, NULL);
11440}
11441
98bfdba5
PA
11442/* Given an attr with a DW_FORM_dataN value in host byte order,
11443 zero-extend it as appropriate for the symbol's type. The DWARF
11444 standard (v4) is not entirely clear about the meaning of using
11445 DW_FORM_dataN for a constant with a signed type, where the type is
11446 wider than the data. The conclusion of a discussion on the DWARF
11447 list was that this is unspecified. We choose to always zero-extend
11448 because that is the interpretation long in use by GCC. */
c906108c 11449
98bfdba5
PA
11450static gdb_byte *
11451dwarf2_const_value_data (struct attribute *attr, struct type *type,
11452 const char *name, struct obstack *obstack,
11453 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11454{
e7c27a73 11455 struct objfile *objfile = cu->objfile;
e17a4113
UW
11456 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11457 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11458 LONGEST l = DW_UNSND (attr);
11459
11460 if (bits < sizeof (*value) * 8)
11461 {
11462 l &= ((LONGEST) 1 << bits) - 1;
11463 *value = l;
11464 }
11465 else if (bits == sizeof (*value) * 8)
11466 *value = l;
11467 else
11468 {
11469 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11470 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11471 return bytes;
11472 }
11473
11474 return NULL;
11475}
11476
11477/* Read a constant value from an attribute. Either set *VALUE, or if
11478 the value does not fit in *VALUE, set *BYTES - either already
11479 allocated on the objfile obstack, or newly allocated on OBSTACK,
11480 or, set *BATON, if we translated the constant to a location
11481 expression. */
11482
11483static void
11484dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11485 const char *name, struct obstack *obstack,
11486 struct dwarf2_cu *cu,
11487 long *value, gdb_byte **bytes,
11488 struct dwarf2_locexpr_baton **baton)
11489{
11490 struct objfile *objfile = cu->objfile;
11491 struct comp_unit_head *cu_header = &cu->header;
c906108c 11492 struct dwarf_block *blk;
98bfdba5
PA
11493 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11494 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11495
11496 *value = 0;
11497 *bytes = NULL;
11498 *baton = NULL;
c906108c
SS
11499
11500 switch (attr->form)
11501 {
11502 case DW_FORM_addr:
ac56253d 11503 {
ac56253d
TT
11504 gdb_byte *data;
11505
98bfdba5
PA
11506 if (TYPE_LENGTH (type) != cu_header->addr_size)
11507 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11508 cu_header->addr_size,
98bfdba5 11509 TYPE_LENGTH (type));
ac56253d
TT
11510 /* Symbols of this form are reasonably rare, so we just
11511 piggyback on the existing location code rather than writing
11512 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11513 *baton = obstack_alloc (&objfile->objfile_obstack,
11514 sizeof (struct dwarf2_locexpr_baton));
11515 (*baton)->per_cu = cu->per_cu;
11516 gdb_assert ((*baton)->per_cu);
ac56253d 11517
98bfdba5
PA
11518 (*baton)->size = 2 + cu_header->addr_size;
11519 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11520 (*baton)->data = data;
ac56253d
TT
11521
11522 data[0] = DW_OP_addr;
11523 store_unsigned_integer (&data[1], cu_header->addr_size,
11524 byte_order, DW_ADDR (attr));
11525 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11526 }
c906108c 11527 break;
4ac36638 11528 case DW_FORM_string:
93b5768b 11529 case DW_FORM_strp:
98bfdba5
PA
11530 /* DW_STRING is already allocated on the objfile obstack, point
11531 directly to it. */
11532 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11533 break;
c906108c
SS
11534 case DW_FORM_block1:
11535 case DW_FORM_block2:
11536 case DW_FORM_block4:
11537 case DW_FORM_block:
2dc7f7b3 11538 case DW_FORM_exprloc:
c906108c 11539 blk = DW_BLOCK (attr);
98bfdba5
PA
11540 if (TYPE_LENGTH (type) != blk->size)
11541 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11542 TYPE_LENGTH (type));
11543 *bytes = blk->data;
c906108c 11544 break;
2df3850c
JM
11545
11546 /* The DW_AT_const_value attributes are supposed to carry the
11547 symbol's value "represented as it would be on the target
11548 architecture." By the time we get here, it's already been
11549 converted to host endianness, so we just need to sign- or
11550 zero-extend it as appropriate. */
11551 case DW_FORM_data1:
3e43a32a
MS
11552 *bytes = dwarf2_const_value_data (attr, type, name,
11553 obstack, cu, value, 8);
2df3850c 11554 break;
c906108c 11555 case DW_FORM_data2:
3e43a32a
MS
11556 *bytes = dwarf2_const_value_data (attr, type, name,
11557 obstack, cu, value, 16);
2df3850c 11558 break;
c906108c 11559 case DW_FORM_data4:
3e43a32a
MS
11560 *bytes = dwarf2_const_value_data (attr, type, name,
11561 obstack, cu, value, 32);
2df3850c 11562 break;
c906108c 11563 case DW_FORM_data8:
3e43a32a
MS
11564 *bytes = dwarf2_const_value_data (attr, type, name,
11565 obstack, cu, value, 64);
2df3850c
JM
11566 break;
11567
c906108c 11568 case DW_FORM_sdata:
98bfdba5 11569 *value = DW_SND (attr);
2df3850c
JM
11570 break;
11571
c906108c 11572 case DW_FORM_udata:
98bfdba5 11573 *value = DW_UNSND (attr);
c906108c 11574 break;
2df3850c 11575
c906108c 11576 default:
4d3c2250 11577 complaint (&symfile_complaints,
e2e0b3e5 11578 _("unsupported const value attribute form: '%s'"),
4d3c2250 11579 dwarf_form_name (attr->form));
98bfdba5 11580 *value = 0;
c906108c
SS
11581 break;
11582 }
11583}
11584
2df3850c 11585
98bfdba5
PA
11586/* Copy constant value from an attribute to a symbol. */
11587
2df3850c 11588static void
98bfdba5
PA
11589dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11590 struct dwarf2_cu *cu)
2df3850c 11591{
98bfdba5
PA
11592 struct objfile *objfile = cu->objfile;
11593 struct comp_unit_head *cu_header = &cu->header;
11594 long value;
11595 gdb_byte *bytes;
11596 struct dwarf2_locexpr_baton *baton;
2df3850c 11597
98bfdba5
PA
11598 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11599 SYMBOL_PRINT_NAME (sym),
11600 &objfile->objfile_obstack, cu,
11601 &value, &bytes, &baton);
2df3850c 11602
98bfdba5
PA
11603 if (baton != NULL)
11604 {
11605 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11606 SYMBOL_LOCATION_BATON (sym) = baton;
11607 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11608 }
11609 else if (bytes != NULL)
11610 {
11611 SYMBOL_VALUE_BYTES (sym) = bytes;
11612 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11613 }
11614 else
11615 {
11616 SYMBOL_VALUE (sym) = value;
11617 SYMBOL_CLASS (sym) = LOC_CONST;
11618 }
2df3850c
JM
11619}
11620
c906108c
SS
11621/* Return the type of the die in question using its DW_AT_type attribute. */
11622
11623static struct type *
e7c27a73 11624die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11625{
c906108c 11626 struct attribute *type_attr;
c906108c 11627
e142c38c 11628 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11629 if (!type_attr)
11630 {
11631 /* A missing DW_AT_type represents a void type. */
46bf5051 11632 return objfile_type (cu->objfile)->builtin_void;
c906108c 11633 }
348e048f 11634
673bfd45 11635 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11636}
11637
b4ba55a1
JB
11638/* True iff CU's producer generates GNAT Ada auxiliary information
11639 that allows to find parallel types through that information instead
11640 of having to do expensive parallel lookups by type name. */
11641
11642static int
11643need_gnat_info (struct dwarf2_cu *cu)
11644{
11645 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11646 of GNAT produces this auxiliary information, without any indication
11647 that it is produced. Part of enhancing the FSF version of GNAT
11648 to produce that information will be to put in place an indicator
11649 that we can use in order to determine whether the descriptive type
11650 info is available or not. One suggestion that has been made is
11651 to use a new attribute, attached to the CU die. For now, assume
11652 that the descriptive type info is not available. */
11653 return 0;
11654}
11655
b4ba55a1
JB
11656/* Return the auxiliary type of the die in question using its
11657 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11658 attribute is not present. */
11659
11660static struct type *
11661die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11662{
b4ba55a1 11663 struct attribute *type_attr;
b4ba55a1
JB
11664
11665 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11666 if (!type_attr)
11667 return NULL;
11668
673bfd45 11669 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11670}
11671
11672/* If DIE has a descriptive_type attribute, then set the TYPE's
11673 descriptive type accordingly. */
11674
11675static void
11676set_descriptive_type (struct type *type, struct die_info *die,
11677 struct dwarf2_cu *cu)
11678{
11679 struct type *descriptive_type = die_descriptive_type (die, cu);
11680
11681 if (descriptive_type)
11682 {
11683 ALLOCATE_GNAT_AUX_TYPE (type);
11684 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11685 }
11686}
11687
c906108c
SS
11688/* Return the containing type of the die in question using its
11689 DW_AT_containing_type attribute. */
11690
11691static struct type *
e7c27a73 11692die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11693{
c906108c 11694 struct attribute *type_attr;
c906108c 11695
e142c38c 11696 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11697 if (!type_attr)
11698 error (_("Dwarf Error: Problem turning containing type into gdb type "
11699 "[in module %s]"), cu->objfile->name);
11700
673bfd45 11701 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11702}
11703
673bfd45
DE
11704/* Look up the type of DIE in CU using its type attribute ATTR.
11705 If there is no type substitute an error marker. */
11706
c906108c 11707static struct type *
673bfd45
DE
11708lookup_die_type (struct die_info *die, struct attribute *attr,
11709 struct dwarf2_cu *cu)
c906108c 11710{
f792889a
DJ
11711 struct type *this_type;
11712
673bfd45
DE
11713 /* First see if we have it cached. */
11714
11715 if (is_ref_attr (attr))
11716 {
11717 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11718
11719 this_type = get_die_type_at_offset (offset, cu->per_cu);
11720 }
55f1336d 11721 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
11722 {
11723 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11724 struct dwarf2_cu *sig_cu;
11725 unsigned int offset;
11726
11727 /* sig_type will be NULL if the signatured type is missing from
11728 the debug info. */
11729 if (sig_type == NULL)
11730 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11731 "at 0x%x [in module %s]"),
11732 die->offset, cu->objfile->name);
11733
11734 gdb_assert (sig_type->per_cu.from_debug_types);
b3c8eb43 11735 offset = sig_type->per_cu.offset + sig_type->type_offset;
673bfd45
DE
11736 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11737 }
11738 else
11739 {
11740 dump_die_for_error (die);
11741 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11742 dwarf_attr_name (attr->name), cu->objfile->name);
11743 }
11744
11745 /* If not cached we need to read it in. */
11746
11747 if (this_type == NULL)
11748 {
11749 struct die_info *type_die;
11750 struct dwarf2_cu *type_cu = cu;
11751
11752 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11753 /* If the type is cached, we should have found it above. */
11754 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11755 this_type = read_type_die_1 (type_die, type_cu);
11756 }
11757
11758 /* If we still don't have a type use an error marker. */
11759
11760 if (this_type == NULL)
c906108c 11761 {
b00fdb78
TT
11762 char *message, *saved;
11763
11764 /* read_type_die already issued a complaint. */
11765 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11766 cu->objfile->name,
11767 cu->header.offset,
11768 die->offset);
11769 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11770 message, strlen (message));
11771 xfree (message);
11772
11773 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11774 }
673bfd45 11775
f792889a 11776 return this_type;
c906108c
SS
11777}
11778
673bfd45
DE
11779/* Return the type in DIE, CU.
11780 Returns NULL for invalid types.
11781
11782 This first does a lookup in the appropriate type_hash table,
11783 and only reads the die in if necessary.
11784
11785 NOTE: This can be called when reading in partial or full symbols. */
11786
f792889a 11787static struct type *
e7c27a73 11788read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11789{
f792889a
DJ
11790 struct type *this_type;
11791
11792 this_type = get_die_type (die, cu);
11793 if (this_type)
11794 return this_type;
11795
673bfd45
DE
11796 return read_type_die_1 (die, cu);
11797}
11798
11799/* Read the type in DIE, CU.
11800 Returns NULL for invalid types. */
11801
11802static struct type *
11803read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11804{
11805 struct type *this_type = NULL;
11806
c906108c
SS
11807 switch (die->tag)
11808 {
11809 case DW_TAG_class_type:
680b30c7 11810 case DW_TAG_interface_type:
c906108c
SS
11811 case DW_TAG_structure_type:
11812 case DW_TAG_union_type:
f792889a 11813 this_type = read_structure_type (die, cu);
c906108c
SS
11814 break;
11815 case DW_TAG_enumeration_type:
f792889a 11816 this_type = read_enumeration_type (die, cu);
c906108c
SS
11817 break;
11818 case DW_TAG_subprogram:
11819 case DW_TAG_subroutine_type:
edb3359d 11820 case DW_TAG_inlined_subroutine:
f792889a 11821 this_type = read_subroutine_type (die, cu);
c906108c
SS
11822 break;
11823 case DW_TAG_array_type:
f792889a 11824 this_type = read_array_type (die, cu);
c906108c 11825 break;
72019c9c 11826 case DW_TAG_set_type:
f792889a 11827 this_type = read_set_type (die, cu);
72019c9c 11828 break;
c906108c 11829 case DW_TAG_pointer_type:
f792889a 11830 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11831 break;
11832 case DW_TAG_ptr_to_member_type:
f792889a 11833 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11834 break;
11835 case DW_TAG_reference_type:
f792889a 11836 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11837 break;
11838 case DW_TAG_const_type:
f792889a 11839 this_type = read_tag_const_type (die, cu);
c906108c
SS
11840 break;
11841 case DW_TAG_volatile_type:
f792889a 11842 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11843 break;
11844 case DW_TAG_string_type:
f792889a 11845 this_type = read_tag_string_type (die, cu);
c906108c
SS
11846 break;
11847 case DW_TAG_typedef:
f792889a 11848 this_type = read_typedef (die, cu);
c906108c 11849 break;
a02abb62 11850 case DW_TAG_subrange_type:
f792889a 11851 this_type = read_subrange_type (die, cu);
a02abb62 11852 break;
c906108c 11853 case DW_TAG_base_type:
f792889a 11854 this_type = read_base_type (die, cu);
c906108c 11855 break;
81a17f79 11856 case DW_TAG_unspecified_type:
f792889a 11857 this_type = read_unspecified_type (die, cu);
81a17f79 11858 break;
0114d602
DJ
11859 case DW_TAG_namespace:
11860 this_type = read_namespace_type (die, cu);
11861 break;
f55ee35c
JK
11862 case DW_TAG_module:
11863 this_type = read_module_type (die, cu);
11864 break;
c906108c 11865 default:
3e43a32a
MS
11866 complaint (&symfile_complaints,
11867 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11868 dwarf_tag_name (die->tag));
c906108c
SS
11869 break;
11870 }
63d06c5c 11871
f792889a 11872 return this_type;
63d06c5c
DC
11873}
11874
abc72ce4
DE
11875/* See if we can figure out if the class lives in a namespace. We do
11876 this by looking for a member function; its demangled name will
11877 contain namespace info, if there is any.
11878 Return the computed name or NULL.
11879 Space for the result is allocated on the objfile's obstack.
11880 This is the full-die version of guess_partial_die_structure_name.
11881 In this case we know DIE has no useful parent. */
11882
11883static char *
11884guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11885{
11886 struct die_info *spec_die;
11887 struct dwarf2_cu *spec_cu;
11888 struct die_info *child;
11889
11890 spec_cu = cu;
11891 spec_die = die_specification (die, &spec_cu);
11892 if (spec_die != NULL)
11893 {
11894 die = spec_die;
11895 cu = spec_cu;
11896 }
11897
11898 for (child = die->child;
11899 child != NULL;
11900 child = child->sibling)
11901 {
11902 if (child->tag == DW_TAG_subprogram)
11903 {
11904 struct attribute *attr;
11905
11906 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11907 if (attr == NULL)
11908 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11909 if (attr != NULL)
11910 {
11911 char *actual_name
11912 = language_class_name_from_physname (cu->language_defn,
11913 DW_STRING (attr));
11914 char *name = NULL;
11915
11916 if (actual_name != NULL)
11917 {
11918 char *die_name = dwarf2_name (die, cu);
11919
11920 if (die_name != NULL
11921 && strcmp (die_name, actual_name) != 0)
11922 {
11923 /* Strip off the class name from the full name.
11924 We want the prefix. */
11925 int die_name_len = strlen (die_name);
11926 int actual_name_len = strlen (actual_name);
11927
11928 /* Test for '::' as a sanity check. */
11929 if (actual_name_len > die_name_len + 2
3e43a32a
MS
11930 && actual_name[actual_name_len
11931 - die_name_len - 1] == ':')
abc72ce4
DE
11932 name =
11933 obsavestring (actual_name,
11934 actual_name_len - die_name_len - 2,
11935 &cu->objfile->objfile_obstack);
11936 }
11937 }
11938 xfree (actual_name);
11939 return name;
11940 }
11941 }
11942 }
11943
11944 return NULL;
11945}
11946
fdde2d81 11947/* Return the name of the namespace/class that DIE is defined within,
0114d602 11948 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11949
0114d602
DJ
11950 For example, if we're within the method foo() in the following
11951 code:
11952
11953 namespace N {
11954 class C {
11955 void foo () {
11956 }
11957 };
11958 }
11959
11960 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11961
11962static char *
e142c38c 11963determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11964{
0114d602
DJ
11965 struct die_info *parent, *spec_die;
11966 struct dwarf2_cu *spec_cu;
11967 struct type *parent_type;
63d06c5c 11968
f55ee35c
JK
11969 if (cu->language != language_cplus && cu->language != language_java
11970 && cu->language != language_fortran)
0114d602
DJ
11971 return "";
11972
11973 /* We have to be careful in the presence of DW_AT_specification.
11974 For example, with GCC 3.4, given the code
11975
11976 namespace N {
11977 void foo() {
11978 // Definition of N::foo.
11979 }
11980 }
11981
11982 then we'll have a tree of DIEs like this:
11983
11984 1: DW_TAG_compile_unit
11985 2: DW_TAG_namespace // N
11986 3: DW_TAG_subprogram // declaration of N::foo
11987 4: DW_TAG_subprogram // definition of N::foo
11988 DW_AT_specification // refers to die #3
11989
11990 Thus, when processing die #4, we have to pretend that we're in
11991 the context of its DW_AT_specification, namely the contex of die
11992 #3. */
11993 spec_cu = cu;
11994 spec_die = die_specification (die, &spec_cu);
11995 if (spec_die == NULL)
11996 parent = die->parent;
11997 else
63d06c5c 11998 {
0114d602
DJ
11999 parent = spec_die->parent;
12000 cu = spec_cu;
63d06c5c 12001 }
0114d602
DJ
12002
12003 if (parent == NULL)
12004 return "";
98bfdba5
PA
12005 else if (parent->building_fullname)
12006 {
12007 const char *name;
12008 const char *parent_name;
12009
12010 /* It has been seen on RealView 2.2 built binaries,
12011 DW_TAG_template_type_param types actually _defined_ as
12012 children of the parent class:
12013
12014 enum E {};
12015 template class <class Enum> Class{};
12016 Class<enum E> class_e;
12017
12018 1: DW_TAG_class_type (Class)
12019 2: DW_TAG_enumeration_type (E)
12020 3: DW_TAG_enumerator (enum1:0)
12021 3: DW_TAG_enumerator (enum2:1)
12022 ...
12023 2: DW_TAG_template_type_param
12024 DW_AT_type DW_FORM_ref_udata (E)
12025
12026 Besides being broken debug info, it can put GDB into an
12027 infinite loop. Consider:
12028
12029 When we're building the full name for Class<E>, we'll start
12030 at Class, and go look over its template type parameters,
12031 finding E. We'll then try to build the full name of E, and
12032 reach here. We're now trying to build the full name of E,
12033 and look over the parent DIE for containing scope. In the
12034 broken case, if we followed the parent DIE of E, we'd again
12035 find Class, and once again go look at its template type
12036 arguments, etc., etc. Simply don't consider such parent die
12037 as source-level parent of this die (it can't be, the language
12038 doesn't allow it), and break the loop here. */
12039 name = dwarf2_name (die, cu);
12040 parent_name = dwarf2_name (parent, cu);
12041 complaint (&symfile_complaints,
12042 _("template param type '%s' defined within parent '%s'"),
12043 name ? name : "<unknown>",
12044 parent_name ? parent_name : "<unknown>");
12045 return "";
12046 }
63d06c5c 12047 else
0114d602
DJ
12048 switch (parent->tag)
12049 {
63d06c5c 12050 case DW_TAG_namespace:
0114d602 12051 parent_type = read_type_die (parent, cu);
acebe513
UW
12052 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12053 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12054 Work around this problem here. */
12055 if (cu->language == language_cplus
12056 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12057 return "";
0114d602
DJ
12058 /* We give a name to even anonymous namespaces. */
12059 return TYPE_TAG_NAME (parent_type);
63d06c5c 12060 case DW_TAG_class_type:
680b30c7 12061 case DW_TAG_interface_type:
63d06c5c 12062 case DW_TAG_structure_type:
0114d602 12063 case DW_TAG_union_type:
f55ee35c 12064 case DW_TAG_module:
0114d602
DJ
12065 parent_type = read_type_die (parent, cu);
12066 if (TYPE_TAG_NAME (parent_type) != NULL)
12067 return TYPE_TAG_NAME (parent_type);
12068 else
12069 /* An anonymous structure is only allowed non-static data
12070 members; no typedefs, no member functions, et cetera.
12071 So it does not need a prefix. */
12072 return "";
abc72ce4
DE
12073 case DW_TAG_compile_unit:
12074 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12075 if (cu->language == language_cplus
12076 && dwarf2_per_objfile->types.asection != NULL
12077 && die->child != NULL
12078 && (die->tag == DW_TAG_class_type
12079 || die->tag == DW_TAG_structure_type
12080 || die->tag == DW_TAG_union_type))
12081 {
12082 char *name = guess_full_die_structure_name (die, cu);
12083 if (name != NULL)
12084 return name;
12085 }
12086 return "";
63d06c5c 12087 default:
8176b9b8 12088 return determine_prefix (parent, cu);
63d06c5c 12089 }
63d06c5c
DC
12090}
12091
3e43a32a
MS
12092/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12093 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12094 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12095 an obconcat, otherwise allocate storage for the result. The CU argument is
12096 used to determine the language and hence, the appropriate separator. */
987504bb 12097
f55ee35c 12098#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12099
12100static char *
f55ee35c
JK
12101typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12102 int physname, struct dwarf2_cu *cu)
63d06c5c 12103{
f55ee35c 12104 const char *lead = "";
5c315b68 12105 const char *sep;
63d06c5c 12106
3e43a32a
MS
12107 if (suffix == NULL || suffix[0] == '\0'
12108 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12109 sep = "";
12110 else if (cu->language == language_java)
12111 sep = ".";
f55ee35c
JK
12112 else if (cu->language == language_fortran && physname)
12113 {
12114 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12115 DW_AT_MIPS_linkage_name is preferred and used instead. */
12116
12117 lead = "__";
12118 sep = "_MOD_";
12119 }
987504bb
JJ
12120 else
12121 sep = "::";
63d06c5c 12122
6dd47d34
DE
12123 if (prefix == NULL)
12124 prefix = "";
12125 if (suffix == NULL)
12126 suffix = "";
12127
987504bb
JJ
12128 if (obs == NULL)
12129 {
3e43a32a
MS
12130 char *retval
12131 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12132
f55ee35c
JK
12133 strcpy (retval, lead);
12134 strcat (retval, prefix);
6dd47d34
DE
12135 strcat (retval, sep);
12136 strcat (retval, suffix);
63d06c5c
DC
12137 return retval;
12138 }
987504bb
JJ
12139 else
12140 {
12141 /* We have an obstack. */
f55ee35c 12142 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12143 }
63d06c5c
DC
12144}
12145
c906108c
SS
12146/* Return sibling of die, NULL if no sibling. */
12147
f9aca02d 12148static struct die_info *
fba45db2 12149sibling_die (struct die_info *die)
c906108c 12150{
639d11d3 12151 return die->sibling;
c906108c
SS
12152}
12153
71c25dea
TT
12154/* Get name of a die, return NULL if not found. */
12155
12156static char *
12157dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12158 struct obstack *obstack)
12159{
12160 if (name && cu->language == language_cplus)
12161 {
12162 char *canon_name = cp_canonicalize_string (name);
12163
12164 if (canon_name != NULL)
12165 {
12166 if (strcmp (canon_name, name) != 0)
12167 name = obsavestring (canon_name, strlen (canon_name),
12168 obstack);
12169 xfree (canon_name);
12170 }
12171 }
12172
12173 return name;
c906108c
SS
12174}
12175
9219021c
DC
12176/* Get name of a die, return NULL if not found. */
12177
12178static char *
e142c38c 12179dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12180{
12181 struct attribute *attr;
12182
e142c38c 12183 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12184 if ((!attr || !DW_STRING (attr))
12185 && die->tag != DW_TAG_class_type
12186 && die->tag != DW_TAG_interface_type
12187 && die->tag != DW_TAG_structure_type
12188 && die->tag != DW_TAG_union_type)
71c25dea
TT
12189 return NULL;
12190
12191 switch (die->tag)
12192 {
12193 case DW_TAG_compile_unit:
12194 /* Compilation units have a DW_AT_name that is a filename, not
12195 a source language identifier. */
12196 case DW_TAG_enumeration_type:
12197 case DW_TAG_enumerator:
12198 /* These tags always have simple identifiers already; no need
12199 to canonicalize them. */
12200 return DW_STRING (attr);
907af001 12201
418835cc
KS
12202 case DW_TAG_subprogram:
12203 /* Java constructors will all be named "<init>", so return
12204 the class name when we see this special case. */
12205 if (cu->language == language_java
12206 && DW_STRING (attr) != NULL
12207 && strcmp (DW_STRING (attr), "<init>") == 0)
12208 {
12209 struct dwarf2_cu *spec_cu = cu;
12210 struct die_info *spec_die;
12211
12212 /* GCJ will output '<init>' for Java constructor names.
12213 For this special case, return the name of the parent class. */
12214
12215 /* GCJ may output suprogram DIEs with AT_specification set.
12216 If so, use the name of the specified DIE. */
12217 spec_die = die_specification (die, &spec_cu);
12218 if (spec_die != NULL)
12219 return dwarf2_name (spec_die, spec_cu);
12220
12221 do
12222 {
12223 die = die->parent;
12224 if (die->tag == DW_TAG_class_type)
12225 return dwarf2_name (die, cu);
12226 }
12227 while (die->tag != DW_TAG_compile_unit);
12228 }
907af001
UW
12229 break;
12230
12231 case DW_TAG_class_type:
12232 case DW_TAG_interface_type:
12233 case DW_TAG_structure_type:
12234 case DW_TAG_union_type:
12235 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12236 structures or unions. These were of the form "._%d" in GCC 4.1,
12237 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12238 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12239 if (attr && DW_STRING (attr)
12240 && (strncmp (DW_STRING (attr), "._", 2) == 0
12241 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12242 return NULL;
53832f31
TT
12243
12244 /* GCC might emit a nameless typedef that has a linkage name. See
12245 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12246 if (!attr || DW_STRING (attr) == NULL)
12247 {
df5c6c50 12248 char *demangled = NULL;
53832f31
TT
12249
12250 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12251 if (attr == NULL)
12252 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12253
12254 if (attr == NULL || DW_STRING (attr) == NULL)
12255 return NULL;
12256
df5c6c50
JK
12257 /* Avoid demangling DW_STRING (attr) the second time on a second
12258 call for the same DIE. */
12259 if (!DW_STRING_IS_CANONICAL (attr))
12260 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12261
12262 if (demangled)
12263 {
12264 /* FIXME: we already did this for the partial symbol... */
12265 DW_STRING (attr)
12266 = obsavestring (demangled, strlen (demangled),
12267 &cu->objfile->objfile_obstack);
12268 DW_STRING_IS_CANONICAL (attr) = 1;
12269 xfree (demangled);
12270 }
12271 }
907af001
UW
12272 break;
12273
71c25dea 12274 default:
907af001
UW
12275 break;
12276 }
12277
12278 if (!DW_STRING_IS_CANONICAL (attr))
12279 {
12280 DW_STRING (attr)
12281 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12282 &cu->objfile->objfile_obstack);
12283 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12284 }
907af001 12285 return DW_STRING (attr);
9219021c
DC
12286}
12287
12288/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12289 is none. *EXT_CU is the CU containing DIE on input, and the CU
12290 containing the return value on output. */
9219021c
DC
12291
12292static struct die_info *
f2f0e013 12293dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12294{
12295 struct attribute *attr;
9219021c 12296
f2f0e013 12297 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12298 if (attr == NULL)
12299 return NULL;
12300
f2f0e013 12301 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12302}
12303
c906108c
SS
12304/* Convert a DIE tag into its string name. */
12305
12306static char *
aa1ee363 12307dwarf_tag_name (unsigned tag)
c906108c
SS
12308{
12309 switch (tag)
12310 {
12311 case DW_TAG_padding:
12312 return "DW_TAG_padding";
12313 case DW_TAG_array_type:
12314 return "DW_TAG_array_type";
12315 case DW_TAG_class_type:
12316 return "DW_TAG_class_type";
12317 case DW_TAG_entry_point:
12318 return "DW_TAG_entry_point";
12319 case DW_TAG_enumeration_type:
12320 return "DW_TAG_enumeration_type";
12321 case DW_TAG_formal_parameter:
12322 return "DW_TAG_formal_parameter";
12323 case DW_TAG_imported_declaration:
12324 return "DW_TAG_imported_declaration";
12325 case DW_TAG_label:
12326 return "DW_TAG_label";
12327 case DW_TAG_lexical_block:
12328 return "DW_TAG_lexical_block";
12329 case DW_TAG_member:
12330 return "DW_TAG_member";
12331 case DW_TAG_pointer_type:
12332 return "DW_TAG_pointer_type";
12333 case DW_TAG_reference_type:
12334 return "DW_TAG_reference_type";
12335 case DW_TAG_compile_unit:
12336 return "DW_TAG_compile_unit";
12337 case DW_TAG_string_type:
12338 return "DW_TAG_string_type";
12339 case DW_TAG_structure_type:
12340 return "DW_TAG_structure_type";
12341 case DW_TAG_subroutine_type:
12342 return "DW_TAG_subroutine_type";
12343 case DW_TAG_typedef:
12344 return "DW_TAG_typedef";
12345 case DW_TAG_union_type:
12346 return "DW_TAG_union_type";
12347 case DW_TAG_unspecified_parameters:
12348 return "DW_TAG_unspecified_parameters";
12349 case DW_TAG_variant:
12350 return "DW_TAG_variant";
12351 case DW_TAG_common_block:
12352 return "DW_TAG_common_block";
12353 case DW_TAG_common_inclusion:
12354 return "DW_TAG_common_inclusion";
12355 case DW_TAG_inheritance:
12356 return "DW_TAG_inheritance";
12357 case DW_TAG_inlined_subroutine:
12358 return "DW_TAG_inlined_subroutine";
12359 case DW_TAG_module:
12360 return "DW_TAG_module";
12361 case DW_TAG_ptr_to_member_type:
12362 return "DW_TAG_ptr_to_member_type";
12363 case DW_TAG_set_type:
12364 return "DW_TAG_set_type";
12365 case DW_TAG_subrange_type:
12366 return "DW_TAG_subrange_type";
12367 case DW_TAG_with_stmt:
12368 return "DW_TAG_with_stmt";
12369 case DW_TAG_access_declaration:
12370 return "DW_TAG_access_declaration";
12371 case DW_TAG_base_type:
12372 return "DW_TAG_base_type";
12373 case DW_TAG_catch_block:
12374 return "DW_TAG_catch_block";
12375 case DW_TAG_const_type:
12376 return "DW_TAG_const_type";
12377 case DW_TAG_constant:
12378 return "DW_TAG_constant";
12379 case DW_TAG_enumerator:
12380 return "DW_TAG_enumerator";
12381 case DW_TAG_file_type:
12382 return "DW_TAG_file_type";
12383 case DW_TAG_friend:
12384 return "DW_TAG_friend";
12385 case DW_TAG_namelist:
12386 return "DW_TAG_namelist";
12387 case DW_TAG_namelist_item:
12388 return "DW_TAG_namelist_item";
12389 case DW_TAG_packed_type:
12390 return "DW_TAG_packed_type";
12391 case DW_TAG_subprogram:
12392 return "DW_TAG_subprogram";
12393 case DW_TAG_template_type_param:
12394 return "DW_TAG_template_type_param";
12395 case DW_TAG_template_value_param:
12396 return "DW_TAG_template_value_param";
12397 case DW_TAG_thrown_type:
12398 return "DW_TAG_thrown_type";
12399 case DW_TAG_try_block:
12400 return "DW_TAG_try_block";
12401 case DW_TAG_variant_part:
12402 return "DW_TAG_variant_part";
12403 case DW_TAG_variable:
12404 return "DW_TAG_variable";
12405 case DW_TAG_volatile_type:
12406 return "DW_TAG_volatile_type";
d9fa45fe
DC
12407 case DW_TAG_dwarf_procedure:
12408 return "DW_TAG_dwarf_procedure";
12409 case DW_TAG_restrict_type:
12410 return "DW_TAG_restrict_type";
12411 case DW_TAG_interface_type:
12412 return "DW_TAG_interface_type";
12413 case DW_TAG_namespace:
12414 return "DW_TAG_namespace";
12415 case DW_TAG_imported_module:
12416 return "DW_TAG_imported_module";
12417 case DW_TAG_unspecified_type:
12418 return "DW_TAG_unspecified_type";
12419 case DW_TAG_partial_unit:
12420 return "DW_TAG_partial_unit";
12421 case DW_TAG_imported_unit:
12422 return "DW_TAG_imported_unit";
b7619582
GF
12423 case DW_TAG_condition:
12424 return "DW_TAG_condition";
12425 case DW_TAG_shared_type:
12426 return "DW_TAG_shared_type";
348e048f
DE
12427 case DW_TAG_type_unit:
12428 return "DW_TAG_type_unit";
c906108c
SS
12429 case DW_TAG_MIPS_loop:
12430 return "DW_TAG_MIPS_loop";
b7619582
GF
12431 case DW_TAG_HP_array_descriptor:
12432 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12433 case DW_TAG_format_label:
12434 return "DW_TAG_format_label";
12435 case DW_TAG_function_template:
12436 return "DW_TAG_function_template";
12437 case DW_TAG_class_template:
12438 return "DW_TAG_class_template";
b7619582
GF
12439 case DW_TAG_GNU_BINCL:
12440 return "DW_TAG_GNU_BINCL";
12441 case DW_TAG_GNU_EINCL:
12442 return "DW_TAG_GNU_EINCL";
12443 case DW_TAG_upc_shared_type:
12444 return "DW_TAG_upc_shared_type";
12445 case DW_TAG_upc_strict_type:
12446 return "DW_TAG_upc_strict_type";
12447 case DW_TAG_upc_relaxed_type:
12448 return "DW_TAG_upc_relaxed_type";
12449 case DW_TAG_PGI_kanji_type:
12450 return "DW_TAG_PGI_kanji_type";
12451 case DW_TAG_PGI_interface_block:
12452 return "DW_TAG_PGI_interface_block";
c906108c
SS
12453 default:
12454 return "DW_TAG_<unknown>";
12455 }
12456}
12457
12458/* Convert a DWARF attribute code into its string name. */
12459
12460static char *
aa1ee363 12461dwarf_attr_name (unsigned attr)
c906108c
SS
12462{
12463 switch (attr)
12464 {
12465 case DW_AT_sibling:
12466 return "DW_AT_sibling";
12467 case DW_AT_location:
12468 return "DW_AT_location";
12469 case DW_AT_name:
12470 return "DW_AT_name";
12471 case DW_AT_ordering:
12472 return "DW_AT_ordering";
12473 case DW_AT_subscr_data:
12474 return "DW_AT_subscr_data";
12475 case DW_AT_byte_size:
12476 return "DW_AT_byte_size";
12477 case DW_AT_bit_offset:
12478 return "DW_AT_bit_offset";
12479 case DW_AT_bit_size:
12480 return "DW_AT_bit_size";
12481 case DW_AT_element_list:
12482 return "DW_AT_element_list";
12483 case DW_AT_stmt_list:
12484 return "DW_AT_stmt_list";
12485 case DW_AT_low_pc:
12486 return "DW_AT_low_pc";
12487 case DW_AT_high_pc:
12488 return "DW_AT_high_pc";
12489 case DW_AT_language:
12490 return "DW_AT_language";
12491 case DW_AT_member:
12492 return "DW_AT_member";
12493 case DW_AT_discr:
12494 return "DW_AT_discr";
12495 case DW_AT_discr_value:
12496 return "DW_AT_discr_value";
12497 case DW_AT_visibility:
12498 return "DW_AT_visibility";
12499 case DW_AT_import:
12500 return "DW_AT_import";
12501 case DW_AT_string_length:
12502 return "DW_AT_string_length";
12503 case DW_AT_common_reference:
12504 return "DW_AT_common_reference";
12505 case DW_AT_comp_dir:
12506 return "DW_AT_comp_dir";
12507 case DW_AT_const_value:
12508 return "DW_AT_const_value";
12509 case DW_AT_containing_type:
12510 return "DW_AT_containing_type";
12511 case DW_AT_default_value:
12512 return "DW_AT_default_value";
12513 case DW_AT_inline:
12514 return "DW_AT_inline";
12515 case DW_AT_is_optional:
12516 return "DW_AT_is_optional";
12517 case DW_AT_lower_bound:
12518 return "DW_AT_lower_bound";
12519 case DW_AT_producer:
12520 return "DW_AT_producer";
12521 case DW_AT_prototyped:
12522 return "DW_AT_prototyped";
12523 case DW_AT_return_addr:
12524 return "DW_AT_return_addr";
12525 case DW_AT_start_scope:
12526 return "DW_AT_start_scope";
09fa0d7c
JK
12527 case DW_AT_bit_stride:
12528 return "DW_AT_bit_stride";
c906108c
SS
12529 case DW_AT_upper_bound:
12530 return "DW_AT_upper_bound";
12531 case DW_AT_abstract_origin:
12532 return "DW_AT_abstract_origin";
12533 case DW_AT_accessibility:
12534 return "DW_AT_accessibility";
12535 case DW_AT_address_class:
12536 return "DW_AT_address_class";
12537 case DW_AT_artificial:
12538 return "DW_AT_artificial";
12539 case DW_AT_base_types:
12540 return "DW_AT_base_types";
12541 case DW_AT_calling_convention:
12542 return "DW_AT_calling_convention";
12543 case DW_AT_count:
12544 return "DW_AT_count";
12545 case DW_AT_data_member_location:
12546 return "DW_AT_data_member_location";
12547 case DW_AT_decl_column:
12548 return "DW_AT_decl_column";
12549 case DW_AT_decl_file:
12550 return "DW_AT_decl_file";
12551 case DW_AT_decl_line:
12552 return "DW_AT_decl_line";
12553 case DW_AT_declaration:
12554 return "DW_AT_declaration";
12555 case DW_AT_discr_list:
12556 return "DW_AT_discr_list";
12557 case DW_AT_encoding:
12558 return "DW_AT_encoding";
12559 case DW_AT_external:
12560 return "DW_AT_external";
12561 case DW_AT_frame_base:
12562 return "DW_AT_frame_base";
12563 case DW_AT_friend:
12564 return "DW_AT_friend";
12565 case DW_AT_identifier_case:
12566 return "DW_AT_identifier_case";
12567 case DW_AT_macro_info:
12568 return "DW_AT_macro_info";
12569 case DW_AT_namelist_items:
12570 return "DW_AT_namelist_items";
12571 case DW_AT_priority:
12572 return "DW_AT_priority";
12573 case DW_AT_segment:
12574 return "DW_AT_segment";
12575 case DW_AT_specification:
12576 return "DW_AT_specification";
12577 case DW_AT_static_link:
12578 return "DW_AT_static_link";
12579 case DW_AT_type:
12580 return "DW_AT_type";
12581 case DW_AT_use_location:
12582 return "DW_AT_use_location";
12583 case DW_AT_variable_parameter:
12584 return "DW_AT_variable_parameter";
12585 case DW_AT_virtuality:
12586 return "DW_AT_virtuality";
12587 case DW_AT_vtable_elem_location:
12588 return "DW_AT_vtable_elem_location";
b7619582 12589 /* DWARF 3 values. */
d9fa45fe
DC
12590 case DW_AT_allocated:
12591 return "DW_AT_allocated";
12592 case DW_AT_associated:
12593 return "DW_AT_associated";
12594 case DW_AT_data_location:
12595 return "DW_AT_data_location";
09fa0d7c
JK
12596 case DW_AT_byte_stride:
12597 return "DW_AT_byte_stride";
d9fa45fe
DC
12598 case DW_AT_entry_pc:
12599 return "DW_AT_entry_pc";
12600 case DW_AT_use_UTF8:
12601 return "DW_AT_use_UTF8";
12602 case DW_AT_extension:
12603 return "DW_AT_extension";
12604 case DW_AT_ranges:
12605 return "DW_AT_ranges";
12606 case DW_AT_trampoline:
12607 return "DW_AT_trampoline";
12608 case DW_AT_call_column:
12609 return "DW_AT_call_column";
12610 case DW_AT_call_file:
12611 return "DW_AT_call_file";
12612 case DW_AT_call_line:
12613 return "DW_AT_call_line";
b7619582
GF
12614 case DW_AT_description:
12615 return "DW_AT_description";
12616 case DW_AT_binary_scale:
12617 return "DW_AT_binary_scale";
12618 case DW_AT_decimal_scale:
12619 return "DW_AT_decimal_scale";
12620 case DW_AT_small:
12621 return "DW_AT_small";
12622 case DW_AT_decimal_sign:
12623 return "DW_AT_decimal_sign";
12624 case DW_AT_digit_count:
12625 return "DW_AT_digit_count";
12626 case DW_AT_picture_string:
12627 return "DW_AT_picture_string";
12628 case DW_AT_mutable:
12629 return "DW_AT_mutable";
12630 case DW_AT_threads_scaled:
12631 return "DW_AT_threads_scaled";
12632 case DW_AT_explicit:
12633 return "DW_AT_explicit";
12634 case DW_AT_object_pointer:
12635 return "DW_AT_object_pointer";
12636 case DW_AT_endianity:
12637 return "DW_AT_endianity";
12638 case DW_AT_elemental:
12639 return "DW_AT_elemental";
12640 case DW_AT_pure:
12641 return "DW_AT_pure";
12642 case DW_AT_recursive:
12643 return "DW_AT_recursive";
348e048f
DE
12644 /* DWARF 4 values. */
12645 case DW_AT_signature:
12646 return "DW_AT_signature";
31ef98ae
TT
12647 case DW_AT_linkage_name:
12648 return "DW_AT_linkage_name";
b7619582 12649 /* SGI/MIPS extensions. */
c764a876 12650#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12651 case DW_AT_MIPS_fde:
12652 return "DW_AT_MIPS_fde";
c764a876 12653#endif
c906108c
SS
12654 case DW_AT_MIPS_loop_begin:
12655 return "DW_AT_MIPS_loop_begin";
12656 case DW_AT_MIPS_tail_loop_begin:
12657 return "DW_AT_MIPS_tail_loop_begin";
12658 case DW_AT_MIPS_epilog_begin:
12659 return "DW_AT_MIPS_epilog_begin";
12660 case DW_AT_MIPS_loop_unroll_factor:
12661 return "DW_AT_MIPS_loop_unroll_factor";
12662 case DW_AT_MIPS_software_pipeline_depth:
12663 return "DW_AT_MIPS_software_pipeline_depth";
12664 case DW_AT_MIPS_linkage_name:
12665 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12666 case DW_AT_MIPS_stride:
12667 return "DW_AT_MIPS_stride";
12668 case DW_AT_MIPS_abstract_name:
12669 return "DW_AT_MIPS_abstract_name";
12670 case DW_AT_MIPS_clone_origin:
12671 return "DW_AT_MIPS_clone_origin";
12672 case DW_AT_MIPS_has_inlines:
12673 return "DW_AT_MIPS_has_inlines";
b7619582 12674 /* HP extensions. */
c764a876 12675#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12676 case DW_AT_HP_block_index:
12677 return "DW_AT_HP_block_index";
c764a876 12678#endif
b7619582
GF
12679 case DW_AT_HP_unmodifiable:
12680 return "DW_AT_HP_unmodifiable";
12681 case DW_AT_HP_actuals_stmt_list:
12682 return "DW_AT_HP_actuals_stmt_list";
12683 case DW_AT_HP_proc_per_section:
12684 return "DW_AT_HP_proc_per_section";
12685 case DW_AT_HP_raw_data_ptr:
12686 return "DW_AT_HP_raw_data_ptr";
12687 case DW_AT_HP_pass_by_reference:
12688 return "DW_AT_HP_pass_by_reference";
12689 case DW_AT_HP_opt_level:
12690 return "DW_AT_HP_opt_level";
12691 case DW_AT_HP_prof_version_id:
12692 return "DW_AT_HP_prof_version_id";
12693 case DW_AT_HP_opt_flags:
12694 return "DW_AT_HP_opt_flags";
12695 case DW_AT_HP_cold_region_low_pc:
12696 return "DW_AT_HP_cold_region_low_pc";
12697 case DW_AT_HP_cold_region_high_pc:
12698 return "DW_AT_HP_cold_region_high_pc";
12699 case DW_AT_HP_all_variables_modifiable:
12700 return "DW_AT_HP_all_variables_modifiable";
12701 case DW_AT_HP_linkage_name:
12702 return "DW_AT_HP_linkage_name";
12703 case DW_AT_HP_prof_flags:
12704 return "DW_AT_HP_prof_flags";
12705 /* GNU extensions. */
c906108c
SS
12706 case DW_AT_sf_names:
12707 return "DW_AT_sf_names";
12708 case DW_AT_src_info:
12709 return "DW_AT_src_info";
12710 case DW_AT_mac_info:
12711 return "DW_AT_mac_info";
12712 case DW_AT_src_coords:
12713 return "DW_AT_src_coords";
12714 case DW_AT_body_begin:
12715 return "DW_AT_body_begin";
12716 case DW_AT_body_end:
12717 return "DW_AT_body_end";
f5f8a009
EZ
12718 case DW_AT_GNU_vector:
12719 return "DW_AT_GNU_vector";
2de00c64
DE
12720 case DW_AT_GNU_odr_signature:
12721 return "DW_AT_GNU_odr_signature";
b7619582
GF
12722 /* VMS extensions. */
12723 case DW_AT_VMS_rtnbeg_pd_address:
12724 return "DW_AT_VMS_rtnbeg_pd_address";
12725 /* UPC extension. */
12726 case DW_AT_upc_threads_scaled:
12727 return "DW_AT_upc_threads_scaled";
12728 /* PGI (STMicroelectronics) extensions. */
12729 case DW_AT_PGI_lbase:
12730 return "DW_AT_PGI_lbase";
12731 case DW_AT_PGI_soffset:
12732 return "DW_AT_PGI_soffset";
12733 case DW_AT_PGI_lstride:
12734 return "DW_AT_PGI_lstride";
c906108c
SS
12735 default:
12736 return "DW_AT_<unknown>";
12737 }
12738}
12739
12740/* Convert a DWARF value form code into its string name. */
12741
12742static char *
aa1ee363 12743dwarf_form_name (unsigned form)
c906108c
SS
12744{
12745 switch (form)
12746 {
12747 case DW_FORM_addr:
12748 return "DW_FORM_addr";
12749 case DW_FORM_block2:
12750 return "DW_FORM_block2";
12751 case DW_FORM_block4:
12752 return "DW_FORM_block4";
12753 case DW_FORM_data2:
12754 return "DW_FORM_data2";
12755 case DW_FORM_data4:
12756 return "DW_FORM_data4";
12757 case DW_FORM_data8:
12758 return "DW_FORM_data8";
12759 case DW_FORM_string:
12760 return "DW_FORM_string";
12761 case DW_FORM_block:
12762 return "DW_FORM_block";
12763 case DW_FORM_block1:
12764 return "DW_FORM_block1";
12765 case DW_FORM_data1:
12766 return "DW_FORM_data1";
12767 case DW_FORM_flag:
12768 return "DW_FORM_flag";
12769 case DW_FORM_sdata:
12770 return "DW_FORM_sdata";
12771 case DW_FORM_strp:
12772 return "DW_FORM_strp";
12773 case DW_FORM_udata:
12774 return "DW_FORM_udata";
12775 case DW_FORM_ref_addr:
12776 return "DW_FORM_ref_addr";
12777 case DW_FORM_ref1:
12778 return "DW_FORM_ref1";
12779 case DW_FORM_ref2:
12780 return "DW_FORM_ref2";
12781 case DW_FORM_ref4:
12782 return "DW_FORM_ref4";
12783 case DW_FORM_ref8:
12784 return "DW_FORM_ref8";
12785 case DW_FORM_ref_udata:
12786 return "DW_FORM_ref_udata";
12787 case DW_FORM_indirect:
12788 return "DW_FORM_indirect";
348e048f
DE
12789 case DW_FORM_sec_offset:
12790 return "DW_FORM_sec_offset";
12791 case DW_FORM_exprloc:
12792 return "DW_FORM_exprloc";
12793 case DW_FORM_flag_present:
12794 return "DW_FORM_flag_present";
55f1336d
TT
12795 case DW_FORM_ref_sig8:
12796 return "DW_FORM_ref_sig8";
c906108c
SS
12797 default:
12798 return "DW_FORM_<unknown>";
12799 }
12800}
12801
12802/* Convert a DWARF stack opcode into its string name. */
12803
9eae7c52 12804const char *
b1bfef65 12805dwarf_stack_op_name (unsigned op)
c906108c
SS
12806{
12807 switch (op)
12808 {
12809 case DW_OP_addr:
12810 return "DW_OP_addr";
12811 case DW_OP_deref:
12812 return "DW_OP_deref";
12813 case DW_OP_const1u:
12814 return "DW_OP_const1u";
12815 case DW_OP_const1s:
12816 return "DW_OP_const1s";
12817 case DW_OP_const2u:
12818 return "DW_OP_const2u";
12819 case DW_OP_const2s:
12820 return "DW_OP_const2s";
12821 case DW_OP_const4u:
12822 return "DW_OP_const4u";
12823 case DW_OP_const4s:
12824 return "DW_OP_const4s";
12825 case DW_OP_const8u:
12826 return "DW_OP_const8u";
12827 case DW_OP_const8s:
12828 return "DW_OP_const8s";
12829 case DW_OP_constu:
12830 return "DW_OP_constu";
12831 case DW_OP_consts:
12832 return "DW_OP_consts";
12833 case DW_OP_dup:
12834 return "DW_OP_dup";
12835 case DW_OP_drop:
12836 return "DW_OP_drop";
12837 case DW_OP_over:
12838 return "DW_OP_over";
12839 case DW_OP_pick:
12840 return "DW_OP_pick";
12841 case DW_OP_swap:
12842 return "DW_OP_swap";
12843 case DW_OP_rot:
12844 return "DW_OP_rot";
12845 case DW_OP_xderef:
12846 return "DW_OP_xderef";
12847 case DW_OP_abs:
12848 return "DW_OP_abs";
12849 case DW_OP_and:
12850 return "DW_OP_and";
12851 case DW_OP_div:
12852 return "DW_OP_div";
12853 case DW_OP_minus:
12854 return "DW_OP_minus";
12855 case DW_OP_mod:
12856 return "DW_OP_mod";
12857 case DW_OP_mul:
12858 return "DW_OP_mul";
12859 case DW_OP_neg:
12860 return "DW_OP_neg";
12861 case DW_OP_not:
12862 return "DW_OP_not";
12863 case DW_OP_or:
12864 return "DW_OP_or";
12865 case DW_OP_plus:
12866 return "DW_OP_plus";
12867 case DW_OP_plus_uconst:
12868 return "DW_OP_plus_uconst";
12869 case DW_OP_shl:
12870 return "DW_OP_shl";
12871 case DW_OP_shr:
12872 return "DW_OP_shr";
12873 case DW_OP_shra:
12874 return "DW_OP_shra";
12875 case DW_OP_xor:
12876 return "DW_OP_xor";
12877 case DW_OP_bra:
12878 return "DW_OP_bra";
12879 case DW_OP_eq:
12880 return "DW_OP_eq";
12881 case DW_OP_ge:
12882 return "DW_OP_ge";
12883 case DW_OP_gt:
12884 return "DW_OP_gt";
12885 case DW_OP_le:
12886 return "DW_OP_le";
12887 case DW_OP_lt:
12888 return "DW_OP_lt";
12889 case DW_OP_ne:
12890 return "DW_OP_ne";
12891 case DW_OP_skip:
12892 return "DW_OP_skip";
12893 case DW_OP_lit0:
12894 return "DW_OP_lit0";
12895 case DW_OP_lit1:
12896 return "DW_OP_lit1";
12897 case DW_OP_lit2:
12898 return "DW_OP_lit2";
12899 case DW_OP_lit3:
12900 return "DW_OP_lit3";
12901 case DW_OP_lit4:
12902 return "DW_OP_lit4";
12903 case DW_OP_lit5:
12904 return "DW_OP_lit5";
12905 case DW_OP_lit6:
12906 return "DW_OP_lit6";
12907 case DW_OP_lit7:
12908 return "DW_OP_lit7";
12909 case DW_OP_lit8:
12910 return "DW_OP_lit8";
12911 case DW_OP_lit9:
12912 return "DW_OP_lit9";
12913 case DW_OP_lit10:
12914 return "DW_OP_lit10";
12915 case DW_OP_lit11:
12916 return "DW_OP_lit11";
12917 case DW_OP_lit12:
12918 return "DW_OP_lit12";
12919 case DW_OP_lit13:
12920 return "DW_OP_lit13";
12921 case DW_OP_lit14:
12922 return "DW_OP_lit14";
12923 case DW_OP_lit15:
12924 return "DW_OP_lit15";
12925 case DW_OP_lit16:
12926 return "DW_OP_lit16";
12927 case DW_OP_lit17:
12928 return "DW_OP_lit17";
12929 case DW_OP_lit18:
12930 return "DW_OP_lit18";
12931 case DW_OP_lit19:
12932 return "DW_OP_lit19";
12933 case DW_OP_lit20:
12934 return "DW_OP_lit20";
12935 case DW_OP_lit21:
12936 return "DW_OP_lit21";
12937 case DW_OP_lit22:
12938 return "DW_OP_lit22";
12939 case DW_OP_lit23:
12940 return "DW_OP_lit23";
12941 case DW_OP_lit24:
12942 return "DW_OP_lit24";
12943 case DW_OP_lit25:
12944 return "DW_OP_lit25";
12945 case DW_OP_lit26:
12946 return "DW_OP_lit26";
12947 case DW_OP_lit27:
12948 return "DW_OP_lit27";
12949 case DW_OP_lit28:
12950 return "DW_OP_lit28";
12951 case DW_OP_lit29:
12952 return "DW_OP_lit29";
12953 case DW_OP_lit30:
12954 return "DW_OP_lit30";
12955 case DW_OP_lit31:
12956 return "DW_OP_lit31";
12957 case DW_OP_reg0:
12958 return "DW_OP_reg0";
12959 case DW_OP_reg1:
12960 return "DW_OP_reg1";
12961 case DW_OP_reg2:
12962 return "DW_OP_reg2";
12963 case DW_OP_reg3:
12964 return "DW_OP_reg3";
12965 case DW_OP_reg4:
12966 return "DW_OP_reg4";
12967 case DW_OP_reg5:
12968 return "DW_OP_reg5";
12969 case DW_OP_reg6:
12970 return "DW_OP_reg6";
12971 case DW_OP_reg7:
12972 return "DW_OP_reg7";
12973 case DW_OP_reg8:
12974 return "DW_OP_reg8";
12975 case DW_OP_reg9:
12976 return "DW_OP_reg9";
12977 case DW_OP_reg10:
12978 return "DW_OP_reg10";
12979 case DW_OP_reg11:
12980 return "DW_OP_reg11";
12981 case DW_OP_reg12:
12982 return "DW_OP_reg12";
12983 case DW_OP_reg13:
12984 return "DW_OP_reg13";
12985 case DW_OP_reg14:
12986 return "DW_OP_reg14";
12987 case DW_OP_reg15:
12988 return "DW_OP_reg15";
12989 case DW_OP_reg16:
12990 return "DW_OP_reg16";
12991 case DW_OP_reg17:
12992 return "DW_OP_reg17";
12993 case DW_OP_reg18:
12994 return "DW_OP_reg18";
12995 case DW_OP_reg19:
12996 return "DW_OP_reg19";
12997 case DW_OP_reg20:
12998 return "DW_OP_reg20";
12999 case DW_OP_reg21:
13000 return "DW_OP_reg21";
13001 case DW_OP_reg22:
13002 return "DW_OP_reg22";
13003 case DW_OP_reg23:
13004 return "DW_OP_reg23";
13005 case DW_OP_reg24:
13006 return "DW_OP_reg24";
13007 case DW_OP_reg25:
13008 return "DW_OP_reg25";
13009 case DW_OP_reg26:
13010 return "DW_OP_reg26";
13011 case DW_OP_reg27:
13012 return "DW_OP_reg27";
13013 case DW_OP_reg28:
13014 return "DW_OP_reg28";
13015 case DW_OP_reg29:
13016 return "DW_OP_reg29";
13017 case DW_OP_reg30:
13018 return "DW_OP_reg30";
13019 case DW_OP_reg31:
13020 return "DW_OP_reg31";
13021 case DW_OP_breg0:
13022 return "DW_OP_breg0";
13023 case DW_OP_breg1:
13024 return "DW_OP_breg1";
13025 case DW_OP_breg2:
13026 return "DW_OP_breg2";
13027 case DW_OP_breg3:
13028 return "DW_OP_breg3";
13029 case DW_OP_breg4:
13030 return "DW_OP_breg4";
13031 case DW_OP_breg5:
13032 return "DW_OP_breg5";
13033 case DW_OP_breg6:
13034 return "DW_OP_breg6";
13035 case DW_OP_breg7:
13036 return "DW_OP_breg7";
13037 case DW_OP_breg8:
13038 return "DW_OP_breg8";
13039 case DW_OP_breg9:
13040 return "DW_OP_breg9";
13041 case DW_OP_breg10:
13042 return "DW_OP_breg10";
13043 case DW_OP_breg11:
13044 return "DW_OP_breg11";
13045 case DW_OP_breg12:
13046 return "DW_OP_breg12";
13047 case DW_OP_breg13:
13048 return "DW_OP_breg13";
13049 case DW_OP_breg14:
13050 return "DW_OP_breg14";
13051 case DW_OP_breg15:
13052 return "DW_OP_breg15";
13053 case DW_OP_breg16:
13054 return "DW_OP_breg16";
13055 case DW_OP_breg17:
13056 return "DW_OP_breg17";
13057 case DW_OP_breg18:
13058 return "DW_OP_breg18";
13059 case DW_OP_breg19:
13060 return "DW_OP_breg19";
13061 case DW_OP_breg20:
13062 return "DW_OP_breg20";
13063 case DW_OP_breg21:
13064 return "DW_OP_breg21";
13065 case DW_OP_breg22:
13066 return "DW_OP_breg22";
13067 case DW_OP_breg23:
13068 return "DW_OP_breg23";
13069 case DW_OP_breg24:
13070 return "DW_OP_breg24";
13071 case DW_OP_breg25:
13072 return "DW_OP_breg25";
13073 case DW_OP_breg26:
13074 return "DW_OP_breg26";
13075 case DW_OP_breg27:
13076 return "DW_OP_breg27";
13077 case DW_OP_breg28:
13078 return "DW_OP_breg28";
13079 case DW_OP_breg29:
13080 return "DW_OP_breg29";
13081 case DW_OP_breg30:
13082 return "DW_OP_breg30";
13083 case DW_OP_breg31:
13084 return "DW_OP_breg31";
13085 case DW_OP_regx:
13086 return "DW_OP_regx";
13087 case DW_OP_fbreg:
13088 return "DW_OP_fbreg";
13089 case DW_OP_bregx:
13090 return "DW_OP_bregx";
13091 case DW_OP_piece:
13092 return "DW_OP_piece";
13093 case DW_OP_deref_size:
13094 return "DW_OP_deref_size";
13095 case DW_OP_xderef_size:
13096 return "DW_OP_xderef_size";
13097 case DW_OP_nop:
13098 return "DW_OP_nop";
b7619582 13099 /* DWARF 3 extensions. */
ed348acc
EZ
13100 case DW_OP_push_object_address:
13101 return "DW_OP_push_object_address";
13102 case DW_OP_call2:
13103 return "DW_OP_call2";
13104 case DW_OP_call4:
13105 return "DW_OP_call4";
13106 case DW_OP_call_ref:
13107 return "DW_OP_call_ref";
b7619582
GF
13108 case DW_OP_form_tls_address:
13109 return "DW_OP_form_tls_address";
13110 case DW_OP_call_frame_cfa:
13111 return "DW_OP_call_frame_cfa";
13112 case DW_OP_bit_piece:
13113 return "DW_OP_bit_piece";
9eae7c52
TT
13114 /* DWARF 4 extensions. */
13115 case DW_OP_implicit_value:
13116 return "DW_OP_implicit_value";
13117 case DW_OP_stack_value:
13118 return "DW_OP_stack_value";
13119 /* GNU extensions. */
ed348acc
EZ
13120 case DW_OP_GNU_push_tls_address:
13121 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13122 case DW_OP_GNU_uninit:
13123 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13124 case DW_OP_GNU_implicit_pointer:
13125 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13126 case DW_OP_GNU_entry_value:
13127 return "DW_OP_GNU_entry_value";
13128 case DW_OP_GNU_const_type:
13129 return "DW_OP_GNU_const_type";
13130 case DW_OP_GNU_regval_type:
13131 return "DW_OP_GNU_regval_type";
13132 case DW_OP_GNU_deref_type:
13133 return "DW_OP_GNU_deref_type";
13134 case DW_OP_GNU_convert:
13135 return "DW_OP_GNU_convert";
13136 case DW_OP_GNU_reinterpret:
13137 return "DW_OP_GNU_reinterpret";
c906108c 13138 default:
b1bfef65 13139 return NULL;
c906108c
SS
13140 }
13141}
13142
13143static char *
fba45db2 13144dwarf_bool_name (unsigned mybool)
c906108c
SS
13145{
13146 if (mybool)
13147 return "TRUE";
13148 else
13149 return "FALSE";
13150}
13151
13152/* Convert a DWARF type code into its string name. */
13153
13154static char *
aa1ee363 13155dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13156{
13157 switch (enc)
13158 {
b7619582
GF
13159 case DW_ATE_void:
13160 return "DW_ATE_void";
c906108c
SS
13161 case DW_ATE_address:
13162 return "DW_ATE_address";
13163 case DW_ATE_boolean:
13164 return "DW_ATE_boolean";
13165 case DW_ATE_complex_float:
13166 return "DW_ATE_complex_float";
13167 case DW_ATE_float:
13168 return "DW_ATE_float";
13169 case DW_ATE_signed:
13170 return "DW_ATE_signed";
13171 case DW_ATE_signed_char:
13172 return "DW_ATE_signed_char";
13173 case DW_ATE_unsigned:
13174 return "DW_ATE_unsigned";
13175 case DW_ATE_unsigned_char:
13176 return "DW_ATE_unsigned_char";
b7619582 13177 /* DWARF 3. */
d9fa45fe
DC
13178 case DW_ATE_imaginary_float:
13179 return "DW_ATE_imaginary_float";
b7619582
GF
13180 case DW_ATE_packed_decimal:
13181 return "DW_ATE_packed_decimal";
13182 case DW_ATE_numeric_string:
13183 return "DW_ATE_numeric_string";
13184 case DW_ATE_edited:
13185 return "DW_ATE_edited";
13186 case DW_ATE_signed_fixed:
13187 return "DW_ATE_signed_fixed";
13188 case DW_ATE_unsigned_fixed:
13189 return "DW_ATE_unsigned_fixed";
13190 case DW_ATE_decimal_float:
13191 return "DW_ATE_decimal_float";
75079b2b
TT
13192 /* DWARF 4. */
13193 case DW_ATE_UTF:
13194 return "DW_ATE_UTF";
b7619582
GF
13195 /* HP extensions. */
13196 case DW_ATE_HP_float80:
13197 return "DW_ATE_HP_float80";
13198 case DW_ATE_HP_complex_float80:
13199 return "DW_ATE_HP_complex_float80";
13200 case DW_ATE_HP_float128:
13201 return "DW_ATE_HP_float128";
13202 case DW_ATE_HP_complex_float128:
13203 return "DW_ATE_HP_complex_float128";
13204 case DW_ATE_HP_floathpintel:
13205 return "DW_ATE_HP_floathpintel";
13206 case DW_ATE_HP_imaginary_float80:
13207 return "DW_ATE_HP_imaginary_float80";
13208 case DW_ATE_HP_imaginary_float128:
13209 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13210 default:
13211 return "DW_ATE_<unknown>";
13212 }
13213}
13214
0963b4bd 13215/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13216
13217#if 0
13218static char *
aa1ee363 13219dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13220{
13221 switch (cfi_opc)
13222 {
13223 case DW_CFA_advance_loc:
13224 return "DW_CFA_advance_loc";
13225 case DW_CFA_offset:
13226 return "DW_CFA_offset";
13227 case DW_CFA_restore:
13228 return "DW_CFA_restore";
13229 case DW_CFA_nop:
13230 return "DW_CFA_nop";
13231 case DW_CFA_set_loc:
13232 return "DW_CFA_set_loc";
13233 case DW_CFA_advance_loc1:
13234 return "DW_CFA_advance_loc1";
13235 case DW_CFA_advance_loc2:
13236 return "DW_CFA_advance_loc2";
13237 case DW_CFA_advance_loc4:
13238 return "DW_CFA_advance_loc4";
13239 case DW_CFA_offset_extended:
13240 return "DW_CFA_offset_extended";
13241 case DW_CFA_restore_extended:
13242 return "DW_CFA_restore_extended";
13243 case DW_CFA_undefined:
13244 return "DW_CFA_undefined";
13245 case DW_CFA_same_value:
13246 return "DW_CFA_same_value";
13247 case DW_CFA_register:
13248 return "DW_CFA_register";
13249 case DW_CFA_remember_state:
13250 return "DW_CFA_remember_state";
13251 case DW_CFA_restore_state:
13252 return "DW_CFA_restore_state";
13253 case DW_CFA_def_cfa:
13254 return "DW_CFA_def_cfa";
13255 case DW_CFA_def_cfa_register:
13256 return "DW_CFA_def_cfa_register";
13257 case DW_CFA_def_cfa_offset:
13258 return "DW_CFA_def_cfa_offset";
b7619582 13259 /* DWARF 3. */
985cb1a3
JM
13260 case DW_CFA_def_cfa_expression:
13261 return "DW_CFA_def_cfa_expression";
13262 case DW_CFA_expression:
13263 return "DW_CFA_expression";
13264 case DW_CFA_offset_extended_sf:
13265 return "DW_CFA_offset_extended_sf";
13266 case DW_CFA_def_cfa_sf:
13267 return "DW_CFA_def_cfa_sf";
13268 case DW_CFA_def_cfa_offset_sf:
13269 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13270 case DW_CFA_val_offset:
13271 return "DW_CFA_val_offset";
13272 case DW_CFA_val_offset_sf:
13273 return "DW_CFA_val_offset_sf";
13274 case DW_CFA_val_expression:
13275 return "DW_CFA_val_expression";
13276 /* SGI/MIPS specific. */
c906108c
SS
13277 case DW_CFA_MIPS_advance_loc8:
13278 return "DW_CFA_MIPS_advance_loc8";
b7619582 13279 /* GNU extensions. */
985cb1a3
JM
13280 case DW_CFA_GNU_window_save:
13281 return "DW_CFA_GNU_window_save";
13282 case DW_CFA_GNU_args_size:
13283 return "DW_CFA_GNU_args_size";
13284 case DW_CFA_GNU_negative_offset_extended:
13285 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13286 default:
13287 return "DW_CFA_<unknown>";
13288 }
13289}
13290#endif
13291
f9aca02d 13292static void
d97bc12b 13293dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13294{
13295 unsigned int i;
13296
d97bc12b
DE
13297 print_spaces (indent, f);
13298 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13299 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13300
13301 if (die->parent != NULL)
13302 {
13303 print_spaces (indent, f);
13304 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13305 die->parent->offset);
13306 }
13307
13308 print_spaces (indent, f);
13309 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13310 dwarf_bool_name (die->child != NULL));
c906108c 13311
d97bc12b
DE
13312 print_spaces (indent, f);
13313 fprintf_unfiltered (f, " attributes:\n");
13314
c906108c
SS
13315 for (i = 0; i < die->num_attrs; ++i)
13316 {
d97bc12b
DE
13317 print_spaces (indent, f);
13318 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13319 dwarf_attr_name (die->attrs[i].name),
13320 dwarf_form_name (die->attrs[i].form));
d97bc12b 13321
c906108c
SS
13322 switch (die->attrs[i].form)
13323 {
13324 case DW_FORM_ref_addr:
13325 case DW_FORM_addr:
d97bc12b 13326 fprintf_unfiltered (f, "address: ");
5af949e3 13327 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13328 break;
13329 case DW_FORM_block2:
13330 case DW_FORM_block4:
13331 case DW_FORM_block:
13332 case DW_FORM_block1:
3e43a32a
MS
13333 fprintf_unfiltered (f, "block: size %d",
13334 DW_BLOCK (&die->attrs[i])->size);
c906108c 13335 break;
2dc7f7b3
TT
13336 case DW_FORM_exprloc:
13337 fprintf_unfiltered (f, "expression: size %u",
13338 DW_BLOCK (&die->attrs[i])->size);
13339 break;
10b3939b
DJ
13340 case DW_FORM_ref1:
13341 case DW_FORM_ref2:
13342 case DW_FORM_ref4:
d97bc12b 13343 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13344 (long) (DW_ADDR (&die->attrs[i])));
13345 break;
c906108c
SS
13346 case DW_FORM_data1:
13347 case DW_FORM_data2:
13348 case DW_FORM_data4:
ce5d95e1 13349 case DW_FORM_data8:
c906108c
SS
13350 case DW_FORM_udata:
13351 case DW_FORM_sdata:
43bbcdc2
PH
13352 fprintf_unfiltered (f, "constant: %s",
13353 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13354 break;
2dc7f7b3
TT
13355 case DW_FORM_sec_offset:
13356 fprintf_unfiltered (f, "section offset: %s",
13357 pulongest (DW_UNSND (&die->attrs[i])));
13358 break;
55f1336d 13359 case DW_FORM_ref_sig8:
348e048f
DE
13360 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13361 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b3c8eb43 13362 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
348e048f
DE
13363 else
13364 fprintf_unfiltered (f, "signatured type, offset: unknown");
13365 break;
c906108c 13366 case DW_FORM_string:
4bdf3d34 13367 case DW_FORM_strp:
8285870a 13368 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13369 DW_STRING (&die->attrs[i])
8285870a
JK
13370 ? DW_STRING (&die->attrs[i]) : "",
13371 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13372 break;
13373 case DW_FORM_flag:
13374 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13375 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13376 else
d97bc12b 13377 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13378 break;
2dc7f7b3
TT
13379 case DW_FORM_flag_present:
13380 fprintf_unfiltered (f, "flag: TRUE");
13381 break;
a8329558 13382 case DW_FORM_indirect:
0963b4bd
MS
13383 /* The reader will have reduced the indirect form to
13384 the "base form" so this form should not occur. */
3e43a32a
MS
13385 fprintf_unfiltered (f,
13386 "unexpected attribute form: DW_FORM_indirect");
a8329558 13387 break;
c906108c 13388 default:
d97bc12b 13389 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13390 die->attrs[i].form);
d97bc12b 13391 break;
c906108c 13392 }
d97bc12b 13393 fprintf_unfiltered (f, "\n");
c906108c
SS
13394 }
13395}
13396
f9aca02d 13397static void
d97bc12b 13398dump_die_for_error (struct die_info *die)
c906108c 13399{
d97bc12b
DE
13400 dump_die_shallow (gdb_stderr, 0, die);
13401}
13402
13403static void
13404dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13405{
13406 int indent = level * 4;
13407
13408 gdb_assert (die != NULL);
13409
13410 if (level >= max_level)
13411 return;
13412
13413 dump_die_shallow (f, indent, die);
13414
13415 if (die->child != NULL)
c906108c 13416 {
d97bc12b
DE
13417 print_spaces (indent, f);
13418 fprintf_unfiltered (f, " Children:");
13419 if (level + 1 < max_level)
13420 {
13421 fprintf_unfiltered (f, "\n");
13422 dump_die_1 (f, level + 1, max_level, die->child);
13423 }
13424 else
13425 {
3e43a32a
MS
13426 fprintf_unfiltered (f,
13427 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13428 }
13429 }
13430
13431 if (die->sibling != NULL && level > 0)
13432 {
13433 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13434 }
13435}
13436
d97bc12b
DE
13437/* This is called from the pdie macro in gdbinit.in.
13438 It's not static so gcc will keep a copy callable from gdb. */
13439
13440void
13441dump_die (struct die_info *die, int max_level)
13442{
13443 dump_die_1 (gdb_stdlog, 0, max_level, die);
13444}
13445
f9aca02d 13446static void
51545339 13447store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13448{
51545339 13449 void **slot;
c906108c 13450
51545339
DJ
13451 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13452
13453 *slot = die;
c906108c
SS
13454}
13455
93311388
DE
13456static int
13457is_ref_attr (struct attribute *attr)
c906108c 13458{
c906108c
SS
13459 switch (attr->form)
13460 {
13461 case DW_FORM_ref_addr:
c906108c
SS
13462 case DW_FORM_ref1:
13463 case DW_FORM_ref2:
13464 case DW_FORM_ref4:
613e1657 13465 case DW_FORM_ref8:
c906108c 13466 case DW_FORM_ref_udata:
93311388 13467 return 1;
c906108c 13468 default:
93311388 13469 return 0;
c906108c 13470 }
93311388
DE
13471}
13472
13473static unsigned int
13474dwarf2_get_ref_die_offset (struct attribute *attr)
13475{
13476 if (is_ref_attr (attr))
13477 return DW_ADDR (attr);
13478
13479 complaint (&symfile_complaints,
13480 _("unsupported die ref attribute form: '%s'"),
13481 dwarf_form_name (attr->form));
13482 return 0;
c906108c
SS
13483}
13484
43bbcdc2
PH
13485/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13486 * the value held by the attribute is not constant. */
a02abb62 13487
43bbcdc2 13488static LONGEST
a02abb62
JB
13489dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13490{
13491 if (attr->form == DW_FORM_sdata)
13492 return DW_SND (attr);
13493 else if (attr->form == DW_FORM_udata
13494 || attr->form == DW_FORM_data1
13495 || attr->form == DW_FORM_data2
13496 || attr->form == DW_FORM_data4
13497 || attr->form == DW_FORM_data8)
13498 return DW_UNSND (attr);
13499 else
13500 {
3e43a32a
MS
13501 complaint (&symfile_complaints,
13502 _("Attribute value is not a constant (%s)"),
a02abb62
JB
13503 dwarf_form_name (attr->form));
13504 return default_value;
13505 }
13506}
13507
03dd20cc 13508/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
13509 unit and add it to our queue.
13510 The result is non-zero if PER_CU was queued, otherwise the result is zero
13511 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 13512
348e048f 13513static int
03dd20cc
DJ
13514maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13515 struct dwarf2_per_cu_data *per_cu)
13516{
98bfdba5
PA
13517 /* We may arrive here during partial symbol reading, if we need full
13518 DIEs to process an unusual case (e.g. template arguments). Do
13519 not queue PER_CU, just tell our caller to load its DIEs. */
13520 if (dwarf2_per_objfile->reading_partial_symbols)
13521 {
13522 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13523 return 1;
13524 return 0;
13525 }
13526
03dd20cc
DJ
13527 /* Mark the dependence relation so that we don't flush PER_CU
13528 too early. */
13529 dwarf2_add_dependence (this_cu, per_cu);
13530
13531 /* If it's already on the queue, we have nothing to do. */
13532 if (per_cu->queued)
348e048f 13533 return 0;
03dd20cc
DJ
13534
13535 /* If the compilation unit is already loaded, just mark it as
13536 used. */
13537 if (per_cu->cu != NULL)
13538 {
13539 per_cu->cu->last_used = 0;
348e048f 13540 return 0;
03dd20cc
DJ
13541 }
13542
13543 /* Add it to the queue. */
13544 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
13545
13546 return 1;
13547}
13548
13549/* Follow reference or signature attribute ATTR of SRC_DIE.
13550 On entry *REF_CU is the CU of SRC_DIE.
13551 On exit *REF_CU is the CU of the result. */
13552
13553static struct die_info *
13554follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13555 struct dwarf2_cu **ref_cu)
13556{
13557 struct die_info *die;
13558
13559 if (is_ref_attr (attr))
13560 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 13561 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
13562 die = follow_die_sig (src_die, attr, ref_cu);
13563 else
13564 {
13565 dump_die_for_error (src_die);
13566 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13567 (*ref_cu)->objfile->name);
13568 }
13569
13570 return die;
03dd20cc
DJ
13571}
13572
5c631832 13573/* Follow reference OFFSET.
673bfd45
DE
13574 On entry *REF_CU is the CU of the source die referencing OFFSET.
13575 On exit *REF_CU is the CU of the result.
13576 Returns NULL if OFFSET is invalid. */
f504f079 13577
f9aca02d 13578static struct die_info *
5c631832 13579follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13580{
10b3939b 13581 struct die_info temp_die;
f2f0e013 13582 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13583
348e048f
DE
13584 gdb_assert (cu->per_cu != NULL);
13585
98bfdba5
PA
13586 target_cu = cu;
13587
348e048f
DE
13588 if (cu->per_cu->from_debug_types)
13589 {
13590 /* .debug_types CUs cannot reference anything outside their CU.
13591 If they need to, they have to reference a signatured type via
55f1336d 13592 DW_FORM_ref_sig8. */
348e048f 13593 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13594 return NULL;
348e048f
DE
13595 }
13596 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13597 {
13598 struct dwarf2_per_cu_data *per_cu;
9a619af0 13599
45452591 13600 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13601
13602 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13603 if (maybe_queue_comp_unit (cu, per_cu))
13604 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13605
10b3939b
DJ
13606 target_cu = per_cu->cu;
13607 }
98bfdba5
PA
13608 else if (cu->dies == NULL)
13609 {
13610 /* We're loading full DIEs during partial symbol reading. */
13611 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13612 load_full_comp_unit (cu->per_cu, cu->objfile);
13613 }
c906108c 13614
f2f0e013 13615 *ref_cu = target_cu;
51545339 13616 temp_die.offset = offset;
5c631832
JK
13617 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13618}
10b3939b 13619
5c631832
JK
13620/* Follow reference attribute ATTR of SRC_DIE.
13621 On entry *REF_CU is the CU of SRC_DIE.
13622 On exit *REF_CU is the CU of the result. */
13623
13624static struct die_info *
13625follow_die_ref (struct die_info *src_die, struct attribute *attr,
13626 struct dwarf2_cu **ref_cu)
13627{
13628 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13629 struct dwarf2_cu *cu = *ref_cu;
13630 struct die_info *die;
13631
13632 die = follow_die_offset (offset, ref_cu);
13633 if (!die)
13634 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13635 "at 0x%x [in module %s]"),
13636 offset, src_die->offset, cu->objfile->name);
348e048f 13637
5c631832
JK
13638 return die;
13639}
13640
13641/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13642 value is intended for DW_OP_call*. */
13643
13644struct dwarf2_locexpr_baton
13645dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
13646 struct dwarf2_per_cu_data *per_cu,
13647 CORE_ADDR (*get_frame_pc) (void *baton),
13648 void *baton)
5c631832
JK
13649{
13650 struct dwarf2_cu *cu = per_cu->cu;
13651 struct die_info *die;
13652 struct attribute *attr;
13653 struct dwarf2_locexpr_baton retval;
13654
8cf6f0b1
TT
13655 dw2_setup (per_cu->objfile);
13656
5c631832
JK
13657 die = follow_die_offset (offset, &cu);
13658 if (!die)
13659 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13660 offset, per_cu->cu->objfile->name);
13661
13662 attr = dwarf2_attr (die, DW_AT_location, cu);
13663 if (!attr)
13664 {
13665 /* DWARF: "If there is no such attribute, then there is no effect.". */
13666
13667 retval.data = NULL;
13668 retval.size = 0;
13669 }
8cf6f0b1
TT
13670 else if (attr_form_is_section_offset (attr))
13671 {
13672 struct dwarf2_loclist_baton loclist_baton;
13673 CORE_ADDR pc = (*get_frame_pc) (baton);
13674 size_t size;
13675
13676 fill_in_loclist_baton (cu, &loclist_baton, attr);
13677
13678 retval.data = dwarf2_find_location_expression (&loclist_baton,
13679 &size, pc);
13680 retval.size = size;
13681 }
5c631832
JK
13682 else
13683 {
13684 if (!attr_form_is_block (attr))
13685 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13686 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13687 offset, per_cu->cu->objfile->name);
13688
13689 retval.data = DW_BLOCK (attr)->data;
13690 retval.size = DW_BLOCK (attr)->size;
13691 }
13692 retval.per_cu = cu->per_cu;
13693 return retval;
348e048f
DE
13694}
13695
8a9b8146
TT
13696/* Return the type of the DIE at DIE_OFFSET in the CU named by
13697 PER_CU. */
13698
13699struct type *
13700dwarf2_get_die_type (unsigned int die_offset,
13701 struct dwarf2_per_cu_data *per_cu)
13702{
8a9b8146 13703 dw2_setup (per_cu->objfile);
9ff3b74f 13704 return get_die_type_at_offset (die_offset, per_cu);
8a9b8146
TT
13705}
13706
348e048f
DE
13707/* Follow the signature attribute ATTR in SRC_DIE.
13708 On entry *REF_CU is the CU of SRC_DIE.
13709 On exit *REF_CU is the CU of the result. */
13710
13711static struct die_info *
13712follow_die_sig (struct die_info *src_die, struct attribute *attr,
13713 struct dwarf2_cu **ref_cu)
13714{
13715 struct objfile *objfile = (*ref_cu)->objfile;
13716 struct die_info temp_die;
13717 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13718 struct dwarf2_cu *sig_cu;
13719 struct die_info *die;
13720
13721 /* sig_type will be NULL if the signatured type is missing from
13722 the debug info. */
13723 if (sig_type == NULL)
13724 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13725 "at 0x%x [in module %s]"),
13726 src_die->offset, objfile->name);
13727
13728 /* If necessary, add it to the queue and load its DIEs. */
13729
13730 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13731 read_signatured_type (objfile, sig_type);
13732
13733 gdb_assert (sig_type->per_cu.cu != NULL);
13734
13735 sig_cu = sig_type->per_cu.cu;
13736 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13737 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13738 if (die)
13739 {
13740 *ref_cu = sig_cu;
13741 return die;
13742 }
13743
3e43a32a
MS
13744 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13745 "from DIE at 0x%x [in module %s]"),
348e048f
DE
13746 sig_type->type_offset, src_die->offset, objfile->name);
13747}
13748
13749/* Given an offset of a signatured type, return its signatured_type. */
13750
13751static struct signatured_type *
13752lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13753{
13754 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13755 unsigned int length, initial_length_size;
13756 unsigned int sig_offset;
13757 struct signatured_type find_entry, *type_sig;
13758
13759 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13760 sig_offset = (initial_length_size
13761 + 2 /*version*/
13762 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13763 + 1 /*address_size*/);
13764 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13765 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13766
13767 /* This is only used to lookup previously recorded types.
13768 If we didn't find it, it's our bug. */
13769 gdb_assert (type_sig != NULL);
b3c8eb43 13770 gdb_assert (offset == type_sig->per_cu.offset);
348e048f
DE
13771
13772 return type_sig;
13773}
13774
13775/* Read in signatured type at OFFSET and build its CU and die(s). */
13776
13777static void
13778read_signatured_type_at_offset (struct objfile *objfile,
13779 unsigned int offset)
13780{
13781 struct signatured_type *type_sig;
13782
be391dca
TT
13783 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13784
348e048f
DE
13785 /* We have the section offset, but we need the signature to do the
13786 hash table lookup. */
13787 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13788
13789 gdb_assert (type_sig->per_cu.cu == NULL);
13790
13791 read_signatured_type (objfile, type_sig);
13792
13793 gdb_assert (type_sig->per_cu.cu != NULL);
13794}
13795
13796/* Read in a signatured type and build its CU and DIEs. */
13797
13798static void
13799read_signatured_type (struct objfile *objfile,
13800 struct signatured_type *type_sig)
13801{
1fd400ff 13802 gdb_byte *types_ptr;
348e048f
DE
13803 struct die_reader_specs reader_specs;
13804 struct dwarf2_cu *cu;
13805 ULONGEST signature;
13806 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13807
1fd400ff 13808 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
b3c8eb43 13809 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->per_cu.offset;
1fd400ff 13810
348e048f
DE
13811 gdb_assert (type_sig->per_cu.cu == NULL);
13812
9816fde3
JK
13813 cu = xmalloc (sizeof (*cu));
13814 init_one_comp_unit (cu, objfile);
13815
348e048f
DE
13816 type_sig->per_cu.cu = cu;
13817 cu->per_cu = &type_sig->per_cu;
13818
13819 /* If an error occurs while loading, release our storage. */
13820 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13821
13822 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13823 types_ptr, objfile->obfd);
13824 gdb_assert (signature == type_sig->signature);
13825
13826 cu->die_hash
13827 = htab_create_alloc_ex (cu->header.length / 12,
13828 die_hash,
13829 die_eq,
13830 NULL,
13831 &cu->comp_unit_obstack,
13832 hashtab_obstack_allocate,
13833 dummy_obstack_deallocate);
13834
13835 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13836 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13837
13838 init_cu_die_reader (&reader_specs, cu);
13839
13840 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13841 NULL /*parent*/);
13842
13843 /* We try not to read any attributes in this function, because not
13844 all objfiles needed for references have been loaded yet, and symbol
13845 table processing isn't initialized. But we have to set the CU language,
13846 or we won't be able to build types correctly. */
9816fde3 13847 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
13848
13849 do_cleanups (back_to);
13850
13851 /* We've successfully allocated this compilation unit. Let our caller
13852 clean it up when finished with it. */
13853 discard_cleanups (free_cu_cleanup);
13854
13855 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13856 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13857}
13858
c906108c
SS
13859/* Decode simple location descriptions.
13860 Given a pointer to a dwarf block that defines a location, compute
13861 the location and return the value.
13862
4cecd739
DJ
13863 NOTE drow/2003-11-18: This function is called in two situations
13864 now: for the address of static or global variables (partial symbols
13865 only) and for offsets into structures which are expected to be
13866 (more or less) constant. The partial symbol case should go away,
13867 and only the constant case should remain. That will let this
13868 function complain more accurately. A few special modes are allowed
13869 without complaint for global variables (for instance, global
13870 register values and thread-local values).
c906108c
SS
13871
13872 A location description containing no operations indicates that the
4cecd739 13873 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13874 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13875 callers will only want a very basic result and this can become a
13876 complaint.
c906108c 13877
d53d4ac5 13878 Note that stack[0] is unused except as a default error return. */
c906108c
SS
13879
13880static CORE_ADDR
e7c27a73 13881decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13882{
e7c27a73 13883 struct objfile *objfile = cu->objfile;
c906108c
SS
13884 int i;
13885 int size = blk->size;
fe1b8b76 13886 gdb_byte *data = blk->data;
c906108c
SS
13887 CORE_ADDR stack[64];
13888 int stacki;
13889 unsigned int bytes_read, unsnd;
fe1b8b76 13890 gdb_byte op;
c906108c
SS
13891
13892 i = 0;
13893 stacki = 0;
13894 stack[stacki] = 0;
d53d4ac5 13895 stack[++stacki] = 0;
c906108c
SS
13896
13897 while (i < size)
13898 {
c906108c
SS
13899 op = data[i++];
13900 switch (op)
13901 {
f1bea926
JM
13902 case DW_OP_lit0:
13903 case DW_OP_lit1:
13904 case DW_OP_lit2:
13905 case DW_OP_lit3:
13906 case DW_OP_lit4:
13907 case DW_OP_lit5:
13908 case DW_OP_lit6:
13909 case DW_OP_lit7:
13910 case DW_OP_lit8:
13911 case DW_OP_lit9:
13912 case DW_OP_lit10:
13913 case DW_OP_lit11:
13914 case DW_OP_lit12:
13915 case DW_OP_lit13:
13916 case DW_OP_lit14:
13917 case DW_OP_lit15:
13918 case DW_OP_lit16:
13919 case DW_OP_lit17:
13920 case DW_OP_lit18:
13921 case DW_OP_lit19:
13922 case DW_OP_lit20:
13923 case DW_OP_lit21:
13924 case DW_OP_lit22:
13925 case DW_OP_lit23:
13926 case DW_OP_lit24:
13927 case DW_OP_lit25:
13928 case DW_OP_lit26:
13929 case DW_OP_lit27:
13930 case DW_OP_lit28:
13931 case DW_OP_lit29:
13932 case DW_OP_lit30:
13933 case DW_OP_lit31:
13934 stack[++stacki] = op - DW_OP_lit0;
13935 break;
13936
c906108c
SS
13937 case DW_OP_reg0:
13938 case DW_OP_reg1:
13939 case DW_OP_reg2:
13940 case DW_OP_reg3:
13941 case DW_OP_reg4:
13942 case DW_OP_reg5:
13943 case DW_OP_reg6:
13944 case DW_OP_reg7:
13945 case DW_OP_reg8:
13946 case DW_OP_reg9:
13947 case DW_OP_reg10:
13948 case DW_OP_reg11:
13949 case DW_OP_reg12:
13950 case DW_OP_reg13:
13951 case DW_OP_reg14:
13952 case DW_OP_reg15:
13953 case DW_OP_reg16:
13954 case DW_OP_reg17:
13955 case DW_OP_reg18:
13956 case DW_OP_reg19:
13957 case DW_OP_reg20:
13958 case DW_OP_reg21:
13959 case DW_OP_reg22:
13960 case DW_OP_reg23:
13961 case DW_OP_reg24:
13962 case DW_OP_reg25:
13963 case DW_OP_reg26:
13964 case DW_OP_reg27:
13965 case DW_OP_reg28:
13966 case DW_OP_reg29:
13967 case DW_OP_reg30:
13968 case DW_OP_reg31:
c906108c 13969 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13970 if (i < size)
13971 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13972 break;
13973
13974 case DW_OP_regx:
c906108c
SS
13975 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13976 i += bytes_read;
c906108c 13977 stack[++stacki] = unsnd;
4cecd739
DJ
13978 if (i < size)
13979 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13980 break;
13981
13982 case DW_OP_addr:
107d2387 13983 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13984 cu, &bytes_read);
107d2387 13985 i += bytes_read;
c906108c
SS
13986 break;
13987
13988 case DW_OP_const1u:
13989 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13990 i += 1;
13991 break;
13992
13993 case DW_OP_const1s:
13994 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13995 i += 1;
13996 break;
13997
13998 case DW_OP_const2u:
13999 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14000 i += 2;
14001 break;
14002
14003 case DW_OP_const2s:
14004 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14005 i += 2;
14006 break;
14007
14008 case DW_OP_const4u:
14009 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14010 i += 4;
14011 break;
14012
14013 case DW_OP_const4s:
14014 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14015 i += 4;
14016 break;
14017
14018 case DW_OP_constu:
14019 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 14020 &bytes_read);
c906108c
SS
14021 i += bytes_read;
14022 break;
14023
14024 case DW_OP_consts:
14025 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14026 i += bytes_read;
14027 break;
14028
f1bea926
JM
14029 case DW_OP_dup:
14030 stack[stacki + 1] = stack[stacki];
14031 stacki++;
14032 break;
14033
c906108c
SS
14034 case DW_OP_plus:
14035 stack[stacki - 1] += stack[stacki];
14036 stacki--;
14037 break;
14038
14039 case DW_OP_plus_uconst:
3e43a32a
MS
14040 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14041 &bytes_read);
c906108c
SS
14042 i += bytes_read;
14043 break;
14044
14045 case DW_OP_minus:
f1bea926 14046 stack[stacki - 1] -= stack[stacki];
c906108c
SS
14047 stacki--;
14048 break;
14049
7a292a7a 14050 case DW_OP_deref:
7a292a7a 14051 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
14052 this using GDB's address_class enum. This is valid for partial
14053 global symbols, although the variable's address will be bogus
14054 in the psymtab. */
7a292a7a 14055 if (i < size)
4d3c2250 14056 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
14057 break;
14058
9d774e44 14059 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
14060 /* The top of the stack has the offset from the beginning
14061 of the thread control block at which the variable is located. */
14062 /* Nothing should follow this operator, so the top of stack would
14063 be returned. */
4cecd739
DJ
14064 /* This is valid for partial global symbols, but the variable's
14065 address will be bogus in the psymtab. */
9d774e44 14066 if (i < size)
4d3c2250 14067 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
14068 break;
14069
42be36b3
CT
14070 case DW_OP_GNU_uninit:
14071 break;
14072
c906108c 14073 default:
b1bfef65
TT
14074 {
14075 const char *name = dwarf_stack_op_name (op);
14076
14077 if (name)
14078 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14079 name);
14080 else
14081 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14082 op);
14083 }
14084
c906108c
SS
14085 return (stack[stacki]);
14086 }
d53d4ac5
TT
14087
14088 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14089 outside of the allocated space. Also enforce minimum>0. */
14090 if (stacki >= ARRAY_SIZE (stack) - 1)
14091 {
14092 complaint (&symfile_complaints,
14093 _("location description stack overflow"));
14094 return 0;
14095 }
14096
14097 if (stacki <= 0)
14098 {
14099 complaint (&symfile_complaints,
14100 _("location description stack underflow"));
14101 return 0;
14102 }
c906108c
SS
14103 }
14104 return (stack[stacki]);
14105}
14106
14107/* memory allocation interface */
14108
c906108c 14109static struct dwarf_block *
7b5a2f43 14110dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14111{
14112 struct dwarf_block *blk;
14113
14114 blk = (struct dwarf_block *)
7b5a2f43 14115 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14116 return (blk);
14117}
14118
14119static struct abbrev_info *
f3dd6933 14120dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14121{
14122 struct abbrev_info *abbrev;
14123
f3dd6933
DJ
14124 abbrev = (struct abbrev_info *)
14125 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14126 memset (abbrev, 0, sizeof (struct abbrev_info));
14127 return (abbrev);
14128}
14129
14130static struct die_info *
b60c80d6 14131dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14132{
14133 struct die_info *die;
b60c80d6
DJ
14134 size_t size = sizeof (struct die_info);
14135
14136 if (num_attrs > 1)
14137 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14138
b60c80d6 14139 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14140 memset (die, 0, sizeof (struct die_info));
14141 return (die);
14142}
2e276125
JB
14143
14144\f
14145/* Macro support. */
14146
2e276125
JB
14147/* Return the full name of file number I in *LH's file name table.
14148 Use COMP_DIR as the name of the current directory of the
14149 compilation. The result is allocated using xmalloc; the caller is
14150 responsible for freeing it. */
14151static char *
14152file_full_name (int file, struct line_header *lh, const char *comp_dir)
14153{
6a83a1e6
EZ
14154 /* Is the file number a valid index into the line header's file name
14155 table? Remember that file numbers start with one, not zero. */
14156 if (1 <= file && file <= lh->num_file_names)
14157 {
14158 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14159
6a83a1e6
EZ
14160 if (IS_ABSOLUTE_PATH (fe->name))
14161 return xstrdup (fe->name);
14162 else
14163 {
14164 const char *dir;
14165 int dir_len;
14166 char *full_name;
14167
14168 if (fe->dir_index)
14169 dir = lh->include_dirs[fe->dir_index - 1];
14170 else
14171 dir = comp_dir;
14172
14173 if (dir)
14174 {
14175 dir_len = strlen (dir);
14176 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14177 strcpy (full_name, dir);
14178 full_name[dir_len] = '/';
14179 strcpy (full_name + dir_len + 1, fe->name);
14180 return full_name;
14181 }
14182 else
14183 return xstrdup (fe->name);
14184 }
14185 }
2e276125
JB
14186 else
14187 {
6a83a1e6
EZ
14188 /* The compiler produced a bogus file number. We can at least
14189 record the macro definitions made in the file, even if we
14190 won't be able to find the file by name. */
14191 char fake_name[80];
9a619af0 14192
6a83a1e6 14193 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14194
6e70227d 14195 complaint (&symfile_complaints,
6a83a1e6
EZ
14196 _("bad file number in macro information (%d)"),
14197 file);
2e276125 14198
6a83a1e6 14199 return xstrdup (fake_name);
2e276125
JB
14200 }
14201}
14202
14203
14204static struct macro_source_file *
14205macro_start_file (int file, int line,
14206 struct macro_source_file *current_file,
14207 const char *comp_dir,
14208 struct line_header *lh, struct objfile *objfile)
14209{
14210 /* The full name of this source file. */
14211 char *full_name = file_full_name (file, lh, comp_dir);
14212
14213 /* We don't create a macro table for this compilation unit
14214 at all until we actually get a filename. */
14215 if (! pending_macros)
4a146b47 14216 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14217 objfile->macro_cache);
2e276125
JB
14218
14219 if (! current_file)
14220 /* If we have no current file, then this must be the start_file
14221 directive for the compilation unit's main source file. */
14222 current_file = macro_set_main (pending_macros, full_name);
14223 else
14224 current_file = macro_include (current_file, line, full_name);
14225
14226 xfree (full_name);
6e70227d 14227
2e276125
JB
14228 return current_file;
14229}
14230
14231
14232/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14233 followed by a null byte. */
14234static char *
14235copy_string (const char *buf, int len)
14236{
14237 char *s = xmalloc (len + 1);
9a619af0 14238
2e276125
JB
14239 memcpy (s, buf, len);
14240 s[len] = '\0';
2e276125
JB
14241 return s;
14242}
14243
14244
14245static const char *
14246consume_improper_spaces (const char *p, const char *body)
14247{
14248 if (*p == ' ')
14249 {
4d3c2250 14250 complaint (&symfile_complaints,
3e43a32a
MS
14251 _("macro definition contains spaces "
14252 "in formal argument list:\n`%s'"),
4d3c2250 14253 body);
2e276125
JB
14254
14255 while (*p == ' ')
14256 p++;
14257 }
14258
14259 return p;
14260}
14261
14262
14263static void
14264parse_macro_definition (struct macro_source_file *file, int line,
14265 const char *body)
14266{
14267 const char *p;
14268
14269 /* The body string takes one of two forms. For object-like macro
14270 definitions, it should be:
14271
14272 <macro name> " " <definition>
14273
14274 For function-like macro definitions, it should be:
14275
14276 <macro name> "() " <definition>
14277 or
14278 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14279
14280 Spaces may appear only where explicitly indicated, and in the
14281 <definition>.
14282
14283 The Dwarf 2 spec says that an object-like macro's name is always
14284 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14285 the space when the macro's definition is the empty string.
2e276125
JB
14286
14287 The Dwarf 2 spec says that there should be no spaces between the
14288 formal arguments in a function-like macro's formal argument list,
14289 but versions of GCC around March 2002 include spaces after the
14290 commas. */
14291
14292
14293 /* Find the extent of the macro name. The macro name is terminated
14294 by either a space or null character (for an object-like macro) or
14295 an opening paren (for a function-like macro). */
14296 for (p = body; *p; p++)
14297 if (*p == ' ' || *p == '(')
14298 break;
14299
14300 if (*p == ' ' || *p == '\0')
14301 {
14302 /* It's an object-like macro. */
14303 int name_len = p - body;
14304 char *name = copy_string (body, name_len);
14305 const char *replacement;
14306
14307 if (*p == ' ')
14308 replacement = body + name_len + 1;
14309 else
14310 {
4d3c2250 14311 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14312 replacement = body + name_len;
14313 }
6e70227d 14314
2e276125
JB
14315 macro_define_object (file, line, name, replacement);
14316
14317 xfree (name);
14318 }
14319 else if (*p == '(')
14320 {
14321 /* It's a function-like macro. */
14322 char *name = copy_string (body, p - body);
14323 int argc = 0;
14324 int argv_size = 1;
14325 char **argv = xmalloc (argv_size * sizeof (*argv));
14326
14327 p++;
14328
14329 p = consume_improper_spaces (p, body);
14330
14331 /* Parse the formal argument list. */
14332 while (*p && *p != ')')
14333 {
14334 /* Find the extent of the current argument name. */
14335 const char *arg_start = p;
14336
14337 while (*p && *p != ',' && *p != ')' && *p != ' ')
14338 p++;
14339
14340 if (! *p || p == arg_start)
4d3c2250 14341 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14342 else
14343 {
14344 /* Make sure argv has room for the new argument. */
14345 if (argc >= argv_size)
14346 {
14347 argv_size *= 2;
14348 argv = xrealloc (argv, argv_size * sizeof (*argv));
14349 }
14350
14351 argv[argc++] = copy_string (arg_start, p - arg_start);
14352 }
14353
14354 p = consume_improper_spaces (p, body);
14355
14356 /* Consume the comma, if present. */
14357 if (*p == ',')
14358 {
14359 p++;
14360
14361 p = consume_improper_spaces (p, body);
14362 }
14363 }
14364
14365 if (*p == ')')
14366 {
14367 p++;
14368
14369 if (*p == ' ')
14370 /* Perfectly formed definition, no complaints. */
14371 macro_define_function (file, line, name,
6e70227d 14372 argc, (const char **) argv,
2e276125
JB
14373 p + 1);
14374 else if (*p == '\0')
14375 {
14376 /* Complain, but do define it. */
4d3c2250 14377 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14378 macro_define_function (file, line, name,
6e70227d 14379 argc, (const char **) argv,
2e276125
JB
14380 p);
14381 }
14382 else
14383 /* Just complain. */
4d3c2250 14384 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14385 }
14386 else
14387 /* Just complain. */
4d3c2250 14388 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14389
14390 xfree (name);
14391 {
14392 int i;
14393
14394 for (i = 0; i < argc; i++)
14395 xfree (argv[i]);
14396 }
14397 xfree (argv);
14398 }
14399 else
4d3c2250 14400 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14401}
14402
14403
14404static void
14405dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14406 char *comp_dir, bfd *abfd,
e7c27a73 14407 struct dwarf2_cu *cu)
2e276125 14408{
fe1b8b76 14409 gdb_byte *mac_ptr, *mac_end;
2e276125 14410 struct macro_source_file *current_file = 0;
757a13d0
JK
14411 enum dwarf_macinfo_record_type macinfo_type;
14412 int at_commandline;
2e276125 14413
be391dca
TT
14414 dwarf2_read_section (dwarf2_per_objfile->objfile,
14415 &dwarf2_per_objfile->macinfo);
dce234bc 14416 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 14417 {
e2e0b3e5 14418 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
14419 return;
14420 }
14421
757a13d0
JK
14422 /* First pass: Find the name of the base filename.
14423 This filename is needed in order to process all macros whose definition
14424 (or undefinition) comes from the command line. These macros are defined
14425 before the first DW_MACINFO_start_file entry, and yet still need to be
14426 associated to the base file.
14427
14428 To determine the base file name, we scan the macro definitions until we
14429 reach the first DW_MACINFO_start_file entry. We then initialize
14430 CURRENT_FILE accordingly so that any macro definition found before the
14431 first DW_MACINFO_start_file can still be associated to the base file. */
14432
dce234bc
PP
14433 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14434 mac_end = dwarf2_per_objfile->macinfo.buffer
14435 + dwarf2_per_objfile->macinfo.size;
2e276125 14436
757a13d0 14437 do
2e276125 14438 {
2e276125
JB
14439 /* Do we at least have room for a macinfo type byte? */
14440 if (mac_ptr >= mac_end)
14441 {
757a13d0 14442 /* Complaint is printed during the second pass as GDB will probably
3e43a32a
MS
14443 stop the first pass earlier upon finding
14444 DW_MACINFO_start_file. */
757a13d0 14445 break;
2e276125
JB
14446 }
14447
14448 macinfo_type = read_1_byte (abfd, mac_ptr);
14449 mac_ptr++;
14450
14451 switch (macinfo_type)
14452 {
14453 /* A zero macinfo type indicates the end of the macro
14454 information. */
14455 case 0:
757a13d0
JK
14456 break;
14457
14458 case DW_MACINFO_define:
14459 case DW_MACINFO_undef:
14460 /* Only skip the data by MAC_PTR. */
14461 {
14462 unsigned int bytes_read;
14463
14464 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14465 mac_ptr += bytes_read;
9b1c24c8 14466 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14467 mac_ptr += bytes_read;
14468 }
14469 break;
14470
14471 case DW_MACINFO_start_file:
14472 {
14473 unsigned int bytes_read;
14474 int line, file;
14475
14476 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14477 mac_ptr += bytes_read;
14478 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14479 mac_ptr += bytes_read;
14480
3e43a32a
MS
14481 current_file = macro_start_file (file, line, current_file,
14482 comp_dir, lh, cu->objfile);
757a13d0
JK
14483 }
14484 break;
14485
14486 case DW_MACINFO_end_file:
14487 /* No data to skip by MAC_PTR. */
14488 break;
14489
14490 case DW_MACINFO_vendor_ext:
14491 /* Only skip the data by MAC_PTR. */
14492 {
14493 unsigned int bytes_read;
14494
14495 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14496 mac_ptr += bytes_read;
9b1c24c8 14497 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14498 mac_ptr += bytes_read;
14499 }
14500 break;
14501
14502 default:
14503 break;
14504 }
14505 } while (macinfo_type != 0 && current_file == NULL);
14506
14507 /* Second pass: Process all entries.
14508
14509 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14510 command-line macro definitions/undefinitions. This flag is unset when we
14511 reach the first DW_MACINFO_start_file entry. */
14512
dce234bc 14513 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
14514
14515 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14516 GDB is still reading the definitions from command line. First
14517 DW_MACINFO_start_file will need to be ignored as it was already executed
14518 to create CURRENT_FILE for the main source holding also the command line
14519 definitions. On first met DW_MACINFO_start_file this flag is reset to
14520 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14521
14522 at_commandline = 1;
14523
14524 do
14525 {
14526 /* Do we at least have room for a macinfo type byte? */
14527 if (mac_ptr >= mac_end)
14528 {
14529 dwarf2_macros_too_long_complaint ();
14530 break;
14531 }
14532
14533 macinfo_type = read_1_byte (abfd, mac_ptr);
14534 mac_ptr++;
14535
14536 switch (macinfo_type)
14537 {
14538 /* A zero macinfo type indicates the end of the macro
14539 information. */
14540 case 0:
14541 break;
2e276125
JB
14542
14543 case DW_MACINFO_define:
14544 case DW_MACINFO_undef:
14545 {
891d2f0b 14546 unsigned int bytes_read;
2e276125
JB
14547 int line;
14548 char *body;
14549
14550 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14551 mac_ptr += bytes_read;
9b1c24c8 14552 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14553 mac_ptr += bytes_read;
14554
14555 if (! current_file)
757a13d0
JK
14556 {
14557 /* DWARF violation as no main source is present. */
14558 complaint (&symfile_complaints,
14559 _("debug info with no main source gives macro %s "
14560 "on line %d: %s"),
6e70227d
DE
14561 macinfo_type == DW_MACINFO_define ?
14562 _("definition") :
905e0470
PM
14563 macinfo_type == DW_MACINFO_undef ?
14564 _("undefinition") :
14565 _("something-or-other"), line, body);
757a13d0
JK
14566 break;
14567 }
3e43a32a
MS
14568 if ((line == 0 && !at_commandline)
14569 || (line != 0 && at_commandline))
4d3c2250 14570 complaint (&symfile_complaints,
757a13d0
JK
14571 _("debug info gives %s macro %s with %s line %d: %s"),
14572 at_commandline ? _("command-line") : _("in-file"),
905e0470 14573 macinfo_type == DW_MACINFO_define ?
6e70227d 14574 _("definition") :
905e0470
PM
14575 macinfo_type == DW_MACINFO_undef ?
14576 _("undefinition") :
14577 _("something-or-other"),
757a13d0
JK
14578 line == 0 ? _("zero") : _("non-zero"), line, body);
14579
14580 if (macinfo_type == DW_MACINFO_define)
14581 parse_macro_definition (current_file, line, body);
14582 else if (macinfo_type == DW_MACINFO_undef)
14583 macro_undef (current_file, line, body);
2e276125
JB
14584 }
14585 break;
14586
14587 case DW_MACINFO_start_file:
14588 {
891d2f0b 14589 unsigned int bytes_read;
2e276125
JB
14590 int line, file;
14591
14592 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14593 mac_ptr += bytes_read;
14594 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14595 mac_ptr += bytes_read;
14596
3e43a32a
MS
14597 if ((line == 0 && !at_commandline)
14598 || (line != 0 && at_commandline))
757a13d0
JK
14599 complaint (&symfile_complaints,
14600 _("debug info gives source %d included "
14601 "from %s at %s line %d"),
14602 file, at_commandline ? _("command-line") : _("file"),
14603 line == 0 ? _("zero") : _("non-zero"), line);
14604
14605 if (at_commandline)
14606 {
14607 /* This DW_MACINFO_start_file was executed in the pass one. */
14608 at_commandline = 0;
14609 }
14610 else
14611 current_file = macro_start_file (file, line,
14612 current_file, comp_dir,
14613 lh, cu->objfile);
2e276125
JB
14614 }
14615 break;
14616
14617 case DW_MACINFO_end_file:
14618 if (! current_file)
4d3c2250 14619 complaint (&symfile_complaints,
3e43a32a
MS
14620 _("macro debug info has an unmatched "
14621 "`close_file' directive"));
2e276125
JB
14622 else
14623 {
14624 current_file = current_file->included_by;
14625 if (! current_file)
14626 {
14627 enum dwarf_macinfo_record_type next_type;
14628
14629 /* GCC circa March 2002 doesn't produce the zero
14630 type byte marking the end of the compilation
14631 unit. Complain if it's not there, but exit no
14632 matter what. */
14633
14634 /* Do we at least have room for a macinfo type byte? */
14635 if (mac_ptr >= mac_end)
14636 {
4d3c2250 14637 dwarf2_macros_too_long_complaint ();
2e276125
JB
14638 return;
14639 }
14640
14641 /* We don't increment mac_ptr here, so this is just
14642 a look-ahead. */
14643 next_type = read_1_byte (abfd, mac_ptr);
14644 if (next_type != 0)
4d3c2250 14645 complaint (&symfile_complaints,
3e43a32a
MS
14646 _("no terminating 0-type entry for "
14647 "macros in `.debug_macinfo' section"));
2e276125
JB
14648
14649 return;
14650 }
14651 }
14652 break;
14653
14654 case DW_MACINFO_vendor_ext:
14655 {
891d2f0b 14656 unsigned int bytes_read;
2e276125 14657 int constant;
2e276125
JB
14658
14659 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14660 mac_ptr += bytes_read;
e8e80198 14661 read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14662 mac_ptr += bytes_read;
14663
14664 /* We don't recognize any vendor extensions. */
14665 }
14666 break;
14667 }
757a13d0 14668 } while (macinfo_type != 0);
2e276125 14669}
8e19ed76
PS
14670
14671/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 14672 if so return true else false. */
8e19ed76
PS
14673static int
14674attr_form_is_block (struct attribute *attr)
14675{
14676 return (attr == NULL ? 0 :
14677 attr->form == DW_FORM_block1
14678 || attr->form == DW_FORM_block2
14679 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14680 || attr->form == DW_FORM_block
14681 || attr->form == DW_FORM_exprloc);
8e19ed76 14682}
4c2df51b 14683
c6a0999f
JB
14684/* Return non-zero if ATTR's value is a section offset --- classes
14685 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14686 You may use DW_UNSND (attr) to retrieve such offsets.
14687
14688 Section 7.5.4, "Attribute Encodings", explains that no attribute
14689 may have a value that belongs to more than one of these classes; it
14690 would be ambiguous if we did, because we use the same forms for all
14691 of them. */
3690dd37
JB
14692static int
14693attr_form_is_section_offset (struct attribute *attr)
14694{
14695 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14696 || attr->form == DW_FORM_data8
14697 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14698}
14699
14700
14701/* Return non-zero if ATTR's value falls in the 'constant' class, or
14702 zero otherwise. When this function returns true, you can apply
14703 dwarf2_get_attr_constant_value to it.
14704
14705 However, note that for some attributes you must check
14706 attr_form_is_section_offset before using this test. DW_FORM_data4
14707 and DW_FORM_data8 are members of both the constant class, and of
14708 the classes that contain offsets into other debug sections
14709 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14710 that, if an attribute's can be either a constant or one of the
14711 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14712 taken as section offsets, not constants. */
14713static int
14714attr_form_is_constant (struct attribute *attr)
14715{
14716 switch (attr->form)
14717 {
14718 case DW_FORM_sdata:
14719 case DW_FORM_udata:
14720 case DW_FORM_data1:
14721 case DW_FORM_data2:
14722 case DW_FORM_data4:
14723 case DW_FORM_data8:
14724 return 1;
14725 default:
14726 return 0;
14727 }
14728}
14729
8cf6f0b1
TT
14730/* A helper function that fills in a dwarf2_loclist_baton. */
14731
14732static void
14733fill_in_loclist_baton (struct dwarf2_cu *cu,
14734 struct dwarf2_loclist_baton *baton,
14735 struct attribute *attr)
14736{
14737 dwarf2_read_section (dwarf2_per_objfile->objfile,
14738 &dwarf2_per_objfile->loc);
14739
14740 baton->per_cu = cu->per_cu;
14741 gdb_assert (baton->per_cu);
14742 /* We don't know how long the location list is, but make sure we
14743 don't run off the edge of the section. */
14744 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14745 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14746 baton->base_address = cu->base_address;
14747}
14748
4c2df51b
DJ
14749static void
14750dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14751 struct dwarf2_cu *cu)
4c2df51b 14752{
3690dd37 14753 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14754 /* ".debug_loc" may not exist at all, or the offset may be outside
14755 the section. If so, fall through to the complaint in the
14756 other branch. */
9e0ac564
TT
14757 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14758 &dwarf2_per_objfile->loc))
4c2df51b 14759 {
0d53c4c4 14760 struct dwarf2_loclist_baton *baton;
4c2df51b 14761
4a146b47 14762 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14763 sizeof (struct dwarf2_loclist_baton));
4c2df51b 14764
8cf6f0b1 14765 fill_in_loclist_baton (cu, baton, attr);
be391dca 14766
d00adf39 14767 if (cu->base_known == 0)
0d53c4c4 14768 complaint (&symfile_complaints,
3e43a32a
MS
14769 _("Location list used without "
14770 "specifying the CU base address."));
4c2df51b 14771
768a979c 14772 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14773 SYMBOL_LOCATION_BATON (sym) = baton;
14774 }
14775 else
14776 {
14777 struct dwarf2_locexpr_baton *baton;
14778
4a146b47 14779 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14780 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14781 baton->per_cu = cu->per_cu;
14782 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14783
14784 if (attr_form_is_block (attr))
14785 {
14786 /* Note that we're just copying the block's data pointer
14787 here, not the actual data. We're still pointing into the
6502dd73
DJ
14788 info_buffer for SYM's objfile; right now we never release
14789 that buffer, but when we do clean up properly this may
14790 need to change. */
0d53c4c4
DJ
14791 baton->size = DW_BLOCK (attr)->size;
14792 baton->data = DW_BLOCK (attr)->data;
14793 }
14794 else
14795 {
14796 dwarf2_invalid_attrib_class_complaint ("location description",
14797 SYMBOL_NATURAL_NAME (sym));
14798 baton->size = 0;
14799 baton->data = NULL;
14800 }
6e70227d 14801
768a979c 14802 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14803 SYMBOL_LOCATION_BATON (sym) = baton;
14804 }
4c2df51b 14805}
6502dd73 14806
9aa1f1e3
TT
14807/* Return the OBJFILE associated with the compilation unit CU. If CU
14808 came from a separate debuginfo file, then the master objfile is
14809 returned. */
ae0d2f24
UW
14810
14811struct objfile *
14812dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14813{
9291a0cd 14814 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14815
14816 /* Return the master objfile, so that we can report and look up the
14817 correct file containing this variable. */
14818 if (objfile->separate_debug_objfile_backlink)
14819 objfile = objfile->separate_debug_objfile_backlink;
14820
14821 return objfile;
14822}
14823
14824/* Return the address size given in the compilation unit header for CU. */
14825
14826CORE_ADDR
14827dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14828{
14829 if (per_cu->cu)
14830 return per_cu->cu->header.addr_size;
14831 else
14832 {
14833 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14834 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14835 struct dwarf2_per_objfile *per_objfile
14836 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14837 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14838 struct comp_unit_head cu_header;
9a619af0 14839
ae0d2f24
UW
14840 memset (&cu_header, 0, sizeof cu_header);
14841 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14842 return cu_header.addr_size;
14843 }
14844}
14845
9eae7c52
TT
14846/* Return the offset size given in the compilation unit header for CU. */
14847
14848int
14849dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14850{
14851 if (per_cu->cu)
14852 return per_cu->cu->header.offset_size;
14853 else
14854 {
14855 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14856 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14857 struct dwarf2_per_objfile *per_objfile
14858 = objfile_data (objfile, dwarf2_objfile_data_key);
14859 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14860 struct comp_unit_head cu_header;
14861
14862 memset (&cu_header, 0, sizeof cu_header);
14863 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14864 return cu_header.offset_size;
14865 }
14866}
14867
9aa1f1e3
TT
14868/* Return the text offset of the CU. The returned offset comes from
14869 this CU's objfile. If this objfile came from a separate debuginfo
14870 file, then the offset may be different from the corresponding
14871 offset in the parent objfile. */
14872
14873CORE_ADDR
14874dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14875{
bb3fa9d0 14876 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14877
14878 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14879}
14880
348e048f
DE
14881/* Locate the .debug_info compilation unit from CU's objfile which contains
14882 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14883
14884static struct dwarf2_per_cu_data *
c764a876 14885dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14886 struct objfile *objfile)
14887{
14888 struct dwarf2_per_cu_data *this_cu;
14889 int low, high;
14890
ae038cb0
DJ
14891 low = 0;
14892 high = dwarf2_per_objfile->n_comp_units - 1;
14893 while (high > low)
14894 {
14895 int mid = low + (high - low) / 2;
9a619af0 14896
ae038cb0
DJ
14897 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14898 high = mid;
14899 else
14900 low = mid + 1;
14901 }
14902 gdb_assert (low == high);
14903 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14904 {
10b3939b 14905 if (low == 0)
8a3fe4f8
AC
14906 error (_("Dwarf Error: could not find partial DIE containing "
14907 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14908 (long) offset, bfd_get_filename (objfile->obfd));
14909
ae038cb0
DJ
14910 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14911 return dwarf2_per_objfile->all_comp_units[low-1];
14912 }
14913 else
14914 {
14915 this_cu = dwarf2_per_objfile->all_comp_units[low];
14916 if (low == dwarf2_per_objfile->n_comp_units - 1
14917 && offset >= this_cu->offset + this_cu->length)
c764a876 14918 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14919 gdb_assert (offset < this_cu->offset + this_cu->length);
14920 return this_cu;
14921 }
14922}
14923
10b3939b
DJ
14924/* Locate the compilation unit from OBJFILE which is located at exactly
14925 OFFSET. Raises an error on failure. */
14926
ae038cb0 14927static struct dwarf2_per_cu_data *
c764a876 14928dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14929{
14930 struct dwarf2_per_cu_data *this_cu;
9a619af0 14931
ae038cb0
DJ
14932 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14933 if (this_cu->offset != offset)
c764a876 14934 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14935 return this_cu;
14936}
14937
9816fde3 14938/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 14939
9816fde3
JK
14940static void
14941init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 14942{
9816fde3 14943 memset (cu, 0, sizeof (*cu));
93311388
DE
14944 cu->objfile = objfile;
14945 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
14946}
14947
14948/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14949
14950static void
14951prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14952{
14953 struct attribute *attr;
14954
14955 /* Set the language we're debugging. */
14956 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14957 if (attr)
14958 set_cu_language (DW_UNSND (attr), cu);
14959 else
9cded63f
TT
14960 {
14961 cu->language = language_minimal;
14962 cu->language_defn = language_def (cu->language);
14963 }
93311388
DE
14964}
14965
ae038cb0
DJ
14966/* Release one cached compilation unit, CU. We unlink it from the tree
14967 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14968 the caller is responsible for that.
14969 NOTE: DATA is a void * because this function is also used as a
14970 cleanup routine. */
ae038cb0
DJ
14971
14972static void
14973free_one_comp_unit (void *data)
14974{
14975 struct dwarf2_cu *cu = data;
14976
14977 if (cu->per_cu != NULL)
14978 cu->per_cu->cu = NULL;
14979 cu->per_cu = NULL;
14980
14981 obstack_free (&cu->comp_unit_obstack, NULL);
14982
14983 xfree (cu);
14984}
14985
72bf9492 14986/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14987 when we're finished with it. We can't free the pointer itself, but be
14988 sure to unlink it from the cache. Also release any associated storage
14989 and perform cache maintenance.
72bf9492
DJ
14990
14991 Only used during partial symbol parsing. */
14992
14993static void
14994free_stack_comp_unit (void *data)
14995{
14996 struct dwarf2_cu *cu = data;
14997
14998 obstack_free (&cu->comp_unit_obstack, NULL);
14999 cu->partial_dies = NULL;
ae038cb0
DJ
15000
15001 if (cu->per_cu != NULL)
15002 {
15003 /* This compilation unit is on the stack in our caller, so we
15004 should not xfree it. Just unlink it. */
15005 cu->per_cu->cu = NULL;
15006 cu->per_cu = NULL;
15007
15008 /* If we had a per-cu pointer, then we may have other compilation
15009 units loaded, so age them now. */
15010 age_cached_comp_units ();
15011 }
15012}
15013
15014/* Free all cached compilation units. */
15015
15016static void
15017free_cached_comp_units (void *data)
15018{
15019 struct dwarf2_per_cu_data *per_cu, **last_chain;
15020
15021 per_cu = dwarf2_per_objfile->read_in_chain;
15022 last_chain = &dwarf2_per_objfile->read_in_chain;
15023 while (per_cu != NULL)
15024 {
15025 struct dwarf2_per_cu_data *next_cu;
15026
15027 next_cu = per_cu->cu->read_in_chain;
15028
15029 free_one_comp_unit (per_cu->cu);
15030 *last_chain = next_cu;
15031
15032 per_cu = next_cu;
15033 }
15034}
15035
15036/* Increase the age counter on each cached compilation unit, and free
15037 any that are too old. */
15038
15039static void
15040age_cached_comp_units (void)
15041{
15042 struct dwarf2_per_cu_data *per_cu, **last_chain;
15043
15044 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15045 per_cu = dwarf2_per_objfile->read_in_chain;
15046 while (per_cu != NULL)
15047 {
15048 per_cu->cu->last_used ++;
15049 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15050 dwarf2_mark (per_cu->cu);
15051 per_cu = per_cu->cu->read_in_chain;
15052 }
15053
15054 per_cu = dwarf2_per_objfile->read_in_chain;
15055 last_chain = &dwarf2_per_objfile->read_in_chain;
15056 while (per_cu != NULL)
15057 {
15058 struct dwarf2_per_cu_data *next_cu;
15059
15060 next_cu = per_cu->cu->read_in_chain;
15061
15062 if (!per_cu->cu->mark)
15063 {
15064 free_one_comp_unit (per_cu->cu);
15065 *last_chain = next_cu;
15066 }
15067 else
15068 last_chain = &per_cu->cu->read_in_chain;
15069
15070 per_cu = next_cu;
15071 }
15072}
15073
15074/* Remove a single compilation unit from the cache. */
15075
15076static void
15077free_one_cached_comp_unit (void *target_cu)
15078{
15079 struct dwarf2_per_cu_data *per_cu, **last_chain;
15080
15081 per_cu = dwarf2_per_objfile->read_in_chain;
15082 last_chain = &dwarf2_per_objfile->read_in_chain;
15083 while (per_cu != NULL)
15084 {
15085 struct dwarf2_per_cu_data *next_cu;
15086
15087 next_cu = per_cu->cu->read_in_chain;
15088
15089 if (per_cu->cu == target_cu)
15090 {
15091 free_one_comp_unit (per_cu->cu);
15092 *last_chain = next_cu;
15093 break;
15094 }
15095 else
15096 last_chain = &per_cu->cu->read_in_chain;
15097
15098 per_cu = next_cu;
15099 }
15100}
15101
fe3e1990
DJ
15102/* Release all extra memory associated with OBJFILE. */
15103
15104void
15105dwarf2_free_objfile (struct objfile *objfile)
15106{
15107 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15108
15109 if (dwarf2_per_objfile == NULL)
15110 return;
15111
15112 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15113 free_cached_comp_units (NULL);
15114
7b9f3c50
DE
15115 if (dwarf2_per_objfile->quick_file_names_table)
15116 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 15117
fe3e1990
DJ
15118 /* Everything else should be on the objfile obstack. */
15119}
15120
1c379e20
DJ
15121/* A pair of DIE offset and GDB type pointer. We store these
15122 in a hash table separate from the DIEs, and preserve them
15123 when the DIEs are flushed out of cache. */
15124
15125struct dwarf2_offset_and_type
15126{
15127 unsigned int offset;
15128 struct type *type;
15129};
15130
15131/* Hash function for a dwarf2_offset_and_type. */
15132
15133static hashval_t
15134offset_and_type_hash (const void *item)
15135{
15136 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 15137
1c379e20
DJ
15138 return ofs->offset;
15139}
15140
15141/* Equality function for a dwarf2_offset_and_type. */
15142
15143static int
15144offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15145{
15146 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15147 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 15148
1c379e20
DJ
15149 return ofs_lhs->offset == ofs_rhs->offset;
15150}
15151
15152/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
15153 table if necessary. For convenience, return TYPE.
15154
15155 The DIEs reading must have careful ordering to:
15156 * Not cause infite loops trying to read in DIEs as a prerequisite for
15157 reading current DIE.
15158 * Not trying to dereference contents of still incompletely read in types
15159 while reading in other DIEs.
15160 * Enable referencing still incompletely read in types just by a pointer to
15161 the type without accessing its fields.
15162
15163 Therefore caller should follow these rules:
15164 * Try to fetch any prerequisite types we may need to build this DIE type
15165 before building the type and calling set_die_type.
e71ec853 15166 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
15167 possible before fetching more types to complete the current type.
15168 * Make the type as complete as possible before fetching more types. */
1c379e20 15169
f792889a 15170static struct type *
1c379e20
DJ
15171set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15172{
15173 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
15174 struct objfile *objfile = cu->objfile;
15175 htab_t *type_hash_ptr;
1c379e20 15176
b4ba55a1
JB
15177 /* For Ada types, make sure that the gnat-specific data is always
15178 initialized (if not already set). There are a few types where
15179 we should not be doing so, because the type-specific area is
15180 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15181 where the type-specific area is used to store the floatformat).
15182 But this is not a problem, because the gnat-specific information
15183 is actually not needed for these types. */
15184 if (need_gnat_info (cu)
15185 && TYPE_CODE (type) != TYPE_CODE_FUNC
15186 && TYPE_CODE (type) != TYPE_CODE_FLT
15187 && !HAVE_GNAT_AUX_INFO (type))
15188 INIT_GNAT_SPECIFIC (type);
15189
673bfd45
DE
15190 if (cu->per_cu->from_debug_types)
15191 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15192 else
15193 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15194
15195 if (*type_hash_ptr == NULL)
f792889a 15196 {
673bfd45
DE
15197 *type_hash_ptr
15198 = htab_create_alloc_ex (127,
f792889a
DJ
15199 offset_and_type_hash,
15200 offset_and_type_eq,
15201 NULL,
673bfd45 15202 &objfile->objfile_obstack,
f792889a
DJ
15203 hashtab_obstack_allocate,
15204 dummy_obstack_deallocate);
f792889a 15205 }
1c379e20
DJ
15206
15207 ofs.offset = die->offset;
15208 ofs.type = type;
15209 slot = (struct dwarf2_offset_and_type **)
673bfd45 15210 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
15211 if (*slot)
15212 complaint (&symfile_complaints,
15213 _("A problem internal to GDB: DIE 0x%x has type already set"),
15214 die->offset);
673bfd45 15215 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 15216 **slot = ofs;
f792889a 15217 return type;
1c379e20
DJ
15218}
15219
673bfd45
DE
15220/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15221 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
15222
15223static struct type *
673bfd45
DE
15224get_die_type_at_offset (unsigned int offset,
15225 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
15226{
15227 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 15228 htab_t type_hash;
f792889a 15229
673bfd45
DE
15230 if (per_cu->from_debug_types)
15231 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15232 else
15233 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
15234 if (type_hash == NULL)
15235 return NULL;
1c379e20 15236
673bfd45 15237 ofs.offset = offset;
1c379e20
DJ
15238 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15239 if (slot)
15240 return slot->type;
15241 else
15242 return NULL;
15243}
15244
673bfd45
DE
15245/* Look up the type for DIE in the appropriate type_hash table,
15246 or return NULL if DIE does not have a saved type. */
15247
15248static struct type *
15249get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15250{
15251 return get_die_type_at_offset (die->offset, cu->per_cu);
15252}
15253
10b3939b
DJ
15254/* Add a dependence relationship from CU to REF_PER_CU. */
15255
15256static void
15257dwarf2_add_dependence (struct dwarf2_cu *cu,
15258 struct dwarf2_per_cu_data *ref_per_cu)
15259{
15260 void **slot;
15261
15262 if (cu->dependencies == NULL)
15263 cu->dependencies
15264 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15265 NULL, &cu->comp_unit_obstack,
15266 hashtab_obstack_allocate,
15267 dummy_obstack_deallocate);
15268
15269 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15270 if (*slot == NULL)
15271 *slot = ref_per_cu;
15272}
1c379e20 15273
f504f079
DE
15274/* Subroutine of dwarf2_mark to pass to htab_traverse.
15275 Set the mark field in every compilation unit in the
ae038cb0
DJ
15276 cache that we must keep because we are keeping CU. */
15277
10b3939b
DJ
15278static int
15279dwarf2_mark_helper (void **slot, void *data)
15280{
15281 struct dwarf2_per_cu_data *per_cu;
15282
15283 per_cu = (struct dwarf2_per_cu_data *) *slot;
15284 if (per_cu->cu->mark)
15285 return 1;
15286 per_cu->cu->mark = 1;
15287
15288 if (per_cu->cu->dependencies != NULL)
15289 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15290
15291 return 1;
15292}
15293
f504f079
DE
15294/* Set the mark field in CU and in every other compilation unit in the
15295 cache that we must keep because we are keeping CU. */
15296
ae038cb0
DJ
15297static void
15298dwarf2_mark (struct dwarf2_cu *cu)
15299{
15300 if (cu->mark)
15301 return;
15302 cu->mark = 1;
10b3939b
DJ
15303 if (cu->dependencies != NULL)
15304 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
15305}
15306
15307static void
15308dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15309{
15310 while (per_cu)
15311 {
15312 per_cu->cu->mark = 0;
15313 per_cu = per_cu->cu->read_in_chain;
15314 }
72bf9492
DJ
15315}
15316
72bf9492
DJ
15317/* Trivial hash function for partial_die_info: the hash value of a DIE
15318 is its offset in .debug_info for this objfile. */
15319
15320static hashval_t
15321partial_die_hash (const void *item)
15322{
15323 const struct partial_die_info *part_die = item;
9a619af0 15324
72bf9492
DJ
15325 return part_die->offset;
15326}
15327
15328/* Trivial comparison function for partial_die_info structures: two DIEs
15329 are equal if they have the same offset. */
15330
15331static int
15332partial_die_eq (const void *item_lhs, const void *item_rhs)
15333{
15334 const struct partial_die_info *part_die_lhs = item_lhs;
15335 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 15336
72bf9492
DJ
15337 return part_die_lhs->offset == part_die_rhs->offset;
15338}
15339
ae038cb0
DJ
15340static struct cmd_list_element *set_dwarf2_cmdlist;
15341static struct cmd_list_element *show_dwarf2_cmdlist;
15342
15343static void
15344set_dwarf2_cmd (char *args, int from_tty)
15345{
15346 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15347}
15348
15349static void
15350show_dwarf2_cmd (char *args, int from_tty)
6e70227d 15351{
ae038cb0
DJ
15352 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15353}
15354
dce234bc
PP
15355/* If section described by INFO was mmapped, munmap it now. */
15356
15357static void
15358munmap_section_buffer (struct dwarf2_section_info *info)
15359{
15360 if (info->was_mmapped)
15361 {
15362#ifdef HAVE_MMAP
15363 intptr_t begin = (intptr_t) info->buffer;
15364 intptr_t map_begin = begin & ~(pagesize - 1);
15365 size_t map_length = info->size + begin - map_begin;
9a619af0 15366
dce234bc
PP
15367 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15368#else
15369 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 15370 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
15371#endif
15372 }
15373}
15374
15375/* munmap debug sections for OBJFILE, if necessary. */
15376
15377static void
c1bd65d0 15378dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
15379{
15380 struct dwarf2_per_objfile *data = d;
9a619af0 15381
16be1145
DE
15382 /* This is sorted according to the order they're defined in to make it easier
15383 to keep in sync. */
dce234bc
PP
15384 munmap_section_buffer (&data->info);
15385 munmap_section_buffer (&data->abbrev);
15386 munmap_section_buffer (&data->line);
16be1145 15387 munmap_section_buffer (&data->loc);
dce234bc 15388 munmap_section_buffer (&data->macinfo);
16be1145 15389 munmap_section_buffer (&data->str);
dce234bc 15390 munmap_section_buffer (&data->ranges);
16be1145 15391 munmap_section_buffer (&data->types);
dce234bc
PP
15392 munmap_section_buffer (&data->frame);
15393 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
15394 munmap_section_buffer (&data->gdb_index);
15395}
15396
15397\f
ae2de4f8 15398/* The "save gdb-index" command. */
9291a0cd
TT
15399
15400/* The contents of the hash table we create when building the string
15401 table. */
15402struct strtab_entry
15403{
15404 offset_type offset;
15405 const char *str;
15406};
15407
559a7a62
JK
15408/* Hash function for a strtab_entry.
15409
15410 Function is used only during write_hash_table so no index format backward
15411 compatibility is needed. */
b89be57b 15412
9291a0cd
TT
15413static hashval_t
15414hash_strtab_entry (const void *e)
15415{
15416 const struct strtab_entry *entry = e;
559a7a62 15417 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
15418}
15419
15420/* Equality function for a strtab_entry. */
b89be57b 15421
9291a0cd
TT
15422static int
15423eq_strtab_entry (const void *a, const void *b)
15424{
15425 const struct strtab_entry *ea = a;
15426 const struct strtab_entry *eb = b;
15427 return !strcmp (ea->str, eb->str);
15428}
15429
15430/* Create a strtab_entry hash table. */
b89be57b 15431
9291a0cd
TT
15432static htab_t
15433create_strtab (void)
15434{
15435 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15436 xfree, xcalloc, xfree);
15437}
15438
15439/* Add a string to the constant pool. Return the string's offset in
15440 host order. */
b89be57b 15441
9291a0cd
TT
15442static offset_type
15443add_string (htab_t table, struct obstack *cpool, const char *str)
15444{
15445 void **slot;
15446 struct strtab_entry entry;
15447 struct strtab_entry *result;
15448
15449 entry.str = str;
15450 slot = htab_find_slot (table, &entry, INSERT);
15451 if (*slot)
15452 result = *slot;
15453 else
15454 {
15455 result = XNEW (struct strtab_entry);
15456 result->offset = obstack_object_size (cpool);
15457 result->str = str;
15458 obstack_grow_str0 (cpool, str);
15459 *slot = result;
15460 }
15461 return result->offset;
15462}
15463
15464/* An entry in the symbol table. */
15465struct symtab_index_entry
15466{
15467 /* The name of the symbol. */
15468 const char *name;
15469 /* The offset of the name in the constant pool. */
15470 offset_type index_offset;
15471 /* A sorted vector of the indices of all the CUs that hold an object
15472 of this name. */
15473 VEC (offset_type) *cu_indices;
15474};
15475
15476/* The symbol table. This is a power-of-2-sized hash table. */
15477struct mapped_symtab
15478{
15479 offset_type n_elements;
15480 offset_type size;
15481 struct symtab_index_entry **data;
15482};
15483
15484/* Hash function for a symtab_index_entry. */
b89be57b 15485
9291a0cd
TT
15486static hashval_t
15487hash_symtab_entry (const void *e)
15488{
15489 const struct symtab_index_entry *entry = e;
15490 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15491 sizeof (offset_type) * VEC_length (offset_type,
15492 entry->cu_indices),
15493 0);
15494}
15495
15496/* Equality function for a symtab_index_entry. */
b89be57b 15497
9291a0cd
TT
15498static int
15499eq_symtab_entry (const void *a, const void *b)
15500{
15501 const struct symtab_index_entry *ea = a;
15502 const struct symtab_index_entry *eb = b;
15503 int len = VEC_length (offset_type, ea->cu_indices);
15504 if (len != VEC_length (offset_type, eb->cu_indices))
15505 return 0;
15506 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15507 VEC_address (offset_type, eb->cu_indices),
15508 sizeof (offset_type) * len);
15509}
15510
15511/* Destroy a symtab_index_entry. */
b89be57b 15512
9291a0cd
TT
15513static void
15514delete_symtab_entry (void *p)
15515{
15516 struct symtab_index_entry *entry = p;
15517 VEC_free (offset_type, entry->cu_indices);
15518 xfree (entry);
15519}
15520
15521/* Create a hash table holding symtab_index_entry objects. */
b89be57b 15522
9291a0cd 15523static htab_t
3876f04e 15524create_symbol_hash_table (void)
9291a0cd
TT
15525{
15526 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15527 delete_symtab_entry, xcalloc, xfree);
15528}
15529
15530/* Create a new mapped symtab object. */
b89be57b 15531
9291a0cd
TT
15532static struct mapped_symtab *
15533create_mapped_symtab (void)
15534{
15535 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15536 symtab->n_elements = 0;
15537 symtab->size = 1024;
15538 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15539 return symtab;
15540}
15541
15542/* Destroy a mapped_symtab. */
b89be57b 15543
9291a0cd
TT
15544static void
15545cleanup_mapped_symtab (void *p)
15546{
15547 struct mapped_symtab *symtab = p;
15548 /* The contents of the array are freed when the other hash table is
15549 destroyed. */
15550 xfree (symtab->data);
15551 xfree (symtab);
15552}
15553
15554/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
15555 the slot.
15556
15557 Function is used only during write_hash_table so no index format backward
15558 compatibility is needed. */
b89be57b 15559
9291a0cd
TT
15560static struct symtab_index_entry **
15561find_slot (struct mapped_symtab *symtab, const char *name)
15562{
559a7a62 15563 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
15564
15565 index = hash & (symtab->size - 1);
15566 step = ((hash * 17) & (symtab->size - 1)) | 1;
15567
15568 for (;;)
15569 {
15570 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15571 return &symtab->data[index];
15572 index = (index + step) & (symtab->size - 1);
15573 }
15574}
15575
15576/* Expand SYMTAB's hash table. */
b89be57b 15577
9291a0cd
TT
15578static void
15579hash_expand (struct mapped_symtab *symtab)
15580{
15581 offset_type old_size = symtab->size;
15582 offset_type i;
15583 struct symtab_index_entry **old_entries = symtab->data;
15584
15585 symtab->size *= 2;
15586 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15587
15588 for (i = 0; i < old_size; ++i)
15589 {
15590 if (old_entries[i])
15591 {
15592 struct symtab_index_entry **slot = find_slot (symtab,
15593 old_entries[i]->name);
15594 *slot = old_entries[i];
15595 }
15596 }
15597
15598 xfree (old_entries);
15599}
15600
15601/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15602 is the index of the CU in which the symbol appears. */
b89be57b 15603
9291a0cd
TT
15604static void
15605add_index_entry (struct mapped_symtab *symtab, const char *name,
15606 offset_type cu_index)
15607{
15608 struct symtab_index_entry **slot;
15609
15610 ++symtab->n_elements;
15611 if (4 * symtab->n_elements / 3 >= symtab->size)
15612 hash_expand (symtab);
15613
15614 slot = find_slot (symtab, name);
15615 if (!*slot)
15616 {
15617 *slot = XNEW (struct symtab_index_entry);
15618 (*slot)->name = name;
15619 (*slot)->cu_indices = NULL;
15620 }
15621 /* Don't push an index twice. Due to how we add entries we only
15622 have to check the last one. */
15623 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 15624 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
15625 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15626}
15627
15628/* Add a vector of indices to the constant pool. */
b89be57b 15629
9291a0cd 15630static offset_type
3876f04e 15631add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15632 struct symtab_index_entry *entry)
15633{
15634 void **slot;
15635
3876f04e 15636 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15637 if (!*slot)
15638 {
15639 offset_type len = VEC_length (offset_type, entry->cu_indices);
15640 offset_type val = MAYBE_SWAP (len);
15641 offset_type iter;
15642 int i;
15643
15644 *slot = entry;
15645 entry->index_offset = obstack_object_size (cpool);
15646
15647 obstack_grow (cpool, &val, sizeof (val));
15648 for (i = 0;
15649 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15650 ++i)
15651 {
15652 val = MAYBE_SWAP (iter);
15653 obstack_grow (cpool, &val, sizeof (val));
15654 }
15655 }
15656 else
15657 {
15658 struct symtab_index_entry *old_entry = *slot;
15659 entry->index_offset = old_entry->index_offset;
15660 entry = old_entry;
15661 }
15662 return entry->index_offset;
15663}
15664
15665/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15666 constant pool entries going into the obstack CPOOL. */
b89be57b 15667
9291a0cd
TT
15668static void
15669write_hash_table (struct mapped_symtab *symtab,
15670 struct obstack *output, struct obstack *cpool)
15671{
15672 offset_type i;
3876f04e 15673 htab_t symbol_hash_table;
9291a0cd
TT
15674 htab_t str_table;
15675
3876f04e 15676 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15677 str_table = create_strtab ();
3876f04e 15678
9291a0cd
TT
15679 /* We add all the index vectors to the constant pool first, to
15680 ensure alignment is ok. */
15681 for (i = 0; i < symtab->size; ++i)
15682 {
15683 if (symtab->data[i])
3876f04e 15684 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15685 }
15686
15687 /* Now write out the hash table. */
15688 for (i = 0; i < symtab->size; ++i)
15689 {
15690 offset_type str_off, vec_off;
15691
15692 if (symtab->data[i])
15693 {
15694 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15695 vec_off = symtab->data[i]->index_offset;
15696 }
15697 else
15698 {
15699 /* While 0 is a valid constant pool index, it is not valid
15700 to have 0 for both offsets. */
15701 str_off = 0;
15702 vec_off = 0;
15703 }
15704
15705 str_off = MAYBE_SWAP (str_off);
15706 vec_off = MAYBE_SWAP (vec_off);
15707
15708 obstack_grow (output, &str_off, sizeof (str_off));
15709 obstack_grow (output, &vec_off, sizeof (vec_off));
15710 }
15711
15712 htab_delete (str_table);
3876f04e 15713 htab_delete (symbol_hash_table);
9291a0cd
TT
15714}
15715
0a5429f6
DE
15716/* Struct to map psymtab to CU index in the index file. */
15717struct psymtab_cu_index_map
15718{
15719 struct partial_symtab *psymtab;
15720 unsigned int cu_index;
15721};
15722
15723static hashval_t
15724hash_psymtab_cu_index (const void *item)
15725{
15726 const struct psymtab_cu_index_map *map = item;
15727
15728 return htab_hash_pointer (map->psymtab);
15729}
15730
15731static int
15732eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15733{
15734 const struct psymtab_cu_index_map *lhs = item_lhs;
15735 const struct psymtab_cu_index_map *rhs = item_rhs;
15736
15737 return lhs->psymtab == rhs->psymtab;
15738}
15739
15740/* Helper struct for building the address table. */
15741struct addrmap_index_data
15742{
15743 struct objfile *objfile;
15744 struct obstack *addr_obstack;
15745 htab_t cu_index_htab;
15746
15747 /* Non-zero if the previous_* fields are valid.
15748 We can't write an entry until we see the next entry (since it is only then
15749 that we know the end of the entry). */
15750 int previous_valid;
15751 /* Index of the CU in the table of all CUs in the index file. */
15752 unsigned int previous_cu_index;
0963b4bd 15753 /* Start address of the CU. */
0a5429f6
DE
15754 CORE_ADDR previous_cu_start;
15755};
15756
15757/* Write an address entry to OBSTACK. */
b89be57b 15758
9291a0cd 15759static void
0a5429f6
DE
15760add_address_entry (struct objfile *objfile, struct obstack *obstack,
15761 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 15762{
0a5429f6 15763 offset_type cu_index_to_write;
9291a0cd
TT
15764 char addr[8];
15765 CORE_ADDR baseaddr;
15766
15767 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15768
0a5429f6
DE
15769 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15770 obstack_grow (obstack, addr, 8);
15771 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15772 obstack_grow (obstack, addr, 8);
15773 cu_index_to_write = MAYBE_SWAP (cu_index);
15774 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15775}
15776
15777/* Worker function for traversing an addrmap to build the address table. */
15778
15779static int
15780add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15781{
15782 struct addrmap_index_data *data = datap;
15783 struct partial_symtab *pst = obj;
15784 offset_type cu_index;
15785 void **slot;
15786
15787 if (data->previous_valid)
15788 add_address_entry (data->objfile, data->addr_obstack,
15789 data->previous_cu_start, start_addr,
15790 data->previous_cu_index);
15791
15792 data->previous_cu_start = start_addr;
15793 if (pst != NULL)
15794 {
15795 struct psymtab_cu_index_map find_map, *map;
15796 find_map.psymtab = pst;
15797 map = htab_find (data->cu_index_htab, &find_map);
15798 gdb_assert (map != NULL);
15799 data->previous_cu_index = map->cu_index;
15800 data->previous_valid = 1;
15801 }
15802 else
15803 data->previous_valid = 0;
15804
15805 return 0;
15806}
15807
15808/* Write OBJFILE's address map to OBSTACK.
15809 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15810 in the index file. */
15811
15812static void
15813write_address_map (struct objfile *objfile, struct obstack *obstack,
15814 htab_t cu_index_htab)
15815{
15816 struct addrmap_index_data addrmap_index_data;
15817
15818 /* When writing the address table, we have to cope with the fact that
15819 the addrmap iterator only provides the start of a region; we have to
15820 wait until the next invocation to get the start of the next region. */
15821
15822 addrmap_index_data.objfile = objfile;
15823 addrmap_index_data.addr_obstack = obstack;
15824 addrmap_index_data.cu_index_htab = cu_index_htab;
15825 addrmap_index_data.previous_valid = 0;
15826
15827 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15828 &addrmap_index_data);
15829
15830 /* It's highly unlikely the last entry (end address = 0xff...ff)
15831 is valid, but we should still handle it.
15832 The end address is recorded as the start of the next region, but that
15833 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15834 anyway. */
15835 if (addrmap_index_data.previous_valid)
15836 add_address_entry (objfile, obstack,
15837 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15838 addrmap_index_data.previous_cu_index);
9291a0cd
TT
15839}
15840
15841/* Add a list of partial symbols to SYMTAB. */
b89be57b 15842
9291a0cd
TT
15843static void
15844write_psymbols (struct mapped_symtab *symtab,
987d643c 15845 htab_t psyms_seen,
9291a0cd
TT
15846 struct partial_symbol **psymp,
15847 int count,
987d643c
TT
15848 offset_type cu_index,
15849 int is_static)
9291a0cd
TT
15850{
15851 for (; count-- > 0; ++psymp)
15852 {
987d643c
TT
15853 void **slot, *lookup;
15854
9291a0cd
TT
15855 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15856 error (_("Ada is not currently supported by the index"));
987d643c
TT
15857
15858 /* We only want to add a given psymbol once. However, we also
15859 want to account for whether it is global or static. So, we
15860 may add it twice, using slightly different values. */
15861 if (is_static)
15862 {
15863 uintptr_t val = 1 | (uintptr_t) *psymp;
15864
15865 lookup = (void *) val;
15866 }
15867 else
15868 lookup = *psymp;
15869
15870 /* Only add a given psymbol once. */
15871 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15872 if (!*slot)
15873 {
15874 *slot = lookup;
15875 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15876 }
9291a0cd
TT
15877 }
15878}
15879
15880/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15881 exception if there is an error. */
b89be57b 15882
9291a0cd
TT
15883static void
15884write_obstack (FILE *file, struct obstack *obstack)
15885{
15886 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15887 file)
15888 != obstack_object_size (obstack))
15889 error (_("couldn't data write to file"));
15890}
15891
15892/* Unlink a file if the argument is not NULL. */
b89be57b 15893
9291a0cd
TT
15894static void
15895unlink_if_set (void *p)
15896{
15897 char **filename = p;
15898 if (*filename)
15899 unlink (*filename);
15900}
15901
1fd400ff
TT
15902/* A helper struct used when iterating over debug_types. */
15903struct signatured_type_index_data
15904{
15905 struct objfile *objfile;
15906 struct mapped_symtab *symtab;
15907 struct obstack *types_list;
987d643c 15908 htab_t psyms_seen;
1fd400ff
TT
15909 int cu_index;
15910};
15911
15912/* A helper function that writes a single signatured_type to an
15913 obstack. */
b89be57b 15914
1fd400ff
TT
15915static int
15916write_one_signatured_type (void **slot, void *d)
15917{
15918 struct signatured_type_index_data *info = d;
15919 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15920 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15921 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15922 gdb_byte val[8];
15923
15924 write_psymbols (info->symtab,
987d643c 15925 info->psyms_seen,
3e43a32a
MS
15926 info->objfile->global_psymbols.list
15927 + psymtab->globals_offset,
987d643c
TT
15928 psymtab->n_global_syms, info->cu_index,
15929 0);
1fd400ff 15930 write_psymbols (info->symtab,
987d643c 15931 info->psyms_seen,
3e43a32a
MS
15932 info->objfile->static_psymbols.list
15933 + psymtab->statics_offset,
987d643c
TT
15934 psymtab->n_static_syms, info->cu_index,
15935 1);
1fd400ff 15936
b3c8eb43 15937 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
1fd400ff
TT
15938 obstack_grow (info->types_list, val, 8);
15939 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15940 obstack_grow (info->types_list, val, 8);
15941 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15942 obstack_grow (info->types_list, val, 8);
15943
15944 ++info->cu_index;
15945
15946 return 1;
15947}
15948
987d643c
TT
15949/* A cleanup function for an htab_t. */
15950
15951static void
15952cleanup_htab (void *arg)
15953{
15954 htab_delete (arg);
15955}
15956
9291a0cd 15957/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 15958
9291a0cd
TT
15959static void
15960write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15961{
15962 struct cleanup *cleanup;
15963 char *filename, *cleanup_filename;
1fd400ff
TT
15964 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15965 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15966 int i;
15967 FILE *out_file;
15968 struct mapped_symtab *symtab;
15969 offset_type val, size_of_contents, total_len;
15970 struct stat st;
15971 char buf[8];
987d643c 15972 htab_t psyms_seen;
0a5429f6
DE
15973 htab_t cu_index_htab;
15974 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 15975
b4f2f049 15976 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 15977 return;
b4f2f049 15978
9291a0cd
TT
15979 if (dwarf2_per_objfile->using_index)
15980 error (_("Cannot use an index to create the index"));
15981
15982 if (stat (objfile->name, &st) < 0)
7e17e088 15983 perror_with_name (objfile->name);
9291a0cd
TT
15984
15985 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15986 INDEX_SUFFIX, (char *) NULL);
15987 cleanup = make_cleanup (xfree, filename);
15988
15989 out_file = fopen (filename, "wb");
15990 if (!out_file)
15991 error (_("Can't open `%s' for writing"), filename);
15992
15993 cleanup_filename = filename;
15994 make_cleanup (unlink_if_set, &cleanup_filename);
15995
15996 symtab = create_mapped_symtab ();
15997 make_cleanup (cleanup_mapped_symtab, symtab);
15998
15999 obstack_init (&addr_obstack);
16000 make_cleanup_obstack_free (&addr_obstack);
16001
16002 obstack_init (&cu_list);
16003 make_cleanup_obstack_free (&cu_list);
16004
1fd400ff
TT
16005 obstack_init (&types_cu_list);
16006 make_cleanup_obstack_free (&types_cu_list);
16007
987d643c
TT
16008 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16009 NULL, xcalloc, xfree);
16010 make_cleanup (cleanup_htab, psyms_seen);
16011
0a5429f6
DE
16012 /* While we're scanning CU's create a table that maps a psymtab pointer
16013 (which is what addrmap records) to its index (which is what is recorded
16014 in the index file). This will later be needed to write the address
16015 table. */
16016 cu_index_htab = htab_create_alloc (100,
16017 hash_psymtab_cu_index,
16018 eq_psymtab_cu_index,
16019 NULL, xcalloc, xfree);
16020 make_cleanup (cleanup_htab, cu_index_htab);
16021 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16022 xmalloc (sizeof (struct psymtab_cu_index_map)
16023 * dwarf2_per_objfile->n_comp_units);
16024 make_cleanup (xfree, psymtab_cu_index_map);
16025
16026 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
16027 work here. Also, the debug_types entries do not appear in
16028 all_comp_units, but only in their own hash table. */
9291a0cd
TT
16029 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16030 {
3e43a32a
MS
16031 struct dwarf2_per_cu_data *per_cu
16032 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 16033 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 16034 gdb_byte val[8];
0a5429f6
DE
16035 struct psymtab_cu_index_map *map;
16036 void **slot;
9291a0cd
TT
16037
16038 write_psymbols (symtab,
987d643c 16039 psyms_seen,
9291a0cd 16040 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
16041 psymtab->n_global_syms, i,
16042 0);
9291a0cd 16043 write_psymbols (symtab,
987d643c 16044 psyms_seen,
9291a0cd 16045 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
16046 psymtab->n_static_syms, i,
16047 1);
9291a0cd 16048
0a5429f6
DE
16049 map = &psymtab_cu_index_map[i];
16050 map->psymtab = psymtab;
16051 map->cu_index = i;
16052 slot = htab_find_slot (cu_index_htab, map, INSERT);
16053 gdb_assert (slot != NULL);
16054 gdb_assert (*slot == NULL);
16055 *slot = map;
9291a0cd 16056
e254ef6a 16057 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 16058 obstack_grow (&cu_list, val, 8);
e254ef6a 16059 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
16060 obstack_grow (&cu_list, val, 8);
16061 }
16062
0a5429f6
DE
16063 /* Dump the address map. */
16064 write_address_map (objfile, &addr_obstack, cu_index_htab);
16065
1fd400ff
TT
16066 /* Write out the .debug_type entries, if any. */
16067 if (dwarf2_per_objfile->signatured_types)
16068 {
16069 struct signatured_type_index_data sig_data;
16070
16071 sig_data.objfile = objfile;
16072 sig_data.symtab = symtab;
16073 sig_data.types_list = &types_cu_list;
987d643c 16074 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
16075 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16076 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16077 write_one_signatured_type, &sig_data);
16078 }
16079
9291a0cd
TT
16080 obstack_init (&constant_pool);
16081 make_cleanup_obstack_free (&constant_pool);
16082 obstack_init (&symtab_obstack);
16083 make_cleanup_obstack_free (&symtab_obstack);
16084 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16085
16086 obstack_init (&contents);
16087 make_cleanup_obstack_free (&contents);
1fd400ff 16088 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
16089 total_len = size_of_contents;
16090
16091 /* The version number. */
559a7a62 16092 val = MAYBE_SWAP (5);
9291a0cd
TT
16093 obstack_grow (&contents, &val, sizeof (val));
16094
16095 /* The offset of the CU list from the start of the file. */
16096 val = MAYBE_SWAP (total_len);
16097 obstack_grow (&contents, &val, sizeof (val));
16098 total_len += obstack_object_size (&cu_list);
16099
1fd400ff
TT
16100 /* The offset of the types CU list from the start of the file. */
16101 val = MAYBE_SWAP (total_len);
16102 obstack_grow (&contents, &val, sizeof (val));
16103 total_len += obstack_object_size (&types_cu_list);
16104
9291a0cd
TT
16105 /* The offset of the address table from the start of the file. */
16106 val = MAYBE_SWAP (total_len);
16107 obstack_grow (&contents, &val, sizeof (val));
16108 total_len += obstack_object_size (&addr_obstack);
16109
16110 /* The offset of the symbol table from the start of the file. */
16111 val = MAYBE_SWAP (total_len);
16112 obstack_grow (&contents, &val, sizeof (val));
16113 total_len += obstack_object_size (&symtab_obstack);
16114
16115 /* The offset of the constant pool from the start of the file. */
16116 val = MAYBE_SWAP (total_len);
16117 obstack_grow (&contents, &val, sizeof (val));
16118 total_len += obstack_object_size (&constant_pool);
16119
16120 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16121
16122 write_obstack (out_file, &contents);
16123 write_obstack (out_file, &cu_list);
1fd400ff 16124 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
16125 write_obstack (out_file, &addr_obstack);
16126 write_obstack (out_file, &symtab_obstack);
16127 write_obstack (out_file, &constant_pool);
16128
16129 fclose (out_file);
16130
16131 /* We want to keep the file, so we set cleanup_filename to NULL
16132 here. See unlink_if_set. */
16133 cleanup_filename = NULL;
16134
16135 do_cleanups (cleanup);
16136}
16137
90476074
TT
16138/* Implementation of the `save gdb-index' command.
16139
16140 Note that the file format used by this command is documented in the
16141 GDB manual. Any changes here must be documented there. */
11570e71 16142
9291a0cd
TT
16143static void
16144save_gdb_index_command (char *arg, int from_tty)
16145{
16146 struct objfile *objfile;
16147
16148 if (!arg || !*arg)
96d19272 16149 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
16150
16151 ALL_OBJFILES (objfile)
16152 {
16153 struct stat st;
16154
16155 /* If the objfile does not correspond to an actual file, skip it. */
16156 if (stat (objfile->name, &st) < 0)
16157 continue;
16158
16159 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16160 if (dwarf2_per_objfile)
16161 {
16162 volatile struct gdb_exception except;
16163
16164 TRY_CATCH (except, RETURN_MASK_ERROR)
16165 {
16166 write_psymtabs_to_index (objfile, arg);
16167 }
16168 if (except.reason < 0)
16169 exception_fprintf (gdb_stderr, except,
16170 _("Error while writing index for `%s': "),
16171 objfile->name);
16172 }
16173 }
dce234bc
PP
16174}
16175
9291a0cd
TT
16176\f
16177
9eae7c52
TT
16178int dwarf2_always_disassemble;
16179
16180static void
16181show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16182 struct cmd_list_element *c, const char *value)
16183{
3e43a32a
MS
16184 fprintf_filtered (file,
16185 _("Whether to always disassemble "
16186 "DWARF expressions is %s.\n"),
9eae7c52
TT
16187 value);
16188}
16189
6502dd73
DJ
16190void _initialize_dwarf2_read (void);
16191
16192void
16193_initialize_dwarf2_read (void)
16194{
96d19272
JK
16195 struct cmd_list_element *c;
16196
dce234bc 16197 dwarf2_objfile_data_key
c1bd65d0 16198 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 16199
1bedd215
AC
16200 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16201Set DWARF 2 specific variables.\n\
16202Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16203 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16204 0/*allow-unknown*/, &maintenance_set_cmdlist);
16205
1bedd215
AC
16206 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16207Show DWARF 2 specific variables\n\
16208Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16209 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16210 0/*allow-unknown*/, &maintenance_show_cmdlist);
16211
16212 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
16213 &dwarf2_max_cache_age, _("\
16214Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16215Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16216A higher limit means that cached compilation units will be stored\n\
16217in memory longer, and more total memory will be used. Zero disables\n\
16218caching, which can slow down startup."),
2c5b56ce 16219 NULL,
920d2a44 16220 show_dwarf2_max_cache_age,
2c5b56ce 16221 &set_dwarf2_cmdlist,
ae038cb0 16222 &show_dwarf2_cmdlist);
d97bc12b 16223
9eae7c52
TT
16224 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16225 &dwarf2_always_disassemble, _("\
16226Set whether `info address' always disassembles DWARF expressions."), _("\
16227Show whether `info address' always disassembles DWARF expressions."), _("\
16228When enabled, DWARF expressions are always printed in an assembly-like\n\
16229syntax. When disabled, expressions will be printed in a more\n\
16230conversational style, when possible."),
16231 NULL,
16232 show_dwarf2_always_disassemble,
16233 &set_dwarf2_cmdlist,
16234 &show_dwarf2_cmdlist);
16235
d97bc12b
DE
16236 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16237Set debugging of the dwarf2 DIE reader."), _("\
16238Show debugging of the dwarf2 DIE reader."), _("\
16239When enabled (non-zero), DIEs are dumped after they are read in.\n\
16240The value is the maximum depth to print."),
16241 NULL,
16242 NULL,
16243 &setdebuglist, &showdebuglist);
9291a0cd 16244
96d19272 16245 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 16246 _("\
fc1a9d6e 16247Save a gdb-index file.\n\
11570e71 16248Usage: save gdb-index DIRECTORY"),
96d19272
JK
16249 &save_cmdlist);
16250 set_cmd_completer (c, filename_completer);
6502dd73 16251}
This page took 2.145424 seconds and 4 git commands to generate.