gdb/
[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
900e11f9
JK
122/* When non-zero, cross-check physname against demangler. */
123static int check_physname = 0;
124
dce234bc
PP
125static int pagesize;
126
df8a16a1
DJ
127/* When set, the file that we're processing is known to have debugging
128 info for C++ namespaces. GCC 3.3.x did not produce this information,
129 but later versions do. */
130
131static int processing_has_namespace_info;
132
6502dd73
DJ
133static const struct objfile_data *dwarf2_objfile_data_key;
134
dce234bc
PP
135struct dwarf2_section_info
136{
137 asection *asection;
138 gdb_byte *buffer;
139 bfd_size_type size;
b315ab21
TG
140 /* Not NULL if the section was actually mmapped. */
141 void *map_addr;
142 /* Page aligned size of mmapped area. */
143 bfd_size_type map_len;
be391dca
TT
144 /* True if we have tried to read this section. */
145 int readin;
dce234bc
PP
146};
147
8b70b953
TT
148typedef struct dwarf2_section_info dwarf2_section_info_def;
149DEF_VEC_O (dwarf2_section_info_def);
150
9291a0cd
TT
151/* All offsets in the index are of this type. It must be
152 architecture-independent. */
153typedef uint32_t offset_type;
154
155DEF_VEC_I (offset_type);
156
157/* A description of the mapped index. The file format is described in
158 a comment by the code that writes the index. */
159struct mapped_index
160{
559a7a62
JK
161 /* Index data format version. */
162 int version;
163
9291a0cd
TT
164 /* The total length of the buffer. */
165 off_t total_size;
b11b1f88 166
9291a0cd
TT
167 /* A pointer to the address table data. */
168 const gdb_byte *address_table;
b11b1f88 169
9291a0cd
TT
170 /* Size of the address table data in bytes. */
171 offset_type address_table_size;
b11b1f88 172
3876f04e
DE
173 /* The symbol table, implemented as a hash table. */
174 const offset_type *symbol_table;
b11b1f88 175
9291a0cd 176 /* Size in slots, each slot is 2 offset_types. */
3876f04e 177 offset_type symbol_table_slots;
b11b1f88 178
9291a0cd
TT
179 /* A pointer to the constant pool. */
180 const char *constant_pool;
181};
182
6502dd73
DJ
183struct dwarf2_per_objfile
184{
dce234bc
PP
185 struct dwarf2_section_info info;
186 struct dwarf2_section_info abbrev;
187 struct dwarf2_section_info line;
dce234bc
PP
188 struct dwarf2_section_info loc;
189 struct dwarf2_section_info macinfo;
cf2c3c16 190 struct dwarf2_section_info macro;
dce234bc
PP
191 struct dwarf2_section_info str;
192 struct dwarf2_section_info ranges;
193 struct dwarf2_section_info frame;
194 struct dwarf2_section_info eh_frame;
9291a0cd 195 struct dwarf2_section_info gdb_index;
ae038cb0 196
8b70b953
TT
197 VEC (dwarf2_section_info_def) *types;
198
be391dca
TT
199 /* Back link. */
200 struct objfile *objfile;
201
10b3939b
DJ
202 /* A list of all the compilation units. This is used to locate
203 the target compilation unit of a particular reference. */
ae038cb0
DJ
204 struct dwarf2_per_cu_data **all_comp_units;
205
206 /* The number of compilation units in ALL_COMP_UNITS. */
207 int n_comp_units;
208
1fd400ff
TT
209 /* The number of .debug_types-related CUs. */
210 int n_type_comp_units;
211
212 /* The .debug_types-related CUs. */
213 struct dwarf2_per_cu_data **type_comp_units;
214
ae038cb0
DJ
215 /* A chain of compilation units that are currently read in, so that
216 they can be freed later. */
217 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 218
348e048f
DE
219 /* A table mapping .debug_types signatures to its signatured_type entry.
220 This is NULL if the .debug_types section hasn't been read in yet. */
221 htab_t signatured_types;
222
72dca2f5
FR
223 /* A flag indicating wether this objfile has a section loaded at a
224 VMA of 0. */
225 int has_section_at_zero;
9291a0cd 226
ae2de4f8
DE
227 /* True if we are using the mapped index,
228 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
229 unsigned char using_index;
230
ae2de4f8 231 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 232 struct mapped_index *index_table;
98bfdba5 233
7b9f3c50
DE
234 /* When using index_table, this keeps track of all quick_file_names entries.
235 TUs can share line table entries with CUs or other TUs, and there can be
236 a lot more TUs than unique line tables, so we maintain a separate table
237 of all line table entries to support the sharing. */
238 htab_t quick_file_names_table;
239
98bfdba5
PA
240 /* Set during partial symbol reading, to prevent queueing of full
241 symbols. */
242 int reading_partial_symbols;
673bfd45
DE
243
244 /* Table mapping type .debug_info DIE offsets to types.
245 This is NULL if not allocated yet.
246 It (currently) makes sense to allocate debug_types_type_hash lazily.
247 To keep things simple we allocate both lazily. */
248 htab_t debug_info_type_hash;
249
250 /* Table mapping type .debug_types DIE offsets to types.
251 This is NULL if not allocated yet. */
252 htab_t debug_types_type_hash;
6502dd73
DJ
253};
254
255static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 256
251d32d9 257/* Default names of the debugging sections. */
c906108c 258
233a11ab
CS
259/* Note that if the debugging section has been compressed, it might
260 have a name like .zdebug_info. */
261
251d32d9
TG
262static const struct dwarf2_debug_sections dwarf2_elf_names = {
263 { ".debug_info", ".zdebug_info" },
264 { ".debug_abbrev", ".zdebug_abbrev" },
265 { ".debug_line", ".zdebug_line" },
266 { ".debug_loc", ".zdebug_loc" },
267 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 268 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
269 { ".debug_str", ".zdebug_str" },
270 { ".debug_ranges", ".zdebug_ranges" },
271 { ".debug_types", ".zdebug_types" },
272 { ".debug_frame", ".zdebug_frame" },
273 { ".eh_frame", NULL },
24d3216f
TT
274 { ".gdb_index", ".zgdb_index" },
275 23
251d32d9 276};
c906108c
SS
277
278/* local data types */
279
0963b4bd 280/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
281#ifndef ABBREV_HASH_SIZE
282#define ABBREV_HASH_SIZE 121
283#endif
284
107d2387
AC
285/* The data in a compilation unit header, after target2host
286 translation, looks like this. */
c906108c 287struct comp_unit_head
a738430d 288{
c764a876 289 unsigned int length;
a738430d 290 short version;
a738430d
MK
291 unsigned char addr_size;
292 unsigned char signed_addr_p;
9cbfa09e 293 unsigned int abbrev_offset;
57349743 294
a738430d
MK
295 /* Size of file offsets; either 4 or 8. */
296 unsigned int offset_size;
57349743 297
a738430d
MK
298 /* Size of the length field; either 4 or 12. */
299 unsigned int initial_length_size;
57349743 300
a738430d
MK
301 /* Offset to the first byte of this compilation unit header in the
302 .debug_info section, for resolving relative reference dies. */
303 unsigned int offset;
57349743 304
d00adf39
DE
305 /* Offset to first die in this cu from the start of the cu.
306 This will be the first byte following the compilation unit header. */
307 unsigned int first_die_offset;
a738430d 308};
c906108c 309
3da10d80
KS
310/* Type used for delaying computation of method physnames.
311 See comments for compute_delayed_physnames. */
312struct delayed_method_info
313{
314 /* The type to which the method is attached, i.e., its parent class. */
315 struct type *type;
316
317 /* The index of the method in the type's function fieldlists. */
318 int fnfield_index;
319
320 /* The index of the method in the fieldlist. */
321 int index;
322
323 /* The name of the DIE. */
324 const char *name;
325
326 /* The DIE associated with this method. */
327 struct die_info *die;
328};
329
330typedef struct delayed_method_info delayed_method_info;
331DEF_VEC_O (delayed_method_info);
332
e7c27a73
DJ
333/* Internal state when decoding a particular compilation unit. */
334struct dwarf2_cu
335{
336 /* The objfile containing this compilation unit. */
337 struct objfile *objfile;
338
d00adf39 339 /* The header of the compilation unit. */
e7c27a73 340 struct comp_unit_head header;
e142c38c 341
d00adf39
DE
342 /* Base address of this compilation unit. */
343 CORE_ADDR base_address;
344
345 /* Non-zero if base_address has been set. */
346 int base_known;
347
e142c38c
DJ
348 struct function_range *first_fn, *last_fn, *cached_fn;
349
350 /* The language we are debugging. */
351 enum language language;
352 const struct language_defn *language_defn;
353
b0f35d58
DL
354 const char *producer;
355
e142c38c
DJ
356 /* The generic symbol table building routines have separate lists for
357 file scope symbols and all all other scopes (local scopes). So
358 we need to select the right one to pass to add_symbol_to_list().
359 We do it by keeping a pointer to the correct list in list_in_scope.
360
361 FIXME: The original dwarf code just treated the file scope as the
362 first local scope, and all other local scopes as nested local
363 scopes, and worked fine. Check to see if we really need to
364 distinguish these in buildsym.c. */
365 struct pending **list_in_scope;
366
f3dd6933
DJ
367 /* DWARF abbreviation table associated with this compilation unit. */
368 struct abbrev_info **dwarf2_abbrevs;
369
370 /* Storage for the abbrev table. */
371 struct obstack abbrev_obstack;
72bf9492
DJ
372
373 /* Hash table holding all the loaded partial DIEs. */
374 htab_t partial_dies;
375
376 /* Storage for things with the same lifetime as this read-in compilation
377 unit, including partial DIEs. */
378 struct obstack comp_unit_obstack;
379
ae038cb0
DJ
380 /* When multiple dwarf2_cu structures are living in memory, this field
381 chains them all together, so that they can be released efficiently.
382 We will probably also want a generation counter so that most-recently-used
383 compilation units are cached... */
384 struct dwarf2_per_cu_data *read_in_chain;
385
386 /* Backchain to our per_cu entry if the tree has been built. */
387 struct dwarf2_per_cu_data *per_cu;
388
389 /* How many compilation units ago was this CU last referenced? */
390 int last_used;
391
10b3939b 392 /* A hash table of die offsets for following references. */
51545339 393 htab_t die_hash;
10b3939b
DJ
394
395 /* Full DIEs if read in. */
396 struct die_info *dies;
397
398 /* A set of pointers to dwarf2_per_cu_data objects for compilation
399 units referenced by this one. Only set during full symbol processing;
400 partial symbol tables do not have dependencies. */
401 htab_t dependencies;
402
cb1df416
DJ
403 /* Header data from the line table, during full symbol processing. */
404 struct line_header *line_header;
405
3da10d80
KS
406 /* A list of methods which need to have physnames computed
407 after all type information has been read. */
408 VEC (delayed_method_info) *method_list;
409
ae038cb0
DJ
410 /* Mark used when releasing cached dies. */
411 unsigned int mark : 1;
412
413 /* This flag will be set if this compilation unit might include
414 inter-compilation-unit references. */
415 unsigned int has_form_ref_addr : 1;
416
72bf9492
DJ
417 /* This flag will be set if this compilation unit includes any
418 DW_TAG_namespace DIEs. If we know that there are explicit
419 DIEs for namespaces, we don't need to try to infer them
420 from mangled names. */
421 unsigned int has_namespace_info : 1;
8be455d7
JK
422
423 /* This CU references .debug_loc. See the symtab->locations_valid field.
424 This test is imperfect as there may exist optimized debug code not using
425 any location list and still facing inlining issues if handled as
426 unoptimized code. For a future better test see GCC PR other/32998. */
427
428 unsigned int has_loclist : 1;
e7c27a73
DJ
429};
430
10b3939b
DJ
431/* Persistent data held for a compilation unit, even when not
432 processing it. We put a pointer to this structure in the
433 read_symtab_private field of the psymtab. If we encounter
434 inter-compilation-unit references, we also maintain a sorted
435 list of all compilation units. */
436
ae038cb0
DJ
437struct dwarf2_per_cu_data
438{
348e048f 439 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 440 bytes should suffice to store the length of any compilation unit
45452591
DE
441 - if it doesn't, GDB will fall over anyway.
442 NOTE: Unlike comp_unit_head.length, this length includes
443 initial_length_size. */
c764a876 444 unsigned int offset;
348e048f 445 unsigned int length : 29;
ae038cb0
DJ
446
447 /* Flag indicating this compilation unit will be read in before
448 any of the current compilation units are processed. */
c764a876 449 unsigned int queued : 1;
ae038cb0 450
5afb4e99
DJ
451 /* This flag will be set if we need to load absolutely all DIEs
452 for this compilation unit, instead of just the ones we think
453 are interesting. It gets set if we look for a DIE in the
454 hash table and don't find it. */
455 unsigned int load_all_dies : 1;
456
8b70b953
TT
457 /* Non-null if this CU is from .debug_types; in which case it points
458 to the section. Otherwise it's from .debug_info. */
459 struct dwarf2_section_info *debug_type_section;
348e048f 460
17ea53c3
JK
461 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
462 of the CU cache it gets reset to NULL again. */
ae038cb0 463 struct dwarf2_cu *cu;
1c379e20 464
9291a0cd
TT
465 /* The corresponding objfile. */
466 struct objfile *objfile;
467
468 /* When using partial symbol tables, the 'psymtab' field is active.
469 Otherwise the 'quick' field is active. */
470 union
471 {
472 /* The partial symbol table associated with this compilation unit,
473 or NULL for partial units (which do not have an associated
474 symtab). */
475 struct partial_symtab *psymtab;
476
477 /* Data needed by the "quick" functions. */
478 struct dwarf2_per_cu_quick_data *quick;
479 } v;
ae038cb0
DJ
480};
481
348e048f
DE
482/* Entry in the signatured_types hash table. */
483
484struct signatured_type
485{
486 ULONGEST signature;
487
348e048f
DE
488 /* Offset in .debug_types of the type defined by this TU. */
489 unsigned int type_offset;
490
491 /* The CU(/TU) of this type. */
492 struct dwarf2_per_cu_data per_cu;
493};
494
0963b4bd
MS
495/* Struct used to pass misc. parameters to read_die_and_children, et
496 al. which are used for both .debug_info and .debug_types dies.
497 All parameters here are unchanging for the life of the call. This
498 struct exists to abstract away the constant parameters of die
499 reading. */
93311388
DE
500
501struct die_reader_specs
502{
503 /* The bfd of this objfile. */
504 bfd* abfd;
505
506 /* The CU of the DIE we are parsing. */
507 struct dwarf2_cu *cu;
508
509 /* Pointer to start of section buffer.
510 This is either the start of .debug_info or .debug_types. */
511 const gdb_byte *buffer;
512};
513
debd256d
JB
514/* The line number information for a compilation unit (found in the
515 .debug_line section) begins with a "statement program header",
516 which contains the following information. */
517struct line_header
518{
519 unsigned int total_length;
520 unsigned short version;
521 unsigned int header_length;
522 unsigned char minimum_instruction_length;
2dc7f7b3 523 unsigned char maximum_ops_per_instruction;
debd256d
JB
524 unsigned char default_is_stmt;
525 int line_base;
526 unsigned char line_range;
527 unsigned char opcode_base;
528
529 /* standard_opcode_lengths[i] is the number of operands for the
530 standard opcode whose value is i. This means that
531 standard_opcode_lengths[0] is unused, and the last meaningful
532 element is standard_opcode_lengths[opcode_base - 1]. */
533 unsigned char *standard_opcode_lengths;
534
535 /* The include_directories table. NOTE! These strings are not
536 allocated with xmalloc; instead, they are pointers into
537 debug_line_buffer. If you try to free them, `free' will get
538 indigestion. */
539 unsigned int num_include_dirs, include_dirs_size;
540 char **include_dirs;
541
542 /* The file_names table. NOTE! These strings are not allocated
543 with xmalloc; instead, they are pointers into debug_line_buffer.
544 Don't try to free them directly. */
545 unsigned int num_file_names, file_names_size;
546 struct file_entry
c906108c 547 {
debd256d
JB
548 char *name;
549 unsigned int dir_index;
550 unsigned int mod_time;
551 unsigned int length;
aaa75496 552 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 553 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
554 } *file_names;
555
556 /* The start and end of the statement program following this
6502dd73 557 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 558 gdb_byte *statement_program_start, *statement_program_end;
debd256d 559};
c906108c
SS
560
561/* When we construct a partial symbol table entry we only
0963b4bd 562 need this much information. */
c906108c
SS
563struct partial_die_info
564 {
72bf9492 565 /* Offset of this DIE. */
c906108c 566 unsigned int offset;
72bf9492
DJ
567
568 /* DWARF-2 tag for this DIE. */
569 ENUM_BITFIELD(dwarf_tag) tag : 16;
570
72bf9492
DJ
571 /* Assorted flags describing the data found in this DIE. */
572 unsigned int has_children : 1;
573 unsigned int is_external : 1;
574 unsigned int is_declaration : 1;
575 unsigned int has_type : 1;
576 unsigned int has_specification : 1;
577 unsigned int has_pc_info : 1;
578
579 /* Flag set if the SCOPE field of this structure has been
580 computed. */
581 unsigned int scope_set : 1;
582
fa4028e9
JB
583 /* Flag set if the DIE has a byte_size attribute. */
584 unsigned int has_byte_size : 1;
585
98bfdba5
PA
586 /* Flag set if any of the DIE's children are template arguments. */
587 unsigned int has_template_arguments : 1;
588
abc72ce4
DE
589 /* Flag set if fixup_partial_die has been called on this die. */
590 unsigned int fixup_called : 1;
591
72bf9492 592 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 593 sometimes a default name for unnamed DIEs. */
c906108c 594 char *name;
72bf9492 595
abc72ce4
DE
596 /* The linkage name, if present. */
597 const char *linkage_name;
598
72bf9492
DJ
599 /* The scope to prepend to our children. This is generally
600 allocated on the comp_unit_obstack, so will disappear
601 when this compilation unit leaves the cache. */
602 char *scope;
603
604 /* The location description associated with this DIE, if any. */
605 struct dwarf_block *locdesc;
606
607 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
608 CORE_ADDR lowpc;
609 CORE_ADDR highpc;
72bf9492 610
93311388 611 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 612 DW_AT_sibling, if any. */
abc72ce4
DE
613 /* NOTE: This member isn't strictly necessary, read_partial_die could
614 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 615 gdb_byte *sibling;
72bf9492
DJ
616
617 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
618 DW_AT_specification (or DW_AT_abstract_origin or
619 DW_AT_extension). */
620 unsigned int spec_offset;
621
622 /* Pointers to this DIE's parent, first child, and next sibling,
623 if any. */
624 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
625 };
626
0963b4bd 627/* This data structure holds the information of an abbrev. */
c906108c
SS
628struct abbrev_info
629 {
630 unsigned int number; /* number identifying abbrev */
631 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
632 unsigned short has_children; /* boolean */
633 unsigned short num_attrs; /* number of attributes */
c906108c
SS
634 struct attr_abbrev *attrs; /* an array of attribute descriptions */
635 struct abbrev_info *next; /* next in chain */
636 };
637
638struct attr_abbrev
639 {
9d25dd43
DE
640 ENUM_BITFIELD(dwarf_attribute) name : 16;
641 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
642 };
643
0963b4bd 644/* Attributes have a name and a value. */
b60c80d6
DJ
645struct attribute
646 {
9d25dd43 647 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
648 ENUM_BITFIELD(dwarf_form) form : 15;
649
650 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
651 field should be in u.str (existing only for DW_STRING) but it is kept
652 here for better struct attribute alignment. */
653 unsigned int string_is_canonical : 1;
654
b60c80d6
DJ
655 union
656 {
657 char *str;
658 struct dwarf_block *blk;
43bbcdc2
PH
659 ULONGEST unsnd;
660 LONGEST snd;
b60c80d6 661 CORE_ADDR addr;
348e048f 662 struct signatured_type *signatured_type;
b60c80d6
DJ
663 }
664 u;
665 };
666
0963b4bd 667/* This data structure holds a complete die structure. */
c906108c
SS
668struct die_info
669 {
76815b17
DE
670 /* DWARF-2 tag for this DIE. */
671 ENUM_BITFIELD(dwarf_tag) tag : 16;
672
673 /* Number of attributes */
98bfdba5
PA
674 unsigned char num_attrs;
675
676 /* True if we're presently building the full type name for the
677 type derived from this DIE. */
678 unsigned char building_fullname : 1;
76815b17
DE
679
680 /* Abbrev number */
681 unsigned int abbrev;
682
93311388 683 /* Offset in .debug_info or .debug_types section. */
76815b17 684 unsigned int offset;
78ba4af6
JB
685
686 /* The dies in a compilation unit form an n-ary tree. PARENT
687 points to this die's parent; CHILD points to the first child of
688 this node; and all the children of a given node are chained
4950bc1c 689 together via their SIBLING fields. */
639d11d3
DC
690 struct die_info *child; /* Its first child, if any. */
691 struct die_info *sibling; /* Its next sibling, if any. */
692 struct die_info *parent; /* Its parent, if any. */
c906108c 693
b60c80d6
DJ
694 /* An array of attributes, with NUM_ATTRS elements. There may be
695 zero, but it's not common and zero-sized arrays are not
696 sufficiently portable C. */
697 struct attribute attrs[1];
c906108c
SS
698 };
699
5fb290d7
DJ
700struct function_range
701{
702 const char *name;
703 CORE_ADDR lowpc, highpc;
704 int seen_line;
705 struct function_range *next;
706};
707
0963b4bd 708/* Get at parts of an attribute structure. */
c906108c
SS
709
710#define DW_STRING(attr) ((attr)->u.str)
8285870a 711#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
712#define DW_UNSND(attr) ((attr)->u.unsnd)
713#define DW_BLOCK(attr) ((attr)->u.blk)
714#define DW_SND(attr) ((attr)->u.snd)
715#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 716#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 717
0963b4bd 718/* Blocks are a bunch of untyped bytes. */
c906108c
SS
719struct dwarf_block
720 {
721 unsigned int size;
fe1b8b76 722 gdb_byte *data;
c906108c
SS
723 };
724
c906108c
SS
725#ifndef ATTR_ALLOC_CHUNK
726#define ATTR_ALLOC_CHUNK 4
727#endif
728
c906108c
SS
729/* Allocate fields for structs, unions and enums in this size. */
730#ifndef DW_FIELD_ALLOC_CHUNK
731#define DW_FIELD_ALLOC_CHUNK 4
732#endif
733
c906108c
SS
734/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
735 but this would require a corresponding change in unpack_field_as_long
736 and friends. */
737static int bits_per_byte = 8;
738
739/* The routines that read and process dies for a C struct or C++ class
740 pass lists of data member fields and lists of member function fields
741 in an instance of a field_info structure, as defined below. */
742struct field_info
c5aa993b 743 {
0963b4bd 744 /* List of data member and baseclasses fields. */
c5aa993b
JM
745 struct nextfield
746 {
747 struct nextfield *next;
748 int accessibility;
749 int virtuality;
750 struct field field;
751 }
7d0ccb61 752 *fields, *baseclasses;
c906108c 753
7d0ccb61 754 /* Number of fields (including baseclasses). */
c5aa993b 755 int nfields;
c906108c 756
c5aa993b
JM
757 /* Number of baseclasses. */
758 int nbaseclasses;
c906108c 759
c5aa993b
JM
760 /* Set if the accesibility of one of the fields is not public. */
761 int non_public_fields;
c906108c 762
c5aa993b
JM
763 /* Member function fields array, entries are allocated in the order they
764 are encountered in the object file. */
765 struct nextfnfield
766 {
767 struct nextfnfield *next;
768 struct fn_field fnfield;
769 }
770 *fnfields;
c906108c 771
c5aa993b
JM
772 /* Member function fieldlist array, contains name of possibly overloaded
773 member function, number of overloaded member functions and a pointer
774 to the head of the member function field chain. */
775 struct fnfieldlist
776 {
777 char *name;
778 int length;
779 struct nextfnfield *head;
780 }
781 *fnfieldlists;
c906108c 782
c5aa993b
JM
783 /* Number of entries in the fnfieldlists array. */
784 int nfnfields;
98751a41
JK
785
786 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
787 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
788 struct typedef_field_list
789 {
790 struct typedef_field field;
791 struct typedef_field_list *next;
792 }
793 *typedef_field_list;
794 unsigned typedef_field_list_count;
c5aa993b 795 };
c906108c 796
10b3939b
DJ
797/* One item on the queue of compilation units to read in full symbols
798 for. */
799struct dwarf2_queue_item
800{
801 struct dwarf2_per_cu_data *per_cu;
802 struct dwarf2_queue_item *next;
803};
804
805/* The current queue. */
806static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
807
ae038cb0
DJ
808/* Loaded secondary compilation units are kept in memory until they
809 have not been referenced for the processing of this many
810 compilation units. Set this to zero to disable caching. Cache
811 sizes of up to at least twenty will improve startup time for
812 typical inter-CU-reference binaries, at an obvious memory cost. */
813static int dwarf2_max_cache_age = 5;
920d2a44
AC
814static void
815show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
816 struct cmd_list_element *c, const char *value)
817{
3e43a32a
MS
818 fprintf_filtered (file, _("The upper bound on the age of cached "
819 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
820 value);
821}
822
ae038cb0 823
0963b4bd 824/* Various complaints about symbol reading that don't abort the process. */
c906108c 825
4d3c2250
KB
826static void
827dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 828{
4d3c2250 829 complaint (&symfile_complaints,
e2e0b3e5 830 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
831}
832
25e43795
DJ
833static void
834dwarf2_debug_line_missing_file_complaint (void)
835{
836 complaint (&symfile_complaints,
837 _(".debug_line section has line data without a file"));
838}
839
59205f5a
JB
840static void
841dwarf2_debug_line_missing_end_sequence_complaint (void)
842{
843 complaint (&symfile_complaints,
3e43a32a
MS
844 _(".debug_line section has line "
845 "program sequence without an end"));
59205f5a
JB
846}
847
4d3c2250
KB
848static void
849dwarf2_complex_location_expr_complaint (void)
2e276125 850{
e2e0b3e5 851 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
852}
853
4d3c2250
KB
854static void
855dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
856 int arg3)
2e276125 857{
4d3c2250 858 complaint (&symfile_complaints,
3e43a32a
MS
859 _("const value length mismatch for '%s', got %d, expected %d"),
860 arg1, arg2, arg3);
4d3c2250
KB
861}
862
863static void
cf2c3c16 864dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
2e276125 865{
4d3c2250 866 complaint (&symfile_complaints,
cf2c3c16
TT
867 _("macro info runs off end of `%s' section"),
868 section->asection->name);
4d3c2250
KB
869}
870
871static void
872dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 873{
4d3c2250 874 complaint (&symfile_complaints,
3e43a32a
MS
875 _("macro debug info contains a "
876 "malformed macro definition:\n`%s'"),
4d3c2250
KB
877 arg1);
878}
879
880static void
881dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 882{
4d3c2250 883 complaint (&symfile_complaints,
3e43a32a
MS
884 _("invalid attribute class or form for '%s' in '%s'"),
885 arg1, arg2);
4d3c2250 886}
c906108c 887
c906108c
SS
888/* local function prototypes */
889
4efb68b1 890static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 891
aaa75496
JB
892static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
893 struct objfile *);
894
918dd910
JK
895static void dwarf2_find_base_address (struct die_info *die,
896 struct dwarf2_cu *cu);
897
c67a9c90 898static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 899
72bf9492
DJ
900static void scan_partial_symbols (struct partial_die_info *,
901 CORE_ADDR *, CORE_ADDR *,
5734ee8b 902 int, struct dwarf2_cu *);
c906108c 903
72bf9492
DJ
904static void add_partial_symbol (struct partial_die_info *,
905 struct dwarf2_cu *);
63d06c5c 906
72bf9492
DJ
907static void add_partial_namespace (struct partial_die_info *pdi,
908 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 909 int need_pc, struct dwarf2_cu *cu);
63d06c5c 910
5d7cb8df
JK
911static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
912 CORE_ADDR *highpc, int need_pc,
913 struct dwarf2_cu *cu);
914
72bf9492
DJ
915static void add_partial_enumeration (struct partial_die_info *enum_pdi,
916 struct dwarf2_cu *cu);
91c24f0a 917
bc30ff58
JB
918static void add_partial_subprogram (struct partial_die_info *pdi,
919 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 920 int need_pc, struct dwarf2_cu *cu);
bc30ff58 921
fe1b8b76 922static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
923 gdb_byte *buffer, gdb_byte *info_ptr,
924 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 925
a14ed312 926static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 927
a14ed312 928static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 929
e7c27a73 930static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 931
f3dd6933 932static void dwarf2_free_abbrev_table (void *);
c906108c 933
fe1b8b76 934static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 935 struct dwarf2_cu *);
72bf9492 936
57349743 937static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 938 struct dwarf2_cu *);
c906108c 939
93311388
DE
940static struct partial_die_info *load_partial_dies (bfd *,
941 gdb_byte *, gdb_byte *,
942 int, struct dwarf2_cu *);
72bf9492 943
fe1b8b76 944static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
945 struct abbrev_info *abbrev,
946 unsigned int, bfd *,
947 gdb_byte *, gdb_byte *,
948 struct dwarf2_cu *);
c906108c 949
c764a876 950static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 951 struct dwarf2_cu *);
72bf9492
DJ
952
953static void fixup_partial_die (struct partial_die_info *,
954 struct dwarf2_cu *);
955
fe1b8b76
JB
956static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
957 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 958
fe1b8b76
JB
959static gdb_byte *read_attribute_value (struct attribute *, unsigned,
960 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 961
fe1b8b76 962static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 963
fe1b8b76 964static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 965
fe1b8b76 966static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 967
fe1b8b76 968static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 969
93311388 970static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 971
fe1b8b76 972static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 973 unsigned int *);
c906108c 974
c764a876
DE
975static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
976
977static LONGEST read_checked_initial_length_and_offset
978 (bfd *, gdb_byte *, const struct comp_unit_head *,
979 unsigned int *, unsigned int *);
613e1657 980
fe1b8b76 981static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
982 unsigned int *);
983
984static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 985
fe1b8b76 986static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 987
9b1c24c8 988static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 989
fe1b8b76
JB
990static char *read_indirect_string (bfd *, gdb_byte *,
991 const struct comp_unit_head *,
992 unsigned int *);
4bdf3d34 993
fe1b8b76 994static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 995
fe1b8b76 996static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 997
fe1b8b76 998static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 999
e142c38c 1000static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1001
e142c38c
DJ
1002static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1003 struct dwarf2_cu *);
c906108c 1004
348e048f
DE
1005static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1006 unsigned int,
1007 struct dwarf2_cu *);
1008
05cf31d1
JB
1009static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1010 struct dwarf2_cu *cu);
1011
e142c38c 1012static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1013
e142c38c 1014static struct die_info *die_specification (struct die_info *die,
f2f0e013 1015 struct dwarf2_cu **);
63d06c5c 1016
debd256d
JB
1017static void free_line_header (struct line_header *lh);
1018
aaa75496
JB
1019static void add_file_name (struct line_header *, char *, unsigned int,
1020 unsigned int, unsigned int);
1021
debd256d
JB
1022static struct line_header *(dwarf_decode_line_header
1023 (unsigned int offset,
e7c27a73 1024 bfd *abfd, struct dwarf2_cu *cu));
debd256d 1025
72b9f47f 1026static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 1027 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1028
72b9f47f 1029static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1030
a14ed312 1031static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1032 struct dwarf2_cu *);
c906108c 1033
34eaf542
TT
1034static struct symbol *new_symbol_full (struct die_info *, struct type *,
1035 struct dwarf2_cu *, struct symbol *);
1036
a14ed312 1037static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1038 struct dwarf2_cu *);
c906108c 1039
98bfdba5
PA
1040static void dwarf2_const_value_attr (struct attribute *attr,
1041 struct type *type,
1042 const char *name,
1043 struct obstack *obstack,
1044 struct dwarf2_cu *cu, long *value,
1045 gdb_byte **bytes,
1046 struct dwarf2_locexpr_baton **baton);
2df3850c 1047
e7c27a73 1048static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1049
b4ba55a1
JB
1050static int need_gnat_info (struct dwarf2_cu *);
1051
3e43a32a
MS
1052static struct type *die_descriptive_type (struct die_info *,
1053 struct dwarf2_cu *);
b4ba55a1
JB
1054
1055static void set_descriptive_type (struct type *, struct die_info *,
1056 struct dwarf2_cu *);
1057
e7c27a73
DJ
1058static struct type *die_containing_type (struct die_info *,
1059 struct dwarf2_cu *);
c906108c 1060
673bfd45
DE
1061static struct type *lookup_die_type (struct die_info *, struct attribute *,
1062 struct dwarf2_cu *);
c906108c 1063
f792889a 1064static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1065
673bfd45
DE
1066static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1067
086ed43d 1068static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1069
6e70227d 1070static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1071 const char *suffix, int physname,
1072 struct dwarf2_cu *cu);
63d06c5c 1073
e7c27a73 1074static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1075
348e048f
DE
1076static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1077
e7c27a73 1078static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1079
e7c27a73 1080static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1081
ff013f42
JK
1082static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1083 struct dwarf2_cu *, struct partial_symtab *);
1084
a14ed312 1085static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1086 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1087 struct partial_symtab *);
c906108c 1088
fae299cd
DC
1089static void get_scope_pc_bounds (struct die_info *,
1090 CORE_ADDR *, CORE_ADDR *,
1091 struct dwarf2_cu *);
1092
801e3a5b
JB
1093static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1094 CORE_ADDR, struct dwarf2_cu *);
1095
a14ed312 1096static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1097 struct dwarf2_cu *);
c906108c 1098
a14ed312 1099static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1100 struct type *, struct dwarf2_cu *);
c906108c 1101
a14ed312 1102static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1103 struct die_info *, struct type *,
e7c27a73 1104 struct dwarf2_cu *);
c906108c 1105
a14ed312 1106static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1107 struct type *,
1108 struct dwarf2_cu *);
c906108c 1109
134d01f1 1110static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1111
e7c27a73 1112static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1113
e7c27a73 1114static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1115
5d7cb8df
JK
1116static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1117
27aa8d6a
SW
1118static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1119
f55ee35c
JK
1120static struct type *read_module_type (struct die_info *die,
1121 struct dwarf2_cu *cu);
1122
38d518c9 1123static const char *namespace_name (struct die_info *die,
e142c38c 1124 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1125
134d01f1 1126static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1127
e7c27a73 1128static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1129
6e70227d 1130static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1131 struct dwarf2_cu *);
1132
93311388 1133static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1134
93311388
DE
1135static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1136 gdb_byte *info_ptr,
d97bc12b
DE
1137 gdb_byte **new_info_ptr,
1138 struct die_info *parent);
1139
93311388
DE
1140static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1141 gdb_byte *info_ptr,
fe1b8b76 1142 gdb_byte **new_info_ptr,
639d11d3
DC
1143 struct die_info *parent);
1144
93311388
DE
1145static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1146 gdb_byte *info_ptr,
fe1b8b76 1147 gdb_byte **new_info_ptr,
639d11d3
DC
1148 struct die_info *parent);
1149
93311388
DE
1150static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1151 struct die_info **, gdb_byte *,
1152 int *);
1153
e7c27a73 1154static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1155
71c25dea
TT
1156static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1157 struct obstack *);
1158
e142c38c 1159static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1160
98bfdba5
PA
1161static const char *dwarf2_full_name (char *name,
1162 struct die_info *die,
1163 struct dwarf2_cu *cu);
1164
e142c38c 1165static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1166 struct dwarf2_cu **);
9219021c 1167
a14ed312 1168static char *dwarf_tag_name (unsigned int);
c906108c 1169
a14ed312 1170static char *dwarf_attr_name (unsigned int);
c906108c 1171
a14ed312 1172static char *dwarf_form_name (unsigned int);
c906108c 1173
a14ed312 1174static char *dwarf_bool_name (unsigned int);
c906108c 1175
a14ed312 1176static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1177
1178#if 0
a14ed312 1179static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1180#endif
1181
f9aca02d 1182static struct die_info *sibling_die (struct die_info *);
c906108c 1183
d97bc12b
DE
1184static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1185
1186static void dump_die_for_error (struct die_info *);
1187
1188static void dump_die_1 (struct ui_file *, int level, int max_level,
1189 struct die_info *);
c906108c 1190
d97bc12b 1191/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1192
51545339 1193static void store_in_ref_table (struct die_info *,
10b3939b 1194 struct dwarf2_cu *);
c906108c 1195
93311388
DE
1196static int is_ref_attr (struct attribute *);
1197
c764a876 1198static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1199
43bbcdc2 1200static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1201
348e048f
DE
1202static struct die_info *follow_die_ref_or_sig (struct die_info *,
1203 struct attribute *,
1204 struct dwarf2_cu **);
1205
10b3939b
DJ
1206static struct die_info *follow_die_ref (struct die_info *,
1207 struct attribute *,
f2f0e013 1208 struct dwarf2_cu **);
c906108c 1209
348e048f
DE
1210static struct die_info *follow_die_sig (struct die_info *,
1211 struct attribute *,
1212 struct dwarf2_cu **);
1213
6c83ed52
TT
1214static struct signatured_type *lookup_signatured_type_at_offset
1215 (struct objfile *objfile,
1216 struct dwarf2_section_info *section,
1217 unsigned int offset);
1218
348e048f 1219static void read_signatured_type_at_offset (struct objfile *objfile,
8b70b953 1220 struct dwarf2_section_info *sect,
348e048f
DE
1221 unsigned int offset);
1222
1223static void read_signatured_type (struct objfile *,
1224 struct signatured_type *type_sig);
1225
c906108c
SS
1226/* memory allocation interface */
1227
7b5a2f43 1228static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1229
f3dd6933 1230static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1231
b60c80d6 1232static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1233
e142c38c 1234static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1235
e142c38c
DJ
1236static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1237 struct dwarf2_cu *);
5fb290d7 1238
2e276125 1239static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1240 char *, bfd *, struct dwarf2_cu *,
1241 struct dwarf2_section_info *,
1242 int);
2e276125 1243
8e19ed76
PS
1244static int attr_form_is_block (struct attribute *);
1245
3690dd37
JB
1246static int attr_form_is_section_offset (struct attribute *);
1247
1248static int attr_form_is_constant (struct attribute *);
1249
8cf6f0b1
TT
1250static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1251 struct dwarf2_loclist_baton *baton,
1252 struct attribute *attr);
1253
93e7bd98
DJ
1254static void dwarf2_symbol_mark_computed (struct attribute *attr,
1255 struct symbol *sym,
1256 struct dwarf2_cu *cu);
4c2df51b 1257
93311388
DE
1258static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1259 struct abbrev_info *abbrev,
1260 struct dwarf2_cu *cu);
4bb7a0a7 1261
72bf9492
DJ
1262static void free_stack_comp_unit (void *);
1263
72bf9492
DJ
1264static hashval_t partial_die_hash (const void *item);
1265
1266static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1267
ae038cb0 1268static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1269 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1270
1271static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1272 (unsigned int offset, struct objfile *objfile);
ae038cb0 1273
9816fde3
JK
1274static void init_one_comp_unit (struct dwarf2_cu *cu,
1275 struct objfile *objfile);
1276
1277static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1278 struct die_info *comp_unit_die);
93311388 1279
ae038cb0
DJ
1280static void free_one_comp_unit (void *);
1281
1282static void free_cached_comp_units (void *);
1283
1284static void age_cached_comp_units (void);
1285
1286static void free_one_cached_comp_unit (void *);
1287
f792889a
DJ
1288static struct type *set_die_type (struct die_info *, struct type *,
1289 struct dwarf2_cu *);
1c379e20 1290
ae038cb0
DJ
1291static void create_all_comp_units (struct objfile *);
1292
1fd400ff
TT
1293static int create_debug_types_hash_table (struct objfile *objfile);
1294
93311388
DE
1295static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1296 struct objfile *);
10b3939b
DJ
1297
1298static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1299
1300static void dwarf2_add_dependence (struct dwarf2_cu *,
1301 struct dwarf2_per_cu_data *);
1302
ae038cb0
DJ
1303static void dwarf2_mark (struct dwarf2_cu *);
1304
1305static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1306
673bfd45
DE
1307static struct type *get_die_type_at_offset (unsigned int,
1308 struct dwarf2_per_cu_data *per_cu);
1309
f792889a 1310static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1311
9291a0cd
TT
1312static void dwarf2_release_queue (void *dummy);
1313
1314static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1315 struct objfile *objfile);
1316
1317static void process_queue (struct objfile *objfile);
1318
1319static void find_file_and_directory (struct die_info *die,
1320 struct dwarf2_cu *cu,
1321 char **name, char **comp_dir);
1322
1323static char *file_full_name (int file, struct line_header *lh,
1324 const char *comp_dir);
1325
1326static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1327 gdb_byte *info_ptr,
1328 gdb_byte *buffer,
1329 unsigned int buffer_size,
1330 bfd *abfd);
1331
1332static void init_cu_die_reader (struct die_reader_specs *reader,
1333 struct dwarf2_cu *cu);
1334
673bfd45 1335static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1336
9291a0cd
TT
1337#if WORDS_BIGENDIAN
1338
1339/* Convert VALUE between big- and little-endian. */
1340static offset_type
1341byte_swap (offset_type value)
1342{
1343 offset_type result;
1344
1345 result = (value & 0xff) << 24;
1346 result |= (value & 0xff00) << 8;
1347 result |= (value & 0xff0000) >> 8;
1348 result |= (value & 0xff000000) >> 24;
1349 return result;
1350}
1351
1352#define MAYBE_SWAP(V) byte_swap (V)
1353
1354#else
1355#define MAYBE_SWAP(V) (V)
1356#endif /* WORDS_BIGENDIAN */
1357
1358/* The suffix for an index file. */
1359#define INDEX_SUFFIX ".gdb-index"
1360
3da10d80
KS
1361static const char *dwarf2_physname (char *name, struct die_info *die,
1362 struct dwarf2_cu *cu);
1363
c906108c 1364/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1365 information and return true if we have enough to do something.
1366 NAMES points to the dwarf2 section names, or is NULL if the standard
1367 ELF names are used. */
c906108c
SS
1368
1369int
251d32d9
TG
1370dwarf2_has_info (struct objfile *objfile,
1371 const struct dwarf2_debug_sections *names)
c906108c 1372{
be391dca
TT
1373 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1374 if (!dwarf2_per_objfile)
1375 {
1376 /* Initialize per-objfile state. */
1377 struct dwarf2_per_objfile *data
1378 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1379
be391dca
TT
1380 memset (data, 0, sizeof (*data));
1381 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1382 dwarf2_per_objfile = data;
6502dd73 1383
251d32d9
TG
1384 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1385 (void *) names);
be391dca
TT
1386 dwarf2_per_objfile->objfile = objfile;
1387 }
1388 return (dwarf2_per_objfile->info.asection != NULL
1389 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1390}
1391
251d32d9
TG
1392/* When loading sections, we look either for uncompressed section or for
1393 compressed section names. */
233a11ab
CS
1394
1395static int
251d32d9
TG
1396section_is_p (const char *section_name,
1397 const struct dwarf2_section_names *names)
233a11ab 1398{
251d32d9
TG
1399 if (names->normal != NULL
1400 && strcmp (section_name, names->normal) == 0)
1401 return 1;
1402 if (names->compressed != NULL
1403 && strcmp (section_name, names->compressed) == 0)
1404 return 1;
1405 return 0;
233a11ab
CS
1406}
1407
c906108c
SS
1408/* This function is mapped across the sections and remembers the
1409 offset and size of each of the debugging sections we are interested
1410 in. */
1411
1412static void
251d32d9 1413dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1414{
251d32d9
TG
1415 const struct dwarf2_debug_sections *names;
1416
1417 if (vnames == NULL)
1418 names = &dwarf2_elf_names;
1419 else
1420 names = (const struct dwarf2_debug_sections *) vnames;
1421
1422 if (section_is_p (sectp->name, &names->info))
c906108c 1423 {
dce234bc
PP
1424 dwarf2_per_objfile->info.asection = sectp;
1425 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1426 }
251d32d9 1427 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1428 {
dce234bc
PP
1429 dwarf2_per_objfile->abbrev.asection = sectp;
1430 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1431 }
251d32d9 1432 else if (section_is_p (sectp->name, &names->line))
c906108c 1433 {
dce234bc
PP
1434 dwarf2_per_objfile->line.asection = sectp;
1435 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1436 }
251d32d9 1437 else if (section_is_p (sectp->name, &names->loc))
c906108c 1438 {
dce234bc
PP
1439 dwarf2_per_objfile->loc.asection = sectp;
1440 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1441 }
251d32d9 1442 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1443 {
dce234bc
PP
1444 dwarf2_per_objfile->macinfo.asection = sectp;
1445 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1446 }
cf2c3c16
TT
1447 else if (section_is_p (sectp->name, &names->macro))
1448 {
1449 dwarf2_per_objfile->macro.asection = sectp;
1450 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1451 }
251d32d9 1452 else if (section_is_p (sectp->name, &names->str))
c906108c 1453 {
dce234bc
PP
1454 dwarf2_per_objfile->str.asection = sectp;
1455 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1456 }
251d32d9 1457 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1458 {
dce234bc
PP
1459 dwarf2_per_objfile->frame.asection = sectp;
1460 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1461 }
251d32d9 1462 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1463 {
3799ccc6 1464 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1465
3799ccc6
EZ
1466 if (aflag & SEC_HAS_CONTENTS)
1467 {
dce234bc
PP
1468 dwarf2_per_objfile->eh_frame.asection = sectp;
1469 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1470 }
b6af0555 1471 }
251d32d9 1472 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1473 {
dce234bc
PP
1474 dwarf2_per_objfile->ranges.asection = sectp;
1475 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1476 }
251d32d9 1477 else if (section_is_p (sectp->name, &names->types))
348e048f 1478 {
8b70b953
TT
1479 struct dwarf2_section_info type_section;
1480
1481 memset (&type_section, 0, sizeof (type_section));
1482 type_section.asection = sectp;
1483 type_section.size = bfd_get_section_size (sectp);
1484
1485 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1486 &type_section);
348e048f 1487 }
251d32d9 1488 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1489 {
1490 dwarf2_per_objfile->gdb_index.asection = sectp;
1491 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1492 }
dce234bc 1493
72dca2f5
FR
1494 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1495 && bfd_section_vma (abfd, sectp) == 0)
1496 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1497}
1498
dce234bc
PP
1499/* Decompress a section that was compressed using zlib. Store the
1500 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1501
1502static void
dce234bc
PP
1503zlib_decompress_section (struct objfile *objfile, asection *sectp,
1504 gdb_byte **outbuf, bfd_size_type *outsize)
1505{
1506 bfd *abfd = objfile->obfd;
1507#ifndef HAVE_ZLIB_H
1508 error (_("Support for zlib-compressed DWARF data (from '%s') "
1509 "is disabled in this copy of GDB"),
1510 bfd_get_filename (abfd));
1511#else
1512 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1513 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1514 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1515 bfd_size_type uncompressed_size;
1516 gdb_byte *uncompressed_buffer;
1517 z_stream strm;
1518 int rc;
1519 int header_size = 12;
1520
1521 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1522 || bfd_bread (compressed_buffer,
1523 compressed_size, abfd) != compressed_size)
dce234bc
PP
1524 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1525 bfd_get_filename (abfd));
1526
1527 /* Read the zlib header. In this case, it should be "ZLIB" followed
1528 by the uncompressed section size, 8 bytes in big-endian order. */
1529 if (compressed_size < header_size
1530 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1531 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1532 bfd_get_filename (abfd));
1533 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1534 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1535 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1536 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1537 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1538 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1539 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1540 uncompressed_size += compressed_buffer[11];
1541
1542 /* It is possible the section consists of several compressed
1543 buffers concatenated together, so we uncompress in a loop. */
1544 strm.zalloc = NULL;
1545 strm.zfree = NULL;
1546 strm.opaque = NULL;
1547 strm.avail_in = compressed_size - header_size;
1548 strm.next_in = (Bytef*) compressed_buffer + header_size;
1549 strm.avail_out = uncompressed_size;
1550 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1551 uncompressed_size);
1552 rc = inflateInit (&strm);
1553 while (strm.avail_in > 0)
1554 {
1555 if (rc != Z_OK)
1556 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1557 bfd_get_filename (abfd), rc);
1558 strm.next_out = ((Bytef*) uncompressed_buffer
1559 + (uncompressed_size - strm.avail_out));
1560 rc = inflate (&strm, Z_FINISH);
1561 if (rc != Z_STREAM_END)
1562 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1563 bfd_get_filename (abfd), rc);
1564 rc = inflateReset (&strm);
1565 }
1566 rc = inflateEnd (&strm);
1567 if (rc != Z_OK
1568 || strm.avail_out != 0)
1569 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1570 bfd_get_filename (abfd), rc);
1571
affddf13 1572 do_cleanups (cleanup);
dce234bc
PP
1573 *outbuf = uncompressed_buffer;
1574 *outsize = uncompressed_size;
1575#endif
233a11ab
CS
1576}
1577
9e0ac564
TT
1578/* A helper function that decides whether a section is empty. */
1579
1580static int
1581dwarf2_section_empty_p (struct dwarf2_section_info *info)
1582{
1583 return info->asection == NULL || info->size == 0;
1584}
1585
dce234bc
PP
1586/* Read the contents of the section SECTP from object file specified by
1587 OBJFILE, store info about the section into INFO.
1588 If the section is compressed, uncompress it before returning. */
c906108c 1589
dce234bc
PP
1590static void
1591dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1592{
dce234bc
PP
1593 bfd *abfd = objfile->obfd;
1594 asection *sectp = info->asection;
1595 gdb_byte *buf, *retbuf;
1596 unsigned char header[4];
c906108c 1597
be391dca
TT
1598 if (info->readin)
1599 return;
dce234bc 1600 info->buffer = NULL;
b315ab21 1601 info->map_addr = NULL;
be391dca 1602 info->readin = 1;
188dd5d6 1603
9e0ac564 1604 if (dwarf2_section_empty_p (info))
dce234bc 1605 return;
c906108c 1606
dce234bc
PP
1607 /* Check if the file has a 4-byte header indicating compression. */
1608 if (info->size > sizeof (header)
1609 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1610 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1611 {
1612 /* Upon decompression, update the buffer and its size. */
1613 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1614 {
1615 zlib_decompress_section (objfile, sectp, &info->buffer,
1616 &info->size);
1617 return;
1618 }
1619 }
4bdf3d34 1620
dce234bc
PP
1621#ifdef HAVE_MMAP
1622 if (pagesize == 0)
1623 pagesize = getpagesize ();
2e276125 1624
dce234bc
PP
1625 /* Only try to mmap sections which are large enough: we don't want to
1626 waste space due to fragmentation. Also, only try mmap for sections
1627 without relocations. */
1628
1629 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1630 {
b315ab21
TG
1631 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1632 MAP_PRIVATE, sectp->filepos,
1633 &info->map_addr, &info->map_len);
dce234bc 1634
b315ab21 1635 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1636 {
be391dca 1637#if HAVE_POSIX_MADVISE
b315ab21 1638 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1639#endif
dce234bc
PP
1640 return;
1641 }
1642 }
1643#endif
1644
1645 /* If we get here, we are a normal, not-compressed section. */
1646 info->buffer = buf
1647 = obstack_alloc (&objfile->objfile_obstack, info->size);
1648
1649 /* When debugging .o files, we may need to apply relocations; see
1650 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1651 We never compress sections in .o files, so we only need to
1652 try this when the section is not compressed. */
ac8035ab 1653 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1654 if (retbuf != NULL)
1655 {
1656 info->buffer = retbuf;
1657 return;
1658 }
1659
1660 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1661 || bfd_bread (buf, info->size, abfd) != info->size)
1662 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1663 bfd_get_filename (abfd));
1664}
1665
9e0ac564
TT
1666/* A helper function that returns the size of a section in a safe way.
1667 If you are positive that the section has been read before using the
1668 size, then it is safe to refer to the dwarf2_section_info object's
1669 "size" field directly. In other cases, you must call this
1670 function, because for compressed sections the size field is not set
1671 correctly until the section has been read. */
1672
1673static bfd_size_type
1674dwarf2_section_size (struct objfile *objfile,
1675 struct dwarf2_section_info *info)
1676{
1677 if (!info->readin)
1678 dwarf2_read_section (objfile, info);
1679 return info->size;
1680}
1681
dce234bc 1682/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1683 SECTION_NAME. */
af34e669 1684
dce234bc 1685void
3017a003
TG
1686dwarf2_get_section_info (struct objfile *objfile,
1687 enum dwarf2_section_enum sect,
dce234bc
PP
1688 asection **sectp, gdb_byte **bufp,
1689 bfd_size_type *sizep)
1690{
1691 struct dwarf2_per_objfile *data
1692 = objfile_data (objfile, dwarf2_objfile_data_key);
1693 struct dwarf2_section_info *info;
a3b2a86b
TT
1694
1695 /* We may see an objfile without any DWARF, in which case we just
1696 return nothing. */
1697 if (data == NULL)
1698 {
1699 *sectp = NULL;
1700 *bufp = NULL;
1701 *sizep = 0;
1702 return;
1703 }
3017a003
TG
1704 switch (sect)
1705 {
1706 case DWARF2_DEBUG_FRAME:
1707 info = &data->frame;
1708 break;
1709 case DWARF2_EH_FRAME:
1710 info = &data->eh_frame;
1711 break;
1712 default:
1713 gdb_assert_not_reached ("unexpected section");
1714 }
dce234bc 1715
9e0ac564 1716 dwarf2_read_section (objfile, info);
dce234bc
PP
1717
1718 *sectp = info->asection;
1719 *bufp = info->buffer;
1720 *sizep = info->size;
1721}
1722
9291a0cd 1723\f
7b9f3c50
DE
1724/* DWARF quick_symbols_functions support. */
1725
1726/* TUs can share .debug_line entries, and there can be a lot more TUs than
1727 unique line tables, so we maintain a separate table of all .debug_line
1728 derived entries to support the sharing.
1729 All the quick functions need is the list of file names. We discard the
1730 line_header when we're done and don't need to record it here. */
1731struct quick_file_names
1732{
1733 /* The offset in .debug_line of the line table. We hash on this. */
1734 unsigned int offset;
1735
1736 /* The number of entries in file_names, real_names. */
1737 unsigned int num_file_names;
1738
1739 /* The file names from the line table, after being run through
1740 file_full_name. */
1741 const char **file_names;
1742
1743 /* The file names from the line table after being run through
1744 gdb_realpath. These are computed lazily. */
1745 const char **real_names;
1746};
1747
1748/* When using the index (and thus not using psymtabs), each CU has an
1749 object of this type. This is used to hold information needed by
1750 the various "quick" methods. */
1751struct dwarf2_per_cu_quick_data
1752{
1753 /* The file table. This can be NULL if there was no file table
1754 or it's currently not read in.
1755 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1756 struct quick_file_names *file_names;
1757
1758 /* The corresponding symbol table. This is NULL if symbols for this
1759 CU have not yet been read. */
1760 struct symtab *symtab;
1761
1762 /* A temporary mark bit used when iterating over all CUs in
1763 expand_symtabs_matching. */
1764 unsigned int mark : 1;
1765
1766 /* True if we've tried to read the file table and found there isn't one.
1767 There will be no point in trying to read it again next time. */
1768 unsigned int no_file_data : 1;
1769};
1770
1771/* Hash function for a quick_file_names. */
1772
1773static hashval_t
1774hash_file_name_entry (const void *e)
1775{
1776 const struct quick_file_names *file_data = e;
1777
1778 return file_data->offset;
1779}
1780
1781/* Equality function for a quick_file_names. */
1782
1783static int
1784eq_file_name_entry (const void *a, const void *b)
1785{
1786 const struct quick_file_names *ea = a;
1787 const struct quick_file_names *eb = b;
1788
1789 return ea->offset == eb->offset;
1790}
1791
1792/* Delete function for a quick_file_names. */
1793
1794static void
1795delete_file_name_entry (void *e)
1796{
1797 struct quick_file_names *file_data = e;
1798 int i;
1799
1800 for (i = 0; i < file_data->num_file_names; ++i)
1801 {
1802 xfree ((void*) file_data->file_names[i]);
1803 if (file_data->real_names)
1804 xfree ((void*) file_data->real_names[i]);
1805 }
1806
1807 /* The space for the struct itself lives on objfile_obstack,
1808 so we don't free it here. */
1809}
1810
1811/* Create a quick_file_names hash table. */
1812
1813static htab_t
1814create_quick_file_names_table (unsigned int nr_initial_entries)
1815{
1816 return htab_create_alloc (nr_initial_entries,
1817 hash_file_name_entry, eq_file_name_entry,
1818 delete_file_name_entry, xcalloc, xfree);
1819}
9291a0cd 1820
918dd910
JK
1821/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1822 have to be created afterwards. You should call age_cached_comp_units after
1823 processing PER_CU->CU. dw2_setup must have been already called. */
1824
1825static void
1826load_cu (struct dwarf2_per_cu_data *per_cu)
1827{
8b70b953
TT
1828 if (per_cu->debug_type_section)
1829 read_signatured_type_at_offset (per_cu->objfile,
1830 per_cu->debug_type_section,
1831 per_cu->offset);
918dd910
JK
1832 else
1833 load_full_comp_unit (per_cu, per_cu->objfile);
1834
1835 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1836
1837 gdb_assert (per_cu->cu != NULL);
1838}
1839
9291a0cd
TT
1840/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1841 this CU came. */
2fdf6df6 1842
9291a0cd
TT
1843static void
1844dw2_do_instantiate_symtab (struct objfile *objfile,
1845 struct dwarf2_per_cu_data *per_cu)
1846{
1847 struct cleanup *back_to;
1848
1849 back_to = make_cleanup (dwarf2_release_queue, NULL);
1850
1851 queue_comp_unit (per_cu, objfile);
1852
918dd910 1853 load_cu (per_cu);
9291a0cd
TT
1854
1855 process_queue (objfile);
1856
1857 /* Age the cache, releasing compilation units that have not
1858 been used recently. */
1859 age_cached_comp_units ();
1860
1861 do_cleanups (back_to);
1862}
1863
1864/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1865 the objfile from which this CU came. Returns the resulting symbol
1866 table. */
2fdf6df6 1867
9291a0cd
TT
1868static struct symtab *
1869dw2_instantiate_symtab (struct objfile *objfile,
1870 struct dwarf2_per_cu_data *per_cu)
1871{
1872 if (!per_cu->v.quick->symtab)
1873 {
1874 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1875 increment_reading_symtab ();
1876 dw2_do_instantiate_symtab (objfile, per_cu);
1877 do_cleanups (back_to);
1878 }
1879 return per_cu->v.quick->symtab;
1880}
1881
1fd400ff 1882/* Return the CU given its index. */
2fdf6df6 1883
1fd400ff
TT
1884static struct dwarf2_per_cu_data *
1885dw2_get_cu (int index)
1886{
1887 if (index >= dwarf2_per_objfile->n_comp_units)
1888 {
1889 index -= dwarf2_per_objfile->n_comp_units;
1890 return dwarf2_per_objfile->type_comp_units[index];
1891 }
1892 return dwarf2_per_objfile->all_comp_units[index];
1893}
1894
9291a0cd
TT
1895/* A helper function that knows how to read a 64-bit value in a way
1896 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1897 otherwise. */
2fdf6df6 1898
9291a0cd
TT
1899static int
1900extract_cu_value (const char *bytes, ULONGEST *result)
1901{
1902 if (sizeof (ULONGEST) < 8)
1903 {
1904 int i;
1905
1906 /* Ignore the upper 4 bytes if they are all zero. */
1907 for (i = 0; i < 4; ++i)
1908 if (bytes[i + 4] != 0)
1909 return 0;
1910
1911 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1912 }
1913 else
1914 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1915 return 1;
1916}
1917
1918/* Read the CU list from the mapped index, and use it to create all
1919 the CU objects for this objfile. Return 0 if something went wrong,
1920 1 if everything went ok. */
2fdf6df6 1921
9291a0cd 1922static int
1fd400ff
TT
1923create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1924 offset_type cu_list_elements)
9291a0cd
TT
1925{
1926 offset_type i;
9291a0cd
TT
1927
1928 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1929 dwarf2_per_objfile->all_comp_units
1930 = obstack_alloc (&objfile->objfile_obstack,
1931 dwarf2_per_objfile->n_comp_units
1932 * sizeof (struct dwarf2_per_cu_data *));
1933
1934 for (i = 0; i < cu_list_elements; i += 2)
1935 {
1936 struct dwarf2_per_cu_data *the_cu;
1937 ULONGEST offset, length;
1938
1939 if (!extract_cu_value (cu_list, &offset)
1940 || !extract_cu_value (cu_list + 8, &length))
1941 return 0;
1942 cu_list += 2 * 8;
1943
1944 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1945 struct dwarf2_per_cu_data);
1946 the_cu->offset = offset;
1947 the_cu->length = length;
1948 the_cu->objfile = objfile;
1949 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1950 struct dwarf2_per_cu_quick_data);
1951 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1952 }
1953
1954 return 1;
1955}
1956
1fd400ff 1957/* Create the signatured type hash table from the index. */
673bfd45 1958
1fd400ff 1959static int
673bfd45 1960create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 1961 struct dwarf2_section_info *section,
673bfd45
DE
1962 const gdb_byte *bytes,
1963 offset_type elements)
1fd400ff
TT
1964{
1965 offset_type i;
673bfd45 1966 htab_t sig_types_hash;
1fd400ff
TT
1967
1968 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1969 dwarf2_per_objfile->type_comp_units
1970 = obstack_alloc (&objfile->objfile_obstack,
1971 dwarf2_per_objfile->n_type_comp_units
1972 * sizeof (struct dwarf2_per_cu_data *));
1973
673bfd45 1974 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1975
1976 for (i = 0; i < elements; i += 3)
1977 {
1978 struct signatured_type *type_sig;
1979 ULONGEST offset, type_offset, signature;
1980 void **slot;
1981
1982 if (!extract_cu_value (bytes, &offset)
1983 || !extract_cu_value (bytes + 8, &type_offset))
1984 return 0;
1985 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1986 bytes += 3 * 8;
1987
1988 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1989 struct signatured_type);
1990 type_sig->signature = signature;
1fd400ff 1991 type_sig->type_offset = type_offset;
8b70b953 1992 type_sig->per_cu.debug_type_section = section;
1fd400ff
TT
1993 type_sig->per_cu.offset = offset;
1994 type_sig->per_cu.objfile = objfile;
1995 type_sig->per_cu.v.quick
1996 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1997 struct dwarf2_per_cu_quick_data);
1998
673bfd45 1999 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
2000 *slot = type_sig;
2001
2002 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
2003 }
2004
673bfd45 2005 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2006
2007 return 1;
2008}
2009
9291a0cd
TT
2010/* Read the address map data from the mapped index, and use it to
2011 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2012
9291a0cd
TT
2013static void
2014create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2015{
2016 const gdb_byte *iter, *end;
2017 struct obstack temp_obstack;
2018 struct addrmap *mutable_map;
2019 struct cleanup *cleanup;
2020 CORE_ADDR baseaddr;
2021
2022 obstack_init (&temp_obstack);
2023 cleanup = make_cleanup_obstack_free (&temp_obstack);
2024 mutable_map = addrmap_create_mutable (&temp_obstack);
2025
2026 iter = index->address_table;
2027 end = iter + index->address_table_size;
2028
2029 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2030
2031 while (iter < end)
2032 {
2033 ULONGEST hi, lo, cu_index;
2034 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2035 iter += 8;
2036 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2037 iter += 8;
2038 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2039 iter += 4;
2040
2041 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2042 dw2_get_cu (cu_index));
9291a0cd
TT
2043 }
2044
2045 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2046 &objfile->objfile_obstack);
2047 do_cleanups (cleanup);
2048}
2049
59d7bcaf
JK
2050/* The hash function for strings in the mapped index. This is the same as
2051 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2052 implementation. This is necessary because the hash function is tied to the
2053 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2054 SYMBOL_HASH_NEXT.
2055
2056 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2057
9291a0cd 2058static hashval_t
559a7a62 2059mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2060{
2061 const unsigned char *str = (const unsigned char *) p;
2062 hashval_t r = 0;
2063 unsigned char c;
2064
2065 while ((c = *str++) != 0)
559a7a62
JK
2066 {
2067 if (index_version >= 5)
2068 c = tolower (c);
2069 r = r * 67 + c - 113;
2070 }
9291a0cd
TT
2071
2072 return r;
2073}
2074
2075/* Find a slot in the mapped index INDEX for the object named NAME.
2076 If NAME is found, set *VEC_OUT to point to the CU vector in the
2077 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2078
9291a0cd
TT
2079static int
2080find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2081 offset_type **vec_out)
2082{
0cf03b49
JK
2083 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2084 offset_type hash;
9291a0cd 2085 offset_type slot, step;
559a7a62 2086 int (*cmp) (const char *, const char *);
9291a0cd 2087
0cf03b49
JK
2088 if (current_language->la_language == language_cplus
2089 || current_language->la_language == language_java
2090 || current_language->la_language == language_fortran)
2091 {
2092 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2093 not contain any. */
2094 const char *paren = strchr (name, '(');
2095
2096 if (paren)
2097 {
2098 char *dup;
2099
2100 dup = xmalloc (paren - name + 1);
2101 memcpy (dup, name, paren - name);
2102 dup[paren - name] = 0;
2103
2104 make_cleanup (xfree, dup);
2105 name = dup;
2106 }
2107 }
2108
559a7a62
JK
2109 /* Index version 4 did not support case insensitive searches. But the
2110 indexes for case insensitive languages are built in lowercase, therefore
2111 simulate our NAME being searched is also lowercased. */
2112 hash = mapped_index_string_hash ((index->version == 4
2113 && case_sensitivity == case_sensitive_off
2114 ? 5 : index->version),
2115 name);
2116
3876f04e
DE
2117 slot = hash & (index->symbol_table_slots - 1);
2118 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2119 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2120
2121 for (;;)
2122 {
2123 /* Convert a slot number to an offset into the table. */
2124 offset_type i = 2 * slot;
2125 const char *str;
3876f04e 2126 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2127 {
2128 do_cleanups (back_to);
2129 return 0;
2130 }
9291a0cd 2131
3876f04e 2132 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2133 if (!cmp (name, str))
9291a0cd
TT
2134 {
2135 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2136 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2137 do_cleanups (back_to);
9291a0cd
TT
2138 return 1;
2139 }
2140
3876f04e 2141 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2142 }
2143}
2144
2145/* Read the index file. If everything went ok, initialize the "quick"
2146 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2147
9291a0cd
TT
2148static int
2149dwarf2_read_index (struct objfile *objfile)
2150{
9291a0cd
TT
2151 char *addr;
2152 struct mapped_index *map;
b3b272e1 2153 offset_type *metadata;
ac0b195c
KW
2154 const gdb_byte *cu_list;
2155 const gdb_byte *types_list = NULL;
2156 offset_type version, cu_list_elements;
2157 offset_type types_list_elements = 0;
1fd400ff 2158 int i;
9291a0cd 2159
9e0ac564 2160 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2161 return 0;
82430852
JK
2162
2163 /* Older elfutils strip versions could keep the section in the main
2164 executable while splitting it for the separate debug info file. */
2165 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2166 & SEC_HAS_CONTENTS) == 0)
2167 return 0;
2168
9291a0cd
TT
2169 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2170
2171 addr = dwarf2_per_objfile->gdb_index.buffer;
2172 /* Version check. */
1fd400ff 2173 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2174 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2175 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2176 contained incomplete addrmap. So, it seems better to just ignore such
559a7a62
JK
2177 indices. Index version 4 uses a different hash function than index
2178 version 5 and later. */
831adc1f 2179 if (version < 4)
9291a0cd 2180 return 0;
594e8718
JK
2181 /* Indexes with higher version than the one supported by GDB may be no
2182 longer backward compatible. */
559a7a62 2183 if (version > 5)
594e8718 2184 return 0;
9291a0cd
TT
2185
2186 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2187 map->version = version;
b3b272e1 2188 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2189
2190 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2191
2192 i = 0;
2193 cu_list = addr + MAYBE_SWAP (metadata[i]);
2194 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2195 / 8);
1fd400ff
TT
2196 ++i;
2197
987d643c
TT
2198 types_list = addr + MAYBE_SWAP (metadata[i]);
2199 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2200 - MAYBE_SWAP (metadata[i]))
2201 / 8);
2202 ++i;
1fd400ff
TT
2203
2204 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2205 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2206 - MAYBE_SWAP (metadata[i]));
2207 ++i;
2208
3876f04e
DE
2209 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2210 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2211 - MAYBE_SWAP (metadata[i]))
2212 / (2 * sizeof (offset_type)));
1fd400ff 2213 ++i;
9291a0cd 2214
1fd400ff
TT
2215 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2216
2217 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2218 return 0;
2219
8b70b953
TT
2220 if (types_list_elements)
2221 {
2222 struct dwarf2_section_info *section;
2223
2224 /* We can only handle a single .debug_types when we have an
2225 index. */
2226 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2227 return 0;
2228
2229 section = VEC_index (dwarf2_section_info_def,
2230 dwarf2_per_objfile->types, 0);
2231
2232 if (!create_signatured_type_table_from_index (objfile, section,
2233 types_list,
2234 types_list_elements))
2235 return 0;
2236 }
9291a0cd
TT
2237
2238 create_addrmap_from_index (objfile, map);
2239
2240 dwarf2_per_objfile->index_table = map;
2241 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2242 dwarf2_per_objfile->quick_file_names_table =
2243 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2244
2245 return 1;
2246}
2247
2248/* A helper for the "quick" functions which sets the global
2249 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2250
9291a0cd
TT
2251static void
2252dw2_setup (struct objfile *objfile)
2253{
2254 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2255 gdb_assert (dwarf2_per_objfile);
2256}
2257
2258/* A helper for the "quick" functions which attempts to read the line
2259 table for THIS_CU. */
2fdf6df6 2260
7b9f3c50
DE
2261static struct quick_file_names *
2262dw2_get_file_names (struct objfile *objfile,
2263 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2264{
2265 bfd *abfd = objfile->obfd;
7b9f3c50 2266 struct line_header *lh;
9291a0cd
TT
2267 struct attribute *attr;
2268 struct cleanup *cleanups;
2269 struct die_info *comp_unit_die;
36374493 2270 struct dwarf2_section_info* sec;
9291a0cd
TT
2271 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2272 int has_children, i;
2273 struct dwarf2_cu cu;
2274 unsigned int bytes_read, buffer_size;
2275 struct die_reader_specs reader_specs;
2276 char *name, *comp_dir;
7b9f3c50
DE
2277 void **slot;
2278 struct quick_file_names *qfn;
2279 unsigned int line_offset;
9291a0cd 2280
7b9f3c50
DE
2281 if (this_cu->v.quick->file_names != NULL)
2282 return this_cu->v.quick->file_names;
2283 /* If we know there is no line data, no point in looking again. */
2284 if (this_cu->v.quick->no_file_data)
2285 return NULL;
9291a0cd 2286
9816fde3 2287 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2288 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2289
8b70b953
TT
2290 if (this_cu->debug_type_section)
2291 sec = this_cu->debug_type_section;
36374493
DE
2292 else
2293 sec = &dwarf2_per_objfile->info;
2294 dwarf2_read_section (objfile, sec);
2295 buffer_size = sec->size;
2296 buffer = sec->buffer;
9291a0cd
TT
2297 info_ptr = buffer + this_cu->offset;
2298 beg_of_comp_unit = info_ptr;
2299
2300 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2301 buffer, buffer_size,
2302 abfd);
2303
2304 /* Complete the cu_header. */
2305 cu.header.offset = beg_of_comp_unit - buffer;
2306 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2307
2308 this_cu->cu = &cu;
2309 cu.per_cu = this_cu;
2310
2311 dwarf2_read_abbrevs (abfd, &cu);
2312 make_cleanup (dwarf2_free_abbrev_table, &cu);
2313
8b70b953 2314 if (this_cu->debug_type_section)
9291a0cd
TT
2315 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2316 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2317 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2318 &has_children);
9291a0cd 2319
7b9f3c50
DE
2320 lh = NULL;
2321 slot = NULL;
2322 line_offset = 0;
9291a0cd
TT
2323 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2324 if (attr)
2325 {
7b9f3c50
DE
2326 struct quick_file_names find_entry;
2327
2328 line_offset = DW_UNSND (attr);
2329
2330 /* We may have already read in this line header (TU line header sharing).
2331 If we have we're done. */
2332 find_entry.offset = line_offset;
2333 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2334 &find_entry, INSERT);
2335 if (*slot != NULL)
2336 {
2337 do_cleanups (cleanups);
2338 this_cu->v.quick->file_names = *slot;
2339 return *slot;
2340 }
2341
9291a0cd
TT
2342 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2343 }
2344 if (lh == NULL)
2345 {
2346 do_cleanups (cleanups);
7b9f3c50
DE
2347 this_cu->v.quick->no_file_data = 1;
2348 return NULL;
9291a0cd
TT
2349 }
2350
7b9f3c50
DE
2351 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2352 qfn->offset = line_offset;
2353 gdb_assert (slot != NULL);
2354 *slot = qfn;
9291a0cd 2355
7b9f3c50 2356 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2357
7b9f3c50
DE
2358 qfn->num_file_names = lh->num_file_names;
2359 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2360 lh->num_file_names * sizeof (char *));
9291a0cd 2361 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2362 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2363 qfn->real_names = NULL;
9291a0cd 2364
7b9f3c50 2365 free_line_header (lh);
9291a0cd 2366 do_cleanups (cleanups);
7b9f3c50
DE
2367
2368 this_cu->v.quick->file_names = qfn;
2369 return qfn;
9291a0cd
TT
2370}
2371
2372/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2373 real path for a given file name from the line table. */
2fdf6df6 2374
9291a0cd 2375static const char *
7b9f3c50
DE
2376dw2_get_real_path (struct objfile *objfile,
2377 struct quick_file_names *qfn, int index)
9291a0cd 2378{
7b9f3c50
DE
2379 if (qfn->real_names == NULL)
2380 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2381 qfn->num_file_names, sizeof (char *));
9291a0cd 2382
7b9f3c50
DE
2383 if (qfn->real_names[index] == NULL)
2384 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2385
7b9f3c50 2386 return qfn->real_names[index];
9291a0cd
TT
2387}
2388
2389static struct symtab *
2390dw2_find_last_source_symtab (struct objfile *objfile)
2391{
2392 int index;
ae2de4f8 2393
9291a0cd
TT
2394 dw2_setup (objfile);
2395 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2396 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2397}
2398
7b9f3c50
DE
2399/* Traversal function for dw2_forget_cached_source_info. */
2400
2401static int
2402dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2403{
7b9f3c50 2404 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2405
7b9f3c50 2406 if (file_data->real_names)
9291a0cd 2407 {
7b9f3c50 2408 int i;
9291a0cd 2409
7b9f3c50 2410 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2411 {
7b9f3c50
DE
2412 xfree ((void*) file_data->real_names[i]);
2413 file_data->real_names[i] = NULL;
9291a0cd
TT
2414 }
2415 }
7b9f3c50
DE
2416
2417 return 1;
2418}
2419
2420static void
2421dw2_forget_cached_source_info (struct objfile *objfile)
2422{
2423 dw2_setup (objfile);
2424
2425 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2426 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2427}
2428
2429static int
2430dw2_lookup_symtab (struct objfile *objfile, const char *name,
2431 const char *full_path, const char *real_path,
2432 struct symtab **result)
2433{
2434 int i;
2435 int check_basename = lbasename (name) == name;
2436 struct dwarf2_per_cu_data *base_cu = NULL;
2437
2438 dw2_setup (objfile);
ae2de4f8 2439
1fd400ff
TT
2440 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2441 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2442 {
2443 int j;
e254ef6a 2444 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2445 struct quick_file_names *file_data;
9291a0cd 2446
e254ef6a 2447 if (per_cu->v.quick->symtab)
9291a0cd
TT
2448 continue;
2449
7b9f3c50
DE
2450 file_data = dw2_get_file_names (objfile, per_cu);
2451 if (file_data == NULL)
9291a0cd
TT
2452 continue;
2453
7b9f3c50 2454 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2455 {
7b9f3c50 2456 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2457
2458 if (FILENAME_CMP (name, this_name) == 0)
2459 {
e254ef6a 2460 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2461 return 1;
2462 }
2463
2464 if (check_basename && ! base_cu
2465 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2466 base_cu = per_cu;
9291a0cd
TT
2467
2468 if (full_path != NULL)
2469 {
7b9f3c50
DE
2470 const char *this_real_name = dw2_get_real_path (objfile,
2471 file_data, j);
9291a0cd 2472
7b9f3c50
DE
2473 if (this_real_name != NULL
2474 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2475 {
e254ef6a 2476 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2477 return 1;
2478 }
2479 }
2480
2481 if (real_path != NULL)
2482 {
7b9f3c50
DE
2483 const char *this_real_name = dw2_get_real_path (objfile,
2484 file_data, j);
9291a0cd 2485
7b9f3c50
DE
2486 if (this_real_name != NULL
2487 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2488 {
74dd2ca6
DE
2489 *result = dw2_instantiate_symtab (objfile, per_cu);
2490 return 1;
9291a0cd
TT
2491 }
2492 }
2493 }
2494 }
2495
2496 if (base_cu)
2497 {
2498 *result = dw2_instantiate_symtab (objfile, base_cu);
2499 return 1;
2500 }
2501
2502 return 0;
2503}
2504
2505static struct symtab *
2506dw2_lookup_symbol (struct objfile *objfile, int block_index,
2507 const char *name, domain_enum domain)
2508{
774b6a14 2509 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2510 instead. */
2511 return NULL;
2512}
2513
2514/* A helper function that expands all symtabs that hold an object
2515 named NAME. */
2fdf6df6 2516
9291a0cd
TT
2517static void
2518dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2519{
2520 dw2_setup (objfile);
2521
ae2de4f8 2522 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2523 if (dwarf2_per_objfile->index_table)
2524 {
2525 offset_type *vec;
2526
2527 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2528 name, &vec))
2529 {
2530 offset_type i, len = MAYBE_SWAP (*vec);
2531 for (i = 0; i < len; ++i)
2532 {
2533 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2534 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2535
e254ef6a 2536 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2537 }
2538 }
2539 }
2540}
2541
774b6a14
TT
2542static void
2543dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2544 enum block_enum block_kind, const char *name,
774b6a14 2545 domain_enum domain)
9291a0cd 2546{
774b6a14 2547 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2548}
2549
2550static void
2551dw2_print_stats (struct objfile *objfile)
2552{
2553 int i, count;
2554
2555 dw2_setup (objfile);
2556 count = 0;
1fd400ff
TT
2557 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2558 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2559 {
e254ef6a 2560 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2561
e254ef6a 2562 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2563 ++count;
2564 }
2565 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2566}
2567
2568static void
2569dw2_dump (struct objfile *objfile)
2570{
2571 /* Nothing worth printing. */
2572}
2573
2574static void
2575dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2576 struct section_offsets *delta)
2577{
2578 /* There's nothing to relocate here. */
2579}
2580
2581static void
2582dw2_expand_symtabs_for_function (struct objfile *objfile,
2583 const char *func_name)
2584{
2585 dw2_do_expand_symtabs_matching (objfile, func_name);
2586}
2587
2588static void
2589dw2_expand_all_symtabs (struct objfile *objfile)
2590{
2591 int i;
2592
2593 dw2_setup (objfile);
1fd400ff
TT
2594
2595 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2596 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2597 {
e254ef6a 2598 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2599
e254ef6a 2600 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2601 }
2602}
2603
2604static void
2605dw2_expand_symtabs_with_filename (struct objfile *objfile,
2606 const char *filename)
2607{
2608 int i;
2609
2610 dw2_setup (objfile);
d4637a04
DE
2611
2612 /* We don't need to consider type units here.
2613 This is only called for examining code, e.g. expand_line_sal.
2614 There can be an order of magnitude (or more) more type units
2615 than comp units, and we avoid them if we can. */
2616
2617 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2618 {
2619 int j;
e254ef6a 2620 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2621 struct quick_file_names *file_data;
9291a0cd 2622
e254ef6a 2623 if (per_cu->v.quick->symtab)
9291a0cd
TT
2624 continue;
2625
7b9f3c50
DE
2626 file_data = dw2_get_file_names (objfile, per_cu);
2627 if (file_data == NULL)
9291a0cd
TT
2628 continue;
2629
7b9f3c50 2630 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2631 {
7b9f3c50 2632 const char *this_name = file_data->file_names[j];
1ef75ecc 2633 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2634 {
e254ef6a 2635 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2636 break;
2637 }
2638 }
2639 }
2640}
2641
dd786858 2642static const char *
9291a0cd
TT
2643dw2_find_symbol_file (struct objfile *objfile, const char *name)
2644{
e254ef6a 2645 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2646 offset_type *vec;
7b9f3c50 2647 struct quick_file_names *file_data;
9291a0cd
TT
2648
2649 dw2_setup (objfile);
2650
ae2de4f8 2651 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2652 if (!dwarf2_per_objfile->index_table)
2653 return NULL;
2654
2655 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2656 name, &vec))
2657 return NULL;
2658
2659 /* Note that this just looks at the very first one named NAME -- but
2660 actually we are looking for a function. find_main_filename
2661 should be rewritten so that it doesn't require a custom hook. It
2662 could just use the ordinary symbol tables. */
2663 /* vec[0] is the length, which must always be >0. */
e254ef6a 2664 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2665
7b9f3c50
DE
2666 file_data = dw2_get_file_names (objfile, per_cu);
2667 if (file_data == NULL)
9291a0cd
TT
2668 return NULL;
2669
7b9f3c50 2670 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2671}
2672
2673static void
40658b94
PH
2674dw2_map_matching_symbols (const char * name, domain_enum namespace,
2675 struct objfile *objfile, int global,
2676 int (*callback) (struct block *,
2677 struct symbol *, void *),
2edb89d3
JK
2678 void *data, symbol_compare_ftype *match,
2679 symbol_compare_ftype *ordered_compare)
9291a0cd 2680{
40658b94 2681 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2682 current language is Ada for a non-Ada objfile using GNU index. As Ada
2683 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2684}
2685
2686static void
2687dw2_expand_symtabs_matching (struct objfile *objfile,
2688 int (*file_matcher) (const char *, void *),
2689 int (*name_matcher) (const char *, void *),
8903c50d 2690 enum search_domain kind,
9291a0cd
TT
2691 void *data)
2692{
2693 int i;
2694 offset_type iter;
4b5246aa 2695 struct mapped_index *index;
9291a0cd
TT
2696
2697 dw2_setup (objfile);
ae2de4f8
DE
2698
2699 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2700 if (!dwarf2_per_objfile->index_table)
2701 return;
4b5246aa 2702 index = dwarf2_per_objfile->index_table;
9291a0cd 2703
7b08b9eb
JK
2704 if (file_matcher != NULL)
2705 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2706 + dwarf2_per_objfile->n_type_comp_units); ++i)
2707 {
2708 int j;
2709 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2710 struct quick_file_names *file_data;
2711
2712 per_cu->v.quick->mark = 0;
2713 if (per_cu->v.quick->symtab)
2714 continue;
2715
2716 file_data = dw2_get_file_names (objfile, per_cu);
2717 if (file_data == NULL)
2718 continue;
2719
2720 for (j = 0; j < file_data->num_file_names; ++j)
2721 {
2722 if (file_matcher (file_data->file_names[j], data))
2723 {
2724 per_cu->v.quick->mark = 1;
2725 break;
2726 }
2727 }
2728 }
9291a0cd 2729
3876f04e 2730 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2731 {
2732 offset_type idx = 2 * iter;
2733 const char *name;
2734 offset_type *vec, vec_len, vec_idx;
2735
3876f04e 2736 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2737 continue;
2738
3876f04e 2739 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2740
2741 if (! (*name_matcher) (name, data))
2742 continue;
2743
2744 /* The name was matched, now expand corresponding CUs that were
2745 marked. */
4b5246aa 2746 vec = (offset_type *) (index->constant_pool
3876f04e 2747 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2748 vec_len = MAYBE_SWAP (vec[0]);
2749 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2750 {
e254ef6a 2751 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2752
e254ef6a 2753 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2754 if (file_matcher == NULL || per_cu->v.quick->mark)
e254ef6a 2755 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2756 }
2757 }
2758}
2759
2760static struct symtab *
2761dw2_find_pc_sect_symtab (struct objfile *objfile,
2762 struct minimal_symbol *msymbol,
2763 CORE_ADDR pc,
2764 struct obj_section *section,
2765 int warn_if_readin)
2766{
2767 struct dwarf2_per_cu_data *data;
2768
2769 dw2_setup (objfile);
2770
2771 if (!objfile->psymtabs_addrmap)
2772 return NULL;
2773
2774 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2775 if (!data)
2776 return NULL;
2777
2778 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2779 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2780 paddress (get_objfile_arch (objfile), pc));
2781
2782 return dw2_instantiate_symtab (objfile, data);
2783}
2784
9291a0cd 2785static void
44b13c5a 2786dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
9291a0cd
TT
2787 void *data)
2788{
2789 int i;
2790
2791 dw2_setup (objfile);
ae2de4f8 2792
1fd400ff
TT
2793 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2794 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2795 {
2796 int j;
e254ef6a 2797 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2798 struct quick_file_names *file_data;
9291a0cd 2799
e254ef6a 2800 if (per_cu->v.quick->symtab)
9291a0cd
TT
2801 continue;
2802
7b9f3c50
DE
2803 file_data = dw2_get_file_names (objfile, per_cu);
2804 if (file_data == NULL)
9291a0cd
TT
2805 continue;
2806
7b9f3c50 2807 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2808 {
7b9f3c50
DE
2809 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2810 j);
2811 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2812 }
2813 }
2814}
2815
2816static int
2817dw2_has_symbols (struct objfile *objfile)
2818{
2819 return 1;
2820}
2821
2822const struct quick_symbol_functions dwarf2_gdb_index_functions =
2823{
2824 dw2_has_symbols,
2825 dw2_find_last_source_symtab,
2826 dw2_forget_cached_source_info,
2827 dw2_lookup_symtab,
2828 dw2_lookup_symbol,
774b6a14 2829 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2830 dw2_print_stats,
2831 dw2_dump,
2832 dw2_relocate,
2833 dw2_expand_symtabs_for_function,
2834 dw2_expand_all_symtabs,
2835 dw2_expand_symtabs_with_filename,
2836 dw2_find_symbol_file,
40658b94 2837 dw2_map_matching_symbols,
9291a0cd
TT
2838 dw2_expand_symtabs_matching,
2839 dw2_find_pc_sect_symtab,
9291a0cd
TT
2840 dw2_map_symbol_filenames
2841};
2842
2843/* Initialize for reading DWARF for this objfile. Return 0 if this
2844 file will use psymtabs, or 1 if using the GNU index. */
2845
2846int
2847dwarf2_initialize_objfile (struct objfile *objfile)
2848{
2849 /* If we're about to read full symbols, don't bother with the
2850 indices. In this case we also don't care if some other debug
2851 format is making psymtabs, because they are all about to be
2852 expanded anyway. */
2853 if ((objfile->flags & OBJF_READNOW))
2854 {
2855 int i;
2856
2857 dwarf2_per_objfile->using_index = 1;
2858 create_all_comp_units (objfile);
1fd400ff 2859 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2860 dwarf2_per_objfile->quick_file_names_table =
2861 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2862
1fd400ff
TT
2863 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2864 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2865 {
e254ef6a 2866 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2867
e254ef6a
DE
2868 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2869 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2870 }
2871
2872 /* Return 1 so that gdb sees the "quick" functions. However,
2873 these functions will be no-ops because we will have expanded
2874 all symtabs. */
2875 return 1;
2876 }
2877
2878 if (dwarf2_read_index (objfile))
2879 return 1;
2880
9291a0cd
TT
2881 return 0;
2882}
2883
2884\f
2885
dce234bc
PP
2886/* Build a partial symbol table. */
2887
2888void
f29dff0a 2889dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2890{
f29dff0a 2891 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2892 {
2893 init_psymbol_list (objfile, 1024);
2894 }
2895
d146bf1e 2896 dwarf2_build_psymtabs_hard (objfile);
c906108c 2897}
c906108c 2898
45452591
DE
2899/* Return TRUE if OFFSET is within CU_HEADER. */
2900
2901static inline int
2902offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2903{
2904 unsigned int bottom = cu_header->offset;
2905 unsigned int top = (cu_header->offset
2906 + cu_header->length
2907 + cu_header->initial_length_size);
9a619af0 2908
45452591
DE
2909 return (offset >= bottom && offset < top);
2910}
2911
93311388
DE
2912/* Read in the comp unit header information from the debug_info at info_ptr.
2913 NOTE: This leaves members offset, first_die_offset to be filled in
2914 by the caller. */
107d2387 2915
fe1b8b76 2916static gdb_byte *
107d2387 2917read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2918 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2919{
2920 int signed_addr;
891d2f0b 2921 unsigned int bytes_read;
c764a876
DE
2922
2923 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2924 cu_header->initial_length_size = bytes_read;
2925 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2926 info_ptr += bytes_read;
107d2387
AC
2927 cu_header->version = read_2_bytes (abfd, info_ptr);
2928 info_ptr += 2;
613e1657 2929 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2930 &bytes_read);
613e1657 2931 info_ptr += bytes_read;
107d2387
AC
2932 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2933 info_ptr += 1;
2934 signed_addr = bfd_get_sign_extend_vma (abfd);
2935 if (signed_addr < 0)
8e65ff28 2936 internal_error (__FILE__, __LINE__,
e2e0b3e5 2937 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2938 cu_header->signed_addr_p = signed_addr;
c764a876 2939
107d2387
AC
2940 return info_ptr;
2941}
2942
fe1b8b76
JB
2943static gdb_byte *
2944partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2945 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2946 bfd *abfd)
2947{
fe1b8b76 2948 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2949
2950 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2951
2dc7f7b3 2952 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2953 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2954 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2955 bfd_get_filename (abfd));
72bf9492 2956
9e0ac564
TT
2957 if (header->abbrev_offset
2958 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2959 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
2960 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2961 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2962 (long) header->abbrev_offset,
93311388 2963 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2964 bfd_get_filename (abfd));
2965
2966 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2967 > buffer + buffer_size)
8a3fe4f8
AC
2968 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2969 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2970 (long) header->length,
93311388 2971 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2972 bfd_get_filename (abfd));
2973
2974 return info_ptr;
2975}
2976
348e048f
DE
2977/* Read in the types comp unit header information from .debug_types entry at
2978 types_ptr. The result is a pointer to one past the end of the header. */
2979
2980static gdb_byte *
2981read_type_comp_unit_head (struct comp_unit_head *cu_header,
8b70b953 2982 struct dwarf2_section_info *section,
348e048f
DE
2983 ULONGEST *signature,
2984 gdb_byte *types_ptr, bfd *abfd)
2985{
348e048f
DE
2986 gdb_byte *initial_types_ptr = types_ptr;
2987
8b70b953
TT
2988 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
2989 cu_header->offset = types_ptr - section->buffer;
348e048f
DE
2990
2991 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2992
2993 *signature = read_8_bytes (abfd, types_ptr);
2994 types_ptr += 8;
2995 types_ptr += cu_header->offset_size;
2996 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2997
2998 return types_ptr;
2999}
3000
aaa75496
JB
3001/* Allocate a new partial symtab for file named NAME and mark this new
3002 partial symtab as being an include of PST. */
3003
3004static void
3005dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3006 struct objfile *objfile)
3007{
3008 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3009
3010 subpst->section_offsets = pst->section_offsets;
3011 subpst->textlow = 0;
3012 subpst->texthigh = 0;
3013
3014 subpst->dependencies = (struct partial_symtab **)
3015 obstack_alloc (&objfile->objfile_obstack,
3016 sizeof (struct partial_symtab *));
3017 subpst->dependencies[0] = pst;
3018 subpst->number_of_dependencies = 1;
3019
3020 subpst->globals_offset = 0;
3021 subpst->n_global_syms = 0;
3022 subpst->statics_offset = 0;
3023 subpst->n_static_syms = 0;
3024 subpst->symtab = NULL;
3025 subpst->read_symtab = pst->read_symtab;
3026 subpst->readin = 0;
3027
3028 /* No private part is necessary for include psymtabs. This property
3029 can be used to differentiate between such include psymtabs and
10b3939b 3030 the regular ones. */
58a9656e 3031 subpst->read_symtab_private = NULL;
aaa75496
JB
3032}
3033
3034/* Read the Line Number Program data and extract the list of files
3035 included by the source file represented by PST. Build an include
d85a05f0 3036 partial symtab for each of these included files. */
aaa75496
JB
3037
3038static void
3039dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 3040 struct die_info *die,
aaa75496
JB
3041 struct partial_symtab *pst)
3042{
3043 struct objfile *objfile = cu->objfile;
3044 bfd *abfd = objfile->obfd;
d85a05f0
DJ
3045 struct line_header *lh = NULL;
3046 struct attribute *attr;
aaa75496 3047
d85a05f0
DJ
3048 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3049 if (attr)
3050 {
3051 unsigned int line_offset = DW_UNSND (attr);
9a619af0 3052
d85a05f0
DJ
3053 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3054 }
aaa75496
JB
3055 if (lh == NULL)
3056 return; /* No linetable, so no includes. */
3057
c6da4cef
DE
3058 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3059 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
3060
3061 free_line_header (lh);
3062}
3063
348e048f
DE
3064static hashval_t
3065hash_type_signature (const void *item)
3066{
3067 const struct signatured_type *type_sig = item;
9a619af0 3068
348e048f
DE
3069 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3070 return type_sig->signature;
3071}
3072
3073static int
3074eq_type_signature (const void *item_lhs, const void *item_rhs)
3075{
3076 const struct signatured_type *lhs = item_lhs;
3077 const struct signatured_type *rhs = item_rhs;
9a619af0 3078
348e048f
DE
3079 return lhs->signature == rhs->signature;
3080}
3081
1fd400ff
TT
3082/* Allocate a hash table for signatured types. */
3083
3084static htab_t
673bfd45 3085allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3086{
3087 return htab_create_alloc_ex (41,
3088 hash_type_signature,
3089 eq_type_signature,
3090 NULL,
3091 &objfile->objfile_obstack,
3092 hashtab_obstack_allocate,
3093 dummy_obstack_deallocate);
3094}
3095
3096/* A helper function to add a signatured type CU to a list. */
3097
3098static int
3099add_signatured_type_cu_to_list (void **slot, void *datum)
3100{
3101 struct signatured_type *sigt = *slot;
3102 struct dwarf2_per_cu_data ***datap = datum;
3103
3104 **datap = &sigt->per_cu;
3105 ++*datap;
3106
3107 return 1;
3108}
3109
348e048f
DE
3110/* Create the hash table of all entries in the .debug_types section.
3111 The result is zero if there is an error (e.g. missing .debug_types section),
3112 otherwise non-zero. */
3113
3114static int
3115create_debug_types_hash_table (struct objfile *objfile)
3116{
8b70b953 3117 htab_t types_htab = NULL;
1fd400ff 3118 struct dwarf2_per_cu_data **iter;
8b70b953
TT
3119 int ix;
3120 struct dwarf2_section_info *section;
348e048f 3121
8b70b953 3122 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
348e048f
DE
3123 {
3124 dwarf2_per_objfile->signatured_types = NULL;
3125 return 0;
3126 }
3127
8b70b953
TT
3128 for (ix = 0;
3129 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3130 ix, section);
3131 ++ix)
3132 {
3133 gdb_byte *info_ptr, *end_ptr;
348e048f 3134
8b70b953
TT
3135 dwarf2_read_section (objfile, section);
3136 info_ptr = section->buffer;
348e048f 3137
8b70b953
TT
3138 if (info_ptr == NULL)
3139 continue;
348e048f 3140
8b70b953
TT
3141 if (types_htab == NULL)
3142 types_htab = allocate_signatured_type_table (objfile);
348e048f 3143
8b70b953
TT
3144 if (dwarf2_die_debug)
3145 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3146
3147 end_ptr = info_ptr + section->size;
3148 while (info_ptr < end_ptr)
3149 {
3150 unsigned int offset;
3151 unsigned int offset_size;
3152 unsigned int type_offset;
3153 unsigned int length, initial_length_size;
3154 unsigned short version;
3155 ULONGEST signature;
3156 struct signatured_type *type_sig;
3157 void **slot;
3158 gdb_byte *ptr = info_ptr;
348e048f 3159
8b70b953 3160 offset = ptr - section->buffer;
348e048f 3161
8b70b953
TT
3162 /* We need to read the type's signature in order to build the hash
3163 table, but we don't need to read anything else just yet. */
348e048f 3164
8b70b953
TT
3165 /* Sanity check to ensure entire cu is present. */
3166 length = read_initial_length (objfile->obfd, ptr,
3167 &initial_length_size);
3168 if (ptr + length + initial_length_size > end_ptr)
3169 {
3170 complaint (&symfile_complaints,
3171 _("debug type entry runs off end "
3172 "of `.debug_types' section, ignored"));
3173 break;
3174 }
348e048f 3175
8b70b953
TT
3176 offset_size = initial_length_size == 4 ? 4 : 8;
3177 ptr += initial_length_size;
3178 version = bfd_get_16 (objfile->obfd, ptr);
3179 ptr += 2;
3180 ptr += offset_size; /* abbrev offset */
3181 ptr += 1; /* address size */
3182 signature = bfd_get_64 (objfile->obfd, ptr);
3183 ptr += 8;
3184 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3185
3186 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3187 memset (type_sig, 0, sizeof (*type_sig));
3188 type_sig->signature = signature;
3189 type_sig->type_offset = type_offset;
3190 type_sig->per_cu.objfile = objfile;
3191 type_sig->per_cu.debug_type_section = section;
3192 type_sig->per_cu.offset = offset;
3193
3194 slot = htab_find_slot (types_htab, type_sig, INSERT);
3195 gdb_assert (slot != NULL);
3196 if (*slot != NULL)
3197 {
3198 const struct signatured_type *dup_sig = *slot;
b3c8eb43 3199
8b70b953
TT
3200 complaint (&symfile_complaints,
3201 _("debug type entry at offset 0x%x is duplicate to the "
3202 "entry at offset 0x%x, signature 0x%s"),
3203 offset, dup_sig->per_cu.offset,
3204 phex (signature, sizeof (signature)));
3205 gdb_assert (signature == dup_sig->signature);
3206 }
3207 *slot = type_sig;
348e048f 3208
8b70b953
TT
3209 if (dwarf2_die_debug)
3210 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3211 offset, phex (signature, sizeof (signature)));
348e048f 3212
8b70b953
TT
3213 info_ptr = info_ptr + initial_length_size + length;
3214 }
348e048f
DE
3215 }
3216
3217 dwarf2_per_objfile->signatured_types = types_htab;
3218
1fd400ff
TT
3219 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3220 dwarf2_per_objfile->type_comp_units
3221 = obstack_alloc (&objfile->objfile_obstack,
3222 dwarf2_per_objfile->n_type_comp_units
3223 * sizeof (struct dwarf2_per_cu_data *));
3224 iter = &dwarf2_per_objfile->type_comp_units[0];
3225 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3226 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3227 == dwarf2_per_objfile->n_type_comp_units);
3228
348e048f
DE
3229 return 1;
3230}
3231
3232/* Lookup a signature based type.
3233 Returns NULL if SIG is not present in the table. */
3234
3235static struct signatured_type *
3236lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3237{
3238 struct signatured_type find_entry, *entry;
3239
3240 if (dwarf2_per_objfile->signatured_types == NULL)
3241 {
3242 complaint (&symfile_complaints,
55f1336d 3243 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3244 return 0;
3245 }
3246
3247 find_entry.signature = sig;
3248 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3249 return entry;
3250}
3251
d85a05f0
DJ
3252/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3253
3254static void
3255init_cu_die_reader (struct die_reader_specs *reader,
3256 struct dwarf2_cu *cu)
3257{
3258 reader->abfd = cu->objfile->obfd;
3259 reader->cu = cu;
8b70b953 3260 if (cu->per_cu->debug_type_section)
be391dca 3261 {
8b70b953
TT
3262 gdb_assert (cu->per_cu->debug_type_section->readin);
3263 reader->buffer = cu->per_cu->debug_type_section->buffer;
be391dca 3264 }
d85a05f0 3265 else
be391dca
TT
3266 {
3267 gdb_assert (dwarf2_per_objfile->info.readin);
3268 reader->buffer = dwarf2_per_objfile->info.buffer;
3269 }
d85a05f0
DJ
3270}
3271
3272/* Find the base address of the compilation unit for range lists and
3273 location lists. It will normally be specified by DW_AT_low_pc.
3274 In DWARF-3 draft 4, the base address could be overridden by
3275 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3276 compilation units with discontinuous ranges. */
3277
3278static void
3279dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3280{
3281 struct attribute *attr;
3282
3283 cu->base_known = 0;
3284 cu->base_address = 0;
3285
3286 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3287 if (attr)
3288 {
3289 cu->base_address = DW_ADDR (attr);
3290 cu->base_known = 1;
3291 }
3292 else
3293 {
3294 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3295 if (attr)
3296 {
3297 cu->base_address = DW_ADDR (attr);
3298 cu->base_known = 1;
3299 }
3300 }
3301}
3302
348e048f
DE
3303/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3304 to combine the common parts.
93311388 3305 Process a compilation unit for a psymtab.
348e048f
DE
3306 BUFFER is a pointer to the beginning of the dwarf section buffer,
3307 either .debug_info or debug_types.
93311388
DE
3308 INFO_PTR is a pointer to the start of the CU.
3309 Returns a pointer to the next CU. */
aaa75496 3310
93311388
DE
3311static gdb_byte *
3312process_psymtab_comp_unit (struct objfile *objfile,
3313 struct dwarf2_per_cu_data *this_cu,
3314 gdb_byte *buffer, gdb_byte *info_ptr,
3315 unsigned int buffer_size)
c906108c 3316{
c906108c 3317 bfd *abfd = objfile->obfd;
93311388 3318 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3319 struct die_info *comp_unit_die;
c906108c 3320 struct partial_symtab *pst;
5734ee8b 3321 CORE_ADDR baseaddr;
93311388
DE
3322 struct cleanup *back_to_inner;
3323 struct dwarf2_cu cu;
d85a05f0
DJ
3324 int has_children, has_pc_info;
3325 struct attribute *attr;
d85a05f0
DJ
3326 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3327 struct die_reader_specs reader_specs;
3e2a0cee 3328 const char *filename;
c906108c 3329
9816fde3 3330 init_one_comp_unit (&cu, objfile);
93311388 3331 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3332
93311388
DE
3333 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3334 buffer, buffer_size,
3335 abfd);
10b3939b 3336
93311388
DE
3337 /* Complete the cu_header. */
3338 cu.header.offset = beg_of_comp_unit - buffer;
3339 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3340
93311388 3341 cu.list_in_scope = &file_symbols;
af703f96 3342
328c9494
DJ
3343 /* If this compilation unit was already read in, free the
3344 cached copy in order to read it in again. This is
3345 necessary because we skipped some symbols when we first
3346 read in the compilation unit (see load_partial_dies).
3347 This problem could be avoided, but the benefit is
3348 unclear. */
3349 if (this_cu->cu != NULL)
3350 free_one_cached_comp_unit (this_cu->cu);
3351
3352 /* Note that this is a pointer to our stack frame, being
3353 added to a global data structure. It will be cleaned up
3354 in free_stack_comp_unit when we finish with this
3355 compilation unit. */
3356 this_cu->cu = &cu;
d85a05f0
DJ
3357 cu.per_cu = this_cu;
3358
93311388
DE
3359 /* Read the abbrevs for this compilation unit into a table. */
3360 dwarf2_read_abbrevs (abfd, &cu);
3361 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3362
93311388 3363 /* Read the compilation unit die. */
8b70b953 3364 if (this_cu->debug_type_section)
348e048f 3365 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3366 init_cu_die_reader (&reader_specs, &cu);
3367 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3368 &has_children);
93311388 3369
8b70b953 3370 if (this_cu->debug_type_section)
348e048f 3371 {
b3c8eb43
JK
3372 /* LENGTH has not been set yet for type units. */
3373 gdb_assert (this_cu->offset == cu.header.offset);
348e048f
DE
3374 this_cu->length = cu.header.length + cu.header.initial_length_size;
3375 }
d85a05f0 3376 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3377 {
93311388
DE
3378 info_ptr = (beg_of_comp_unit + cu.header.length
3379 + cu.header.initial_length_size);
3380 do_cleanups (back_to_inner);
3381 return info_ptr;
3382 }
72bf9492 3383
9816fde3 3384 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3385
93311388 3386 /* Allocate a new partial symbol table structure. */
d85a05f0 3387 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3388 if (attr == NULL || !DW_STRING (attr))
3389 filename = "";
3390 else
3391 filename = DW_STRING (attr);
93311388 3392 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3393 filename,
93311388
DE
3394 /* TEXTLOW and TEXTHIGH are set below. */
3395 0,
3396 objfile->global_psymbols.next,
3397 objfile->static_psymbols.next);
72bf9492 3398
d85a05f0
DJ
3399 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3400 if (attr != NULL)
3401 pst->dirname = DW_STRING (attr);
72bf9492 3402
e38df1d0 3403 pst->read_symtab_private = this_cu;
72bf9492 3404
93311388 3405 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3406
0963b4bd 3407 /* Store the function that reads in the rest of the symbol table. */
93311388 3408 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3409
9291a0cd 3410 this_cu->v.psymtab = pst;
c906108c 3411
d85a05f0
DJ
3412 dwarf2_find_base_address (comp_unit_die, &cu);
3413
93311388
DE
3414 /* Possibly set the default values of LOWPC and HIGHPC from
3415 `DW_AT_ranges'. */
d85a05f0
DJ
3416 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3417 &best_highpc, &cu, pst);
3418 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3419 /* Store the contiguous range if it is not empty; it can be empty for
3420 CUs with no code. */
3421 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3422 best_lowpc + baseaddr,
3423 best_highpc + baseaddr - 1, pst);
93311388
DE
3424
3425 /* Check if comp unit has_children.
3426 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3427 If not, there's no more debug_info for this comp unit. */
d85a05f0 3428 if (has_children)
93311388
DE
3429 {
3430 struct partial_die_info *first_die;
3431 CORE_ADDR lowpc, highpc;
31ffec48 3432
93311388
DE
3433 lowpc = ((CORE_ADDR) -1);
3434 highpc = ((CORE_ADDR) 0);
c906108c 3435
93311388 3436 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3437
93311388 3438 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3439 ! has_pc_info, &cu);
57c22c6c 3440
93311388
DE
3441 /* If we didn't find a lowpc, set it to highpc to avoid
3442 complaints from `maint check'. */
3443 if (lowpc == ((CORE_ADDR) -1))
3444 lowpc = highpc;
10b3939b 3445
93311388
DE
3446 /* If the compilation unit didn't have an explicit address range,
3447 then use the information extracted from its child dies. */
d85a05f0 3448 if (! has_pc_info)
93311388 3449 {
d85a05f0
DJ
3450 best_lowpc = lowpc;
3451 best_highpc = highpc;
93311388
DE
3452 }
3453 }
d85a05f0
DJ
3454 pst->textlow = best_lowpc + baseaddr;
3455 pst->texthigh = best_highpc + baseaddr;
c906108c 3456
93311388
DE
3457 pst->n_global_syms = objfile->global_psymbols.next -
3458 (objfile->global_psymbols.list + pst->globals_offset);
3459 pst->n_static_syms = objfile->static_psymbols.next -
3460 (objfile->static_psymbols.list + pst->statics_offset);
3461 sort_pst_symbols (pst);
c906108c 3462
93311388
DE
3463 info_ptr = (beg_of_comp_unit + cu.header.length
3464 + cu.header.initial_length_size);
ae038cb0 3465
8b70b953 3466 if (this_cu->debug_type_section)
348e048f
DE
3467 {
3468 /* It's not clear we want to do anything with stmt lists here.
3469 Waiting to see what gcc ultimately does. */
3470 }
d85a05f0 3471 else
93311388
DE
3472 {
3473 /* Get the list of files included in the current compilation unit,
3474 and build a psymtab for each of them. */
d85a05f0 3475 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3476 }
ae038cb0 3477
93311388 3478 do_cleanups (back_to_inner);
ae038cb0 3479
93311388
DE
3480 return info_ptr;
3481}
ff013f42 3482
348e048f
DE
3483/* Traversal function for htab_traverse_noresize.
3484 Process one .debug_types comp-unit. */
3485
3486static int
3487process_type_comp_unit (void **slot, void *info)
3488{
3489 struct signatured_type *entry = (struct signatured_type *) *slot;
3490 struct objfile *objfile = (struct objfile *) info;
3491 struct dwarf2_per_cu_data *this_cu;
3492
3493 this_cu = &entry->per_cu;
348e048f 3494
8b70b953 3495 gdb_assert (this_cu->debug_type_section->readin);
348e048f 3496 process_psymtab_comp_unit (objfile, this_cu,
8b70b953
TT
3497 this_cu->debug_type_section->buffer,
3498 (this_cu->debug_type_section->buffer
3499 + this_cu->offset),
3500 this_cu->debug_type_section->size);
348e048f
DE
3501
3502 return 1;
3503}
3504
3505/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3506 Build partial symbol tables for the .debug_types comp-units. */
3507
3508static void
3509build_type_psymtabs (struct objfile *objfile)
3510{
3511 if (! create_debug_types_hash_table (objfile))
3512 return;
3513
3514 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3515 process_type_comp_unit, objfile);
3516}
3517
60606b2c
TT
3518/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3519
3520static void
3521psymtabs_addrmap_cleanup (void *o)
3522{
3523 struct objfile *objfile = o;
ec61707d 3524
60606b2c
TT
3525 objfile->psymtabs_addrmap = NULL;
3526}
3527
93311388
DE
3528/* Build the partial symbol table by doing a quick pass through the
3529 .debug_info and .debug_abbrev sections. */
72bf9492 3530
93311388 3531static void
c67a9c90 3532dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3533{
93311388 3534 gdb_byte *info_ptr;
60606b2c
TT
3535 struct cleanup *back_to, *addrmap_cleanup;
3536 struct obstack temp_obstack;
93311388 3537
98bfdba5
PA
3538 dwarf2_per_objfile->reading_partial_symbols = 1;
3539
be391dca 3540 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3541 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3542
93311388
DE
3543 /* Any cached compilation units will be linked by the per-objfile
3544 read_in_chain. Make sure to free them when we're done. */
3545 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3546
348e048f
DE
3547 build_type_psymtabs (objfile);
3548
93311388 3549 create_all_comp_units (objfile);
c906108c 3550
60606b2c
TT
3551 /* Create a temporary address map on a temporary obstack. We later
3552 copy this to the final obstack. */
3553 obstack_init (&temp_obstack);
3554 make_cleanup_obstack_free (&temp_obstack);
3555 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3556 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3557
93311388
DE
3558 /* Since the objects we're extracting from .debug_info vary in
3559 length, only the individual functions to extract them (like
3560 read_comp_unit_head and load_partial_die) can really know whether
3561 the buffer is large enough to hold another complete object.
c906108c 3562
93311388
DE
3563 At the moment, they don't actually check that. If .debug_info
3564 holds just one extra byte after the last compilation unit's dies,
3565 then read_comp_unit_head will happily read off the end of the
3566 buffer. read_partial_die is similarly casual. Those functions
3567 should be fixed.
c906108c 3568
93311388
DE
3569 For this loop condition, simply checking whether there's any data
3570 left at all should be sufficient. */
c906108c 3571
93311388
DE
3572 while (info_ptr < (dwarf2_per_objfile->info.buffer
3573 + dwarf2_per_objfile->info.size))
3574 {
3575 struct dwarf2_per_cu_data *this_cu;
dd373385 3576
3e43a32a
MS
3577 this_cu = dwarf2_find_comp_unit (info_ptr
3578 - dwarf2_per_objfile->info.buffer,
93311388 3579 objfile);
aaa75496 3580
93311388
DE
3581 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3582 dwarf2_per_objfile->info.buffer,
3583 info_ptr,
3584 dwarf2_per_objfile->info.size);
c906108c 3585 }
ff013f42
JK
3586
3587 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3588 &objfile->objfile_obstack);
60606b2c 3589 discard_cleanups (addrmap_cleanup);
ff013f42 3590
ae038cb0
DJ
3591 do_cleanups (back_to);
3592}
3593
93311388 3594/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3595
3596static void
93311388
DE
3597load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3598 struct objfile *objfile)
ae038cb0
DJ
3599{
3600 bfd *abfd = objfile->obfd;
fe1b8b76 3601 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3602 struct die_info *comp_unit_die;
ae038cb0 3603 struct dwarf2_cu *cu;
1d9ec526 3604 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3605 int has_children;
3606 struct die_reader_specs reader_specs;
98bfdba5 3607 int read_cu = 0;
ae038cb0 3608
8b70b953 3609 gdb_assert (! this_cu->debug_type_section);
348e048f 3610
be391dca 3611 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3612 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3613 beg_of_comp_unit = info_ptr;
3614
98bfdba5
PA
3615 if (this_cu->cu == NULL)
3616 {
9816fde3
JK
3617 cu = xmalloc (sizeof (*cu));
3618 init_one_comp_unit (cu, objfile);
ae038cb0 3619
98bfdba5 3620 read_cu = 1;
ae038cb0 3621
98bfdba5
PA
3622 /* If an error occurs while loading, release our storage. */
3623 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3624
98bfdba5
PA
3625 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3626 dwarf2_per_objfile->info.buffer,
3627 dwarf2_per_objfile->info.size,
3628 abfd);
ae038cb0 3629
98bfdba5
PA
3630 /* Complete the cu_header. */
3631 cu->header.offset = this_cu->offset;
3632 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3633
3634 /* Link this compilation unit into the compilation unit tree. */
3635 this_cu->cu = cu;
3636 cu->per_cu = this_cu;
98bfdba5
PA
3637
3638 /* Link this CU into read_in_chain. */
3639 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3640 dwarf2_per_objfile->read_in_chain = this_cu;
3641 }
3642 else
3643 {
3644 cu = this_cu->cu;
3645 info_ptr += cu->header.first_die_offset;
3646 }
ae038cb0
DJ
3647
3648 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3649 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3650 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3651 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3652
3653 /* Read the compilation unit die. */
d85a05f0
DJ
3654 init_cu_die_reader (&reader_specs, cu);
3655 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3656 &has_children);
ae038cb0 3657
9816fde3 3658 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3659
ae038cb0
DJ
3660 /* Check if comp unit has_children.
3661 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3662 If not, there's no more debug_info for this comp unit. */
d85a05f0 3663 if (has_children)
93311388 3664 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3665
98bfdba5
PA
3666 do_cleanups (free_abbrevs_cleanup);
3667
3668 if (read_cu)
3669 {
3670 /* We've successfully allocated this compilation unit. Let our
3671 caller clean it up when finished with it. */
3672 discard_cleanups (free_cu_cleanup);
3673 }
ae038cb0
DJ
3674}
3675
3676/* Create a list of all compilation units in OBJFILE. We do this only
3677 if an inter-comp-unit reference is found; presumably if there is one,
3678 there will be many, and one will occur early in the .debug_info section.
3679 So there's no point in building this list incrementally. */
3680
3681static void
3682create_all_comp_units (struct objfile *objfile)
3683{
3684 int n_allocated;
3685 int n_comp_units;
3686 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3687 gdb_byte *info_ptr;
3688
3689 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3690 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3691
3692 n_comp_units = 0;
3693 n_allocated = 10;
3694 all_comp_units = xmalloc (n_allocated
3695 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3696
3e43a32a
MS
3697 while (info_ptr < dwarf2_per_objfile->info.buffer
3698 + dwarf2_per_objfile->info.size)
ae038cb0 3699 {
c764a876 3700 unsigned int length, initial_length_size;
ae038cb0 3701 struct dwarf2_per_cu_data *this_cu;
c764a876 3702 unsigned int offset;
ae038cb0 3703
dce234bc 3704 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3705
3706 /* Read just enough information to find out where the next
3707 compilation unit is. */
c764a876
DE
3708 length = read_initial_length (objfile->obfd, info_ptr,
3709 &initial_length_size);
ae038cb0
DJ
3710
3711 /* Save the compilation unit for later lookup. */
3712 this_cu = obstack_alloc (&objfile->objfile_obstack,
3713 sizeof (struct dwarf2_per_cu_data));
3714 memset (this_cu, 0, sizeof (*this_cu));
3715 this_cu->offset = offset;
c764a876 3716 this_cu->length = length + initial_length_size;
9291a0cd 3717 this_cu->objfile = objfile;
ae038cb0
DJ
3718
3719 if (n_comp_units == n_allocated)
3720 {
3721 n_allocated *= 2;
3722 all_comp_units = xrealloc (all_comp_units,
3723 n_allocated
3724 * sizeof (struct dwarf2_per_cu_data *));
3725 }
3726 all_comp_units[n_comp_units++] = this_cu;
3727
3728 info_ptr = info_ptr + this_cu->length;
3729 }
3730
3731 dwarf2_per_objfile->all_comp_units
3732 = obstack_alloc (&objfile->objfile_obstack,
3733 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3734 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3735 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3736 xfree (all_comp_units);
3737 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3738}
3739
5734ee8b
DJ
3740/* Process all loaded DIEs for compilation unit CU, starting at
3741 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3742 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3743 DW_AT_ranges). If NEED_PC is set, then this function will set
3744 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3745 and record the covered ranges in the addrmap. */
c906108c 3746
72bf9492
DJ
3747static void
3748scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3749 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3750{
72bf9492 3751 struct partial_die_info *pdi;
c906108c 3752
91c24f0a
DC
3753 /* Now, march along the PDI's, descending into ones which have
3754 interesting children but skipping the children of the other ones,
3755 until we reach the end of the compilation unit. */
c906108c 3756
72bf9492 3757 pdi = first_die;
91c24f0a 3758
72bf9492
DJ
3759 while (pdi != NULL)
3760 {
3761 fixup_partial_die (pdi, cu);
c906108c 3762
f55ee35c 3763 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3764 children, so we need to look at them. Ditto for anonymous
3765 enums. */
933c6fe4 3766
72bf9492 3767 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3768 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3769 {
72bf9492 3770 switch (pdi->tag)
c906108c
SS
3771 {
3772 case DW_TAG_subprogram:
5734ee8b 3773 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3774 break;
72929c62 3775 case DW_TAG_constant:
c906108c
SS
3776 case DW_TAG_variable:
3777 case DW_TAG_typedef:
91c24f0a 3778 case DW_TAG_union_type:
72bf9492 3779 if (!pdi->is_declaration)
63d06c5c 3780 {
72bf9492 3781 add_partial_symbol (pdi, cu);
63d06c5c
DC
3782 }
3783 break;
c906108c 3784 case DW_TAG_class_type:
680b30c7 3785 case DW_TAG_interface_type:
c906108c 3786 case DW_TAG_structure_type:
72bf9492 3787 if (!pdi->is_declaration)
c906108c 3788 {
72bf9492 3789 add_partial_symbol (pdi, cu);
c906108c
SS
3790 }
3791 break;
91c24f0a 3792 case DW_TAG_enumeration_type:
72bf9492
DJ
3793 if (!pdi->is_declaration)
3794 add_partial_enumeration (pdi, cu);
c906108c
SS
3795 break;
3796 case DW_TAG_base_type:
a02abb62 3797 case DW_TAG_subrange_type:
c906108c 3798 /* File scope base type definitions are added to the partial
c5aa993b 3799 symbol table. */
72bf9492 3800 add_partial_symbol (pdi, cu);
c906108c 3801 break;
d9fa45fe 3802 case DW_TAG_namespace:
5734ee8b 3803 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3804 break;
5d7cb8df
JK
3805 case DW_TAG_module:
3806 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3807 break;
c906108c
SS
3808 default:
3809 break;
3810 }
3811 }
3812
72bf9492
DJ
3813 /* If the die has a sibling, skip to the sibling. */
3814
3815 pdi = pdi->die_sibling;
3816 }
3817}
3818
3819/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3820
72bf9492 3821 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3822 name is concatenated with "::" and the partial DIE's name. For
3823 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3824 Enumerators are an exception; they use the scope of their parent
3825 enumeration type, i.e. the name of the enumeration type is not
3826 prepended to the enumerator.
91c24f0a 3827
72bf9492
DJ
3828 There are two complexities. One is DW_AT_specification; in this
3829 case "parent" means the parent of the target of the specification,
3830 instead of the direct parent of the DIE. The other is compilers
3831 which do not emit DW_TAG_namespace; in this case we try to guess
3832 the fully qualified name of structure types from their members'
3833 linkage names. This must be done using the DIE's children rather
3834 than the children of any DW_AT_specification target. We only need
3835 to do this for structures at the top level, i.e. if the target of
3836 any DW_AT_specification (if any; otherwise the DIE itself) does not
3837 have a parent. */
3838
3839/* Compute the scope prefix associated with PDI's parent, in
3840 compilation unit CU. The result will be allocated on CU's
3841 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3842 field. NULL is returned if no prefix is necessary. */
3843static char *
3844partial_die_parent_scope (struct partial_die_info *pdi,
3845 struct dwarf2_cu *cu)
3846{
3847 char *grandparent_scope;
3848 struct partial_die_info *parent, *real_pdi;
91c24f0a 3849
72bf9492
DJ
3850 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3851 then this means the parent of the specification DIE. */
3852
3853 real_pdi = pdi;
72bf9492 3854 while (real_pdi->has_specification)
10b3939b 3855 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3856
3857 parent = real_pdi->die_parent;
3858 if (parent == NULL)
3859 return NULL;
3860
3861 if (parent->scope_set)
3862 return parent->scope;
3863
3864 fixup_partial_die (parent, cu);
3865
10b3939b 3866 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3867
acebe513
UW
3868 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3869 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3870 Work around this problem here. */
3871 if (cu->language == language_cplus
6e70227d 3872 && parent->tag == DW_TAG_namespace
acebe513
UW
3873 && strcmp (parent->name, "::") == 0
3874 && grandparent_scope == NULL)
3875 {
3876 parent->scope = NULL;
3877 parent->scope_set = 1;
3878 return NULL;
3879 }
3880
72bf9492 3881 if (parent->tag == DW_TAG_namespace
f55ee35c 3882 || parent->tag == DW_TAG_module
72bf9492
DJ
3883 || parent->tag == DW_TAG_structure_type
3884 || parent->tag == DW_TAG_class_type
680b30c7 3885 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3886 || parent->tag == DW_TAG_union_type
3887 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3888 {
3889 if (grandparent_scope == NULL)
3890 parent->scope = parent->name;
3891 else
3e43a32a
MS
3892 parent->scope = typename_concat (&cu->comp_unit_obstack,
3893 grandparent_scope,
f55ee35c 3894 parent->name, 0, cu);
72bf9492 3895 }
ceeb3d5a 3896 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3897 /* Enumerators should not get the name of the enumeration as a prefix. */
3898 parent->scope = grandparent_scope;
3899 else
3900 {
3901 /* FIXME drow/2004-04-01: What should we be doing with
3902 function-local names? For partial symbols, we should probably be
3903 ignoring them. */
3904 complaint (&symfile_complaints,
e2e0b3e5 3905 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3906 parent->tag, pdi->offset);
3907 parent->scope = grandparent_scope;
c906108c
SS
3908 }
3909
72bf9492
DJ
3910 parent->scope_set = 1;
3911 return parent->scope;
3912}
3913
3914/* Return the fully scoped name associated with PDI, from compilation unit
3915 CU. The result will be allocated with malloc. */
3916static char *
3917partial_die_full_name (struct partial_die_info *pdi,
3918 struct dwarf2_cu *cu)
3919{
3920 char *parent_scope;
3921
98bfdba5
PA
3922 /* If this is a template instantiation, we can not work out the
3923 template arguments from partial DIEs. So, unfortunately, we have
3924 to go through the full DIEs. At least any work we do building
3925 types here will be reused if full symbols are loaded later. */
3926 if (pdi->has_template_arguments)
3927 {
3928 fixup_partial_die (pdi, cu);
3929
3930 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3931 {
3932 struct die_info *die;
3933 struct attribute attr;
3934 struct dwarf2_cu *ref_cu = cu;
3935
3936 attr.name = 0;
3937 attr.form = DW_FORM_ref_addr;
3938 attr.u.addr = pdi->offset;
3939 die = follow_die_ref (NULL, &attr, &ref_cu);
3940
3941 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3942 }
3943 }
3944
72bf9492
DJ
3945 parent_scope = partial_die_parent_scope (pdi, cu);
3946 if (parent_scope == NULL)
3947 return NULL;
3948 else
f55ee35c 3949 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3950}
3951
3952static void
72bf9492 3953add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3954{
e7c27a73 3955 struct objfile *objfile = cu->objfile;
c906108c 3956 CORE_ADDR addr = 0;
decbce07 3957 char *actual_name = NULL;
5c4e30ca 3958 const struct partial_symbol *psym = NULL;
e142c38c 3959 CORE_ADDR baseaddr;
72bf9492 3960 int built_actual_name = 0;
e142c38c
DJ
3961
3962 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3963
94af9270
KS
3964 actual_name = partial_die_full_name (pdi, cu);
3965 if (actual_name)
3966 built_actual_name = 1;
63d06c5c 3967
72bf9492
DJ
3968 if (actual_name == NULL)
3969 actual_name = pdi->name;
3970
c906108c
SS
3971 switch (pdi->tag)
3972 {
3973 case DW_TAG_subprogram:
2cfa0c8d 3974 if (pdi->is_external || cu->language == language_ada)
c906108c 3975 {
2cfa0c8d
JB
3976 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3977 of the global scope. But in Ada, we want to be able to access
3978 nested procedures globally. So all Ada subprograms are stored
3979 in the global scope. */
f47fb265 3980 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3981 mst_text, objfile); */
f47fb265
MS
3982 add_psymbol_to_list (actual_name, strlen (actual_name),
3983 built_actual_name,
3984 VAR_DOMAIN, LOC_BLOCK,
3985 &objfile->global_psymbols,
3986 0, pdi->lowpc + baseaddr,
3987 cu->language, objfile);
c906108c
SS
3988 }
3989 else
3990 {
f47fb265 3991 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3992 mst_file_text, objfile); */
f47fb265
MS
3993 add_psymbol_to_list (actual_name, strlen (actual_name),
3994 built_actual_name,
3995 VAR_DOMAIN, LOC_BLOCK,
3996 &objfile->static_psymbols,
3997 0, pdi->lowpc + baseaddr,
3998 cu->language, objfile);
c906108c
SS
3999 }
4000 break;
72929c62
JB
4001 case DW_TAG_constant:
4002 {
4003 struct psymbol_allocation_list *list;
4004
4005 if (pdi->is_external)
4006 list = &objfile->global_psymbols;
4007 else
4008 list = &objfile->static_psymbols;
f47fb265
MS
4009 add_psymbol_to_list (actual_name, strlen (actual_name),
4010 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4011 list, 0, 0, cu->language, objfile);
72929c62
JB
4012 }
4013 break;
c906108c 4014 case DW_TAG_variable:
caac4577
JG
4015 if (pdi->locdesc)
4016 addr = decode_locdesc (pdi->locdesc, cu);
4017
4018 if (pdi->locdesc
4019 && addr == 0
4020 && !dwarf2_per_objfile->has_section_at_zero)
4021 {
4022 /* A global or static variable may also have been stripped
4023 out by the linker if unused, in which case its address
4024 will be nullified; do not add such variables into partial
4025 symbol table then. */
4026 }
4027 else if (pdi->is_external)
c906108c
SS
4028 {
4029 /* Global Variable.
4030 Don't enter into the minimal symbol tables as there is
4031 a minimal symbol table entry from the ELF symbols already.
4032 Enter into partial symbol table if it has a location
4033 descriptor or a type.
4034 If the location descriptor is missing, new_symbol will create
4035 a LOC_UNRESOLVED symbol, the address of the variable will then
4036 be determined from the minimal symbol table whenever the variable
4037 is referenced.
4038 The address for the partial symbol table entry is not
4039 used by GDB, but it comes in handy for debugging partial symbol
4040 table building. */
4041
c906108c 4042 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4043 add_psymbol_to_list (actual_name, strlen (actual_name),
4044 built_actual_name,
4045 VAR_DOMAIN, LOC_STATIC,
4046 &objfile->global_psymbols,
4047 0, addr + baseaddr,
4048 cu->language, objfile);
c906108c
SS
4049 }
4050 else
4051 {
0963b4bd 4052 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4053 if (pdi->locdesc == NULL)
decbce07
MS
4054 {
4055 if (built_actual_name)
4056 xfree (actual_name);
4057 return;
4058 }
f47fb265 4059 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4060 mst_file_data, objfile); */
f47fb265
MS
4061 add_psymbol_to_list (actual_name, strlen (actual_name),
4062 built_actual_name,
4063 VAR_DOMAIN, LOC_STATIC,
4064 &objfile->static_psymbols,
4065 0, addr + baseaddr,
4066 cu->language, objfile);
c906108c
SS
4067 }
4068 break;
4069 case DW_TAG_typedef:
4070 case DW_TAG_base_type:
a02abb62 4071 case DW_TAG_subrange_type:
38d518c9 4072 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4073 built_actual_name,
176620f1 4074 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4075 &objfile->static_psymbols,
e142c38c 4076 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4077 break;
72bf9492
DJ
4078 case DW_TAG_namespace:
4079 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4080 built_actual_name,
72bf9492
DJ
4081 VAR_DOMAIN, LOC_TYPEDEF,
4082 &objfile->global_psymbols,
4083 0, (CORE_ADDR) 0, cu->language, objfile);
4084 break;
c906108c 4085 case DW_TAG_class_type:
680b30c7 4086 case DW_TAG_interface_type:
c906108c
SS
4087 case DW_TAG_structure_type:
4088 case DW_TAG_union_type:
4089 case DW_TAG_enumeration_type:
fa4028e9
JB
4090 /* Skip external references. The DWARF standard says in the section
4091 about "Structure, Union, and Class Type Entries": "An incomplete
4092 structure, union or class type is represented by a structure,
4093 union or class entry that does not have a byte size attribute
4094 and that has a DW_AT_declaration attribute." */
4095 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4096 {
4097 if (built_actual_name)
4098 xfree (actual_name);
4099 return;
4100 }
fa4028e9 4101
63d06c5c
DC
4102 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4103 static vs. global. */
38d518c9 4104 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4105 built_actual_name,
176620f1 4106 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4107 (cu->language == language_cplus
4108 || cu->language == language_java)
63d06c5c
DC
4109 ? &objfile->global_psymbols
4110 : &objfile->static_psymbols,
e142c38c 4111 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4112
c906108c
SS
4113 break;
4114 case DW_TAG_enumerator:
38d518c9 4115 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4116 built_actual_name,
176620f1 4117 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4118 (cu->language == language_cplus
4119 || cu->language == language_java)
f6fe98ef
DJ
4120 ? &objfile->global_psymbols
4121 : &objfile->static_psymbols,
e142c38c 4122 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4123 break;
4124 default:
4125 break;
4126 }
5c4e30ca 4127
72bf9492
DJ
4128 if (built_actual_name)
4129 xfree (actual_name);
c906108c
SS
4130}
4131
5c4e30ca
DC
4132/* Read a partial die corresponding to a namespace; also, add a symbol
4133 corresponding to that namespace to the symbol table. NAMESPACE is
4134 the name of the enclosing namespace. */
91c24f0a 4135
72bf9492
DJ
4136static void
4137add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4138 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4139 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4140{
72bf9492 4141 /* Add a symbol for the namespace. */
e7c27a73 4142
72bf9492 4143 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4144
4145 /* Now scan partial symbols in that namespace. */
4146
91c24f0a 4147 if (pdi->has_children)
5734ee8b 4148 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4149}
4150
5d7cb8df
JK
4151/* Read a partial die corresponding to a Fortran module. */
4152
4153static void
4154add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4155 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4156{
f55ee35c 4157 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4158
4159 if (pdi->has_children)
4160 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4161}
4162
bc30ff58
JB
4163/* Read a partial die corresponding to a subprogram and create a partial
4164 symbol for that subprogram. When the CU language allows it, this
4165 routine also defines a partial symbol for each nested subprogram
4166 that this subprogram contains.
6e70227d 4167
bc30ff58
JB
4168 DIE my also be a lexical block, in which case we simply search
4169 recursively for suprograms defined inside that lexical block.
4170 Again, this is only performed when the CU language allows this
4171 type of definitions. */
4172
4173static void
4174add_partial_subprogram (struct partial_die_info *pdi,
4175 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4176 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4177{
4178 if (pdi->tag == DW_TAG_subprogram)
4179 {
4180 if (pdi->has_pc_info)
4181 {
4182 if (pdi->lowpc < *lowpc)
4183 *lowpc = pdi->lowpc;
4184 if (pdi->highpc > *highpc)
4185 *highpc = pdi->highpc;
5734ee8b
DJ
4186 if (need_pc)
4187 {
4188 CORE_ADDR baseaddr;
4189 struct objfile *objfile = cu->objfile;
4190
4191 baseaddr = ANOFFSET (objfile->section_offsets,
4192 SECT_OFF_TEXT (objfile));
4193 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4194 pdi->lowpc + baseaddr,
4195 pdi->highpc - 1 + baseaddr,
9291a0cd 4196 cu->per_cu->v.psymtab);
5734ee8b 4197 }
bc30ff58 4198 if (!pdi->is_declaration)
e8d05480
JB
4199 /* Ignore subprogram DIEs that do not have a name, they are
4200 illegal. Do not emit a complaint at this point, we will
4201 do so when we convert this psymtab into a symtab. */
4202 if (pdi->name)
4203 add_partial_symbol (pdi, cu);
bc30ff58
JB
4204 }
4205 }
6e70227d 4206
bc30ff58
JB
4207 if (! pdi->has_children)
4208 return;
4209
4210 if (cu->language == language_ada)
4211 {
4212 pdi = pdi->die_child;
4213 while (pdi != NULL)
4214 {
4215 fixup_partial_die (pdi, cu);
4216 if (pdi->tag == DW_TAG_subprogram
4217 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4218 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4219 pdi = pdi->die_sibling;
4220 }
4221 }
4222}
4223
91c24f0a
DC
4224/* Read a partial die corresponding to an enumeration type. */
4225
72bf9492
DJ
4226static void
4227add_partial_enumeration (struct partial_die_info *enum_pdi,
4228 struct dwarf2_cu *cu)
91c24f0a 4229{
72bf9492 4230 struct partial_die_info *pdi;
91c24f0a
DC
4231
4232 if (enum_pdi->name != NULL)
72bf9492
DJ
4233 add_partial_symbol (enum_pdi, cu);
4234
4235 pdi = enum_pdi->die_child;
4236 while (pdi)
91c24f0a 4237 {
72bf9492 4238 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4239 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4240 else
72bf9492
DJ
4241 add_partial_symbol (pdi, cu);
4242 pdi = pdi->die_sibling;
91c24f0a 4243 }
91c24f0a
DC
4244}
4245
4bb7a0a7
DJ
4246/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4247 Return the corresponding abbrev, or NULL if the number is zero (indicating
4248 an empty DIE). In either case *BYTES_READ will be set to the length of
4249 the initial number. */
4250
4251static struct abbrev_info *
fe1b8b76 4252peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4253 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4254{
4255 bfd *abfd = cu->objfile->obfd;
4256 unsigned int abbrev_number;
4257 struct abbrev_info *abbrev;
4258
4259 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4260
4261 if (abbrev_number == 0)
4262 return NULL;
4263
4264 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4265 if (!abbrev)
4266 {
3e43a32a
MS
4267 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4268 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4269 }
4270
4271 return abbrev;
4272}
4273
93311388
DE
4274/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4275 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4276 DIE. Any children of the skipped DIEs will also be skipped. */
4277
fe1b8b76 4278static gdb_byte *
93311388 4279skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4280{
4281 struct abbrev_info *abbrev;
4282 unsigned int bytes_read;
4283
4284 while (1)
4285 {
4286 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4287 if (abbrev == NULL)
4288 return info_ptr + bytes_read;
4289 else
93311388 4290 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4291 }
4292}
4293
93311388
DE
4294/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4295 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4296 abbrev corresponding to that skipped uleb128 should be passed in
4297 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4298 children. */
4299
fe1b8b76 4300static gdb_byte *
93311388
DE
4301skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4302 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4303{
4304 unsigned int bytes_read;
4305 struct attribute attr;
4306 bfd *abfd = cu->objfile->obfd;
4307 unsigned int form, i;
4308
4309 for (i = 0; i < abbrev->num_attrs; i++)
4310 {
4311 /* The only abbrev we care about is DW_AT_sibling. */
4312 if (abbrev->attrs[i].name == DW_AT_sibling)
4313 {
4314 read_attribute (&attr, &abbrev->attrs[i],
4315 abfd, info_ptr, cu);
4316 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4317 complaint (&symfile_complaints,
4318 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4319 else
93311388 4320 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4321 }
4322
4323 /* If it isn't DW_AT_sibling, skip this attribute. */
4324 form = abbrev->attrs[i].form;
4325 skip_attribute:
4326 switch (form)
4327 {
4bb7a0a7 4328 case DW_FORM_ref_addr:
ae411497
TT
4329 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4330 and later it is offset sized. */
4331 if (cu->header.version == 2)
4332 info_ptr += cu->header.addr_size;
4333 else
4334 info_ptr += cu->header.offset_size;
4335 break;
4336 case DW_FORM_addr:
4bb7a0a7
DJ
4337 info_ptr += cu->header.addr_size;
4338 break;
4339 case DW_FORM_data1:
4340 case DW_FORM_ref1:
4341 case DW_FORM_flag:
4342 info_ptr += 1;
4343 break;
2dc7f7b3
TT
4344 case DW_FORM_flag_present:
4345 break;
4bb7a0a7
DJ
4346 case DW_FORM_data2:
4347 case DW_FORM_ref2:
4348 info_ptr += 2;
4349 break;
4350 case DW_FORM_data4:
4351 case DW_FORM_ref4:
4352 info_ptr += 4;
4353 break;
4354 case DW_FORM_data8:
4355 case DW_FORM_ref8:
55f1336d 4356 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4357 info_ptr += 8;
4358 break;
4359 case DW_FORM_string:
9b1c24c8 4360 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4361 info_ptr += bytes_read;
4362 break;
2dc7f7b3 4363 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4364 case DW_FORM_strp:
4365 info_ptr += cu->header.offset_size;
4366 break;
2dc7f7b3 4367 case DW_FORM_exprloc:
4bb7a0a7
DJ
4368 case DW_FORM_block:
4369 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4370 info_ptr += bytes_read;
4371 break;
4372 case DW_FORM_block1:
4373 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4374 break;
4375 case DW_FORM_block2:
4376 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4377 break;
4378 case DW_FORM_block4:
4379 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4380 break;
4381 case DW_FORM_sdata:
4382 case DW_FORM_udata:
4383 case DW_FORM_ref_udata:
4384 info_ptr = skip_leb128 (abfd, info_ptr);
4385 break;
4386 case DW_FORM_indirect:
4387 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4388 info_ptr += bytes_read;
4389 /* We need to continue parsing from here, so just go back to
4390 the top. */
4391 goto skip_attribute;
4392
4393 default:
3e43a32a
MS
4394 error (_("Dwarf Error: Cannot handle %s "
4395 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4396 dwarf_form_name (form),
4397 bfd_get_filename (abfd));
4398 }
4399 }
4400
4401 if (abbrev->has_children)
93311388 4402 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4403 else
4404 return info_ptr;
4405}
4406
93311388
DE
4407/* Locate ORIG_PDI's sibling.
4408 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4409 in BUFFER. */
91c24f0a 4410
fe1b8b76 4411static gdb_byte *
93311388
DE
4412locate_pdi_sibling (struct partial_die_info *orig_pdi,
4413 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4414 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4415{
4416 /* Do we know the sibling already? */
72bf9492 4417
91c24f0a
DC
4418 if (orig_pdi->sibling)
4419 return orig_pdi->sibling;
4420
4421 /* Are there any children to deal with? */
4422
4423 if (!orig_pdi->has_children)
4424 return info_ptr;
4425
4bb7a0a7 4426 /* Skip the children the long way. */
91c24f0a 4427
93311388 4428 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4429}
4430
c906108c
SS
4431/* Expand this partial symbol table into a full symbol table. */
4432
4433static void
fba45db2 4434dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4435{
c906108c
SS
4436 if (pst != NULL)
4437 {
4438 if (pst->readin)
4439 {
3e43a32a
MS
4440 warning (_("bug: psymtab for %s is already read in."),
4441 pst->filename);
c906108c
SS
4442 }
4443 else
4444 {
4445 if (info_verbose)
4446 {
3e43a32a
MS
4447 printf_filtered (_("Reading in symbols for %s..."),
4448 pst->filename);
c906108c
SS
4449 gdb_flush (gdb_stdout);
4450 }
4451
10b3939b
DJ
4452 /* Restore our global data. */
4453 dwarf2_per_objfile = objfile_data (pst->objfile,
4454 dwarf2_objfile_data_key);
4455
b2ab525c
KB
4456 /* If this psymtab is constructed from a debug-only objfile, the
4457 has_section_at_zero flag will not necessarily be correct. We
4458 can get the correct value for this flag by looking at the data
4459 associated with the (presumably stripped) associated objfile. */
4460 if (pst->objfile->separate_debug_objfile_backlink)
4461 {
4462 struct dwarf2_per_objfile *dpo_backlink
4463 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4464 dwarf2_objfile_data_key);
9a619af0 4465
b2ab525c
KB
4466 dwarf2_per_objfile->has_section_at_zero
4467 = dpo_backlink->has_section_at_zero;
4468 }
4469
98bfdba5
PA
4470 dwarf2_per_objfile->reading_partial_symbols = 0;
4471
c906108c
SS
4472 psymtab_to_symtab_1 (pst);
4473
4474 /* Finish up the debug error message. */
4475 if (info_verbose)
a3f17187 4476 printf_filtered (_("done.\n"));
c906108c
SS
4477 }
4478 }
4479}
4480
10b3939b
DJ
4481/* Add PER_CU to the queue. */
4482
4483static void
03dd20cc 4484queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4485{
4486 struct dwarf2_queue_item *item;
4487
4488 per_cu->queued = 1;
4489 item = xmalloc (sizeof (*item));
4490 item->per_cu = per_cu;
4491 item->next = NULL;
4492
4493 if (dwarf2_queue == NULL)
4494 dwarf2_queue = item;
4495 else
4496 dwarf2_queue_tail->next = item;
4497
4498 dwarf2_queue_tail = item;
4499}
4500
4501/* Process the queue. */
4502
4503static void
4504process_queue (struct objfile *objfile)
4505{
4506 struct dwarf2_queue_item *item, *next_item;
4507
03dd20cc
DJ
4508 /* The queue starts out with one item, but following a DIE reference
4509 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4510 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4511 {
9291a0cd
TT
4512 if (dwarf2_per_objfile->using_index
4513 ? !item->per_cu->v.quick->symtab
4514 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4515 process_full_comp_unit (item->per_cu);
4516
4517 item->per_cu->queued = 0;
4518 next_item = item->next;
4519 xfree (item);
4520 }
4521
4522 dwarf2_queue_tail = NULL;
4523}
4524
4525/* Free all allocated queue entries. This function only releases anything if
4526 an error was thrown; if the queue was processed then it would have been
4527 freed as we went along. */
4528
4529static void
4530dwarf2_release_queue (void *dummy)
4531{
4532 struct dwarf2_queue_item *item, *last;
4533
4534 item = dwarf2_queue;
4535 while (item)
4536 {
4537 /* Anything still marked queued is likely to be in an
4538 inconsistent state, so discard it. */
4539 if (item->per_cu->queued)
4540 {
4541 if (item->per_cu->cu != NULL)
4542 free_one_cached_comp_unit (item->per_cu->cu);
4543 item->per_cu->queued = 0;
4544 }
4545
4546 last = item;
4547 item = item->next;
4548 xfree (last);
4549 }
4550
4551 dwarf2_queue = dwarf2_queue_tail = NULL;
4552}
4553
4554/* Read in full symbols for PST, and anything it depends on. */
4555
c906108c 4556static void
fba45db2 4557psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4558{
10b3939b 4559 struct dwarf2_per_cu_data *per_cu;
c906108c 4560 struct cleanup *back_to;
aaa75496
JB
4561 int i;
4562
4563 for (i = 0; i < pst->number_of_dependencies; i++)
4564 if (!pst->dependencies[i]->readin)
4565 {
4566 /* Inform about additional files that need to be read in. */
4567 if (info_verbose)
4568 {
a3f17187 4569 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4570 fputs_filtered (" ", gdb_stdout);
4571 wrap_here ("");
4572 fputs_filtered ("and ", gdb_stdout);
4573 wrap_here ("");
4574 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4575 wrap_here (""); /* Flush output. */
aaa75496
JB
4576 gdb_flush (gdb_stdout);
4577 }
4578 psymtab_to_symtab_1 (pst->dependencies[i]);
4579 }
4580
e38df1d0 4581 per_cu = pst->read_symtab_private;
10b3939b
DJ
4582
4583 if (per_cu == NULL)
aaa75496
JB
4584 {
4585 /* It's an include file, no symbols to read for it.
4586 Everything is in the parent symtab. */
4587 pst->readin = 1;
4588 return;
4589 }
c906108c 4590
9291a0cd 4591 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4592}
4593
93311388 4594/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4595
93311388 4596static void
3e43a32a
MS
4597load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4598 struct objfile *objfile)
10b3939b 4599{
31ffec48 4600 bfd *abfd = objfile->obfd;
10b3939b 4601 struct dwarf2_cu *cu;
c764a876 4602 unsigned int offset;
93311388 4603 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4604 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4605 struct attribute *attr;
98bfdba5 4606 int read_cu = 0;
6502dd73 4607
8b70b953 4608 gdb_assert (! per_cu->debug_type_section);
348e048f 4609
c906108c 4610 /* Set local variables from the partial symbol table info. */
10b3939b 4611 offset = per_cu->offset;
6502dd73 4612
be391dca 4613 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4614 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4615 beg_of_comp_unit = info_ptr;
63d06c5c 4616
98bfdba5
PA
4617 if (per_cu->cu == NULL)
4618 {
9816fde3
JK
4619 cu = xmalloc (sizeof (*cu));
4620 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4621
4622 read_cu = 1;
c906108c 4623
98bfdba5
PA
4624 /* If an error occurs while loading, release our storage. */
4625 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4626
98bfdba5
PA
4627 /* Read in the comp_unit header. */
4628 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4629
98bfdba5
PA
4630 /* Complete the cu_header. */
4631 cu->header.offset = offset;
4632 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4633
98bfdba5
PA
4634 /* Read the abbrevs for this compilation unit. */
4635 dwarf2_read_abbrevs (abfd, cu);
4636 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4637
98bfdba5
PA
4638 /* Link this compilation unit into the compilation unit tree. */
4639 per_cu->cu = cu;
4640 cu->per_cu = per_cu;
98bfdba5
PA
4641
4642 /* Link this CU into read_in_chain. */
4643 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4644 dwarf2_per_objfile->read_in_chain = per_cu;
4645 }
4646 else
4647 {
4648 cu = per_cu->cu;
4649 info_ptr += cu->header.first_die_offset;
4650 }
e142c38c 4651
93311388 4652 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4653
4654 /* We try not to read any attributes in this function, because not
4655 all objfiles needed for references have been loaded yet, and symbol
4656 table processing isn't initialized. But we have to set the CU language,
4657 or we won't be able to build types correctly. */
9816fde3 4658 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4659
a6c727b2
DJ
4660 /* Similarly, if we do not read the producer, we can not apply
4661 producer-specific interpretation. */
4662 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4663 if (attr)
4664 cu->producer = DW_STRING (attr);
4665
98bfdba5
PA
4666 if (read_cu)
4667 {
4668 do_cleanups (free_abbrevs_cleanup);
e142c38c 4669
98bfdba5
PA
4670 /* We've successfully allocated this compilation unit. Let our
4671 caller clean it up when finished with it. */
4672 discard_cleanups (free_cu_cleanup);
4673 }
10b3939b
DJ
4674}
4675
3da10d80
KS
4676/* Add a DIE to the delayed physname list. */
4677
4678static void
4679add_to_method_list (struct type *type, int fnfield_index, int index,
4680 const char *name, struct die_info *die,
4681 struct dwarf2_cu *cu)
4682{
4683 struct delayed_method_info mi;
4684 mi.type = type;
4685 mi.fnfield_index = fnfield_index;
4686 mi.index = index;
4687 mi.name = name;
4688 mi.die = die;
4689 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4690}
4691
4692/* A cleanup for freeing the delayed method list. */
4693
4694static void
4695free_delayed_list (void *ptr)
4696{
4697 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4698 if (cu->method_list != NULL)
4699 {
4700 VEC_free (delayed_method_info, cu->method_list);
4701 cu->method_list = NULL;
4702 }
4703}
4704
4705/* Compute the physnames of any methods on the CU's method list.
4706
4707 The computation of method physnames is delayed in order to avoid the
4708 (bad) condition that one of the method's formal parameters is of an as yet
4709 incomplete type. */
4710
4711static void
4712compute_delayed_physnames (struct dwarf2_cu *cu)
4713{
4714 int i;
4715 struct delayed_method_info *mi;
4716 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4717 {
1d06ead6 4718 const char *physname;
3da10d80
KS
4719 struct fn_fieldlist *fn_flp
4720 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4721 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4722 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4723 }
4724}
4725
4632c0d0
JK
4726/* Check for GCC >= 4.x. Return minor version (x) of 4.x in such case. If it
4727 is not GCC or it is GCC older than 4.x return -1. If it is GCC 5.x or
4728 higher return INT_MAX. */
8be455d7
JK
4729
4730static int
4632c0d0 4731producer_is_gcc_ge_4 (struct dwarf2_cu *cu)
8be455d7
JK
4732{
4733 const char *cs;
4734 int major, minor;
4735
4736 if (cu->producer == NULL)
4737 {
4738 /* For unknown compilers expect their behavior is not compliant. For GCC
4739 this case can also happen for -gdwarf-4 type units supported since
4740 gcc-4.5. */
4741
4632c0d0 4742 return -1;
8be455d7
JK
4743 }
4744
4745 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
4746
4747 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
4748 {
4749 /* For non-GCC compilers expect their behavior is not compliant. */
4750
4632c0d0 4751 return -1;
8be455d7
JK
4752 }
4753 cs = &cu->producer[strlen ("GNU ")];
4754 while (*cs && !isdigit (*cs))
4755 cs++;
4756 if (sscanf (cs, "%d.%d", &major, &minor) != 2)
4757 {
4758 /* Not recognized as GCC. */
4759
4632c0d0 4760 return -1;
8be455d7
JK
4761 }
4762
4632c0d0
JK
4763 if (major < 4)
4764 return -1;
4765 if (major > 4)
4766 return INT_MAX;
4767 return minor;
8be455d7
JK
4768}
4769
10b3939b
DJ
4770/* Generate full symbol information for PST and CU, whose DIEs have
4771 already been loaded into memory. */
4772
4773static void
4774process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4775{
10b3939b 4776 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4777 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4778 CORE_ADDR lowpc, highpc;
4779 struct symtab *symtab;
3da10d80 4780 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4781 CORE_ADDR baseaddr;
4782
4783 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4784
10b3939b
DJ
4785 buildsym_init ();
4786 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4787 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4788
4789 cu->list_in_scope = &file_symbols;
c906108c
SS
4790
4791 /* Do line number decoding in read_file_scope () */
10b3939b 4792 process_die (cu->dies, cu);
c906108c 4793
3da10d80
KS
4794 /* Now that we have processed all the DIEs in the CU, all the types
4795 should be complete, and it should now be safe to compute all of the
4796 physnames. */
4797 compute_delayed_physnames (cu);
4798 do_cleanups (delayed_list_cleanup);
4799
fae299cd
DC
4800 /* Some compilers don't define a DW_AT_high_pc attribute for the
4801 compilation unit. If the DW_AT_high_pc is missing, synthesize
4802 it, by scanning the DIE's below the compilation unit. */
10b3939b 4803 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4804
613e1657 4805 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4806
8be455d7 4807 if (symtab != NULL)
c906108c 4808 {
4632c0d0
JK
4809 int gcc_4_minor = producer_is_gcc_ge_4 (cu);
4810
8be455d7
JK
4811 /* Set symtab language to language from DW_AT_language. If the
4812 compilation is from a C file generated by language preprocessors, do
4813 not set the language if it was already deduced by start_subfile. */
4814 if (!(cu->language == language_c && symtab->language != language_c))
4815 symtab->language = cu->language;
4816
4817 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4818 produce DW_AT_location with location lists but it can be possibly
4819 invalid without -fvar-tracking.
4820
4821 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4822 needed, it would be wrong due to missing DW_AT_producer there.
4823
4824 Still one can confuse GDB by using non-standard GCC compilation
4825 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4826 */
4632c0d0 4827 if (cu->has_loclist && gcc_4_minor >= 0)
8be455d7 4828 symtab->locations_valid = 1;
e0d00bc7
JK
4829
4830 if (gcc_4_minor >= 5)
4831 symtab->epilogue_unwind_valid = 1;
c906108c 4832 }
9291a0cd
TT
4833
4834 if (dwarf2_per_objfile->using_index)
4835 per_cu->v.quick->symtab = symtab;
4836 else
4837 {
4838 struct partial_symtab *pst = per_cu->v.psymtab;
4839 pst->symtab = symtab;
4840 pst->readin = 1;
4841 }
c906108c
SS
4842
4843 do_cleanups (back_to);
4844}
4845
4846/* Process a die and its children. */
4847
4848static void
e7c27a73 4849process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4850{
4851 switch (die->tag)
4852 {
4853 case DW_TAG_padding:
4854 break;
4855 case DW_TAG_compile_unit:
e7c27a73 4856 read_file_scope (die, cu);
c906108c 4857 break;
348e048f
DE
4858 case DW_TAG_type_unit:
4859 read_type_unit_scope (die, cu);
4860 break;
c906108c 4861 case DW_TAG_subprogram:
c906108c 4862 case DW_TAG_inlined_subroutine:
edb3359d 4863 read_func_scope (die, cu);
c906108c
SS
4864 break;
4865 case DW_TAG_lexical_block:
14898363
L
4866 case DW_TAG_try_block:
4867 case DW_TAG_catch_block:
e7c27a73 4868 read_lexical_block_scope (die, cu);
c906108c
SS
4869 break;
4870 case DW_TAG_class_type:
680b30c7 4871 case DW_TAG_interface_type:
c906108c
SS
4872 case DW_TAG_structure_type:
4873 case DW_TAG_union_type:
134d01f1 4874 process_structure_scope (die, cu);
c906108c
SS
4875 break;
4876 case DW_TAG_enumeration_type:
134d01f1 4877 process_enumeration_scope (die, cu);
c906108c 4878 break;
134d01f1 4879
f792889a
DJ
4880 /* These dies have a type, but processing them does not create
4881 a symbol or recurse to process the children. Therefore we can
4882 read them on-demand through read_type_die. */
c906108c 4883 case DW_TAG_subroutine_type:
72019c9c 4884 case DW_TAG_set_type:
c906108c 4885 case DW_TAG_array_type:
c906108c 4886 case DW_TAG_pointer_type:
c906108c 4887 case DW_TAG_ptr_to_member_type:
c906108c 4888 case DW_TAG_reference_type:
c906108c 4889 case DW_TAG_string_type:
c906108c 4890 break;
134d01f1 4891
c906108c 4892 case DW_TAG_base_type:
a02abb62 4893 case DW_TAG_subrange_type:
cb249c71 4894 case DW_TAG_typedef:
134d01f1
DJ
4895 /* Add a typedef symbol for the type definition, if it has a
4896 DW_AT_name. */
f792889a 4897 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4898 break;
c906108c 4899 case DW_TAG_common_block:
e7c27a73 4900 read_common_block (die, cu);
c906108c
SS
4901 break;
4902 case DW_TAG_common_inclusion:
4903 break;
d9fa45fe 4904 case DW_TAG_namespace:
63d06c5c 4905 processing_has_namespace_info = 1;
e7c27a73 4906 read_namespace (die, cu);
d9fa45fe 4907 break;
5d7cb8df 4908 case DW_TAG_module:
f55ee35c 4909 processing_has_namespace_info = 1;
5d7cb8df
JK
4910 read_module (die, cu);
4911 break;
d9fa45fe
DC
4912 case DW_TAG_imported_declaration:
4913 case DW_TAG_imported_module:
63d06c5c 4914 processing_has_namespace_info = 1;
27aa8d6a
SW
4915 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4916 || cu->language != language_fortran))
4917 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4918 dwarf_tag_name (die->tag));
4919 read_import_statement (die, cu);
d9fa45fe 4920 break;
c906108c 4921 default:
e7c27a73 4922 new_symbol (die, NULL, cu);
c906108c
SS
4923 break;
4924 }
4925}
4926
94af9270
KS
4927/* A helper function for dwarf2_compute_name which determines whether DIE
4928 needs to have the name of the scope prepended to the name listed in the
4929 die. */
4930
4931static int
4932die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4933{
1c809c68
TT
4934 struct attribute *attr;
4935
94af9270
KS
4936 switch (die->tag)
4937 {
4938 case DW_TAG_namespace:
4939 case DW_TAG_typedef:
4940 case DW_TAG_class_type:
4941 case DW_TAG_interface_type:
4942 case DW_TAG_structure_type:
4943 case DW_TAG_union_type:
4944 case DW_TAG_enumeration_type:
4945 case DW_TAG_enumerator:
4946 case DW_TAG_subprogram:
4947 case DW_TAG_member:
4948 return 1;
4949
4950 case DW_TAG_variable:
c2b0a229 4951 case DW_TAG_constant:
94af9270
KS
4952 /* We only need to prefix "globally" visible variables. These include
4953 any variable marked with DW_AT_external or any variable that
4954 lives in a namespace. [Variables in anonymous namespaces
4955 require prefixing, but they are not DW_AT_external.] */
4956
4957 if (dwarf2_attr (die, DW_AT_specification, cu))
4958 {
4959 struct dwarf2_cu *spec_cu = cu;
9a619af0 4960
94af9270
KS
4961 return die_needs_namespace (die_specification (die, &spec_cu),
4962 spec_cu);
4963 }
4964
1c809c68 4965 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4966 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4967 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4968 return 0;
4969 /* A variable in a lexical block of some kind does not need a
4970 namespace, even though in C++ such variables may be external
4971 and have a mangled name. */
4972 if (die->parent->tag == DW_TAG_lexical_block
4973 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4974 || die->parent->tag == DW_TAG_catch_block
4975 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4976 return 0;
4977 return 1;
94af9270
KS
4978
4979 default:
4980 return 0;
4981 }
4982}
4983
98bfdba5
PA
4984/* Retrieve the last character from a mem_file. */
4985
4986static void
4987do_ui_file_peek_last (void *object, const char *buffer, long length)
4988{
4989 char *last_char_p = (char *) object;
4990
4991 if (length > 0)
4992 *last_char_p = buffer[length - 1];
4993}
4994
94af9270
KS
4995/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4996 compute the physname for the object, which include a method's
4997 formal parameters (C++/Java) and return type (Java).
4998
af6b7be1
JB
4999 For Ada, return the DIE's linkage name rather than the fully qualified
5000 name. PHYSNAME is ignored..
5001
94af9270
KS
5002 The result is allocated on the objfile_obstack and canonicalized. */
5003
5004static const char *
5005dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5006 int physname)
5007{
5008 if (name == NULL)
5009 name = dwarf2_name (die, cu);
5010
f55ee35c
JK
5011 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5012 compute it by typename_concat inside GDB. */
5013 if (cu->language == language_ada
5014 || (cu->language == language_fortran && physname))
5015 {
5016 /* For Ada unit, we prefer the linkage name over the name, as
5017 the former contains the exported name, which the user expects
5018 to be able to reference. Ideally, we want the user to be able
5019 to reference this entity using either natural or linkage name,
5020 but we haven't started looking at this enhancement yet. */
5021 struct attribute *attr;
5022
5023 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5024 if (attr == NULL)
5025 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5026 if (attr && DW_STRING (attr))
5027 return DW_STRING (attr);
5028 }
5029
94af9270
KS
5030 /* These are the only languages we know how to qualify names in. */
5031 if (name != NULL
f55ee35c
JK
5032 && (cu->language == language_cplus || cu->language == language_java
5033 || cu->language == language_fortran))
94af9270
KS
5034 {
5035 if (die_needs_namespace (die, cu))
5036 {
5037 long length;
5038 char *prefix;
5039 struct ui_file *buf;
5040
5041 prefix = determine_prefix (die, cu);
5042 buf = mem_fileopen ();
5043 if (*prefix != '\0')
5044 {
f55ee35c
JK
5045 char *prefixed_name = typename_concat (NULL, prefix, name,
5046 physname, cu);
9a619af0 5047
94af9270
KS
5048 fputs_unfiltered (prefixed_name, buf);
5049 xfree (prefixed_name);
5050 }
5051 else
62d5b8da 5052 fputs_unfiltered (name, buf);
94af9270 5053
98bfdba5
PA
5054 /* Template parameters may be specified in the DIE's DW_AT_name, or
5055 as children with DW_TAG_template_type_param or
5056 DW_TAG_value_type_param. If the latter, add them to the name
5057 here. If the name already has template parameters, then
5058 skip this step; some versions of GCC emit both, and
5059 it is more efficient to use the pre-computed name.
5060
5061 Something to keep in mind about this process: it is very
5062 unlikely, or in some cases downright impossible, to produce
5063 something that will match the mangled name of a function.
5064 If the definition of the function has the same debug info,
5065 we should be able to match up with it anyway. But fallbacks
5066 using the minimal symbol, for instance to find a method
5067 implemented in a stripped copy of libstdc++, will not work.
5068 If we do not have debug info for the definition, we will have to
5069 match them up some other way.
5070
5071 When we do name matching there is a related problem with function
5072 templates; two instantiated function templates are allowed to
5073 differ only by their return types, which we do not add here. */
5074
5075 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5076 {
5077 struct attribute *attr;
5078 struct die_info *child;
5079 int first = 1;
5080
5081 die->building_fullname = 1;
5082
5083 for (child = die->child; child != NULL; child = child->sibling)
5084 {
5085 struct type *type;
5086 long value;
5087 gdb_byte *bytes;
5088 struct dwarf2_locexpr_baton *baton;
5089 struct value *v;
5090
5091 if (child->tag != DW_TAG_template_type_param
5092 && child->tag != DW_TAG_template_value_param)
5093 continue;
5094
5095 if (first)
5096 {
5097 fputs_unfiltered ("<", buf);
5098 first = 0;
5099 }
5100 else
5101 fputs_unfiltered (", ", buf);
5102
5103 attr = dwarf2_attr (child, DW_AT_type, cu);
5104 if (attr == NULL)
5105 {
5106 complaint (&symfile_complaints,
5107 _("template parameter missing DW_AT_type"));
5108 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5109 continue;
5110 }
5111 type = die_type (child, cu);
5112
5113 if (child->tag == DW_TAG_template_type_param)
5114 {
5115 c_print_type (type, "", buf, -1, 0);
5116 continue;
5117 }
5118
5119 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5120 if (attr == NULL)
5121 {
5122 complaint (&symfile_complaints,
3e43a32a
MS
5123 _("template parameter missing "
5124 "DW_AT_const_value"));
98bfdba5
PA
5125 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5126 continue;
5127 }
5128
5129 dwarf2_const_value_attr (attr, type, name,
5130 &cu->comp_unit_obstack, cu,
5131 &value, &bytes, &baton);
5132
5133 if (TYPE_NOSIGN (type))
5134 /* GDB prints characters as NUMBER 'CHAR'. If that's
5135 changed, this can use value_print instead. */
5136 c_printchar (value, type, buf);
5137 else
5138 {
5139 struct value_print_options opts;
5140
5141 if (baton != NULL)
5142 v = dwarf2_evaluate_loc_desc (type, NULL,
5143 baton->data,
5144 baton->size,
5145 baton->per_cu);
5146 else if (bytes != NULL)
5147 {
5148 v = allocate_value (type);
5149 memcpy (value_contents_writeable (v), bytes,
5150 TYPE_LENGTH (type));
5151 }
5152 else
5153 v = value_from_longest (type, value);
5154
3e43a32a
MS
5155 /* Specify decimal so that we do not depend on
5156 the radix. */
98bfdba5
PA
5157 get_formatted_print_options (&opts, 'd');
5158 opts.raw = 1;
5159 value_print (v, buf, &opts);
5160 release_value (v);
5161 value_free (v);
5162 }
5163 }
5164
5165 die->building_fullname = 0;
5166
5167 if (!first)
5168 {
5169 /* Close the argument list, with a space if necessary
5170 (nested templates). */
5171 char last_char = '\0';
5172 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5173 if (last_char == '>')
5174 fputs_unfiltered (" >", buf);
5175 else
5176 fputs_unfiltered (">", buf);
5177 }
5178 }
5179
94af9270
KS
5180 /* For Java and C++ methods, append formal parameter type
5181 information, if PHYSNAME. */
6e70227d 5182
94af9270
KS
5183 if (physname && die->tag == DW_TAG_subprogram
5184 && (cu->language == language_cplus
5185 || cu->language == language_java))
5186 {
5187 struct type *type = read_type_die (die, cu);
5188
3167638f 5189 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5190
5191 if (cu->language == language_java)
5192 {
5193 /* For java, we must append the return type to method
0963b4bd 5194 names. */
94af9270
KS
5195 if (die->tag == DW_TAG_subprogram)
5196 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5197 0, 0);
5198 }
5199 else if (cu->language == language_cplus)
5200 {
60430eff
DJ
5201 /* Assume that an artificial first parameter is
5202 "this", but do not crash if it is not. RealView
5203 marks unnamed (and thus unused) parameters as
5204 artificial; there is no way to differentiate
5205 the two cases. */
94af9270
KS
5206 if (TYPE_NFIELDS (type) > 0
5207 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5208 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5209 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5210 0))))
94af9270
KS
5211 fputs_unfiltered (" const", buf);
5212 }
5213 }
5214
5215 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5216 &length);
5217 ui_file_delete (buf);
5218
5219 if (cu->language == language_cplus)
5220 {
5221 char *cname
5222 = dwarf2_canonicalize_name (name, cu,
5223 &cu->objfile->objfile_obstack);
9a619af0 5224
94af9270
KS
5225 if (cname != NULL)
5226 name = cname;
5227 }
5228 }
5229 }
5230
5231 return name;
5232}
5233
0114d602
DJ
5234/* Return the fully qualified name of DIE, based on its DW_AT_name.
5235 If scope qualifiers are appropriate they will be added. The result
5236 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5237 not have a name. NAME may either be from a previous call to
5238 dwarf2_name or NULL.
5239
0963b4bd 5240 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5241
5242static const char *
94af9270 5243dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5244{
94af9270
KS
5245 return dwarf2_compute_name (name, die, cu, 0);
5246}
0114d602 5247
94af9270
KS
5248/* Construct a physname for the given DIE in CU. NAME may either be
5249 from a previous call to dwarf2_name or NULL. The result will be
5250 allocated on the objfile_objstack or NULL if the DIE does not have a
5251 name.
0114d602 5252
94af9270 5253 The output string will be canonicalized (if C++/Java). */
0114d602 5254
94af9270
KS
5255static const char *
5256dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5257{
900e11f9
JK
5258 struct attribute *attr;
5259 const char *retval, *mangled = NULL, *canon = NULL;
5260 struct cleanup *back_to;
5261 int need_copy = 1;
5262
5263 /* In this case dwarf2_compute_name is just a shortcut not building anything
5264 on its own. */
5265 if (!die_needs_namespace (die, cu))
5266 return dwarf2_compute_name (name, die, cu, 1);
5267
5268 back_to = make_cleanup (null_cleanup, NULL);
5269
5270 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5271 if (!attr)
5272 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5273
5274 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5275 has computed. */
5276 if (attr && DW_STRING (attr))
5277 {
5278 char *demangled;
5279
5280 mangled = DW_STRING (attr);
5281
5282 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5283 type. It is easier for GDB users to search for such functions as
5284 `name(params)' than `long name(params)'. In such case the minimal
5285 symbol names do not match the full symbol names but for template
5286 functions there is never a need to look up their definition from their
5287 declaration so the only disadvantage remains the minimal symbol
5288 variant `long name(params)' does not have the proper inferior type.
5289 */
5290
5291 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5292 | (cu->language == language_java
5293 ? DMGL_JAVA | DMGL_RET_POSTFIX
5294 : DMGL_RET_DROP)));
5295 if (demangled)
5296 {
5297 make_cleanup (xfree, demangled);
5298 canon = demangled;
5299 }
5300 else
5301 {
5302 canon = mangled;
5303 need_copy = 0;
5304 }
5305 }
5306
5307 if (canon == NULL || check_physname)
5308 {
5309 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5310
5311 if (canon != NULL && strcmp (physname, canon) != 0)
5312 {
5313 /* It may not mean a bug in GDB. The compiler could also
5314 compute DW_AT_linkage_name incorrectly. But in such case
5315 GDB would need to be bug-to-bug compatible. */
5316
5317 complaint (&symfile_complaints,
5318 _("Computed physname <%s> does not match demangled <%s> "
5319 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5320 physname, canon, mangled, die->offset, cu->objfile->name);
5321
5322 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5323 is available here - over computed PHYSNAME. It is safer
5324 against both buggy GDB and buggy compilers. */
5325
5326 retval = canon;
5327 }
5328 else
5329 {
5330 retval = physname;
5331 need_copy = 0;
5332 }
5333 }
5334 else
5335 retval = canon;
5336
5337 if (need_copy)
5338 retval = obsavestring (retval, strlen (retval),
5339 &cu->objfile->objfile_obstack);
5340
5341 do_cleanups (back_to);
5342 return retval;
0114d602
DJ
5343}
5344
27aa8d6a
SW
5345/* Read the import statement specified by the given die and record it. */
5346
5347static void
5348read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5349{
5350 struct attribute *import_attr;
32019081 5351 struct die_info *imported_die, *child_die;
de4affc9 5352 struct dwarf2_cu *imported_cu;
27aa8d6a 5353 const char *imported_name;
794684b6 5354 const char *imported_name_prefix;
13387711
SW
5355 const char *canonical_name;
5356 const char *import_alias;
5357 const char *imported_declaration = NULL;
794684b6 5358 const char *import_prefix;
32019081
JK
5359 VEC (const_char_ptr) *excludes = NULL;
5360 struct cleanup *cleanups;
13387711
SW
5361
5362 char *temp;
27aa8d6a
SW
5363
5364 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5365 if (import_attr == NULL)
5366 {
5367 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5368 dwarf_tag_name (die->tag));
5369 return;
5370 }
5371
de4affc9
CC
5372 imported_cu = cu;
5373 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5374 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5375 if (imported_name == NULL)
5376 {
5377 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5378
5379 The import in the following code:
5380 namespace A
5381 {
5382 typedef int B;
5383 }
5384
5385 int main ()
5386 {
5387 using A::B;
5388 B b;
5389 return b;
5390 }
5391
5392 ...
5393 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5394 <52> DW_AT_decl_file : 1
5395 <53> DW_AT_decl_line : 6
5396 <54> DW_AT_import : <0x75>
5397 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5398 <59> DW_AT_name : B
5399 <5b> DW_AT_decl_file : 1
5400 <5c> DW_AT_decl_line : 2
5401 <5d> DW_AT_type : <0x6e>
5402 ...
5403 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5404 <76> DW_AT_byte_size : 4
5405 <77> DW_AT_encoding : 5 (signed)
5406
5407 imports the wrong die ( 0x75 instead of 0x58 ).
5408 This case will be ignored until the gcc bug is fixed. */
5409 return;
5410 }
5411
82856980
SW
5412 /* Figure out the local name after import. */
5413 import_alias = dwarf2_name (die, cu);
27aa8d6a 5414
794684b6
SW
5415 /* Figure out where the statement is being imported to. */
5416 import_prefix = determine_prefix (die, cu);
5417
5418 /* Figure out what the scope of the imported die is and prepend it
5419 to the name of the imported die. */
de4affc9 5420 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5421
f55ee35c
JK
5422 if (imported_die->tag != DW_TAG_namespace
5423 && imported_die->tag != DW_TAG_module)
794684b6 5424 {
13387711
SW
5425 imported_declaration = imported_name;
5426 canonical_name = imported_name_prefix;
794684b6 5427 }
13387711 5428 else if (strlen (imported_name_prefix) > 0)
794684b6 5429 {
13387711
SW
5430 temp = alloca (strlen (imported_name_prefix)
5431 + 2 + strlen (imported_name) + 1);
5432 strcpy (temp, imported_name_prefix);
5433 strcat (temp, "::");
5434 strcat (temp, imported_name);
5435 canonical_name = temp;
794684b6 5436 }
13387711
SW
5437 else
5438 canonical_name = imported_name;
794684b6 5439
32019081
JK
5440 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5441
5442 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5443 for (child_die = die->child; child_die && child_die->tag;
5444 child_die = sibling_die (child_die))
5445 {
5446 /* DWARF-4: A Fortran use statement with a “rename list” may be
5447 represented by an imported module entry with an import attribute
5448 referring to the module and owned entries corresponding to those
5449 entities that are renamed as part of being imported. */
5450
5451 if (child_die->tag != DW_TAG_imported_declaration)
5452 {
5453 complaint (&symfile_complaints,
5454 _("child DW_TAG_imported_declaration expected "
5455 "- DIE at 0x%x [in module %s]"),
5456 child_die->offset, cu->objfile->name);
5457 continue;
5458 }
5459
5460 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5461 if (import_attr == NULL)
5462 {
5463 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5464 dwarf_tag_name (child_die->tag));
5465 continue;
5466 }
5467
5468 imported_cu = cu;
5469 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5470 &imported_cu);
5471 imported_name = dwarf2_name (imported_die, imported_cu);
5472 if (imported_name == NULL)
5473 {
5474 complaint (&symfile_complaints,
5475 _("child DW_TAG_imported_declaration has unknown "
5476 "imported name - DIE at 0x%x [in module %s]"),
5477 child_die->offset, cu->objfile->name);
5478 continue;
5479 }
5480
5481 VEC_safe_push (const_char_ptr, excludes, imported_name);
5482
5483 process_die (child_die, cu);
5484 }
5485
c0cc3a76
SW
5486 cp_add_using_directive (import_prefix,
5487 canonical_name,
5488 import_alias,
13387711 5489 imported_declaration,
32019081 5490 excludes,
c0cc3a76 5491 &cu->objfile->objfile_obstack);
32019081
JK
5492
5493 do_cleanups (cleanups);
27aa8d6a
SW
5494}
5495
5fb290d7 5496static void
e142c38c 5497initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5498{
e142c38c 5499 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5500}
5501
ae2de4f8
DE
5502/* Cleanup function for read_file_scope. */
5503
cb1df416
DJ
5504static void
5505free_cu_line_header (void *arg)
5506{
5507 struct dwarf2_cu *cu = arg;
5508
5509 free_line_header (cu->line_header);
5510 cu->line_header = NULL;
5511}
5512
9291a0cd
TT
5513static void
5514find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5515 char **name, char **comp_dir)
5516{
5517 struct attribute *attr;
5518
5519 *name = NULL;
5520 *comp_dir = NULL;
5521
5522 /* Find the filename. Do not use dwarf2_name here, since the filename
5523 is not a source language identifier. */
5524 attr = dwarf2_attr (die, DW_AT_name, cu);
5525 if (attr)
5526 {
5527 *name = DW_STRING (attr);
5528 }
5529
5530 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5531 if (attr)
5532 *comp_dir = DW_STRING (attr);
5533 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5534 {
5535 *comp_dir = ldirname (*name);
5536 if (*comp_dir != NULL)
5537 make_cleanup (xfree, *comp_dir);
5538 }
5539 if (*comp_dir != NULL)
5540 {
5541 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5542 directory, get rid of it. */
5543 char *cp = strchr (*comp_dir, ':');
5544
5545 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5546 *comp_dir = cp + 1;
5547 }
5548
5549 if (*name == NULL)
5550 *name = "<unknown>";
5551}
5552
2ab95328
TT
5553/* Handle DW_AT_stmt_list for a compilation unit. */
5554
5555static void
5556handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5557 const char *comp_dir)
5558{
5559 struct attribute *attr;
5560 struct objfile *objfile = cu->objfile;
5561 bfd *abfd = objfile->obfd;
5562
5563 /* Decode line number information if present. We do this before
5564 processing child DIEs, so that the line header table is available
5565 for DW_AT_decl_file. */
5566 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5567 if (attr)
5568 {
5569 unsigned int line_offset = DW_UNSND (attr);
5570 struct line_header *line_header
5571 = dwarf_decode_line_header (line_offset, abfd, cu);
5572
5573 if (line_header)
5574 {
5575 cu->line_header = line_header;
5576 make_cleanup (free_cu_line_header, cu);
5577 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5578 }
5579 }
5580}
5581
ae2de4f8
DE
5582/* Process DW_TAG_compile_unit. */
5583
c906108c 5584static void
e7c27a73 5585read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5586{
e7c27a73 5587 struct objfile *objfile = cu->objfile;
debd256d 5588 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5589 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5590 CORE_ADDR highpc = ((CORE_ADDR) 0);
5591 struct attribute *attr;
e1024ff1 5592 char *name = NULL;
c906108c
SS
5593 char *comp_dir = NULL;
5594 struct die_info *child_die;
5595 bfd *abfd = objfile->obfd;
e142c38c 5596 CORE_ADDR baseaddr;
6e70227d 5597
e142c38c 5598 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5599
fae299cd 5600 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5601
5602 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5603 from finish_block. */
2acceee2 5604 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5605 lowpc = highpc;
5606 lowpc += baseaddr;
5607 highpc += baseaddr;
5608
9291a0cd 5609 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5610
e142c38c 5611 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5612 if (attr)
5613 {
e142c38c 5614 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5615 }
5616
b0f35d58 5617 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5618 if (attr)
b0f35d58 5619 cu->producer = DW_STRING (attr);
303b6f5d 5620
f4b8a18d
KW
5621 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5622 standardised yet. As a workaround for the language detection we fall
5623 back to the DW_AT_producer string. */
5624 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5625 cu->language = language_opencl;
5626
0963b4bd 5627 /* We assume that we're processing GCC output. */
c906108c 5628 processing_gcc_compilation = 2;
c906108c 5629
df8a16a1
DJ
5630 processing_has_namespace_info = 0;
5631
c906108c
SS
5632 start_symtab (name, comp_dir, lowpc);
5633 record_debugformat ("DWARF 2");
303b6f5d 5634 record_producer (cu->producer);
c906108c 5635
e142c38c 5636 initialize_cu_func_list (cu);
c906108c 5637
2ab95328 5638 handle_DW_AT_stmt_list (die, cu, comp_dir);
debd256d 5639
cb1df416
DJ
5640 /* Process all dies in compilation unit. */
5641 if (die->child != NULL)
5642 {
5643 child_die = die->child;
5644 while (child_die && child_die->tag)
5645 {
5646 process_die (child_die, cu);
5647 child_die = sibling_die (child_die);
5648 }
5649 }
5650
2e276125
JB
5651 /* Decode macro information, if present. Dwarf 2 macro information
5652 refers to information in the line number info statement program
5653 header, so we can only read it if we've read the header
5654 successfully. */
cf2c3c16 5655 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
2ab95328 5656 if (attr && cu->line_header)
2e276125 5657 {
cf2c3c16
TT
5658 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5659 complaint (&symfile_complaints,
5660 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5661
5662 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5663 comp_dir, abfd, cu,
5664 &dwarf2_per_objfile->macro, 1);
5665 }
5666 else
5667 {
5668 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5669 if (attr && cu->line_header)
5670 {
5671 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5672
cf2c3c16
TT
5673 dwarf_decode_macros (cu->line_header, macro_offset,
5674 comp_dir, abfd, cu,
5675 &dwarf2_per_objfile->macinfo, 0);
5676 }
2e276125 5677 }
debd256d 5678 do_cleanups (back_to);
5fb290d7
DJ
5679}
5680
ae2de4f8
DE
5681/* Process DW_TAG_type_unit.
5682 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5683 actual type being defined by this TU. In this case the first top
5684 level sibling is there to provide context only. */
5685
5686static void
5687read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5688{
5689 struct objfile *objfile = cu->objfile;
5690 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5691 CORE_ADDR lowpc;
5692 struct attribute *attr;
5693 char *name = NULL;
5694 char *comp_dir = NULL;
5695 struct die_info *child_die;
5696 bfd *abfd = objfile->obfd;
348e048f
DE
5697
5698 /* start_symtab needs a low pc, but we don't really have one.
5699 Do what read_file_scope would do in the absence of such info. */
5700 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5701
5702 /* Find the filename. Do not use dwarf2_name here, since the filename
5703 is not a source language identifier. */
5704 attr = dwarf2_attr (die, DW_AT_name, cu);
5705 if (attr)
5706 name = DW_STRING (attr);
5707
5708 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5709 if (attr)
5710 comp_dir = DW_STRING (attr);
5711 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5712 {
5713 comp_dir = ldirname (name);
5714 if (comp_dir != NULL)
5715 make_cleanup (xfree, comp_dir);
5716 }
5717
5718 if (name == NULL)
5719 name = "<unknown>";
5720
5721 attr = dwarf2_attr (die, DW_AT_language, cu);
5722 if (attr)
5723 set_cu_language (DW_UNSND (attr), cu);
5724
5725 /* This isn't technically needed today. It is done for symmetry
5726 with read_file_scope. */
5727 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5728 if (attr)
348e048f
DE
5729 cu->producer = DW_STRING (attr);
5730
0963b4bd 5731 /* We assume that we're processing GCC output. */
348e048f
DE
5732 processing_gcc_compilation = 2;
5733
5734 processing_has_namespace_info = 0;
5735
5736 start_symtab (name, comp_dir, lowpc);
5737 record_debugformat ("DWARF 2");
5738 record_producer (cu->producer);
5739
2ab95328
TT
5740 handle_DW_AT_stmt_list (die, cu, comp_dir);
5741
348e048f
DE
5742 /* Process the dies in the type unit. */
5743 if (die->child == NULL)
5744 {
5745 dump_die_for_error (die);
5746 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5747 bfd_get_filename (abfd));
5748 }
5749
5750 child_die = die->child;
5751
5752 while (child_die && child_die->tag)
5753 {
5754 process_die (child_die, cu);
5755
5756 child_die = sibling_die (child_die);
5757 }
5758
5759 do_cleanups (back_to);
5760}
5761
5fb290d7 5762static void
e142c38c
DJ
5763add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5764 struct dwarf2_cu *cu)
5fb290d7
DJ
5765{
5766 struct function_range *thisfn;
5767
5768 thisfn = (struct function_range *)
7b5a2f43 5769 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5770 thisfn->name = name;
5771 thisfn->lowpc = lowpc;
5772 thisfn->highpc = highpc;
5773 thisfn->seen_line = 0;
5774 thisfn->next = NULL;
5775
e142c38c
DJ
5776 if (cu->last_fn == NULL)
5777 cu->first_fn = thisfn;
5fb290d7 5778 else
e142c38c 5779 cu->last_fn->next = thisfn;
5fb290d7 5780
e142c38c 5781 cu->last_fn = thisfn;
c906108c
SS
5782}
5783
d389af10
JK
5784/* qsort helper for inherit_abstract_dies. */
5785
5786static int
5787unsigned_int_compar (const void *ap, const void *bp)
5788{
5789 unsigned int a = *(unsigned int *) ap;
5790 unsigned int b = *(unsigned int *) bp;
5791
5792 return (a > b) - (b > a);
5793}
5794
5795/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5796 Inherit only the children of the DW_AT_abstract_origin DIE not being
5797 already referenced by DW_AT_abstract_origin from the children of the
5798 current DIE. */
d389af10
JK
5799
5800static void
5801inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5802{
5803 struct die_info *child_die;
5804 unsigned die_children_count;
5805 /* CU offsets which were referenced by children of the current DIE. */
5806 unsigned *offsets;
5807 unsigned *offsets_end, *offsetp;
5808 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5809 struct die_info *origin_die;
5810 /* Iterator of the ORIGIN_DIE children. */
5811 struct die_info *origin_child_die;
5812 struct cleanup *cleanups;
5813 struct attribute *attr;
cd02d79d
PA
5814 struct dwarf2_cu *origin_cu;
5815 struct pending **origin_previous_list_in_scope;
d389af10
JK
5816
5817 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5818 if (!attr)
5819 return;
5820
cd02d79d
PA
5821 /* Note that following die references may follow to a die in a
5822 different cu. */
5823
5824 origin_cu = cu;
5825 origin_die = follow_die_ref (die, attr, &origin_cu);
5826
5827 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5828 symbols in. */
5829 origin_previous_list_in_scope = origin_cu->list_in_scope;
5830 origin_cu->list_in_scope = cu->list_in_scope;
5831
edb3359d
DJ
5832 if (die->tag != origin_die->tag
5833 && !(die->tag == DW_TAG_inlined_subroutine
5834 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5835 complaint (&symfile_complaints,
5836 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5837 die->offset, origin_die->offset);
5838
5839 child_die = die->child;
5840 die_children_count = 0;
5841 while (child_die && child_die->tag)
5842 {
5843 child_die = sibling_die (child_die);
5844 die_children_count++;
5845 }
5846 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5847 cleanups = make_cleanup (xfree, offsets);
5848
5849 offsets_end = offsets;
5850 child_die = die->child;
5851 while (child_die && child_die->tag)
5852 {
c38f313d
DJ
5853 /* For each CHILD_DIE, find the corresponding child of
5854 ORIGIN_DIE. If there is more than one layer of
5855 DW_AT_abstract_origin, follow them all; there shouldn't be,
5856 but GCC versions at least through 4.4 generate this (GCC PR
5857 40573). */
5858 struct die_info *child_origin_die = child_die;
cd02d79d 5859 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5860
c38f313d
DJ
5861 while (1)
5862 {
cd02d79d
PA
5863 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5864 child_origin_cu);
c38f313d
DJ
5865 if (attr == NULL)
5866 break;
cd02d79d
PA
5867 child_origin_die = follow_die_ref (child_origin_die, attr,
5868 &child_origin_cu);
c38f313d
DJ
5869 }
5870
d389af10
JK
5871 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5872 counterpart may exist. */
c38f313d 5873 if (child_origin_die != child_die)
d389af10 5874 {
edb3359d
DJ
5875 if (child_die->tag != child_origin_die->tag
5876 && !(child_die->tag == DW_TAG_inlined_subroutine
5877 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5878 complaint (&symfile_complaints,
5879 _("Child DIE 0x%x and its abstract origin 0x%x have "
5880 "different tags"), child_die->offset,
5881 child_origin_die->offset);
c38f313d
DJ
5882 if (child_origin_die->parent != origin_die)
5883 complaint (&symfile_complaints,
5884 _("Child DIE 0x%x and its abstract origin 0x%x have "
5885 "different parents"), child_die->offset,
5886 child_origin_die->offset);
5887 else
5888 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5889 }
5890 child_die = sibling_die (child_die);
5891 }
5892 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5893 unsigned_int_compar);
5894 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5895 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5896 complaint (&symfile_complaints,
5897 _("Multiple children of DIE 0x%x refer "
5898 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5899 die->offset, *offsetp);
5900
5901 offsetp = offsets;
5902 origin_child_die = origin_die->child;
5903 while (origin_child_die && origin_child_die->tag)
5904 {
5905 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5906 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5907 offsetp++;
5908 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5909 {
5910 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5911 process_die (origin_child_die, origin_cu);
d389af10
JK
5912 }
5913 origin_child_die = sibling_die (origin_child_die);
5914 }
cd02d79d 5915 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5916
5917 do_cleanups (cleanups);
5918}
5919
c906108c 5920static void
e7c27a73 5921read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5922{
e7c27a73 5923 struct objfile *objfile = cu->objfile;
52f0bd74 5924 struct context_stack *new;
c906108c
SS
5925 CORE_ADDR lowpc;
5926 CORE_ADDR highpc;
5927 struct die_info *child_die;
edb3359d 5928 struct attribute *attr, *call_line, *call_file;
c906108c 5929 char *name;
e142c38c 5930 CORE_ADDR baseaddr;
801e3a5b 5931 struct block *block;
edb3359d 5932 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5933 VEC (symbolp) *template_args = NULL;
5934 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5935
5936 if (inlined_func)
5937 {
5938 /* If we do not have call site information, we can't show the
5939 caller of this inlined function. That's too confusing, so
5940 only use the scope for local variables. */
5941 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5942 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5943 if (call_line == NULL || call_file == NULL)
5944 {
5945 read_lexical_block_scope (die, cu);
5946 return;
5947 }
5948 }
c906108c 5949
e142c38c
DJ
5950 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5951
94af9270 5952 name = dwarf2_name (die, cu);
c906108c 5953
e8d05480
JB
5954 /* Ignore functions with missing or empty names. These are actually
5955 illegal according to the DWARF standard. */
5956 if (name == NULL)
5957 {
5958 complaint (&symfile_complaints,
5959 _("missing name for subprogram DIE at %d"), die->offset);
5960 return;
5961 }
5962
5963 /* Ignore functions with missing or invalid low and high pc attributes. */
5964 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5965 {
ae4d0c03
PM
5966 attr = dwarf2_attr (die, DW_AT_external, cu);
5967 if (!attr || !DW_UNSND (attr))
5968 complaint (&symfile_complaints,
3e43a32a
MS
5969 _("cannot get low and high bounds "
5970 "for subprogram DIE at %d"),
ae4d0c03 5971 die->offset);
e8d05480
JB
5972 return;
5973 }
c906108c
SS
5974
5975 lowpc += baseaddr;
5976 highpc += baseaddr;
5977
5fb290d7 5978 /* Record the function range for dwarf_decode_lines. */
e142c38c 5979 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5980
34eaf542
TT
5981 /* If we have any template arguments, then we must allocate a
5982 different sort of symbol. */
5983 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5984 {
5985 if (child_die->tag == DW_TAG_template_type_param
5986 || child_die->tag == DW_TAG_template_value_param)
5987 {
5988 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5989 struct template_symbol);
5990 templ_func->base.is_cplus_template_function = 1;
5991 break;
5992 }
5993 }
5994
c906108c 5995 new = push_context (0, lowpc);
34eaf542
TT
5996 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5997 (struct symbol *) templ_func);
4c2df51b 5998
4cecd739
DJ
5999 /* If there is a location expression for DW_AT_frame_base, record
6000 it. */
e142c38c 6001 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 6002 if (attr)
c034e007
AC
6003 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6004 expression is being recorded directly in the function's symbol
6005 and not in a separate frame-base object. I guess this hack is
6006 to avoid adding some sort of frame-base adjunct/annex to the
6007 function's symbol :-(. The problem with doing this is that it
6008 results in a function symbol with a location expression that
6009 has nothing to do with the location of the function, ouch! The
6010 relationship should be: a function's symbol has-a frame base; a
6011 frame-base has-a location expression. */
e7c27a73 6012 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 6013
e142c38c 6014 cu->list_in_scope = &local_symbols;
c906108c 6015
639d11d3 6016 if (die->child != NULL)
c906108c 6017 {
639d11d3 6018 child_die = die->child;
c906108c
SS
6019 while (child_die && child_die->tag)
6020 {
34eaf542
TT
6021 if (child_die->tag == DW_TAG_template_type_param
6022 || child_die->tag == DW_TAG_template_value_param)
6023 {
6024 struct symbol *arg = new_symbol (child_die, NULL, cu);
6025
f1078f66
DJ
6026 if (arg != NULL)
6027 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6028 }
6029 else
6030 process_die (child_die, cu);
c906108c
SS
6031 child_die = sibling_die (child_die);
6032 }
6033 }
6034
d389af10
JK
6035 inherit_abstract_dies (die, cu);
6036
4a811a97
UW
6037 /* If we have a DW_AT_specification, we might need to import using
6038 directives from the context of the specification DIE. See the
6039 comment in determine_prefix. */
6040 if (cu->language == language_cplus
6041 && dwarf2_attr (die, DW_AT_specification, cu))
6042 {
6043 struct dwarf2_cu *spec_cu = cu;
6044 struct die_info *spec_die = die_specification (die, &spec_cu);
6045
6046 while (spec_die)
6047 {
6048 child_die = spec_die->child;
6049 while (child_die && child_die->tag)
6050 {
6051 if (child_die->tag == DW_TAG_imported_module)
6052 process_die (child_die, spec_cu);
6053 child_die = sibling_die (child_die);
6054 }
6055
6056 /* In some cases, GCC generates specification DIEs that
6057 themselves contain DW_AT_specification attributes. */
6058 spec_die = die_specification (spec_die, &spec_cu);
6059 }
6060 }
6061
c906108c
SS
6062 new = pop_context ();
6063 /* Make a block for the local symbols within. */
801e3a5b
JB
6064 block = finish_block (new->name, &local_symbols, new->old_blocks,
6065 lowpc, highpc, objfile);
6066
df8a16a1 6067 /* For C++, set the block's scope. */
f55ee35c 6068 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 6069 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 6070 determine_prefix (die, cu),
df8a16a1
DJ
6071 processing_has_namespace_info);
6072
801e3a5b
JB
6073 /* If we have address ranges, record them. */
6074 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 6075
34eaf542
TT
6076 /* Attach template arguments to function. */
6077 if (! VEC_empty (symbolp, template_args))
6078 {
6079 gdb_assert (templ_func != NULL);
6080
6081 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6082 templ_func->template_arguments
6083 = obstack_alloc (&objfile->objfile_obstack,
6084 (templ_func->n_template_arguments
6085 * sizeof (struct symbol *)));
6086 memcpy (templ_func->template_arguments,
6087 VEC_address (symbolp, template_args),
6088 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6089 VEC_free (symbolp, template_args);
6090 }
6091
208d8187
JB
6092 /* In C++, we can have functions nested inside functions (e.g., when
6093 a function declares a class that has methods). This means that
6094 when we finish processing a function scope, we may need to go
6095 back to building a containing block's symbol lists. */
6096 local_symbols = new->locals;
6097 param_symbols = new->params;
27aa8d6a 6098 using_directives = new->using_directives;
208d8187 6099
921e78cf
JB
6100 /* If we've finished processing a top-level function, subsequent
6101 symbols go in the file symbol list. */
6102 if (outermost_context_p ())
e142c38c 6103 cu->list_in_scope = &file_symbols;
c906108c
SS
6104}
6105
6106/* Process all the DIES contained within a lexical block scope. Start
6107 a new scope, process the dies, and then close the scope. */
6108
6109static void
e7c27a73 6110read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6111{
e7c27a73 6112 struct objfile *objfile = cu->objfile;
52f0bd74 6113 struct context_stack *new;
c906108c
SS
6114 CORE_ADDR lowpc, highpc;
6115 struct die_info *child_die;
e142c38c
DJ
6116 CORE_ADDR baseaddr;
6117
6118 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6119
6120 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6121 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6122 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6123 be nasty. Might be easier to properly extend generic blocks to
af34e669 6124 describe ranges. */
d85a05f0 6125 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6126 return;
6127 lowpc += baseaddr;
6128 highpc += baseaddr;
6129
6130 push_context (0, lowpc);
639d11d3 6131 if (die->child != NULL)
c906108c 6132 {
639d11d3 6133 child_die = die->child;
c906108c
SS
6134 while (child_die && child_die->tag)
6135 {
e7c27a73 6136 process_die (child_die, cu);
c906108c
SS
6137 child_die = sibling_die (child_die);
6138 }
6139 }
6140 new = pop_context ();
6141
8540c487 6142 if (local_symbols != NULL || using_directives != NULL)
c906108c 6143 {
801e3a5b
JB
6144 struct block *block
6145 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6146 highpc, objfile);
6147
6148 /* Note that recording ranges after traversing children, as we
6149 do here, means that recording a parent's ranges entails
6150 walking across all its children's ranges as they appear in
6151 the address map, which is quadratic behavior.
6152
6153 It would be nicer to record the parent's ranges before
6154 traversing its children, simply overriding whatever you find
6155 there. But since we don't even decide whether to create a
6156 block until after we've traversed its children, that's hard
6157 to do. */
6158 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6159 }
6160 local_symbols = new->locals;
27aa8d6a 6161 using_directives = new->using_directives;
c906108c
SS
6162}
6163
43039443 6164/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6165 Return 1 if the attributes are present and valid, otherwise, return 0.
6166 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6167
6168static int
6169dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6170 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6171 struct partial_symtab *ranges_pst)
43039443
JK
6172{
6173 struct objfile *objfile = cu->objfile;
6174 struct comp_unit_head *cu_header = &cu->header;
6175 bfd *obfd = objfile->obfd;
6176 unsigned int addr_size = cu_header->addr_size;
6177 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6178 /* Base address selection entry. */
6179 CORE_ADDR base;
6180 int found_base;
6181 unsigned int dummy;
6182 gdb_byte *buffer;
6183 CORE_ADDR marker;
6184 int low_set;
6185 CORE_ADDR low = 0;
6186 CORE_ADDR high = 0;
ff013f42 6187 CORE_ADDR baseaddr;
43039443 6188
d00adf39
DE
6189 found_base = cu->base_known;
6190 base = cu->base_address;
43039443 6191
be391dca 6192 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6193 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6194 {
6195 complaint (&symfile_complaints,
6196 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6197 offset);
6198 return 0;
6199 }
dce234bc 6200 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6201
6202 /* Read in the largest possible address. */
6203 marker = read_address (obfd, buffer, cu, &dummy);
6204 if ((marker & mask) == mask)
6205 {
6206 /* If we found the largest possible address, then
6207 read the base address. */
6208 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6209 buffer += 2 * addr_size;
6210 offset += 2 * addr_size;
6211 found_base = 1;
6212 }
6213
6214 low_set = 0;
6215
e7030f15 6216 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6217
43039443
JK
6218 while (1)
6219 {
6220 CORE_ADDR range_beginning, range_end;
6221
6222 range_beginning = read_address (obfd, buffer, cu, &dummy);
6223 buffer += addr_size;
6224 range_end = read_address (obfd, buffer, cu, &dummy);
6225 buffer += addr_size;
6226 offset += 2 * addr_size;
6227
6228 /* An end of list marker is a pair of zero addresses. */
6229 if (range_beginning == 0 && range_end == 0)
6230 /* Found the end of list entry. */
6231 break;
6232
6233 /* Each base address selection entry is a pair of 2 values.
6234 The first is the largest possible address, the second is
6235 the base address. Check for a base address here. */
6236 if ((range_beginning & mask) == mask)
6237 {
6238 /* If we found the largest possible address, then
6239 read the base address. */
6240 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6241 found_base = 1;
6242 continue;
6243 }
6244
6245 if (!found_base)
6246 {
6247 /* We have no valid base address for the ranges
6248 data. */
6249 complaint (&symfile_complaints,
6250 _("Invalid .debug_ranges data (no base address)"));
6251 return 0;
6252 }
6253
9277c30c
UW
6254 if (range_beginning > range_end)
6255 {
6256 /* Inverted range entries are invalid. */
6257 complaint (&symfile_complaints,
6258 _("Invalid .debug_ranges data (inverted range)"));
6259 return 0;
6260 }
6261
6262 /* Empty range entries have no effect. */
6263 if (range_beginning == range_end)
6264 continue;
6265
43039443
JK
6266 range_beginning += base;
6267 range_end += base;
6268
9277c30c 6269 if (ranges_pst != NULL)
ff013f42 6270 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6271 range_beginning + baseaddr,
6272 range_end - 1 + baseaddr,
ff013f42
JK
6273 ranges_pst);
6274
43039443
JK
6275 /* FIXME: This is recording everything as a low-high
6276 segment of consecutive addresses. We should have a
6277 data structure for discontiguous block ranges
6278 instead. */
6279 if (! low_set)
6280 {
6281 low = range_beginning;
6282 high = range_end;
6283 low_set = 1;
6284 }
6285 else
6286 {
6287 if (range_beginning < low)
6288 low = range_beginning;
6289 if (range_end > high)
6290 high = range_end;
6291 }
6292 }
6293
6294 if (! low_set)
6295 /* If the first entry is an end-of-list marker, the range
6296 describes an empty scope, i.e. no instructions. */
6297 return 0;
6298
6299 if (low_return)
6300 *low_return = low;
6301 if (high_return)
6302 *high_return = high;
6303 return 1;
6304}
6305
af34e669
DJ
6306/* Get low and high pc attributes from a die. Return 1 if the attributes
6307 are present and valid, otherwise, return 0. Return -1 if the range is
6308 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6309static int
af34e669 6310dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6311 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6312 struct partial_symtab *pst)
c906108c
SS
6313{
6314 struct attribute *attr;
af34e669
DJ
6315 CORE_ADDR low = 0;
6316 CORE_ADDR high = 0;
6317 int ret = 0;
c906108c 6318
e142c38c 6319 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6320 if (attr)
af34e669
DJ
6321 {
6322 high = DW_ADDR (attr);
e142c38c 6323 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6324 if (attr)
6325 low = DW_ADDR (attr);
6326 else
6327 /* Found high w/o low attribute. */
6328 return 0;
6329
6330 /* Found consecutive range of addresses. */
6331 ret = 1;
6332 }
c906108c 6333 else
af34e669 6334 {
e142c38c 6335 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6336 if (attr != NULL)
6337 {
af34e669 6338 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6339 .debug_ranges section. */
d85a05f0 6340 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6341 return 0;
43039443 6342 /* Found discontinuous range of addresses. */
af34e669
DJ
6343 ret = -1;
6344 }
6345 }
c906108c 6346
9373cf26
JK
6347 /* read_partial_die has also the strict LOW < HIGH requirement. */
6348 if (high <= low)
c906108c
SS
6349 return 0;
6350
6351 /* When using the GNU linker, .gnu.linkonce. sections are used to
6352 eliminate duplicate copies of functions and vtables and such.
6353 The linker will arbitrarily choose one and discard the others.
6354 The AT_*_pc values for such functions refer to local labels in
6355 these sections. If the section from that file was discarded, the
6356 labels are not in the output, so the relocs get a value of 0.
6357 If this is a discarded function, mark the pc bounds as invalid,
6358 so that GDB will ignore it. */
72dca2f5 6359 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6360 return 0;
6361
6362 *lowpc = low;
6363 *highpc = high;
af34e669 6364 return ret;
c906108c
SS
6365}
6366
b084d499
JB
6367/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6368 its low and high PC addresses. Do nothing if these addresses could not
6369 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6370 and HIGHPC to the high address if greater than HIGHPC. */
6371
6372static void
6373dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6374 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6375 struct dwarf2_cu *cu)
6376{
6377 CORE_ADDR low, high;
6378 struct die_info *child = die->child;
6379
d85a05f0 6380 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6381 {
6382 *lowpc = min (*lowpc, low);
6383 *highpc = max (*highpc, high);
6384 }
6385
6386 /* If the language does not allow nested subprograms (either inside
6387 subprograms or lexical blocks), we're done. */
6388 if (cu->language != language_ada)
6389 return;
6e70227d 6390
b084d499
JB
6391 /* Check all the children of the given DIE. If it contains nested
6392 subprograms, then check their pc bounds. Likewise, we need to
6393 check lexical blocks as well, as they may also contain subprogram
6394 definitions. */
6395 while (child && child->tag)
6396 {
6397 if (child->tag == DW_TAG_subprogram
6398 || child->tag == DW_TAG_lexical_block)
6399 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6400 child = sibling_die (child);
6401 }
6402}
6403
fae299cd
DC
6404/* Get the low and high pc's represented by the scope DIE, and store
6405 them in *LOWPC and *HIGHPC. If the correct values can't be
6406 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6407
6408static void
6409get_scope_pc_bounds (struct die_info *die,
6410 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6411 struct dwarf2_cu *cu)
6412{
6413 CORE_ADDR best_low = (CORE_ADDR) -1;
6414 CORE_ADDR best_high = (CORE_ADDR) 0;
6415 CORE_ADDR current_low, current_high;
6416
d85a05f0 6417 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6418 {
6419 best_low = current_low;
6420 best_high = current_high;
6421 }
6422 else
6423 {
6424 struct die_info *child = die->child;
6425
6426 while (child && child->tag)
6427 {
6428 switch (child->tag) {
6429 case DW_TAG_subprogram:
b084d499 6430 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6431 break;
6432 case DW_TAG_namespace:
f55ee35c 6433 case DW_TAG_module:
fae299cd
DC
6434 /* FIXME: carlton/2004-01-16: Should we do this for
6435 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6436 that current GCC's always emit the DIEs corresponding
6437 to definitions of methods of classes as children of a
6438 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6439 the DIEs giving the declarations, which could be
6440 anywhere). But I don't see any reason why the
6441 standards says that they have to be there. */
6442 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6443
6444 if (current_low != ((CORE_ADDR) -1))
6445 {
6446 best_low = min (best_low, current_low);
6447 best_high = max (best_high, current_high);
6448 }
6449 break;
6450 default:
0963b4bd 6451 /* Ignore. */
fae299cd
DC
6452 break;
6453 }
6454
6455 child = sibling_die (child);
6456 }
6457 }
6458
6459 *lowpc = best_low;
6460 *highpc = best_high;
6461}
6462
801e3a5b
JB
6463/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6464 in DIE. */
6465static void
6466dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6467 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6468{
6469 struct attribute *attr;
6470
6471 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6472 if (attr)
6473 {
6474 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6475
801e3a5b
JB
6476 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6477 if (attr)
6478 {
6479 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6480
801e3a5b
JB
6481 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6482 }
6483 }
6484
6485 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6486 if (attr)
6487 {
6488 bfd *obfd = cu->objfile->obfd;
6489
6490 /* The value of the DW_AT_ranges attribute is the offset of the
6491 address range list in the .debug_ranges section. */
6492 unsigned long offset = DW_UNSND (attr);
dce234bc 6493 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6494
6495 /* For some target architectures, but not others, the
6496 read_address function sign-extends the addresses it returns.
6497 To recognize base address selection entries, we need a
6498 mask. */
6499 unsigned int addr_size = cu->header.addr_size;
6500 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6501
6502 /* The base address, to which the next pair is relative. Note
6503 that this 'base' is a DWARF concept: most entries in a range
6504 list are relative, to reduce the number of relocs against the
6505 debugging information. This is separate from this function's
6506 'baseaddr' argument, which GDB uses to relocate debugging
6507 information from a shared library based on the address at
6508 which the library was loaded. */
d00adf39
DE
6509 CORE_ADDR base = cu->base_address;
6510 int base_known = cu->base_known;
801e3a5b 6511
be391dca 6512 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6513 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6514 {
6515 complaint (&symfile_complaints,
6516 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6517 offset);
6518 return;
6519 }
6520
6521 for (;;)
6522 {
6523 unsigned int bytes_read;
6524 CORE_ADDR start, end;
6525
6526 start = read_address (obfd, buffer, cu, &bytes_read);
6527 buffer += bytes_read;
6528 end = read_address (obfd, buffer, cu, &bytes_read);
6529 buffer += bytes_read;
6530
6531 /* Did we find the end of the range list? */
6532 if (start == 0 && end == 0)
6533 break;
6534
6535 /* Did we find a base address selection entry? */
6536 else if ((start & base_select_mask) == base_select_mask)
6537 {
6538 base = end;
6539 base_known = 1;
6540 }
6541
6542 /* We found an ordinary address range. */
6543 else
6544 {
6545 if (!base_known)
6546 {
6547 complaint (&symfile_complaints,
3e43a32a
MS
6548 _("Invalid .debug_ranges data "
6549 "(no base address)"));
801e3a5b
JB
6550 return;
6551 }
6552
9277c30c
UW
6553 if (start > end)
6554 {
6555 /* Inverted range entries are invalid. */
6556 complaint (&symfile_complaints,
6557 _("Invalid .debug_ranges data "
6558 "(inverted range)"));
6559 return;
6560 }
6561
6562 /* Empty range entries have no effect. */
6563 if (start == end)
6564 continue;
6565
6e70227d
DE
6566 record_block_range (block,
6567 baseaddr + base + start,
801e3a5b
JB
6568 baseaddr + base + end - 1);
6569 }
6570 }
6571 }
6572}
6573
60d5a603
JK
6574/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6575 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6576 during 4.6.0 experimental. */
6577
6578static int
6579producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6580{
6581 const char *cs;
6582 int major, minor, release;
6583
6584 if (cu->producer == NULL)
6585 {
6586 /* For unknown compilers expect their behavior is DWARF version
6587 compliant.
6588
6589 GCC started to support .debug_types sections by -gdwarf-4 since
6590 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6591 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6592 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6593 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6594
6595 return 0;
6596 }
6597
6598 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6599
6600 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6601 {
6602 /* For non-GCC compilers expect their behavior is DWARF version
6603 compliant. */
6604
6605 return 0;
6606 }
6607 cs = &cu->producer[strlen ("GNU ")];
6608 while (*cs && !isdigit (*cs))
6609 cs++;
6610 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6611 {
6612 /* Not recognized as GCC. */
6613
6614 return 0;
6615 }
6616
6617 return major < 4 || (major == 4 && minor < 6);
6618}
6619
6620/* Return the default accessibility type if it is not overriden by
6621 DW_AT_accessibility. */
6622
6623static enum dwarf_access_attribute
6624dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6625{
6626 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6627 {
6628 /* The default DWARF 2 accessibility for members is public, the default
6629 accessibility for inheritance is private. */
6630
6631 if (die->tag != DW_TAG_inheritance)
6632 return DW_ACCESS_public;
6633 else
6634 return DW_ACCESS_private;
6635 }
6636 else
6637 {
6638 /* DWARF 3+ defines the default accessibility a different way. The same
6639 rules apply now for DW_TAG_inheritance as for the members and it only
6640 depends on the container kind. */
6641
6642 if (die->parent->tag == DW_TAG_class_type)
6643 return DW_ACCESS_private;
6644 else
6645 return DW_ACCESS_public;
6646 }
6647}
6648
74ac6d43
TT
6649/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6650 offset. If the attribute was not found return 0, otherwise return
6651 1. If it was found but could not properly be handled, set *OFFSET
6652 to 0. */
6653
6654static int
6655handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6656 LONGEST *offset)
6657{
6658 struct attribute *attr;
6659
6660 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6661 if (attr != NULL)
6662 {
6663 *offset = 0;
6664
6665 /* Note that we do not check for a section offset first here.
6666 This is because DW_AT_data_member_location is new in DWARF 4,
6667 so if we see it, we can assume that a constant form is really
6668 a constant and not a section offset. */
6669 if (attr_form_is_constant (attr))
6670 *offset = dwarf2_get_attr_constant_value (attr, 0);
6671 else if (attr_form_is_section_offset (attr))
6672 dwarf2_complex_location_expr_complaint ();
6673 else if (attr_form_is_block (attr))
6674 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6675 else
6676 dwarf2_complex_location_expr_complaint ();
6677
6678 return 1;
6679 }
6680
6681 return 0;
6682}
6683
c906108c
SS
6684/* Add an aggregate field to the field list. */
6685
6686static void
107d2387 6687dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6688 struct dwarf2_cu *cu)
6e70227d 6689{
e7c27a73 6690 struct objfile *objfile = cu->objfile;
5e2b427d 6691 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6692 struct nextfield *new_field;
6693 struct attribute *attr;
6694 struct field *fp;
6695 char *fieldname = "";
6696
6697 /* Allocate a new field list entry and link it in. */
6698 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6699 make_cleanup (xfree, new_field);
c906108c 6700 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6701
6702 if (die->tag == DW_TAG_inheritance)
6703 {
6704 new_field->next = fip->baseclasses;
6705 fip->baseclasses = new_field;
6706 }
6707 else
6708 {
6709 new_field->next = fip->fields;
6710 fip->fields = new_field;
6711 }
c906108c
SS
6712 fip->nfields++;
6713
e142c38c 6714 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6715 if (attr)
6716 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
6717 else
6718 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
6719 if (new_field->accessibility != DW_ACCESS_public)
6720 fip->non_public_fields = 1;
60d5a603 6721
e142c38c 6722 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6723 if (attr)
6724 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
6725 else
6726 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
6727
6728 fp = &new_field->field;
a9a9bd0f 6729
e142c38c 6730 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6731 {
74ac6d43
TT
6732 LONGEST offset;
6733
a9a9bd0f 6734 /* Data member other than a C++ static data member. */
6e70227d 6735
c906108c 6736 /* Get type of field. */
e7c27a73 6737 fp->type = die_type (die, cu);
c906108c 6738
d6a843b5 6739 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6740
c906108c 6741 /* Get bit size of field (zero if none). */
e142c38c 6742 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6743 if (attr)
6744 {
6745 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6746 }
6747 else
6748 {
6749 FIELD_BITSIZE (*fp) = 0;
6750 }
6751
6752 /* Get bit offset of field. */
74ac6d43
TT
6753 if (handle_data_member_location (die, cu, &offset))
6754 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 6755 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6756 if (attr)
6757 {
5e2b427d 6758 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6759 {
6760 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6761 additional bit offset from the MSB of the containing
6762 anonymous object to the MSB of the field. We don't
6763 have to do anything special since we don't need to
6764 know the size of the anonymous object. */
c906108c
SS
6765 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6766 }
6767 else
6768 {
6769 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6770 MSB of the anonymous object, subtract off the number of
6771 bits from the MSB of the field to the MSB of the
6772 object, and then subtract off the number of bits of
6773 the field itself. The result is the bit offset of
6774 the LSB of the field. */
c906108c
SS
6775 int anonymous_size;
6776 int bit_offset = DW_UNSND (attr);
6777
e142c38c 6778 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6779 if (attr)
6780 {
6781 /* The size of the anonymous object containing
6782 the bit field is explicit, so use the
6783 indicated size (in bytes). */
6784 anonymous_size = DW_UNSND (attr);
6785 }
6786 else
6787 {
6788 /* The size of the anonymous object containing
6789 the bit field must be inferred from the type
6790 attribute of the data member containing the
6791 bit field. */
6792 anonymous_size = TYPE_LENGTH (fp->type);
6793 }
6794 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6795 - bit_offset - FIELD_BITSIZE (*fp);
6796 }
6797 }
6798
6799 /* Get name of field. */
39cbfefa
DJ
6800 fieldname = dwarf2_name (die, cu);
6801 if (fieldname == NULL)
6802 fieldname = "";
d8151005
DJ
6803
6804 /* The name is already allocated along with this objfile, so we don't
6805 need to duplicate it for the type. */
6806 fp->name = fieldname;
c906108c
SS
6807
6808 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6809 pointer or virtual base class pointer) to private. */
e142c38c 6810 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6811 {
d48cc9dd 6812 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6813 new_field->accessibility = DW_ACCESS_private;
6814 fip->non_public_fields = 1;
6815 }
6816 }
a9a9bd0f 6817 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6818 {
a9a9bd0f
DC
6819 /* C++ static member. */
6820
6821 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6822 is a declaration, but all versions of G++ as of this writing
6823 (so through at least 3.2.1) incorrectly generate
6824 DW_TAG_variable tags. */
6e70227d 6825
ff355380 6826 const char *physname;
c906108c 6827
a9a9bd0f 6828 /* Get name of field. */
39cbfefa
DJ
6829 fieldname = dwarf2_name (die, cu);
6830 if (fieldname == NULL)
c906108c
SS
6831 return;
6832
254e6b9e 6833 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6834 if (attr
6835 /* Only create a symbol if this is an external value.
6836 new_symbol checks this and puts the value in the global symbol
6837 table, which we want. If it is not external, new_symbol
6838 will try to put the value in cu->list_in_scope which is wrong. */
6839 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6840 {
6841 /* A static const member, not much different than an enum as far as
6842 we're concerned, except that we can support more types. */
6843 new_symbol (die, NULL, cu);
6844 }
6845
2df3850c 6846 /* Get physical name. */
ff355380 6847 physname = dwarf2_physname (fieldname, die, cu);
c906108c 6848
d8151005
DJ
6849 /* The name is already allocated along with this objfile, so we don't
6850 need to duplicate it for the type. */
6851 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6852 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6853 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6854 }
6855 else if (die->tag == DW_TAG_inheritance)
6856 {
74ac6d43 6857 LONGEST offset;
d4b96c9a 6858
74ac6d43
TT
6859 /* C++ base class field. */
6860 if (handle_data_member_location (die, cu, &offset))
6861 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 6862 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6863 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6864 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6865 fip->nbaseclasses++;
6866 }
6867}
6868
98751a41
JK
6869/* Add a typedef defined in the scope of the FIP's class. */
6870
6871static void
6872dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6873 struct dwarf2_cu *cu)
6e70227d 6874{
98751a41 6875 struct objfile *objfile = cu->objfile;
98751a41
JK
6876 struct typedef_field_list *new_field;
6877 struct attribute *attr;
6878 struct typedef_field *fp;
6879 char *fieldname = "";
6880
6881 /* Allocate a new field list entry and link it in. */
6882 new_field = xzalloc (sizeof (*new_field));
6883 make_cleanup (xfree, new_field);
6884
6885 gdb_assert (die->tag == DW_TAG_typedef);
6886
6887 fp = &new_field->field;
6888
6889 /* Get name of field. */
6890 fp->name = dwarf2_name (die, cu);
6891 if (fp->name == NULL)
6892 return;
6893
6894 fp->type = read_type_die (die, cu);
6895
6896 new_field->next = fip->typedef_field_list;
6897 fip->typedef_field_list = new_field;
6898 fip->typedef_field_list_count++;
6899}
6900
c906108c
SS
6901/* Create the vector of fields, and attach it to the type. */
6902
6903static void
fba45db2 6904dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6905 struct dwarf2_cu *cu)
c906108c
SS
6906{
6907 int nfields = fip->nfields;
6908
6909 /* Record the field count, allocate space for the array of fields,
6910 and create blank accessibility bitfields if necessary. */
6911 TYPE_NFIELDS (type) = nfields;
6912 TYPE_FIELDS (type) = (struct field *)
6913 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6914 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6915
b4ba55a1 6916 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6917 {
6918 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6919
6920 TYPE_FIELD_PRIVATE_BITS (type) =
6921 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6922 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6923
6924 TYPE_FIELD_PROTECTED_BITS (type) =
6925 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6926 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6927
774b6a14
TT
6928 TYPE_FIELD_IGNORE_BITS (type) =
6929 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6930 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6931 }
6932
6933 /* If the type has baseclasses, allocate and clear a bit vector for
6934 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6935 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6936 {
6937 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6938 unsigned char *pointer;
c906108c
SS
6939
6940 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6941 pointer = TYPE_ALLOC (type, num_bytes);
6942 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6943 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6944 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6945 }
6946
3e43a32a
MS
6947 /* Copy the saved-up fields into the field vector. Start from the head of
6948 the list, adding to the tail of the field array, so that they end up in
6949 the same order in the array in which they were added to the list. */
c906108c
SS
6950 while (nfields-- > 0)
6951 {
7d0ccb61
DJ
6952 struct nextfield *fieldp;
6953
6954 if (fip->fields)
6955 {
6956 fieldp = fip->fields;
6957 fip->fields = fieldp->next;
6958 }
6959 else
6960 {
6961 fieldp = fip->baseclasses;
6962 fip->baseclasses = fieldp->next;
6963 }
6964
6965 TYPE_FIELD (type, nfields) = fieldp->field;
6966 switch (fieldp->accessibility)
c906108c 6967 {
c5aa993b 6968 case DW_ACCESS_private:
b4ba55a1
JB
6969 if (cu->language != language_ada)
6970 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6971 break;
c906108c 6972
c5aa993b 6973 case DW_ACCESS_protected:
b4ba55a1
JB
6974 if (cu->language != language_ada)
6975 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6976 break;
c906108c 6977
c5aa993b
JM
6978 case DW_ACCESS_public:
6979 break;
c906108c 6980
c5aa993b
JM
6981 default:
6982 /* Unknown accessibility. Complain and treat it as public. */
6983 {
e2e0b3e5 6984 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6985 fieldp->accessibility);
c5aa993b
JM
6986 }
6987 break;
c906108c
SS
6988 }
6989 if (nfields < fip->nbaseclasses)
6990 {
7d0ccb61 6991 switch (fieldp->virtuality)
c906108c 6992 {
c5aa993b
JM
6993 case DW_VIRTUALITY_virtual:
6994 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 6995 if (cu->language == language_ada)
a73c6dcd 6996 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
6997 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6998 break;
c906108c
SS
6999 }
7000 }
c906108c
SS
7001 }
7002}
7003
c906108c
SS
7004/* Add a member function to the proper fieldlist. */
7005
7006static void
107d2387 7007dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 7008 struct type *type, struct dwarf2_cu *cu)
c906108c 7009{
e7c27a73 7010 struct objfile *objfile = cu->objfile;
c906108c
SS
7011 struct attribute *attr;
7012 struct fnfieldlist *flp;
7013 int i;
7014 struct fn_field *fnp;
7015 char *fieldname;
c906108c 7016 struct nextfnfield *new_fnfield;
f792889a 7017 struct type *this_type;
60d5a603 7018 enum dwarf_access_attribute accessibility;
c906108c 7019
b4ba55a1 7020 if (cu->language == language_ada)
a73c6dcd 7021 error (_("unexpected member function in Ada type"));
b4ba55a1 7022
2df3850c 7023 /* Get name of member function. */
39cbfefa
DJ
7024 fieldname = dwarf2_name (die, cu);
7025 if (fieldname == NULL)
2df3850c 7026 return;
c906108c 7027
c906108c
SS
7028 /* Look up member function name in fieldlist. */
7029 for (i = 0; i < fip->nfnfields; i++)
7030 {
27bfe10e 7031 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
7032 break;
7033 }
7034
7035 /* Create new list element if necessary. */
7036 if (i < fip->nfnfields)
7037 flp = &fip->fnfieldlists[i];
7038 else
7039 {
7040 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7041 {
7042 fip->fnfieldlists = (struct fnfieldlist *)
7043 xrealloc (fip->fnfieldlists,
7044 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7045 * sizeof (struct fnfieldlist));
c906108c 7046 if (fip->nfnfields == 0)
c13c43fd 7047 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
7048 }
7049 flp = &fip->fnfieldlists[fip->nfnfields];
7050 flp->name = fieldname;
7051 flp->length = 0;
7052 flp->head = NULL;
3da10d80 7053 i = fip->nfnfields++;
c906108c
SS
7054 }
7055
7056 /* Create a new member function field and chain it to the field list
0963b4bd 7057 entry. */
c906108c 7058 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 7059 make_cleanup (xfree, new_fnfield);
c906108c
SS
7060 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7061 new_fnfield->next = flp->head;
7062 flp->head = new_fnfield;
7063 flp->length++;
7064
7065 /* Fill in the member function field info. */
7066 fnp = &new_fnfield->fnfield;
3da10d80
KS
7067
7068 /* Delay processing of the physname until later. */
7069 if (cu->language == language_cplus || cu->language == language_java)
7070 {
7071 add_to_method_list (type, i, flp->length - 1, fieldname,
7072 die, cu);
7073 }
7074 else
7075 {
1d06ead6 7076 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
7077 fnp->physname = physname ? physname : "";
7078 }
7079
c906108c 7080 fnp->type = alloc_type (objfile);
f792889a
DJ
7081 this_type = read_type_die (die, cu);
7082 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 7083 {
f792889a 7084 int nparams = TYPE_NFIELDS (this_type);
c906108c 7085
f792889a 7086 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7087 of the method itself (TYPE_CODE_METHOD). */
7088 smash_to_method_type (fnp->type, type,
f792889a
DJ
7089 TYPE_TARGET_TYPE (this_type),
7090 TYPE_FIELDS (this_type),
7091 TYPE_NFIELDS (this_type),
7092 TYPE_VARARGS (this_type));
c906108c
SS
7093
7094 /* Handle static member functions.
c5aa993b 7095 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7096 member functions. G++ helps GDB by marking the first
7097 parameter for non-static member functions (which is the this
7098 pointer) as artificial. We obtain this information from
7099 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7100 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7101 fnp->voffset = VOFFSET_STATIC;
7102 }
7103 else
e2e0b3e5 7104 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7105 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7106
7107 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7108 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7109 fnp->fcontext = die_containing_type (die, cu);
c906108c 7110
3e43a32a
MS
7111 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7112 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7113
7114 /* Get accessibility. */
e142c38c 7115 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7116 if (attr)
60d5a603
JK
7117 accessibility = DW_UNSND (attr);
7118 else
7119 accessibility = dwarf2_default_access_attribute (die, cu);
7120 switch (accessibility)
c906108c 7121 {
60d5a603
JK
7122 case DW_ACCESS_private:
7123 fnp->is_private = 1;
7124 break;
7125 case DW_ACCESS_protected:
7126 fnp->is_protected = 1;
7127 break;
c906108c
SS
7128 }
7129
b02dede2 7130 /* Check for artificial methods. */
e142c38c 7131 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7132 if (attr && DW_UNSND (attr) != 0)
7133 fnp->is_artificial = 1;
7134
0d564a31 7135 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7136 function. For older versions of GCC, this is an offset in the
7137 appropriate virtual table, as specified by DW_AT_containing_type.
7138 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7139 to the object address. */
7140
e142c38c 7141 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7142 if (attr)
8e19ed76 7143 {
aec5aa8b 7144 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7145 {
aec5aa8b
TT
7146 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7147 {
7148 /* Old-style GCC. */
7149 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7150 }
7151 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7152 || (DW_BLOCK (attr)->size > 1
7153 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7154 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7155 {
7156 struct dwarf_block blk;
7157 int offset;
7158
7159 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7160 ? 1 : 2);
7161 blk.size = DW_BLOCK (attr)->size - offset;
7162 blk.data = DW_BLOCK (attr)->data + offset;
7163 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7164 if ((fnp->voffset % cu->header.addr_size) != 0)
7165 dwarf2_complex_location_expr_complaint ();
7166 else
7167 fnp->voffset /= cu->header.addr_size;
7168 fnp->voffset += 2;
7169 }
7170 else
7171 dwarf2_complex_location_expr_complaint ();
7172
7173 if (!fnp->fcontext)
7174 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7175 }
3690dd37 7176 else if (attr_form_is_section_offset (attr))
8e19ed76 7177 {
4d3c2250 7178 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7179 }
7180 else
7181 {
4d3c2250
KB
7182 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7183 fieldname);
8e19ed76 7184 }
0d564a31 7185 }
d48cc9dd
DJ
7186 else
7187 {
7188 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7189 if (attr && DW_UNSND (attr))
7190 {
7191 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7192 complaint (&symfile_complaints,
3e43a32a
MS
7193 _("Member function \"%s\" (offset %d) is virtual "
7194 "but the vtable offset is not specified"),
d48cc9dd 7195 fieldname, die->offset);
9655fd1a 7196 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7197 TYPE_CPLUS_DYNAMIC (type) = 1;
7198 }
7199 }
c906108c
SS
7200}
7201
7202/* Create the vector of member function fields, and attach it to the type. */
7203
7204static void
fba45db2 7205dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7206 struct dwarf2_cu *cu)
c906108c
SS
7207{
7208 struct fnfieldlist *flp;
7209 int total_length = 0;
7210 int i;
7211
b4ba55a1 7212 if (cu->language == language_ada)
a73c6dcd 7213 error (_("unexpected member functions in Ada type"));
b4ba55a1 7214
c906108c
SS
7215 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7216 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7217 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7218
7219 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7220 {
7221 struct nextfnfield *nfp = flp->head;
7222 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7223 int k;
7224
7225 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7226 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7227 fn_flp->fn_fields = (struct fn_field *)
7228 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7229 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7230 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7231
7232 total_length += flp->length;
7233 }
7234
7235 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7236 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7237}
7238
1168df01
JB
7239/* Returns non-zero if NAME is the name of a vtable member in CU's
7240 language, zero otherwise. */
7241static int
7242is_vtable_name (const char *name, struct dwarf2_cu *cu)
7243{
7244 static const char vptr[] = "_vptr";
987504bb 7245 static const char vtable[] = "vtable";
1168df01 7246
987504bb
JJ
7247 /* Look for the C++ and Java forms of the vtable. */
7248 if ((cu->language == language_java
7249 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7250 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7251 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7252 return 1;
7253
7254 return 0;
7255}
7256
c0dd20ea 7257/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7258 functions, with the ABI-specified layout. If TYPE describes
7259 such a structure, smash it into a member function type.
61049d3b
DJ
7260
7261 GCC shouldn't do this; it should just output pointer to member DIEs.
7262 This is GCC PR debug/28767. */
c0dd20ea 7263
0b92b5bb
TT
7264static void
7265quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7266{
0b92b5bb 7267 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7268
7269 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7270 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7271 return;
c0dd20ea
DJ
7272
7273 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7274 if (TYPE_FIELD_NAME (type, 0) == NULL
7275 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7276 || TYPE_FIELD_NAME (type, 1) == NULL
7277 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7278 return;
c0dd20ea
DJ
7279
7280 /* Find the type of the method. */
0b92b5bb 7281 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7282 if (pfn_type == NULL
7283 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7284 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7285 return;
c0dd20ea
DJ
7286
7287 /* Look for the "this" argument. */
7288 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7289 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7290 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7291 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7292 return;
c0dd20ea
DJ
7293
7294 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7295 new_type = alloc_type (objfile);
7296 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7297 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7298 TYPE_VARARGS (pfn_type));
0b92b5bb 7299 smash_to_methodptr_type (type, new_type);
c0dd20ea 7300}
1168df01 7301
c906108c 7302/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7303 (definition) to create a type for the structure or union. Fill in
7304 the type's name and general properties; the members will not be
7305 processed until process_structure_type.
c906108c 7306
c767944b
DJ
7307 NOTE: we need to call these functions regardless of whether or not the
7308 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7309 structure or union. This gets the type entered into our set of
7310 user defined types.
7311
7312 However, if the structure is incomplete (an opaque struct/union)
7313 then suppress creating a symbol table entry for it since gdb only
7314 wants to find the one with the complete definition. Note that if
7315 it is complete, we just call new_symbol, which does it's own
7316 checking about whether the struct/union is anonymous or not (and
7317 suppresses creating a symbol table entry itself). */
7318
f792889a 7319static struct type *
134d01f1 7320read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7321{
e7c27a73 7322 struct objfile *objfile = cu->objfile;
c906108c
SS
7323 struct type *type;
7324 struct attribute *attr;
39cbfefa 7325 char *name;
c906108c 7326
348e048f
DE
7327 /* If the definition of this type lives in .debug_types, read that type.
7328 Don't follow DW_AT_specification though, that will take us back up
7329 the chain and we want to go down. */
7330 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7331 if (attr)
7332 {
7333 struct dwarf2_cu *type_cu = cu;
7334 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7335
348e048f
DE
7336 /* We could just recurse on read_structure_type, but we need to call
7337 get_die_type to ensure only one type for this DIE is created.
7338 This is important, for example, because for c++ classes we need
7339 TYPE_NAME set which is only done by new_symbol. Blech. */
7340 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7341
7342 /* TYPE_CU may not be the same as CU.
7343 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7344 return set_die_type (die, type, cu);
7345 }
7346
c0dd20ea 7347 type = alloc_type (objfile);
c906108c 7348 INIT_CPLUS_SPECIFIC (type);
93311388 7349
39cbfefa
DJ
7350 name = dwarf2_name (die, cu);
7351 if (name != NULL)
c906108c 7352 {
987504bb
JJ
7353 if (cu->language == language_cplus
7354 || cu->language == language_java)
63d06c5c 7355 {
3da10d80
KS
7356 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7357
7358 /* dwarf2_full_name might have already finished building the DIE's
7359 type. If so, there is no need to continue. */
7360 if (get_die_type (die, cu) != NULL)
7361 return get_die_type (die, cu);
7362
7363 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7364 if (die->tag == DW_TAG_structure_type
7365 || die->tag == DW_TAG_class_type)
7366 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7367 }
7368 else
7369 {
d8151005
DJ
7370 /* The name is already allocated along with this objfile, so
7371 we don't need to duplicate it for the type. */
94af9270
KS
7372 TYPE_TAG_NAME (type) = (char *) name;
7373 if (die->tag == DW_TAG_class_type)
7374 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7375 }
c906108c
SS
7376 }
7377
7378 if (die->tag == DW_TAG_structure_type)
7379 {
7380 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7381 }
7382 else if (die->tag == DW_TAG_union_type)
7383 {
7384 TYPE_CODE (type) = TYPE_CODE_UNION;
7385 }
7386 else
7387 {
c906108c
SS
7388 TYPE_CODE (type) = TYPE_CODE_CLASS;
7389 }
7390
0cc2414c
TT
7391 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7392 TYPE_DECLARED_CLASS (type) = 1;
7393
e142c38c 7394 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7395 if (attr)
7396 {
7397 TYPE_LENGTH (type) = DW_UNSND (attr);
7398 }
7399 else
7400 {
7401 TYPE_LENGTH (type) = 0;
7402 }
7403
876cecd0 7404 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7405 if (die_is_declaration (die, cu))
876cecd0 7406 TYPE_STUB (type) = 1;
a6c727b2
DJ
7407 else if (attr == NULL && die->child == NULL
7408 && producer_is_realview (cu->producer))
7409 /* RealView does not output the required DW_AT_declaration
7410 on incomplete types. */
7411 TYPE_STUB (type) = 1;
dc718098 7412
c906108c
SS
7413 /* We need to add the type field to the die immediately so we don't
7414 infinitely recurse when dealing with pointers to the structure
0963b4bd 7415 type within the structure itself. */
1c379e20 7416 set_die_type (die, type, cu);
c906108c 7417
7e314c57
JK
7418 /* set_die_type should be already done. */
7419 set_descriptive_type (type, die, cu);
7420
c767944b
DJ
7421 return type;
7422}
7423
7424/* Finish creating a structure or union type, including filling in
7425 its members and creating a symbol for it. */
7426
7427static void
7428process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7429{
7430 struct objfile *objfile = cu->objfile;
7431 struct die_info *child_die = die->child;
7432 struct type *type;
7433
7434 type = get_die_type (die, cu);
7435 if (type == NULL)
7436 type = read_structure_type (die, cu);
7437
e142c38c 7438 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7439 {
7440 struct field_info fi;
7441 struct die_info *child_die;
34eaf542 7442 VEC (symbolp) *template_args = NULL;
c767944b 7443 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7444
7445 memset (&fi, 0, sizeof (struct field_info));
7446
639d11d3 7447 child_die = die->child;
c906108c
SS
7448
7449 while (child_die && child_die->tag)
7450 {
a9a9bd0f
DC
7451 if (child_die->tag == DW_TAG_member
7452 || child_die->tag == DW_TAG_variable)
c906108c 7453 {
a9a9bd0f
DC
7454 /* NOTE: carlton/2002-11-05: A C++ static data member
7455 should be a DW_TAG_member that is a declaration, but
7456 all versions of G++ as of this writing (so through at
7457 least 3.2.1) incorrectly generate DW_TAG_variable
7458 tags for them instead. */
e7c27a73 7459 dwarf2_add_field (&fi, child_die, cu);
c906108c 7460 }
8713b1b1 7461 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7462 {
0963b4bd 7463 /* C++ member function. */
e7c27a73 7464 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7465 }
7466 else if (child_die->tag == DW_TAG_inheritance)
7467 {
7468 /* C++ base class field. */
e7c27a73 7469 dwarf2_add_field (&fi, child_die, cu);
c906108c 7470 }
98751a41
JK
7471 else if (child_die->tag == DW_TAG_typedef)
7472 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7473 else if (child_die->tag == DW_TAG_template_type_param
7474 || child_die->tag == DW_TAG_template_value_param)
7475 {
7476 struct symbol *arg = new_symbol (child_die, NULL, cu);
7477
f1078f66
DJ
7478 if (arg != NULL)
7479 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7480 }
7481
c906108c
SS
7482 child_die = sibling_die (child_die);
7483 }
7484
34eaf542
TT
7485 /* Attach template arguments to type. */
7486 if (! VEC_empty (symbolp, template_args))
7487 {
7488 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7489 TYPE_N_TEMPLATE_ARGUMENTS (type)
7490 = VEC_length (symbolp, template_args);
7491 TYPE_TEMPLATE_ARGUMENTS (type)
7492 = obstack_alloc (&objfile->objfile_obstack,
7493 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7494 * sizeof (struct symbol *)));
7495 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7496 VEC_address (symbolp, template_args),
7497 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7498 * sizeof (struct symbol *)));
7499 VEC_free (symbolp, template_args);
7500 }
7501
c906108c
SS
7502 /* Attach fields and member functions to the type. */
7503 if (fi.nfields)
e7c27a73 7504 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7505 if (fi.nfnfields)
7506 {
e7c27a73 7507 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7508
c5aa993b 7509 /* Get the type which refers to the base class (possibly this
c906108c 7510 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7511 class from the DW_AT_containing_type attribute. This use of
7512 DW_AT_containing_type is a GNU extension. */
c906108c 7513
e142c38c 7514 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7515 {
e7c27a73 7516 struct type *t = die_containing_type (die, cu);
c906108c
SS
7517
7518 TYPE_VPTR_BASETYPE (type) = t;
7519 if (type == t)
7520 {
c906108c
SS
7521 int i;
7522
7523 /* Our own class provides vtbl ptr. */
7524 for (i = TYPE_NFIELDS (t) - 1;
7525 i >= TYPE_N_BASECLASSES (t);
7526 --i)
7527 {
7528 char *fieldname = TYPE_FIELD_NAME (t, i);
7529
1168df01 7530 if (is_vtable_name (fieldname, cu))
c906108c
SS
7531 {
7532 TYPE_VPTR_FIELDNO (type) = i;
7533 break;
7534 }
7535 }
7536
7537 /* Complain if virtual function table field not found. */
7538 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7539 complaint (&symfile_complaints,
3e43a32a
MS
7540 _("virtual function table pointer "
7541 "not found when defining class '%s'"),
4d3c2250
KB
7542 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7543 "");
c906108c
SS
7544 }
7545 else
7546 {
7547 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7548 }
7549 }
f6235d4c
EZ
7550 else if (cu->producer
7551 && strncmp (cu->producer,
7552 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7553 {
7554 /* The IBM XLC compiler does not provide direct indication
7555 of the containing type, but the vtable pointer is
7556 always named __vfp. */
7557
7558 int i;
7559
7560 for (i = TYPE_NFIELDS (type) - 1;
7561 i >= TYPE_N_BASECLASSES (type);
7562 --i)
7563 {
7564 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7565 {
7566 TYPE_VPTR_FIELDNO (type) = i;
7567 TYPE_VPTR_BASETYPE (type) = type;
7568 break;
7569 }
7570 }
7571 }
c906108c 7572 }
98751a41
JK
7573
7574 /* Copy fi.typedef_field_list linked list elements content into the
7575 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7576 if (fi.typedef_field_list)
7577 {
7578 int i = fi.typedef_field_list_count;
7579
a0d7a4ff 7580 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7581 TYPE_TYPEDEF_FIELD_ARRAY (type)
7582 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7583 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7584
7585 /* Reverse the list order to keep the debug info elements order. */
7586 while (--i >= 0)
7587 {
7588 struct typedef_field *dest, *src;
6e70227d 7589
98751a41
JK
7590 dest = &TYPE_TYPEDEF_FIELD (type, i);
7591 src = &fi.typedef_field_list->field;
7592 fi.typedef_field_list = fi.typedef_field_list->next;
7593 *dest = *src;
7594 }
7595 }
c767944b
DJ
7596
7597 do_cleanups (back_to);
eb2a6f42
TT
7598
7599 if (HAVE_CPLUS_STRUCT (type))
7600 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 7601 }
63d06c5c 7602
0b92b5bb
TT
7603 quirk_gcc_member_function_pointer (type, cu->objfile);
7604
90aeadfc
DC
7605 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7606 snapshots) has been known to create a die giving a declaration
7607 for a class that has, as a child, a die giving a definition for a
7608 nested class. So we have to process our children even if the
7609 current die is a declaration. Normally, of course, a declaration
7610 won't have any children at all. */
134d01f1 7611
90aeadfc
DC
7612 while (child_die != NULL && child_die->tag)
7613 {
7614 if (child_die->tag == DW_TAG_member
7615 || child_die->tag == DW_TAG_variable
34eaf542
TT
7616 || child_die->tag == DW_TAG_inheritance
7617 || child_die->tag == DW_TAG_template_value_param
7618 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7619 {
90aeadfc 7620 /* Do nothing. */
134d01f1 7621 }
90aeadfc
DC
7622 else
7623 process_die (child_die, cu);
134d01f1 7624
90aeadfc 7625 child_die = sibling_die (child_die);
134d01f1
DJ
7626 }
7627
fa4028e9
JB
7628 /* Do not consider external references. According to the DWARF standard,
7629 these DIEs are identified by the fact that they have no byte_size
7630 attribute, and a declaration attribute. */
7631 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7632 || !die_is_declaration (die, cu))
c767944b 7633 new_symbol (die, type, cu);
134d01f1
DJ
7634}
7635
7636/* Given a DW_AT_enumeration_type die, set its type. We do not
7637 complete the type's fields yet, or create any symbols. */
c906108c 7638
f792889a 7639static struct type *
134d01f1 7640read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7641{
e7c27a73 7642 struct objfile *objfile = cu->objfile;
c906108c 7643 struct type *type;
c906108c 7644 struct attribute *attr;
0114d602 7645 const char *name;
134d01f1 7646
348e048f
DE
7647 /* If the definition of this type lives in .debug_types, read that type.
7648 Don't follow DW_AT_specification though, that will take us back up
7649 the chain and we want to go down. */
7650 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7651 if (attr)
7652 {
7653 struct dwarf2_cu *type_cu = cu;
7654 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7655
348e048f 7656 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7657
7658 /* TYPE_CU may not be the same as CU.
7659 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7660 return set_die_type (die, type, cu);
7661 }
7662
c906108c
SS
7663 type = alloc_type (objfile);
7664
7665 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7666 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7667 if (name != NULL)
0114d602 7668 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7669
e142c38c 7670 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7671 if (attr)
7672 {
7673 TYPE_LENGTH (type) = DW_UNSND (attr);
7674 }
7675 else
7676 {
7677 TYPE_LENGTH (type) = 0;
7678 }
7679
137033e9
JB
7680 /* The enumeration DIE can be incomplete. In Ada, any type can be
7681 declared as private in the package spec, and then defined only
7682 inside the package body. Such types are known as Taft Amendment
7683 Types. When another package uses such a type, an incomplete DIE
7684 may be generated by the compiler. */
02eb380e 7685 if (die_is_declaration (die, cu))
876cecd0 7686 TYPE_STUB (type) = 1;
02eb380e 7687
f792889a 7688 return set_die_type (die, type, cu);
134d01f1
DJ
7689}
7690
7691/* Given a pointer to a die which begins an enumeration, process all
7692 the dies that define the members of the enumeration, and create the
7693 symbol for the enumeration type.
7694
7695 NOTE: We reverse the order of the element list. */
7696
7697static void
7698process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7699{
f792889a 7700 struct type *this_type;
134d01f1 7701
f792889a
DJ
7702 this_type = get_die_type (die, cu);
7703 if (this_type == NULL)
7704 this_type = read_enumeration_type (die, cu);
9dc481d3 7705
639d11d3 7706 if (die->child != NULL)
c906108c 7707 {
9dc481d3
DE
7708 struct die_info *child_die;
7709 struct symbol *sym;
7710 struct field *fields = NULL;
7711 int num_fields = 0;
7712 int unsigned_enum = 1;
7713 char *name;
7714
639d11d3 7715 child_die = die->child;
c906108c
SS
7716 while (child_die && child_die->tag)
7717 {
7718 if (child_die->tag != DW_TAG_enumerator)
7719 {
e7c27a73 7720 process_die (child_die, cu);
c906108c
SS
7721 }
7722 else
7723 {
39cbfefa
DJ
7724 name = dwarf2_name (child_die, cu);
7725 if (name)
c906108c 7726 {
f792889a 7727 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7728 if (SYMBOL_VALUE (sym) < 0)
7729 unsigned_enum = 0;
7730
7731 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7732 {
7733 fields = (struct field *)
7734 xrealloc (fields,
7735 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7736 * sizeof (struct field));
c906108c
SS
7737 }
7738
3567439c 7739 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7740 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7741 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7742 FIELD_BITSIZE (fields[num_fields]) = 0;
7743
7744 num_fields++;
7745 }
7746 }
7747
7748 child_die = sibling_die (child_die);
7749 }
7750
7751 if (num_fields)
7752 {
f792889a
DJ
7753 TYPE_NFIELDS (this_type) = num_fields;
7754 TYPE_FIELDS (this_type) = (struct field *)
7755 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7756 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7757 sizeof (struct field) * num_fields);
b8c9b27d 7758 xfree (fields);
c906108c
SS
7759 }
7760 if (unsigned_enum)
876cecd0 7761 TYPE_UNSIGNED (this_type) = 1;
c906108c 7762 }
134d01f1 7763
6c83ed52
TT
7764 /* If we are reading an enum from a .debug_types unit, and the enum
7765 is a declaration, and the enum is not the signatured type in the
7766 unit, then we do not want to add a symbol for it. Adding a
7767 symbol would in some cases obscure the true definition of the
7768 enum, giving users an incomplete type when the definition is
7769 actually available. Note that we do not want to do this for all
7770 enums which are just declarations, because C++0x allows forward
7771 enum declarations. */
7772 if (cu->per_cu->debug_type_section
7773 && die_is_declaration (die, cu))
7774 {
7775 struct signatured_type *type_sig;
7776
7777 type_sig
7778 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
7779 cu->per_cu->debug_type_section,
7780 cu->per_cu->offset);
7781 if (type_sig->type_offset != die->offset)
7782 return;
7783 }
7784
f792889a 7785 new_symbol (die, this_type, cu);
c906108c
SS
7786}
7787
7788/* Extract all information from a DW_TAG_array_type DIE and put it in
7789 the DIE's type field. For now, this only handles one dimensional
7790 arrays. */
7791
f792889a 7792static struct type *
e7c27a73 7793read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7794{
e7c27a73 7795 struct objfile *objfile = cu->objfile;
c906108c 7796 struct die_info *child_die;
7e314c57 7797 struct type *type;
c906108c
SS
7798 struct type *element_type, *range_type, *index_type;
7799 struct type **range_types = NULL;
7800 struct attribute *attr;
7801 int ndim = 0;
7802 struct cleanup *back_to;
39cbfefa 7803 char *name;
c906108c 7804
e7c27a73 7805 element_type = die_type (die, cu);
c906108c 7806
7e314c57
JK
7807 /* The die_type call above may have already set the type for this DIE. */
7808 type = get_die_type (die, cu);
7809 if (type)
7810 return type;
7811
c906108c
SS
7812 /* Irix 6.2 native cc creates array types without children for
7813 arrays with unspecified length. */
639d11d3 7814 if (die->child == NULL)
c906108c 7815 {
46bf5051 7816 index_type = objfile_type (objfile)->builtin_int;
c906108c 7817 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7818 type = create_array_type (NULL, element_type, range_type);
7819 return set_die_type (die, type, cu);
c906108c
SS
7820 }
7821
7822 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7823 child_die = die->child;
c906108c
SS
7824 while (child_die && child_die->tag)
7825 {
7826 if (child_die->tag == DW_TAG_subrange_type)
7827 {
f792889a 7828 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7829
f792889a 7830 if (child_type != NULL)
a02abb62 7831 {
0963b4bd
MS
7832 /* The range type was succesfully read. Save it for the
7833 array type creation. */
a02abb62
JB
7834 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7835 {
7836 range_types = (struct type **)
7837 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7838 * sizeof (struct type *));
7839 if (ndim == 0)
7840 make_cleanup (free_current_contents, &range_types);
7841 }
f792889a 7842 range_types[ndim++] = child_type;
a02abb62 7843 }
c906108c
SS
7844 }
7845 child_die = sibling_die (child_die);
7846 }
7847
7848 /* Dwarf2 dimensions are output from left to right, create the
7849 necessary array types in backwards order. */
7ca2d3a3 7850
c906108c 7851 type = element_type;
7ca2d3a3
DL
7852
7853 if (read_array_order (die, cu) == DW_ORD_col_major)
7854 {
7855 int i = 0;
9a619af0 7856
7ca2d3a3
DL
7857 while (i < ndim)
7858 type = create_array_type (NULL, type, range_types[i++]);
7859 }
7860 else
7861 {
7862 while (ndim-- > 0)
7863 type = create_array_type (NULL, type, range_types[ndim]);
7864 }
c906108c 7865
f5f8a009
EZ
7866 /* Understand Dwarf2 support for vector types (like they occur on
7867 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7868 array type. This is not part of the Dwarf2/3 standard yet, but a
7869 custom vendor extension. The main difference between a regular
7870 array and the vector variant is that vectors are passed by value
7871 to functions. */
e142c38c 7872 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7873 if (attr)
ea37ba09 7874 make_vector_type (type);
f5f8a009 7875
dbc98a8b
KW
7876 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7877 implementation may choose to implement triple vectors using this
7878 attribute. */
7879 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7880 if (attr)
7881 {
7882 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7883 TYPE_LENGTH (type) = DW_UNSND (attr);
7884 else
3e43a32a
MS
7885 complaint (&symfile_complaints,
7886 _("DW_AT_byte_size for array type smaller "
7887 "than the total size of elements"));
dbc98a8b
KW
7888 }
7889
39cbfefa
DJ
7890 name = dwarf2_name (die, cu);
7891 if (name)
7892 TYPE_NAME (type) = name;
6e70227d 7893
0963b4bd 7894 /* Install the type in the die. */
7e314c57
JK
7895 set_die_type (die, type, cu);
7896
7897 /* set_die_type should be already done. */
b4ba55a1
JB
7898 set_descriptive_type (type, die, cu);
7899
c906108c
SS
7900 do_cleanups (back_to);
7901
7e314c57 7902 return type;
c906108c
SS
7903}
7904
7ca2d3a3 7905static enum dwarf_array_dim_ordering
6e70227d 7906read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7907{
7908 struct attribute *attr;
7909
7910 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7911
7912 if (attr) return DW_SND (attr);
7913
0963b4bd
MS
7914 /* GNU F77 is a special case, as at 08/2004 array type info is the
7915 opposite order to the dwarf2 specification, but data is still
7916 laid out as per normal fortran.
7ca2d3a3 7917
0963b4bd
MS
7918 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7919 version checking. */
7ca2d3a3 7920
905e0470
PM
7921 if (cu->language == language_fortran
7922 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7923 {
7924 return DW_ORD_row_major;
7925 }
7926
6e70227d 7927 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7928 {
7929 case array_column_major:
7930 return DW_ORD_col_major;
7931 case array_row_major:
7932 default:
7933 return DW_ORD_row_major;
7934 };
7935}
7936
72019c9c 7937/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 7938 the DIE's type field. */
72019c9c 7939
f792889a 7940static struct type *
72019c9c
GM
7941read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7942{
7e314c57
JK
7943 struct type *domain_type, *set_type;
7944 struct attribute *attr;
f792889a 7945
7e314c57
JK
7946 domain_type = die_type (die, cu);
7947
7948 /* The die_type call above may have already set the type for this DIE. */
7949 set_type = get_die_type (die, cu);
7950 if (set_type)
7951 return set_type;
7952
7953 set_type = create_set_type (NULL, domain_type);
7954
7955 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7956 if (attr)
7957 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7958
f792889a 7959 return set_die_type (die, set_type, cu);
72019c9c 7960}
7ca2d3a3 7961
c906108c
SS
7962/* First cut: install each common block member as a global variable. */
7963
7964static void
e7c27a73 7965read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7966{
7967 struct die_info *child_die;
7968 struct attribute *attr;
7969 struct symbol *sym;
7970 CORE_ADDR base = (CORE_ADDR) 0;
7971
e142c38c 7972 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7973 if (attr)
7974 {
0963b4bd 7975 /* Support the .debug_loc offsets. */
8e19ed76
PS
7976 if (attr_form_is_block (attr))
7977 {
e7c27a73 7978 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7979 }
3690dd37 7980 else if (attr_form_is_section_offset (attr))
8e19ed76 7981 {
4d3c2250 7982 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7983 }
7984 else
7985 {
4d3c2250
KB
7986 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7987 "common block member");
8e19ed76 7988 }
c906108c 7989 }
639d11d3 7990 if (die->child != NULL)
c906108c 7991 {
639d11d3 7992 child_die = die->child;
c906108c
SS
7993 while (child_die && child_die->tag)
7994 {
74ac6d43
TT
7995 LONGEST offset;
7996
e7c27a73 7997 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
7998 if (sym != NULL
7999 && handle_data_member_location (child_die, cu, &offset))
c906108c 8000 {
74ac6d43 8001 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
8002 add_symbol_to_list (sym, &global_symbols);
8003 }
8004 child_die = sibling_die (child_die);
8005 }
8006 }
8007}
8008
0114d602 8009/* Create a type for a C++ namespace. */
d9fa45fe 8010
0114d602
DJ
8011static struct type *
8012read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 8013{
e7c27a73 8014 struct objfile *objfile = cu->objfile;
0114d602 8015 const char *previous_prefix, *name;
9219021c 8016 int is_anonymous;
0114d602
DJ
8017 struct type *type;
8018
8019 /* For extensions, reuse the type of the original namespace. */
8020 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8021 {
8022 struct die_info *ext_die;
8023 struct dwarf2_cu *ext_cu = cu;
9a619af0 8024
0114d602
DJ
8025 ext_die = dwarf2_extension (die, &ext_cu);
8026 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
8027
8028 /* EXT_CU may not be the same as CU.
8029 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
8030 return set_die_type (die, type, cu);
8031 }
9219021c 8032
e142c38c 8033 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
8034
8035 /* Now build the name of the current namespace. */
8036
0114d602
DJ
8037 previous_prefix = determine_prefix (die, cu);
8038 if (previous_prefix[0] != '\0')
8039 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 8040 previous_prefix, name, 0, cu);
0114d602
DJ
8041
8042 /* Create the type. */
8043 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8044 objfile);
8045 TYPE_NAME (type) = (char *) name;
8046 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8047
60531b24 8048 return set_die_type (die, type, cu);
0114d602
DJ
8049}
8050
8051/* Read a C++ namespace. */
8052
8053static void
8054read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8055{
8056 struct objfile *objfile = cu->objfile;
0114d602 8057 int is_anonymous;
9219021c 8058
5c4e30ca
DC
8059 /* Add a symbol associated to this if we haven't seen the namespace
8060 before. Also, add a using directive if it's an anonymous
8061 namespace. */
9219021c 8062
f2f0e013 8063 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
8064 {
8065 struct type *type;
8066
0114d602 8067 type = read_type_die (die, cu);
e7c27a73 8068 new_symbol (die, type, cu);
5c4e30ca 8069
e8e80198 8070 namespace_name (die, &is_anonymous, cu);
5c4e30ca 8071 if (is_anonymous)
0114d602
DJ
8072 {
8073 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 8074
c0cc3a76 8075 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 8076 NULL, NULL, &objfile->objfile_obstack);
0114d602 8077 }
5c4e30ca 8078 }
9219021c 8079
639d11d3 8080 if (die->child != NULL)
d9fa45fe 8081 {
639d11d3 8082 struct die_info *child_die = die->child;
6e70227d 8083
d9fa45fe
DC
8084 while (child_die && child_die->tag)
8085 {
e7c27a73 8086 process_die (child_die, cu);
d9fa45fe
DC
8087 child_die = sibling_die (child_die);
8088 }
8089 }
38d518c9
EZ
8090}
8091
f55ee35c
JK
8092/* Read a Fortran module as type. This DIE can be only a declaration used for
8093 imported module. Still we need that type as local Fortran "use ... only"
8094 declaration imports depend on the created type in determine_prefix. */
8095
8096static struct type *
8097read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8098{
8099 struct objfile *objfile = cu->objfile;
8100 char *module_name;
8101 struct type *type;
8102
8103 module_name = dwarf2_name (die, cu);
8104 if (!module_name)
3e43a32a
MS
8105 complaint (&symfile_complaints,
8106 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
8107 die->offset);
8108 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8109
8110 /* determine_prefix uses TYPE_TAG_NAME. */
8111 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8112
8113 return set_die_type (die, type, cu);
8114}
8115
5d7cb8df
JK
8116/* Read a Fortran module. */
8117
8118static void
8119read_module (struct die_info *die, struct dwarf2_cu *cu)
8120{
8121 struct die_info *child_die = die->child;
8122
5d7cb8df
JK
8123 while (child_die && child_die->tag)
8124 {
8125 process_die (child_die, cu);
8126 child_die = sibling_die (child_die);
8127 }
8128}
8129
38d518c9
EZ
8130/* Return the name of the namespace represented by DIE. Set
8131 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8132 namespace. */
8133
8134static const char *
e142c38c 8135namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8136{
8137 struct die_info *current_die;
8138 const char *name = NULL;
8139
8140 /* Loop through the extensions until we find a name. */
8141
8142 for (current_die = die;
8143 current_die != NULL;
f2f0e013 8144 current_die = dwarf2_extension (die, &cu))
38d518c9 8145 {
e142c38c 8146 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8147 if (name != NULL)
8148 break;
8149 }
8150
8151 /* Is it an anonymous namespace? */
8152
8153 *is_anonymous = (name == NULL);
8154 if (*is_anonymous)
2b1dbab0 8155 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8156
8157 return name;
d9fa45fe
DC
8158}
8159
c906108c
SS
8160/* Extract all information from a DW_TAG_pointer_type DIE and add to
8161 the user defined type vector. */
8162
f792889a 8163static struct type *
e7c27a73 8164read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8165{
5e2b427d 8166 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8167 struct comp_unit_head *cu_header = &cu->header;
c906108c 8168 struct type *type;
8b2dbe47
KB
8169 struct attribute *attr_byte_size;
8170 struct attribute *attr_address_class;
8171 int byte_size, addr_class;
7e314c57
JK
8172 struct type *target_type;
8173
8174 target_type = die_type (die, cu);
c906108c 8175
7e314c57
JK
8176 /* The die_type call above may have already set the type for this DIE. */
8177 type = get_die_type (die, cu);
8178 if (type)
8179 return type;
8180
8181 type = lookup_pointer_type (target_type);
8b2dbe47 8182
e142c38c 8183 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8184 if (attr_byte_size)
8185 byte_size = DW_UNSND (attr_byte_size);
c906108c 8186 else
8b2dbe47
KB
8187 byte_size = cu_header->addr_size;
8188
e142c38c 8189 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8190 if (attr_address_class)
8191 addr_class = DW_UNSND (attr_address_class);
8192 else
8193 addr_class = DW_ADDR_none;
8194
8195 /* If the pointer size or address class is different than the
8196 default, create a type variant marked as such and set the
8197 length accordingly. */
8198 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8199 {
5e2b427d 8200 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8201 {
8202 int type_flags;
8203
849957d9 8204 type_flags = gdbarch_address_class_type_flags
5e2b427d 8205 (gdbarch, byte_size, addr_class);
876cecd0
TT
8206 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8207 == 0);
8b2dbe47
KB
8208 type = make_type_with_address_space (type, type_flags);
8209 }
8210 else if (TYPE_LENGTH (type) != byte_size)
8211 {
3e43a32a
MS
8212 complaint (&symfile_complaints,
8213 _("invalid pointer size %d"), byte_size);
8b2dbe47 8214 }
6e70227d 8215 else
9a619af0
MS
8216 {
8217 /* Should we also complain about unhandled address classes? */
8218 }
c906108c 8219 }
8b2dbe47
KB
8220
8221 TYPE_LENGTH (type) = byte_size;
f792889a 8222 return set_die_type (die, type, cu);
c906108c
SS
8223}
8224
8225/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8226 the user defined type vector. */
8227
f792889a 8228static struct type *
e7c27a73 8229read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8230{
8231 struct type *type;
8232 struct type *to_type;
8233 struct type *domain;
8234
e7c27a73
DJ
8235 to_type = die_type (die, cu);
8236 domain = die_containing_type (die, cu);
0d5de010 8237
7e314c57
JK
8238 /* The calls above may have already set the type for this DIE. */
8239 type = get_die_type (die, cu);
8240 if (type)
8241 return type;
8242
0d5de010
DJ
8243 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8244 type = lookup_methodptr_type (to_type);
8245 else
8246 type = lookup_memberptr_type (to_type, domain);
c906108c 8247
f792889a 8248 return set_die_type (die, type, cu);
c906108c
SS
8249}
8250
8251/* Extract all information from a DW_TAG_reference_type DIE and add to
8252 the user defined type vector. */
8253
f792889a 8254static struct type *
e7c27a73 8255read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8256{
e7c27a73 8257 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8258 struct type *type, *target_type;
c906108c
SS
8259 struct attribute *attr;
8260
7e314c57
JK
8261 target_type = die_type (die, cu);
8262
8263 /* The die_type call above may have already set the type for this DIE. */
8264 type = get_die_type (die, cu);
8265 if (type)
8266 return type;
8267
8268 type = lookup_reference_type (target_type);
e142c38c 8269 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8270 if (attr)
8271 {
8272 TYPE_LENGTH (type) = DW_UNSND (attr);
8273 }
8274 else
8275 {
107d2387 8276 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8277 }
f792889a 8278 return set_die_type (die, type, cu);
c906108c
SS
8279}
8280
f792889a 8281static struct type *
e7c27a73 8282read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8283{
f792889a 8284 struct type *base_type, *cv_type;
c906108c 8285
e7c27a73 8286 base_type = die_type (die, cu);
7e314c57
JK
8287
8288 /* The die_type call above may have already set the type for this DIE. */
8289 cv_type = get_die_type (die, cu);
8290 if (cv_type)
8291 return cv_type;
8292
2f608a3a
KW
8293 /* In case the const qualifier is applied to an array type, the element type
8294 is so qualified, not the array type (section 6.7.3 of C99). */
8295 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8296 {
8297 struct type *el_type, *inner_array;
8298
8299 base_type = copy_type (base_type);
8300 inner_array = base_type;
8301
8302 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8303 {
8304 TYPE_TARGET_TYPE (inner_array) =
8305 copy_type (TYPE_TARGET_TYPE (inner_array));
8306 inner_array = TYPE_TARGET_TYPE (inner_array);
8307 }
8308
8309 el_type = TYPE_TARGET_TYPE (inner_array);
8310 TYPE_TARGET_TYPE (inner_array) =
8311 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8312
8313 return set_die_type (die, base_type, cu);
8314 }
8315
f792889a
DJ
8316 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8317 return set_die_type (die, cv_type, cu);
c906108c
SS
8318}
8319
f792889a 8320static struct type *
e7c27a73 8321read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8322{
f792889a 8323 struct type *base_type, *cv_type;
c906108c 8324
e7c27a73 8325 base_type = die_type (die, cu);
7e314c57
JK
8326
8327 /* The die_type call above may have already set the type for this DIE. */
8328 cv_type = get_die_type (die, cu);
8329 if (cv_type)
8330 return cv_type;
8331
f792889a
DJ
8332 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8333 return set_die_type (die, cv_type, cu);
c906108c
SS
8334}
8335
8336/* Extract all information from a DW_TAG_string_type DIE and add to
8337 the user defined type vector. It isn't really a user defined type,
8338 but it behaves like one, with other DIE's using an AT_user_def_type
8339 attribute to reference it. */
8340
f792889a 8341static struct type *
e7c27a73 8342read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8343{
e7c27a73 8344 struct objfile *objfile = cu->objfile;
3b7538c0 8345 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8346 struct type *type, *range_type, *index_type, *char_type;
8347 struct attribute *attr;
8348 unsigned int length;
8349
e142c38c 8350 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8351 if (attr)
8352 {
8353 length = DW_UNSND (attr);
8354 }
8355 else
8356 {
0963b4bd 8357 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8358 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8359 if (attr)
8360 {
8361 length = DW_UNSND (attr);
8362 }
8363 else
8364 {
8365 length = 1;
8366 }
c906108c 8367 }
6ccb9162 8368
46bf5051 8369 index_type = objfile_type (objfile)->builtin_int;
c906108c 8370 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8371 char_type = language_string_char_type (cu->language_defn, gdbarch);
8372 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8373
f792889a 8374 return set_die_type (die, type, cu);
c906108c
SS
8375}
8376
8377/* Handle DIES due to C code like:
8378
8379 struct foo
c5aa993b
JM
8380 {
8381 int (*funcp)(int a, long l);
8382 int b;
8383 };
c906108c 8384
0963b4bd 8385 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8386
f792889a 8387static struct type *
e7c27a73 8388read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8389{
0963b4bd
MS
8390 struct type *type; /* Type that this function returns. */
8391 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8392 struct attribute *attr;
8393
e7c27a73 8394 type = die_type (die, cu);
7e314c57
JK
8395
8396 /* The die_type call above may have already set the type for this DIE. */
8397 ftype = get_die_type (die, cu);
8398 if (ftype)
8399 return ftype;
8400
0c8b41f1 8401 ftype = lookup_function_type (type);
c906108c 8402
5b8101ae 8403 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8404 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8405 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8406 || cu->language == language_cplus
5b8101ae
PM
8407 || cu->language == language_java
8408 || cu->language == language_pascal)
876cecd0 8409 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8410 else if (producer_is_realview (cu->producer))
8411 /* RealView does not emit DW_AT_prototyped. We can not
8412 distinguish prototyped and unprototyped functions; default to
8413 prototyped, since that is more common in modern code (and
8414 RealView warns about unprototyped functions). */
8415 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8416
c055b101
CV
8417 /* Store the calling convention in the type if it's available in
8418 the subroutine die. Otherwise set the calling convention to
8419 the default value DW_CC_normal. */
8420 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8421 if (attr)
8422 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8423 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8424 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8425 else
8426 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8427
8428 /* We need to add the subroutine type to the die immediately so
8429 we don't infinitely recurse when dealing with parameters
0963b4bd 8430 declared as the same subroutine type. */
76c10ea2 8431 set_die_type (die, ftype, cu);
6e70227d 8432
639d11d3 8433 if (die->child != NULL)
c906108c 8434 {
8072405b 8435 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 8436 struct die_info *child_die;
8072405b 8437 int nparams, iparams;
c906108c
SS
8438
8439 /* Count the number of parameters.
8440 FIXME: GDB currently ignores vararg functions, but knows about
8441 vararg member functions. */
8072405b 8442 nparams = 0;
639d11d3 8443 child_die = die->child;
c906108c
SS
8444 while (child_die && child_die->tag)
8445 {
8446 if (child_die->tag == DW_TAG_formal_parameter)
8447 nparams++;
8448 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8449 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8450 child_die = sibling_die (child_die);
8451 }
8452
8453 /* Allocate storage for parameters and fill them in. */
8454 TYPE_NFIELDS (ftype) = nparams;
8455 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8456 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8457
8072405b
JK
8458 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8459 even if we error out during the parameters reading below. */
8460 for (iparams = 0; iparams < nparams; iparams++)
8461 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8462
8463 iparams = 0;
639d11d3 8464 child_die = die->child;
c906108c
SS
8465 while (child_die && child_die->tag)
8466 {
8467 if (child_die->tag == DW_TAG_formal_parameter)
8468 {
3ce3b1ba
PA
8469 struct type *arg_type;
8470
8471 /* DWARF version 2 has no clean way to discern C++
8472 static and non-static member functions. G++ helps
8473 GDB by marking the first parameter for non-static
8474 member functions (which is the this pointer) as
8475 artificial. We pass this information to
8476 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8477
8478 DWARF version 3 added DW_AT_object_pointer, which GCC
8479 4.5 does not yet generate. */
e142c38c 8480 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8481 if (attr)
8482 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8483 else
418835cc
KS
8484 {
8485 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8486
8487 /* GCC/43521: In java, the formal parameter
8488 "this" is sometimes not marked with DW_AT_artificial. */
8489 if (cu->language == language_java)
8490 {
8491 const char *name = dwarf2_name (child_die, cu);
9a619af0 8492
418835cc
KS
8493 if (name && !strcmp (name, "this"))
8494 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8495 }
8496 }
3ce3b1ba
PA
8497 arg_type = die_type (child_die, cu);
8498
8499 /* RealView does not mark THIS as const, which the testsuite
8500 expects. GCC marks THIS as const in method definitions,
8501 but not in the class specifications (GCC PR 43053). */
8502 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8503 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8504 {
8505 int is_this = 0;
8506 struct dwarf2_cu *arg_cu = cu;
8507 const char *name = dwarf2_name (child_die, cu);
8508
8509 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8510 if (attr)
8511 {
8512 /* If the compiler emits this, use it. */
8513 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8514 is_this = 1;
8515 }
8516 else if (name && strcmp (name, "this") == 0)
8517 /* Function definitions will have the argument names. */
8518 is_this = 1;
8519 else if (name == NULL && iparams == 0)
8520 /* Declarations may not have the names, so like
8521 elsewhere in GDB, assume an artificial first
8522 argument is "this". */
8523 is_this = 1;
8524
8525 if (is_this)
8526 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8527 arg_type, 0);
8528 }
8529
8530 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8531 iparams++;
8532 }
8533 child_die = sibling_die (child_die);
8534 }
8535 }
8536
76c10ea2 8537 return ftype;
c906108c
SS
8538}
8539
f792889a 8540static struct type *
e7c27a73 8541read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8542{
e7c27a73 8543 struct objfile *objfile = cu->objfile;
0114d602 8544 const char *name = NULL;
f792889a 8545 struct type *this_type;
c906108c 8546
94af9270 8547 name = dwarf2_full_name (NULL, die, cu);
f792889a 8548 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8549 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8550 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8551 set_die_type (die, this_type, cu);
8552 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8553 return this_type;
c906108c
SS
8554}
8555
8556/* Find a representation of a given base type and install
8557 it in the TYPE field of the die. */
8558
f792889a 8559static struct type *
e7c27a73 8560read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8561{
e7c27a73 8562 struct objfile *objfile = cu->objfile;
c906108c
SS
8563 struct type *type;
8564 struct attribute *attr;
8565 int encoding = 0, size = 0;
39cbfefa 8566 char *name;
6ccb9162
UW
8567 enum type_code code = TYPE_CODE_INT;
8568 int type_flags = 0;
8569 struct type *target_type = NULL;
c906108c 8570
e142c38c 8571 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8572 if (attr)
8573 {
8574 encoding = DW_UNSND (attr);
8575 }
e142c38c 8576 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8577 if (attr)
8578 {
8579 size = DW_UNSND (attr);
8580 }
39cbfefa 8581 name = dwarf2_name (die, cu);
6ccb9162 8582 if (!name)
c906108c 8583 {
6ccb9162
UW
8584 complaint (&symfile_complaints,
8585 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8586 }
6ccb9162
UW
8587
8588 switch (encoding)
c906108c 8589 {
6ccb9162
UW
8590 case DW_ATE_address:
8591 /* Turn DW_ATE_address into a void * pointer. */
8592 code = TYPE_CODE_PTR;
8593 type_flags |= TYPE_FLAG_UNSIGNED;
8594 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8595 break;
8596 case DW_ATE_boolean:
8597 code = TYPE_CODE_BOOL;
8598 type_flags |= TYPE_FLAG_UNSIGNED;
8599 break;
8600 case DW_ATE_complex_float:
8601 code = TYPE_CODE_COMPLEX;
8602 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8603 break;
8604 case DW_ATE_decimal_float:
8605 code = TYPE_CODE_DECFLOAT;
8606 break;
8607 case DW_ATE_float:
8608 code = TYPE_CODE_FLT;
8609 break;
8610 case DW_ATE_signed:
8611 break;
8612 case DW_ATE_unsigned:
8613 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8614 if (cu->language == language_fortran
8615 && name
8616 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8617 code = TYPE_CODE_CHAR;
6ccb9162
UW
8618 break;
8619 case DW_ATE_signed_char:
6e70227d 8620 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8621 || cu->language == language_pascal
8622 || cu->language == language_fortran)
6ccb9162
UW
8623 code = TYPE_CODE_CHAR;
8624 break;
8625 case DW_ATE_unsigned_char:
868a0084 8626 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8627 || cu->language == language_pascal
8628 || cu->language == language_fortran)
6ccb9162
UW
8629 code = TYPE_CODE_CHAR;
8630 type_flags |= TYPE_FLAG_UNSIGNED;
8631 break;
75079b2b
TT
8632 case DW_ATE_UTF:
8633 /* We just treat this as an integer and then recognize the
8634 type by name elsewhere. */
8635 break;
8636
6ccb9162
UW
8637 default:
8638 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8639 dwarf_type_encoding_name (encoding));
8640 break;
c906108c 8641 }
6ccb9162 8642
0114d602
DJ
8643 type = init_type (code, size, type_flags, NULL, objfile);
8644 TYPE_NAME (type) = name;
6ccb9162
UW
8645 TYPE_TARGET_TYPE (type) = target_type;
8646
0114d602 8647 if (name && strcmp (name, "char") == 0)
876cecd0 8648 TYPE_NOSIGN (type) = 1;
0114d602 8649
f792889a 8650 return set_die_type (die, type, cu);
c906108c
SS
8651}
8652
a02abb62
JB
8653/* Read the given DW_AT_subrange DIE. */
8654
f792889a 8655static struct type *
a02abb62
JB
8656read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8657{
8658 struct type *base_type;
8659 struct type *range_type;
8660 struct attribute *attr;
43bbcdc2
PH
8661 LONGEST low = 0;
8662 LONGEST high = -1;
39cbfefa 8663 char *name;
43bbcdc2 8664 LONGEST negative_mask;
e77813c8 8665
a02abb62 8666 base_type = die_type (die, cu);
953ac07e
JK
8667 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8668 check_typedef (base_type);
a02abb62 8669
7e314c57
JK
8670 /* The die_type call above may have already set the type for this DIE. */
8671 range_type = get_die_type (die, cu);
8672 if (range_type)
8673 return range_type;
8674
e142c38c 8675 if (cu->language == language_fortran)
6e70227d 8676 {
a02abb62
JB
8677 /* FORTRAN implies a lower bound of 1, if not given. */
8678 low = 1;
8679 }
8680
dd5e6932
DJ
8681 /* FIXME: For variable sized arrays either of these could be
8682 a variable rather than a constant value. We'll allow it,
8683 but we don't know how to handle it. */
e142c38c 8684 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8685 if (attr)
8686 low = dwarf2_get_attr_constant_value (attr, 0);
8687
e142c38c 8688 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8689 if (attr)
6e70227d 8690 {
d48323d8 8691 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
8692 {
8693 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8694 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8695 FIXME: GDB does not yet know how to handle dynamic
8696 arrays properly, treat them as arrays with unspecified
8697 length for now.
8698
8699 FIXME: jimb/2003-09-22: GDB does not really know
8700 how to handle arrays of unspecified length
8701 either; we just represent them as zero-length
8702 arrays. Choose an appropriate upper bound given
8703 the lower bound we've computed above. */
8704 high = low - 1;
8705 }
8706 else
8707 high = dwarf2_get_attr_constant_value (attr, 1);
8708 }
e77813c8
PM
8709 else
8710 {
8711 attr = dwarf2_attr (die, DW_AT_count, cu);
8712 if (attr)
8713 {
8714 int count = dwarf2_get_attr_constant_value (attr, 1);
8715 high = low + count - 1;
8716 }
c2ff108b
JK
8717 else
8718 {
8719 /* Unspecified array length. */
8720 high = low - 1;
8721 }
e77813c8
PM
8722 }
8723
8724 /* Dwarf-2 specifications explicitly allows to create subrange types
8725 without specifying a base type.
8726 In that case, the base type must be set to the type of
8727 the lower bound, upper bound or count, in that order, if any of these
8728 three attributes references an object that has a type.
8729 If no base type is found, the Dwarf-2 specifications say that
8730 a signed integer type of size equal to the size of an address should
8731 be used.
8732 For the following C code: `extern char gdb_int [];'
8733 GCC produces an empty range DIE.
8734 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 8735 high bound or count are not yet handled by this code. */
e77813c8
PM
8736 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8737 {
8738 struct objfile *objfile = cu->objfile;
8739 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8740 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8741 struct type *int_type = objfile_type (objfile)->builtin_int;
8742
8743 /* Test "int", "long int", and "long long int" objfile types,
8744 and select the first one having a size above or equal to the
8745 architecture address size. */
8746 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8747 base_type = int_type;
8748 else
8749 {
8750 int_type = objfile_type (objfile)->builtin_long;
8751 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8752 base_type = int_type;
8753 else
8754 {
8755 int_type = objfile_type (objfile)->builtin_long_long;
8756 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8757 base_type = int_type;
8758 }
8759 }
8760 }
a02abb62 8761
6e70227d 8762 negative_mask =
43bbcdc2
PH
8763 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8764 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8765 low |= negative_mask;
8766 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8767 high |= negative_mask;
8768
a02abb62
JB
8769 range_type = create_range_type (NULL, base_type, low, high);
8770
bbb0eef6
JK
8771 /* Mark arrays with dynamic length at least as an array of unspecified
8772 length. GDB could check the boundary but before it gets implemented at
8773 least allow accessing the array elements. */
d48323d8 8774 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
8775 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8776
c2ff108b
JK
8777 /* Ada expects an empty array on no boundary attributes. */
8778 if (attr == NULL && cu->language != language_ada)
8779 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8780
39cbfefa
DJ
8781 name = dwarf2_name (die, cu);
8782 if (name)
8783 TYPE_NAME (range_type) = name;
6e70227d 8784
e142c38c 8785 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8786 if (attr)
8787 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8788
7e314c57
JK
8789 set_die_type (die, range_type, cu);
8790
8791 /* set_die_type should be already done. */
b4ba55a1
JB
8792 set_descriptive_type (range_type, die, cu);
8793
7e314c57 8794 return range_type;
a02abb62 8795}
6e70227d 8796
f792889a 8797static struct type *
81a17f79
JB
8798read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8799{
8800 struct type *type;
81a17f79 8801
81a17f79
JB
8802 /* For now, we only support the C meaning of an unspecified type: void. */
8803
0114d602
DJ
8804 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8805 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8806
f792889a 8807 return set_die_type (die, type, cu);
81a17f79 8808}
a02abb62 8809
51545339
DJ
8810/* Trivial hash function for die_info: the hash value of a DIE
8811 is its offset in .debug_info for this objfile. */
8812
8813static hashval_t
8814die_hash (const void *item)
8815{
8816 const struct die_info *die = item;
9a619af0 8817
51545339
DJ
8818 return die->offset;
8819}
8820
8821/* Trivial comparison function for die_info structures: two DIEs
8822 are equal if they have the same offset. */
8823
8824static int
8825die_eq (const void *item_lhs, const void *item_rhs)
8826{
8827 const struct die_info *die_lhs = item_lhs;
8828 const struct die_info *die_rhs = item_rhs;
9a619af0 8829
51545339
DJ
8830 return die_lhs->offset == die_rhs->offset;
8831}
8832
c906108c
SS
8833/* Read a whole compilation unit into a linked list of dies. */
8834
f9aca02d 8835static struct die_info *
93311388 8836read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8837{
93311388 8838 struct die_reader_specs reader_specs;
98bfdba5 8839 int read_abbrevs = 0;
1d9ec526 8840 struct cleanup *back_to = NULL;
98bfdba5
PA
8841 struct die_info *die;
8842
8843 if (cu->dwarf2_abbrevs == NULL)
8844 {
8845 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8846 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8847 read_abbrevs = 1;
8848 }
93311388 8849
348e048f 8850 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8851 cu->die_hash
8852 = htab_create_alloc_ex (cu->header.length / 12,
8853 die_hash,
8854 die_eq,
8855 NULL,
8856 &cu->comp_unit_obstack,
8857 hashtab_obstack_allocate,
8858 dummy_obstack_deallocate);
8859
93311388
DE
8860 init_cu_die_reader (&reader_specs, cu);
8861
98bfdba5
PA
8862 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8863
8864 if (read_abbrevs)
8865 do_cleanups (back_to);
8866
8867 return die;
639d11d3
DC
8868}
8869
d97bc12b
DE
8870/* Main entry point for reading a DIE and all children.
8871 Read the DIE and dump it if requested. */
8872
8873static struct die_info *
93311388
DE
8874read_die_and_children (const struct die_reader_specs *reader,
8875 gdb_byte *info_ptr,
d97bc12b
DE
8876 gdb_byte **new_info_ptr,
8877 struct die_info *parent)
8878{
93311388 8879 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8880 new_info_ptr, parent);
8881
8882 if (dwarf2_die_debug)
8883 {
348e048f
DE
8884 fprintf_unfiltered (gdb_stdlog,
8885 "\nRead die from %s of %s:\n",
8b70b953
TT
8886 (reader->cu->per_cu->debug_type_section
8887 ? ".debug_types"
8888 : ".debug_info"),
348e048f 8889 reader->abfd->filename);
d97bc12b
DE
8890 dump_die (result, dwarf2_die_debug);
8891 }
8892
8893 return result;
8894}
8895
639d11d3
DC
8896/* Read a single die and all its descendents. Set the die's sibling
8897 field to NULL; set other fields in the die correctly, and set all
8898 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8899 location of the info_ptr after reading all of those dies. PARENT
8900 is the parent of the die in question. */
8901
8902static struct die_info *
93311388
DE
8903read_die_and_children_1 (const struct die_reader_specs *reader,
8904 gdb_byte *info_ptr,
d97bc12b
DE
8905 gdb_byte **new_info_ptr,
8906 struct die_info *parent)
639d11d3
DC
8907{
8908 struct die_info *die;
fe1b8b76 8909 gdb_byte *cur_ptr;
639d11d3
DC
8910 int has_children;
8911
93311388 8912 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8913 if (die == NULL)
8914 {
8915 *new_info_ptr = cur_ptr;
8916 return NULL;
8917 }
93311388 8918 store_in_ref_table (die, reader->cu);
639d11d3
DC
8919
8920 if (has_children)
348e048f 8921 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8922 else
8923 {
8924 die->child = NULL;
8925 *new_info_ptr = cur_ptr;
8926 }
8927
8928 die->sibling = NULL;
8929 die->parent = parent;
8930 return die;
8931}
8932
8933/* Read a die, all of its descendents, and all of its siblings; set
8934 all of the fields of all of the dies correctly. Arguments are as
8935 in read_die_and_children. */
8936
8937static struct die_info *
93311388
DE
8938read_die_and_siblings (const struct die_reader_specs *reader,
8939 gdb_byte *info_ptr,
fe1b8b76 8940 gdb_byte **new_info_ptr,
639d11d3
DC
8941 struct die_info *parent)
8942{
8943 struct die_info *first_die, *last_sibling;
fe1b8b76 8944 gdb_byte *cur_ptr;
639d11d3 8945
c906108c 8946 cur_ptr = info_ptr;
639d11d3
DC
8947 first_die = last_sibling = NULL;
8948
8949 while (1)
c906108c 8950 {
639d11d3 8951 struct die_info *die
93311388 8952 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8953
1d325ec1 8954 if (die == NULL)
c906108c 8955 {
639d11d3
DC
8956 *new_info_ptr = cur_ptr;
8957 return first_die;
c906108c 8958 }
1d325ec1
DJ
8959
8960 if (!first_die)
8961 first_die = die;
c906108c 8962 else
1d325ec1
DJ
8963 last_sibling->sibling = die;
8964
8965 last_sibling = die;
c906108c 8966 }
c906108c
SS
8967}
8968
93311388
DE
8969/* Read the die from the .debug_info section buffer. Set DIEP to
8970 point to a newly allocated die with its information, except for its
8971 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8972 whether the die has children or not. */
8973
8974static gdb_byte *
8975read_full_die (const struct die_reader_specs *reader,
8976 struct die_info **diep, gdb_byte *info_ptr,
8977 int *has_children)
8978{
8979 unsigned int abbrev_number, bytes_read, i, offset;
8980 struct abbrev_info *abbrev;
8981 struct die_info *die;
8982 struct dwarf2_cu *cu = reader->cu;
8983 bfd *abfd = reader->abfd;
8984
8985 offset = info_ptr - reader->buffer;
8986 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8987 info_ptr += bytes_read;
8988 if (!abbrev_number)
8989 {
8990 *diep = NULL;
8991 *has_children = 0;
8992 return info_ptr;
8993 }
8994
8995 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8996 if (!abbrev)
348e048f
DE
8997 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8998 abbrev_number,
8999 bfd_get_filename (abfd));
9000
93311388
DE
9001 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9002 die->offset = offset;
9003 die->tag = abbrev->tag;
9004 die->abbrev = abbrev_number;
9005
9006 die->num_attrs = abbrev->num_attrs;
9007
9008 for (i = 0; i < abbrev->num_attrs; ++i)
9009 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9010 abfd, info_ptr, cu);
9011
9012 *diep = die;
9013 *has_children = abbrev->has_children;
9014 return info_ptr;
9015}
9016
c906108c
SS
9017/* In DWARF version 2, the description of the debugging information is
9018 stored in a separate .debug_abbrev section. Before we read any
9019 dies from a section we read in all abbreviations and install them
72bf9492
DJ
9020 in a hash table. This function also sets flags in CU describing
9021 the data found in the abbrev table. */
c906108c
SS
9022
9023static void
e7c27a73 9024dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 9025{
e7c27a73 9026 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 9027 gdb_byte *abbrev_ptr;
c906108c
SS
9028 struct abbrev_info *cur_abbrev;
9029 unsigned int abbrev_number, bytes_read, abbrev_name;
9030 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
9031 struct attr_abbrev *cur_attrs;
9032 unsigned int allocated_attrs;
c906108c 9033
0963b4bd 9034 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
9035 obstack_init (&cu->abbrev_obstack);
9036 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9037 (ABBREV_HASH_SIZE
9038 * sizeof (struct abbrev_info *)));
9039 memset (cu->dwarf2_abbrevs, 0,
9040 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 9041
be391dca
TT
9042 dwarf2_read_section (dwarf2_per_objfile->objfile,
9043 &dwarf2_per_objfile->abbrev);
dce234bc 9044 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
9045 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9046 abbrev_ptr += bytes_read;
9047
f3dd6933
DJ
9048 allocated_attrs = ATTR_ALLOC_CHUNK;
9049 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 9050
0963b4bd 9051 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
9052 while (abbrev_number)
9053 {
f3dd6933 9054 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
9055
9056 /* read in abbrev header */
9057 cur_abbrev->number = abbrev_number;
9058 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9059 abbrev_ptr += bytes_read;
9060 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9061 abbrev_ptr += 1;
9062
72bf9492
DJ
9063 if (cur_abbrev->tag == DW_TAG_namespace)
9064 cu->has_namespace_info = 1;
9065
c906108c
SS
9066 /* now read in declarations */
9067 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9068 abbrev_ptr += bytes_read;
9069 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9070 abbrev_ptr += bytes_read;
9071 while (abbrev_name)
9072 {
f3dd6933 9073 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 9074 {
f3dd6933
DJ
9075 allocated_attrs += ATTR_ALLOC_CHUNK;
9076 cur_attrs
9077 = xrealloc (cur_attrs, (allocated_attrs
9078 * sizeof (struct attr_abbrev)));
c906108c 9079 }
ae038cb0
DJ
9080
9081 /* Record whether this compilation unit might have
9082 inter-compilation-unit references. If we don't know what form
9083 this attribute will have, then it might potentially be a
9084 DW_FORM_ref_addr, so we conservatively expect inter-CU
9085 references. */
9086
9087 if (abbrev_form == DW_FORM_ref_addr
9088 || abbrev_form == DW_FORM_indirect)
9089 cu->has_form_ref_addr = 1;
9090
f3dd6933
DJ
9091 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9092 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
9093 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9094 abbrev_ptr += bytes_read;
9095 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9096 abbrev_ptr += bytes_read;
9097 }
9098
f3dd6933
DJ
9099 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9100 (cur_abbrev->num_attrs
9101 * sizeof (struct attr_abbrev)));
9102 memcpy (cur_abbrev->attrs, cur_attrs,
9103 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9104
c906108c 9105 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9106 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9107 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9108
9109 /* Get next abbreviation.
9110 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9111 always properly terminated with an abbrev number of 0.
9112 Exit loop if we encounter an abbreviation which we have
9113 already read (which means we are about to read the abbreviations
9114 for the next compile unit) or if the end of the abbreviation
9115 table is reached. */
dce234bc
PP
9116 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9117 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9118 break;
9119 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9120 abbrev_ptr += bytes_read;
e7c27a73 9121 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9122 break;
9123 }
f3dd6933
DJ
9124
9125 xfree (cur_attrs);
c906108c
SS
9126}
9127
f3dd6933 9128/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9129
c906108c 9130static void
f3dd6933 9131dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9132{
f3dd6933 9133 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9134
f3dd6933
DJ
9135 obstack_free (&cu->abbrev_obstack, NULL);
9136 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9137}
9138
9139/* Lookup an abbrev_info structure in the abbrev hash table. */
9140
9141static struct abbrev_info *
e7c27a73 9142dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9143{
9144 unsigned int hash_number;
9145 struct abbrev_info *abbrev;
9146
9147 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9148 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9149
9150 while (abbrev)
9151 {
9152 if (abbrev->number == number)
9153 return abbrev;
9154 else
9155 abbrev = abbrev->next;
9156 }
9157 return NULL;
9158}
9159
72bf9492
DJ
9160/* Returns nonzero if TAG represents a type that we might generate a partial
9161 symbol for. */
9162
9163static int
9164is_type_tag_for_partial (int tag)
9165{
9166 switch (tag)
9167 {
9168#if 0
9169 /* Some types that would be reasonable to generate partial symbols for,
9170 that we don't at present. */
9171 case DW_TAG_array_type:
9172 case DW_TAG_file_type:
9173 case DW_TAG_ptr_to_member_type:
9174 case DW_TAG_set_type:
9175 case DW_TAG_string_type:
9176 case DW_TAG_subroutine_type:
9177#endif
9178 case DW_TAG_base_type:
9179 case DW_TAG_class_type:
680b30c7 9180 case DW_TAG_interface_type:
72bf9492
DJ
9181 case DW_TAG_enumeration_type:
9182 case DW_TAG_structure_type:
9183 case DW_TAG_subrange_type:
9184 case DW_TAG_typedef:
9185 case DW_TAG_union_type:
9186 return 1;
9187 default:
9188 return 0;
9189 }
9190}
9191
9192/* Load all DIEs that are interesting for partial symbols into memory. */
9193
9194static struct partial_die_info *
93311388
DE
9195load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9196 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
9197{
9198 struct partial_die_info *part_die;
9199 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9200 struct abbrev_info *abbrev;
9201 unsigned int bytes_read;
5afb4e99 9202 unsigned int load_all = 0;
72bf9492
DJ
9203
9204 int nesting_level = 1;
9205
9206 parent_die = NULL;
9207 last_die = NULL;
9208
5afb4e99
DJ
9209 if (cu->per_cu && cu->per_cu->load_all_dies)
9210 load_all = 1;
9211
72bf9492
DJ
9212 cu->partial_dies
9213 = htab_create_alloc_ex (cu->header.length / 12,
9214 partial_die_hash,
9215 partial_die_eq,
9216 NULL,
9217 &cu->comp_unit_obstack,
9218 hashtab_obstack_allocate,
9219 dummy_obstack_deallocate);
9220
9221 part_die = obstack_alloc (&cu->comp_unit_obstack,
9222 sizeof (struct partial_die_info));
9223
9224 while (1)
9225 {
9226 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9227
9228 /* A NULL abbrev means the end of a series of children. */
9229 if (abbrev == NULL)
9230 {
9231 if (--nesting_level == 0)
9232 {
9233 /* PART_DIE was probably the last thing allocated on the
9234 comp_unit_obstack, so we could call obstack_free
9235 here. We don't do that because the waste is small,
9236 and will be cleaned up when we're done with this
9237 compilation unit. This way, we're also more robust
9238 against other users of the comp_unit_obstack. */
9239 return first_die;
9240 }
9241 info_ptr += bytes_read;
9242 last_die = parent_die;
9243 parent_die = parent_die->die_parent;
9244 continue;
9245 }
9246
98bfdba5
PA
9247 /* Check for template arguments. We never save these; if
9248 they're seen, we just mark the parent, and go on our way. */
9249 if (parent_die != NULL
9250 && cu->language == language_cplus
9251 && (abbrev->tag == DW_TAG_template_type_param
9252 || abbrev->tag == DW_TAG_template_value_param))
9253 {
9254 parent_die->has_template_arguments = 1;
9255
9256 if (!load_all)
9257 {
9258 /* We don't need a partial DIE for the template argument. */
9259 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9260 cu);
9261 continue;
9262 }
9263 }
9264
9265 /* We only recurse into subprograms looking for template arguments.
9266 Skip their other children. */
9267 if (!load_all
9268 && cu->language == language_cplus
9269 && parent_die != NULL
9270 && parent_die->tag == DW_TAG_subprogram)
9271 {
9272 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9273 continue;
9274 }
9275
5afb4e99
DJ
9276 /* Check whether this DIE is interesting enough to save. Normally
9277 we would not be interested in members here, but there may be
9278 later variables referencing them via DW_AT_specification (for
9279 static members). */
9280 if (!load_all
9281 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9282 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9283 && abbrev->tag != DW_TAG_enumerator
9284 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9285 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9286 && abbrev->tag != DW_TAG_variable
5afb4e99 9287 && abbrev->tag != DW_TAG_namespace
f55ee35c 9288 && abbrev->tag != DW_TAG_module
5afb4e99 9289 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9290 {
9291 /* Otherwise we skip to the next sibling, if any. */
93311388 9292 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9293 continue;
9294 }
9295
93311388
DE
9296 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9297 buffer, info_ptr, cu);
72bf9492
DJ
9298
9299 /* This two-pass algorithm for processing partial symbols has a
9300 high cost in cache pressure. Thus, handle some simple cases
9301 here which cover the majority of C partial symbols. DIEs
9302 which neither have specification tags in them, nor could have
9303 specification tags elsewhere pointing at them, can simply be
9304 processed and discarded.
9305
9306 This segment is also optional; scan_partial_symbols and
9307 add_partial_symbol will handle these DIEs if we chain
9308 them in normally. When compilers which do not emit large
9309 quantities of duplicate debug information are more common,
9310 this code can probably be removed. */
9311
9312 /* Any complete simple types at the top level (pretty much all
9313 of them, for a language without namespaces), can be processed
9314 directly. */
9315 if (parent_die == NULL
9316 && part_die->has_specification == 0
9317 && part_die->is_declaration == 0
d8228535 9318 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9319 || part_die->tag == DW_TAG_base_type
9320 || part_die->tag == DW_TAG_subrange_type))
9321 {
9322 if (building_psymtab && part_die->name != NULL)
04a679b8 9323 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
9324 VAR_DOMAIN, LOC_TYPEDEF,
9325 &cu->objfile->static_psymbols,
9326 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 9327 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9328 continue;
9329 }
9330
d8228535
JK
9331 /* The exception for DW_TAG_typedef with has_children above is
9332 a workaround of GCC PR debug/47510. In the case of this complaint
9333 type_name_no_tag_or_error will error on such types later.
9334
9335 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9336 it could not find the child DIEs referenced later, this is checked
9337 above. In correct DWARF DW_TAG_typedef should have no children. */
9338
9339 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9340 complaint (&symfile_complaints,
9341 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9342 "- DIE at 0x%x [in module %s]"),
9343 part_die->offset, cu->objfile->name);
9344
72bf9492
DJ
9345 /* If we're at the second level, and we're an enumerator, and
9346 our parent has no specification (meaning possibly lives in a
9347 namespace elsewhere), then we can add the partial symbol now
9348 instead of queueing it. */
9349 if (part_die->tag == DW_TAG_enumerator
9350 && parent_die != NULL
9351 && parent_die->die_parent == NULL
9352 && parent_die->tag == DW_TAG_enumeration_type
9353 && parent_die->has_specification == 0)
9354 {
9355 if (part_die->name == NULL)
3e43a32a
MS
9356 complaint (&symfile_complaints,
9357 _("malformed enumerator DIE ignored"));
72bf9492 9358 else if (building_psymtab)
04a679b8 9359 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9360 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9361 (cu->language == language_cplus
9362 || cu->language == language_java)
72bf9492
DJ
9363 ? &cu->objfile->global_psymbols
9364 : &cu->objfile->static_psymbols,
9365 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9366
93311388 9367 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9368 continue;
9369 }
9370
9371 /* We'll save this DIE so link it in. */
9372 part_die->die_parent = parent_die;
9373 part_die->die_sibling = NULL;
9374 part_die->die_child = NULL;
9375
9376 if (last_die && last_die == parent_die)
9377 last_die->die_child = part_die;
9378 else if (last_die)
9379 last_die->die_sibling = part_die;
9380
9381 last_die = part_die;
9382
9383 if (first_die == NULL)
9384 first_die = part_die;
9385
9386 /* Maybe add the DIE to the hash table. Not all DIEs that we
9387 find interesting need to be in the hash table, because we
9388 also have the parent/sibling/child chains; only those that we
9389 might refer to by offset later during partial symbol reading.
9390
9391 For now this means things that might have be the target of a
9392 DW_AT_specification, DW_AT_abstract_origin, or
9393 DW_AT_extension. DW_AT_extension will refer only to
9394 namespaces; DW_AT_abstract_origin refers to functions (and
9395 many things under the function DIE, but we do not recurse
9396 into function DIEs during partial symbol reading) and
9397 possibly variables as well; DW_AT_specification refers to
9398 declarations. Declarations ought to have the DW_AT_declaration
9399 flag. It happens that GCC forgets to put it in sometimes, but
9400 only for functions, not for types.
9401
9402 Adding more things than necessary to the hash table is harmless
9403 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9404 wasted time in find_partial_die, when we reread the compilation
9405 unit with load_all_dies set. */
72bf9492 9406
5afb4e99 9407 if (load_all
72929c62 9408 || abbrev->tag == DW_TAG_constant
5afb4e99 9409 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9410 || abbrev->tag == DW_TAG_variable
9411 || abbrev->tag == DW_TAG_namespace
9412 || part_die->is_declaration)
9413 {
9414 void **slot;
9415
9416 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9417 part_die->offset, INSERT);
9418 *slot = part_die;
9419 }
9420
9421 part_die = obstack_alloc (&cu->comp_unit_obstack,
9422 sizeof (struct partial_die_info));
9423
9424 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9425 we have no reason to follow the children of structures; for other
98bfdba5
PA
9426 languages we have to, so that we can get at method physnames
9427 to infer fully qualified class names, for DW_AT_specification,
9428 and for C++ template arguments. For C++, we also look one level
9429 inside functions to find template arguments (if the name of the
9430 function does not already contain the template arguments).
bc30ff58
JB
9431
9432 For Ada, we need to scan the children of subprograms and lexical
9433 blocks as well because Ada allows the definition of nested
9434 entities that could be interesting for the debugger, such as
9435 nested subprograms for instance. */
72bf9492 9436 if (last_die->has_children
5afb4e99
DJ
9437 && (load_all
9438 || last_die->tag == DW_TAG_namespace
f55ee35c 9439 || last_die->tag == DW_TAG_module
72bf9492 9440 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9441 || (cu->language == language_cplus
9442 && last_die->tag == DW_TAG_subprogram
9443 && (last_die->name == NULL
9444 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9445 || (cu->language != language_c
9446 && (last_die->tag == DW_TAG_class_type
680b30c7 9447 || last_die->tag == DW_TAG_interface_type
72bf9492 9448 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9449 || last_die->tag == DW_TAG_union_type))
9450 || (cu->language == language_ada
9451 && (last_die->tag == DW_TAG_subprogram
9452 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9453 {
9454 nesting_level++;
9455 parent_die = last_die;
9456 continue;
9457 }
9458
9459 /* Otherwise we skip to the next sibling, if any. */
93311388 9460 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9461
9462 /* Back to the top, do it again. */
9463 }
9464}
9465
c906108c
SS
9466/* Read a minimal amount of information into the minimal die structure. */
9467
fe1b8b76 9468static gdb_byte *
72bf9492
DJ
9469read_partial_die (struct partial_die_info *part_die,
9470 struct abbrev_info *abbrev,
9471 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9472 gdb_byte *buffer, gdb_byte *info_ptr,
9473 struct dwarf2_cu *cu)
c906108c 9474{
fa238c03 9475 unsigned int i;
c906108c 9476 struct attribute attr;
c5aa993b 9477 int has_low_pc_attr = 0;
c906108c
SS
9478 int has_high_pc_attr = 0;
9479
72bf9492 9480 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9481
93311388 9482 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9483
9484 info_ptr += abbrev_len;
9485
9486 if (abbrev == NULL)
9487 return info_ptr;
9488
c906108c
SS
9489 part_die->tag = abbrev->tag;
9490 part_die->has_children = abbrev->has_children;
c906108c
SS
9491
9492 for (i = 0; i < abbrev->num_attrs; ++i)
9493 {
e7c27a73 9494 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9495
9496 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9497 partial symbol table. */
c906108c
SS
9498 switch (attr.name)
9499 {
9500 case DW_AT_name:
71c25dea
TT
9501 switch (part_die->tag)
9502 {
9503 case DW_TAG_compile_unit:
348e048f 9504 case DW_TAG_type_unit:
71c25dea
TT
9505 /* Compilation units have a DW_AT_name that is a filename, not
9506 a source language identifier. */
9507 case DW_TAG_enumeration_type:
9508 case DW_TAG_enumerator:
9509 /* These tags always have simple identifiers already; no need
9510 to canonicalize them. */
9511 part_die->name = DW_STRING (&attr);
9512 break;
9513 default:
9514 part_die->name
9515 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9516 &cu->objfile->objfile_obstack);
71c25dea
TT
9517 break;
9518 }
c906108c 9519 break;
31ef98ae 9520 case DW_AT_linkage_name:
c906108c 9521 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9522 /* Note that both forms of linkage name might appear. We
9523 assume they will be the same, and we only store the last
9524 one we see. */
94af9270
KS
9525 if (cu->language == language_ada)
9526 part_die->name = DW_STRING (&attr);
abc72ce4 9527 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9528 break;
9529 case DW_AT_low_pc:
9530 has_low_pc_attr = 1;
9531 part_die->lowpc = DW_ADDR (&attr);
9532 break;
9533 case DW_AT_high_pc:
9534 has_high_pc_attr = 1;
9535 part_die->highpc = DW_ADDR (&attr);
9536 break;
9537 case DW_AT_location:
0963b4bd 9538 /* Support the .debug_loc offsets. */
8e19ed76
PS
9539 if (attr_form_is_block (&attr))
9540 {
9541 part_die->locdesc = DW_BLOCK (&attr);
9542 }
3690dd37 9543 else if (attr_form_is_section_offset (&attr))
8e19ed76 9544 {
4d3c2250 9545 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9546 }
9547 else
9548 {
4d3c2250
KB
9549 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9550 "partial symbol information");
8e19ed76 9551 }
c906108c 9552 break;
c906108c
SS
9553 case DW_AT_external:
9554 part_die->is_external = DW_UNSND (&attr);
9555 break;
9556 case DW_AT_declaration:
9557 part_die->is_declaration = DW_UNSND (&attr);
9558 break;
9559 case DW_AT_type:
9560 part_die->has_type = 1;
9561 break;
9562 case DW_AT_abstract_origin:
9563 case DW_AT_specification:
72bf9492
DJ
9564 case DW_AT_extension:
9565 part_die->has_specification = 1;
c764a876 9566 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9567 break;
9568 case DW_AT_sibling:
9569 /* Ignore absolute siblings, they might point outside of
9570 the current compile unit. */
9571 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9572 complaint (&symfile_complaints,
9573 _("ignoring absolute DW_AT_sibling"));
c906108c 9574 else
93311388 9575 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9576 break;
fa4028e9
JB
9577 case DW_AT_byte_size:
9578 part_die->has_byte_size = 1;
9579 break;
68511cec
CES
9580 case DW_AT_calling_convention:
9581 /* DWARF doesn't provide a way to identify a program's source-level
9582 entry point. DW_AT_calling_convention attributes are only meant
9583 to describe functions' calling conventions.
9584
9585 However, because it's a necessary piece of information in
9586 Fortran, and because DW_CC_program is the only piece of debugging
9587 information whose definition refers to a 'main program' at all,
9588 several compilers have begun marking Fortran main programs with
9589 DW_CC_program --- even when those functions use the standard
9590 calling conventions.
9591
9592 So until DWARF specifies a way to provide this information and
9593 compilers pick up the new representation, we'll support this
9594 practice. */
9595 if (DW_UNSND (&attr) == DW_CC_program
9596 && cu->language == language_fortran)
01f8c46d
JK
9597 {
9598 set_main_name (part_die->name);
9599
9600 /* As this DIE has a static linkage the name would be difficult
9601 to look up later. */
9602 language_of_main = language_fortran;
9603 }
68511cec 9604 break;
c906108c
SS
9605 default:
9606 break;
9607 }
9608 }
9609
9373cf26
JK
9610 if (has_low_pc_attr && has_high_pc_attr)
9611 {
9612 /* When using the GNU linker, .gnu.linkonce. sections are used to
9613 eliminate duplicate copies of functions and vtables and such.
9614 The linker will arbitrarily choose one and discard the others.
9615 The AT_*_pc values for such functions refer to local labels in
9616 these sections. If the section from that file was discarded, the
9617 labels are not in the output, so the relocs get a value of 0.
9618 If this is a discarded function, mark the pc bounds as invalid,
9619 so that GDB will ignore it. */
9620 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9621 {
9622 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9623
9624 complaint (&symfile_complaints,
9625 _("DW_AT_low_pc %s is zero "
9626 "for DIE at 0x%x [in module %s]"),
9627 paddress (gdbarch, part_die->lowpc),
9628 part_die->offset, cu->objfile->name);
9629 }
9630 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9631 else if (part_die->lowpc >= part_die->highpc)
9632 {
9633 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9634
9635 complaint (&symfile_complaints,
9636 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9637 "for DIE at 0x%x [in module %s]"),
9638 paddress (gdbarch, part_die->lowpc),
9639 paddress (gdbarch, part_die->highpc),
9640 part_die->offset, cu->objfile->name);
9641 }
9642 else
9643 part_die->has_pc_info = 1;
9644 }
85cbf3d3 9645
c906108c
SS
9646 return info_ptr;
9647}
9648
72bf9492
DJ
9649/* Find a cached partial DIE at OFFSET in CU. */
9650
9651static struct partial_die_info *
c764a876 9652find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9653{
9654 struct partial_die_info *lookup_die = NULL;
9655 struct partial_die_info part_die;
9656
9657 part_die.offset = offset;
9658 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9659
72bf9492
DJ
9660 return lookup_die;
9661}
9662
348e048f
DE
9663/* Find a partial DIE at OFFSET, which may or may not be in CU,
9664 except in the case of .debug_types DIEs which do not reference
9665 outside their CU (they do however referencing other types via
55f1336d 9666 DW_FORM_ref_sig8). */
72bf9492
DJ
9667
9668static struct partial_die_info *
c764a876 9669find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9670{
5afb4e99
DJ
9671 struct dwarf2_per_cu_data *per_cu = NULL;
9672 struct partial_die_info *pd = NULL;
72bf9492 9673
8b70b953 9674 if (cu->per_cu->debug_type_section)
348e048f
DE
9675 {
9676 pd = find_partial_die_in_comp_unit (offset, cu);
9677 if (pd != NULL)
9678 return pd;
9679 goto not_found;
9680 }
9681
45452591 9682 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9683 {
9684 pd = find_partial_die_in_comp_unit (offset, cu);
9685 if (pd != NULL)
9686 return pd;
9687 }
72bf9492 9688
ae038cb0
DJ
9689 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9690
98bfdba5
PA
9691 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9692 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9693
9694 per_cu->cu->last_used = 0;
5afb4e99
DJ
9695 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9696
9697 if (pd == NULL && per_cu->load_all_dies == 0)
9698 {
9699 struct cleanup *back_to;
9700 struct partial_die_info comp_unit_die;
9701 struct abbrev_info *abbrev;
9702 unsigned int bytes_read;
9703 char *info_ptr;
9704
9705 per_cu->load_all_dies = 1;
9706
9707 /* Re-read the DIEs. */
9708 back_to = make_cleanup (null_cleanup, 0);
9709 if (per_cu->cu->dwarf2_abbrevs == NULL)
9710 {
9711 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9712 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9713 }
dce234bc 9714 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9715 + per_cu->cu->header.offset
9716 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9717 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9718 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9719 per_cu->cu->objfile->obfd,
9720 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9721 per_cu->cu);
9722 if (comp_unit_die.has_children)
93311388
DE
9723 load_partial_dies (per_cu->cu->objfile->obfd,
9724 dwarf2_per_objfile->info.buffer, info_ptr,
9725 0, per_cu->cu);
5afb4e99
DJ
9726 do_cleanups (back_to);
9727
9728 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9729 }
9730
348e048f
DE
9731 not_found:
9732
5afb4e99
DJ
9733 if (pd == NULL)
9734 internal_error (__FILE__, __LINE__,
3e43a32a
MS
9735 _("could not find partial DIE 0x%x "
9736 "in cache [from module %s]\n"),
5afb4e99
DJ
9737 offset, bfd_get_filename (cu->objfile->obfd));
9738 return pd;
72bf9492
DJ
9739}
9740
abc72ce4
DE
9741/* See if we can figure out if the class lives in a namespace. We do
9742 this by looking for a member function; its demangled name will
9743 contain namespace info, if there is any. */
9744
9745static void
9746guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9747 struct dwarf2_cu *cu)
9748{
9749 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9750 what template types look like, because the demangler
9751 frequently doesn't give the same name as the debug info. We
9752 could fix this by only using the demangled name to get the
9753 prefix (but see comment in read_structure_type). */
9754
9755 struct partial_die_info *real_pdi;
9756 struct partial_die_info *child_pdi;
9757
9758 /* If this DIE (this DIE's specification, if any) has a parent, then
9759 we should not do this. We'll prepend the parent's fully qualified
9760 name when we create the partial symbol. */
9761
9762 real_pdi = struct_pdi;
9763 while (real_pdi->has_specification)
9764 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9765
9766 if (real_pdi->die_parent != NULL)
9767 return;
9768
9769 for (child_pdi = struct_pdi->die_child;
9770 child_pdi != NULL;
9771 child_pdi = child_pdi->die_sibling)
9772 {
9773 if (child_pdi->tag == DW_TAG_subprogram
9774 && child_pdi->linkage_name != NULL)
9775 {
9776 char *actual_class_name
9777 = language_class_name_from_physname (cu->language_defn,
9778 child_pdi->linkage_name);
9779 if (actual_class_name != NULL)
9780 {
9781 struct_pdi->name
9782 = obsavestring (actual_class_name,
9783 strlen (actual_class_name),
9784 &cu->objfile->objfile_obstack);
9785 xfree (actual_class_name);
9786 }
9787 break;
9788 }
9789 }
9790}
9791
72bf9492
DJ
9792/* Adjust PART_DIE before generating a symbol for it. This function
9793 may set the is_external flag or change the DIE's name. */
9794
9795static void
9796fixup_partial_die (struct partial_die_info *part_die,
9797 struct dwarf2_cu *cu)
9798{
abc72ce4
DE
9799 /* Once we've fixed up a die, there's no point in doing so again.
9800 This also avoids a memory leak if we were to call
9801 guess_partial_die_structure_name multiple times. */
9802 if (part_die->fixup_called)
9803 return;
9804
72bf9492
DJ
9805 /* If we found a reference attribute and the DIE has no name, try
9806 to find a name in the referred to DIE. */
9807
9808 if (part_die->name == NULL && part_die->has_specification)
9809 {
9810 struct partial_die_info *spec_die;
72bf9492 9811
10b3939b 9812 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9813
10b3939b 9814 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9815
9816 if (spec_die->name)
9817 {
9818 part_die->name = spec_die->name;
9819
9820 /* Copy DW_AT_external attribute if it is set. */
9821 if (spec_die->is_external)
9822 part_die->is_external = spec_die->is_external;
9823 }
9824 }
9825
9826 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9827
9828 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 9829 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 9830
abc72ce4
DE
9831 /* If there is no parent die to provide a namespace, and there are
9832 children, see if we can determine the namespace from their linkage
9833 name.
9834 NOTE: We need to do this even if cu->has_namespace_info != 0.
9835 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9836 if (cu->language == language_cplus
8b70b953 9837 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
9838 && part_die->die_parent == NULL
9839 && part_die->has_children
9840 && (part_die->tag == DW_TAG_class_type
9841 || part_die->tag == DW_TAG_structure_type
9842 || part_die->tag == DW_TAG_union_type))
9843 guess_partial_die_structure_name (part_die, cu);
9844
53832f31
TT
9845 /* GCC might emit a nameless struct or union that has a linkage
9846 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
9847 if (part_die->name == NULL
9848 && (part_die->tag == DW_TAG_structure_type
9849 || part_die->tag == DW_TAG_union_type
9850 || part_die->tag == DW_TAG_class_type)
9851 && part_die->linkage_name != NULL)
9852 {
9853 char *demangled;
9854
9855 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9856 if (demangled)
9857 {
9858 part_die->name = obsavestring (demangled, strlen (demangled),
9859 &cu->objfile->objfile_obstack);
9860 xfree (demangled);
9861 }
9862 }
9863
abc72ce4 9864 part_die->fixup_called = 1;
72bf9492
DJ
9865}
9866
a8329558 9867/* Read an attribute value described by an attribute form. */
c906108c 9868
fe1b8b76 9869static gdb_byte *
a8329558 9870read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9871 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9872 struct dwarf2_cu *cu)
c906108c 9873{
e7c27a73 9874 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9875 unsigned int bytes_read;
9876 struct dwarf_block *blk;
9877
a8329558
KW
9878 attr->form = form;
9879 switch (form)
c906108c 9880 {
c906108c 9881 case DW_FORM_ref_addr:
ae411497
TT
9882 if (cu->header.version == 2)
9883 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9884 else
3e43a32a
MS
9885 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9886 &cu->header, &bytes_read);
ae411497
TT
9887 info_ptr += bytes_read;
9888 break;
9889 case DW_FORM_addr:
e7c27a73 9890 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9891 info_ptr += bytes_read;
c906108c
SS
9892 break;
9893 case DW_FORM_block2:
7b5a2f43 9894 blk = dwarf_alloc_block (cu);
c906108c
SS
9895 blk->size = read_2_bytes (abfd, info_ptr);
9896 info_ptr += 2;
9897 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9898 info_ptr += blk->size;
9899 DW_BLOCK (attr) = blk;
9900 break;
9901 case DW_FORM_block4:
7b5a2f43 9902 blk = dwarf_alloc_block (cu);
c906108c
SS
9903 blk->size = read_4_bytes (abfd, info_ptr);
9904 info_ptr += 4;
9905 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9906 info_ptr += blk->size;
9907 DW_BLOCK (attr) = blk;
9908 break;
9909 case DW_FORM_data2:
9910 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9911 info_ptr += 2;
9912 break;
9913 case DW_FORM_data4:
9914 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9915 info_ptr += 4;
9916 break;
9917 case DW_FORM_data8:
9918 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9919 info_ptr += 8;
9920 break;
2dc7f7b3
TT
9921 case DW_FORM_sec_offset:
9922 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9923 info_ptr += bytes_read;
9924 break;
c906108c 9925 case DW_FORM_string:
9b1c24c8 9926 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9927 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9928 info_ptr += bytes_read;
9929 break;
4bdf3d34
JJ
9930 case DW_FORM_strp:
9931 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9932 &bytes_read);
8285870a 9933 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9934 info_ptr += bytes_read;
9935 break;
2dc7f7b3 9936 case DW_FORM_exprloc:
c906108c 9937 case DW_FORM_block:
7b5a2f43 9938 blk = dwarf_alloc_block (cu);
c906108c
SS
9939 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9940 info_ptr += bytes_read;
9941 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9942 info_ptr += blk->size;
9943 DW_BLOCK (attr) = blk;
9944 break;
9945 case DW_FORM_block1:
7b5a2f43 9946 blk = dwarf_alloc_block (cu);
c906108c
SS
9947 blk->size = read_1_byte (abfd, info_ptr);
9948 info_ptr += 1;
9949 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9950 info_ptr += blk->size;
9951 DW_BLOCK (attr) = blk;
9952 break;
9953 case DW_FORM_data1:
9954 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9955 info_ptr += 1;
9956 break;
9957 case DW_FORM_flag:
9958 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9959 info_ptr += 1;
9960 break;
2dc7f7b3
TT
9961 case DW_FORM_flag_present:
9962 DW_UNSND (attr) = 1;
9963 break;
c906108c
SS
9964 case DW_FORM_sdata:
9965 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9966 info_ptr += bytes_read;
9967 break;
9968 case DW_FORM_udata:
9969 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9970 info_ptr += bytes_read;
9971 break;
9972 case DW_FORM_ref1:
10b3939b 9973 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9974 info_ptr += 1;
9975 break;
9976 case DW_FORM_ref2:
10b3939b 9977 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9978 info_ptr += 2;
9979 break;
9980 case DW_FORM_ref4:
10b3939b 9981 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9982 info_ptr += 4;
9983 break;
613e1657 9984 case DW_FORM_ref8:
10b3939b 9985 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9986 info_ptr += 8;
9987 break;
55f1336d 9988 case DW_FORM_ref_sig8:
348e048f
DE
9989 /* Convert the signature to something we can record in DW_UNSND
9990 for later lookup.
9991 NOTE: This is NULL if the type wasn't found. */
9992 DW_SIGNATURED_TYPE (attr) =
9993 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9994 info_ptr += 8;
9995 break;
c906108c 9996 case DW_FORM_ref_udata:
10b3939b
DJ
9997 DW_ADDR (attr) = (cu->header.offset
9998 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9999 info_ptr += bytes_read;
10000 break;
c906108c 10001 case DW_FORM_indirect:
a8329558
KW
10002 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10003 info_ptr += bytes_read;
e7c27a73 10004 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 10005 break;
c906108c 10006 default:
8a3fe4f8 10007 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
10008 dwarf_form_name (form),
10009 bfd_get_filename (abfd));
c906108c 10010 }
28e94949
JB
10011
10012 /* We have seen instances where the compiler tried to emit a byte
10013 size attribute of -1 which ended up being encoded as an unsigned
10014 0xffffffff. Although 0xffffffff is technically a valid size value,
10015 an object of this size seems pretty unlikely so we can relatively
10016 safely treat these cases as if the size attribute was invalid and
10017 treat them as zero by default. */
10018 if (attr->name == DW_AT_byte_size
10019 && form == DW_FORM_data4
10020 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
10021 {
10022 complaint
10023 (&symfile_complaints,
43bbcdc2
PH
10024 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10025 hex_string (DW_UNSND (attr)));
01c66ae6
JB
10026 DW_UNSND (attr) = 0;
10027 }
28e94949 10028
c906108c
SS
10029 return info_ptr;
10030}
10031
a8329558
KW
10032/* Read an attribute described by an abbreviated attribute. */
10033
fe1b8b76 10034static gdb_byte *
a8329558 10035read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 10036 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
10037{
10038 attr->name = abbrev->name;
e7c27a73 10039 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
10040}
10041
0963b4bd 10042/* Read dwarf information from a buffer. */
c906108c
SS
10043
10044static unsigned int
fe1b8b76 10045read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 10046{
fe1b8b76 10047 return bfd_get_8 (abfd, buf);
c906108c
SS
10048}
10049
10050static int
fe1b8b76 10051read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 10052{
fe1b8b76 10053 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
10054}
10055
10056static unsigned int
fe1b8b76 10057read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10058{
fe1b8b76 10059 return bfd_get_16 (abfd, buf);
c906108c
SS
10060}
10061
10062static int
fe1b8b76 10063read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10064{
fe1b8b76 10065 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
10066}
10067
10068static unsigned int
fe1b8b76 10069read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10070{
fe1b8b76 10071 return bfd_get_32 (abfd, buf);
c906108c
SS
10072}
10073
10074static int
fe1b8b76 10075read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10076{
fe1b8b76 10077 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
10078}
10079
93311388 10080static ULONGEST
fe1b8b76 10081read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10082{
fe1b8b76 10083 return bfd_get_64 (abfd, buf);
c906108c
SS
10084}
10085
10086static CORE_ADDR
fe1b8b76 10087read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 10088 unsigned int *bytes_read)
c906108c 10089{
e7c27a73 10090 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10091 CORE_ADDR retval = 0;
10092
107d2387 10093 if (cu_header->signed_addr_p)
c906108c 10094 {
107d2387
AC
10095 switch (cu_header->addr_size)
10096 {
10097 case 2:
fe1b8b76 10098 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
10099 break;
10100 case 4:
fe1b8b76 10101 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
10102 break;
10103 case 8:
fe1b8b76 10104 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
10105 break;
10106 default:
8e65ff28 10107 internal_error (__FILE__, __LINE__,
e2e0b3e5 10108 _("read_address: bad switch, signed [in module %s]"),
659b0389 10109 bfd_get_filename (abfd));
107d2387
AC
10110 }
10111 }
10112 else
10113 {
10114 switch (cu_header->addr_size)
10115 {
10116 case 2:
fe1b8b76 10117 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10118 break;
10119 case 4:
fe1b8b76 10120 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10121 break;
10122 case 8:
fe1b8b76 10123 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10124 break;
10125 default:
8e65ff28 10126 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10127 _("read_address: bad switch, "
10128 "unsigned [in module %s]"),
659b0389 10129 bfd_get_filename (abfd));
107d2387 10130 }
c906108c 10131 }
64367e0a 10132
107d2387
AC
10133 *bytes_read = cu_header->addr_size;
10134 return retval;
c906108c
SS
10135}
10136
f7ef9339 10137/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10138 specification allows the initial length to take up either 4 bytes
10139 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10140 bytes describe the length and all offsets will be 8 bytes in length
10141 instead of 4.
10142
f7ef9339
KB
10143 An older, non-standard 64-bit format is also handled by this
10144 function. The older format in question stores the initial length
10145 as an 8-byte quantity without an escape value. Lengths greater
10146 than 2^32 aren't very common which means that the initial 4 bytes
10147 is almost always zero. Since a length value of zero doesn't make
10148 sense for the 32-bit format, this initial zero can be considered to
10149 be an escape value which indicates the presence of the older 64-bit
10150 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10151 greater than 4GB. If it becomes necessary to handle lengths
10152 somewhat larger than 4GB, we could allow other small values (such
10153 as the non-sensical values of 1, 2, and 3) to also be used as
10154 escape values indicating the presence of the old format.
f7ef9339 10155
917c78fc
MK
10156 The value returned via bytes_read should be used to increment the
10157 relevant pointer after calling read_initial_length().
c764a876 10158
613e1657
KB
10159 [ Note: read_initial_length() and read_offset() are based on the
10160 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10161 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10162 from:
10163
f7ef9339 10164 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10165
613e1657
KB
10166 This document is only a draft and is subject to change. (So beware.)
10167
f7ef9339 10168 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10169 determined empirically by examining 64-bit ELF files produced by
10170 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10171
10172 - Kevin, July 16, 2002
613e1657
KB
10173 ] */
10174
10175static LONGEST
c764a876 10176read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10177{
fe1b8b76 10178 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10179
dd373385 10180 if (length == 0xffffffff)
613e1657 10181 {
fe1b8b76 10182 length = bfd_get_64 (abfd, buf + 4);
613e1657 10183 *bytes_read = 12;
613e1657 10184 }
dd373385 10185 else if (length == 0)
f7ef9339 10186 {
dd373385 10187 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10188 length = bfd_get_64 (abfd, buf);
f7ef9339 10189 *bytes_read = 8;
f7ef9339 10190 }
613e1657
KB
10191 else
10192 {
10193 *bytes_read = 4;
613e1657
KB
10194 }
10195
c764a876
DE
10196 return length;
10197}
dd373385 10198
c764a876
DE
10199/* Cover function for read_initial_length.
10200 Returns the length of the object at BUF, and stores the size of the
10201 initial length in *BYTES_READ and stores the size that offsets will be in
10202 *OFFSET_SIZE.
10203 If the initial length size is not equivalent to that specified in
10204 CU_HEADER then issue a complaint.
10205 This is useful when reading non-comp-unit headers. */
dd373385 10206
c764a876
DE
10207static LONGEST
10208read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10209 const struct comp_unit_head *cu_header,
10210 unsigned int *bytes_read,
10211 unsigned int *offset_size)
10212{
10213 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10214
10215 gdb_assert (cu_header->initial_length_size == 4
10216 || cu_header->initial_length_size == 8
10217 || cu_header->initial_length_size == 12);
10218
10219 if (cu_header->initial_length_size != *bytes_read)
10220 complaint (&symfile_complaints,
10221 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10222
c764a876 10223 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10224 return length;
613e1657
KB
10225}
10226
10227/* Read an offset from the data stream. The size of the offset is
917c78fc 10228 given by cu_header->offset_size. */
613e1657
KB
10229
10230static LONGEST
fe1b8b76 10231read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10232 unsigned int *bytes_read)
c764a876
DE
10233{
10234 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10235
c764a876
DE
10236 *bytes_read = cu_header->offset_size;
10237 return offset;
10238}
10239
10240/* Read an offset from the data stream. */
10241
10242static LONGEST
10243read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10244{
10245 LONGEST retval = 0;
10246
c764a876 10247 switch (offset_size)
613e1657
KB
10248 {
10249 case 4:
fe1b8b76 10250 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10251 break;
10252 case 8:
fe1b8b76 10253 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10254 break;
10255 default:
8e65ff28 10256 internal_error (__FILE__, __LINE__,
c764a876 10257 _("read_offset_1: bad switch [in module %s]"),
659b0389 10258 bfd_get_filename (abfd));
613e1657
KB
10259 }
10260
917c78fc 10261 return retval;
613e1657
KB
10262}
10263
fe1b8b76
JB
10264static gdb_byte *
10265read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10266{
10267 /* If the size of a host char is 8 bits, we can return a pointer
10268 to the buffer, otherwise we have to copy the data to a buffer
10269 allocated on the temporary obstack. */
4bdf3d34 10270 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10271 return buf;
c906108c
SS
10272}
10273
10274static char *
9b1c24c8 10275read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10276{
10277 /* If the size of a host char is 8 bits, we can return a pointer
10278 to the string, otherwise we have to copy the string to a buffer
10279 allocated on the temporary obstack. */
4bdf3d34 10280 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10281 if (*buf == '\0')
10282 {
10283 *bytes_read_ptr = 1;
10284 return NULL;
10285 }
fe1b8b76
JB
10286 *bytes_read_ptr = strlen ((char *) buf) + 1;
10287 return (char *) buf;
4bdf3d34
JJ
10288}
10289
10290static char *
cf2c3c16 10291read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 10292{
be391dca 10293 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10294 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
10295 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10296 bfd_get_filename (abfd));
dce234bc 10297 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
10298 error (_("DW_FORM_strp pointing outside of "
10299 ".debug_str section [in module %s]"),
10300 bfd_get_filename (abfd));
4bdf3d34 10301 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10302 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10303 return NULL;
dce234bc 10304 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10305}
10306
cf2c3c16
TT
10307static char *
10308read_indirect_string (bfd *abfd, gdb_byte *buf,
10309 const struct comp_unit_head *cu_header,
10310 unsigned int *bytes_read_ptr)
10311{
10312 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10313
10314 return read_indirect_string_at_offset (abfd, str_offset);
10315}
10316
ce5d95e1 10317static unsigned long
fe1b8b76 10318read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10319{
ce5d95e1
JB
10320 unsigned long result;
10321 unsigned int num_read;
c906108c
SS
10322 int i, shift;
10323 unsigned char byte;
10324
10325 result = 0;
10326 shift = 0;
10327 num_read = 0;
10328 i = 0;
10329 while (1)
10330 {
fe1b8b76 10331 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10332 buf++;
10333 num_read++;
ce5d95e1 10334 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10335 if ((byte & 128) == 0)
10336 {
10337 break;
10338 }
10339 shift += 7;
10340 }
10341 *bytes_read_ptr = num_read;
10342 return result;
10343}
10344
ce5d95e1 10345static long
fe1b8b76 10346read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10347{
ce5d95e1 10348 long result;
77e0b926 10349 int i, shift, num_read;
c906108c
SS
10350 unsigned char byte;
10351
10352 result = 0;
10353 shift = 0;
c906108c
SS
10354 num_read = 0;
10355 i = 0;
10356 while (1)
10357 {
fe1b8b76 10358 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10359 buf++;
10360 num_read++;
ce5d95e1 10361 result |= ((long)(byte & 127) << shift);
c906108c
SS
10362 shift += 7;
10363 if ((byte & 128) == 0)
10364 {
10365 break;
10366 }
10367 }
77e0b926
DJ
10368 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10369 result |= -(((long)1) << shift);
c906108c
SS
10370 *bytes_read_ptr = num_read;
10371 return result;
10372}
10373
4bb7a0a7
DJ
10374/* Return a pointer to just past the end of an LEB128 number in BUF. */
10375
fe1b8b76
JB
10376static gdb_byte *
10377skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10378{
10379 int byte;
10380
10381 while (1)
10382 {
fe1b8b76 10383 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10384 buf++;
10385 if ((byte & 128) == 0)
10386 return buf;
10387 }
10388}
10389
c906108c 10390static void
e142c38c 10391set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10392{
10393 switch (lang)
10394 {
10395 case DW_LANG_C89:
76bee0cc 10396 case DW_LANG_C99:
c906108c 10397 case DW_LANG_C:
e142c38c 10398 cu->language = language_c;
c906108c
SS
10399 break;
10400 case DW_LANG_C_plus_plus:
e142c38c 10401 cu->language = language_cplus;
c906108c 10402 break;
6aecb9c2
JB
10403 case DW_LANG_D:
10404 cu->language = language_d;
10405 break;
c906108c
SS
10406 case DW_LANG_Fortran77:
10407 case DW_LANG_Fortran90:
b21b22e0 10408 case DW_LANG_Fortran95:
e142c38c 10409 cu->language = language_fortran;
c906108c
SS
10410 break;
10411 case DW_LANG_Mips_Assembler:
e142c38c 10412 cu->language = language_asm;
c906108c 10413 break;
bebd888e 10414 case DW_LANG_Java:
e142c38c 10415 cu->language = language_java;
bebd888e 10416 break;
c906108c 10417 case DW_LANG_Ada83:
8aaf0b47 10418 case DW_LANG_Ada95:
bc5f45f8
JB
10419 cu->language = language_ada;
10420 break;
72019c9c
GM
10421 case DW_LANG_Modula2:
10422 cu->language = language_m2;
10423 break;
fe8e67fd
PM
10424 case DW_LANG_Pascal83:
10425 cu->language = language_pascal;
10426 break;
22566fbd
DJ
10427 case DW_LANG_ObjC:
10428 cu->language = language_objc;
10429 break;
c906108c
SS
10430 case DW_LANG_Cobol74:
10431 case DW_LANG_Cobol85:
c906108c 10432 default:
e142c38c 10433 cu->language = language_minimal;
c906108c
SS
10434 break;
10435 }
e142c38c 10436 cu->language_defn = language_def (cu->language);
c906108c
SS
10437}
10438
10439/* Return the named attribute or NULL if not there. */
10440
10441static struct attribute *
e142c38c 10442dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10443{
10444 unsigned int i;
10445 struct attribute *spec = NULL;
10446
10447 for (i = 0; i < die->num_attrs; ++i)
10448 {
10449 if (die->attrs[i].name == name)
10b3939b 10450 return &die->attrs[i];
c906108c
SS
10451 if (die->attrs[i].name == DW_AT_specification
10452 || die->attrs[i].name == DW_AT_abstract_origin)
10453 spec = &die->attrs[i];
10454 }
c906108c 10455
10b3939b 10456 if (spec)
f2f0e013
DJ
10457 {
10458 die = follow_die_ref (die, spec, &cu);
10459 return dwarf2_attr (die, name, cu);
10460 }
c5aa993b 10461
c906108c
SS
10462 return NULL;
10463}
10464
348e048f
DE
10465/* Return the named attribute or NULL if not there,
10466 but do not follow DW_AT_specification, etc.
10467 This is for use in contexts where we're reading .debug_types dies.
10468 Following DW_AT_specification, DW_AT_abstract_origin will take us
10469 back up the chain, and we want to go down. */
10470
10471static struct attribute *
10472dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10473 struct dwarf2_cu *cu)
10474{
10475 unsigned int i;
10476
10477 for (i = 0; i < die->num_attrs; ++i)
10478 if (die->attrs[i].name == name)
10479 return &die->attrs[i];
10480
10481 return NULL;
10482}
10483
05cf31d1
JB
10484/* Return non-zero iff the attribute NAME is defined for the given DIE,
10485 and holds a non-zero value. This function should only be used for
2dc7f7b3 10486 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10487
10488static int
10489dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10490{
10491 struct attribute *attr = dwarf2_attr (die, name, cu);
10492
10493 return (attr && DW_UNSND (attr));
10494}
10495
3ca72b44 10496static int
e142c38c 10497die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10498{
05cf31d1
JB
10499 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10500 which value is non-zero. However, we have to be careful with
10501 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10502 (via dwarf2_flag_true_p) follows this attribute. So we may
10503 end up accidently finding a declaration attribute that belongs
10504 to a different DIE referenced by the specification attribute,
10505 even though the given DIE does not have a declaration attribute. */
10506 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10507 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10508}
10509
63d06c5c 10510/* Return the die giving the specification for DIE, if there is
f2f0e013 10511 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10512 containing the return value on output. If there is no
10513 specification, but there is an abstract origin, that is
10514 returned. */
63d06c5c
DC
10515
10516static struct die_info *
f2f0e013 10517die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10518{
f2f0e013
DJ
10519 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10520 *spec_cu);
63d06c5c 10521
edb3359d
DJ
10522 if (spec_attr == NULL)
10523 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10524
63d06c5c
DC
10525 if (spec_attr == NULL)
10526 return NULL;
10527 else
f2f0e013 10528 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10529}
c906108c 10530
debd256d 10531/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10532 refers to.
10533 NOTE: This is also used as a "cleanup" function. */
10534
debd256d
JB
10535static void
10536free_line_header (struct line_header *lh)
10537{
10538 if (lh->standard_opcode_lengths)
a8bc7b56 10539 xfree (lh->standard_opcode_lengths);
debd256d
JB
10540
10541 /* Remember that all the lh->file_names[i].name pointers are
10542 pointers into debug_line_buffer, and don't need to be freed. */
10543 if (lh->file_names)
a8bc7b56 10544 xfree (lh->file_names);
debd256d
JB
10545
10546 /* Similarly for the include directory names. */
10547 if (lh->include_dirs)
a8bc7b56 10548 xfree (lh->include_dirs);
debd256d 10549
a8bc7b56 10550 xfree (lh);
debd256d
JB
10551}
10552
debd256d 10553/* Add an entry to LH's include directory table. */
ae2de4f8 10554
debd256d
JB
10555static void
10556add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10557{
debd256d
JB
10558 /* Grow the array if necessary. */
10559 if (lh->include_dirs_size == 0)
c5aa993b 10560 {
debd256d
JB
10561 lh->include_dirs_size = 1; /* for testing */
10562 lh->include_dirs = xmalloc (lh->include_dirs_size
10563 * sizeof (*lh->include_dirs));
10564 }
10565 else if (lh->num_include_dirs >= lh->include_dirs_size)
10566 {
10567 lh->include_dirs_size *= 2;
10568 lh->include_dirs = xrealloc (lh->include_dirs,
10569 (lh->include_dirs_size
10570 * sizeof (*lh->include_dirs)));
c5aa993b 10571 }
c906108c 10572
debd256d
JB
10573 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10574}
6e70227d 10575
debd256d 10576/* Add an entry to LH's file name table. */
ae2de4f8 10577
debd256d
JB
10578static void
10579add_file_name (struct line_header *lh,
10580 char *name,
10581 unsigned int dir_index,
10582 unsigned int mod_time,
10583 unsigned int length)
10584{
10585 struct file_entry *fe;
10586
10587 /* Grow the array if necessary. */
10588 if (lh->file_names_size == 0)
10589 {
10590 lh->file_names_size = 1; /* for testing */
10591 lh->file_names = xmalloc (lh->file_names_size
10592 * sizeof (*lh->file_names));
10593 }
10594 else if (lh->num_file_names >= lh->file_names_size)
10595 {
10596 lh->file_names_size *= 2;
10597 lh->file_names = xrealloc (lh->file_names,
10598 (lh->file_names_size
10599 * sizeof (*lh->file_names)));
10600 }
10601
10602 fe = &lh->file_names[lh->num_file_names++];
10603 fe->name = name;
10604 fe->dir_index = dir_index;
10605 fe->mod_time = mod_time;
10606 fe->length = length;
aaa75496 10607 fe->included_p = 0;
cb1df416 10608 fe->symtab = NULL;
debd256d 10609}
6e70227d 10610
debd256d 10611/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10612 .debug_line, according to the endianness of ABFD. Return a pointer
10613 to a struct line_header, allocated using xmalloc.
debd256d
JB
10614
10615 NOTE: the strings in the include directory and file name tables of
10616 the returned object point into debug_line_buffer, and must not be
10617 freed. */
ae2de4f8 10618
debd256d
JB
10619static struct line_header *
10620dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10621 struct dwarf2_cu *cu)
debd256d
JB
10622{
10623 struct cleanup *back_to;
10624 struct line_header *lh;
fe1b8b76 10625 gdb_byte *line_ptr;
c764a876 10626 unsigned int bytes_read, offset_size;
debd256d
JB
10627 int i;
10628 char *cur_dir, *cur_file;
10629
be391dca 10630 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10631 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10632 {
e2e0b3e5 10633 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10634 return 0;
10635 }
10636
a738430d
MK
10637 /* Make sure that at least there's room for the total_length field.
10638 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10639 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10640 {
4d3c2250 10641 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10642 return 0;
10643 }
10644
10645 lh = xmalloc (sizeof (*lh));
10646 memset (lh, 0, sizeof (*lh));
10647 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10648 (void *) lh);
10649
dce234bc 10650 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10651
a738430d 10652 /* Read in the header. */
6e70227d 10653 lh->total_length =
c764a876
DE
10654 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10655 &bytes_read, &offset_size);
debd256d 10656 line_ptr += bytes_read;
dce234bc
PP
10657 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10658 + dwarf2_per_objfile->line.size))
debd256d 10659 {
4d3c2250 10660 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10661 return 0;
10662 }
10663 lh->statement_program_end = line_ptr + lh->total_length;
10664 lh->version = read_2_bytes (abfd, line_ptr);
10665 line_ptr += 2;
c764a876
DE
10666 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10667 line_ptr += offset_size;
debd256d
JB
10668 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10669 line_ptr += 1;
2dc7f7b3
TT
10670 if (lh->version >= 4)
10671 {
10672 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10673 line_ptr += 1;
10674 }
10675 else
10676 lh->maximum_ops_per_instruction = 1;
10677
10678 if (lh->maximum_ops_per_instruction == 0)
10679 {
10680 lh->maximum_ops_per_instruction = 1;
10681 complaint (&symfile_complaints,
3e43a32a
MS
10682 _("invalid maximum_ops_per_instruction "
10683 "in `.debug_line' section"));
2dc7f7b3
TT
10684 }
10685
debd256d
JB
10686 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10687 line_ptr += 1;
10688 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10689 line_ptr += 1;
10690 lh->line_range = read_1_byte (abfd, line_ptr);
10691 line_ptr += 1;
10692 lh->opcode_base = read_1_byte (abfd, line_ptr);
10693 line_ptr += 1;
10694 lh->standard_opcode_lengths
fe1b8b76 10695 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10696
10697 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10698 for (i = 1; i < lh->opcode_base; ++i)
10699 {
10700 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10701 line_ptr += 1;
10702 }
10703
a738430d 10704 /* Read directory table. */
9b1c24c8 10705 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10706 {
10707 line_ptr += bytes_read;
10708 add_include_dir (lh, cur_dir);
10709 }
10710 line_ptr += bytes_read;
10711
a738430d 10712 /* Read file name table. */
9b1c24c8 10713 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10714 {
10715 unsigned int dir_index, mod_time, length;
10716
10717 line_ptr += bytes_read;
10718 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10719 line_ptr += bytes_read;
10720 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10721 line_ptr += bytes_read;
10722 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10723 line_ptr += bytes_read;
10724
10725 add_file_name (lh, cur_file, dir_index, mod_time, length);
10726 }
10727 line_ptr += bytes_read;
6e70227d 10728 lh->statement_program_start = line_ptr;
debd256d 10729
dce234bc
PP
10730 if (line_ptr > (dwarf2_per_objfile->line.buffer
10731 + dwarf2_per_objfile->line.size))
4d3c2250 10732 complaint (&symfile_complaints,
3e43a32a
MS
10733 _("line number info header doesn't "
10734 "fit in `.debug_line' section"));
debd256d
JB
10735
10736 discard_cleanups (back_to);
10737 return lh;
10738}
c906108c 10739
5fb290d7
DJ
10740/* This function exists to work around a bug in certain compilers
10741 (particularly GCC 2.95), in which the first line number marker of a
10742 function does not show up until after the prologue, right before
10743 the second line number marker. This function shifts ADDRESS down
10744 to the beginning of the function if necessary, and is called on
10745 addresses passed to record_line. */
10746
10747static CORE_ADDR
e142c38c 10748check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10749{
10750 struct function_range *fn;
10751
10752 /* Find the function_range containing address. */
e142c38c 10753 if (!cu->first_fn)
5fb290d7
DJ
10754 return address;
10755
e142c38c
DJ
10756 if (!cu->cached_fn)
10757 cu->cached_fn = cu->first_fn;
5fb290d7 10758
e142c38c 10759 fn = cu->cached_fn;
5fb290d7
DJ
10760 while (fn)
10761 if (fn->lowpc <= address && fn->highpc > address)
10762 goto found;
10763 else
10764 fn = fn->next;
10765
e142c38c
DJ
10766 fn = cu->first_fn;
10767 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10768 if (fn->lowpc <= address && fn->highpc > address)
10769 goto found;
10770 else
10771 fn = fn->next;
10772
10773 return address;
10774
10775 found:
10776 if (fn->seen_line)
10777 return address;
10778 if (address != fn->lowpc)
4d3c2250 10779 complaint (&symfile_complaints,
e2e0b3e5 10780 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10781 (unsigned long) address, fn->name);
5fb290d7
DJ
10782 fn->seen_line = 1;
10783 return fn->lowpc;
10784}
10785
c6da4cef
DE
10786/* Subroutine of dwarf_decode_lines to simplify it.
10787 Return the file name of the psymtab for included file FILE_INDEX
10788 in line header LH of PST.
10789 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10790 If space for the result is malloc'd, it will be freed by a cleanup.
10791 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10792
10793static char *
10794psymtab_include_file_name (const struct line_header *lh, int file_index,
10795 const struct partial_symtab *pst,
10796 const char *comp_dir)
10797{
10798 const struct file_entry fe = lh->file_names [file_index];
10799 char *include_name = fe.name;
10800 char *include_name_to_compare = include_name;
10801 char *dir_name = NULL;
72b9f47f
TT
10802 const char *pst_filename;
10803 char *copied_name = NULL;
c6da4cef
DE
10804 int file_is_pst;
10805
10806 if (fe.dir_index)
10807 dir_name = lh->include_dirs[fe.dir_index - 1];
10808
10809 if (!IS_ABSOLUTE_PATH (include_name)
10810 && (dir_name != NULL || comp_dir != NULL))
10811 {
10812 /* Avoid creating a duplicate psymtab for PST.
10813 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10814 Before we do the comparison, however, we need to account
10815 for DIR_NAME and COMP_DIR.
10816 First prepend dir_name (if non-NULL). If we still don't
10817 have an absolute path prepend comp_dir (if non-NULL).
10818 However, the directory we record in the include-file's
10819 psymtab does not contain COMP_DIR (to match the
10820 corresponding symtab(s)).
10821
10822 Example:
10823
10824 bash$ cd /tmp
10825 bash$ gcc -g ./hello.c
10826 include_name = "hello.c"
10827 dir_name = "."
10828 DW_AT_comp_dir = comp_dir = "/tmp"
10829 DW_AT_name = "./hello.c" */
10830
10831 if (dir_name != NULL)
10832 {
10833 include_name = concat (dir_name, SLASH_STRING,
10834 include_name, (char *)NULL);
10835 include_name_to_compare = include_name;
10836 make_cleanup (xfree, include_name);
10837 }
10838 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10839 {
10840 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10841 include_name, (char *)NULL);
10842 }
10843 }
10844
10845 pst_filename = pst->filename;
10846 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10847 {
72b9f47f
TT
10848 copied_name = concat (pst->dirname, SLASH_STRING,
10849 pst_filename, (char *)NULL);
10850 pst_filename = copied_name;
c6da4cef
DE
10851 }
10852
1e3fad37 10853 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10854
10855 if (include_name_to_compare != include_name)
10856 xfree (include_name_to_compare);
72b9f47f
TT
10857 if (copied_name != NULL)
10858 xfree (copied_name);
c6da4cef
DE
10859
10860 if (file_is_pst)
10861 return NULL;
10862 return include_name;
10863}
10864
c91513d8
PP
10865/* Ignore this record_line request. */
10866
10867static void
10868noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10869{
10870 return;
10871}
10872
aaa75496
JB
10873/* Decode the Line Number Program (LNP) for the given line_header
10874 structure and CU. The actual information extracted and the type
10875 of structures created from the LNP depends on the value of PST.
10876
10877 1. If PST is NULL, then this procedure uses the data from the program
10878 to create all necessary symbol tables, and their linetables.
6e70227d 10879
aaa75496
JB
10880 2. If PST is not NULL, this procedure reads the program to determine
10881 the list of files included by the unit represented by PST, and
c6da4cef
DE
10882 builds all the associated partial symbol tables.
10883
10884 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10885 It is used for relative paths in the line table.
10886 NOTE: When processing partial symtabs (pst != NULL),
10887 comp_dir == pst->dirname.
10888
10889 NOTE: It is important that psymtabs have the same file name (via strcmp)
10890 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10891 symtab we don't use it in the name of the psymtabs we create.
10892 E.g. expand_line_sal requires this when finding psymtabs to expand.
10893 A good testcase for this is mb-inline.exp. */
debd256d 10894
c906108c 10895static void
72b9f47f 10896dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10897 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10898{
a8c50c1f 10899 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10900 gdb_byte *line_end;
a8c50c1f 10901 unsigned int bytes_read, extended_len;
c906108c 10902 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10903 CORE_ADDR baseaddr;
10904 struct objfile *objfile = cu->objfile;
fbf65064 10905 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10906 const int decode_for_pst_p = (pst != NULL);
cb1df416 10907 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
10908 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10909 = record_line;
e142c38c
DJ
10910
10911 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10912
debd256d
JB
10913 line_ptr = lh->statement_program_start;
10914 line_end = lh->statement_program_end;
c906108c
SS
10915
10916 /* Read the statement sequences until there's nothing left. */
10917 while (line_ptr < line_end)
10918 {
10919 /* state machine registers */
10920 CORE_ADDR address = 0;
10921 unsigned int file = 1;
10922 unsigned int line = 1;
10923 unsigned int column = 0;
debd256d 10924 int is_stmt = lh->default_is_stmt;
c906108c
SS
10925 int basic_block = 0;
10926 int end_sequence = 0;
fbf65064 10927 CORE_ADDR addr;
2dc7f7b3 10928 unsigned char op_index = 0;
c906108c 10929
aaa75496 10930 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10931 {
aaa75496 10932 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10933 /* lh->include_dirs and lh->file_names are 0-based, but the
10934 directory and file name numbers in the statement program
10935 are 1-based. */
10936 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10937 char *dir = NULL;
a738430d 10938
debd256d
JB
10939 if (fe->dir_index)
10940 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10941
10942 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10943 }
10944
a738430d 10945 /* Decode the table. */
c5aa993b 10946 while (!end_sequence)
c906108c
SS
10947 {
10948 op_code = read_1_byte (abfd, line_ptr);
10949 line_ptr += 1;
59205f5a
JB
10950 if (line_ptr > line_end)
10951 {
10952 dwarf2_debug_line_missing_end_sequence_complaint ();
10953 break;
10954 }
9aa1fe7e 10955
debd256d 10956 if (op_code >= lh->opcode_base)
6e70227d 10957 {
a738430d 10958 /* Special operand. */
debd256d 10959 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10960 address += (((op_index + (adj_opcode / lh->line_range))
10961 / lh->maximum_ops_per_instruction)
10962 * lh->minimum_instruction_length);
10963 op_index = ((op_index + (adj_opcode / lh->line_range))
10964 % lh->maximum_ops_per_instruction);
debd256d 10965 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10966 if (lh->num_file_names < file || file == 0)
25e43795 10967 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10968 /* For now we ignore lines not starting on an
10969 instruction boundary. */
10970 else if (op_index == 0)
25e43795
DJ
10971 {
10972 lh->file_names[file - 1].included_p = 1;
ca5f395d 10973 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10974 {
10975 if (last_subfile != current_subfile)
10976 {
10977 addr = gdbarch_addr_bits_remove (gdbarch, address);
10978 if (last_subfile)
c91513d8 10979 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10980 last_subfile = current_subfile;
10981 }
25e43795 10982 /* Append row to matrix using current values. */
fbf65064
UW
10983 addr = check_cu_functions (address, cu);
10984 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10985 (*p_record_line) (current_subfile, line, addr);
366da635 10986 }
25e43795 10987 }
ca5f395d 10988 basic_block = 0;
9aa1fe7e
GK
10989 }
10990 else switch (op_code)
c906108c
SS
10991 {
10992 case DW_LNS_extended_op:
3e43a32a
MS
10993 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10994 &bytes_read);
473b7be6 10995 line_ptr += bytes_read;
a8c50c1f 10996 extended_end = line_ptr + extended_len;
c906108c
SS
10997 extended_op = read_1_byte (abfd, line_ptr);
10998 line_ptr += 1;
10999 switch (extended_op)
11000 {
11001 case DW_LNE_end_sequence:
c91513d8 11002 p_record_line = record_line;
c906108c 11003 end_sequence = 1;
c906108c
SS
11004 break;
11005 case DW_LNE_set_address:
e7c27a73 11006 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
11007
11008 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11009 {
11010 /* This line table is for a function which has been
11011 GCd by the linker. Ignore it. PR gdb/12528 */
11012
11013 long line_offset
11014 = line_ptr - dwarf2_per_objfile->line.buffer;
11015
11016 complaint (&symfile_complaints,
11017 _(".debug_line address at offset 0x%lx is 0 "
11018 "[in module %s]"),
11019 line_offset, cu->objfile->name);
11020 p_record_line = noop_record_line;
11021 }
11022
2dc7f7b3 11023 op_index = 0;
107d2387
AC
11024 line_ptr += bytes_read;
11025 address += baseaddr;
c906108c
SS
11026 break;
11027 case DW_LNE_define_file:
debd256d
JB
11028 {
11029 char *cur_file;
11030 unsigned int dir_index, mod_time, length;
6e70227d 11031
3e43a32a
MS
11032 cur_file = read_direct_string (abfd, line_ptr,
11033 &bytes_read);
debd256d
JB
11034 line_ptr += bytes_read;
11035 dir_index =
11036 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11037 line_ptr += bytes_read;
11038 mod_time =
11039 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11040 line_ptr += bytes_read;
11041 length =
11042 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11043 line_ptr += bytes_read;
11044 add_file_name (lh, cur_file, dir_index, mod_time, length);
11045 }
c906108c 11046 break;
d0c6ba3d
CC
11047 case DW_LNE_set_discriminator:
11048 /* The discriminator is not interesting to the debugger;
11049 just ignore it. */
11050 line_ptr = extended_end;
11051 break;
c906108c 11052 default:
4d3c2250 11053 complaint (&symfile_complaints,
e2e0b3e5 11054 _("mangled .debug_line section"));
debd256d 11055 return;
c906108c 11056 }
a8c50c1f
DJ
11057 /* Make sure that we parsed the extended op correctly. If e.g.
11058 we expected a different address size than the producer used,
11059 we may have read the wrong number of bytes. */
11060 if (line_ptr != extended_end)
11061 {
11062 complaint (&symfile_complaints,
11063 _("mangled .debug_line section"));
11064 return;
11065 }
c906108c
SS
11066 break;
11067 case DW_LNS_copy:
59205f5a 11068 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11069 dwarf2_debug_line_missing_file_complaint ();
11070 else
366da635 11071 {
25e43795 11072 lh->file_names[file - 1].included_p = 1;
ca5f395d 11073 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11074 {
11075 if (last_subfile != current_subfile)
11076 {
11077 addr = gdbarch_addr_bits_remove (gdbarch, address);
11078 if (last_subfile)
c91513d8 11079 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11080 last_subfile = current_subfile;
11081 }
11082 addr = check_cu_functions (address, cu);
11083 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 11084 (*p_record_line) (current_subfile, line, addr);
fbf65064 11085 }
366da635 11086 }
c906108c
SS
11087 basic_block = 0;
11088 break;
11089 case DW_LNS_advance_pc:
2dc7f7b3
TT
11090 {
11091 CORE_ADDR adjust
11092 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11093
11094 address += (((op_index + adjust)
11095 / lh->maximum_ops_per_instruction)
11096 * lh->minimum_instruction_length);
11097 op_index = ((op_index + adjust)
11098 % lh->maximum_ops_per_instruction);
11099 line_ptr += bytes_read;
11100 }
c906108c
SS
11101 break;
11102 case DW_LNS_advance_line:
11103 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11104 line_ptr += bytes_read;
11105 break;
11106 case DW_LNS_set_file:
debd256d 11107 {
a738430d
MK
11108 /* The arrays lh->include_dirs and lh->file_names are
11109 0-based, but the directory and file name numbers in
11110 the statement program are 1-based. */
debd256d 11111 struct file_entry *fe;
4f1520fb 11112 char *dir = NULL;
a738430d 11113
debd256d
JB
11114 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11115 line_ptr += bytes_read;
59205f5a 11116 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11117 dwarf2_debug_line_missing_file_complaint ();
11118 else
11119 {
11120 fe = &lh->file_names[file - 1];
11121 if (fe->dir_index)
11122 dir = lh->include_dirs[fe->dir_index - 1];
11123 if (!decode_for_pst_p)
11124 {
11125 last_subfile = current_subfile;
11126 dwarf2_start_subfile (fe->name, dir, comp_dir);
11127 }
11128 }
debd256d 11129 }
c906108c
SS
11130 break;
11131 case DW_LNS_set_column:
11132 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11133 line_ptr += bytes_read;
11134 break;
11135 case DW_LNS_negate_stmt:
11136 is_stmt = (!is_stmt);
11137 break;
11138 case DW_LNS_set_basic_block:
11139 basic_block = 1;
11140 break;
c2c6d25f
JM
11141 /* Add to the address register of the state machine the
11142 address increment value corresponding to special opcode
a738430d
MK
11143 255. I.e., this value is scaled by the minimum
11144 instruction length since special opcode 255 would have
b021a221 11145 scaled the increment. */
c906108c 11146 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11147 {
11148 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11149
11150 address += (((op_index + adjust)
11151 / lh->maximum_ops_per_instruction)
11152 * lh->minimum_instruction_length);
11153 op_index = ((op_index + adjust)
11154 % lh->maximum_ops_per_instruction);
11155 }
c906108c
SS
11156 break;
11157 case DW_LNS_fixed_advance_pc:
11158 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11159 op_index = 0;
c906108c
SS
11160 line_ptr += 2;
11161 break;
9aa1fe7e 11162 default:
a738430d
MK
11163 {
11164 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11165 int i;
a738430d 11166
debd256d 11167 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11168 {
11169 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11170 line_ptr += bytes_read;
11171 }
11172 }
c906108c
SS
11173 }
11174 }
59205f5a
JB
11175 if (lh->num_file_names < file || file == 0)
11176 dwarf2_debug_line_missing_file_complaint ();
11177 else
11178 {
11179 lh->file_names[file - 1].included_p = 1;
11180 if (!decode_for_pst_p)
fbf65064
UW
11181 {
11182 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11183 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11184 }
59205f5a 11185 }
c906108c 11186 }
aaa75496
JB
11187
11188 if (decode_for_pst_p)
11189 {
11190 int file_index;
11191
11192 /* Now that we're done scanning the Line Header Program, we can
11193 create the psymtab of each included file. */
11194 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11195 if (lh->file_names[file_index].included_p == 1)
11196 {
c6da4cef
DE
11197 char *include_name =
11198 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11199 if (include_name != NULL)
aaa75496
JB
11200 dwarf2_create_include_psymtab (include_name, pst, objfile);
11201 }
11202 }
cb1df416
DJ
11203 else
11204 {
11205 /* Make sure a symtab is created for every file, even files
11206 which contain only variables (i.e. no code with associated
11207 line numbers). */
11208
11209 int i;
11210 struct file_entry *fe;
11211
11212 for (i = 0; i < lh->num_file_names; i++)
11213 {
11214 char *dir = NULL;
9a619af0 11215
cb1df416
DJ
11216 fe = &lh->file_names[i];
11217 if (fe->dir_index)
11218 dir = lh->include_dirs[fe->dir_index - 1];
11219 dwarf2_start_subfile (fe->name, dir, comp_dir);
11220
11221 /* Skip the main file; we don't need it, and it must be
11222 allocated last, so that it will show up before the
11223 non-primary symtabs in the objfile's symtab list. */
11224 if (current_subfile == first_subfile)
11225 continue;
11226
11227 if (current_subfile->symtab == NULL)
11228 current_subfile->symtab = allocate_symtab (current_subfile->name,
11229 cu->objfile);
11230 fe->symtab = current_subfile->symtab;
11231 }
11232 }
c906108c
SS
11233}
11234
11235/* Start a subfile for DWARF. FILENAME is the name of the file and
11236 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11237 or NULL if not known. COMP_DIR is the compilation directory for the
11238 linetable's compilation unit or NULL if not known.
c906108c
SS
11239 This routine tries to keep line numbers from identical absolute and
11240 relative file names in a common subfile.
11241
11242 Using the `list' example from the GDB testsuite, which resides in
11243 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11244 of /srcdir/list0.c yields the following debugging information for list0.c:
11245
c5aa993b
JM
11246 DW_AT_name: /srcdir/list0.c
11247 DW_AT_comp_dir: /compdir
357e46e7 11248 files.files[0].name: list0.h
c5aa993b 11249 files.files[0].dir: /srcdir
357e46e7 11250 files.files[1].name: list0.c
c5aa993b 11251 files.files[1].dir: /srcdir
c906108c
SS
11252
11253 The line number information for list0.c has to end up in a single
4f1520fb
FR
11254 subfile, so that `break /srcdir/list0.c:1' works as expected.
11255 start_subfile will ensure that this happens provided that we pass the
11256 concatenation of files.files[1].dir and files.files[1].name as the
11257 subfile's name. */
c906108c
SS
11258
11259static void
3e43a32a
MS
11260dwarf2_start_subfile (char *filename, const char *dirname,
11261 const char *comp_dir)
c906108c 11262{
4f1520fb
FR
11263 char *fullname;
11264
11265 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11266 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11267 second argument to start_subfile. To be consistent, we do the
11268 same here. In order not to lose the line information directory,
11269 we concatenate it to the filename when it makes sense.
11270 Note that the Dwarf3 standard says (speaking of filenames in line
11271 information): ``The directory index is ignored for file names
11272 that represent full path names''. Thus ignoring dirname in the
11273 `else' branch below isn't an issue. */
c906108c 11274
d5166ae1 11275 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11276 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11277 else
11278 fullname = filename;
c906108c 11279
4f1520fb
FR
11280 start_subfile (fullname, comp_dir);
11281
11282 if (fullname != filename)
11283 xfree (fullname);
c906108c
SS
11284}
11285
4c2df51b
DJ
11286static void
11287var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11288 struct dwarf2_cu *cu)
4c2df51b 11289{
e7c27a73
DJ
11290 struct objfile *objfile = cu->objfile;
11291 struct comp_unit_head *cu_header = &cu->header;
11292
4c2df51b
DJ
11293 /* NOTE drow/2003-01-30: There used to be a comment and some special
11294 code here to turn a symbol with DW_AT_external and a
11295 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11296 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11297 with some versions of binutils) where shared libraries could have
11298 relocations against symbols in their debug information - the
11299 minimal symbol would have the right address, but the debug info
11300 would not. It's no longer necessary, because we will explicitly
11301 apply relocations when we read in the debug information now. */
11302
11303 /* A DW_AT_location attribute with no contents indicates that a
11304 variable has been optimized away. */
11305 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11306 {
11307 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11308 return;
11309 }
11310
11311 /* Handle one degenerate form of location expression specially, to
11312 preserve GDB's previous behavior when section offsets are
11313 specified. If this is just a DW_OP_addr then mark this symbol
11314 as LOC_STATIC. */
11315
11316 if (attr_form_is_block (attr)
11317 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11318 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11319 {
891d2f0b 11320 unsigned int dummy;
4c2df51b
DJ
11321
11322 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11323 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11324 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11325 fixup_symbol_section (sym, objfile);
11326 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11327 SYMBOL_SECTION (sym));
4c2df51b
DJ
11328 return;
11329 }
11330
11331 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11332 expression evaluator, and use LOC_COMPUTED only when necessary
11333 (i.e. when the value of a register or memory location is
11334 referenced, or a thread-local block, etc.). Then again, it might
11335 not be worthwhile. I'm assuming that it isn't unless performance
11336 or memory numbers show me otherwise. */
11337
e7c27a73 11338 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11339 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11340
11341 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11342 cu->has_loclist = 1;
4c2df51b
DJ
11343}
11344
c906108c
SS
11345/* Given a pointer to a DWARF information entry, figure out if we need
11346 to make a symbol table entry for it, and if so, create a new entry
11347 and return a pointer to it.
11348 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11349 used the passed type.
11350 If SPACE is not NULL, use it to hold the new symbol. If it is
11351 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11352
11353static struct symbol *
34eaf542
TT
11354new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11355 struct symbol *space)
c906108c 11356{
e7c27a73 11357 struct objfile *objfile = cu->objfile;
c906108c
SS
11358 struct symbol *sym = NULL;
11359 char *name;
11360 struct attribute *attr = NULL;
11361 struct attribute *attr2 = NULL;
e142c38c 11362 CORE_ADDR baseaddr;
e37fd15a
SW
11363 struct pending **list_to_add = NULL;
11364
edb3359d 11365 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11366
11367 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11368
94af9270 11369 name = dwarf2_name (die, cu);
c906108c
SS
11370 if (name)
11371 {
94af9270 11372 const char *linkagename;
34eaf542 11373 int suppress_add = 0;
94af9270 11374
34eaf542
TT
11375 if (space)
11376 sym = space;
11377 else
11378 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11379 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11380
11381 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11382 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11383 linkagename = dwarf2_physname (name, die, cu);
11384 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11385
f55ee35c
JK
11386 /* Fortran does not have mangling standard and the mangling does differ
11387 between gfortran, iFort etc. */
11388 if (cu->language == language_fortran
b250c185 11389 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11390 symbol_set_demangled_name (&(sym->ginfo),
11391 (char *) dwarf2_full_name (name, die, cu),
11392 NULL);
f55ee35c 11393
c906108c 11394 /* Default assumptions.
c5aa993b 11395 Use the passed type or decode it from the die. */
176620f1 11396 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11397 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11398 if (type != NULL)
11399 SYMBOL_TYPE (sym) = type;
11400 else
e7c27a73 11401 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11402 attr = dwarf2_attr (die,
11403 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11404 cu);
c906108c
SS
11405 if (attr)
11406 {
11407 SYMBOL_LINE (sym) = DW_UNSND (attr);
11408 }
cb1df416 11409
edb3359d
DJ
11410 attr = dwarf2_attr (die,
11411 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11412 cu);
cb1df416
DJ
11413 if (attr)
11414 {
11415 int file_index = DW_UNSND (attr);
9a619af0 11416
cb1df416
DJ
11417 if (cu->line_header == NULL
11418 || file_index > cu->line_header->num_file_names)
11419 complaint (&symfile_complaints,
11420 _("file index out of range"));
1c3d648d 11421 else if (file_index > 0)
cb1df416
DJ
11422 {
11423 struct file_entry *fe;
9a619af0 11424
cb1df416
DJ
11425 fe = &cu->line_header->file_names[file_index - 1];
11426 SYMBOL_SYMTAB (sym) = fe->symtab;
11427 }
11428 }
11429
c906108c
SS
11430 switch (die->tag)
11431 {
11432 case DW_TAG_label:
e142c38c 11433 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11434 if (attr)
11435 {
11436 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11437 }
0f5238ed
TT
11438 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11439 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11440 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11441 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11442 break;
11443 case DW_TAG_subprogram:
11444 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11445 finish_block. */
11446 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11447 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11448 if ((attr2 && (DW_UNSND (attr2) != 0))
11449 || cu->language == language_ada)
c906108c 11450 {
2cfa0c8d
JB
11451 /* Subprograms marked external are stored as a global symbol.
11452 Ada subprograms, whether marked external or not, are always
11453 stored as a global symbol, because we want to be able to
11454 access them globally. For instance, we want to be able
11455 to break on a nested subprogram without having to
11456 specify the context. */
e37fd15a 11457 list_to_add = &global_symbols;
c906108c
SS
11458 }
11459 else
11460 {
e37fd15a 11461 list_to_add = cu->list_in_scope;
c906108c
SS
11462 }
11463 break;
edb3359d
DJ
11464 case DW_TAG_inlined_subroutine:
11465 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11466 finish_block. */
11467 SYMBOL_CLASS (sym) = LOC_BLOCK;
11468 SYMBOL_INLINED (sym) = 1;
11469 /* Do not add the symbol to any lists. It will be found via
11470 BLOCK_FUNCTION from the blockvector. */
11471 break;
34eaf542
TT
11472 case DW_TAG_template_value_param:
11473 suppress_add = 1;
11474 /* Fall through. */
72929c62 11475 case DW_TAG_constant:
c906108c 11476 case DW_TAG_variable:
254e6b9e 11477 case DW_TAG_member:
0963b4bd
MS
11478 /* Compilation with minimal debug info may result in
11479 variables with missing type entries. Change the
11480 misleading `void' type to something sensible. */
c906108c 11481 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11482 SYMBOL_TYPE (sym)
46bf5051 11483 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11484
e142c38c 11485 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11486 /* In the case of DW_TAG_member, we should only be called for
11487 static const members. */
11488 if (die->tag == DW_TAG_member)
11489 {
3863f96c
DE
11490 /* dwarf2_add_field uses die_is_declaration,
11491 so we do the same. */
254e6b9e
DE
11492 gdb_assert (die_is_declaration (die, cu));
11493 gdb_assert (attr);
11494 }
c906108c
SS
11495 if (attr)
11496 {
e7c27a73 11497 dwarf2_const_value (attr, sym, cu);
e142c38c 11498 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11499 if (!suppress_add)
34eaf542
TT
11500 {
11501 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11502 list_to_add = &global_symbols;
34eaf542 11503 else
e37fd15a 11504 list_to_add = cu->list_in_scope;
34eaf542 11505 }
c906108c
SS
11506 break;
11507 }
e142c38c 11508 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11509 if (attr)
11510 {
e7c27a73 11511 var_decode_location (attr, sym, cu);
e142c38c 11512 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11513 if (SYMBOL_CLASS (sym) == LOC_STATIC
11514 && SYMBOL_VALUE_ADDRESS (sym) == 0
11515 && !dwarf2_per_objfile->has_section_at_zero)
11516 {
11517 /* When a static variable is eliminated by the linker,
11518 the corresponding debug information is not stripped
11519 out, but the variable address is set to null;
11520 do not add such variables into symbol table. */
11521 }
11522 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11523 {
f55ee35c
JK
11524 /* Workaround gfortran PR debug/40040 - it uses
11525 DW_AT_location for variables in -fPIC libraries which may
11526 get overriden by other libraries/executable and get
11527 a different address. Resolve it by the minimal symbol
11528 which may come from inferior's executable using copy
11529 relocation. Make this workaround only for gfortran as for
11530 other compilers GDB cannot guess the minimal symbol
11531 Fortran mangling kind. */
11532 if (cu->language == language_fortran && die->parent
11533 && die->parent->tag == DW_TAG_module
11534 && cu->producer
11535 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11536 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11537
1c809c68
TT
11538 /* A variable with DW_AT_external is never static,
11539 but it may be block-scoped. */
11540 list_to_add = (cu->list_in_scope == &file_symbols
11541 ? &global_symbols : cu->list_in_scope);
1c809c68 11542 }
c906108c 11543 else
e37fd15a 11544 list_to_add = cu->list_in_scope;
c906108c
SS
11545 }
11546 else
11547 {
11548 /* We do not know the address of this symbol.
c5aa993b
JM
11549 If it is an external symbol and we have type information
11550 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11551 The address of the variable will then be determined from
11552 the minimal symbol table whenever the variable is
11553 referenced. */
e142c38c 11554 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11555 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11556 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11557 {
0fe7935b
DJ
11558 /* A variable with DW_AT_external is never static, but it
11559 may be block-scoped. */
11560 list_to_add = (cu->list_in_scope == &file_symbols
11561 ? &global_symbols : cu->list_in_scope);
11562
c906108c 11563 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11564 }
442ddf59
JK
11565 else if (!die_is_declaration (die, cu))
11566 {
11567 /* Use the default LOC_OPTIMIZED_OUT class. */
11568 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11569 if (!suppress_add)
11570 list_to_add = cu->list_in_scope;
442ddf59 11571 }
c906108c
SS
11572 }
11573 break;
11574 case DW_TAG_formal_parameter:
edb3359d
DJ
11575 /* If we are inside a function, mark this as an argument. If
11576 not, we might be looking at an argument to an inlined function
11577 when we do not have enough information to show inlined frames;
11578 pretend it's a local variable in that case so that the user can
11579 still see it. */
11580 if (context_stack_depth > 0
11581 && context_stack[context_stack_depth - 1].name != NULL)
11582 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11583 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11584 if (attr)
11585 {
e7c27a73 11586 var_decode_location (attr, sym, cu);
c906108c 11587 }
e142c38c 11588 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11589 if (attr)
11590 {
e7c27a73 11591 dwarf2_const_value (attr, sym, cu);
c906108c 11592 }
f346a30d
PM
11593 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11594 if (attr && DW_UNSND (attr))
11595 {
11596 struct type *ref_type;
11597
11598 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11599 SYMBOL_TYPE (sym) = ref_type;
11600 }
11601
e37fd15a 11602 list_to_add = cu->list_in_scope;
c906108c
SS
11603 break;
11604 case DW_TAG_unspecified_parameters:
11605 /* From varargs functions; gdb doesn't seem to have any
11606 interest in this information, so just ignore it for now.
11607 (FIXME?) */
11608 break;
34eaf542
TT
11609 case DW_TAG_template_type_param:
11610 suppress_add = 1;
11611 /* Fall through. */
c906108c 11612 case DW_TAG_class_type:
680b30c7 11613 case DW_TAG_interface_type:
c906108c
SS
11614 case DW_TAG_structure_type:
11615 case DW_TAG_union_type:
72019c9c 11616 case DW_TAG_set_type:
c906108c
SS
11617 case DW_TAG_enumeration_type:
11618 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11619 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11620
63d06c5c 11621 {
987504bb 11622 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11623 really ever be static objects: otherwise, if you try
11624 to, say, break of a class's method and you're in a file
11625 which doesn't mention that class, it won't work unless
11626 the check for all static symbols in lookup_symbol_aux
11627 saves you. See the OtherFileClass tests in
11628 gdb.c++/namespace.exp. */
11629
e37fd15a 11630 if (!suppress_add)
34eaf542 11631 {
34eaf542
TT
11632 list_to_add = (cu->list_in_scope == &file_symbols
11633 && (cu->language == language_cplus
11634 || cu->language == language_java)
11635 ? &global_symbols : cu->list_in_scope);
63d06c5c 11636
64382290
TT
11637 /* The semantics of C++ state that "struct foo {
11638 ... }" also defines a typedef for "foo". A Java
11639 class declaration also defines a typedef for the
11640 class. */
11641 if (cu->language == language_cplus
11642 || cu->language == language_java
11643 || cu->language == language_ada)
11644 {
11645 /* The symbol's name is already allocated along
11646 with this objfile, so we don't need to
11647 duplicate it for the type. */
11648 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11649 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11650 }
63d06c5c
DC
11651 }
11652 }
c906108c
SS
11653 break;
11654 case DW_TAG_typedef:
63d06c5c
DC
11655 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11656 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11657 list_to_add = cu->list_in_scope;
63d06c5c 11658 break;
c906108c 11659 case DW_TAG_base_type:
a02abb62 11660 case DW_TAG_subrange_type:
c906108c 11661 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11662 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11663 list_to_add = cu->list_in_scope;
c906108c
SS
11664 break;
11665 case DW_TAG_enumerator:
e142c38c 11666 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11667 if (attr)
11668 {
e7c27a73 11669 dwarf2_const_value (attr, sym, cu);
c906108c 11670 }
63d06c5c
DC
11671 {
11672 /* NOTE: carlton/2003-11-10: See comment above in the
11673 DW_TAG_class_type, etc. block. */
11674
e142c38c 11675 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11676 && (cu->language == language_cplus
11677 || cu->language == language_java)
e142c38c 11678 ? &global_symbols : cu->list_in_scope);
63d06c5c 11679 }
c906108c 11680 break;
5c4e30ca
DC
11681 case DW_TAG_namespace:
11682 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11683 list_to_add = &global_symbols;
5c4e30ca 11684 break;
c906108c
SS
11685 default:
11686 /* Not a tag we recognize. Hopefully we aren't processing
11687 trash data, but since we must specifically ignore things
11688 we don't recognize, there is nothing else we should do at
0963b4bd 11689 this point. */
e2e0b3e5 11690 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11691 dwarf_tag_name (die->tag));
c906108c
SS
11692 break;
11693 }
df8a16a1 11694
e37fd15a
SW
11695 if (suppress_add)
11696 {
11697 sym->hash_next = objfile->template_symbols;
11698 objfile->template_symbols = sym;
11699 list_to_add = NULL;
11700 }
11701
11702 if (list_to_add != NULL)
11703 add_symbol_to_list (sym, list_to_add);
11704
df8a16a1
DJ
11705 /* For the benefit of old versions of GCC, check for anonymous
11706 namespaces based on the demangled name. */
11707 if (!processing_has_namespace_info
94af9270 11708 && cu->language == language_cplus)
df8a16a1 11709 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
11710 }
11711 return (sym);
11712}
11713
34eaf542
TT
11714/* A wrapper for new_symbol_full that always allocates a new symbol. */
11715
11716static struct symbol *
11717new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11718{
11719 return new_symbol_full (die, type, cu, NULL);
11720}
11721
98bfdba5
PA
11722/* Given an attr with a DW_FORM_dataN value in host byte order,
11723 zero-extend it as appropriate for the symbol's type. The DWARF
11724 standard (v4) is not entirely clear about the meaning of using
11725 DW_FORM_dataN for a constant with a signed type, where the type is
11726 wider than the data. The conclusion of a discussion on the DWARF
11727 list was that this is unspecified. We choose to always zero-extend
11728 because that is the interpretation long in use by GCC. */
c906108c 11729
98bfdba5
PA
11730static gdb_byte *
11731dwarf2_const_value_data (struct attribute *attr, struct type *type,
11732 const char *name, struct obstack *obstack,
11733 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11734{
e7c27a73 11735 struct objfile *objfile = cu->objfile;
e17a4113
UW
11736 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11737 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11738 LONGEST l = DW_UNSND (attr);
11739
11740 if (bits < sizeof (*value) * 8)
11741 {
11742 l &= ((LONGEST) 1 << bits) - 1;
11743 *value = l;
11744 }
11745 else if (bits == sizeof (*value) * 8)
11746 *value = l;
11747 else
11748 {
11749 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11750 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11751 return bytes;
11752 }
11753
11754 return NULL;
11755}
11756
11757/* Read a constant value from an attribute. Either set *VALUE, or if
11758 the value does not fit in *VALUE, set *BYTES - either already
11759 allocated on the objfile obstack, or newly allocated on OBSTACK,
11760 or, set *BATON, if we translated the constant to a location
11761 expression. */
11762
11763static void
11764dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11765 const char *name, struct obstack *obstack,
11766 struct dwarf2_cu *cu,
11767 long *value, gdb_byte **bytes,
11768 struct dwarf2_locexpr_baton **baton)
11769{
11770 struct objfile *objfile = cu->objfile;
11771 struct comp_unit_head *cu_header = &cu->header;
c906108c 11772 struct dwarf_block *blk;
98bfdba5
PA
11773 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11774 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11775
11776 *value = 0;
11777 *bytes = NULL;
11778 *baton = NULL;
c906108c
SS
11779
11780 switch (attr->form)
11781 {
11782 case DW_FORM_addr:
ac56253d 11783 {
ac56253d
TT
11784 gdb_byte *data;
11785
98bfdba5
PA
11786 if (TYPE_LENGTH (type) != cu_header->addr_size)
11787 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11788 cu_header->addr_size,
98bfdba5 11789 TYPE_LENGTH (type));
ac56253d
TT
11790 /* Symbols of this form are reasonably rare, so we just
11791 piggyback on the existing location code rather than writing
11792 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11793 *baton = obstack_alloc (&objfile->objfile_obstack,
11794 sizeof (struct dwarf2_locexpr_baton));
11795 (*baton)->per_cu = cu->per_cu;
11796 gdb_assert ((*baton)->per_cu);
ac56253d 11797
98bfdba5
PA
11798 (*baton)->size = 2 + cu_header->addr_size;
11799 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11800 (*baton)->data = data;
ac56253d
TT
11801
11802 data[0] = DW_OP_addr;
11803 store_unsigned_integer (&data[1], cu_header->addr_size,
11804 byte_order, DW_ADDR (attr));
11805 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11806 }
c906108c 11807 break;
4ac36638 11808 case DW_FORM_string:
93b5768b 11809 case DW_FORM_strp:
98bfdba5
PA
11810 /* DW_STRING is already allocated on the objfile obstack, point
11811 directly to it. */
11812 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11813 break;
c906108c
SS
11814 case DW_FORM_block1:
11815 case DW_FORM_block2:
11816 case DW_FORM_block4:
11817 case DW_FORM_block:
2dc7f7b3 11818 case DW_FORM_exprloc:
c906108c 11819 blk = DW_BLOCK (attr);
98bfdba5
PA
11820 if (TYPE_LENGTH (type) != blk->size)
11821 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11822 TYPE_LENGTH (type));
11823 *bytes = blk->data;
c906108c 11824 break;
2df3850c
JM
11825
11826 /* The DW_AT_const_value attributes are supposed to carry the
11827 symbol's value "represented as it would be on the target
11828 architecture." By the time we get here, it's already been
11829 converted to host endianness, so we just need to sign- or
11830 zero-extend it as appropriate. */
11831 case DW_FORM_data1:
3e43a32a
MS
11832 *bytes = dwarf2_const_value_data (attr, type, name,
11833 obstack, cu, value, 8);
2df3850c 11834 break;
c906108c 11835 case DW_FORM_data2:
3e43a32a
MS
11836 *bytes = dwarf2_const_value_data (attr, type, name,
11837 obstack, cu, value, 16);
2df3850c 11838 break;
c906108c 11839 case DW_FORM_data4:
3e43a32a
MS
11840 *bytes = dwarf2_const_value_data (attr, type, name,
11841 obstack, cu, value, 32);
2df3850c 11842 break;
c906108c 11843 case DW_FORM_data8:
3e43a32a
MS
11844 *bytes = dwarf2_const_value_data (attr, type, name,
11845 obstack, cu, value, 64);
2df3850c
JM
11846 break;
11847
c906108c 11848 case DW_FORM_sdata:
98bfdba5 11849 *value = DW_SND (attr);
2df3850c
JM
11850 break;
11851
c906108c 11852 case DW_FORM_udata:
98bfdba5 11853 *value = DW_UNSND (attr);
c906108c 11854 break;
2df3850c 11855
c906108c 11856 default:
4d3c2250 11857 complaint (&symfile_complaints,
e2e0b3e5 11858 _("unsupported const value attribute form: '%s'"),
4d3c2250 11859 dwarf_form_name (attr->form));
98bfdba5 11860 *value = 0;
c906108c
SS
11861 break;
11862 }
11863}
11864
2df3850c 11865
98bfdba5
PA
11866/* Copy constant value from an attribute to a symbol. */
11867
2df3850c 11868static void
98bfdba5
PA
11869dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11870 struct dwarf2_cu *cu)
2df3850c 11871{
98bfdba5
PA
11872 struct objfile *objfile = cu->objfile;
11873 struct comp_unit_head *cu_header = &cu->header;
11874 long value;
11875 gdb_byte *bytes;
11876 struct dwarf2_locexpr_baton *baton;
2df3850c 11877
98bfdba5
PA
11878 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11879 SYMBOL_PRINT_NAME (sym),
11880 &objfile->objfile_obstack, cu,
11881 &value, &bytes, &baton);
2df3850c 11882
98bfdba5
PA
11883 if (baton != NULL)
11884 {
11885 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11886 SYMBOL_LOCATION_BATON (sym) = baton;
11887 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11888 }
11889 else if (bytes != NULL)
11890 {
11891 SYMBOL_VALUE_BYTES (sym) = bytes;
11892 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11893 }
11894 else
11895 {
11896 SYMBOL_VALUE (sym) = value;
11897 SYMBOL_CLASS (sym) = LOC_CONST;
11898 }
2df3850c
JM
11899}
11900
c906108c
SS
11901/* Return the type of the die in question using its DW_AT_type attribute. */
11902
11903static struct type *
e7c27a73 11904die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11905{
c906108c 11906 struct attribute *type_attr;
c906108c 11907
e142c38c 11908 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11909 if (!type_attr)
11910 {
11911 /* A missing DW_AT_type represents a void type. */
46bf5051 11912 return objfile_type (cu->objfile)->builtin_void;
c906108c 11913 }
348e048f 11914
673bfd45 11915 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11916}
11917
b4ba55a1
JB
11918/* True iff CU's producer generates GNAT Ada auxiliary information
11919 that allows to find parallel types through that information instead
11920 of having to do expensive parallel lookups by type name. */
11921
11922static int
11923need_gnat_info (struct dwarf2_cu *cu)
11924{
11925 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11926 of GNAT produces this auxiliary information, without any indication
11927 that it is produced. Part of enhancing the FSF version of GNAT
11928 to produce that information will be to put in place an indicator
11929 that we can use in order to determine whether the descriptive type
11930 info is available or not. One suggestion that has been made is
11931 to use a new attribute, attached to the CU die. For now, assume
11932 that the descriptive type info is not available. */
11933 return 0;
11934}
11935
b4ba55a1
JB
11936/* Return the auxiliary type of the die in question using its
11937 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11938 attribute is not present. */
11939
11940static struct type *
11941die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11942{
b4ba55a1 11943 struct attribute *type_attr;
b4ba55a1
JB
11944
11945 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11946 if (!type_attr)
11947 return NULL;
11948
673bfd45 11949 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11950}
11951
11952/* If DIE has a descriptive_type attribute, then set the TYPE's
11953 descriptive type accordingly. */
11954
11955static void
11956set_descriptive_type (struct type *type, struct die_info *die,
11957 struct dwarf2_cu *cu)
11958{
11959 struct type *descriptive_type = die_descriptive_type (die, cu);
11960
11961 if (descriptive_type)
11962 {
11963 ALLOCATE_GNAT_AUX_TYPE (type);
11964 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11965 }
11966}
11967
c906108c
SS
11968/* Return the containing type of the die in question using its
11969 DW_AT_containing_type attribute. */
11970
11971static struct type *
e7c27a73 11972die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11973{
c906108c 11974 struct attribute *type_attr;
c906108c 11975
e142c38c 11976 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11977 if (!type_attr)
11978 error (_("Dwarf Error: Problem turning containing type into gdb type "
11979 "[in module %s]"), cu->objfile->name);
11980
673bfd45 11981 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11982}
11983
673bfd45
DE
11984/* Look up the type of DIE in CU using its type attribute ATTR.
11985 If there is no type substitute an error marker. */
11986
c906108c 11987static struct type *
673bfd45
DE
11988lookup_die_type (struct die_info *die, struct attribute *attr,
11989 struct dwarf2_cu *cu)
c906108c 11990{
f792889a
DJ
11991 struct type *this_type;
11992
673bfd45
DE
11993 /* First see if we have it cached. */
11994
11995 if (is_ref_attr (attr))
11996 {
11997 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11998
11999 this_type = get_die_type_at_offset (offset, cu->per_cu);
12000 }
55f1336d 12001 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
12002 {
12003 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12004 struct dwarf2_cu *sig_cu;
12005 unsigned int offset;
12006
12007 /* sig_type will be NULL if the signatured type is missing from
12008 the debug info. */
12009 if (sig_type == NULL)
12010 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12011 "at 0x%x [in module %s]"),
12012 die->offset, cu->objfile->name);
12013
8b70b953 12014 gdb_assert (sig_type->per_cu.debug_type_section);
b3c8eb43 12015 offset = sig_type->per_cu.offset + sig_type->type_offset;
673bfd45
DE
12016 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12017 }
12018 else
12019 {
12020 dump_die_for_error (die);
12021 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12022 dwarf_attr_name (attr->name), cu->objfile->name);
12023 }
12024
12025 /* If not cached we need to read it in. */
12026
12027 if (this_type == NULL)
12028 {
12029 struct die_info *type_die;
12030 struct dwarf2_cu *type_cu = cu;
12031
12032 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12033 /* If the type is cached, we should have found it above. */
12034 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12035 this_type = read_type_die_1 (type_die, type_cu);
12036 }
12037
12038 /* If we still don't have a type use an error marker. */
12039
12040 if (this_type == NULL)
c906108c 12041 {
b00fdb78
TT
12042 char *message, *saved;
12043
12044 /* read_type_die already issued a complaint. */
12045 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12046 cu->objfile->name,
12047 cu->header.offset,
12048 die->offset);
12049 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
12050 message, strlen (message));
12051 xfree (message);
12052
12053 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 12054 }
673bfd45 12055
f792889a 12056 return this_type;
c906108c
SS
12057}
12058
673bfd45
DE
12059/* Return the type in DIE, CU.
12060 Returns NULL for invalid types.
12061
12062 This first does a lookup in the appropriate type_hash table,
12063 and only reads the die in if necessary.
12064
12065 NOTE: This can be called when reading in partial or full symbols. */
12066
f792889a 12067static struct type *
e7c27a73 12068read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12069{
f792889a
DJ
12070 struct type *this_type;
12071
12072 this_type = get_die_type (die, cu);
12073 if (this_type)
12074 return this_type;
12075
673bfd45
DE
12076 return read_type_die_1 (die, cu);
12077}
12078
12079/* Read the type in DIE, CU.
12080 Returns NULL for invalid types. */
12081
12082static struct type *
12083read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12084{
12085 struct type *this_type = NULL;
12086
c906108c
SS
12087 switch (die->tag)
12088 {
12089 case DW_TAG_class_type:
680b30c7 12090 case DW_TAG_interface_type:
c906108c
SS
12091 case DW_TAG_structure_type:
12092 case DW_TAG_union_type:
f792889a 12093 this_type = read_structure_type (die, cu);
c906108c
SS
12094 break;
12095 case DW_TAG_enumeration_type:
f792889a 12096 this_type = read_enumeration_type (die, cu);
c906108c
SS
12097 break;
12098 case DW_TAG_subprogram:
12099 case DW_TAG_subroutine_type:
edb3359d 12100 case DW_TAG_inlined_subroutine:
f792889a 12101 this_type = read_subroutine_type (die, cu);
c906108c
SS
12102 break;
12103 case DW_TAG_array_type:
f792889a 12104 this_type = read_array_type (die, cu);
c906108c 12105 break;
72019c9c 12106 case DW_TAG_set_type:
f792889a 12107 this_type = read_set_type (die, cu);
72019c9c 12108 break;
c906108c 12109 case DW_TAG_pointer_type:
f792889a 12110 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12111 break;
12112 case DW_TAG_ptr_to_member_type:
f792889a 12113 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12114 break;
12115 case DW_TAG_reference_type:
f792889a 12116 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12117 break;
12118 case DW_TAG_const_type:
f792889a 12119 this_type = read_tag_const_type (die, cu);
c906108c
SS
12120 break;
12121 case DW_TAG_volatile_type:
f792889a 12122 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12123 break;
12124 case DW_TAG_string_type:
f792889a 12125 this_type = read_tag_string_type (die, cu);
c906108c
SS
12126 break;
12127 case DW_TAG_typedef:
f792889a 12128 this_type = read_typedef (die, cu);
c906108c 12129 break;
a02abb62 12130 case DW_TAG_subrange_type:
f792889a 12131 this_type = read_subrange_type (die, cu);
a02abb62 12132 break;
c906108c 12133 case DW_TAG_base_type:
f792889a 12134 this_type = read_base_type (die, cu);
c906108c 12135 break;
81a17f79 12136 case DW_TAG_unspecified_type:
f792889a 12137 this_type = read_unspecified_type (die, cu);
81a17f79 12138 break;
0114d602
DJ
12139 case DW_TAG_namespace:
12140 this_type = read_namespace_type (die, cu);
12141 break;
f55ee35c
JK
12142 case DW_TAG_module:
12143 this_type = read_module_type (die, cu);
12144 break;
c906108c 12145 default:
3e43a32a
MS
12146 complaint (&symfile_complaints,
12147 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12148 dwarf_tag_name (die->tag));
c906108c
SS
12149 break;
12150 }
63d06c5c 12151
f792889a 12152 return this_type;
63d06c5c
DC
12153}
12154
abc72ce4
DE
12155/* See if we can figure out if the class lives in a namespace. We do
12156 this by looking for a member function; its demangled name will
12157 contain namespace info, if there is any.
12158 Return the computed name or NULL.
12159 Space for the result is allocated on the objfile's obstack.
12160 This is the full-die version of guess_partial_die_structure_name.
12161 In this case we know DIE has no useful parent. */
12162
12163static char *
12164guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12165{
12166 struct die_info *spec_die;
12167 struct dwarf2_cu *spec_cu;
12168 struct die_info *child;
12169
12170 spec_cu = cu;
12171 spec_die = die_specification (die, &spec_cu);
12172 if (spec_die != NULL)
12173 {
12174 die = spec_die;
12175 cu = spec_cu;
12176 }
12177
12178 for (child = die->child;
12179 child != NULL;
12180 child = child->sibling)
12181 {
12182 if (child->tag == DW_TAG_subprogram)
12183 {
12184 struct attribute *attr;
12185
12186 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12187 if (attr == NULL)
12188 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12189 if (attr != NULL)
12190 {
12191 char *actual_name
12192 = language_class_name_from_physname (cu->language_defn,
12193 DW_STRING (attr));
12194 char *name = NULL;
12195
12196 if (actual_name != NULL)
12197 {
12198 char *die_name = dwarf2_name (die, cu);
12199
12200 if (die_name != NULL
12201 && strcmp (die_name, actual_name) != 0)
12202 {
12203 /* Strip off the class name from the full name.
12204 We want the prefix. */
12205 int die_name_len = strlen (die_name);
12206 int actual_name_len = strlen (actual_name);
12207
12208 /* Test for '::' as a sanity check. */
12209 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12210 && actual_name[actual_name_len
12211 - die_name_len - 1] == ':')
abc72ce4
DE
12212 name =
12213 obsavestring (actual_name,
12214 actual_name_len - die_name_len - 2,
12215 &cu->objfile->objfile_obstack);
12216 }
12217 }
12218 xfree (actual_name);
12219 return name;
12220 }
12221 }
12222 }
12223
12224 return NULL;
12225}
12226
fdde2d81 12227/* Return the name of the namespace/class that DIE is defined within,
0114d602 12228 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12229
0114d602
DJ
12230 For example, if we're within the method foo() in the following
12231 code:
12232
12233 namespace N {
12234 class C {
12235 void foo () {
12236 }
12237 };
12238 }
12239
12240 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
12241
12242static char *
e142c38c 12243determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12244{
0114d602
DJ
12245 struct die_info *parent, *spec_die;
12246 struct dwarf2_cu *spec_cu;
12247 struct type *parent_type;
63d06c5c 12248
f55ee35c
JK
12249 if (cu->language != language_cplus && cu->language != language_java
12250 && cu->language != language_fortran)
0114d602
DJ
12251 return "";
12252
12253 /* We have to be careful in the presence of DW_AT_specification.
12254 For example, with GCC 3.4, given the code
12255
12256 namespace N {
12257 void foo() {
12258 // Definition of N::foo.
12259 }
12260 }
12261
12262 then we'll have a tree of DIEs like this:
12263
12264 1: DW_TAG_compile_unit
12265 2: DW_TAG_namespace // N
12266 3: DW_TAG_subprogram // declaration of N::foo
12267 4: DW_TAG_subprogram // definition of N::foo
12268 DW_AT_specification // refers to die #3
12269
12270 Thus, when processing die #4, we have to pretend that we're in
12271 the context of its DW_AT_specification, namely the contex of die
12272 #3. */
12273 spec_cu = cu;
12274 spec_die = die_specification (die, &spec_cu);
12275 if (spec_die == NULL)
12276 parent = die->parent;
12277 else
63d06c5c 12278 {
0114d602
DJ
12279 parent = spec_die->parent;
12280 cu = spec_cu;
63d06c5c 12281 }
0114d602
DJ
12282
12283 if (parent == NULL)
12284 return "";
98bfdba5
PA
12285 else if (parent->building_fullname)
12286 {
12287 const char *name;
12288 const char *parent_name;
12289
12290 /* It has been seen on RealView 2.2 built binaries,
12291 DW_TAG_template_type_param types actually _defined_ as
12292 children of the parent class:
12293
12294 enum E {};
12295 template class <class Enum> Class{};
12296 Class<enum E> class_e;
12297
12298 1: DW_TAG_class_type (Class)
12299 2: DW_TAG_enumeration_type (E)
12300 3: DW_TAG_enumerator (enum1:0)
12301 3: DW_TAG_enumerator (enum2:1)
12302 ...
12303 2: DW_TAG_template_type_param
12304 DW_AT_type DW_FORM_ref_udata (E)
12305
12306 Besides being broken debug info, it can put GDB into an
12307 infinite loop. Consider:
12308
12309 When we're building the full name for Class<E>, we'll start
12310 at Class, and go look over its template type parameters,
12311 finding E. We'll then try to build the full name of E, and
12312 reach here. We're now trying to build the full name of E,
12313 and look over the parent DIE for containing scope. In the
12314 broken case, if we followed the parent DIE of E, we'd again
12315 find Class, and once again go look at its template type
12316 arguments, etc., etc. Simply don't consider such parent die
12317 as source-level parent of this die (it can't be, the language
12318 doesn't allow it), and break the loop here. */
12319 name = dwarf2_name (die, cu);
12320 parent_name = dwarf2_name (parent, cu);
12321 complaint (&symfile_complaints,
12322 _("template param type '%s' defined within parent '%s'"),
12323 name ? name : "<unknown>",
12324 parent_name ? parent_name : "<unknown>");
12325 return "";
12326 }
63d06c5c 12327 else
0114d602
DJ
12328 switch (parent->tag)
12329 {
63d06c5c 12330 case DW_TAG_namespace:
0114d602 12331 parent_type = read_type_die (parent, cu);
acebe513
UW
12332 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12333 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12334 Work around this problem here. */
12335 if (cu->language == language_cplus
12336 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12337 return "";
0114d602
DJ
12338 /* We give a name to even anonymous namespaces. */
12339 return TYPE_TAG_NAME (parent_type);
63d06c5c 12340 case DW_TAG_class_type:
680b30c7 12341 case DW_TAG_interface_type:
63d06c5c 12342 case DW_TAG_structure_type:
0114d602 12343 case DW_TAG_union_type:
f55ee35c 12344 case DW_TAG_module:
0114d602
DJ
12345 parent_type = read_type_die (parent, cu);
12346 if (TYPE_TAG_NAME (parent_type) != NULL)
12347 return TYPE_TAG_NAME (parent_type);
12348 else
12349 /* An anonymous structure is only allowed non-static data
12350 members; no typedefs, no member functions, et cetera.
12351 So it does not need a prefix. */
12352 return "";
abc72ce4
DE
12353 case DW_TAG_compile_unit:
12354 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12355 if (cu->language == language_cplus
8b70b953 12356 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12357 && die->child != NULL
12358 && (die->tag == DW_TAG_class_type
12359 || die->tag == DW_TAG_structure_type
12360 || die->tag == DW_TAG_union_type))
12361 {
12362 char *name = guess_full_die_structure_name (die, cu);
12363 if (name != NULL)
12364 return name;
12365 }
12366 return "";
63d06c5c 12367 default:
8176b9b8 12368 return determine_prefix (parent, cu);
63d06c5c 12369 }
63d06c5c
DC
12370}
12371
3e43a32a
MS
12372/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12373 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12374 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12375 an obconcat, otherwise allocate storage for the result. The CU argument is
12376 used to determine the language and hence, the appropriate separator. */
987504bb 12377
f55ee35c 12378#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12379
12380static char *
f55ee35c
JK
12381typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12382 int physname, struct dwarf2_cu *cu)
63d06c5c 12383{
f55ee35c 12384 const char *lead = "";
5c315b68 12385 const char *sep;
63d06c5c 12386
3e43a32a
MS
12387 if (suffix == NULL || suffix[0] == '\0'
12388 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12389 sep = "";
12390 else if (cu->language == language_java)
12391 sep = ".";
f55ee35c
JK
12392 else if (cu->language == language_fortran && physname)
12393 {
12394 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12395 DW_AT_MIPS_linkage_name is preferred and used instead. */
12396
12397 lead = "__";
12398 sep = "_MOD_";
12399 }
987504bb
JJ
12400 else
12401 sep = "::";
63d06c5c 12402
6dd47d34
DE
12403 if (prefix == NULL)
12404 prefix = "";
12405 if (suffix == NULL)
12406 suffix = "";
12407
987504bb
JJ
12408 if (obs == NULL)
12409 {
3e43a32a
MS
12410 char *retval
12411 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12412
f55ee35c
JK
12413 strcpy (retval, lead);
12414 strcat (retval, prefix);
6dd47d34
DE
12415 strcat (retval, sep);
12416 strcat (retval, suffix);
63d06c5c
DC
12417 return retval;
12418 }
987504bb
JJ
12419 else
12420 {
12421 /* We have an obstack. */
f55ee35c 12422 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12423 }
63d06c5c
DC
12424}
12425
c906108c
SS
12426/* Return sibling of die, NULL if no sibling. */
12427
f9aca02d 12428static struct die_info *
fba45db2 12429sibling_die (struct die_info *die)
c906108c 12430{
639d11d3 12431 return die->sibling;
c906108c
SS
12432}
12433
71c25dea
TT
12434/* Get name of a die, return NULL if not found. */
12435
12436static char *
12437dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12438 struct obstack *obstack)
12439{
12440 if (name && cu->language == language_cplus)
12441 {
12442 char *canon_name = cp_canonicalize_string (name);
12443
12444 if (canon_name != NULL)
12445 {
12446 if (strcmp (canon_name, name) != 0)
12447 name = obsavestring (canon_name, strlen (canon_name),
12448 obstack);
12449 xfree (canon_name);
12450 }
12451 }
12452
12453 return name;
c906108c
SS
12454}
12455
9219021c
DC
12456/* Get name of a die, return NULL if not found. */
12457
12458static char *
e142c38c 12459dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12460{
12461 struct attribute *attr;
12462
e142c38c 12463 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12464 if ((!attr || !DW_STRING (attr))
12465 && die->tag != DW_TAG_class_type
12466 && die->tag != DW_TAG_interface_type
12467 && die->tag != DW_TAG_structure_type
12468 && die->tag != DW_TAG_union_type)
71c25dea
TT
12469 return NULL;
12470
12471 switch (die->tag)
12472 {
12473 case DW_TAG_compile_unit:
12474 /* Compilation units have a DW_AT_name that is a filename, not
12475 a source language identifier. */
12476 case DW_TAG_enumeration_type:
12477 case DW_TAG_enumerator:
12478 /* These tags always have simple identifiers already; no need
12479 to canonicalize them. */
12480 return DW_STRING (attr);
907af001 12481
418835cc
KS
12482 case DW_TAG_subprogram:
12483 /* Java constructors will all be named "<init>", so return
12484 the class name when we see this special case. */
12485 if (cu->language == language_java
12486 && DW_STRING (attr) != NULL
12487 && strcmp (DW_STRING (attr), "<init>") == 0)
12488 {
12489 struct dwarf2_cu *spec_cu = cu;
12490 struct die_info *spec_die;
12491
12492 /* GCJ will output '<init>' for Java constructor names.
12493 For this special case, return the name of the parent class. */
12494
12495 /* GCJ may output suprogram DIEs with AT_specification set.
12496 If so, use the name of the specified DIE. */
12497 spec_die = die_specification (die, &spec_cu);
12498 if (spec_die != NULL)
12499 return dwarf2_name (spec_die, spec_cu);
12500
12501 do
12502 {
12503 die = die->parent;
12504 if (die->tag == DW_TAG_class_type)
12505 return dwarf2_name (die, cu);
12506 }
12507 while (die->tag != DW_TAG_compile_unit);
12508 }
907af001
UW
12509 break;
12510
12511 case DW_TAG_class_type:
12512 case DW_TAG_interface_type:
12513 case DW_TAG_structure_type:
12514 case DW_TAG_union_type:
12515 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12516 structures or unions. These were of the form "._%d" in GCC 4.1,
12517 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12518 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12519 if (attr && DW_STRING (attr)
12520 && (strncmp (DW_STRING (attr), "._", 2) == 0
12521 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12522 return NULL;
53832f31
TT
12523
12524 /* GCC might emit a nameless typedef that has a linkage name. See
12525 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12526 if (!attr || DW_STRING (attr) == NULL)
12527 {
df5c6c50 12528 char *demangled = NULL;
53832f31
TT
12529
12530 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12531 if (attr == NULL)
12532 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12533
12534 if (attr == NULL || DW_STRING (attr) == NULL)
12535 return NULL;
12536
df5c6c50
JK
12537 /* Avoid demangling DW_STRING (attr) the second time on a second
12538 call for the same DIE. */
12539 if (!DW_STRING_IS_CANONICAL (attr))
12540 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12541
12542 if (demangled)
12543 {
12544 /* FIXME: we already did this for the partial symbol... */
12545 DW_STRING (attr)
12546 = obsavestring (demangled, strlen (demangled),
12547 &cu->objfile->objfile_obstack);
12548 DW_STRING_IS_CANONICAL (attr) = 1;
12549 xfree (demangled);
12550 }
12551 }
907af001
UW
12552 break;
12553
71c25dea 12554 default:
907af001
UW
12555 break;
12556 }
12557
12558 if (!DW_STRING_IS_CANONICAL (attr))
12559 {
12560 DW_STRING (attr)
12561 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12562 &cu->objfile->objfile_obstack);
12563 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12564 }
907af001 12565 return DW_STRING (attr);
9219021c
DC
12566}
12567
12568/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12569 is none. *EXT_CU is the CU containing DIE on input, and the CU
12570 containing the return value on output. */
9219021c
DC
12571
12572static struct die_info *
f2f0e013 12573dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12574{
12575 struct attribute *attr;
9219021c 12576
f2f0e013 12577 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12578 if (attr == NULL)
12579 return NULL;
12580
f2f0e013 12581 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12582}
12583
c906108c
SS
12584/* Convert a DIE tag into its string name. */
12585
12586static char *
aa1ee363 12587dwarf_tag_name (unsigned tag)
c906108c
SS
12588{
12589 switch (tag)
12590 {
12591 case DW_TAG_padding:
12592 return "DW_TAG_padding";
12593 case DW_TAG_array_type:
12594 return "DW_TAG_array_type";
12595 case DW_TAG_class_type:
12596 return "DW_TAG_class_type";
12597 case DW_TAG_entry_point:
12598 return "DW_TAG_entry_point";
12599 case DW_TAG_enumeration_type:
12600 return "DW_TAG_enumeration_type";
12601 case DW_TAG_formal_parameter:
12602 return "DW_TAG_formal_parameter";
12603 case DW_TAG_imported_declaration:
12604 return "DW_TAG_imported_declaration";
12605 case DW_TAG_label:
12606 return "DW_TAG_label";
12607 case DW_TAG_lexical_block:
12608 return "DW_TAG_lexical_block";
12609 case DW_TAG_member:
12610 return "DW_TAG_member";
12611 case DW_TAG_pointer_type:
12612 return "DW_TAG_pointer_type";
12613 case DW_TAG_reference_type:
12614 return "DW_TAG_reference_type";
12615 case DW_TAG_compile_unit:
12616 return "DW_TAG_compile_unit";
12617 case DW_TAG_string_type:
12618 return "DW_TAG_string_type";
12619 case DW_TAG_structure_type:
12620 return "DW_TAG_structure_type";
12621 case DW_TAG_subroutine_type:
12622 return "DW_TAG_subroutine_type";
12623 case DW_TAG_typedef:
12624 return "DW_TAG_typedef";
12625 case DW_TAG_union_type:
12626 return "DW_TAG_union_type";
12627 case DW_TAG_unspecified_parameters:
12628 return "DW_TAG_unspecified_parameters";
12629 case DW_TAG_variant:
12630 return "DW_TAG_variant";
12631 case DW_TAG_common_block:
12632 return "DW_TAG_common_block";
12633 case DW_TAG_common_inclusion:
12634 return "DW_TAG_common_inclusion";
12635 case DW_TAG_inheritance:
12636 return "DW_TAG_inheritance";
12637 case DW_TAG_inlined_subroutine:
12638 return "DW_TAG_inlined_subroutine";
12639 case DW_TAG_module:
12640 return "DW_TAG_module";
12641 case DW_TAG_ptr_to_member_type:
12642 return "DW_TAG_ptr_to_member_type";
12643 case DW_TAG_set_type:
12644 return "DW_TAG_set_type";
12645 case DW_TAG_subrange_type:
12646 return "DW_TAG_subrange_type";
12647 case DW_TAG_with_stmt:
12648 return "DW_TAG_with_stmt";
12649 case DW_TAG_access_declaration:
12650 return "DW_TAG_access_declaration";
12651 case DW_TAG_base_type:
12652 return "DW_TAG_base_type";
12653 case DW_TAG_catch_block:
12654 return "DW_TAG_catch_block";
12655 case DW_TAG_const_type:
12656 return "DW_TAG_const_type";
12657 case DW_TAG_constant:
12658 return "DW_TAG_constant";
12659 case DW_TAG_enumerator:
12660 return "DW_TAG_enumerator";
12661 case DW_TAG_file_type:
12662 return "DW_TAG_file_type";
12663 case DW_TAG_friend:
12664 return "DW_TAG_friend";
12665 case DW_TAG_namelist:
12666 return "DW_TAG_namelist";
12667 case DW_TAG_namelist_item:
12668 return "DW_TAG_namelist_item";
12669 case DW_TAG_packed_type:
12670 return "DW_TAG_packed_type";
12671 case DW_TAG_subprogram:
12672 return "DW_TAG_subprogram";
12673 case DW_TAG_template_type_param:
12674 return "DW_TAG_template_type_param";
12675 case DW_TAG_template_value_param:
12676 return "DW_TAG_template_value_param";
12677 case DW_TAG_thrown_type:
12678 return "DW_TAG_thrown_type";
12679 case DW_TAG_try_block:
12680 return "DW_TAG_try_block";
12681 case DW_TAG_variant_part:
12682 return "DW_TAG_variant_part";
12683 case DW_TAG_variable:
12684 return "DW_TAG_variable";
12685 case DW_TAG_volatile_type:
12686 return "DW_TAG_volatile_type";
d9fa45fe
DC
12687 case DW_TAG_dwarf_procedure:
12688 return "DW_TAG_dwarf_procedure";
12689 case DW_TAG_restrict_type:
12690 return "DW_TAG_restrict_type";
12691 case DW_TAG_interface_type:
12692 return "DW_TAG_interface_type";
12693 case DW_TAG_namespace:
12694 return "DW_TAG_namespace";
12695 case DW_TAG_imported_module:
12696 return "DW_TAG_imported_module";
12697 case DW_TAG_unspecified_type:
12698 return "DW_TAG_unspecified_type";
12699 case DW_TAG_partial_unit:
12700 return "DW_TAG_partial_unit";
12701 case DW_TAG_imported_unit:
12702 return "DW_TAG_imported_unit";
b7619582
GF
12703 case DW_TAG_condition:
12704 return "DW_TAG_condition";
12705 case DW_TAG_shared_type:
12706 return "DW_TAG_shared_type";
348e048f
DE
12707 case DW_TAG_type_unit:
12708 return "DW_TAG_type_unit";
c906108c
SS
12709 case DW_TAG_MIPS_loop:
12710 return "DW_TAG_MIPS_loop";
b7619582
GF
12711 case DW_TAG_HP_array_descriptor:
12712 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12713 case DW_TAG_format_label:
12714 return "DW_TAG_format_label";
12715 case DW_TAG_function_template:
12716 return "DW_TAG_function_template";
12717 case DW_TAG_class_template:
12718 return "DW_TAG_class_template";
b7619582
GF
12719 case DW_TAG_GNU_BINCL:
12720 return "DW_TAG_GNU_BINCL";
12721 case DW_TAG_GNU_EINCL:
12722 return "DW_TAG_GNU_EINCL";
12723 case DW_TAG_upc_shared_type:
12724 return "DW_TAG_upc_shared_type";
12725 case DW_TAG_upc_strict_type:
12726 return "DW_TAG_upc_strict_type";
12727 case DW_TAG_upc_relaxed_type:
12728 return "DW_TAG_upc_relaxed_type";
12729 case DW_TAG_PGI_kanji_type:
12730 return "DW_TAG_PGI_kanji_type";
12731 case DW_TAG_PGI_interface_block:
12732 return "DW_TAG_PGI_interface_block";
c906108c
SS
12733 default:
12734 return "DW_TAG_<unknown>";
12735 }
12736}
12737
12738/* Convert a DWARF attribute code into its string name. */
12739
12740static char *
aa1ee363 12741dwarf_attr_name (unsigned attr)
c906108c
SS
12742{
12743 switch (attr)
12744 {
12745 case DW_AT_sibling:
12746 return "DW_AT_sibling";
12747 case DW_AT_location:
12748 return "DW_AT_location";
12749 case DW_AT_name:
12750 return "DW_AT_name";
12751 case DW_AT_ordering:
12752 return "DW_AT_ordering";
12753 case DW_AT_subscr_data:
12754 return "DW_AT_subscr_data";
12755 case DW_AT_byte_size:
12756 return "DW_AT_byte_size";
12757 case DW_AT_bit_offset:
12758 return "DW_AT_bit_offset";
12759 case DW_AT_bit_size:
12760 return "DW_AT_bit_size";
12761 case DW_AT_element_list:
12762 return "DW_AT_element_list";
12763 case DW_AT_stmt_list:
12764 return "DW_AT_stmt_list";
12765 case DW_AT_low_pc:
12766 return "DW_AT_low_pc";
12767 case DW_AT_high_pc:
12768 return "DW_AT_high_pc";
12769 case DW_AT_language:
12770 return "DW_AT_language";
12771 case DW_AT_member:
12772 return "DW_AT_member";
12773 case DW_AT_discr:
12774 return "DW_AT_discr";
12775 case DW_AT_discr_value:
12776 return "DW_AT_discr_value";
12777 case DW_AT_visibility:
12778 return "DW_AT_visibility";
12779 case DW_AT_import:
12780 return "DW_AT_import";
12781 case DW_AT_string_length:
12782 return "DW_AT_string_length";
12783 case DW_AT_common_reference:
12784 return "DW_AT_common_reference";
12785 case DW_AT_comp_dir:
12786 return "DW_AT_comp_dir";
12787 case DW_AT_const_value:
12788 return "DW_AT_const_value";
12789 case DW_AT_containing_type:
12790 return "DW_AT_containing_type";
12791 case DW_AT_default_value:
12792 return "DW_AT_default_value";
12793 case DW_AT_inline:
12794 return "DW_AT_inline";
12795 case DW_AT_is_optional:
12796 return "DW_AT_is_optional";
12797 case DW_AT_lower_bound:
12798 return "DW_AT_lower_bound";
12799 case DW_AT_producer:
12800 return "DW_AT_producer";
12801 case DW_AT_prototyped:
12802 return "DW_AT_prototyped";
12803 case DW_AT_return_addr:
12804 return "DW_AT_return_addr";
12805 case DW_AT_start_scope:
12806 return "DW_AT_start_scope";
09fa0d7c
JK
12807 case DW_AT_bit_stride:
12808 return "DW_AT_bit_stride";
c906108c
SS
12809 case DW_AT_upper_bound:
12810 return "DW_AT_upper_bound";
12811 case DW_AT_abstract_origin:
12812 return "DW_AT_abstract_origin";
12813 case DW_AT_accessibility:
12814 return "DW_AT_accessibility";
12815 case DW_AT_address_class:
12816 return "DW_AT_address_class";
12817 case DW_AT_artificial:
12818 return "DW_AT_artificial";
12819 case DW_AT_base_types:
12820 return "DW_AT_base_types";
12821 case DW_AT_calling_convention:
12822 return "DW_AT_calling_convention";
12823 case DW_AT_count:
12824 return "DW_AT_count";
12825 case DW_AT_data_member_location:
12826 return "DW_AT_data_member_location";
12827 case DW_AT_decl_column:
12828 return "DW_AT_decl_column";
12829 case DW_AT_decl_file:
12830 return "DW_AT_decl_file";
12831 case DW_AT_decl_line:
12832 return "DW_AT_decl_line";
12833 case DW_AT_declaration:
12834 return "DW_AT_declaration";
12835 case DW_AT_discr_list:
12836 return "DW_AT_discr_list";
12837 case DW_AT_encoding:
12838 return "DW_AT_encoding";
12839 case DW_AT_external:
12840 return "DW_AT_external";
12841 case DW_AT_frame_base:
12842 return "DW_AT_frame_base";
12843 case DW_AT_friend:
12844 return "DW_AT_friend";
12845 case DW_AT_identifier_case:
12846 return "DW_AT_identifier_case";
12847 case DW_AT_macro_info:
12848 return "DW_AT_macro_info";
12849 case DW_AT_namelist_items:
12850 return "DW_AT_namelist_items";
12851 case DW_AT_priority:
12852 return "DW_AT_priority";
12853 case DW_AT_segment:
12854 return "DW_AT_segment";
12855 case DW_AT_specification:
12856 return "DW_AT_specification";
12857 case DW_AT_static_link:
12858 return "DW_AT_static_link";
12859 case DW_AT_type:
12860 return "DW_AT_type";
12861 case DW_AT_use_location:
12862 return "DW_AT_use_location";
12863 case DW_AT_variable_parameter:
12864 return "DW_AT_variable_parameter";
12865 case DW_AT_virtuality:
12866 return "DW_AT_virtuality";
12867 case DW_AT_vtable_elem_location:
12868 return "DW_AT_vtable_elem_location";
b7619582 12869 /* DWARF 3 values. */
d9fa45fe
DC
12870 case DW_AT_allocated:
12871 return "DW_AT_allocated";
12872 case DW_AT_associated:
12873 return "DW_AT_associated";
12874 case DW_AT_data_location:
12875 return "DW_AT_data_location";
09fa0d7c
JK
12876 case DW_AT_byte_stride:
12877 return "DW_AT_byte_stride";
d9fa45fe
DC
12878 case DW_AT_entry_pc:
12879 return "DW_AT_entry_pc";
12880 case DW_AT_use_UTF8:
12881 return "DW_AT_use_UTF8";
12882 case DW_AT_extension:
12883 return "DW_AT_extension";
12884 case DW_AT_ranges:
12885 return "DW_AT_ranges";
12886 case DW_AT_trampoline:
12887 return "DW_AT_trampoline";
12888 case DW_AT_call_column:
12889 return "DW_AT_call_column";
12890 case DW_AT_call_file:
12891 return "DW_AT_call_file";
12892 case DW_AT_call_line:
12893 return "DW_AT_call_line";
b7619582
GF
12894 case DW_AT_description:
12895 return "DW_AT_description";
12896 case DW_AT_binary_scale:
12897 return "DW_AT_binary_scale";
12898 case DW_AT_decimal_scale:
12899 return "DW_AT_decimal_scale";
12900 case DW_AT_small:
12901 return "DW_AT_small";
12902 case DW_AT_decimal_sign:
12903 return "DW_AT_decimal_sign";
12904 case DW_AT_digit_count:
12905 return "DW_AT_digit_count";
12906 case DW_AT_picture_string:
12907 return "DW_AT_picture_string";
12908 case DW_AT_mutable:
12909 return "DW_AT_mutable";
12910 case DW_AT_threads_scaled:
12911 return "DW_AT_threads_scaled";
12912 case DW_AT_explicit:
12913 return "DW_AT_explicit";
12914 case DW_AT_object_pointer:
12915 return "DW_AT_object_pointer";
12916 case DW_AT_endianity:
12917 return "DW_AT_endianity";
12918 case DW_AT_elemental:
12919 return "DW_AT_elemental";
12920 case DW_AT_pure:
12921 return "DW_AT_pure";
12922 case DW_AT_recursive:
12923 return "DW_AT_recursive";
348e048f
DE
12924 /* DWARF 4 values. */
12925 case DW_AT_signature:
12926 return "DW_AT_signature";
31ef98ae
TT
12927 case DW_AT_linkage_name:
12928 return "DW_AT_linkage_name";
b7619582 12929 /* SGI/MIPS extensions. */
c764a876 12930#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12931 case DW_AT_MIPS_fde:
12932 return "DW_AT_MIPS_fde";
c764a876 12933#endif
c906108c
SS
12934 case DW_AT_MIPS_loop_begin:
12935 return "DW_AT_MIPS_loop_begin";
12936 case DW_AT_MIPS_tail_loop_begin:
12937 return "DW_AT_MIPS_tail_loop_begin";
12938 case DW_AT_MIPS_epilog_begin:
12939 return "DW_AT_MIPS_epilog_begin";
12940 case DW_AT_MIPS_loop_unroll_factor:
12941 return "DW_AT_MIPS_loop_unroll_factor";
12942 case DW_AT_MIPS_software_pipeline_depth:
12943 return "DW_AT_MIPS_software_pipeline_depth";
12944 case DW_AT_MIPS_linkage_name:
12945 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12946 case DW_AT_MIPS_stride:
12947 return "DW_AT_MIPS_stride";
12948 case DW_AT_MIPS_abstract_name:
12949 return "DW_AT_MIPS_abstract_name";
12950 case DW_AT_MIPS_clone_origin:
12951 return "DW_AT_MIPS_clone_origin";
12952 case DW_AT_MIPS_has_inlines:
12953 return "DW_AT_MIPS_has_inlines";
b7619582 12954 /* HP extensions. */
c764a876 12955#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12956 case DW_AT_HP_block_index:
12957 return "DW_AT_HP_block_index";
c764a876 12958#endif
b7619582
GF
12959 case DW_AT_HP_unmodifiable:
12960 return "DW_AT_HP_unmodifiable";
12961 case DW_AT_HP_actuals_stmt_list:
12962 return "DW_AT_HP_actuals_stmt_list";
12963 case DW_AT_HP_proc_per_section:
12964 return "DW_AT_HP_proc_per_section";
12965 case DW_AT_HP_raw_data_ptr:
12966 return "DW_AT_HP_raw_data_ptr";
12967 case DW_AT_HP_pass_by_reference:
12968 return "DW_AT_HP_pass_by_reference";
12969 case DW_AT_HP_opt_level:
12970 return "DW_AT_HP_opt_level";
12971 case DW_AT_HP_prof_version_id:
12972 return "DW_AT_HP_prof_version_id";
12973 case DW_AT_HP_opt_flags:
12974 return "DW_AT_HP_opt_flags";
12975 case DW_AT_HP_cold_region_low_pc:
12976 return "DW_AT_HP_cold_region_low_pc";
12977 case DW_AT_HP_cold_region_high_pc:
12978 return "DW_AT_HP_cold_region_high_pc";
12979 case DW_AT_HP_all_variables_modifiable:
12980 return "DW_AT_HP_all_variables_modifiable";
12981 case DW_AT_HP_linkage_name:
12982 return "DW_AT_HP_linkage_name";
12983 case DW_AT_HP_prof_flags:
12984 return "DW_AT_HP_prof_flags";
12985 /* GNU extensions. */
c906108c
SS
12986 case DW_AT_sf_names:
12987 return "DW_AT_sf_names";
12988 case DW_AT_src_info:
12989 return "DW_AT_src_info";
12990 case DW_AT_mac_info:
12991 return "DW_AT_mac_info";
12992 case DW_AT_src_coords:
12993 return "DW_AT_src_coords";
12994 case DW_AT_body_begin:
12995 return "DW_AT_body_begin";
12996 case DW_AT_body_end:
12997 return "DW_AT_body_end";
f5f8a009
EZ
12998 case DW_AT_GNU_vector:
12999 return "DW_AT_GNU_vector";
2de00c64
DE
13000 case DW_AT_GNU_odr_signature:
13001 return "DW_AT_GNU_odr_signature";
b7619582
GF
13002 /* VMS extensions. */
13003 case DW_AT_VMS_rtnbeg_pd_address:
13004 return "DW_AT_VMS_rtnbeg_pd_address";
13005 /* UPC extension. */
13006 case DW_AT_upc_threads_scaled:
13007 return "DW_AT_upc_threads_scaled";
13008 /* PGI (STMicroelectronics) extensions. */
13009 case DW_AT_PGI_lbase:
13010 return "DW_AT_PGI_lbase";
13011 case DW_AT_PGI_soffset:
13012 return "DW_AT_PGI_soffset";
13013 case DW_AT_PGI_lstride:
13014 return "DW_AT_PGI_lstride";
c906108c
SS
13015 default:
13016 return "DW_AT_<unknown>";
13017 }
13018}
13019
13020/* Convert a DWARF value form code into its string name. */
13021
13022static char *
aa1ee363 13023dwarf_form_name (unsigned form)
c906108c
SS
13024{
13025 switch (form)
13026 {
13027 case DW_FORM_addr:
13028 return "DW_FORM_addr";
13029 case DW_FORM_block2:
13030 return "DW_FORM_block2";
13031 case DW_FORM_block4:
13032 return "DW_FORM_block4";
13033 case DW_FORM_data2:
13034 return "DW_FORM_data2";
13035 case DW_FORM_data4:
13036 return "DW_FORM_data4";
13037 case DW_FORM_data8:
13038 return "DW_FORM_data8";
13039 case DW_FORM_string:
13040 return "DW_FORM_string";
13041 case DW_FORM_block:
13042 return "DW_FORM_block";
13043 case DW_FORM_block1:
13044 return "DW_FORM_block1";
13045 case DW_FORM_data1:
13046 return "DW_FORM_data1";
13047 case DW_FORM_flag:
13048 return "DW_FORM_flag";
13049 case DW_FORM_sdata:
13050 return "DW_FORM_sdata";
13051 case DW_FORM_strp:
13052 return "DW_FORM_strp";
13053 case DW_FORM_udata:
13054 return "DW_FORM_udata";
13055 case DW_FORM_ref_addr:
13056 return "DW_FORM_ref_addr";
13057 case DW_FORM_ref1:
13058 return "DW_FORM_ref1";
13059 case DW_FORM_ref2:
13060 return "DW_FORM_ref2";
13061 case DW_FORM_ref4:
13062 return "DW_FORM_ref4";
13063 case DW_FORM_ref8:
13064 return "DW_FORM_ref8";
13065 case DW_FORM_ref_udata:
13066 return "DW_FORM_ref_udata";
13067 case DW_FORM_indirect:
13068 return "DW_FORM_indirect";
348e048f
DE
13069 case DW_FORM_sec_offset:
13070 return "DW_FORM_sec_offset";
13071 case DW_FORM_exprloc:
13072 return "DW_FORM_exprloc";
13073 case DW_FORM_flag_present:
13074 return "DW_FORM_flag_present";
55f1336d
TT
13075 case DW_FORM_ref_sig8:
13076 return "DW_FORM_ref_sig8";
c906108c
SS
13077 default:
13078 return "DW_FORM_<unknown>";
13079 }
13080}
13081
13082/* Convert a DWARF stack opcode into its string name. */
13083
9eae7c52 13084const char *
b1bfef65 13085dwarf_stack_op_name (unsigned op)
c906108c
SS
13086{
13087 switch (op)
13088 {
13089 case DW_OP_addr:
13090 return "DW_OP_addr";
13091 case DW_OP_deref:
13092 return "DW_OP_deref";
13093 case DW_OP_const1u:
13094 return "DW_OP_const1u";
13095 case DW_OP_const1s:
13096 return "DW_OP_const1s";
13097 case DW_OP_const2u:
13098 return "DW_OP_const2u";
13099 case DW_OP_const2s:
13100 return "DW_OP_const2s";
13101 case DW_OP_const4u:
13102 return "DW_OP_const4u";
13103 case DW_OP_const4s:
13104 return "DW_OP_const4s";
13105 case DW_OP_const8u:
13106 return "DW_OP_const8u";
13107 case DW_OP_const8s:
13108 return "DW_OP_const8s";
13109 case DW_OP_constu:
13110 return "DW_OP_constu";
13111 case DW_OP_consts:
13112 return "DW_OP_consts";
13113 case DW_OP_dup:
13114 return "DW_OP_dup";
13115 case DW_OP_drop:
13116 return "DW_OP_drop";
13117 case DW_OP_over:
13118 return "DW_OP_over";
13119 case DW_OP_pick:
13120 return "DW_OP_pick";
13121 case DW_OP_swap:
13122 return "DW_OP_swap";
13123 case DW_OP_rot:
13124 return "DW_OP_rot";
13125 case DW_OP_xderef:
13126 return "DW_OP_xderef";
13127 case DW_OP_abs:
13128 return "DW_OP_abs";
13129 case DW_OP_and:
13130 return "DW_OP_and";
13131 case DW_OP_div:
13132 return "DW_OP_div";
13133 case DW_OP_minus:
13134 return "DW_OP_minus";
13135 case DW_OP_mod:
13136 return "DW_OP_mod";
13137 case DW_OP_mul:
13138 return "DW_OP_mul";
13139 case DW_OP_neg:
13140 return "DW_OP_neg";
13141 case DW_OP_not:
13142 return "DW_OP_not";
13143 case DW_OP_or:
13144 return "DW_OP_or";
13145 case DW_OP_plus:
13146 return "DW_OP_plus";
13147 case DW_OP_plus_uconst:
13148 return "DW_OP_plus_uconst";
13149 case DW_OP_shl:
13150 return "DW_OP_shl";
13151 case DW_OP_shr:
13152 return "DW_OP_shr";
13153 case DW_OP_shra:
13154 return "DW_OP_shra";
13155 case DW_OP_xor:
13156 return "DW_OP_xor";
13157 case DW_OP_bra:
13158 return "DW_OP_bra";
13159 case DW_OP_eq:
13160 return "DW_OP_eq";
13161 case DW_OP_ge:
13162 return "DW_OP_ge";
13163 case DW_OP_gt:
13164 return "DW_OP_gt";
13165 case DW_OP_le:
13166 return "DW_OP_le";
13167 case DW_OP_lt:
13168 return "DW_OP_lt";
13169 case DW_OP_ne:
13170 return "DW_OP_ne";
13171 case DW_OP_skip:
13172 return "DW_OP_skip";
13173 case DW_OP_lit0:
13174 return "DW_OP_lit0";
13175 case DW_OP_lit1:
13176 return "DW_OP_lit1";
13177 case DW_OP_lit2:
13178 return "DW_OP_lit2";
13179 case DW_OP_lit3:
13180 return "DW_OP_lit3";
13181 case DW_OP_lit4:
13182 return "DW_OP_lit4";
13183 case DW_OP_lit5:
13184 return "DW_OP_lit5";
13185 case DW_OP_lit6:
13186 return "DW_OP_lit6";
13187 case DW_OP_lit7:
13188 return "DW_OP_lit7";
13189 case DW_OP_lit8:
13190 return "DW_OP_lit8";
13191 case DW_OP_lit9:
13192 return "DW_OP_lit9";
13193 case DW_OP_lit10:
13194 return "DW_OP_lit10";
13195 case DW_OP_lit11:
13196 return "DW_OP_lit11";
13197 case DW_OP_lit12:
13198 return "DW_OP_lit12";
13199 case DW_OP_lit13:
13200 return "DW_OP_lit13";
13201 case DW_OP_lit14:
13202 return "DW_OP_lit14";
13203 case DW_OP_lit15:
13204 return "DW_OP_lit15";
13205 case DW_OP_lit16:
13206 return "DW_OP_lit16";
13207 case DW_OP_lit17:
13208 return "DW_OP_lit17";
13209 case DW_OP_lit18:
13210 return "DW_OP_lit18";
13211 case DW_OP_lit19:
13212 return "DW_OP_lit19";
13213 case DW_OP_lit20:
13214 return "DW_OP_lit20";
13215 case DW_OP_lit21:
13216 return "DW_OP_lit21";
13217 case DW_OP_lit22:
13218 return "DW_OP_lit22";
13219 case DW_OP_lit23:
13220 return "DW_OP_lit23";
13221 case DW_OP_lit24:
13222 return "DW_OP_lit24";
13223 case DW_OP_lit25:
13224 return "DW_OP_lit25";
13225 case DW_OP_lit26:
13226 return "DW_OP_lit26";
13227 case DW_OP_lit27:
13228 return "DW_OP_lit27";
13229 case DW_OP_lit28:
13230 return "DW_OP_lit28";
13231 case DW_OP_lit29:
13232 return "DW_OP_lit29";
13233 case DW_OP_lit30:
13234 return "DW_OP_lit30";
13235 case DW_OP_lit31:
13236 return "DW_OP_lit31";
13237 case DW_OP_reg0:
13238 return "DW_OP_reg0";
13239 case DW_OP_reg1:
13240 return "DW_OP_reg1";
13241 case DW_OP_reg2:
13242 return "DW_OP_reg2";
13243 case DW_OP_reg3:
13244 return "DW_OP_reg3";
13245 case DW_OP_reg4:
13246 return "DW_OP_reg4";
13247 case DW_OP_reg5:
13248 return "DW_OP_reg5";
13249 case DW_OP_reg6:
13250 return "DW_OP_reg6";
13251 case DW_OP_reg7:
13252 return "DW_OP_reg7";
13253 case DW_OP_reg8:
13254 return "DW_OP_reg8";
13255 case DW_OP_reg9:
13256 return "DW_OP_reg9";
13257 case DW_OP_reg10:
13258 return "DW_OP_reg10";
13259 case DW_OP_reg11:
13260 return "DW_OP_reg11";
13261 case DW_OP_reg12:
13262 return "DW_OP_reg12";
13263 case DW_OP_reg13:
13264 return "DW_OP_reg13";
13265 case DW_OP_reg14:
13266 return "DW_OP_reg14";
13267 case DW_OP_reg15:
13268 return "DW_OP_reg15";
13269 case DW_OP_reg16:
13270 return "DW_OP_reg16";
13271 case DW_OP_reg17:
13272 return "DW_OP_reg17";
13273 case DW_OP_reg18:
13274 return "DW_OP_reg18";
13275 case DW_OP_reg19:
13276 return "DW_OP_reg19";
13277 case DW_OP_reg20:
13278 return "DW_OP_reg20";
13279 case DW_OP_reg21:
13280 return "DW_OP_reg21";
13281 case DW_OP_reg22:
13282 return "DW_OP_reg22";
13283 case DW_OP_reg23:
13284 return "DW_OP_reg23";
13285 case DW_OP_reg24:
13286 return "DW_OP_reg24";
13287 case DW_OP_reg25:
13288 return "DW_OP_reg25";
13289 case DW_OP_reg26:
13290 return "DW_OP_reg26";
13291 case DW_OP_reg27:
13292 return "DW_OP_reg27";
13293 case DW_OP_reg28:
13294 return "DW_OP_reg28";
13295 case DW_OP_reg29:
13296 return "DW_OP_reg29";
13297 case DW_OP_reg30:
13298 return "DW_OP_reg30";
13299 case DW_OP_reg31:
13300 return "DW_OP_reg31";
13301 case DW_OP_breg0:
13302 return "DW_OP_breg0";
13303 case DW_OP_breg1:
13304 return "DW_OP_breg1";
13305 case DW_OP_breg2:
13306 return "DW_OP_breg2";
13307 case DW_OP_breg3:
13308 return "DW_OP_breg3";
13309 case DW_OP_breg4:
13310 return "DW_OP_breg4";
13311 case DW_OP_breg5:
13312 return "DW_OP_breg5";
13313 case DW_OP_breg6:
13314 return "DW_OP_breg6";
13315 case DW_OP_breg7:
13316 return "DW_OP_breg7";
13317 case DW_OP_breg8:
13318 return "DW_OP_breg8";
13319 case DW_OP_breg9:
13320 return "DW_OP_breg9";
13321 case DW_OP_breg10:
13322 return "DW_OP_breg10";
13323 case DW_OP_breg11:
13324 return "DW_OP_breg11";
13325 case DW_OP_breg12:
13326 return "DW_OP_breg12";
13327 case DW_OP_breg13:
13328 return "DW_OP_breg13";
13329 case DW_OP_breg14:
13330 return "DW_OP_breg14";
13331 case DW_OP_breg15:
13332 return "DW_OP_breg15";
13333 case DW_OP_breg16:
13334 return "DW_OP_breg16";
13335 case DW_OP_breg17:
13336 return "DW_OP_breg17";
13337 case DW_OP_breg18:
13338 return "DW_OP_breg18";
13339 case DW_OP_breg19:
13340 return "DW_OP_breg19";
13341 case DW_OP_breg20:
13342 return "DW_OP_breg20";
13343 case DW_OP_breg21:
13344 return "DW_OP_breg21";
13345 case DW_OP_breg22:
13346 return "DW_OP_breg22";
13347 case DW_OP_breg23:
13348 return "DW_OP_breg23";
13349 case DW_OP_breg24:
13350 return "DW_OP_breg24";
13351 case DW_OP_breg25:
13352 return "DW_OP_breg25";
13353 case DW_OP_breg26:
13354 return "DW_OP_breg26";
13355 case DW_OP_breg27:
13356 return "DW_OP_breg27";
13357 case DW_OP_breg28:
13358 return "DW_OP_breg28";
13359 case DW_OP_breg29:
13360 return "DW_OP_breg29";
13361 case DW_OP_breg30:
13362 return "DW_OP_breg30";
13363 case DW_OP_breg31:
13364 return "DW_OP_breg31";
13365 case DW_OP_regx:
13366 return "DW_OP_regx";
13367 case DW_OP_fbreg:
13368 return "DW_OP_fbreg";
13369 case DW_OP_bregx:
13370 return "DW_OP_bregx";
13371 case DW_OP_piece:
13372 return "DW_OP_piece";
13373 case DW_OP_deref_size:
13374 return "DW_OP_deref_size";
13375 case DW_OP_xderef_size:
13376 return "DW_OP_xderef_size";
13377 case DW_OP_nop:
13378 return "DW_OP_nop";
b7619582 13379 /* DWARF 3 extensions. */
ed348acc
EZ
13380 case DW_OP_push_object_address:
13381 return "DW_OP_push_object_address";
13382 case DW_OP_call2:
13383 return "DW_OP_call2";
13384 case DW_OP_call4:
13385 return "DW_OP_call4";
13386 case DW_OP_call_ref:
13387 return "DW_OP_call_ref";
b7619582
GF
13388 case DW_OP_form_tls_address:
13389 return "DW_OP_form_tls_address";
13390 case DW_OP_call_frame_cfa:
13391 return "DW_OP_call_frame_cfa";
13392 case DW_OP_bit_piece:
13393 return "DW_OP_bit_piece";
9eae7c52
TT
13394 /* DWARF 4 extensions. */
13395 case DW_OP_implicit_value:
13396 return "DW_OP_implicit_value";
13397 case DW_OP_stack_value:
13398 return "DW_OP_stack_value";
13399 /* GNU extensions. */
ed348acc
EZ
13400 case DW_OP_GNU_push_tls_address:
13401 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13402 case DW_OP_GNU_uninit:
13403 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13404 case DW_OP_GNU_implicit_pointer:
13405 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13406 case DW_OP_GNU_entry_value:
13407 return "DW_OP_GNU_entry_value";
13408 case DW_OP_GNU_const_type:
13409 return "DW_OP_GNU_const_type";
13410 case DW_OP_GNU_regval_type:
13411 return "DW_OP_GNU_regval_type";
13412 case DW_OP_GNU_deref_type:
13413 return "DW_OP_GNU_deref_type";
13414 case DW_OP_GNU_convert:
13415 return "DW_OP_GNU_convert";
13416 case DW_OP_GNU_reinterpret:
13417 return "DW_OP_GNU_reinterpret";
c906108c 13418 default:
b1bfef65 13419 return NULL;
c906108c
SS
13420 }
13421}
13422
13423static char *
fba45db2 13424dwarf_bool_name (unsigned mybool)
c906108c
SS
13425{
13426 if (mybool)
13427 return "TRUE";
13428 else
13429 return "FALSE";
13430}
13431
13432/* Convert a DWARF type code into its string name. */
13433
13434static char *
aa1ee363 13435dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13436{
13437 switch (enc)
13438 {
b7619582
GF
13439 case DW_ATE_void:
13440 return "DW_ATE_void";
c906108c
SS
13441 case DW_ATE_address:
13442 return "DW_ATE_address";
13443 case DW_ATE_boolean:
13444 return "DW_ATE_boolean";
13445 case DW_ATE_complex_float:
13446 return "DW_ATE_complex_float";
13447 case DW_ATE_float:
13448 return "DW_ATE_float";
13449 case DW_ATE_signed:
13450 return "DW_ATE_signed";
13451 case DW_ATE_signed_char:
13452 return "DW_ATE_signed_char";
13453 case DW_ATE_unsigned:
13454 return "DW_ATE_unsigned";
13455 case DW_ATE_unsigned_char:
13456 return "DW_ATE_unsigned_char";
b7619582 13457 /* DWARF 3. */
d9fa45fe
DC
13458 case DW_ATE_imaginary_float:
13459 return "DW_ATE_imaginary_float";
b7619582
GF
13460 case DW_ATE_packed_decimal:
13461 return "DW_ATE_packed_decimal";
13462 case DW_ATE_numeric_string:
13463 return "DW_ATE_numeric_string";
13464 case DW_ATE_edited:
13465 return "DW_ATE_edited";
13466 case DW_ATE_signed_fixed:
13467 return "DW_ATE_signed_fixed";
13468 case DW_ATE_unsigned_fixed:
13469 return "DW_ATE_unsigned_fixed";
13470 case DW_ATE_decimal_float:
13471 return "DW_ATE_decimal_float";
75079b2b
TT
13472 /* DWARF 4. */
13473 case DW_ATE_UTF:
13474 return "DW_ATE_UTF";
b7619582
GF
13475 /* HP extensions. */
13476 case DW_ATE_HP_float80:
13477 return "DW_ATE_HP_float80";
13478 case DW_ATE_HP_complex_float80:
13479 return "DW_ATE_HP_complex_float80";
13480 case DW_ATE_HP_float128:
13481 return "DW_ATE_HP_float128";
13482 case DW_ATE_HP_complex_float128:
13483 return "DW_ATE_HP_complex_float128";
13484 case DW_ATE_HP_floathpintel:
13485 return "DW_ATE_HP_floathpintel";
13486 case DW_ATE_HP_imaginary_float80:
13487 return "DW_ATE_HP_imaginary_float80";
13488 case DW_ATE_HP_imaginary_float128:
13489 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13490 default:
13491 return "DW_ATE_<unknown>";
13492 }
13493}
13494
0963b4bd 13495/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13496
13497#if 0
13498static char *
aa1ee363 13499dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13500{
13501 switch (cfi_opc)
13502 {
13503 case DW_CFA_advance_loc:
13504 return "DW_CFA_advance_loc";
13505 case DW_CFA_offset:
13506 return "DW_CFA_offset";
13507 case DW_CFA_restore:
13508 return "DW_CFA_restore";
13509 case DW_CFA_nop:
13510 return "DW_CFA_nop";
13511 case DW_CFA_set_loc:
13512 return "DW_CFA_set_loc";
13513 case DW_CFA_advance_loc1:
13514 return "DW_CFA_advance_loc1";
13515 case DW_CFA_advance_loc2:
13516 return "DW_CFA_advance_loc2";
13517 case DW_CFA_advance_loc4:
13518 return "DW_CFA_advance_loc4";
13519 case DW_CFA_offset_extended:
13520 return "DW_CFA_offset_extended";
13521 case DW_CFA_restore_extended:
13522 return "DW_CFA_restore_extended";
13523 case DW_CFA_undefined:
13524 return "DW_CFA_undefined";
13525 case DW_CFA_same_value:
13526 return "DW_CFA_same_value";
13527 case DW_CFA_register:
13528 return "DW_CFA_register";
13529 case DW_CFA_remember_state:
13530 return "DW_CFA_remember_state";
13531 case DW_CFA_restore_state:
13532 return "DW_CFA_restore_state";
13533 case DW_CFA_def_cfa:
13534 return "DW_CFA_def_cfa";
13535 case DW_CFA_def_cfa_register:
13536 return "DW_CFA_def_cfa_register";
13537 case DW_CFA_def_cfa_offset:
13538 return "DW_CFA_def_cfa_offset";
b7619582 13539 /* DWARF 3. */
985cb1a3
JM
13540 case DW_CFA_def_cfa_expression:
13541 return "DW_CFA_def_cfa_expression";
13542 case DW_CFA_expression:
13543 return "DW_CFA_expression";
13544 case DW_CFA_offset_extended_sf:
13545 return "DW_CFA_offset_extended_sf";
13546 case DW_CFA_def_cfa_sf:
13547 return "DW_CFA_def_cfa_sf";
13548 case DW_CFA_def_cfa_offset_sf:
13549 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13550 case DW_CFA_val_offset:
13551 return "DW_CFA_val_offset";
13552 case DW_CFA_val_offset_sf:
13553 return "DW_CFA_val_offset_sf";
13554 case DW_CFA_val_expression:
13555 return "DW_CFA_val_expression";
13556 /* SGI/MIPS specific. */
c906108c
SS
13557 case DW_CFA_MIPS_advance_loc8:
13558 return "DW_CFA_MIPS_advance_loc8";
b7619582 13559 /* GNU extensions. */
985cb1a3
JM
13560 case DW_CFA_GNU_window_save:
13561 return "DW_CFA_GNU_window_save";
13562 case DW_CFA_GNU_args_size:
13563 return "DW_CFA_GNU_args_size";
13564 case DW_CFA_GNU_negative_offset_extended:
13565 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13566 default:
13567 return "DW_CFA_<unknown>";
13568 }
13569}
13570#endif
13571
f9aca02d 13572static void
d97bc12b 13573dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13574{
13575 unsigned int i;
13576
d97bc12b
DE
13577 print_spaces (indent, f);
13578 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13579 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13580
13581 if (die->parent != NULL)
13582 {
13583 print_spaces (indent, f);
13584 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13585 die->parent->offset);
13586 }
13587
13588 print_spaces (indent, f);
13589 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13590 dwarf_bool_name (die->child != NULL));
c906108c 13591
d97bc12b
DE
13592 print_spaces (indent, f);
13593 fprintf_unfiltered (f, " attributes:\n");
13594
c906108c
SS
13595 for (i = 0; i < die->num_attrs; ++i)
13596 {
d97bc12b
DE
13597 print_spaces (indent, f);
13598 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13599 dwarf_attr_name (die->attrs[i].name),
13600 dwarf_form_name (die->attrs[i].form));
d97bc12b 13601
c906108c
SS
13602 switch (die->attrs[i].form)
13603 {
13604 case DW_FORM_ref_addr:
13605 case DW_FORM_addr:
d97bc12b 13606 fprintf_unfiltered (f, "address: ");
5af949e3 13607 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13608 break;
13609 case DW_FORM_block2:
13610 case DW_FORM_block4:
13611 case DW_FORM_block:
13612 case DW_FORM_block1:
3e43a32a
MS
13613 fprintf_unfiltered (f, "block: size %d",
13614 DW_BLOCK (&die->attrs[i])->size);
c906108c 13615 break;
2dc7f7b3
TT
13616 case DW_FORM_exprloc:
13617 fprintf_unfiltered (f, "expression: size %u",
13618 DW_BLOCK (&die->attrs[i])->size);
13619 break;
10b3939b
DJ
13620 case DW_FORM_ref1:
13621 case DW_FORM_ref2:
13622 case DW_FORM_ref4:
d97bc12b 13623 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13624 (long) (DW_ADDR (&die->attrs[i])));
13625 break;
c906108c
SS
13626 case DW_FORM_data1:
13627 case DW_FORM_data2:
13628 case DW_FORM_data4:
ce5d95e1 13629 case DW_FORM_data8:
c906108c
SS
13630 case DW_FORM_udata:
13631 case DW_FORM_sdata:
43bbcdc2
PH
13632 fprintf_unfiltered (f, "constant: %s",
13633 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13634 break;
2dc7f7b3
TT
13635 case DW_FORM_sec_offset:
13636 fprintf_unfiltered (f, "section offset: %s",
13637 pulongest (DW_UNSND (&die->attrs[i])));
13638 break;
55f1336d 13639 case DW_FORM_ref_sig8:
348e048f
DE
13640 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13641 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b3c8eb43 13642 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
348e048f
DE
13643 else
13644 fprintf_unfiltered (f, "signatured type, offset: unknown");
13645 break;
c906108c 13646 case DW_FORM_string:
4bdf3d34 13647 case DW_FORM_strp:
8285870a 13648 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13649 DW_STRING (&die->attrs[i])
8285870a
JK
13650 ? DW_STRING (&die->attrs[i]) : "",
13651 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13652 break;
13653 case DW_FORM_flag:
13654 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13655 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13656 else
d97bc12b 13657 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13658 break;
2dc7f7b3
TT
13659 case DW_FORM_flag_present:
13660 fprintf_unfiltered (f, "flag: TRUE");
13661 break;
a8329558 13662 case DW_FORM_indirect:
0963b4bd
MS
13663 /* The reader will have reduced the indirect form to
13664 the "base form" so this form should not occur. */
3e43a32a
MS
13665 fprintf_unfiltered (f,
13666 "unexpected attribute form: DW_FORM_indirect");
a8329558 13667 break;
c906108c 13668 default:
d97bc12b 13669 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13670 die->attrs[i].form);
d97bc12b 13671 break;
c906108c 13672 }
d97bc12b 13673 fprintf_unfiltered (f, "\n");
c906108c
SS
13674 }
13675}
13676
f9aca02d 13677static void
d97bc12b 13678dump_die_for_error (struct die_info *die)
c906108c 13679{
d97bc12b
DE
13680 dump_die_shallow (gdb_stderr, 0, die);
13681}
13682
13683static void
13684dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13685{
13686 int indent = level * 4;
13687
13688 gdb_assert (die != NULL);
13689
13690 if (level >= max_level)
13691 return;
13692
13693 dump_die_shallow (f, indent, die);
13694
13695 if (die->child != NULL)
c906108c 13696 {
d97bc12b
DE
13697 print_spaces (indent, f);
13698 fprintf_unfiltered (f, " Children:");
13699 if (level + 1 < max_level)
13700 {
13701 fprintf_unfiltered (f, "\n");
13702 dump_die_1 (f, level + 1, max_level, die->child);
13703 }
13704 else
13705 {
3e43a32a
MS
13706 fprintf_unfiltered (f,
13707 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13708 }
13709 }
13710
13711 if (die->sibling != NULL && level > 0)
13712 {
13713 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13714 }
13715}
13716
d97bc12b
DE
13717/* This is called from the pdie macro in gdbinit.in.
13718 It's not static so gcc will keep a copy callable from gdb. */
13719
13720void
13721dump_die (struct die_info *die, int max_level)
13722{
13723 dump_die_1 (gdb_stdlog, 0, max_level, die);
13724}
13725
f9aca02d 13726static void
51545339 13727store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13728{
51545339 13729 void **slot;
c906108c 13730
51545339
DJ
13731 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13732
13733 *slot = die;
c906108c
SS
13734}
13735
93311388
DE
13736static int
13737is_ref_attr (struct attribute *attr)
c906108c 13738{
c906108c
SS
13739 switch (attr->form)
13740 {
13741 case DW_FORM_ref_addr:
c906108c
SS
13742 case DW_FORM_ref1:
13743 case DW_FORM_ref2:
13744 case DW_FORM_ref4:
613e1657 13745 case DW_FORM_ref8:
c906108c 13746 case DW_FORM_ref_udata:
93311388 13747 return 1;
c906108c 13748 default:
93311388 13749 return 0;
c906108c 13750 }
93311388
DE
13751}
13752
13753static unsigned int
13754dwarf2_get_ref_die_offset (struct attribute *attr)
13755{
13756 if (is_ref_attr (attr))
13757 return DW_ADDR (attr);
13758
13759 complaint (&symfile_complaints,
13760 _("unsupported die ref attribute form: '%s'"),
13761 dwarf_form_name (attr->form));
13762 return 0;
c906108c
SS
13763}
13764
43bbcdc2
PH
13765/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13766 * the value held by the attribute is not constant. */
a02abb62 13767
43bbcdc2 13768static LONGEST
a02abb62
JB
13769dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13770{
13771 if (attr->form == DW_FORM_sdata)
13772 return DW_SND (attr);
13773 else if (attr->form == DW_FORM_udata
13774 || attr->form == DW_FORM_data1
13775 || attr->form == DW_FORM_data2
13776 || attr->form == DW_FORM_data4
13777 || attr->form == DW_FORM_data8)
13778 return DW_UNSND (attr);
13779 else
13780 {
3e43a32a
MS
13781 complaint (&symfile_complaints,
13782 _("Attribute value is not a constant (%s)"),
a02abb62
JB
13783 dwarf_form_name (attr->form));
13784 return default_value;
13785 }
13786}
13787
03dd20cc 13788/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
13789 unit and add it to our queue.
13790 The result is non-zero if PER_CU was queued, otherwise the result is zero
13791 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 13792
348e048f 13793static int
03dd20cc
DJ
13794maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13795 struct dwarf2_per_cu_data *per_cu)
13796{
98bfdba5
PA
13797 /* We may arrive here during partial symbol reading, if we need full
13798 DIEs to process an unusual case (e.g. template arguments). Do
13799 not queue PER_CU, just tell our caller to load its DIEs. */
13800 if (dwarf2_per_objfile->reading_partial_symbols)
13801 {
13802 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13803 return 1;
13804 return 0;
13805 }
13806
03dd20cc
DJ
13807 /* Mark the dependence relation so that we don't flush PER_CU
13808 too early. */
13809 dwarf2_add_dependence (this_cu, per_cu);
13810
13811 /* If it's already on the queue, we have nothing to do. */
13812 if (per_cu->queued)
348e048f 13813 return 0;
03dd20cc
DJ
13814
13815 /* If the compilation unit is already loaded, just mark it as
13816 used. */
13817 if (per_cu->cu != NULL)
13818 {
13819 per_cu->cu->last_used = 0;
348e048f 13820 return 0;
03dd20cc
DJ
13821 }
13822
13823 /* Add it to the queue. */
13824 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
13825
13826 return 1;
13827}
13828
13829/* Follow reference or signature attribute ATTR of SRC_DIE.
13830 On entry *REF_CU is the CU of SRC_DIE.
13831 On exit *REF_CU is the CU of the result. */
13832
13833static struct die_info *
13834follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13835 struct dwarf2_cu **ref_cu)
13836{
13837 struct die_info *die;
13838
13839 if (is_ref_attr (attr))
13840 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 13841 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
13842 die = follow_die_sig (src_die, attr, ref_cu);
13843 else
13844 {
13845 dump_die_for_error (src_die);
13846 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13847 (*ref_cu)->objfile->name);
13848 }
13849
13850 return die;
03dd20cc
DJ
13851}
13852
5c631832 13853/* Follow reference OFFSET.
673bfd45
DE
13854 On entry *REF_CU is the CU of the source die referencing OFFSET.
13855 On exit *REF_CU is the CU of the result.
13856 Returns NULL if OFFSET is invalid. */
f504f079 13857
f9aca02d 13858static struct die_info *
5c631832 13859follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13860{
10b3939b 13861 struct die_info temp_die;
f2f0e013 13862 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13863
348e048f
DE
13864 gdb_assert (cu->per_cu != NULL);
13865
98bfdba5
PA
13866 target_cu = cu;
13867
8b70b953 13868 if (cu->per_cu->debug_type_section)
348e048f
DE
13869 {
13870 /* .debug_types CUs cannot reference anything outside their CU.
13871 If they need to, they have to reference a signatured type via
55f1336d 13872 DW_FORM_ref_sig8. */
348e048f 13873 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13874 return NULL;
348e048f
DE
13875 }
13876 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13877 {
13878 struct dwarf2_per_cu_data *per_cu;
9a619af0 13879
45452591 13880 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13881
13882 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13883 if (maybe_queue_comp_unit (cu, per_cu))
13884 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13885
10b3939b
DJ
13886 target_cu = per_cu->cu;
13887 }
98bfdba5
PA
13888 else if (cu->dies == NULL)
13889 {
13890 /* We're loading full DIEs during partial symbol reading. */
13891 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13892 load_full_comp_unit (cu->per_cu, cu->objfile);
13893 }
c906108c 13894
f2f0e013 13895 *ref_cu = target_cu;
51545339 13896 temp_die.offset = offset;
5c631832
JK
13897 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13898}
10b3939b 13899
5c631832
JK
13900/* Follow reference attribute ATTR of SRC_DIE.
13901 On entry *REF_CU is the CU of SRC_DIE.
13902 On exit *REF_CU is the CU of the result. */
13903
13904static struct die_info *
13905follow_die_ref (struct die_info *src_die, struct attribute *attr,
13906 struct dwarf2_cu **ref_cu)
13907{
13908 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13909 struct dwarf2_cu *cu = *ref_cu;
13910 struct die_info *die;
13911
13912 die = follow_die_offset (offset, ref_cu);
13913 if (!die)
13914 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13915 "at 0x%x [in module %s]"),
13916 offset, src_die->offset, cu->objfile->name);
348e048f 13917
5c631832
JK
13918 return die;
13919}
13920
13921/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
918dd910
JK
13922 value is intended for DW_OP_call*. You must call xfree on returned
13923 dwarf2_locexpr_baton->data. */
5c631832
JK
13924
13925struct dwarf2_locexpr_baton
13926dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
13927 struct dwarf2_per_cu_data *per_cu,
13928 CORE_ADDR (*get_frame_pc) (void *baton),
13929 void *baton)
5c631832 13930{
918dd910 13931 struct dwarf2_cu *cu;
5c631832
JK
13932 struct die_info *die;
13933 struct attribute *attr;
13934 struct dwarf2_locexpr_baton retval;
13935
8cf6f0b1
TT
13936 dw2_setup (per_cu->objfile);
13937
918dd910
JK
13938 if (per_cu->cu == NULL)
13939 load_cu (per_cu);
13940 cu = per_cu->cu;
13941
5c631832
JK
13942 die = follow_die_offset (offset, &cu);
13943 if (!die)
13944 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13945 offset, per_cu->cu->objfile->name);
13946
13947 attr = dwarf2_attr (die, DW_AT_location, cu);
13948 if (!attr)
13949 {
13950 /* DWARF: "If there is no such attribute, then there is no effect.". */
13951
13952 retval.data = NULL;
13953 retval.size = 0;
13954 }
8cf6f0b1
TT
13955 else if (attr_form_is_section_offset (attr))
13956 {
13957 struct dwarf2_loclist_baton loclist_baton;
13958 CORE_ADDR pc = (*get_frame_pc) (baton);
13959 size_t size;
13960
13961 fill_in_loclist_baton (cu, &loclist_baton, attr);
13962
13963 retval.data = dwarf2_find_location_expression (&loclist_baton,
13964 &size, pc);
13965 retval.size = size;
13966 }
5c631832
JK
13967 else
13968 {
13969 if (!attr_form_is_block (attr))
13970 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13971 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13972 offset, per_cu->cu->objfile->name);
13973
13974 retval.data = DW_BLOCK (attr)->data;
13975 retval.size = DW_BLOCK (attr)->size;
13976 }
13977 retval.per_cu = cu->per_cu;
918dd910
JK
13978
13979 if (retval.data)
13980 retval.data = xmemdup (retval.data, retval.size, retval.size);
13981
13982 age_cached_comp_units ();
13983
5c631832 13984 return retval;
348e048f
DE
13985}
13986
8a9b8146
TT
13987/* Return the type of the DIE at DIE_OFFSET in the CU named by
13988 PER_CU. */
13989
13990struct type *
13991dwarf2_get_die_type (unsigned int die_offset,
13992 struct dwarf2_per_cu_data *per_cu)
13993{
8a9b8146 13994 dw2_setup (per_cu->objfile);
9ff3b74f 13995 return get_die_type_at_offset (die_offset, per_cu);
8a9b8146
TT
13996}
13997
348e048f
DE
13998/* Follow the signature attribute ATTR in SRC_DIE.
13999 On entry *REF_CU is the CU of SRC_DIE.
14000 On exit *REF_CU is the CU of the result. */
14001
14002static struct die_info *
14003follow_die_sig (struct die_info *src_die, struct attribute *attr,
14004 struct dwarf2_cu **ref_cu)
14005{
14006 struct objfile *objfile = (*ref_cu)->objfile;
14007 struct die_info temp_die;
14008 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14009 struct dwarf2_cu *sig_cu;
14010 struct die_info *die;
14011
14012 /* sig_type will be NULL if the signatured type is missing from
14013 the debug info. */
14014 if (sig_type == NULL)
14015 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14016 "at 0x%x [in module %s]"),
14017 src_die->offset, objfile->name);
14018
14019 /* If necessary, add it to the queue and load its DIEs. */
14020
14021 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14022 read_signatured_type (objfile, sig_type);
14023
14024 gdb_assert (sig_type->per_cu.cu != NULL);
14025
14026 sig_cu = sig_type->per_cu.cu;
14027 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14028 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14029 if (die)
14030 {
14031 *ref_cu = sig_cu;
14032 return die;
14033 }
14034
3e43a32a
MS
14035 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14036 "from DIE at 0x%x [in module %s]"),
348e048f
DE
14037 sig_type->type_offset, src_die->offset, objfile->name);
14038}
14039
14040/* Given an offset of a signatured type, return its signatured_type. */
14041
14042static struct signatured_type *
8b70b953
TT
14043lookup_signatured_type_at_offset (struct objfile *objfile,
14044 struct dwarf2_section_info *section,
14045 unsigned int offset)
348e048f 14046{
8b70b953 14047 gdb_byte *info_ptr = section->buffer + offset;
348e048f
DE
14048 unsigned int length, initial_length_size;
14049 unsigned int sig_offset;
14050 struct signatured_type find_entry, *type_sig;
14051
14052 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14053 sig_offset = (initial_length_size
14054 + 2 /*version*/
14055 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14056 + 1 /*address_size*/);
14057 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14058 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14059
14060 /* This is only used to lookup previously recorded types.
14061 If we didn't find it, it's our bug. */
14062 gdb_assert (type_sig != NULL);
b3c8eb43 14063 gdb_assert (offset == type_sig->per_cu.offset);
348e048f
DE
14064
14065 return type_sig;
14066}
14067
14068/* Read in signatured type at OFFSET and build its CU and die(s). */
14069
14070static void
14071read_signatured_type_at_offset (struct objfile *objfile,
8b70b953 14072 struct dwarf2_section_info *sect,
348e048f
DE
14073 unsigned int offset)
14074{
14075 struct signatured_type *type_sig;
14076
8b70b953 14077 dwarf2_read_section (objfile, sect);
be391dca 14078
348e048f
DE
14079 /* We have the section offset, but we need the signature to do the
14080 hash table lookup. */
8b70b953 14081 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f
DE
14082
14083 gdb_assert (type_sig->per_cu.cu == NULL);
14084
14085 read_signatured_type (objfile, type_sig);
14086
14087 gdb_assert (type_sig->per_cu.cu != NULL);
14088}
14089
14090/* Read in a signatured type and build its CU and DIEs. */
14091
14092static void
14093read_signatured_type (struct objfile *objfile,
14094 struct signatured_type *type_sig)
14095{
1fd400ff 14096 gdb_byte *types_ptr;
348e048f
DE
14097 struct die_reader_specs reader_specs;
14098 struct dwarf2_cu *cu;
14099 ULONGEST signature;
14100 struct cleanup *back_to, *free_cu_cleanup;
8b70b953 14101 struct dwarf2_section_info *section = type_sig->per_cu.debug_type_section;
348e048f 14102
8b70b953
TT
14103 dwarf2_read_section (objfile, section);
14104 types_ptr = section->buffer + type_sig->per_cu.offset;
1fd400ff 14105
348e048f
DE
14106 gdb_assert (type_sig->per_cu.cu == NULL);
14107
9816fde3
JK
14108 cu = xmalloc (sizeof (*cu));
14109 init_one_comp_unit (cu, objfile);
14110
348e048f
DE
14111 type_sig->per_cu.cu = cu;
14112 cu->per_cu = &type_sig->per_cu;
14113
14114 /* If an error occurs while loading, release our storage. */
14115 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14116
8b70b953 14117 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
348e048f
DE
14118 types_ptr, objfile->obfd);
14119 gdb_assert (signature == type_sig->signature);
14120
14121 cu->die_hash
14122 = htab_create_alloc_ex (cu->header.length / 12,
14123 die_hash,
14124 die_eq,
14125 NULL,
14126 &cu->comp_unit_obstack,
14127 hashtab_obstack_allocate,
14128 dummy_obstack_deallocate);
14129
14130 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14131 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14132
14133 init_cu_die_reader (&reader_specs, cu);
14134
14135 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14136 NULL /*parent*/);
14137
14138 /* We try not to read any attributes in this function, because not
14139 all objfiles needed for references have been loaded yet, and symbol
14140 table processing isn't initialized. But we have to set the CU language,
14141 or we won't be able to build types correctly. */
9816fde3 14142 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14143
14144 do_cleanups (back_to);
14145
14146 /* We've successfully allocated this compilation unit. Let our caller
14147 clean it up when finished with it. */
14148 discard_cleanups (free_cu_cleanup);
14149
14150 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14151 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
14152}
14153
c906108c
SS
14154/* Decode simple location descriptions.
14155 Given a pointer to a dwarf block that defines a location, compute
14156 the location and return the value.
14157
4cecd739
DJ
14158 NOTE drow/2003-11-18: This function is called in two situations
14159 now: for the address of static or global variables (partial symbols
14160 only) and for offsets into structures which are expected to be
14161 (more or less) constant. The partial symbol case should go away,
14162 and only the constant case should remain. That will let this
14163 function complain more accurately. A few special modes are allowed
14164 without complaint for global variables (for instance, global
14165 register values and thread-local values).
c906108c
SS
14166
14167 A location description containing no operations indicates that the
4cecd739 14168 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14169 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14170 callers will only want a very basic result and this can become a
14171 complaint.
c906108c 14172
d53d4ac5 14173 Note that stack[0] is unused except as a default error return. */
c906108c
SS
14174
14175static CORE_ADDR
e7c27a73 14176decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14177{
e7c27a73 14178 struct objfile *objfile = cu->objfile;
c906108c
SS
14179 int i;
14180 int size = blk->size;
fe1b8b76 14181 gdb_byte *data = blk->data;
c906108c
SS
14182 CORE_ADDR stack[64];
14183 int stacki;
14184 unsigned int bytes_read, unsnd;
fe1b8b76 14185 gdb_byte op;
c906108c
SS
14186
14187 i = 0;
14188 stacki = 0;
14189 stack[stacki] = 0;
d53d4ac5 14190 stack[++stacki] = 0;
c906108c
SS
14191
14192 while (i < size)
14193 {
c906108c
SS
14194 op = data[i++];
14195 switch (op)
14196 {
f1bea926
JM
14197 case DW_OP_lit0:
14198 case DW_OP_lit1:
14199 case DW_OP_lit2:
14200 case DW_OP_lit3:
14201 case DW_OP_lit4:
14202 case DW_OP_lit5:
14203 case DW_OP_lit6:
14204 case DW_OP_lit7:
14205 case DW_OP_lit8:
14206 case DW_OP_lit9:
14207 case DW_OP_lit10:
14208 case DW_OP_lit11:
14209 case DW_OP_lit12:
14210 case DW_OP_lit13:
14211 case DW_OP_lit14:
14212 case DW_OP_lit15:
14213 case DW_OP_lit16:
14214 case DW_OP_lit17:
14215 case DW_OP_lit18:
14216 case DW_OP_lit19:
14217 case DW_OP_lit20:
14218 case DW_OP_lit21:
14219 case DW_OP_lit22:
14220 case DW_OP_lit23:
14221 case DW_OP_lit24:
14222 case DW_OP_lit25:
14223 case DW_OP_lit26:
14224 case DW_OP_lit27:
14225 case DW_OP_lit28:
14226 case DW_OP_lit29:
14227 case DW_OP_lit30:
14228 case DW_OP_lit31:
14229 stack[++stacki] = op - DW_OP_lit0;
14230 break;
14231
c906108c
SS
14232 case DW_OP_reg0:
14233 case DW_OP_reg1:
14234 case DW_OP_reg2:
14235 case DW_OP_reg3:
14236 case DW_OP_reg4:
14237 case DW_OP_reg5:
14238 case DW_OP_reg6:
14239 case DW_OP_reg7:
14240 case DW_OP_reg8:
14241 case DW_OP_reg9:
14242 case DW_OP_reg10:
14243 case DW_OP_reg11:
14244 case DW_OP_reg12:
14245 case DW_OP_reg13:
14246 case DW_OP_reg14:
14247 case DW_OP_reg15:
14248 case DW_OP_reg16:
14249 case DW_OP_reg17:
14250 case DW_OP_reg18:
14251 case DW_OP_reg19:
14252 case DW_OP_reg20:
14253 case DW_OP_reg21:
14254 case DW_OP_reg22:
14255 case DW_OP_reg23:
14256 case DW_OP_reg24:
14257 case DW_OP_reg25:
14258 case DW_OP_reg26:
14259 case DW_OP_reg27:
14260 case DW_OP_reg28:
14261 case DW_OP_reg29:
14262 case DW_OP_reg30:
14263 case DW_OP_reg31:
c906108c 14264 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
14265 if (i < size)
14266 dwarf2_complex_location_expr_complaint ();
c906108c
SS
14267 break;
14268
14269 case DW_OP_regx:
c906108c
SS
14270 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14271 i += bytes_read;
c906108c 14272 stack[++stacki] = unsnd;
4cecd739
DJ
14273 if (i < size)
14274 dwarf2_complex_location_expr_complaint ();
c906108c
SS
14275 break;
14276
14277 case DW_OP_addr:
107d2387 14278 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 14279 cu, &bytes_read);
107d2387 14280 i += bytes_read;
c906108c
SS
14281 break;
14282
14283 case DW_OP_const1u:
14284 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14285 i += 1;
14286 break;
14287
14288 case DW_OP_const1s:
14289 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14290 i += 1;
14291 break;
14292
14293 case DW_OP_const2u:
14294 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14295 i += 2;
14296 break;
14297
14298 case DW_OP_const2s:
14299 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14300 i += 2;
14301 break;
14302
14303 case DW_OP_const4u:
14304 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14305 i += 4;
14306 break;
14307
14308 case DW_OP_const4s:
14309 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14310 i += 4;
14311 break;
14312
14313 case DW_OP_constu:
14314 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 14315 &bytes_read);
c906108c
SS
14316 i += bytes_read;
14317 break;
14318
14319 case DW_OP_consts:
14320 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14321 i += bytes_read;
14322 break;
14323
f1bea926
JM
14324 case DW_OP_dup:
14325 stack[stacki + 1] = stack[stacki];
14326 stacki++;
14327 break;
14328
c906108c
SS
14329 case DW_OP_plus:
14330 stack[stacki - 1] += stack[stacki];
14331 stacki--;
14332 break;
14333
14334 case DW_OP_plus_uconst:
3e43a32a
MS
14335 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14336 &bytes_read);
c906108c
SS
14337 i += bytes_read;
14338 break;
14339
14340 case DW_OP_minus:
f1bea926 14341 stack[stacki - 1] -= stack[stacki];
c906108c
SS
14342 stacki--;
14343 break;
14344
7a292a7a 14345 case DW_OP_deref:
7a292a7a 14346 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
14347 this using GDB's address_class enum. This is valid for partial
14348 global symbols, although the variable's address will be bogus
14349 in the psymtab. */
7a292a7a 14350 if (i < size)
4d3c2250 14351 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
14352 break;
14353
9d774e44 14354 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
14355 /* The top of the stack has the offset from the beginning
14356 of the thread control block at which the variable is located. */
14357 /* Nothing should follow this operator, so the top of stack would
14358 be returned. */
4cecd739
DJ
14359 /* This is valid for partial global symbols, but the variable's
14360 address will be bogus in the psymtab. */
9d774e44 14361 if (i < size)
4d3c2250 14362 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
14363 break;
14364
42be36b3
CT
14365 case DW_OP_GNU_uninit:
14366 break;
14367
c906108c 14368 default:
b1bfef65
TT
14369 {
14370 const char *name = dwarf_stack_op_name (op);
14371
14372 if (name)
14373 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14374 name);
14375 else
14376 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14377 op);
14378 }
14379
c906108c
SS
14380 return (stack[stacki]);
14381 }
d53d4ac5
TT
14382
14383 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14384 outside of the allocated space. Also enforce minimum>0. */
14385 if (stacki >= ARRAY_SIZE (stack) - 1)
14386 {
14387 complaint (&symfile_complaints,
14388 _("location description stack overflow"));
14389 return 0;
14390 }
14391
14392 if (stacki <= 0)
14393 {
14394 complaint (&symfile_complaints,
14395 _("location description stack underflow"));
14396 return 0;
14397 }
c906108c
SS
14398 }
14399 return (stack[stacki]);
14400}
14401
14402/* memory allocation interface */
14403
c906108c 14404static struct dwarf_block *
7b5a2f43 14405dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14406{
14407 struct dwarf_block *blk;
14408
14409 blk = (struct dwarf_block *)
7b5a2f43 14410 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14411 return (blk);
14412}
14413
14414static struct abbrev_info *
f3dd6933 14415dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14416{
14417 struct abbrev_info *abbrev;
14418
f3dd6933
DJ
14419 abbrev = (struct abbrev_info *)
14420 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14421 memset (abbrev, 0, sizeof (struct abbrev_info));
14422 return (abbrev);
14423}
14424
14425static struct die_info *
b60c80d6 14426dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14427{
14428 struct die_info *die;
b60c80d6
DJ
14429 size_t size = sizeof (struct die_info);
14430
14431 if (num_attrs > 1)
14432 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14433
b60c80d6 14434 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14435 memset (die, 0, sizeof (struct die_info));
14436 return (die);
14437}
2e276125
JB
14438
14439\f
14440/* Macro support. */
14441
2e276125
JB
14442/* Return the full name of file number I in *LH's file name table.
14443 Use COMP_DIR as the name of the current directory of the
14444 compilation. The result is allocated using xmalloc; the caller is
14445 responsible for freeing it. */
14446static char *
14447file_full_name (int file, struct line_header *lh, const char *comp_dir)
14448{
6a83a1e6
EZ
14449 /* Is the file number a valid index into the line header's file name
14450 table? Remember that file numbers start with one, not zero. */
14451 if (1 <= file && file <= lh->num_file_names)
14452 {
14453 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14454
6a83a1e6
EZ
14455 if (IS_ABSOLUTE_PATH (fe->name))
14456 return xstrdup (fe->name);
14457 else
14458 {
14459 const char *dir;
14460 int dir_len;
14461 char *full_name;
14462
14463 if (fe->dir_index)
14464 dir = lh->include_dirs[fe->dir_index - 1];
14465 else
14466 dir = comp_dir;
14467
14468 if (dir)
14469 {
14470 dir_len = strlen (dir);
14471 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14472 strcpy (full_name, dir);
14473 full_name[dir_len] = '/';
14474 strcpy (full_name + dir_len + 1, fe->name);
14475 return full_name;
14476 }
14477 else
14478 return xstrdup (fe->name);
14479 }
14480 }
2e276125
JB
14481 else
14482 {
6a83a1e6
EZ
14483 /* The compiler produced a bogus file number. We can at least
14484 record the macro definitions made in the file, even if we
14485 won't be able to find the file by name. */
14486 char fake_name[80];
9a619af0 14487
6a83a1e6 14488 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14489
6e70227d 14490 complaint (&symfile_complaints,
6a83a1e6
EZ
14491 _("bad file number in macro information (%d)"),
14492 file);
2e276125 14493
6a83a1e6 14494 return xstrdup (fake_name);
2e276125
JB
14495 }
14496}
14497
14498
14499static struct macro_source_file *
14500macro_start_file (int file, int line,
14501 struct macro_source_file *current_file,
14502 const char *comp_dir,
14503 struct line_header *lh, struct objfile *objfile)
14504{
14505 /* The full name of this source file. */
14506 char *full_name = file_full_name (file, lh, comp_dir);
14507
14508 /* We don't create a macro table for this compilation unit
14509 at all until we actually get a filename. */
14510 if (! pending_macros)
4a146b47 14511 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14512 objfile->macro_cache);
2e276125
JB
14513
14514 if (! current_file)
14515 /* If we have no current file, then this must be the start_file
14516 directive for the compilation unit's main source file. */
14517 current_file = macro_set_main (pending_macros, full_name);
14518 else
14519 current_file = macro_include (current_file, line, full_name);
14520
14521 xfree (full_name);
6e70227d 14522
2e276125
JB
14523 return current_file;
14524}
14525
14526
14527/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14528 followed by a null byte. */
14529static char *
14530copy_string (const char *buf, int len)
14531{
14532 char *s = xmalloc (len + 1);
9a619af0 14533
2e276125
JB
14534 memcpy (s, buf, len);
14535 s[len] = '\0';
2e276125
JB
14536 return s;
14537}
14538
14539
14540static const char *
14541consume_improper_spaces (const char *p, const char *body)
14542{
14543 if (*p == ' ')
14544 {
4d3c2250 14545 complaint (&symfile_complaints,
3e43a32a
MS
14546 _("macro definition contains spaces "
14547 "in formal argument list:\n`%s'"),
4d3c2250 14548 body);
2e276125
JB
14549
14550 while (*p == ' ')
14551 p++;
14552 }
14553
14554 return p;
14555}
14556
14557
14558static void
14559parse_macro_definition (struct macro_source_file *file, int line,
14560 const char *body)
14561{
14562 const char *p;
14563
14564 /* The body string takes one of two forms. For object-like macro
14565 definitions, it should be:
14566
14567 <macro name> " " <definition>
14568
14569 For function-like macro definitions, it should be:
14570
14571 <macro name> "() " <definition>
14572 or
14573 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14574
14575 Spaces may appear only where explicitly indicated, and in the
14576 <definition>.
14577
14578 The Dwarf 2 spec says that an object-like macro's name is always
14579 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14580 the space when the macro's definition is the empty string.
2e276125
JB
14581
14582 The Dwarf 2 spec says that there should be no spaces between the
14583 formal arguments in a function-like macro's formal argument list,
14584 but versions of GCC around March 2002 include spaces after the
14585 commas. */
14586
14587
14588 /* Find the extent of the macro name. The macro name is terminated
14589 by either a space or null character (for an object-like macro) or
14590 an opening paren (for a function-like macro). */
14591 for (p = body; *p; p++)
14592 if (*p == ' ' || *p == '(')
14593 break;
14594
14595 if (*p == ' ' || *p == '\0')
14596 {
14597 /* It's an object-like macro. */
14598 int name_len = p - body;
14599 char *name = copy_string (body, name_len);
14600 const char *replacement;
14601
14602 if (*p == ' ')
14603 replacement = body + name_len + 1;
14604 else
14605 {
4d3c2250 14606 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14607 replacement = body + name_len;
14608 }
6e70227d 14609
2e276125
JB
14610 macro_define_object (file, line, name, replacement);
14611
14612 xfree (name);
14613 }
14614 else if (*p == '(')
14615 {
14616 /* It's a function-like macro. */
14617 char *name = copy_string (body, p - body);
14618 int argc = 0;
14619 int argv_size = 1;
14620 char **argv = xmalloc (argv_size * sizeof (*argv));
14621
14622 p++;
14623
14624 p = consume_improper_spaces (p, body);
14625
14626 /* Parse the formal argument list. */
14627 while (*p && *p != ')')
14628 {
14629 /* Find the extent of the current argument name. */
14630 const char *arg_start = p;
14631
14632 while (*p && *p != ',' && *p != ')' && *p != ' ')
14633 p++;
14634
14635 if (! *p || p == arg_start)
4d3c2250 14636 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14637 else
14638 {
14639 /* Make sure argv has room for the new argument. */
14640 if (argc >= argv_size)
14641 {
14642 argv_size *= 2;
14643 argv = xrealloc (argv, argv_size * sizeof (*argv));
14644 }
14645
14646 argv[argc++] = copy_string (arg_start, p - arg_start);
14647 }
14648
14649 p = consume_improper_spaces (p, body);
14650
14651 /* Consume the comma, if present. */
14652 if (*p == ',')
14653 {
14654 p++;
14655
14656 p = consume_improper_spaces (p, body);
14657 }
14658 }
14659
14660 if (*p == ')')
14661 {
14662 p++;
14663
14664 if (*p == ' ')
14665 /* Perfectly formed definition, no complaints. */
14666 macro_define_function (file, line, name,
6e70227d 14667 argc, (const char **) argv,
2e276125
JB
14668 p + 1);
14669 else if (*p == '\0')
14670 {
14671 /* Complain, but do define it. */
4d3c2250 14672 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14673 macro_define_function (file, line, name,
6e70227d 14674 argc, (const char **) argv,
2e276125
JB
14675 p);
14676 }
14677 else
14678 /* Just complain. */
4d3c2250 14679 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14680 }
14681 else
14682 /* Just complain. */
4d3c2250 14683 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14684
14685 xfree (name);
14686 {
14687 int i;
14688
14689 for (i = 0; i < argc; i++)
14690 xfree (argv[i]);
14691 }
14692 xfree (argv);
14693 }
14694 else
4d3c2250 14695 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14696}
14697
cf2c3c16
TT
14698/* Skip some bytes from BYTES according to the form given in FORM.
14699 Returns the new pointer. */
2e276125 14700
cf2c3c16
TT
14701static gdb_byte *
14702skip_form_bytes (bfd *abfd, gdb_byte *bytes,
14703 enum dwarf_form form,
14704 unsigned int offset_size,
14705 struct dwarf2_section_info *section)
2e276125 14706{
cf2c3c16 14707 unsigned int bytes_read;
2e276125 14708
cf2c3c16 14709 switch (form)
2e276125 14710 {
cf2c3c16
TT
14711 case DW_FORM_data1:
14712 case DW_FORM_flag:
14713 ++bytes;
14714 break;
14715
14716 case DW_FORM_data2:
14717 bytes += 2;
14718 break;
14719
14720 case DW_FORM_data4:
14721 bytes += 4;
14722 break;
14723
14724 case DW_FORM_data8:
14725 bytes += 8;
14726 break;
14727
14728 case DW_FORM_string:
14729 read_direct_string (abfd, bytes, &bytes_read);
14730 bytes += bytes_read;
14731 break;
14732
14733 case DW_FORM_sec_offset:
14734 case DW_FORM_strp:
14735 bytes += offset_size;
14736 break;
14737
14738 case DW_FORM_block:
14739 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
14740 bytes += bytes_read;
14741 break;
14742
14743 case DW_FORM_block1:
14744 bytes += 1 + read_1_byte (abfd, bytes);
14745 break;
14746 case DW_FORM_block2:
14747 bytes += 2 + read_2_bytes (abfd, bytes);
14748 break;
14749 case DW_FORM_block4:
14750 bytes += 4 + read_4_bytes (abfd, bytes);
14751 break;
14752
14753 case DW_FORM_sdata:
14754 case DW_FORM_udata:
14755 bytes = skip_leb128 (abfd, bytes);
14756 break;
14757
14758 default:
14759 {
14760 complain:
14761 complaint (&symfile_complaints,
14762 _("invalid form 0x%x in `%s'"),
14763 form,
14764 section->asection->name);
14765 return NULL;
14766 }
2e276125
JB
14767 }
14768
cf2c3c16
TT
14769 return bytes;
14770}
757a13d0 14771
cf2c3c16
TT
14772/* A helper for dwarf_decode_macros that handles skipping an unknown
14773 opcode. Returns an updated pointer to the macro data buffer; or,
14774 on error, issues a complaint and returns NULL. */
757a13d0 14775
cf2c3c16
TT
14776static gdb_byte *
14777skip_unknown_opcode (unsigned int opcode,
14778 gdb_byte **opcode_definitions,
14779 gdb_byte *mac_ptr,
14780 bfd *abfd,
14781 unsigned int offset_size,
14782 struct dwarf2_section_info *section)
14783{
14784 unsigned int bytes_read, i;
14785 unsigned long arg;
14786 gdb_byte *defn;
2e276125 14787
cf2c3c16 14788 if (opcode_definitions[opcode] == NULL)
2e276125 14789 {
cf2c3c16
TT
14790 complaint (&symfile_complaints,
14791 _("unrecognized DW_MACFINO opcode 0x%x"),
14792 opcode);
14793 return NULL;
14794 }
2e276125 14795
cf2c3c16
TT
14796 defn = opcode_definitions[opcode];
14797 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
14798 defn += bytes_read;
2e276125 14799
cf2c3c16
TT
14800 for (i = 0; i < arg; ++i)
14801 {
14802 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
14803 if (mac_ptr == NULL)
14804 {
14805 /* skip_form_bytes already issued the complaint. */
14806 return NULL;
14807 }
14808 }
757a13d0 14809
cf2c3c16
TT
14810 return mac_ptr;
14811}
757a13d0 14812
cf2c3c16
TT
14813/* A helper function which parses the header of a macro section.
14814 If the macro section is the extended (for now called "GNU") type,
14815 then this updates *OFFSET_SIZE. Returns a pointer to just after
14816 the header, or issues a complaint and returns NULL on error. */
757a13d0 14817
cf2c3c16
TT
14818static gdb_byte *
14819dwarf_parse_macro_header (gdb_byte **opcode_definitions,
14820 bfd *abfd,
14821 gdb_byte *mac_ptr,
14822 unsigned int *offset_size,
14823 int section_is_gnu)
14824{
14825 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 14826
cf2c3c16
TT
14827 if (section_is_gnu)
14828 {
14829 unsigned int version, flags;
757a13d0 14830
cf2c3c16
TT
14831 version = read_2_bytes (abfd, mac_ptr);
14832 if (version != 4)
14833 {
14834 complaint (&symfile_complaints,
14835 _("unrecognized version `%d' in .debug_macro section"),
14836 version);
14837 return NULL;
14838 }
14839 mac_ptr += 2;
757a13d0 14840
cf2c3c16
TT
14841 flags = read_1_byte (abfd, mac_ptr);
14842 ++mac_ptr;
14843 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 14844
cf2c3c16
TT
14845 if ((flags & 2) != 0)
14846 /* We don't need the line table offset. */
14847 mac_ptr += *offset_size;
757a13d0 14848
cf2c3c16
TT
14849 /* Vendor opcode descriptions. */
14850 if ((flags & 4) != 0)
14851 {
14852 unsigned int i, count;
757a13d0 14853
cf2c3c16
TT
14854 count = read_1_byte (abfd, mac_ptr);
14855 ++mac_ptr;
14856 for (i = 0; i < count; ++i)
14857 {
14858 unsigned int opcode, bytes_read;
14859 unsigned long arg;
14860
14861 opcode = read_1_byte (abfd, mac_ptr);
14862 ++mac_ptr;
14863 opcode_definitions[opcode] = mac_ptr;
14864 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14865 mac_ptr += bytes_read;
14866 mac_ptr += arg;
14867 }
757a13d0 14868 }
cf2c3c16 14869 }
757a13d0 14870
cf2c3c16
TT
14871 return mac_ptr;
14872}
757a13d0 14873
cf2c3c16
TT
14874/* A helper for dwarf_decode_macros that handles the GNU extensions,
14875 including DW_GNU_MACINFO_transparent_include. */
14876
14877static void
14878dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
14879 struct macro_source_file *current_file,
14880 struct line_header *lh, char *comp_dir,
14881 struct dwarf2_section_info *section,
14882 int section_is_gnu,
14883 unsigned int offset_size,
14884 struct objfile *objfile)
14885{
14886 enum dwarf_macro_record_type macinfo_type;
14887 int at_commandline;
14888 gdb_byte *opcode_definitions[256];
757a13d0 14889
cf2c3c16
TT
14890 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
14891 &offset_size, section_is_gnu);
14892 if (mac_ptr == NULL)
14893 {
14894 /* We already issued a complaint. */
14895 return;
14896 }
757a13d0
JK
14897
14898 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14899 GDB is still reading the definitions from command line. First
14900 DW_MACINFO_start_file will need to be ignored as it was already executed
14901 to create CURRENT_FILE for the main source holding also the command line
14902 definitions. On first met DW_MACINFO_start_file this flag is reset to
14903 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14904
14905 at_commandline = 1;
14906
14907 do
14908 {
14909 /* Do we at least have room for a macinfo type byte? */
14910 if (mac_ptr >= mac_end)
14911 {
cf2c3c16 14912 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
14913 break;
14914 }
14915
14916 macinfo_type = read_1_byte (abfd, mac_ptr);
14917 mac_ptr++;
14918
cf2c3c16
TT
14919 /* Note that we rely on the fact that the corresponding GNU and
14920 DWARF constants are the same. */
757a13d0
JK
14921 switch (macinfo_type)
14922 {
14923 /* A zero macinfo type indicates the end of the macro
14924 information. */
14925 case 0:
14926 break;
2e276125 14927
cf2c3c16
TT
14928 case DW_MACRO_GNU_define:
14929 case DW_MACRO_GNU_undef:
14930 case DW_MACRO_GNU_define_indirect:
14931 case DW_MACRO_GNU_undef_indirect:
2e276125 14932 {
891d2f0b 14933 unsigned int bytes_read;
2e276125
JB
14934 int line;
14935 char *body;
cf2c3c16 14936 int is_define;
2e276125 14937
cf2c3c16
TT
14938 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14939 mac_ptr += bytes_read;
14940
14941 if (macinfo_type == DW_MACRO_GNU_define
14942 || macinfo_type == DW_MACRO_GNU_undef)
14943 {
14944 body = read_direct_string (abfd, mac_ptr, &bytes_read);
14945 mac_ptr += bytes_read;
14946 }
14947 else
14948 {
14949 LONGEST str_offset;
14950
14951 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
14952 mac_ptr += offset_size;
2e276125 14953
cf2c3c16
TT
14954 body = read_indirect_string_at_offset (abfd, str_offset);
14955 }
14956
14957 is_define = (macinfo_type == DW_MACRO_GNU_define
14958 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 14959 if (! current_file)
757a13d0
JK
14960 {
14961 /* DWARF violation as no main source is present. */
14962 complaint (&symfile_complaints,
14963 _("debug info with no main source gives macro %s "
14964 "on line %d: %s"),
cf2c3c16
TT
14965 is_define ? _("definition") : _("undefinition"),
14966 line, body);
757a13d0
JK
14967 break;
14968 }
3e43a32a
MS
14969 if ((line == 0 && !at_commandline)
14970 || (line != 0 && at_commandline))
4d3c2250 14971 complaint (&symfile_complaints,
757a13d0
JK
14972 _("debug info gives %s macro %s with %s line %d: %s"),
14973 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 14974 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
14975 line == 0 ? _("zero") : _("non-zero"), line, body);
14976
cf2c3c16 14977 if (is_define)
757a13d0 14978 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
14979 else
14980 {
14981 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
14982 || macinfo_type == DW_MACRO_GNU_undef_indirect);
14983 macro_undef (current_file, line, body);
14984 }
2e276125
JB
14985 }
14986 break;
14987
cf2c3c16 14988 case DW_MACRO_GNU_start_file:
2e276125 14989 {
891d2f0b 14990 unsigned int bytes_read;
2e276125
JB
14991 int line, file;
14992
14993 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14994 mac_ptr += bytes_read;
14995 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14996 mac_ptr += bytes_read;
14997
3e43a32a
MS
14998 if ((line == 0 && !at_commandline)
14999 || (line != 0 && at_commandline))
757a13d0
JK
15000 complaint (&symfile_complaints,
15001 _("debug info gives source %d included "
15002 "from %s at %s line %d"),
15003 file, at_commandline ? _("command-line") : _("file"),
15004 line == 0 ? _("zero") : _("non-zero"), line);
15005
15006 if (at_commandline)
15007 {
cf2c3c16
TT
15008 /* This DW_MACRO_GNU_start_file was executed in the
15009 pass one. */
757a13d0
JK
15010 at_commandline = 0;
15011 }
15012 else
15013 current_file = macro_start_file (file, line,
15014 current_file, comp_dir,
cf2c3c16 15015 lh, objfile);
2e276125
JB
15016 }
15017 break;
15018
cf2c3c16 15019 case DW_MACRO_GNU_end_file:
2e276125 15020 if (! current_file)
4d3c2250 15021 complaint (&symfile_complaints,
3e43a32a
MS
15022 _("macro debug info has an unmatched "
15023 "`close_file' directive"));
2e276125
JB
15024 else
15025 {
15026 current_file = current_file->included_by;
15027 if (! current_file)
15028 {
cf2c3c16 15029 enum dwarf_macro_record_type next_type;
2e276125
JB
15030
15031 /* GCC circa March 2002 doesn't produce the zero
15032 type byte marking the end of the compilation
15033 unit. Complain if it's not there, but exit no
15034 matter what. */
15035
15036 /* Do we at least have room for a macinfo type byte? */
15037 if (mac_ptr >= mac_end)
15038 {
cf2c3c16 15039 dwarf2_macros_too_long_complaint (section);
2e276125
JB
15040 return;
15041 }
15042
15043 /* We don't increment mac_ptr here, so this is just
15044 a look-ahead. */
15045 next_type = read_1_byte (abfd, mac_ptr);
15046 if (next_type != 0)
4d3c2250 15047 complaint (&symfile_complaints,
3e43a32a
MS
15048 _("no terminating 0-type entry for "
15049 "macros in `.debug_macinfo' section"));
2e276125
JB
15050
15051 return;
15052 }
15053 }
15054 break;
15055
cf2c3c16
TT
15056 case DW_MACRO_GNU_transparent_include:
15057 {
15058 LONGEST offset;
15059
15060 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15061 mac_ptr += offset_size;
15062
15063 dwarf_decode_macro_bytes (abfd,
15064 section->buffer + offset,
15065 mac_end, current_file,
15066 lh, comp_dir,
15067 section, section_is_gnu,
15068 offset_size, objfile);
15069 }
15070 break;
15071
2e276125 15072 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
15073 if (!section_is_gnu)
15074 {
15075 unsigned int bytes_read;
15076 int constant;
2e276125 15077
cf2c3c16
TT
15078 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15079 mac_ptr += bytes_read;
15080 read_direct_string (abfd, mac_ptr, &bytes_read);
15081 mac_ptr += bytes_read;
2e276125 15082
cf2c3c16
TT
15083 /* We don't recognize any vendor extensions. */
15084 break;
15085 }
15086 /* FALLTHROUGH */
15087
15088 default:
15089 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15090 mac_ptr, abfd, offset_size,
15091 section);
15092 if (mac_ptr == NULL)
15093 return;
15094 break;
2e276125 15095 }
757a13d0 15096 } while (macinfo_type != 0);
2e276125 15097}
8e19ed76 15098
cf2c3c16
TT
15099static void
15100dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15101 char *comp_dir, bfd *abfd,
15102 struct dwarf2_cu *cu,
15103 struct dwarf2_section_info *section,
15104 int section_is_gnu)
15105{
15106 gdb_byte *mac_ptr, *mac_end;
15107 struct macro_source_file *current_file = 0;
15108 enum dwarf_macro_record_type macinfo_type;
15109 unsigned int offset_size = cu->header.offset_size;
15110 gdb_byte *opcode_definitions[256];
15111
15112 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15113 if (section->buffer == NULL)
15114 {
15115 complaint (&symfile_complaints, _("missing %s section"),
15116 section->asection->name);
15117 return;
15118 }
15119
15120 /* First pass: Find the name of the base filename.
15121 This filename is needed in order to process all macros whose definition
15122 (or undefinition) comes from the command line. These macros are defined
15123 before the first DW_MACINFO_start_file entry, and yet still need to be
15124 associated to the base file.
15125
15126 To determine the base file name, we scan the macro definitions until we
15127 reach the first DW_MACINFO_start_file entry. We then initialize
15128 CURRENT_FILE accordingly so that any macro definition found before the
15129 first DW_MACINFO_start_file can still be associated to the base file. */
15130
15131 mac_ptr = section->buffer + offset;
15132 mac_end = section->buffer + section->size;
15133
15134 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15135 &offset_size, section_is_gnu);
15136 if (mac_ptr == NULL)
15137 {
15138 /* We already issued a complaint. */
15139 return;
15140 }
15141
15142 do
15143 {
15144 /* Do we at least have room for a macinfo type byte? */
15145 if (mac_ptr >= mac_end)
15146 {
15147 /* Complaint is printed during the second pass as GDB will probably
15148 stop the first pass earlier upon finding
15149 DW_MACINFO_start_file. */
15150 break;
15151 }
15152
15153 macinfo_type = read_1_byte (abfd, mac_ptr);
15154 mac_ptr++;
15155
15156 /* Note that we rely on the fact that the corresponding GNU and
15157 DWARF constants are the same. */
15158 switch (macinfo_type)
15159 {
15160 /* A zero macinfo type indicates the end of the macro
15161 information. */
15162 case 0:
15163 break;
15164
15165 case DW_MACRO_GNU_define:
15166 case DW_MACRO_GNU_undef:
15167 /* Only skip the data by MAC_PTR. */
15168 {
15169 unsigned int bytes_read;
15170
15171 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15172 mac_ptr += bytes_read;
15173 read_direct_string (abfd, mac_ptr, &bytes_read);
15174 mac_ptr += bytes_read;
15175 }
15176 break;
15177
15178 case DW_MACRO_GNU_start_file:
15179 {
15180 unsigned int bytes_read;
15181 int line, file;
15182
15183 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15184 mac_ptr += bytes_read;
15185 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15186 mac_ptr += bytes_read;
15187
15188 current_file = macro_start_file (file, line, current_file,
15189 comp_dir, lh, cu->objfile);
15190 }
15191 break;
15192
15193 case DW_MACRO_GNU_end_file:
15194 /* No data to skip by MAC_PTR. */
15195 break;
15196
15197 case DW_MACRO_GNU_define_indirect:
15198 case DW_MACRO_GNU_undef_indirect:
15199 {
15200 unsigned int bytes_read;
15201
15202 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15203 mac_ptr += bytes_read;
15204 mac_ptr += offset_size;
15205 }
15206 break;
15207
15208 case DW_MACRO_GNU_transparent_include:
15209 /* Note that, according to the spec, a transparent include
15210 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15211 skip this opcode. */
15212 mac_ptr += offset_size;
15213 break;
15214
15215 case DW_MACINFO_vendor_ext:
15216 /* Only skip the data by MAC_PTR. */
15217 if (!section_is_gnu)
15218 {
15219 unsigned int bytes_read;
15220
15221 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15222 mac_ptr += bytes_read;
15223 read_direct_string (abfd, mac_ptr, &bytes_read);
15224 mac_ptr += bytes_read;
15225 }
15226 /* FALLTHROUGH */
15227
15228 default:
15229 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15230 mac_ptr, abfd, offset_size,
15231 section);
15232 if (mac_ptr == NULL)
15233 return;
15234 break;
15235 }
15236 } while (macinfo_type != 0 && current_file == NULL);
15237
15238 /* Second pass: Process all entries.
15239
15240 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15241 command-line macro definitions/undefinitions. This flag is unset when we
15242 reach the first DW_MACINFO_start_file entry. */
15243
15244 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15245 current_file, lh, comp_dir, section, section_is_gnu,
15246 offset_size, cu->objfile);
15247}
15248
8e19ed76 15249/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 15250 if so return true else false. */
8e19ed76
PS
15251static int
15252attr_form_is_block (struct attribute *attr)
15253{
15254 return (attr == NULL ? 0 :
15255 attr->form == DW_FORM_block1
15256 || attr->form == DW_FORM_block2
15257 || attr->form == DW_FORM_block4
2dc7f7b3
TT
15258 || attr->form == DW_FORM_block
15259 || attr->form == DW_FORM_exprloc);
8e19ed76 15260}
4c2df51b 15261
c6a0999f
JB
15262/* Return non-zero if ATTR's value is a section offset --- classes
15263 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15264 You may use DW_UNSND (attr) to retrieve such offsets.
15265
15266 Section 7.5.4, "Attribute Encodings", explains that no attribute
15267 may have a value that belongs to more than one of these classes; it
15268 would be ambiguous if we did, because we use the same forms for all
15269 of them. */
3690dd37
JB
15270static int
15271attr_form_is_section_offset (struct attribute *attr)
15272{
15273 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
15274 || attr->form == DW_FORM_data8
15275 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
15276}
15277
15278
15279/* Return non-zero if ATTR's value falls in the 'constant' class, or
15280 zero otherwise. When this function returns true, you can apply
15281 dwarf2_get_attr_constant_value to it.
15282
15283 However, note that for some attributes you must check
15284 attr_form_is_section_offset before using this test. DW_FORM_data4
15285 and DW_FORM_data8 are members of both the constant class, and of
15286 the classes that contain offsets into other debug sections
15287 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15288 that, if an attribute's can be either a constant or one of the
15289 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15290 taken as section offsets, not constants. */
15291static int
15292attr_form_is_constant (struct attribute *attr)
15293{
15294 switch (attr->form)
15295 {
15296 case DW_FORM_sdata:
15297 case DW_FORM_udata:
15298 case DW_FORM_data1:
15299 case DW_FORM_data2:
15300 case DW_FORM_data4:
15301 case DW_FORM_data8:
15302 return 1;
15303 default:
15304 return 0;
15305 }
15306}
15307
8cf6f0b1
TT
15308/* A helper function that fills in a dwarf2_loclist_baton. */
15309
15310static void
15311fill_in_loclist_baton (struct dwarf2_cu *cu,
15312 struct dwarf2_loclist_baton *baton,
15313 struct attribute *attr)
15314{
15315 dwarf2_read_section (dwarf2_per_objfile->objfile,
15316 &dwarf2_per_objfile->loc);
15317
15318 baton->per_cu = cu->per_cu;
15319 gdb_assert (baton->per_cu);
15320 /* We don't know how long the location list is, but make sure we
15321 don't run off the edge of the section. */
15322 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15323 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15324 baton->base_address = cu->base_address;
15325}
15326
4c2df51b
DJ
15327static void
15328dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 15329 struct dwarf2_cu *cu)
4c2df51b 15330{
3690dd37 15331 if (attr_form_is_section_offset (attr)
99bcc461
DJ
15332 /* ".debug_loc" may not exist at all, or the offset may be outside
15333 the section. If so, fall through to the complaint in the
15334 other branch. */
9e0ac564
TT
15335 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15336 &dwarf2_per_objfile->loc))
4c2df51b 15337 {
0d53c4c4 15338 struct dwarf2_loclist_baton *baton;
4c2df51b 15339
4a146b47 15340 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 15341 sizeof (struct dwarf2_loclist_baton));
4c2df51b 15342
8cf6f0b1 15343 fill_in_loclist_baton (cu, baton, attr);
be391dca 15344
d00adf39 15345 if (cu->base_known == 0)
0d53c4c4 15346 complaint (&symfile_complaints,
3e43a32a
MS
15347 _("Location list used without "
15348 "specifying the CU base address."));
4c2df51b 15349
768a979c 15350 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
15351 SYMBOL_LOCATION_BATON (sym) = baton;
15352 }
15353 else
15354 {
15355 struct dwarf2_locexpr_baton *baton;
15356
4a146b47 15357 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 15358 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
15359 baton->per_cu = cu->per_cu;
15360 gdb_assert (baton->per_cu);
0d53c4c4
DJ
15361
15362 if (attr_form_is_block (attr))
15363 {
15364 /* Note that we're just copying the block's data pointer
15365 here, not the actual data. We're still pointing into the
6502dd73
DJ
15366 info_buffer for SYM's objfile; right now we never release
15367 that buffer, but when we do clean up properly this may
15368 need to change. */
0d53c4c4
DJ
15369 baton->size = DW_BLOCK (attr)->size;
15370 baton->data = DW_BLOCK (attr)->data;
15371 }
15372 else
15373 {
15374 dwarf2_invalid_attrib_class_complaint ("location description",
15375 SYMBOL_NATURAL_NAME (sym));
15376 baton->size = 0;
15377 baton->data = NULL;
15378 }
6e70227d 15379
768a979c 15380 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
15381 SYMBOL_LOCATION_BATON (sym) = baton;
15382 }
4c2df51b 15383}
6502dd73 15384
9aa1f1e3
TT
15385/* Return the OBJFILE associated with the compilation unit CU. If CU
15386 came from a separate debuginfo file, then the master objfile is
15387 returned. */
ae0d2f24
UW
15388
15389struct objfile *
15390dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15391{
9291a0cd 15392 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15393
15394 /* Return the master objfile, so that we can report and look up the
15395 correct file containing this variable. */
15396 if (objfile->separate_debug_objfile_backlink)
15397 objfile = objfile->separate_debug_objfile_backlink;
15398
15399 return objfile;
15400}
15401
15402/* Return the address size given in the compilation unit header for CU. */
15403
15404CORE_ADDR
15405dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15406{
15407 if (per_cu->cu)
15408 return per_cu->cu->header.addr_size;
15409 else
15410 {
15411 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 15412 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15413 struct dwarf2_per_objfile *per_objfile
15414 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 15415 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 15416 struct comp_unit_head cu_header;
9a619af0 15417
ae0d2f24
UW
15418 memset (&cu_header, 0, sizeof cu_header);
15419 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
15420 return cu_header.addr_size;
15421 }
15422}
15423
9eae7c52
TT
15424/* Return the offset size given in the compilation unit header for CU. */
15425
15426int
15427dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15428{
15429 if (per_cu->cu)
15430 return per_cu->cu->header.offset_size;
15431 else
15432 {
15433 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 15434 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
15435 struct dwarf2_per_objfile *per_objfile
15436 = objfile_data (objfile, dwarf2_objfile_data_key);
15437 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
15438 struct comp_unit_head cu_header;
15439
15440 memset (&cu_header, 0, sizeof cu_header);
15441 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
15442 return cu_header.offset_size;
15443 }
15444}
15445
9aa1f1e3
TT
15446/* Return the text offset of the CU. The returned offset comes from
15447 this CU's objfile. If this objfile came from a separate debuginfo
15448 file, then the offset may be different from the corresponding
15449 offset in the parent objfile. */
15450
15451CORE_ADDR
15452dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15453{
bb3fa9d0 15454 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
15455
15456 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15457}
15458
348e048f
DE
15459/* Locate the .debug_info compilation unit from CU's objfile which contains
15460 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
15461
15462static struct dwarf2_per_cu_data *
c764a876 15463dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
15464 struct objfile *objfile)
15465{
15466 struct dwarf2_per_cu_data *this_cu;
15467 int low, high;
15468
ae038cb0
DJ
15469 low = 0;
15470 high = dwarf2_per_objfile->n_comp_units - 1;
15471 while (high > low)
15472 {
15473 int mid = low + (high - low) / 2;
9a619af0 15474
ae038cb0
DJ
15475 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15476 high = mid;
15477 else
15478 low = mid + 1;
15479 }
15480 gdb_assert (low == high);
15481 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15482 {
10b3939b 15483 if (low == 0)
8a3fe4f8
AC
15484 error (_("Dwarf Error: could not find partial DIE containing "
15485 "offset 0x%lx [in module %s]"),
10b3939b
DJ
15486 (long) offset, bfd_get_filename (objfile->obfd));
15487
ae038cb0
DJ
15488 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15489 return dwarf2_per_objfile->all_comp_units[low-1];
15490 }
15491 else
15492 {
15493 this_cu = dwarf2_per_objfile->all_comp_units[low];
15494 if (low == dwarf2_per_objfile->n_comp_units - 1
15495 && offset >= this_cu->offset + this_cu->length)
c764a876 15496 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
15497 gdb_assert (offset < this_cu->offset + this_cu->length);
15498 return this_cu;
15499 }
15500}
15501
10b3939b
DJ
15502/* Locate the compilation unit from OBJFILE which is located at exactly
15503 OFFSET. Raises an error on failure. */
15504
ae038cb0 15505static struct dwarf2_per_cu_data *
c764a876 15506dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
15507{
15508 struct dwarf2_per_cu_data *this_cu;
9a619af0 15509
ae038cb0
DJ
15510 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15511 if (this_cu->offset != offset)
c764a876 15512 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
15513 return this_cu;
15514}
15515
9816fde3 15516/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 15517
9816fde3
JK
15518static void
15519init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 15520{
9816fde3 15521 memset (cu, 0, sizeof (*cu));
93311388
DE
15522 cu->objfile = objfile;
15523 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15524}
15525
15526/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15527
15528static void
15529prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15530{
15531 struct attribute *attr;
15532
15533 /* Set the language we're debugging. */
15534 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15535 if (attr)
15536 set_cu_language (DW_UNSND (attr), cu);
15537 else
9cded63f
TT
15538 {
15539 cu->language = language_minimal;
15540 cu->language_defn = language_def (cu->language);
15541 }
93311388
DE
15542}
15543
ae038cb0
DJ
15544/* Release one cached compilation unit, CU. We unlink it from the tree
15545 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15546 the caller is responsible for that.
15547 NOTE: DATA is a void * because this function is also used as a
15548 cleanup routine. */
ae038cb0
DJ
15549
15550static void
15551free_one_comp_unit (void *data)
15552{
15553 struct dwarf2_cu *cu = data;
15554
15555 if (cu->per_cu != NULL)
15556 cu->per_cu->cu = NULL;
15557 cu->per_cu = NULL;
15558
15559 obstack_free (&cu->comp_unit_obstack, NULL);
15560
15561 xfree (cu);
15562}
15563
72bf9492 15564/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
15565 when we're finished with it. We can't free the pointer itself, but be
15566 sure to unlink it from the cache. Also release any associated storage
15567 and perform cache maintenance.
72bf9492
DJ
15568
15569 Only used during partial symbol parsing. */
15570
15571static void
15572free_stack_comp_unit (void *data)
15573{
15574 struct dwarf2_cu *cu = data;
15575
15576 obstack_free (&cu->comp_unit_obstack, NULL);
15577 cu->partial_dies = NULL;
ae038cb0
DJ
15578
15579 if (cu->per_cu != NULL)
15580 {
15581 /* This compilation unit is on the stack in our caller, so we
15582 should not xfree it. Just unlink it. */
15583 cu->per_cu->cu = NULL;
15584 cu->per_cu = NULL;
15585
15586 /* If we had a per-cu pointer, then we may have other compilation
15587 units loaded, so age them now. */
15588 age_cached_comp_units ();
15589 }
15590}
15591
15592/* Free all cached compilation units. */
15593
15594static void
15595free_cached_comp_units (void *data)
15596{
15597 struct dwarf2_per_cu_data *per_cu, **last_chain;
15598
15599 per_cu = dwarf2_per_objfile->read_in_chain;
15600 last_chain = &dwarf2_per_objfile->read_in_chain;
15601 while (per_cu != NULL)
15602 {
15603 struct dwarf2_per_cu_data *next_cu;
15604
15605 next_cu = per_cu->cu->read_in_chain;
15606
15607 free_one_comp_unit (per_cu->cu);
15608 *last_chain = next_cu;
15609
15610 per_cu = next_cu;
15611 }
15612}
15613
15614/* Increase the age counter on each cached compilation unit, and free
15615 any that are too old. */
15616
15617static void
15618age_cached_comp_units (void)
15619{
15620 struct dwarf2_per_cu_data *per_cu, **last_chain;
15621
15622 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15623 per_cu = dwarf2_per_objfile->read_in_chain;
15624 while (per_cu != NULL)
15625 {
15626 per_cu->cu->last_used ++;
15627 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15628 dwarf2_mark (per_cu->cu);
15629 per_cu = per_cu->cu->read_in_chain;
15630 }
15631
15632 per_cu = dwarf2_per_objfile->read_in_chain;
15633 last_chain = &dwarf2_per_objfile->read_in_chain;
15634 while (per_cu != NULL)
15635 {
15636 struct dwarf2_per_cu_data *next_cu;
15637
15638 next_cu = per_cu->cu->read_in_chain;
15639
15640 if (!per_cu->cu->mark)
15641 {
15642 free_one_comp_unit (per_cu->cu);
15643 *last_chain = next_cu;
15644 }
15645 else
15646 last_chain = &per_cu->cu->read_in_chain;
15647
15648 per_cu = next_cu;
15649 }
15650}
15651
15652/* Remove a single compilation unit from the cache. */
15653
15654static void
15655free_one_cached_comp_unit (void *target_cu)
15656{
15657 struct dwarf2_per_cu_data *per_cu, **last_chain;
15658
15659 per_cu = dwarf2_per_objfile->read_in_chain;
15660 last_chain = &dwarf2_per_objfile->read_in_chain;
15661 while (per_cu != NULL)
15662 {
15663 struct dwarf2_per_cu_data *next_cu;
15664
15665 next_cu = per_cu->cu->read_in_chain;
15666
15667 if (per_cu->cu == target_cu)
15668 {
15669 free_one_comp_unit (per_cu->cu);
15670 *last_chain = next_cu;
15671 break;
15672 }
15673 else
15674 last_chain = &per_cu->cu->read_in_chain;
15675
15676 per_cu = next_cu;
15677 }
15678}
15679
fe3e1990
DJ
15680/* Release all extra memory associated with OBJFILE. */
15681
15682void
15683dwarf2_free_objfile (struct objfile *objfile)
15684{
15685 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15686
15687 if (dwarf2_per_objfile == NULL)
15688 return;
15689
15690 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15691 free_cached_comp_units (NULL);
15692
7b9f3c50
DE
15693 if (dwarf2_per_objfile->quick_file_names_table)
15694 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 15695
fe3e1990
DJ
15696 /* Everything else should be on the objfile obstack. */
15697}
15698
1c379e20
DJ
15699/* A pair of DIE offset and GDB type pointer. We store these
15700 in a hash table separate from the DIEs, and preserve them
15701 when the DIEs are flushed out of cache. */
15702
15703struct dwarf2_offset_and_type
15704{
15705 unsigned int offset;
15706 struct type *type;
15707};
15708
15709/* Hash function for a dwarf2_offset_and_type. */
15710
15711static hashval_t
15712offset_and_type_hash (const void *item)
15713{
15714 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 15715
1c379e20
DJ
15716 return ofs->offset;
15717}
15718
15719/* Equality function for a dwarf2_offset_and_type. */
15720
15721static int
15722offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15723{
15724 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15725 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 15726
1c379e20
DJ
15727 return ofs_lhs->offset == ofs_rhs->offset;
15728}
15729
15730/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
15731 table if necessary. For convenience, return TYPE.
15732
15733 The DIEs reading must have careful ordering to:
15734 * Not cause infite loops trying to read in DIEs as a prerequisite for
15735 reading current DIE.
15736 * Not trying to dereference contents of still incompletely read in types
15737 while reading in other DIEs.
15738 * Enable referencing still incompletely read in types just by a pointer to
15739 the type without accessing its fields.
15740
15741 Therefore caller should follow these rules:
15742 * Try to fetch any prerequisite types we may need to build this DIE type
15743 before building the type and calling set_die_type.
e71ec853 15744 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
15745 possible before fetching more types to complete the current type.
15746 * Make the type as complete as possible before fetching more types. */
1c379e20 15747
f792889a 15748static struct type *
1c379e20
DJ
15749set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15750{
15751 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
15752 struct objfile *objfile = cu->objfile;
15753 htab_t *type_hash_ptr;
1c379e20 15754
b4ba55a1
JB
15755 /* For Ada types, make sure that the gnat-specific data is always
15756 initialized (if not already set). There are a few types where
15757 we should not be doing so, because the type-specific area is
15758 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15759 where the type-specific area is used to store the floatformat).
15760 But this is not a problem, because the gnat-specific information
15761 is actually not needed for these types. */
15762 if (need_gnat_info (cu)
15763 && TYPE_CODE (type) != TYPE_CODE_FUNC
15764 && TYPE_CODE (type) != TYPE_CODE_FLT
15765 && !HAVE_GNAT_AUX_INFO (type))
15766 INIT_GNAT_SPECIFIC (type);
15767
8b70b953 15768 if (cu->per_cu->debug_type_section)
673bfd45
DE
15769 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15770 else
15771 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15772
15773 if (*type_hash_ptr == NULL)
f792889a 15774 {
673bfd45
DE
15775 *type_hash_ptr
15776 = htab_create_alloc_ex (127,
f792889a
DJ
15777 offset_and_type_hash,
15778 offset_and_type_eq,
15779 NULL,
673bfd45 15780 &objfile->objfile_obstack,
f792889a
DJ
15781 hashtab_obstack_allocate,
15782 dummy_obstack_deallocate);
f792889a 15783 }
1c379e20
DJ
15784
15785 ofs.offset = die->offset;
15786 ofs.type = type;
15787 slot = (struct dwarf2_offset_and_type **)
673bfd45 15788 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
15789 if (*slot)
15790 complaint (&symfile_complaints,
15791 _("A problem internal to GDB: DIE 0x%x has type already set"),
15792 die->offset);
673bfd45 15793 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 15794 **slot = ofs;
f792889a 15795 return type;
1c379e20
DJ
15796}
15797
673bfd45
DE
15798/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15799 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
15800
15801static struct type *
673bfd45
DE
15802get_die_type_at_offset (unsigned int offset,
15803 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
15804{
15805 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 15806 htab_t type_hash;
f792889a 15807
8b70b953 15808 if (per_cu->debug_type_section)
673bfd45
DE
15809 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15810 else
15811 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
15812 if (type_hash == NULL)
15813 return NULL;
1c379e20 15814
673bfd45 15815 ofs.offset = offset;
1c379e20
DJ
15816 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15817 if (slot)
15818 return slot->type;
15819 else
15820 return NULL;
15821}
15822
673bfd45
DE
15823/* Look up the type for DIE in the appropriate type_hash table,
15824 or return NULL if DIE does not have a saved type. */
15825
15826static struct type *
15827get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15828{
15829 return get_die_type_at_offset (die->offset, cu->per_cu);
15830}
15831
10b3939b
DJ
15832/* Add a dependence relationship from CU to REF_PER_CU. */
15833
15834static void
15835dwarf2_add_dependence (struct dwarf2_cu *cu,
15836 struct dwarf2_per_cu_data *ref_per_cu)
15837{
15838 void **slot;
15839
15840 if (cu->dependencies == NULL)
15841 cu->dependencies
15842 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15843 NULL, &cu->comp_unit_obstack,
15844 hashtab_obstack_allocate,
15845 dummy_obstack_deallocate);
15846
15847 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15848 if (*slot == NULL)
15849 *slot = ref_per_cu;
15850}
1c379e20 15851
f504f079
DE
15852/* Subroutine of dwarf2_mark to pass to htab_traverse.
15853 Set the mark field in every compilation unit in the
ae038cb0
DJ
15854 cache that we must keep because we are keeping CU. */
15855
10b3939b
DJ
15856static int
15857dwarf2_mark_helper (void **slot, void *data)
15858{
15859 struct dwarf2_per_cu_data *per_cu;
15860
15861 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
15862
15863 /* cu->dependencies references may not yet have been ever read if QUIT aborts
15864 reading of the chain. As such dependencies remain valid it is not much
15865 useful to track and undo them during QUIT cleanups. */
15866 if (per_cu->cu == NULL)
15867 return 1;
15868
10b3939b
DJ
15869 if (per_cu->cu->mark)
15870 return 1;
15871 per_cu->cu->mark = 1;
15872
15873 if (per_cu->cu->dependencies != NULL)
15874 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15875
15876 return 1;
15877}
15878
f504f079
DE
15879/* Set the mark field in CU and in every other compilation unit in the
15880 cache that we must keep because we are keeping CU. */
15881
ae038cb0
DJ
15882static void
15883dwarf2_mark (struct dwarf2_cu *cu)
15884{
15885 if (cu->mark)
15886 return;
15887 cu->mark = 1;
10b3939b
DJ
15888 if (cu->dependencies != NULL)
15889 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
15890}
15891
15892static void
15893dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15894{
15895 while (per_cu)
15896 {
15897 per_cu->cu->mark = 0;
15898 per_cu = per_cu->cu->read_in_chain;
15899 }
72bf9492
DJ
15900}
15901
72bf9492
DJ
15902/* Trivial hash function for partial_die_info: the hash value of a DIE
15903 is its offset in .debug_info for this objfile. */
15904
15905static hashval_t
15906partial_die_hash (const void *item)
15907{
15908 const struct partial_die_info *part_die = item;
9a619af0 15909
72bf9492
DJ
15910 return part_die->offset;
15911}
15912
15913/* Trivial comparison function for partial_die_info structures: two DIEs
15914 are equal if they have the same offset. */
15915
15916static int
15917partial_die_eq (const void *item_lhs, const void *item_rhs)
15918{
15919 const struct partial_die_info *part_die_lhs = item_lhs;
15920 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 15921
72bf9492
DJ
15922 return part_die_lhs->offset == part_die_rhs->offset;
15923}
15924
ae038cb0
DJ
15925static struct cmd_list_element *set_dwarf2_cmdlist;
15926static struct cmd_list_element *show_dwarf2_cmdlist;
15927
15928static void
15929set_dwarf2_cmd (char *args, int from_tty)
15930{
15931 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15932}
15933
15934static void
15935show_dwarf2_cmd (char *args, int from_tty)
6e70227d 15936{
ae038cb0
DJ
15937 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15938}
15939
dce234bc
PP
15940/* If section described by INFO was mmapped, munmap it now. */
15941
15942static void
15943munmap_section_buffer (struct dwarf2_section_info *info)
15944{
b315ab21 15945 if (info->map_addr != NULL)
dce234bc
PP
15946 {
15947#ifdef HAVE_MMAP
b315ab21 15948 int res;
9a619af0 15949
b315ab21
TG
15950 res = munmap (info->map_addr, info->map_len);
15951 gdb_assert (res == 0);
dce234bc
PP
15952#else
15953 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 15954 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
15955#endif
15956 }
15957}
15958
15959/* munmap debug sections for OBJFILE, if necessary. */
15960
15961static void
c1bd65d0 15962dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
15963{
15964 struct dwarf2_per_objfile *data = d;
8b70b953
TT
15965 int ix;
15966 struct dwarf2_section_info *section;
9a619af0 15967
16be1145
DE
15968 /* This is sorted according to the order they're defined in to make it easier
15969 to keep in sync. */
dce234bc
PP
15970 munmap_section_buffer (&data->info);
15971 munmap_section_buffer (&data->abbrev);
15972 munmap_section_buffer (&data->line);
16be1145 15973 munmap_section_buffer (&data->loc);
dce234bc 15974 munmap_section_buffer (&data->macinfo);
cf2c3c16 15975 munmap_section_buffer (&data->macro);
16be1145 15976 munmap_section_buffer (&data->str);
dce234bc 15977 munmap_section_buffer (&data->ranges);
dce234bc
PP
15978 munmap_section_buffer (&data->frame);
15979 munmap_section_buffer (&data->eh_frame);
9291a0cd 15980 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
15981
15982 for (ix = 0;
15983 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
15984 ++ix)
15985 munmap_section_buffer (section);
15986
15987 VEC_free (dwarf2_section_info_def, data->types);
9291a0cd
TT
15988}
15989
15990\f
ae2de4f8 15991/* The "save gdb-index" command. */
9291a0cd
TT
15992
15993/* The contents of the hash table we create when building the string
15994 table. */
15995struct strtab_entry
15996{
15997 offset_type offset;
15998 const char *str;
15999};
16000
559a7a62
JK
16001/* Hash function for a strtab_entry.
16002
16003 Function is used only during write_hash_table so no index format backward
16004 compatibility is needed. */
b89be57b 16005
9291a0cd
TT
16006static hashval_t
16007hash_strtab_entry (const void *e)
16008{
16009 const struct strtab_entry *entry = e;
559a7a62 16010 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
16011}
16012
16013/* Equality function for a strtab_entry. */
b89be57b 16014
9291a0cd
TT
16015static int
16016eq_strtab_entry (const void *a, const void *b)
16017{
16018 const struct strtab_entry *ea = a;
16019 const struct strtab_entry *eb = b;
16020 return !strcmp (ea->str, eb->str);
16021}
16022
16023/* Create a strtab_entry hash table. */
b89be57b 16024
9291a0cd
TT
16025static htab_t
16026create_strtab (void)
16027{
16028 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16029 xfree, xcalloc, xfree);
16030}
16031
16032/* Add a string to the constant pool. Return the string's offset in
16033 host order. */
b89be57b 16034
9291a0cd
TT
16035static offset_type
16036add_string (htab_t table, struct obstack *cpool, const char *str)
16037{
16038 void **slot;
16039 struct strtab_entry entry;
16040 struct strtab_entry *result;
16041
16042 entry.str = str;
16043 slot = htab_find_slot (table, &entry, INSERT);
16044 if (*slot)
16045 result = *slot;
16046 else
16047 {
16048 result = XNEW (struct strtab_entry);
16049 result->offset = obstack_object_size (cpool);
16050 result->str = str;
16051 obstack_grow_str0 (cpool, str);
16052 *slot = result;
16053 }
16054 return result->offset;
16055}
16056
16057/* An entry in the symbol table. */
16058struct symtab_index_entry
16059{
16060 /* The name of the symbol. */
16061 const char *name;
16062 /* The offset of the name in the constant pool. */
16063 offset_type index_offset;
16064 /* A sorted vector of the indices of all the CUs that hold an object
16065 of this name. */
16066 VEC (offset_type) *cu_indices;
16067};
16068
16069/* The symbol table. This is a power-of-2-sized hash table. */
16070struct mapped_symtab
16071{
16072 offset_type n_elements;
16073 offset_type size;
16074 struct symtab_index_entry **data;
16075};
16076
16077/* Hash function for a symtab_index_entry. */
b89be57b 16078
9291a0cd
TT
16079static hashval_t
16080hash_symtab_entry (const void *e)
16081{
16082 const struct symtab_index_entry *entry = e;
16083 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16084 sizeof (offset_type) * VEC_length (offset_type,
16085 entry->cu_indices),
16086 0);
16087}
16088
16089/* Equality function for a symtab_index_entry. */
b89be57b 16090
9291a0cd
TT
16091static int
16092eq_symtab_entry (const void *a, const void *b)
16093{
16094 const struct symtab_index_entry *ea = a;
16095 const struct symtab_index_entry *eb = b;
16096 int len = VEC_length (offset_type, ea->cu_indices);
16097 if (len != VEC_length (offset_type, eb->cu_indices))
16098 return 0;
16099 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16100 VEC_address (offset_type, eb->cu_indices),
16101 sizeof (offset_type) * len);
16102}
16103
16104/* Destroy a symtab_index_entry. */
b89be57b 16105
9291a0cd
TT
16106static void
16107delete_symtab_entry (void *p)
16108{
16109 struct symtab_index_entry *entry = p;
16110 VEC_free (offset_type, entry->cu_indices);
16111 xfree (entry);
16112}
16113
16114/* Create a hash table holding symtab_index_entry objects. */
b89be57b 16115
9291a0cd 16116static htab_t
3876f04e 16117create_symbol_hash_table (void)
9291a0cd
TT
16118{
16119 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16120 delete_symtab_entry, xcalloc, xfree);
16121}
16122
16123/* Create a new mapped symtab object. */
b89be57b 16124
9291a0cd
TT
16125static struct mapped_symtab *
16126create_mapped_symtab (void)
16127{
16128 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16129 symtab->n_elements = 0;
16130 symtab->size = 1024;
16131 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16132 return symtab;
16133}
16134
16135/* Destroy a mapped_symtab. */
b89be57b 16136
9291a0cd
TT
16137static void
16138cleanup_mapped_symtab (void *p)
16139{
16140 struct mapped_symtab *symtab = p;
16141 /* The contents of the array are freed when the other hash table is
16142 destroyed. */
16143 xfree (symtab->data);
16144 xfree (symtab);
16145}
16146
16147/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
16148 the slot.
16149
16150 Function is used only during write_hash_table so no index format backward
16151 compatibility is needed. */
b89be57b 16152
9291a0cd
TT
16153static struct symtab_index_entry **
16154find_slot (struct mapped_symtab *symtab, const char *name)
16155{
559a7a62 16156 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
16157
16158 index = hash & (symtab->size - 1);
16159 step = ((hash * 17) & (symtab->size - 1)) | 1;
16160
16161 for (;;)
16162 {
16163 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16164 return &symtab->data[index];
16165 index = (index + step) & (symtab->size - 1);
16166 }
16167}
16168
16169/* Expand SYMTAB's hash table. */
b89be57b 16170
9291a0cd
TT
16171static void
16172hash_expand (struct mapped_symtab *symtab)
16173{
16174 offset_type old_size = symtab->size;
16175 offset_type i;
16176 struct symtab_index_entry **old_entries = symtab->data;
16177
16178 symtab->size *= 2;
16179 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16180
16181 for (i = 0; i < old_size; ++i)
16182 {
16183 if (old_entries[i])
16184 {
16185 struct symtab_index_entry **slot = find_slot (symtab,
16186 old_entries[i]->name);
16187 *slot = old_entries[i];
16188 }
16189 }
16190
16191 xfree (old_entries);
16192}
16193
16194/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16195 is the index of the CU in which the symbol appears. */
b89be57b 16196
9291a0cd
TT
16197static void
16198add_index_entry (struct mapped_symtab *symtab, const char *name,
16199 offset_type cu_index)
16200{
16201 struct symtab_index_entry **slot;
16202
16203 ++symtab->n_elements;
16204 if (4 * symtab->n_elements / 3 >= symtab->size)
16205 hash_expand (symtab);
16206
16207 slot = find_slot (symtab, name);
16208 if (!*slot)
16209 {
16210 *slot = XNEW (struct symtab_index_entry);
16211 (*slot)->name = name;
16212 (*slot)->cu_indices = NULL;
16213 }
16214 /* Don't push an index twice. Due to how we add entries we only
16215 have to check the last one. */
16216 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 16217 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
16218 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16219}
16220
16221/* Add a vector of indices to the constant pool. */
b89be57b 16222
9291a0cd 16223static offset_type
3876f04e 16224add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
16225 struct symtab_index_entry *entry)
16226{
16227 void **slot;
16228
3876f04e 16229 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
16230 if (!*slot)
16231 {
16232 offset_type len = VEC_length (offset_type, entry->cu_indices);
16233 offset_type val = MAYBE_SWAP (len);
16234 offset_type iter;
16235 int i;
16236
16237 *slot = entry;
16238 entry->index_offset = obstack_object_size (cpool);
16239
16240 obstack_grow (cpool, &val, sizeof (val));
16241 for (i = 0;
16242 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16243 ++i)
16244 {
16245 val = MAYBE_SWAP (iter);
16246 obstack_grow (cpool, &val, sizeof (val));
16247 }
16248 }
16249 else
16250 {
16251 struct symtab_index_entry *old_entry = *slot;
16252 entry->index_offset = old_entry->index_offset;
16253 entry = old_entry;
16254 }
16255 return entry->index_offset;
16256}
16257
16258/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16259 constant pool entries going into the obstack CPOOL. */
b89be57b 16260
9291a0cd
TT
16261static void
16262write_hash_table (struct mapped_symtab *symtab,
16263 struct obstack *output, struct obstack *cpool)
16264{
16265 offset_type i;
3876f04e 16266 htab_t symbol_hash_table;
9291a0cd
TT
16267 htab_t str_table;
16268
3876f04e 16269 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 16270 str_table = create_strtab ();
3876f04e 16271
9291a0cd
TT
16272 /* We add all the index vectors to the constant pool first, to
16273 ensure alignment is ok. */
16274 for (i = 0; i < symtab->size; ++i)
16275 {
16276 if (symtab->data[i])
3876f04e 16277 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
16278 }
16279
16280 /* Now write out the hash table. */
16281 for (i = 0; i < symtab->size; ++i)
16282 {
16283 offset_type str_off, vec_off;
16284
16285 if (symtab->data[i])
16286 {
16287 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16288 vec_off = symtab->data[i]->index_offset;
16289 }
16290 else
16291 {
16292 /* While 0 is a valid constant pool index, it is not valid
16293 to have 0 for both offsets. */
16294 str_off = 0;
16295 vec_off = 0;
16296 }
16297
16298 str_off = MAYBE_SWAP (str_off);
16299 vec_off = MAYBE_SWAP (vec_off);
16300
16301 obstack_grow (output, &str_off, sizeof (str_off));
16302 obstack_grow (output, &vec_off, sizeof (vec_off));
16303 }
16304
16305 htab_delete (str_table);
3876f04e 16306 htab_delete (symbol_hash_table);
9291a0cd
TT
16307}
16308
0a5429f6
DE
16309/* Struct to map psymtab to CU index in the index file. */
16310struct psymtab_cu_index_map
16311{
16312 struct partial_symtab *psymtab;
16313 unsigned int cu_index;
16314};
16315
16316static hashval_t
16317hash_psymtab_cu_index (const void *item)
16318{
16319 const struct psymtab_cu_index_map *map = item;
16320
16321 return htab_hash_pointer (map->psymtab);
16322}
16323
16324static int
16325eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16326{
16327 const struct psymtab_cu_index_map *lhs = item_lhs;
16328 const struct psymtab_cu_index_map *rhs = item_rhs;
16329
16330 return lhs->psymtab == rhs->psymtab;
16331}
16332
16333/* Helper struct for building the address table. */
16334struct addrmap_index_data
16335{
16336 struct objfile *objfile;
16337 struct obstack *addr_obstack;
16338 htab_t cu_index_htab;
16339
16340 /* Non-zero if the previous_* fields are valid.
16341 We can't write an entry until we see the next entry (since it is only then
16342 that we know the end of the entry). */
16343 int previous_valid;
16344 /* Index of the CU in the table of all CUs in the index file. */
16345 unsigned int previous_cu_index;
0963b4bd 16346 /* Start address of the CU. */
0a5429f6
DE
16347 CORE_ADDR previous_cu_start;
16348};
16349
16350/* Write an address entry to OBSTACK. */
b89be57b 16351
9291a0cd 16352static void
0a5429f6
DE
16353add_address_entry (struct objfile *objfile, struct obstack *obstack,
16354 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 16355{
0a5429f6 16356 offset_type cu_index_to_write;
9291a0cd
TT
16357 char addr[8];
16358 CORE_ADDR baseaddr;
16359
16360 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16361
0a5429f6
DE
16362 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16363 obstack_grow (obstack, addr, 8);
16364 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16365 obstack_grow (obstack, addr, 8);
16366 cu_index_to_write = MAYBE_SWAP (cu_index);
16367 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16368}
16369
16370/* Worker function for traversing an addrmap to build the address table. */
16371
16372static int
16373add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16374{
16375 struct addrmap_index_data *data = datap;
16376 struct partial_symtab *pst = obj;
16377 offset_type cu_index;
16378 void **slot;
16379
16380 if (data->previous_valid)
16381 add_address_entry (data->objfile, data->addr_obstack,
16382 data->previous_cu_start, start_addr,
16383 data->previous_cu_index);
16384
16385 data->previous_cu_start = start_addr;
16386 if (pst != NULL)
16387 {
16388 struct psymtab_cu_index_map find_map, *map;
16389 find_map.psymtab = pst;
16390 map = htab_find (data->cu_index_htab, &find_map);
16391 gdb_assert (map != NULL);
16392 data->previous_cu_index = map->cu_index;
16393 data->previous_valid = 1;
16394 }
16395 else
16396 data->previous_valid = 0;
16397
16398 return 0;
16399}
16400
16401/* Write OBJFILE's address map to OBSTACK.
16402 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16403 in the index file. */
16404
16405static void
16406write_address_map (struct objfile *objfile, struct obstack *obstack,
16407 htab_t cu_index_htab)
16408{
16409 struct addrmap_index_data addrmap_index_data;
16410
16411 /* When writing the address table, we have to cope with the fact that
16412 the addrmap iterator only provides the start of a region; we have to
16413 wait until the next invocation to get the start of the next region. */
16414
16415 addrmap_index_data.objfile = objfile;
16416 addrmap_index_data.addr_obstack = obstack;
16417 addrmap_index_data.cu_index_htab = cu_index_htab;
16418 addrmap_index_data.previous_valid = 0;
16419
16420 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16421 &addrmap_index_data);
16422
16423 /* It's highly unlikely the last entry (end address = 0xff...ff)
16424 is valid, but we should still handle it.
16425 The end address is recorded as the start of the next region, but that
16426 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16427 anyway. */
16428 if (addrmap_index_data.previous_valid)
16429 add_address_entry (objfile, obstack,
16430 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16431 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16432}
16433
16434/* Add a list of partial symbols to SYMTAB. */
b89be57b 16435
9291a0cd
TT
16436static void
16437write_psymbols (struct mapped_symtab *symtab,
987d643c 16438 htab_t psyms_seen,
9291a0cd
TT
16439 struct partial_symbol **psymp,
16440 int count,
987d643c
TT
16441 offset_type cu_index,
16442 int is_static)
9291a0cd
TT
16443{
16444 for (; count-- > 0; ++psymp)
16445 {
987d643c
TT
16446 void **slot, *lookup;
16447
9291a0cd
TT
16448 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16449 error (_("Ada is not currently supported by the index"));
987d643c
TT
16450
16451 /* We only want to add a given psymbol once. However, we also
16452 want to account for whether it is global or static. So, we
16453 may add it twice, using slightly different values. */
16454 if (is_static)
16455 {
16456 uintptr_t val = 1 | (uintptr_t) *psymp;
16457
16458 lookup = (void *) val;
16459 }
16460 else
16461 lookup = *psymp;
16462
16463 /* Only add a given psymbol once. */
16464 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16465 if (!*slot)
16466 {
16467 *slot = lookup;
16468 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
16469 }
9291a0cd
TT
16470 }
16471}
16472
16473/* Write the contents of an ("unfinished") obstack to FILE. Throw an
16474 exception if there is an error. */
b89be57b 16475
9291a0cd
TT
16476static void
16477write_obstack (FILE *file, struct obstack *obstack)
16478{
16479 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16480 file)
16481 != obstack_object_size (obstack))
16482 error (_("couldn't data write to file"));
16483}
16484
16485/* Unlink a file if the argument is not NULL. */
b89be57b 16486
9291a0cd
TT
16487static void
16488unlink_if_set (void *p)
16489{
16490 char **filename = p;
16491 if (*filename)
16492 unlink (*filename);
16493}
16494
1fd400ff
TT
16495/* A helper struct used when iterating over debug_types. */
16496struct signatured_type_index_data
16497{
16498 struct objfile *objfile;
16499 struct mapped_symtab *symtab;
16500 struct obstack *types_list;
987d643c 16501 htab_t psyms_seen;
1fd400ff
TT
16502 int cu_index;
16503};
16504
16505/* A helper function that writes a single signatured_type to an
16506 obstack. */
b89be57b 16507
1fd400ff
TT
16508static int
16509write_one_signatured_type (void **slot, void *d)
16510{
16511 struct signatured_type_index_data *info = d;
16512 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
16513 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16514 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
16515 gdb_byte val[8];
16516
16517 write_psymbols (info->symtab,
987d643c 16518 info->psyms_seen,
3e43a32a
MS
16519 info->objfile->global_psymbols.list
16520 + psymtab->globals_offset,
987d643c
TT
16521 psymtab->n_global_syms, info->cu_index,
16522 0);
1fd400ff 16523 write_psymbols (info->symtab,
987d643c 16524 info->psyms_seen,
3e43a32a
MS
16525 info->objfile->static_psymbols.list
16526 + psymtab->statics_offset,
987d643c
TT
16527 psymtab->n_static_syms, info->cu_index,
16528 1);
1fd400ff 16529
b3c8eb43 16530 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
1fd400ff
TT
16531 obstack_grow (info->types_list, val, 8);
16532 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16533 obstack_grow (info->types_list, val, 8);
16534 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16535 obstack_grow (info->types_list, val, 8);
16536
16537 ++info->cu_index;
16538
16539 return 1;
16540}
16541
987d643c
TT
16542/* A cleanup function for an htab_t. */
16543
16544static void
16545cleanup_htab (void *arg)
16546{
16547 htab_delete (arg);
16548}
16549
9291a0cd 16550/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16551
9291a0cd
TT
16552static void
16553write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16554{
16555 struct cleanup *cleanup;
16556 char *filename, *cleanup_filename;
1fd400ff
TT
16557 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16558 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16559 int i;
16560 FILE *out_file;
16561 struct mapped_symtab *symtab;
16562 offset_type val, size_of_contents, total_len;
16563 struct stat st;
16564 char buf[8];
987d643c 16565 htab_t psyms_seen;
0a5429f6
DE
16566 htab_t cu_index_htab;
16567 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 16568
b4f2f049 16569 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 16570 return;
b4f2f049 16571
9291a0cd
TT
16572 if (dwarf2_per_objfile->using_index)
16573 error (_("Cannot use an index to create the index"));
16574
8b70b953
TT
16575 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16576 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16577
9291a0cd 16578 if (stat (objfile->name, &st) < 0)
7e17e088 16579 perror_with_name (objfile->name);
9291a0cd
TT
16580
16581 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16582 INDEX_SUFFIX, (char *) NULL);
16583 cleanup = make_cleanup (xfree, filename);
16584
16585 out_file = fopen (filename, "wb");
16586 if (!out_file)
16587 error (_("Can't open `%s' for writing"), filename);
16588
16589 cleanup_filename = filename;
16590 make_cleanup (unlink_if_set, &cleanup_filename);
16591
16592 symtab = create_mapped_symtab ();
16593 make_cleanup (cleanup_mapped_symtab, symtab);
16594
16595 obstack_init (&addr_obstack);
16596 make_cleanup_obstack_free (&addr_obstack);
16597
16598 obstack_init (&cu_list);
16599 make_cleanup_obstack_free (&cu_list);
16600
1fd400ff
TT
16601 obstack_init (&types_cu_list);
16602 make_cleanup_obstack_free (&types_cu_list);
16603
987d643c
TT
16604 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16605 NULL, xcalloc, xfree);
16606 make_cleanup (cleanup_htab, psyms_seen);
16607
0a5429f6
DE
16608 /* While we're scanning CU's create a table that maps a psymtab pointer
16609 (which is what addrmap records) to its index (which is what is recorded
16610 in the index file). This will later be needed to write the address
16611 table. */
16612 cu_index_htab = htab_create_alloc (100,
16613 hash_psymtab_cu_index,
16614 eq_psymtab_cu_index,
16615 NULL, xcalloc, xfree);
16616 make_cleanup (cleanup_htab, cu_index_htab);
16617 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16618 xmalloc (sizeof (struct psymtab_cu_index_map)
16619 * dwarf2_per_objfile->n_comp_units);
16620 make_cleanup (xfree, psymtab_cu_index_map);
16621
16622 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
16623 work here. Also, the debug_types entries do not appear in
16624 all_comp_units, but only in their own hash table. */
9291a0cd
TT
16625 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16626 {
3e43a32a
MS
16627 struct dwarf2_per_cu_data *per_cu
16628 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 16629 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 16630 gdb_byte val[8];
0a5429f6
DE
16631 struct psymtab_cu_index_map *map;
16632 void **slot;
9291a0cd
TT
16633
16634 write_psymbols (symtab,
987d643c 16635 psyms_seen,
9291a0cd 16636 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
16637 psymtab->n_global_syms, i,
16638 0);
9291a0cd 16639 write_psymbols (symtab,
987d643c 16640 psyms_seen,
9291a0cd 16641 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
16642 psymtab->n_static_syms, i,
16643 1);
9291a0cd 16644
0a5429f6
DE
16645 map = &psymtab_cu_index_map[i];
16646 map->psymtab = psymtab;
16647 map->cu_index = i;
16648 slot = htab_find_slot (cu_index_htab, map, INSERT);
16649 gdb_assert (slot != NULL);
16650 gdb_assert (*slot == NULL);
16651 *slot = map;
9291a0cd 16652
e254ef6a 16653 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 16654 obstack_grow (&cu_list, val, 8);
e254ef6a 16655 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
16656 obstack_grow (&cu_list, val, 8);
16657 }
16658
0a5429f6
DE
16659 /* Dump the address map. */
16660 write_address_map (objfile, &addr_obstack, cu_index_htab);
16661
1fd400ff
TT
16662 /* Write out the .debug_type entries, if any. */
16663 if (dwarf2_per_objfile->signatured_types)
16664 {
16665 struct signatured_type_index_data sig_data;
16666
16667 sig_data.objfile = objfile;
16668 sig_data.symtab = symtab;
16669 sig_data.types_list = &types_cu_list;
987d643c 16670 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
16671 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16672 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16673 write_one_signatured_type, &sig_data);
16674 }
16675
9291a0cd
TT
16676 obstack_init (&constant_pool);
16677 make_cleanup_obstack_free (&constant_pool);
16678 obstack_init (&symtab_obstack);
16679 make_cleanup_obstack_free (&symtab_obstack);
16680 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16681
16682 obstack_init (&contents);
16683 make_cleanup_obstack_free (&contents);
1fd400ff 16684 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
16685 total_len = size_of_contents;
16686
16687 /* The version number. */
559a7a62 16688 val = MAYBE_SWAP (5);
9291a0cd
TT
16689 obstack_grow (&contents, &val, sizeof (val));
16690
16691 /* The offset of the CU list from the start of the file. */
16692 val = MAYBE_SWAP (total_len);
16693 obstack_grow (&contents, &val, sizeof (val));
16694 total_len += obstack_object_size (&cu_list);
16695
1fd400ff
TT
16696 /* The offset of the types CU list from the start of the file. */
16697 val = MAYBE_SWAP (total_len);
16698 obstack_grow (&contents, &val, sizeof (val));
16699 total_len += obstack_object_size (&types_cu_list);
16700
9291a0cd
TT
16701 /* The offset of the address table from the start of the file. */
16702 val = MAYBE_SWAP (total_len);
16703 obstack_grow (&contents, &val, sizeof (val));
16704 total_len += obstack_object_size (&addr_obstack);
16705
16706 /* The offset of the symbol table from the start of the file. */
16707 val = MAYBE_SWAP (total_len);
16708 obstack_grow (&contents, &val, sizeof (val));
16709 total_len += obstack_object_size (&symtab_obstack);
16710
16711 /* The offset of the constant pool from the start of the file. */
16712 val = MAYBE_SWAP (total_len);
16713 obstack_grow (&contents, &val, sizeof (val));
16714 total_len += obstack_object_size (&constant_pool);
16715
16716 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16717
16718 write_obstack (out_file, &contents);
16719 write_obstack (out_file, &cu_list);
1fd400ff 16720 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
16721 write_obstack (out_file, &addr_obstack);
16722 write_obstack (out_file, &symtab_obstack);
16723 write_obstack (out_file, &constant_pool);
16724
16725 fclose (out_file);
16726
16727 /* We want to keep the file, so we set cleanup_filename to NULL
16728 here. See unlink_if_set. */
16729 cleanup_filename = NULL;
16730
16731 do_cleanups (cleanup);
16732}
16733
90476074
TT
16734/* Implementation of the `save gdb-index' command.
16735
16736 Note that the file format used by this command is documented in the
16737 GDB manual. Any changes here must be documented there. */
11570e71 16738
9291a0cd
TT
16739static void
16740save_gdb_index_command (char *arg, int from_tty)
16741{
16742 struct objfile *objfile;
16743
16744 if (!arg || !*arg)
96d19272 16745 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
16746
16747 ALL_OBJFILES (objfile)
16748 {
16749 struct stat st;
16750
16751 /* If the objfile does not correspond to an actual file, skip it. */
16752 if (stat (objfile->name, &st) < 0)
16753 continue;
16754
16755 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16756 if (dwarf2_per_objfile)
16757 {
16758 volatile struct gdb_exception except;
16759
16760 TRY_CATCH (except, RETURN_MASK_ERROR)
16761 {
16762 write_psymtabs_to_index (objfile, arg);
16763 }
16764 if (except.reason < 0)
16765 exception_fprintf (gdb_stderr, except,
16766 _("Error while writing index for `%s': "),
16767 objfile->name);
16768 }
16769 }
dce234bc
PP
16770}
16771
9291a0cd
TT
16772\f
16773
9eae7c52
TT
16774int dwarf2_always_disassemble;
16775
16776static void
16777show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16778 struct cmd_list_element *c, const char *value)
16779{
3e43a32a
MS
16780 fprintf_filtered (file,
16781 _("Whether to always disassemble "
16782 "DWARF expressions is %s.\n"),
9eae7c52
TT
16783 value);
16784}
16785
900e11f9
JK
16786static void
16787show_check_physname (struct ui_file *file, int from_tty,
16788 struct cmd_list_element *c, const char *value)
16789{
16790 fprintf_filtered (file,
16791 _("Whether to check \"physname\" is %s.\n"),
16792 value);
16793}
16794
6502dd73
DJ
16795void _initialize_dwarf2_read (void);
16796
16797void
16798_initialize_dwarf2_read (void)
16799{
96d19272
JK
16800 struct cmd_list_element *c;
16801
dce234bc 16802 dwarf2_objfile_data_key
c1bd65d0 16803 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 16804
1bedd215
AC
16805 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16806Set DWARF 2 specific variables.\n\
16807Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16808 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16809 0/*allow-unknown*/, &maintenance_set_cmdlist);
16810
1bedd215
AC
16811 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16812Show DWARF 2 specific variables\n\
16813Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16814 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16815 0/*allow-unknown*/, &maintenance_show_cmdlist);
16816
16817 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
16818 &dwarf2_max_cache_age, _("\
16819Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16820Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16821A higher limit means that cached compilation units will be stored\n\
16822in memory longer, and more total memory will be used. Zero disables\n\
16823caching, which can slow down startup."),
2c5b56ce 16824 NULL,
920d2a44 16825 show_dwarf2_max_cache_age,
2c5b56ce 16826 &set_dwarf2_cmdlist,
ae038cb0 16827 &show_dwarf2_cmdlist);
d97bc12b 16828
9eae7c52
TT
16829 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16830 &dwarf2_always_disassemble, _("\
16831Set whether `info address' always disassembles DWARF expressions."), _("\
16832Show whether `info address' always disassembles DWARF expressions."), _("\
16833When enabled, DWARF expressions are always printed in an assembly-like\n\
16834syntax. When disabled, expressions will be printed in a more\n\
16835conversational style, when possible."),
16836 NULL,
16837 show_dwarf2_always_disassemble,
16838 &set_dwarf2_cmdlist,
16839 &show_dwarf2_cmdlist);
16840
d97bc12b
DE
16841 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16842Set debugging of the dwarf2 DIE reader."), _("\
16843Show debugging of the dwarf2 DIE reader."), _("\
16844When enabled (non-zero), DIEs are dumped after they are read in.\n\
16845The value is the maximum depth to print."),
16846 NULL,
16847 NULL,
16848 &setdebuglist, &showdebuglist);
9291a0cd 16849
900e11f9
JK
16850 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
16851Set cross-checking of \"physname\" code against demangler."), _("\
16852Show cross-checking of \"physname\" code against demangler."), _("\
16853When enabled, GDB's internal \"physname\" code is checked against\n\
16854the demangler."),
16855 NULL, show_check_physname,
16856 &setdebuglist, &showdebuglist);
16857
96d19272 16858 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 16859 _("\
fc1a9d6e 16860Save a gdb-index file.\n\
11570e71 16861Usage: save gdb-index DIRECTORY"),
96d19272
JK
16862 &save_cmdlist);
16863 set_cmd_completer (c, filename_completer);
6502dd73 16864}
This page took 2.231297 seconds and 4 git commands to generate.