]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/dwarf2read.c
2011-10-11 Sterling Augustine <saugustine@google.com>
[thirdparty/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
96408a79
SA
410 /* To be copied to symtab->call_site_htab. */
411 htab_t call_site_htab;
412
ae038cb0
DJ
413 /* Mark used when releasing cached dies. */
414 unsigned int mark : 1;
415
416 /* This flag will be set if this compilation unit might include
417 inter-compilation-unit references. */
418 unsigned int has_form_ref_addr : 1;
419
72bf9492
DJ
420 /* This flag will be set if this compilation unit includes any
421 DW_TAG_namespace DIEs. If we know that there are explicit
422 DIEs for namespaces, we don't need to try to infer them
423 from mangled names. */
424 unsigned int has_namespace_info : 1;
8be455d7
JK
425
426 /* This CU references .debug_loc. See the symtab->locations_valid field.
427 This test is imperfect as there may exist optimized debug code not using
428 any location list and still facing inlining issues if handled as
429 unoptimized code. For a future better test see GCC PR other/32998. */
430
431 unsigned int has_loclist : 1;
e7c27a73
DJ
432};
433
10b3939b
DJ
434/* Persistent data held for a compilation unit, even when not
435 processing it. We put a pointer to this structure in the
436 read_symtab_private field of the psymtab. If we encounter
437 inter-compilation-unit references, we also maintain a sorted
438 list of all compilation units. */
439
ae038cb0
DJ
440struct dwarf2_per_cu_data
441{
348e048f 442 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 443 bytes should suffice to store the length of any compilation unit
45452591
DE
444 - if it doesn't, GDB will fall over anyway.
445 NOTE: Unlike comp_unit_head.length, this length includes
446 initial_length_size. */
c764a876 447 unsigned int offset;
348e048f 448 unsigned int length : 29;
ae038cb0
DJ
449
450 /* Flag indicating this compilation unit will be read in before
451 any of the current compilation units are processed. */
c764a876 452 unsigned int queued : 1;
ae038cb0 453
5afb4e99
DJ
454 /* This flag will be set if we need to load absolutely all DIEs
455 for this compilation unit, instead of just the ones we think
456 are interesting. It gets set if we look for a DIE in the
457 hash table and don't find it. */
458 unsigned int load_all_dies : 1;
459
8b70b953
TT
460 /* Non-null if this CU is from .debug_types; in which case it points
461 to the section. Otherwise it's from .debug_info. */
462 struct dwarf2_section_info *debug_type_section;
348e048f 463
17ea53c3
JK
464 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
465 of the CU cache it gets reset to NULL again. */
ae038cb0 466 struct dwarf2_cu *cu;
1c379e20 467
9291a0cd
TT
468 /* The corresponding objfile. */
469 struct objfile *objfile;
470
471 /* When using partial symbol tables, the 'psymtab' field is active.
472 Otherwise the 'quick' field is active. */
473 union
474 {
475 /* The partial symbol table associated with this compilation unit,
476 or NULL for partial units (which do not have an associated
477 symtab). */
478 struct partial_symtab *psymtab;
479
480 /* Data needed by the "quick" functions. */
481 struct dwarf2_per_cu_quick_data *quick;
482 } v;
ae038cb0
DJ
483};
484
348e048f
DE
485/* Entry in the signatured_types hash table. */
486
487struct signatured_type
488{
489 ULONGEST signature;
490
348e048f
DE
491 /* Offset in .debug_types of the type defined by this TU. */
492 unsigned int type_offset;
493
494 /* The CU(/TU) of this type. */
495 struct dwarf2_per_cu_data per_cu;
496};
497
0963b4bd
MS
498/* Struct used to pass misc. parameters to read_die_and_children, et
499 al. which are used for both .debug_info and .debug_types dies.
500 All parameters here are unchanging for the life of the call. This
501 struct exists to abstract away the constant parameters of die
502 reading. */
93311388
DE
503
504struct die_reader_specs
505{
506 /* The bfd of this objfile. */
507 bfd* abfd;
508
509 /* The CU of the DIE we are parsing. */
510 struct dwarf2_cu *cu;
511
512 /* Pointer to start of section buffer.
513 This is either the start of .debug_info or .debug_types. */
514 const gdb_byte *buffer;
515};
516
debd256d
JB
517/* The line number information for a compilation unit (found in the
518 .debug_line section) begins with a "statement program header",
519 which contains the following information. */
520struct line_header
521{
522 unsigned int total_length;
523 unsigned short version;
524 unsigned int header_length;
525 unsigned char minimum_instruction_length;
2dc7f7b3 526 unsigned char maximum_ops_per_instruction;
debd256d
JB
527 unsigned char default_is_stmt;
528 int line_base;
529 unsigned char line_range;
530 unsigned char opcode_base;
531
532 /* standard_opcode_lengths[i] is the number of operands for the
533 standard opcode whose value is i. This means that
534 standard_opcode_lengths[0] is unused, and the last meaningful
535 element is standard_opcode_lengths[opcode_base - 1]. */
536 unsigned char *standard_opcode_lengths;
537
538 /* The include_directories table. NOTE! These strings are not
539 allocated with xmalloc; instead, they are pointers into
540 debug_line_buffer. If you try to free them, `free' will get
541 indigestion. */
542 unsigned int num_include_dirs, include_dirs_size;
543 char **include_dirs;
544
545 /* The file_names table. NOTE! These strings are not allocated
546 with xmalloc; instead, they are pointers into debug_line_buffer.
547 Don't try to free them directly. */
548 unsigned int num_file_names, file_names_size;
549 struct file_entry
c906108c 550 {
debd256d
JB
551 char *name;
552 unsigned int dir_index;
553 unsigned int mod_time;
554 unsigned int length;
aaa75496 555 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 556 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
557 } *file_names;
558
559 /* The start and end of the statement program following this
6502dd73 560 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 561 gdb_byte *statement_program_start, *statement_program_end;
debd256d 562};
c906108c
SS
563
564/* When we construct a partial symbol table entry we only
0963b4bd 565 need this much information. */
c906108c
SS
566struct partial_die_info
567 {
72bf9492 568 /* Offset of this DIE. */
c906108c 569 unsigned int offset;
72bf9492
DJ
570
571 /* DWARF-2 tag for this DIE. */
572 ENUM_BITFIELD(dwarf_tag) tag : 16;
573
72bf9492
DJ
574 /* Assorted flags describing the data found in this DIE. */
575 unsigned int has_children : 1;
576 unsigned int is_external : 1;
577 unsigned int is_declaration : 1;
578 unsigned int has_type : 1;
579 unsigned int has_specification : 1;
580 unsigned int has_pc_info : 1;
581
582 /* Flag set if the SCOPE field of this structure has been
583 computed. */
584 unsigned int scope_set : 1;
585
fa4028e9
JB
586 /* Flag set if the DIE has a byte_size attribute. */
587 unsigned int has_byte_size : 1;
588
98bfdba5
PA
589 /* Flag set if any of the DIE's children are template arguments. */
590 unsigned int has_template_arguments : 1;
591
abc72ce4
DE
592 /* Flag set if fixup_partial_die has been called on this die. */
593 unsigned int fixup_called : 1;
594
72bf9492 595 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 596 sometimes a default name for unnamed DIEs. */
c906108c 597 char *name;
72bf9492 598
abc72ce4
DE
599 /* The linkage name, if present. */
600 const char *linkage_name;
601
72bf9492
DJ
602 /* The scope to prepend to our children. This is generally
603 allocated on the comp_unit_obstack, so will disappear
604 when this compilation unit leaves the cache. */
605 char *scope;
606
607 /* The location description associated with this DIE, if any. */
608 struct dwarf_block *locdesc;
609
610 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
611 CORE_ADDR lowpc;
612 CORE_ADDR highpc;
72bf9492 613
93311388 614 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 615 DW_AT_sibling, if any. */
abc72ce4
DE
616 /* NOTE: This member isn't strictly necessary, read_partial_die could
617 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 618 gdb_byte *sibling;
72bf9492
DJ
619
620 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
621 DW_AT_specification (or DW_AT_abstract_origin or
622 DW_AT_extension). */
623 unsigned int spec_offset;
624
625 /* Pointers to this DIE's parent, first child, and next sibling,
626 if any. */
627 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
628 };
629
0963b4bd 630/* This data structure holds the information of an abbrev. */
c906108c
SS
631struct abbrev_info
632 {
633 unsigned int number; /* number identifying abbrev */
634 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
635 unsigned short has_children; /* boolean */
636 unsigned short num_attrs; /* number of attributes */
c906108c
SS
637 struct attr_abbrev *attrs; /* an array of attribute descriptions */
638 struct abbrev_info *next; /* next in chain */
639 };
640
641struct attr_abbrev
642 {
9d25dd43
DE
643 ENUM_BITFIELD(dwarf_attribute) name : 16;
644 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
645 };
646
0963b4bd 647/* Attributes have a name and a value. */
b60c80d6
DJ
648struct attribute
649 {
9d25dd43 650 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
651 ENUM_BITFIELD(dwarf_form) form : 15;
652
653 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
654 field should be in u.str (existing only for DW_STRING) but it is kept
655 here for better struct attribute alignment. */
656 unsigned int string_is_canonical : 1;
657
b60c80d6
DJ
658 union
659 {
660 char *str;
661 struct dwarf_block *blk;
43bbcdc2
PH
662 ULONGEST unsnd;
663 LONGEST snd;
b60c80d6 664 CORE_ADDR addr;
348e048f 665 struct signatured_type *signatured_type;
b60c80d6
DJ
666 }
667 u;
668 };
669
0963b4bd 670/* This data structure holds a complete die structure. */
c906108c
SS
671struct die_info
672 {
76815b17
DE
673 /* DWARF-2 tag for this DIE. */
674 ENUM_BITFIELD(dwarf_tag) tag : 16;
675
676 /* Number of attributes */
98bfdba5
PA
677 unsigned char num_attrs;
678
679 /* True if we're presently building the full type name for the
680 type derived from this DIE. */
681 unsigned char building_fullname : 1;
76815b17
DE
682
683 /* Abbrev number */
684 unsigned int abbrev;
685
93311388 686 /* Offset in .debug_info or .debug_types section. */
76815b17 687 unsigned int offset;
78ba4af6
JB
688
689 /* The dies in a compilation unit form an n-ary tree. PARENT
690 points to this die's parent; CHILD points to the first child of
691 this node; and all the children of a given node are chained
4950bc1c 692 together via their SIBLING fields. */
639d11d3
DC
693 struct die_info *child; /* Its first child, if any. */
694 struct die_info *sibling; /* Its next sibling, if any. */
695 struct die_info *parent; /* Its parent, if any. */
c906108c 696
b60c80d6
DJ
697 /* An array of attributes, with NUM_ATTRS elements. There may be
698 zero, but it's not common and zero-sized arrays are not
699 sufficiently portable C. */
700 struct attribute attrs[1];
c906108c
SS
701 };
702
5fb290d7
DJ
703struct function_range
704{
705 const char *name;
706 CORE_ADDR lowpc, highpc;
707 int seen_line;
708 struct function_range *next;
709};
710
0963b4bd 711/* Get at parts of an attribute structure. */
c906108c
SS
712
713#define DW_STRING(attr) ((attr)->u.str)
8285870a 714#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
715#define DW_UNSND(attr) ((attr)->u.unsnd)
716#define DW_BLOCK(attr) ((attr)->u.blk)
717#define DW_SND(attr) ((attr)->u.snd)
718#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 719#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 720
0963b4bd 721/* Blocks are a bunch of untyped bytes. */
c906108c
SS
722struct dwarf_block
723 {
724 unsigned int size;
fe1b8b76 725 gdb_byte *data;
c906108c
SS
726 };
727
c906108c
SS
728#ifndef ATTR_ALLOC_CHUNK
729#define ATTR_ALLOC_CHUNK 4
730#endif
731
c906108c
SS
732/* Allocate fields for structs, unions and enums in this size. */
733#ifndef DW_FIELD_ALLOC_CHUNK
734#define DW_FIELD_ALLOC_CHUNK 4
735#endif
736
c906108c
SS
737/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
738 but this would require a corresponding change in unpack_field_as_long
739 and friends. */
740static int bits_per_byte = 8;
741
742/* The routines that read and process dies for a C struct or C++ class
743 pass lists of data member fields and lists of member function fields
744 in an instance of a field_info structure, as defined below. */
745struct field_info
c5aa993b 746 {
0963b4bd 747 /* List of data member and baseclasses fields. */
c5aa993b
JM
748 struct nextfield
749 {
750 struct nextfield *next;
751 int accessibility;
752 int virtuality;
753 struct field field;
754 }
7d0ccb61 755 *fields, *baseclasses;
c906108c 756
7d0ccb61 757 /* Number of fields (including baseclasses). */
c5aa993b 758 int nfields;
c906108c 759
c5aa993b
JM
760 /* Number of baseclasses. */
761 int nbaseclasses;
c906108c 762
c5aa993b
JM
763 /* Set if the accesibility of one of the fields is not public. */
764 int non_public_fields;
c906108c 765
c5aa993b
JM
766 /* Member function fields array, entries are allocated in the order they
767 are encountered in the object file. */
768 struct nextfnfield
769 {
770 struct nextfnfield *next;
771 struct fn_field fnfield;
772 }
773 *fnfields;
c906108c 774
c5aa993b
JM
775 /* Member function fieldlist array, contains name of possibly overloaded
776 member function, number of overloaded member functions and a pointer
777 to the head of the member function field chain. */
778 struct fnfieldlist
779 {
780 char *name;
781 int length;
782 struct nextfnfield *head;
783 }
784 *fnfieldlists;
c906108c 785
c5aa993b
JM
786 /* Number of entries in the fnfieldlists array. */
787 int nfnfields;
98751a41
JK
788
789 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
790 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
791 struct typedef_field_list
792 {
793 struct typedef_field field;
794 struct typedef_field_list *next;
795 }
796 *typedef_field_list;
797 unsigned typedef_field_list_count;
c5aa993b 798 };
c906108c 799
10b3939b
DJ
800/* One item on the queue of compilation units to read in full symbols
801 for. */
802struct dwarf2_queue_item
803{
804 struct dwarf2_per_cu_data *per_cu;
805 struct dwarf2_queue_item *next;
806};
807
808/* The current queue. */
809static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
810
ae038cb0
DJ
811/* Loaded secondary compilation units are kept in memory until they
812 have not been referenced for the processing of this many
813 compilation units. Set this to zero to disable caching. Cache
814 sizes of up to at least twenty will improve startup time for
815 typical inter-CU-reference binaries, at an obvious memory cost. */
816static int dwarf2_max_cache_age = 5;
920d2a44
AC
817static void
818show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
819 struct cmd_list_element *c, const char *value)
820{
3e43a32a
MS
821 fprintf_filtered (file, _("The upper bound on the age of cached "
822 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
823 value);
824}
825
ae038cb0 826
0963b4bd 827/* Various complaints about symbol reading that don't abort the process. */
c906108c 828
4d3c2250
KB
829static void
830dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 831{
4d3c2250 832 complaint (&symfile_complaints,
e2e0b3e5 833 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
834}
835
25e43795
DJ
836static void
837dwarf2_debug_line_missing_file_complaint (void)
838{
839 complaint (&symfile_complaints,
840 _(".debug_line section has line data without a file"));
841}
842
59205f5a
JB
843static void
844dwarf2_debug_line_missing_end_sequence_complaint (void)
845{
846 complaint (&symfile_complaints,
3e43a32a
MS
847 _(".debug_line section has line "
848 "program sequence without an end"));
59205f5a
JB
849}
850
4d3c2250
KB
851static void
852dwarf2_complex_location_expr_complaint (void)
2e276125 853{
e2e0b3e5 854 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
855}
856
4d3c2250
KB
857static void
858dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
859 int arg3)
2e276125 860{
4d3c2250 861 complaint (&symfile_complaints,
3e43a32a
MS
862 _("const value length mismatch for '%s', got %d, expected %d"),
863 arg1, arg2, arg3);
4d3c2250
KB
864}
865
866static void
cf2c3c16 867dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
2e276125 868{
4d3c2250 869 complaint (&symfile_complaints,
cf2c3c16
TT
870 _("macro info runs off end of `%s' section"),
871 section->asection->name);
4d3c2250
KB
872}
873
874static void
875dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 876{
4d3c2250 877 complaint (&symfile_complaints,
3e43a32a
MS
878 _("macro debug info contains a "
879 "malformed macro definition:\n`%s'"),
4d3c2250
KB
880 arg1);
881}
882
883static void
884dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 885{
4d3c2250 886 complaint (&symfile_complaints,
3e43a32a
MS
887 _("invalid attribute class or form for '%s' in '%s'"),
888 arg1, arg2);
4d3c2250 889}
c906108c 890
c906108c
SS
891/* local function prototypes */
892
4efb68b1 893static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 894
aaa75496
JB
895static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
896 struct objfile *);
897
918dd910
JK
898static void dwarf2_find_base_address (struct die_info *die,
899 struct dwarf2_cu *cu);
900
c67a9c90 901static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 902
72bf9492
DJ
903static void scan_partial_symbols (struct partial_die_info *,
904 CORE_ADDR *, CORE_ADDR *,
5734ee8b 905 int, struct dwarf2_cu *);
c906108c 906
72bf9492
DJ
907static void add_partial_symbol (struct partial_die_info *,
908 struct dwarf2_cu *);
63d06c5c 909
72bf9492
DJ
910static void add_partial_namespace (struct partial_die_info *pdi,
911 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 912 int need_pc, struct dwarf2_cu *cu);
63d06c5c 913
5d7cb8df
JK
914static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
915 CORE_ADDR *highpc, int need_pc,
916 struct dwarf2_cu *cu);
917
72bf9492
DJ
918static void add_partial_enumeration (struct partial_die_info *enum_pdi,
919 struct dwarf2_cu *cu);
91c24f0a 920
bc30ff58
JB
921static void add_partial_subprogram (struct partial_die_info *pdi,
922 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 923 int need_pc, struct dwarf2_cu *cu);
bc30ff58 924
fe1b8b76 925static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
926 gdb_byte *buffer, gdb_byte *info_ptr,
927 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 928
a14ed312 929static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 930
a14ed312 931static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 932
e7c27a73 933static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 934
f3dd6933 935static void dwarf2_free_abbrev_table (void *);
c906108c 936
fe1b8b76 937static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 938 struct dwarf2_cu *);
72bf9492 939
57349743 940static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 941 struct dwarf2_cu *);
c906108c 942
93311388
DE
943static struct partial_die_info *load_partial_dies (bfd *,
944 gdb_byte *, gdb_byte *,
945 int, struct dwarf2_cu *);
72bf9492 946
fe1b8b76 947static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
948 struct abbrev_info *abbrev,
949 unsigned int, bfd *,
950 gdb_byte *, gdb_byte *,
951 struct dwarf2_cu *);
c906108c 952
c764a876 953static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 954 struct dwarf2_cu *);
72bf9492
DJ
955
956static void fixup_partial_die (struct partial_die_info *,
957 struct dwarf2_cu *);
958
fe1b8b76
JB
959static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
960 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 961
fe1b8b76
JB
962static gdb_byte *read_attribute_value (struct attribute *, unsigned,
963 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 964
fe1b8b76 965static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 966
fe1b8b76 967static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 968
fe1b8b76 969static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 970
fe1b8b76 971static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 972
93311388 973static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 974
fe1b8b76 975static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 976 unsigned int *);
c906108c 977
c764a876
DE
978static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
979
980static LONGEST read_checked_initial_length_and_offset
981 (bfd *, gdb_byte *, const struct comp_unit_head *,
982 unsigned int *, unsigned int *);
613e1657 983
fe1b8b76 984static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
985 unsigned int *);
986
987static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 988
fe1b8b76 989static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 990
9b1c24c8 991static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 992
fe1b8b76
JB
993static char *read_indirect_string (bfd *, gdb_byte *,
994 const struct comp_unit_head *,
995 unsigned int *);
4bdf3d34 996
fe1b8b76 997static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 998
fe1b8b76 999static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1000
fe1b8b76 1001static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 1002
e142c38c 1003static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1004
e142c38c
DJ
1005static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1006 struct dwarf2_cu *);
c906108c 1007
348e048f
DE
1008static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1009 unsigned int,
1010 struct dwarf2_cu *);
1011
05cf31d1
JB
1012static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1013 struct dwarf2_cu *cu);
1014
e142c38c 1015static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1016
e142c38c 1017static struct die_info *die_specification (struct die_info *die,
f2f0e013 1018 struct dwarf2_cu **);
63d06c5c 1019
debd256d
JB
1020static void free_line_header (struct line_header *lh);
1021
aaa75496
JB
1022static void add_file_name (struct line_header *, char *, unsigned int,
1023 unsigned int, unsigned int);
1024
debd256d
JB
1025static struct line_header *(dwarf_decode_line_header
1026 (unsigned int offset,
e7c27a73 1027 bfd *abfd, struct dwarf2_cu *cu));
debd256d 1028
72b9f47f 1029static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 1030 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1031
72b9f47f 1032static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1033
a14ed312 1034static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1035 struct dwarf2_cu *);
c906108c 1036
34eaf542
TT
1037static struct symbol *new_symbol_full (struct die_info *, struct type *,
1038 struct dwarf2_cu *, struct symbol *);
1039
a14ed312 1040static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1041 struct dwarf2_cu *);
c906108c 1042
98bfdba5
PA
1043static void dwarf2_const_value_attr (struct attribute *attr,
1044 struct type *type,
1045 const char *name,
1046 struct obstack *obstack,
1047 struct dwarf2_cu *cu, long *value,
1048 gdb_byte **bytes,
1049 struct dwarf2_locexpr_baton **baton);
2df3850c 1050
e7c27a73 1051static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1052
b4ba55a1
JB
1053static int need_gnat_info (struct dwarf2_cu *);
1054
3e43a32a
MS
1055static struct type *die_descriptive_type (struct die_info *,
1056 struct dwarf2_cu *);
b4ba55a1
JB
1057
1058static void set_descriptive_type (struct type *, struct die_info *,
1059 struct dwarf2_cu *);
1060
e7c27a73
DJ
1061static struct type *die_containing_type (struct die_info *,
1062 struct dwarf2_cu *);
c906108c 1063
673bfd45
DE
1064static struct type *lookup_die_type (struct die_info *, struct attribute *,
1065 struct dwarf2_cu *);
c906108c 1066
f792889a 1067static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1068
673bfd45
DE
1069static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1070
086ed43d 1071static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1072
6e70227d 1073static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1074 const char *suffix, int physname,
1075 struct dwarf2_cu *cu);
63d06c5c 1076
e7c27a73 1077static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1078
348e048f
DE
1079static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1080
e7c27a73 1081static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1082
e7c27a73 1083static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1084
96408a79
SA
1085static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1086
ff013f42
JK
1087static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1088 struct dwarf2_cu *, struct partial_symtab *);
1089
a14ed312 1090static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1091 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1092 struct partial_symtab *);
c906108c 1093
fae299cd
DC
1094static void get_scope_pc_bounds (struct die_info *,
1095 CORE_ADDR *, CORE_ADDR *,
1096 struct dwarf2_cu *);
1097
801e3a5b
JB
1098static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1099 CORE_ADDR, struct dwarf2_cu *);
1100
a14ed312 1101static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1102 struct dwarf2_cu *);
c906108c 1103
a14ed312 1104static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1105 struct type *, struct dwarf2_cu *);
c906108c 1106
a14ed312 1107static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1108 struct die_info *, struct type *,
e7c27a73 1109 struct dwarf2_cu *);
c906108c 1110
a14ed312 1111static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1112 struct type *,
1113 struct dwarf2_cu *);
c906108c 1114
134d01f1 1115static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1116
e7c27a73 1117static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1118
e7c27a73 1119static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1120
5d7cb8df
JK
1121static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1122
27aa8d6a
SW
1123static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1124
f55ee35c
JK
1125static struct type *read_module_type (struct die_info *die,
1126 struct dwarf2_cu *cu);
1127
38d518c9 1128static const char *namespace_name (struct die_info *die,
e142c38c 1129 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1130
134d01f1 1131static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1132
e7c27a73 1133static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1134
6e70227d 1135static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1136 struct dwarf2_cu *);
1137
93311388 1138static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1139
93311388
DE
1140static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1141 gdb_byte *info_ptr,
d97bc12b
DE
1142 gdb_byte **new_info_ptr,
1143 struct die_info *parent);
1144
93311388
DE
1145static struct die_info *read_die_and_children (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 struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1151 gdb_byte *info_ptr,
fe1b8b76 1152 gdb_byte **new_info_ptr,
639d11d3
DC
1153 struct die_info *parent);
1154
93311388
DE
1155static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1156 struct die_info **, gdb_byte *,
1157 int *);
1158
e7c27a73 1159static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1160
71c25dea
TT
1161static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1162 struct obstack *);
1163
e142c38c 1164static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1165
98bfdba5
PA
1166static const char *dwarf2_full_name (char *name,
1167 struct die_info *die,
1168 struct dwarf2_cu *cu);
1169
e142c38c 1170static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1171 struct dwarf2_cu **);
9219021c 1172
a14ed312 1173static char *dwarf_tag_name (unsigned int);
c906108c 1174
a14ed312 1175static char *dwarf_attr_name (unsigned int);
c906108c 1176
a14ed312 1177static char *dwarf_form_name (unsigned int);
c906108c 1178
a14ed312 1179static char *dwarf_bool_name (unsigned int);
c906108c 1180
a14ed312 1181static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1182
1183#if 0
a14ed312 1184static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1185#endif
1186
f9aca02d 1187static struct die_info *sibling_die (struct die_info *);
c906108c 1188
d97bc12b
DE
1189static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1190
1191static void dump_die_for_error (struct die_info *);
1192
1193static void dump_die_1 (struct ui_file *, int level, int max_level,
1194 struct die_info *);
c906108c 1195
d97bc12b 1196/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1197
51545339 1198static void store_in_ref_table (struct die_info *,
10b3939b 1199 struct dwarf2_cu *);
c906108c 1200
93311388
DE
1201static int is_ref_attr (struct attribute *);
1202
c764a876 1203static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1204
43bbcdc2 1205static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1206
348e048f
DE
1207static struct die_info *follow_die_ref_or_sig (struct die_info *,
1208 struct attribute *,
1209 struct dwarf2_cu **);
1210
10b3939b
DJ
1211static struct die_info *follow_die_ref (struct die_info *,
1212 struct attribute *,
f2f0e013 1213 struct dwarf2_cu **);
c906108c 1214
348e048f
DE
1215static struct die_info *follow_die_sig (struct die_info *,
1216 struct attribute *,
1217 struct dwarf2_cu **);
1218
6c83ed52
TT
1219static struct signatured_type *lookup_signatured_type_at_offset
1220 (struct objfile *objfile,
1221 struct dwarf2_section_info *section,
1222 unsigned int offset);
1223
348e048f 1224static void read_signatured_type_at_offset (struct objfile *objfile,
8b70b953 1225 struct dwarf2_section_info *sect,
348e048f
DE
1226 unsigned int offset);
1227
1228static void read_signatured_type (struct objfile *,
1229 struct signatured_type *type_sig);
1230
c906108c
SS
1231/* memory allocation interface */
1232
7b5a2f43 1233static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1234
f3dd6933 1235static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1236
b60c80d6 1237static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1238
e142c38c 1239static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1240
e142c38c
DJ
1241static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1242 struct dwarf2_cu *);
5fb290d7 1243
2e276125 1244static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1245 char *, bfd *, struct dwarf2_cu *,
1246 struct dwarf2_section_info *,
1247 int);
2e276125 1248
8e19ed76
PS
1249static int attr_form_is_block (struct attribute *);
1250
3690dd37
JB
1251static int attr_form_is_section_offset (struct attribute *);
1252
1253static int attr_form_is_constant (struct attribute *);
1254
8cf6f0b1
TT
1255static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1256 struct dwarf2_loclist_baton *baton,
1257 struct attribute *attr);
1258
93e7bd98
DJ
1259static void dwarf2_symbol_mark_computed (struct attribute *attr,
1260 struct symbol *sym,
1261 struct dwarf2_cu *cu);
4c2df51b 1262
93311388
DE
1263static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1264 struct abbrev_info *abbrev,
1265 struct dwarf2_cu *cu);
4bb7a0a7 1266
72bf9492
DJ
1267static void free_stack_comp_unit (void *);
1268
72bf9492
DJ
1269static hashval_t partial_die_hash (const void *item);
1270
1271static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1272
ae038cb0 1273static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1274 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1275
1276static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1277 (unsigned int offset, struct objfile *objfile);
ae038cb0 1278
9816fde3
JK
1279static void init_one_comp_unit (struct dwarf2_cu *cu,
1280 struct objfile *objfile);
1281
1282static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1283 struct die_info *comp_unit_die);
93311388 1284
ae038cb0
DJ
1285static void free_one_comp_unit (void *);
1286
1287static void free_cached_comp_units (void *);
1288
1289static void age_cached_comp_units (void);
1290
1291static void free_one_cached_comp_unit (void *);
1292
f792889a
DJ
1293static struct type *set_die_type (struct die_info *, struct type *,
1294 struct dwarf2_cu *);
1c379e20 1295
ae038cb0
DJ
1296static void create_all_comp_units (struct objfile *);
1297
1fd400ff
TT
1298static int create_debug_types_hash_table (struct objfile *objfile);
1299
93311388
DE
1300static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1301 struct objfile *);
10b3939b
DJ
1302
1303static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1304
1305static void dwarf2_add_dependence (struct dwarf2_cu *,
1306 struct dwarf2_per_cu_data *);
1307
ae038cb0
DJ
1308static void dwarf2_mark (struct dwarf2_cu *);
1309
1310static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1311
673bfd45
DE
1312static struct type *get_die_type_at_offset (unsigned int,
1313 struct dwarf2_per_cu_data *per_cu);
1314
f792889a 1315static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1316
9291a0cd
TT
1317static void dwarf2_release_queue (void *dummy);
1318
1319static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1320 struct objfile *objfile);
1321
1322static void process_queue (struct objfile *objfile);
1323
1324static void find_file_and_directory (struct die_info *die,
1325 struct dwarf2_cu *cu,
1326 char **name, char **comp_dir);
1327
1328static char *file_full_name (int file, struct line_header *lh,
1329 const char *comp_dir);
1330
1331static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1332 gdb_byte *info_ptr,
1333 gdb_byte *buffer,
1334 unsigned int buffer_size,
1335 bfd *abfd);
1336
1337static void init_cu_die_reader (struct die_reader_specs *reader,
1338 struct dwarf2_cu *cu);
1339
673bfd45 1340static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1341
9291a0cd
TT
1342#if WORDS_BIGENDIAN
1343
1344/* Convert VALUE between big- and little-endian. */
1345static offset_type
1346byte_swap (offset_type value)
1347{
1348 offset_type result;
1349
1350 result = (value & 0xff) << 24;
1351 result |= (value & 0xff00) << 8;
1352 result |= (value & 0xff0000) >> 8;
1353 result |= (value & 0xff000000) >> 24;
1354 return result;
1355}
1356
1357#define MAYBE_SWAP(V) byte_swap (V)
1358
1359#else
1360#define MAYBE_SWAP(V) (V)
1361#endif /* WORDS_BIGENDIAN */
1362
1363/* The suffix for an index file. */
1364#define INDEX_SUFFIX ".gdb-index"
1365
3da10d80
KS
1366static const char *dwarf2_physname (char *name, struct die_info *die,
1367 struct dwarf2_cu *cu);
1368
c906108c 1369/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1370 information and return true if we have enough to do something.
1371 NAMES points to the dwarf2 section names, or is NULL if the standard
1372 ELF names are used. */
c906108c
SS
1373
1374int
251d32d9
TG
1375dwarf2_has_info (struct objfile *objfile,
1376 const struct dwarf2_debug_sections *names)
c906108c 1377{
be391dca
TT
1378 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1379 if (!dwarf2_per_objfile)
1380 {
1381 /* Initialize per-objfile state. */
1382 struct dwarf2_per_objfile *data
1383 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1384
be391dca
TT
1385 memset (data, 0, sizeof (*data));
1386 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1387 dwarf2_per_objfile = data;
6502dd73 1388
251d32d9
TG
1389 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1390 (void *) names);
be391dca
TT
1391 dwarf2_per_objfile->objfile = objfile;
1392 }
1393 return (dwarf2_per_objfile->info.asection != NULL
1394 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1395}
1396
251d32d9
TG
1397/* When loading sections, we look either for uncompressed section or for
1398 compressed section names. */
233a11ab
CS
1399
1400static int
251d32d9
TG
1401section_is_p (const char *section_name,
1402 const struct dwarf2_section_names *names)
233a11ab 1403{
251d32d9
TG
1404 if (names->normal != NULL
1405 && strcmp (section_name, names->normal) == 0)
1406 return 1;
1407 if (names->compressed != NULL
1408 && strcmp (section_name, names->compressed) == 0)
1409 return 1;
1410 return 0;
233a11ab
CS
1411}
1412
c906108c
SS
1413/* This function is mapped across the sections and remembers the
1414 offset and size of each of the debugging sections we are interested
1415 in. */
1416
1417static void
251d32d9 1418dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1419{
251d32d9
TG
1420 const struct dwarf2_debug_sections *names;
1421
1422 if (vnames == NULL)
1423 names = &dwarf2_elf_names;
1424 else
1425 names = (const struct dwarf2_debug_sections *) vnames;
1426
1427 if (section_is_p (sectp->name, &names->info))
c906108c 1428 {
dce234bc
PP
1429 dwarf2_per_objfile->info.asection = sectp;
1430 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1431 }
251d32d9 1432 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1433 {
dce234bc
PP
1434 dwarf2_per_objfile->abbrev.asection = sectp;
1435 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1436 }
251d32d9 1437 else if (section_is_p (sectp->name, &names->line))
c906108c 1438 {
dce234bc
PP
1439 dwarf2_per_objfile->line.asection = sectp;
1440 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1441 }
251d32d9 1442 else if (section_is_p (sectp->name, &names->loc))
c906108c 1443 {
dce234bc
PP
1444 dwarf2_per_objfile->loc.asection = sectp;
1445 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1446 }
251d32d9 1447 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1448 {
dce234bc
PP
1449 dwarf2_per_objfile->macinfo.asection = sectp;
1450 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1451 }
cf2c3c16
TT
1452 else if (section_is_p (sectp->name, &names->macro))
1453 {
1454 dwarf2_per_objfile->macro.asection = sectp;
1455 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1456 }
251d32d9 1457 else if (section_is_p (sectp->name, &names->str))
c906108c 1458 {
dce234bc
PP
1459 dwarf2_per_objfile->str.asection = sectp;
1460 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1461 }
251d32d9 1462 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1463 {
dce234bc
PP
1464 dwarf2_per_objfile->frame.asection = sectp;
1465 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1466 }
251d32d9 1467 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1468 {
3799ccc6 1469 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1470
3799ccc6
EZ
1471 if (aflag & SEC_HAS_CONTENTS)
1472 {
dce234bc
PP
1473 dwarf2_per_objfile->eh_frame.asection = sectp;
1474 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1475 }
b6af0555 1476 }
251d32d9 1477 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1478 {
dce234bc
PP
1479 dwarf2_per_objfile->ranges.asection = sectp;
1480 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1481 }
251d32d9 1482 else if (section_is_p (sectp->name, &names->types))
348e048f 1483 {
8b70b953
TT
1484 struct dwarf2_section_info type_section;
1485
1486 memset (&type_section, 0, sizeof (type_section));
1487 type_section.asection = sectp;
1488 type_section.size = bfd_get_section_size (sectp);
1489
1490 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1491 &type_section);
348e048f 1492 }
251d32d9 1493 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1494 {
1495 dwarf2_per_objfile->gdb_index.asection = sectp;
1496 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1497 }
dce234bc 1498
72dca2f5
FR
1499 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1500 && bfd_section_vma (abfd, sectp) == 0)
1501 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1502}
1503
dce234bc
PP
1504/* Decompress a section that was compressed using zlib. Store the
1505 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1506
1507static void
dce234bc
PP
1508zlib_decompress_section (struct objfile *objfile, asection *sectp,
1509 gdb_byte **outbuf, bfd_size_type *outsize)
1510{
1511 bfd *abfd = objfile->obfd;
1512#ifndef HAVE_ZLIB_H
1513 error (_("Support for zlib-compressed DWARF data (from '%s') "
1514 "is disabled in this copy of GDB"),
1515 bfd_get_filename (abfd));
1516#else
1517 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1518 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1519 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1520 bfd_size_type uncompressed_size;
1521 gdb_byte *uncompressed_buffer;
1522 z_stream strm;
1523 int rc;
1524 int header_size = 12;
1525
1526 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1527 || bfd_bread (compressed_buffer,
1528 compressed_size, abfd) != compressed_size)
dce234bc
PP
1529 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1530 bfd_get_filename (abfd));
1531
1532 /* Read the zlib header. In this case, it should be "ZLIB" followed
1533 by the uncompressed section size, 8 bytes in big-endian order. */
1534 if (compressed_size < header_size
1535 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1536 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1537 bfd_get_filename (abfd));
1538 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1539 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1540 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1541 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1542 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1543 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1544 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1545 uncompressed_size += compressed_buffer[11];
1546
1547 /* It is possible the section consists of several compressed
1548 buffers concatenated together, so we uncompress in a loop. */
1549 strm.zalloc = NULL;
1550 strm.zfree = NULL;
1551 strm.opaque = NULL;
1552 strm.avail_in = compressed_size - header_size;
1553 strm.next_in = (Bytef*) compressed_buffer + header_size;
1554 strm.avail_out = uncompressed_size;
1555 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1556 uncompressed_size);
1557 rc = inflateInit (&strm);
1558 while (strm.avail_in > 0)
1559 {
1560 if (rc != Z_OK)
1561 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1562 bfd_get_filename (abfd), rc);
1563 strm.next_out = ((Bytef*) uncompressed_buffer
1564 + (uncompressed_size - strm.avail_out));
1565 rc = inflate (&strm, Z_FINISH);
1566 if (rc != Z_STREAM_END)
1567 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1568 bfd_get_filename (abfd), rc);
1569 rc = inflateReset (&strm);
1570 }
1571 rc = inflateEnd (&strm);
1572 if (rc != Z_OK
1573 || strm.avail_out != 0)
1574 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1575 bfd_get_filename (abfd), rc);
1576
affddf13 1577 do_cleanups (cleanup);
dce234bc
PP
1578 *outbuf = uncompressed_buffer;
1579 *outsize = uncompressed_size;
1580#endif
233a11ab
CS
1581}
1582
9e0ac564
TT
1583/* A helper function that decides whether a section is empty. */
1584
1585static int
1586dwarf2_section_empty_p (struct dwarf2_section_info *info)
1587{
1588 return info->asection == NULL || info->size == 0;
1589}
1590
dce234bc
PP
1591/* Read the contents of the section SECTP from object file specified by
1592 OBJFILE, store info about the section into INFO.
1593 If the section is compressed, uncompress it before returning. */
c906108c 1594
dce234bc
PP
1595static void
1596dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1597{
dce234bc
PP
1598 bfd *abfd = objfile->obfd;
1599 asection *sectp = info->asection;
1600 gdb_byte *buf, *retbuf;
1601 unsigned char header[4];
c906108c 1602
be391dca
TT
1603 if (info->readin)
1604 return;
dce234bc 1605 info->buffer = NULL;
b315ab21 1606 info->map_addr = NULL;
be391dca 1607 info->readin = 1;
188dd5d6 1608
9e0ac564 1609 if (dwarf2_section_empty_p (info))
dce234bc 1610 return;
c906108c 1611
dce234bc
PP
1612 /* Check if the file has a 4-byte header indicating compression. */
1613 if (info->size > sizeof (header)
1614 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1615 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1616 {
1617 /* Upon decompression, update the buffer and its size. */
1618 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1619 {
1620 zlib_decompress_section (objfile, sectp, &info->buffer,
1621 &info->size);
1622 return;
1623 }
1624 }
4bdf3d34 1625
dce234bc
PP
1626#ifdef HAVE_MMAP
1627 if (pagesize == 0)
1628 pagesize = getpagesize ();
2e276125 1629
dce234bc
PP
1630 /* Only try to mmap sections which are large enough: we don't want to
1631 waste space due to fragmentation. Also, only try mmap for sections
1632 without relocations. */
1633
1634 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1635 {
b315ab21
TG
1636 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1637 MAP_PRIVATE, sectp->filepos,
1638 &info->map_addr, &info->map_len);
dce234bc 1639
b315ab21 1640 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1641 {
be391dca 1642#if HAVE_POSIX_MADVISE
b315ab21 1643 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1644#endif
dce234bc
PP
1645 return;
1646 }
1647 }
1648#endif
1649
1650 /* If we get here, we are a normal, not-compressed section. */
1651 info->buffer = buf
1652 = obstack_alloc (&objfile->objfile_obstack, info->size);
1653
1654 /* When debugging .o files, we may need to apply relocations; see
1655 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1656 We never compress sections in .o files, so we only need to
1657 try this when the section is not compressed. */
ac8035ab 1658 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1659 if (retbuf != NULL)
1660 {
1661 info->buffer = retbuf;
1662 return;
1663 }
1664
1665 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1666 || bfd_bread (buf, info->size, abfd) != info->size)
1667 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1668 bfd_get_filename (abfd));
1669}
1670
9e0ac564
TT
1671/* A helper function that returns the size of a section in a safe way.
1672 If you are positive that the section has been read before using the
1673 size, then it is safe to refer to the dwarf2_section_info object's
1674 "size" field directly. In other cases, you must call this
1675 function, because for compressed sections the size field is not set
1676 correctly until the section has been read. */
1677
1678static bfd_size_type
1679dwarf2_section_size (struct objfile *objfile,
1680 struct dwarf2_section_info *info)
1681{
1682 if (!info->readin)
1683 dwarf2_read_section (objfile, info);
1684 return info->size;
1685}
1686
dce234bc 1687/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1688 SECTION_NAME. */
af34e669 1689
dce234bc 1690void
3017a003
TG
1691dwarf2_get_section_info (struct objfile *objfile,
1692 enum dwarf2_section_enum sect,
dce234bc
PP
1693 asection **sectp, gdb_byte **bufp,
1694 bfd_size_type *sizep)
1695{
1696 struct dwarf2_per_objfile *data
1697 = objfile_data (objfile, dwarf2_objfile_data_key);
1698 struct dwarf2_section_info *info;
a3b2a86b
TT
1699
1700 /* We may see an objfile without any DWARF, in which case we just
1701 return nothing. */
1702 if (data == NULL)
1703 {
1704 *sectp = NULL;
1705 *bufp = NULL;
1706 *sizep = 0;
1707 return;
1708 }
3017a003
TG
1709 switch (sect)
1710 {
1711 case DWARF2_DEBUG_FRAME:
1712 info = &data->frame;
1713 break;
1714 case DWARF2_EH_FRAME:
1715 info = &data->eh_frame;
1716 break;
1717 default:
1718 gdb_assert_not_reached ("unexpected section");
1719 }
dce234bc 1720
9e0ac564 1721 dwarf2_read_section (objfile, info);
dce234bc
PP
1722
1723 *sectp = info->asection;
1724 *bufp = info->buffer;
1725 *sizep = info->size;
1726}
1727
9291a0cd 1728\f
7b9f3c50
DE
1729/* DWARF quick_symbols_functions support. */
1730
1731/* TUs can share .debug_line entries, and there can be a lot more TUs than
1732 unique line tables, so we maintain a separate table of all .debug_line
1733 derived entries to support the sharing.
1734 All the quick functions need is the list of file names. We discard the
1735 line_header when we're done and don't need to record it here. */
1736struct quick_file_names
1737{
1738 /* The offset in .debug_line of the line table. We hash on this. */
1739 unsigned int offset;
1740
1741 /* The number of entries in file_names, real_names. */
1742 unsigned int num_file_names;
1743
1744 /* The file names from the line table, after being run through
1745 file_full_name. */
1746 const char **file_names;
1747
1748 /* The file names from the line table after being run through
1749 gdb_realpath. These are computed lazily. */
1750 const char **real_names;
1751};
1752
1753/* When using the index (and thus not using psymtabs), each CU has an
1754 object of this type. This is used to hold information needed by
1755 the various "quick" methods. */
1756struct dwarf2_per_cu_quick_data
1757{
1758 /* The file table. This can be NULL if there was no file table
1759 or it's currently not read in.
1760 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1761 struct quick_file_names *file_names;
1762
1763 /* The corresponding symbol table. This is NULL if symbols for this
1764 CU have not yet been read. */
1765 struct symtab *symtab;
1766
1767 /* A temporary mark bit used when iterating over all CUs in
1768 expand_symtabs_matching. */
1769 unsigned int mark : 1;
1770
1771 /* True if we've tried to read the file table and found there isn't one.
1772 There will be no point in trying to read it again next time. */
1773 unsigned int no_file_data : 1;
1774};
1775
1776/* Hash function for a quick_file_names. */
1777
1778static hashval_t
1779hash_file_name_entry (const void *e)
1780{
1781 const struct quick_file_names *file_data = e;
1782
1783 return file_data->offset;
1784}
1785
1786/* Equality function for a quick_file_names. */
1787
1788static int
1789eq_file_name_entry (const void *a, const void *b)
1790{
1791 const struct quick_file_names *ea = a;
1792 const struct quick_file_names *eb = b;
1793
1794 return ea->offset == eb->offset;
1795}
1796
1797/* Delete function for a quick_file_names. */
1798
1799static void
1800delete_file_name_entry (void *e)
1801{
1802 struct quick_file_names *file_data = e;
1803 int i;
1804
1805 for (i = 0; i < file_data->num_file_names; ++i)
1806 {
1807 xfree ((void*) file_data->file_names[i]);
1808 if (file_data->real_names)
1809 xfree ((void*) file_data->real_names[i]);
1810 }
1811
1812 /* The space for the struct itself lives on objfile_obstack,
1813 so we don't free it here. */
1814}
1815
1816/* Create a quick_file_names hash table. */
1817
1818static htab_t
1819create_quick_file_names_table (unsigned int nr_initial_entries)
1820{
1821 return htab_create_alloc (nr_initial_entries,
1822 hash_file_name_entry, eq_file_name_entry,
1823 delete_file_name_entry, xcalloc, xfree);
1824}
9291a0cd 1825
918dd910
JK
1826/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1827 have to be created afterwards. You should call age_cached_comp_units after
1828 processing PER_CU->CU. dw2_setup must have been already called. */
1829
1830static void
1831load_cu (struct dwarf2_per_cu_data *per_cu)
1832{
8b70b953
TT
1833 if (per_cu->debug_type_section)
1834 read_signatured_type_at_offset (per_cu->objfile,
1835 per_cu->debug_type_section,
1836 per_cu->offset);
918dd910
JK
1837 else
1838 load_full_comp_unit (per_cu, per_cu->objfile);
1839
1840 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1841
1842 gdb_assert (per_cu->cu != NULL);
1843}
1844
9291a0cd
TT
1845/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1846 this CU came. */
2fdf6df6 1847
9291a0cd
TT
1848static void
1849dw2_do_instantiate_symtab (struct objfile *objfile,
1850 struct dwarf2_per_cu_data *per_cu)
1851{
1852 struct cleanup *back_to;
1853
1854 back_to = make_cleanup (dwarf2_release_queue, NULL);
1855
1856 queue_comp_unit (per_cu, objfile);
1857
918dd910 1858 load_cu (per_cu);
9291a0cd
TT
1859
1860 process_queue (objfile);
1861
1862 /* Age the cache, releasing compilation units that have not
1863 been used recently. */
1864 age_cached_comp_units ();
1865
1866 do_cleanups (back_to);
1867}
1868
1869/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1870 the objfile from which this CU came. Returns the resulting symbol
1871 table. */
2fdf6df6 1872
9291a0cd
TT
1873static struct symtab *
1874dw2_instantiate_symtab (struct objfile *objfile,
1875 struct dwarf2_per_cu_data *per_cu)
1876{
1877 if (!per_cu->v.quick->symtab)
1878 {
1879 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1880 increment_reading_symtab ();
1881 dw2_do_instantiate_symtab (objfile, per_cu);
1882 do_cleanups (back_to);
1883 }
1884 return per_cu->v.quick->symtab;
1885}
1886
1fd400ff 1887/* Return the CU given its index. */
2fdf6df6 1888
1fd400ff
TT
1889static struct dwarf2_per_cu_data *
1890dw2_get_cu (int index)
1891{
1892 if (index >= dwarf2_per_objfile->n_comp_units)
1893 {
1894 index -= dwarf2_per_objfile->n_comp_units;
1895 return dwarf2_per_objfile->type_comp_units[index];
1896 }
1897 return dwarf2_per_objfile->all_comp_units[index];
1898}
1899
9291a0cd
TT
1900/* A helper function that knows how to read a 64-bit value in a way
1901 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1902 otherwise. */
2fdf6df6 1903
9291a0cd
TT
1904static int
1905extract_cu_value (const char *bytes, ULONGEST *result)
1906{
1907 if (sizeof (ULONGEST) < 8)
1908 {
1909 int i;
1910
1911 /* Ignore the upper 4 bytes if they are all zero. */
1912 for (i = 0; i < 4; ++i)
1913 if (bytes[i + 4] != 0)
1914 return 0;
1915
1916 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1917 }
1918 else
1919 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1920 return 1;
1921}
1922
1923/* Read the CU list from the mapped index, and use it to create all
1924 the CU objects for this objfile. Return 0 if something went wrong,
1925 1 if everything went ok. */
2fdf6df6 1926
9291a0cd 1927static int
1fd400ff
TT
1928create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1929 offset_type cu_list_elements)
9291a0cd
TT
1930{
1931 offset_type i;
9291a0cd
TT
1932
1933 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1934 dwarf2_per_objfile->all_comp_units
1935 = obstack_alloc (&objfile->objfile_obstack,
1936 dwarf2_per_objfile->n_comp_units
1937 * sizeof (struct dwarf2_per_cu_data *));
1938
1939 for (i = 0; i < cu_list_elements; i += 2)
1940 {
1941 struct dwarf2_per_cu_data *the_cu;
1942 ULONGEST offset, length;
1943
1944 if (!extract_cu_value (cu_list, &offset)
1945 || !extract_cu_value (cu_list + 8, &length))
1946 return 0;
1947 cu_list += 2 * 8;
1948
1949 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1950 struct dwarf2_per_cu_data);
1951 the_cu->offset = offset;
1952 the_cu->length = length;
1953 the_cu->objfile = objfile;
1954 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1955 struct dwarf2_per_cu_quick_data);
1956 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1957 }
1958
1959 return 1;
1960}
1961
1fd400ff 1962/* Create the signatured type hash table from the index. */
673bfd45 1963
1fd400ff 1964static int
673bfd45 1965create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 1966 struct dwarf2_section_info *section,
673bfd45
DE
1967 const gdb_byte *bytes,
1968 offset_type elements)
1fd400ff
TT
1969{
1970 offset_type i;
673bfd45 1971 htab_t sig_types_hash;
1fd400ff
TT
1972
1973 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1974 dwarf2_per_objfile->type_comp_units
1975 = obstack_alloc (&objfile->objfile_obstack,
1976 dwarf2_per_objfile->n_type_comp_units
1977 * sizeof (struct dwarf2_per_cu_data *));
1978
673bfd45 1979 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1980
1981 for (i = 0; i < elements; i += 3)
1982 {
1983 struct signatured_type *type_sig;
1984 ULONGEST offset, type_offset, signature;
1985 void **slot;
1986
1987 if (!extract_cu_value (bytes, &offset)
1988 || !extract_cu_value (bytes + 8, &type_offset))
1989 return 0;
1990 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1991 bytes += 3 * 8;
1992
1993 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1994 struct signatured_type);
1995 type_sig->signature = signature;
1fd400ff 1996 type_sig->type_offset = type_offset;
8b70b953 1997 type_sig->per_cu.debug_type_section = section;
1fd400ff
TT
1998 type_sig->per_cu.offset = offset;
1999 type_sig->per_cu.objfile = objfile;
2000 type_sig->per_cu.v.quick
2001 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2002 struct dwarf2_per_cu_quick_data);
2003
673bfd45 2004 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
2005 *slot = type_sig;
2006
2007 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
2008 }
2009
673bfd45 2010 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2011
2012 return 1;
2013}
2014
9291a0cd
TT
2015/* Read the address map data from the mapped index, and use it to
2016 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2017
9291a0cd
TT
2018static void
2019create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2020{
2021 const gdb_byte *iter, *end;
2022 struct obstack temp_obstack;
2023 struct addrmap *mutable_map;
2024 struct cleanup *cleanup;
2025 CORE_ADDR baseaddr;
2026
2027 obstack_init (&temp_obstack);
2028 cleanup = make_cleanup_obstack_free (&temp_obstack);
2029 mutable_map = addrmap_create_mutable (&temp_obstack);
2030
2031 iter = index->address_table;
2032 end = iter + index->address_table_size;
2033
2034 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2035
2036 while (iter < end)
2037 {
2038 ULONGEST hi, lo, cu_index;
2039 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2040 iter += 8;
2041 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2042 iter += 8;
2043 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2044 iter += 4;
2045
2046 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2047 dw2_get_cu (cu_index));
9291a0cd
TT
2048 }
2049
2050 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2051 &objfile->objfile_obstack);
2052 do_cleanups (cleanup);
2053}
2054
59d7bcaf
JK
2055/* The hash function for strings in the mapped index. This is the same as
2056 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2057 implementation. This is necessary because the hash function is tied to the
2058 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2059 SYMBOL_HASH_NEXT.
2060
2061 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2062
9291a0cd 2063static hashval_t
559a7a62 2064mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2065{
2066 const unsigned char *str = (const unsigned char *) p;
2067 hashval_t r = 0;
2068 unsigned char c;
2069
2070 while ((c = *str++) != 0)
559a7a62
JK
2071 {
2072 if (index_version >= 5)
2073 c = tolower (c);
2074 r = r * 67 + c - 113;
2075 }
9291a0cd
TT
2076
2077 return r;
2078}
2079
2080/* Find a slot in the mapped index INDEX for the object named NAME.
2081 If NAME is found, set *VEC_OUT to point to the CU vector in the
2082 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2083
9291a0cd
TT
2084static int
2085find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2086 offset_type **vec_out)
2087{
0cf03b49
JK
2088 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2089 offset_type hash;
9291a0cd 2090 offset_type slot, step;
559a7a62 2091 int (*cmp) (const char *, const char *);
9291a0cd 2092
0cf03b49
JK
2093 if (current_language->la_language == language_cplus
2094 || current_language->la_language == language_java
2095 || current_language->la_language == language_fortran)
2096 {
2097 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2098 not contain any. */
2099 const char *paren = strchr (name, '(');
2100
2101 if (paren)
2102 {
2103 char *dup;
2104
2105 dup = xmalloc (paren - name + 1);
2106 memcpy (dup, name, paren - name);
2107 dup[paren - name] = 0;
2108
2109 make_cleanup (xfree, dup);
2110 name = dup;
2111 }
2112 }
2113
559a7a62
JK
2114 /* Index version 4 did not support case insensitive searches. But the
2115 indexes for case insensitive languages are built in lowercase, therefore
2116 simulate our NAME being searched is also lowercased. */
2117 hash = mapped_index_string_hash ((index->version == 4
2118 && case_sensitivity == case_sensitive_off
2119 ? 5 : index->version),
2120 name);
2121
3876f04e
DE
2122 slot = hash & (index->symbol_table_slots - 1);
2123 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2124 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2125
2126 for (;;)
2127 {
2128 /* Convert a slot number to an offset into the table. */
2129 offset_type i = 2 * slot;
2130 const char *str;
3876f04e 2131 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2132 {
2133 do_cleanups (back_to);
2134 return 0;
2135 }
9291a0cd 2136
3876f04e 2137 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2138 if (!cmp (name, str))
9291a0cd
TT
2139 {
2140 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2141 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2142 do_cleanups (back_to);
9291a0cd
TT
2143 return 1;
2144 }
2145
3876f04e 2146 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2147 }
2148}
2149
2150/* Read the index file. If everything went ok, initialize the "quick"
2151 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2152
9291a0cd
TT
2153static int
2154dwarf2_read_index (struct objfile *objfile)
2155{
9291a0cd
TT
2156 char *addr;
2157 struct mapped_index *map;
b3b272e1 2158 offset_type *metadata;
ac0b195c
KW
2159 const gdb_byte *cu_list;
2160 const gdb_byte *types_list = NULL;
2161 offset_type version, cu_list_elements;
2162 offset_type types_list_elements = 0;
1fd400ff 2163 int i;
9291a0cd 2164
9e0ac564 2165 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2166 return 0;
82430852
JK
2167
2168 /* Older elfutils strip versions could keep the section in the main
2169 executable while splitting it for the separate debug info file. */
2170 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2171 & SEC_HAS_CONTENTS) == 0)
2172 return 0;
2173
9291a0cd
TT
2174 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2175
2176 addr = dwarf2_per_objfile->gdb_index.buffer;
2177 /* Version check. */
1fd400ff 2178 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2179 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2180 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2181 contained incomplete addrmap. So, it seems better to just ignore such
559a7a62
JK
2182 indices. Index version 4 uses a different hash function than index
2183 version 5 and later. */
831adc1f 2184 if (version < 4)
9291a0cd 2185 return 0;
594e8718
JK
2186 /* Indexes with higher version than the one supported by GDB may be no
2187 longer backward compatible. */
559a7a62 2188 if (version > 5)
594e8718 2189 return 0;
9291a0cd
TT
2190
2191 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2192 map->version = version;
b3b272e1 2193 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2194
2195 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2196
2197 i = 0;
2198 cu_list = addr + MAYBE_SWAP (metadata[i]);
2199 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2200 / 8);
1fd400ff
TT
2201 ++i;
2202
987d643c
TT
2203 types_list = addr + MAYBE_SWAP (metadata[i]);
2204 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2205 - MAYBE_SWAP (metadata[i]))
2206 / 8);
2207 ++i;
1fd400ff
TT
2208
2209 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2210 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2211 - MAYBE_SWAP (metadata[i]));
2212 ++i;
2213
3876f04e
DE
2214 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2215 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2216 - MAYBE_SWAP (metadata[i]))
2217 / (2 * sizeof (offset_type)));
1fd400ff 2218 ++i;
9291a0cd 2219
1fd400ff
TT
2220 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2221
2222 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2223 return 0;
2224
8b70b953
TT
2225 if (types_list_elements)
2226 {
2227 struct dwarf2_section_info *section;
2228
2229 /* We can only handle a single .debug_types when we have an
2230 index. */
2231 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2232 return 0;
2233
2234 section = VEC_index (dwarf2_section_info_def,
2235 dwarf2_per_objfile->types, 0);
2236
2237 if (!create_signatured_type_table_from_index (objfile, section,
2238 types_list,
2239 types_list_elements))
2240 return 0;
2241 }
9291a0cd
TT
2242
2243 create_addrmap_from_index (objfile, map);
2244
2245 dwarf2_per_objfile->index_table = map;
2246 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2247 dwarf2_per_objfile->quick_file_names_table =
2248 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2249
2250 return 1;
2251}
2252
2253/* A helper for the "quick" functions which sets the global
2254 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2255
9291a0cd
TT
2256static void
2257dw2_setup (struct objfile *objfile)
2258{
2259 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2260 gdb_assert (dwarf2_per_objfile);
2261}
2262
2263/* A helper for the "quick" functions which attempts to read the line
2264 table for THIS_CU. */
2fdf6df6 2265
7b9f3c50
DE
2266static struct quick_file_names *
2267dw2_get_file_names (struct objfile *objfile,
2268 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2269{
2270 bfd *abfd = objfile->obfd;
7b9f3c50 2271 struct line_header *lh;
9291a0cd
TT
2272 struct attribute *attr;
2273 struct cleanup *cleanups;
2274 struct die_info *comp_unit_die;
36374493 2275 struct dwarf2_section_info* sec;
9291a0cd
TT
2276 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2277 int has_children, i;
2278 struct dwarf2_cu cu;
2279 unsigned int bytes_read, buffer_size;
2280 struct die_reader_specs reader_specs;
2281 char *name, *comp_dir;
7b9f3c50
DE
2282 void **slot;
2283 struct quick_file_names *qfn;
2284 unsigned int line_offset;
9291a0cd 2285
7b9f3c50
DE
2286 if (this_cu->v.quick->file_names != NULL)
2287 return this_cu->v.quick->file_names;
2288 /* If we know there is no line data, no point in looking again. */
2289 if (this_cu->v.quick->no_file_data)
2290 return NULL;
9291a0cd 2291
9816fde3 2292 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2293 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2294
8b70b953
TT
2295 if (this_cu->debug_type_section)
2296 sec = this_cu->debug_type_section;
36374493
DE
2297 else
2298 sec = &dwarf2_per_objfile->info;
2299 dwarf2_read_section (objfile, sec);
2300 buffer_size = sec->size;
2301 buffer = sec->buffer;
9291a0cd
TT
2302 info_ptr = buffer + this_cu->offset;
2303 beg_of_comp_unit = info_ptr;
2304
2305 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2306 buffer, buffer_size,
2307 abfd);
2308
2309 /* Complete the cu_header. */
2310 cu.header.offset = beg_of_comp_unit - buffer;
2311 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2312
2313 this_cu->cu = &cu;
2314 cu.per_cu = this_cu;
2315
2316 dwarf2_read_abbrevs (abfd, &cu);
2317 make_cleanup (dwarf2_free_abbrev_table, &cu);
2318
8b70b953 2319 if (this_cu->debug_type_section)
9291a0cd
TT
2320 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2321 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2322 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2323 &has_children);
9291a0cd 2324
7b9f3c50
DE
2325 lh = NULL;
2326 slot = NULL;
2327 line_offset = 0;
9291a0cd
TT
2328 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2329 if (attr)
2330 {
7b9f3c50
DE
2331 struct quick_file_names find_entry;
2332
2333 line_offset = DW_UNSND (attr);
2334
2335 /* We may have already read in this line header (TU line header sharing).
2336 If we have we're done. */
2337 find_entry.offset = line_offset;
2338 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2339 &find_entry, INSERT);
2340 if (*slot != NULL)
2341 {
2342 do_cleanups (cleanups);
2343 this_cu->v.quick->file_names = *slot;
2344 return *slot;
2345 }
2346
9291a0cd
TT
2347 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2348 }
2349 if (lh == NULL)
2350 {
2351 do_cleanups (cleanups);
7b9f3c50
DE
2352 this_cu->v.quick->no_file_data = 1;
2353 return NULL;
9291a0cd
TT
2354 }
2355
7b9f3c50
DE
2356 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2357 qfn->offset = line_offset;
2358 gdb_assert (slot != NULL);
2359 *slot = qfn;
9291a0cd 2360
7b9f3c50 2361 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2362
7b9f3c50
DE
2363 qfn->num_file_names = lh->num_file_names;
2364 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2365 lh->num_file_names * sizeof (char *));
9291a0cd 2366 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2367 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2368 qfn->real_names = NULL;
9291a0cd 2369
7b9f3c50 2370 free_line_header (lh);
9291a0cd 2371 do_cleanups (cleanups);
7b9f3c50
DE
2372
2373 this_cu->v.quick->file_names = qfn;
2374 return qfn;
9291a0cd
TT
2375}
2376
2377/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2378 real path for a given file name from the line table. */
2fdf6df6 2379
9291a0cd 2380static const char *
7b9f3c50
DE
2381dw2_get_real_path (struct objfile *objfile,
2382 struct quick_file_names *qfn, int index)
9291a0cd 2383{
7b9f3c50
DE
2384 if (qfn->real_names == NULL)
2385 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2386 qfn->num_file_names, sizeof (char *));
9291a0cd 2387
7b9f3c50
DE
2388 if (qfn->real_names[index] == NULL)
2389 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2390
7b9f3c50 2391 return qfn->real_names[index];
9291a0cd
TT
2392}
2393
2394static struct symtab *
2395dw2_find_last_source_symtab (struct objfile *objfile)
2396{
2397 int index;
ae2de4f8 2398
9291a0cd
TT
2399 dw2_setup (objfile);
2400 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2401 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2402}
2403
7b9f3c50
DE
2404/* Traversal function for dw2_forget_cached_source_info. */
2405
2406static int
2407dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2408{
7b9f3c50 2409 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2410
7b9f3c50 2411 if (file_data->real_names)
9291a0cd 2412 {
7b9f3c50 2413 int i;
9291a0cd 2414
7b9f3c50 2415 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2416 {
7b9f3c50
DE
2417 xfree ((void*) file_data->real_names[i]);
2418 file_data->real_names[i] = NULL;
9291a0cd
TT
2419 }
2420 }
7b9f3c50
DE
2421
2422 return 1;
2423}
2424
2425static void
2426dw2_forget_cached_source_info (struct objfile *objfile)
2427{
2428 dw2_setup (objfile);
2429
2430 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2431 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2432}
2433
2434static int
2435dw2_lookup_symtab (struct objfile *objfile, const char *name,
2436 const char *full_path, const char *real_path,
2437 struct symtab **result)
2438{
2439 int i;
2440 int check_basename = lbasename (name) == name;
2441 struct dwarf2_per_cu_data *base_cu = NULL;
2442
2443 dw2_setup (objfile);
ae2de4f8 2444
1fd400ff
TT
2445 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2446 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2447 {
2448 int j;
e254ef6a 2449 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2450 struct quick_file_names *file_data;
9291a0cd 2451
e254ef6a 2452 if (per_cu->v.quick->symtab)
9291a0cd
TT
2453 continue;
2454
7b9f3c50
DE
2455 file_data = dw2_get_file_names (objfile, per_cu);
2456 if (file_data == NULL)
9291a0cd
TT
2457 continue;
2458
7b9f3c50 2459 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2460 {
7b9f3c50 2461 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2462
2463 if (FILENAME_CMP (name, this_name) == 0)
2464 {
e254ef6a 2465 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2466 return 1;
2467 }
2468
2469 if (check_basename && ! base_cu
2470 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2471 base_cu = per_cu;
9291a0cd
TT
2472
2473 if (full_path != NULL)
2474 {
7b9f3c50
DE
2475 const char *this_real_name = dw2_get_real_path (objfile,
2476 file_data, j);
9291a0cd 2477
7b9f3c50
DE
2478 if (this_real_name != NULL
2479 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2480 {
e254ef6a 2481 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2482 return 1;
2483 }
2484 }
2485
2486 if (real_path != NULL)
2487 {
7b9f3c50
DE
2488 const char *this_real_name = dw2_get_real_path (objfile,
2489 file_data, j);
9291a0cd 2490
7b9f3c50
DE
2491 if (this_real_name != NULL
2492 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2493 {
74dd2ca6
DE
2494 *result = dw2_instantiate_symtab (objfile, per_cu);
2495 return 1;
9291a0cd
TT
2496 }
2497 }
2498 }
2499 }
2500
2501 if (base_cu)
2502 {
2503 *result = dw2_instantiate_symtab (objfile, base_cu);
2504 return 1;
2505 }
2506
2507 return 0;
2508}
2509
2510static struct symtab *
2511dw2_lookup_symbol (struct objfile *objfile, int block_index,
2512 const char *name, domain_enum domain)
2513{
774b6a14 2514 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2515 instead. */
2516 return NULL;
2517}
2518
2519/* A helper function that expands all symtabs that hold an object
2520 named NAME. */
2fdf6df6 2521
9291a0cd
TT
2522static void
2523dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2524{
2525 dw2_setup (objfile);
2526
ae2de4f8 2527 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2528 if (dwarf2_per_objfile->index_table)
2529 {
2530 offset_type *vec;
2531
2532 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2533 name, &vec))
2534 {
2535 offset_type i, len = MAYBE_SWAP (*vec);
2536 for (i = 0; i < len; ++i)
2537 {
2538 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2539 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2540
e254ef6a 2541 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2542 }
2543 }
2544 }
2545}
2546
774b6a14
TT
2547static void
2548dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2549 enum block_enum block_kind, const char *name,
774b6a14 2550 domain_enum domain)
9291a0cd 2551{
774b6a14 2552 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2553}
2554
2555static void
2556dw2_print_stats (struct objfile *objfile)
2557{
2558 int i, count;
2559
2560 dw2_setup (objfile);
2561 count = 0;
1fd400ff
TT
2562 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2563 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2564 {
e254ef6a 2565 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2566
e254ef6a 2567 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2568 ++count;
2569 }
2570 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2571}
2572
2573static void
2574dw2_dump (struct objfile *objfile)
2575{
2576 /* Nothing worth printing. */
2577}
2578
2579static void
2580dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2581 struct section_offsets *delta)
2582{
2583 /* There's nothing to relocate here. */
2584}
2585
2586static void
2587dw2_expand_symtabs_for_function (struct objfile *objfile,
2588 const char *func_name)
2589{
2590 dw2_do_expand_symtabs_matching (objfile, func_name);
2591}
2592
2593static void
2594dw2_expand_all_symtabs (struct objfile *objfile)
2595{
2596 int i;
2597
2598 dw2_setup (objfile);
1fd400ff
TT
2599
2600 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2601 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2602 {
e254ef6a 2603 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2604
e254ef6a 2605 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2606 }
2607}
2608
2609static void
2610dw2_expand_symtabs_with_filename (struct objfile *objfile,
2611 const char *filename)
2612{
2613 int i;
2614
2615 dw2_setup (objfile);
d4637a04
DE
2616
2617 /* We don't need to consider type units here.
2618 This is only called for examining code, e.g. expand_line_sal.
2619 There can be an order of magnitude (or more) more type units
2620 than comp units, and we avoid them if we can. */
2621
2622 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2623 {
2624 int j;
e254ef6a 2625 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2626 struct quick_file_names *file_data;
9291a0cd 2627
e254ef6a 2628 if (per_cu->v.quick->symtab)
9291a0cd
TT
2629 continue;
2630
7b9f3c50
DE
2631 file_data = dw2_get_file_names (objfile, per_cu);
2632 if (file_data == NULL)
9291a0cd
TT
2633 continue;
2634
7b9f3c50 2635 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2636 {
7b9f3c50 2637 const char *this_name = file_data->file_names[j];
1ef75ecc 2638 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2639 {
e254ef6a 2640 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2641 break;
2642 }
2643 }
2644 }
2645}
2646
dd786858 2647static const char *
9291a0cd
TT
2648dw2_find_symbol_file (struct objfile *objfile, const char *name)
2649{
e254ef6a 2650 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2651 offset_type *vec;
7b9f3c50 2652 struct quick_file_names *file_data;
9291a0cd
TT
2653
2654 dw2_setup (objfile);
2655
ae2de4f8 2656 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2657 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2658 {
2659 struct symtab *s;
2660
2661 ALL_OBJFILE_SYMTABS (objfile, s)
2662 if (s->primary)
2663 {
2664 struct blockvector *bv = BLOCKVECTOR (s);
2665 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2666 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2667
2668 if (sym)
2669 return sym->symtab->filename;
2670 }
2671 return NULL;
2672 }
9291a0cd
TT
2673
2674 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2675 name, &vec))
2676 return NULL;
2677
2678 /* Note that this just looks at the very first one named NAME -- but
2679 actually we are looking for a function. find_main_filename
2680 should be rewritten so that it doesn't require a custom hook. It
2681 could just use the ordinary symbol tables. */
2682 /* vec[0] is the length, which must always be >0. */
e254ef6a 2683 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2684
7b9f3c50
DE
2685 file_data = dw2_get_file_names (objfile, per_cu);
2686 if (file_data == NULL)
9291a0cd
TT
2687 return NULL;
2688
7b9f3c50 2689 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2690}
2691
2692static void
40658b94
PH
2693dw2_map_matching_symbols (const char * name, domain_enum namespace,
2694 struct objfile *objfile, int global,
2695 int (*callback) (struct block *,
2696 struct symbol *, void *),
2edb89d3
JK
2697 void *data, symbol_compare_ftype *match,
2698 symbol_compare_ftype *ordered_compare)
9291a0cd 2699{
40658b94 2700 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2701 current language is Ada for a non-Ada objfile using GNU index. As Ada
2702 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2703}
2704
2705static void
2706dw2_expand_symtabs_matching (struct objfile *objfile,
2707 int (*file_matcher) (const char *, void *),
2708 int (*name_matcher) (const char *, void *),
8903c50d 2709 enum search_domain kind,
9291a0cd
TT
2710 void *data)
2711{
2712 int i;
2713 offset_type iter;
4b5246aa 2714 struct mapped_index *index;
9291a0cd
TT
2715
2716 dw2_setup (objfile);
ae2de4f8
DE
2717
2718 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2719 if (!dwarf2_per_objfile->index_table)
2720 return;
4b5246aa 2721 index = dwarf2_per_objfile->index_table;
9291a0cd 2722
7b08b9eb
JK
2723 if (file_matcher != NULL)
2724 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2725 + dwarf2_per_objfile->n_type_comp_units); ++i)
2726 {
2727 int j;
2728 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2729 struct quick_file_names *file_data;
2730
2731 per_cu->v.quick->mark = 0;
2732 if (per_cu->v.quick->symtab)
2733 continue;
2734
2735 file_data = dw2_get_file_names (objfile, per_cu);
2736 if (file_data == NULL)
2737 continue;
2738
2739 for (j = 0; j < file_data->num_file_names; ++j)
2740 {
2741 if (file_matcher (file_data->file_names[j], data))
2742 {
2743 per_cu->v.quick->mark = 1;
2744 break;
2745 }
2746 }
2747 }
9291a0cd 2748
3876f04e 2749 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2750 {
2751 offset_type idx = 2 * iter;
2752 const char *name;
2753 offset_type *vec, vec_len, vec_idx;
2754
3876f04e 2755 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2756 continue;
2757
3876f04e 2758 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2759
2760 if (! (*name_matcher) (name, data))
2761 continue;
2762
2763 /* The name was matched, now expand corresponding CUs that were
2764 marked. */
4b5246aa 2765 vec = (offset_type *) (index->constant_pool
3876f04e 2766 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2767 vec_len = MAYBE_SWAP (vec[0]);
2768 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2769 {
e254ef6a 2770 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2771
e254ef6a 2772 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2773 if (file_matcher == NULL || per_cu->v.quick->mark)
e254ef6a 2774 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2775 }
2776 }
2777}
2778
2779static struct symtab *
2780dw2_find_pc_sect_symtab (struct objfile *objfile,
2781 struct minimal_symbol *msymbol,
2782 CORE_ADDR pc,
2783 struct obj_section *section,
2784 int warn_if_readin)
2785{
2786 struct dwarf2_per_cu_data *data;
2787
2788 dw2_setup (objfile);
2789
2790 if (!objfile->psymtabs_addrmap)
2791 return NULL;
2792
2793 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2794 if (!data)
2795 return NULL;
2796
2797 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2798 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2799 paddress (get_objfile_arch (objfile), pc));
2800
2801 return dw2_instantiate_symtab (objfile, data);
2802}
2803
9291a0cd 2804static void
44b13c5a 2805dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
9291a0cd
TT
2806 void *data)
2807{
2808 int i;
2809
2810 dw2_setup (objfile);
ae2de4f8 2811
1fd400ff
TT
2812 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2813 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2814 {
2815 int j;
e254ef6a 2816 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2817 struct quick_file_names *file_data;
9291a0cd 2818
e254ef6a 2819 if (per_cu->v.quick->symtab)
9291a0cd
TT
2820 continue;
2821
7b9f3c50
DE
2822 file_data = dw2_get_file_names (objfile, per_cu);
2823 if (file_data == NULL)
9291a0cd
TT
2824 continue;
2825
7b9f3c50 2826 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2827 {
7b9f3c50
DE
2828 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2829 j);
2830 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2831 }
2832 }
2833}
2834
2835static int
2836dw2_has_symbols (struct objfile *objfile)
2837{
2838 return 1;
2839}
2840
2841const struct quick_symbol_functions dwarf2_gdb_index_functions =
2842{
2843 dw2_has_symbols,
2844 dw2_find_last_source_symtab,
2845 dw2_forget_cached_source_info,
2846 dw2_lookup_symtab,
2847 dw2_lookup_symbol,
774b6a14 2848 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2849 dw2_print_stats,
2850 dw2_dump,
2851 dw2_relocate,
2852 dw2_expand_symtabs_for_function,
2853 dw2_expand_all_symtabs,
2854 dw2_expand_symtabs_with_filename,
2855 dw2_find_symbol_file,
40658b94 2856 dw2_map_matching_symbols,
9291a0cd
TT
2857 dw2_expand_symtabs_matching,
2858 dw2_find_pc_sect_symtab,
9291a0cd
TT
2859 dw2_map_symbol_filenames
2860};
2861
2862/* Initialize for reading DWARF for this objfile. Return 0 if this
2863 file will use psymtabs, or 1 if using the GNU index. */
2864
2865int
2866dwarf2_initialize_objfile (struct objfile *objfile)
2867{
2868 /* If we're about to read full symbols, don't bother with the
2869 indices. In this case we also don't care if some other debug
2870 format is making psymtabs, because they are all about to be
2871 expanded anyway. */
2872 if ((objfile->flags & OBJF_READNOW))
2873 {
2874 int i;
2875
2876 dwarf2_per_objfile->using_index = 1;
2877 create_all_comp_units (objfile);
1fd400ff 2878 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2879 dwarf2_per_objfile->quick_file_names_table =
2880 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2881
1fd400ff
TT
2882 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2883 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2884 {
e254ef6a 2885 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2886
e254ef6a
DE
2887 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2888 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2889 }
2890
2891 /* Return 1 so that gdb sees the "quick" functions. However,
2892 these functions will be no-ops because we will have expanded
2893 all symtabs. */
2894 return 1;
2895 }
2896
2897 if (dwarf2_read_index (objfile))
2898 return 1;
2899
9291a0cd
TT
2900 return 0;
2901}
2902
2903\f
2904
dce234bc
PP
2905/* Build a partial symbol table. */
2906
2907void
f29dff0a 2908dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2909{
f29dff0a 2910 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2911 {
2912 init_psymbol_list (objfile, 1024);
2913 }
2914
d146bf1e 2915 dwarf2_build_psymtabs_hard (objfile);
c906108c 2916}
c906108c 2917
45452591
DE
2918/* Return TRUE if OFFSET is within CU_HEADER. */
2919
2920static inline int
2921offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2922{
2923 unsigned int bottom = cu_header->offset;
2924 unsigned int top = (cu_header->offset
2925 + cu_header->length
2926 + cu_header->initial_length_size);
9a619af0 2927
45452591
DE
2928 return (offset >= bottom && offset < top);
2929}
2930
93311388
DE
2931/* Read in the comp unit header information from the debug_info at info_ptr.
2932 NOTE: This leaves members offset, first_die_offset to be filled in
2933 by the caller. */
107d2387 2934
fe1b8b76 2935static gdb_byte *
107d2387 2936read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2937 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2938{
2939 int signed_addr;
891d2f0b 2940 unsigned int bytes_read;
c764a876
DE
2941
2942 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2943 cu_header->initial_length_size = bytes_read;
2944 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2945 info_ptr += bytes_read;
107d2387
AC
2946 cu_header->version = read_2_bytes (abfd, info_ptr);
2947 info_ptr += 2;
613e1657 2948 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2949 &bytes_read);
613e1657 2950 info_ptr += bytes_read;
107d2387
AC
2951 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2952 info_ptr += 1;
2953 signed_addr = bfd_get_sign_extend_vma (abfd);
2954 if (signed_addr < 0)
8e65ff28 2955 internal_error (__FILE__, __LINE__,
e2e0b3e5 2956 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2957 cu_header->signed_addr_p = signed_addr;
c764a876 2958
107d2387
AC
2959 return info_ptr;
2960}
2961
fe1b8b76
JB
2962static gdb_byte *
2963partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2964 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2965 bfd *abfd)
2966{
fe1b8b76 2967 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2968
2969 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2970
2dc7f7b3 2971 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2972 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2973 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2974 bfd_get_filename (abfd));
72bf9492 2975
9e0ac564
TT
2976 if (header->abbrev_offset
2977 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2978 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
2979 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2980 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2981 (long) header->abbrev_offset,
93311388 2982 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2983 bfd_get_filename (abfd));
2984
2985 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2986 > buffer + buffer_size)
8a3fe4f8
AC
2987 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2988 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2989 (long) header->length,
93311388 2990 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2991 bfd_get_filename (abfd));
2992
2993 return info_ptr;
2994}
2995
348e048f
DE
2996/* Read in the types comp unit header information from .debug_types entry at
2997 types_ptr. The result is a pointer to one past the end of the header. */
2998
2999static gdb_byte *
3000read_type_comp_unit_head (struct comp_unit_head *cu_header,
8b70b953 3001 struct dwarf2_section_info *section,
348e048f
DE
3002 ULONGEST *signature,
3003 gdb_byte *types_ptr, bfd *abfd)
3004{
348e048f
DE
3005 gdb_byte *initial_types_ptr = types_ptr;
3006
8b70b953
TT
3007 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3008 cu_header->offset = types_ptr - section->buffer;
348e048f
DE
3009
3010 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3011
3012 *signature = read_8_bytes (abfd, types_ptr);
3013 types_ptr += 8;
3014 types_ptr += cu_header->offset_size;
3015 cu_header->first_die_offset = types_ptr - initial_types_ptr;
3016
3017 return types_ptr;
3018}
3019
aaa75496
JB
3020/* Allocate a new partial symtab for file named NAME and mark this new
3021 partial symtab as being an include of PST. */
3022
3023static void
3024dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3025 struct objfile *objfile)
3026{
3027 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3028
3029 subpst->section_offsets = pst->section_offsets;
3030 subpst->textlow = 0;
3031 subpst->texthigh = 0;
3032
3033 subpst->dependencies = (struct partial_symtab **)
3034 obstack_alloc (&objfile->objfile_obstack,
3035 sizeof (struct partial_symtab *));
3036 subpst->dependencies[0] = pst;
3037 subpst->number_of_dependencies = 1;
3038
3039 subpst->globals_offset = 0;
3040 subpst->n_global_syms = 0;
3041 subpst->statics_offset = 0;
3042 subpst->n_static_syms = 0;
3043 subpst->symtab = NULL;
3044 subpst->read_symtab = pst->read_symtab;
3045 subpst->readin = 0;
3046
3047 /* No private part is necessary for include psymtabs. This property
3048 can be used to differentiate between such include psymtabs and
10b3939b 3049 the regular ones. */
58a9656e 3050 subpst->read_symtab_private = NULL;
aaa75496
JB
3051}
3052
3053/* Read the Line Number Program data and extract the list of files
3054 included by the source file represented by PST. Build an include
d85a05f0 3055 partial symtab for each of these included files. */
aaa75496
JB
3056
3057static void
3058dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 3059 struct die_info *die,
aaa75496
JB
3060 struct partial_symtab *pst)
3061{
3062 struct objfile *objfile = cu->objfile;
3063 bfd *abfd = objfile->obfd;
d85a05f0
DJ
3064 struct line_header *lh = NULL;
3065 struct attribute *attr;
aaa75496 3066
d85a05f0
DJ
3067 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3068 if (attr)
3069 {
3070 unsigned int line_offset = DW_UNSND (attr);
9a619af0 3071
d85a05f0
DJ
3072 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3073 }
aaa75496
JB
3074 if (lh == NULL)
3075 return; /* No linetable, so no includes. */
3076
c6da4cef
DE
3077 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3078 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
3079
3080 free_line_header (lh);
3081}
3082
348e048f
DE
3083static hashval_t
3084hash_type_signature (const void *item)
3085{
3086 const struct signatured_type *type_sig = item;
9a619af0 3087
348e048f
DE
3088 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3089 return type_sig->signature;
3090}
3091
3092static int
3093eq_type_signature (const void *item_lhs, const void *item_rhs)
3094{
3095 const struct signatured_type *lhs = item_lhs;
3096 const struct signatured_type *rhs = item_rhs;
9a619af0 3097
348e048f
DE
3098 return lhs->signature == rhs->signature;
3099}
3100
1fd400ff
TT
3101/* Allocate a hash table for signatured types. */
3102
3103static htab_t
673bfd45 3104allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3105{
3106 return htab_create_alloc_ex (41,
3107 hash_type_signature,
3108 eq_type_signature,
3109 NULL,
3110 &objfile->objfile_obstack,
3111 hashtab_obstack_allocate,
3112 dummy_obstack_deallocate);
3113}
3114
3115/* A helper function to add a signatured type CU to a list. */
3116
3117static int
3118add_signatured_type_cu_to_list (void **slot, void *datum)
3119{
3120 struct signatured_type *sigt = *slot;
3121 struct dwarf2_per_cu_data ***datap = datum;
3122
3123 **datap = &sigt->per_cu;
3124 ++*datap;
3125
3126 return 1;
3127}
3128
348e048f
DE
3129/* Create the hash table of all entries in the .debug_types section.
3130 The result is zero if there is an error (e.g. missing .debug_types section),
3131 otherwise non-zero. */
3132
3133static int
3134create_debug_types_hash_table (struct objfile *objfile)
3135{
8b70b953 3136 htab_t types_htab = NULL;
1fd400ff 3137 struct dwarf2_per_cu_data **iter;
8b70b953
TT
3138 int ix;
3139 struct dwarf2_section_info *section;
348e048f 3140
8b70b953 3141 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
348e048f
DE
3142 {
3143 dwarf2_per_objfile->signatured_types = NULL;
3144 return 0;
3145 }
3146
8b70b953
TT
3147 for (ix = 0;
3148 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3149 ix, section);
3150 ++ix)
3151 {
3152 gdb_byte *info_ptr, *end_ptr;
348e048f 3153
8b70b953
TT
3154 dwarf2_read_section (objfile, section);
3155 info_ptr = section->buffer;
348e048f 3156
8b70b953
TT
3157 if (info_ptr == NULL)
3158 continue;
348e048f 3159
8b70b953
TT
3160 if (types_htab == NULL)
3161 types_htab = allocate_signatured_type_table (objfile);
348e048f 3162
8b70b953
TT
3163 if (dwarf2_die_debug)
3164 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3165
3166 end_ptr = info_ptr + section->size;
3167 while (info_ptr < end_ptr)
3168 {
3169 unsigned int offset;
3170 unsigned int offset_size;
3171 unsigned int type_offset;
3172 unsigned int length, initial_length_size;
3173 unsigned short version;
3174 ULONGEST signature;
3175 struct signatured_type *type_sig;
3176 void **slot;
3177 gdb_byte *ptr = info_ptr;
348e048f 3178
8b70b953 3179 offset = ptr - section->buffer;
348e048f 3180
8b70b953
TT
3181 /* We need to read the type's signature in order to build the hash
3182 table, but we don't need to read anything else just yet. */
348e048f 3183
8b70b953
TT
3184 /* Sanity check to ensure entire cu is present. */
3185 length = read_initial_length (objfile->obfd, ptr,
3186 &initial_length_size);
3187 if (ptr + length + initial_length_size > end_ptr)
3188 {
3189 complaint (&symfile_complaints,
3190 _("debug type entry runs off end "
3191 "of `.debug_types' section, ignored"));
3192 break;
3193 }
348e048f 3194
8b70b953
TT
3195 offset_size = initial_length_size == 4 ? 4 : 8;
3196 ptr += initial_length_size;
3197 version = bfd_get_16 (objfile->obfd, ptr);
3198 ptr += 2;
3199 ptr += offset_size; /* abbrev offset */
3200 ptr += 1; /* address size */
3201 signature = bfd_get_64 (objfile->obfd, ptr);
3202 ptr += 8;
3203 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3204
3205 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3206 memset (type_sig, 0, sizeof (*type_sig));
3207 type_sig->signature = signature;
3208 type_sig->type_offset = type_offset;
3209 type_sig->per_cu.objfile = objfile;
3210 type_sig->per_cu.debug_type_section = section;
3211 type_sig->per_cu.offset = offset;
3212
3213 slot = htab_find_slot (types_htab, type_sig, INSERT);
3214 gdb_assert (slot != NULL);
3215 if (*slot != NULL)
3216 {
3217 const struct signatured_type *dup_sig = *slot;
b3c8eb43 3218
8b70b953
TT
3219 complaint (&symfile_complaints,
3220 _("debug type entry at offset 0x%x is duplicate to the "
3221 "entry at offset 0x%x, signature 0x%s"),
3222 offset, dup_sig->per_cu.offset,
3223 phex (signature, sizeof (signature)));
3224 gdb_assert (signature == dup_sig->signature);
3225 }
3226 *slot = type_sig;
348e048f 3227
8b70b953
TT
3228 if (dwarf2_die_debug)
3229 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3230 offset, phex (signature, sizeof (signature)));
348e048f 3231
8b70b953
TT
3232 info_ptr = info_ptr + initial_length_size + length;
3233 }
348e048f
DE
3234 }
3235
3236 dwarf2_per_objfile->signatured_types = types_htab;
3237
1fd400ff
TT
3238 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3239 dwarf2_per_objfile->type_comp_units
3240 = obstack_alloc (&objfile->objfile_obstack,
3241 dwarf2_per_objfile->n_type_comp_units
3242 * sizeof (struct dwarf2_per_cu_data *));
3243 iter = &dwarf2_per_objfile->type_comp_units[0];
3244 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3245 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3246 == dwarf2_per_objfile->n_type_comp_units);
3247
348e048f
DE
3248 return 1;
3249}
3250
3251/* Lookup a signature based type.
3252 Returns NULL if SIG is not present in the table. */
3253
3254static struct signatured_type *
3255lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3256{
3257 struct signatured_type find_entry, *entry;
3258
3259 if (dwarf2_per_objfile->signatured_types == NULL)
3260 {
3261 complaint (&symfile_complaints,
55f1336d 3262 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3263 return 0;
3264 }
3265
3266 find_entry.signature = sig;
3267 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3268 return entry;
3269}
3270
d85a05f0
DJ
3271/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3272
3273static void
3274init_cu_die_reader (struct die_reader_specs *reader,
3275 struct dwarf2_cu *cu)
3276{
3277 reader->abfd = cu->objfile->obfd;
3278 reader->cu = cu;
8b70b953 3279 if (cu->per_cu->debug_type_section)
be391dca 3280 {
8b70b953
TT
3281 gdb_assert (cu->per_cu->debug_type_section->readin);
3282 reader->buffer = cu->per_cu->debug_type_section->buffer;
be391dca 3283 }
d85a05f0 3284 else
be391dca
TT
3285 {
3286 gdb_assert (dwarf2_per_objfile->info.readin);
3287 reader->buffer = dwarf2_per_objfile->info.buffer;
3288 }
d85a05f0
DJ
3289}
3290
3291/* Find the base address of the compilation unit for range lists and
3292 location lists. It will normally be specified by DW_AT_low_pc.
3293 In DWARF-3 draft 4, the base address could be overridden by
3294 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3295 compilation units with discontinuous ranges. */
3296
3297static void
3298dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3299{
3300 struct attribute *attr;
3301
3302 cu->base_known = 0;
3303 cu->base_address = 0;
3304
3305 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3306 if (attr)
3307 {
3308 cu->base_address = DW_ADDR (attr);
3309 cu->base_known = 1;
3310 }
3311 else
3312 {
3313 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3314 if (attr)
3315 {
3316 cu->base_address = DW_ADDR (attr);
3317 cu->base_known = 1;
3318 }
3319 }
3320}
3321
348e048f
DE
3322/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3323 to combine the common parts.
93311388 3324 Process a compilation unit for a psymtab.
348e048f
DE
3325 BUFFER is a pointer to the beginning of the dwarf section buffer,
3326 either .debug_info or debug_types.
93311388
DE
3327 INFO_PTR is a pointer to the start of the CU.
3328 Returns a pointer to the next CU. */
aaa75496 3329
93311388
DE
3330static gdb_byte *
3331process_psymtab_comp_unit (struct objfile *objfile,
3332 struct dwarf2_per_cu_data *this_cu,
3333 gdb_byte *buffer, gdb_byte *info_ptr,
3334 unsigned int buffer_size)
c906108c 3335{
c906108c 3336 bfd *abfd = objfile->obfd;
93311388 3337 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3338 struct die_info *comp_unit_die;
c906108c 3339 struct partial_symtab *pst;
5734ee8b 3340 CORE_ADDR baseaddr;
93311388
DE
3341 struct cleanup *back_to_inner;
3342 struct dwarf2_cu cu;
d85a05f0
DJ
3343 int has_children, has_pc_info;
3344 struct attribute *attr;
d85a05f0
DJ
3345 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3346 struct die_reader_specs reader_specs;
3e2a0cee 3347 const char *filename;
c906108c 3348
9816fde3 3349 init_one_comp_unit (&cu, objfile);
93311388 3350 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3351
93311388
DE
3352 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3353 buffer, buffer_size,
3354 abfd);
10b3939b 3355
93311388
DE
3356 /* Complete the cu_header. */
3357 cu.header.offset = beg_of_comp_unit - buffer;
3358 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3359
93311388 3360 cu.list_in_scope = &file_symbols;
af703f96 3361
328c9494
DJ
3362 /* If this compilation unit was already read in, free the
3363 cached copy in order to read it in again. This is
3364 necessary because we skipped some symbols when we first
3365 read in the compilation unit (see load_partial_dies).
3366 This problem could be avoided, but the benefit is
3367 unclear. */
3368 if (this_cu->cu != NULL)
3369 free_one_cached_comp_unit (this_cu->cu);
3370
3371 /* Note that this is a pointer to our stack frame, being
3372 added to a global data structure. It will be cleaned up
3373 in free_stack_comp_unit when we finish with this
3374 compilation unit. */
3375 this_cu->cu = &cu;
d85a05f0
DJ
3376 cu.per_cu = this_cu;
3377
93311388
DE
3378 /* Read the abbrevs for this compilation unit into a table. */
3379 dwarf2_read_abbrevs (abfd, &cu);
3380 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3381
93311388 3382 /* Read the compilation unit die. */
8b70b953 3383 if (this_cu->debug_type_section)
348e048f 3384 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3385 init_cu_die_reader (&reader_specs, &cu);
3386 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3387 &has_children);
93311388 3388
8b70b953 3389 if (this_cu->debug_type_section)
348e048f 3390 {
b3c8eb43
JK
3391 /* LENGTH has not been set yet for type units. */
3392 gdb_assert (this_cu->offset == cu.header.offset);
348e048f
DE
3393 this_cu->length = cu.header.length + cu.header.initial_length_size;
3394 }
d85a05f0 3395 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3396 {
93311388
DE
3397 info_ptr = (beg_of_comp_unit + cu.header.length
3398 + cu.header.initial_length_size);
3399 do_cleanups (back_to_inner);
3400 return info_ptr;
3401 }
72bf9492 3402
9816fde3 3403 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3404
93311388 3405 /* Allocate a new partial symbol table structure. */
d85a05f0 3406 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3407 if (attr == NULL || !DW_STRING (attr))
3408 filename = "";
3409 else
3410 filename = DW_STRING (attr);
93311388 3411 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3412 filename,
93311388
DE
3413 /* TEXTLOW and TEXTHIGH are set below. */
3414 0,
3415 objfile->global_psymbols.next,
3416 objfile->static_psymbols.next);
72bf9492 3417
d85a05f0
DJ
3418 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3419 if (attr != NULL)
3420 pst->dirname = DW_STRING (attr);
72bf9492 3421
e38df1d0 3422 pst->read_symtab_private = this_cu;
72bf9492 3423
93311388 3424 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3425
0963b4bd 3426 /* Store the function that reads in the rest of the symbol table. */
93311388 3427 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3428
9291a0cd 3429 this_cu->v.psymtab = pst;
c906108c 3430
d85a05f0
DJ
3431 dwarf2_find_base_address (comp_unit_die, &cu);
3432
93311388
DE
3433 /* Possibly set the default values of LOWPC and HIGHPC from
3434 `DW_AT_ranges'. */
d85a05f0
DJ
3435 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3436 &best_highpc, &cu, pst);
3437 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3438 /* Store the contiguous range if it is not empty; it can be empty for
3439 CUs with no code. */
3440 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3441 best_lowpc + baseaddr,
3442 best_highpc + baseaddr - 1, pst);
93311388
DE
3443
3444 /* Check if comp unit has_children.
3445 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3446 If not, there's no more debug_info for this comp unit. */
d85a05f0 3447 if (has_children)
93311388
DE
3448 {
3449 struct partial_die_info *first_die;
3450 CORE_ADDR lowpc, highpc;
31ffec48 3451
93311388
DE
3452 lowpc = ((CORE_ADDR) -1);
3453 highpc = ((CORE_ADDR) 0);
c906108c 3454
93311388 3455 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3456
93311388 3457 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3458 ! has_pc_info, &cu);
57c22c6c 3459
93311388
DE
3460 /* If we didn't find a lowpc, set it to highpc to avoid
3461 complaints from `maint check'. */
3462 if (lowpc == ((CORE_ADDR) -1))
3463 lowpc = highpc;
10b3939b 3464
93311388
DE
3465 /* If the compilation unit didn't have an explicit address range,
3466 then use the information extracted from its child dies. */
d85a05f0 3467 if (! has_pc_info)
93311388 3468 {
d85a05f0
DJ
3469 best_lowpc = lowpc;
3470 best_highpc = highpc;
93311388
DE
3471 }
3472 }
d85a05f0
DJ
3473 pst->textlow = best_lowpc + baseaddr;
3474 pst->texthigh = best_highpc + baseaddr;
c906108c 3475
93311388
DE
3476 pst->n_global_syms = objfile->global_psymbols.next -
3477 (objfile->global_psymbols.list + pst->globals_offset);
3478 pst->n_static_syms = objfile->static_psymbols.next -
3479 (objfile->static_psymbols.list + pst->statics_offset);
3480 sort_pst_symbols (pst);
c906108c 3481
93311388
DE
3482 info_ptr = (beg_of_comp_unit + cu.header.length
3483 + cu.header.initial_length_size);
ae038cb0 3484
8b70b953 3485 if (this_cu->debug_type_section)
348e048f
DE
3486 {
3487 /* It's not clear we want to do anything with stmt lists here.
3488 Waiting to see what gcc ultimately does. */
3489 }
d85a05f0 3490 else
93311388
DE
3491 {
3492 /* Get the list of files included in the current compilation unit,
3493 and build a psymtab for each of them. */
d85a05f0 3494 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3495 }
ae038cb0 3496
93311388 3497 do_cleanups (back_to_inner);
ae038cb0 3498
93311388
DE
3499 return info_ptr;
3500}
ff013f42 3501
348e048f
DE
3502/* Traversal function for htab_traverse_noresize.
3503 Process one .debug_types comp-unit. */
3504
3505static int
3506process_type_comp_unit (void **slot, void *info)
3507{
3508 struct signatured_type *entry = (struct signatured_type *) *slot;
3509 struct objfile *objfile = (struct objfile *) info;
3510 struct dwarf2_per_cu_data *this_cu;
3511
3512 this_cu = &entry->per_cu;
348e048f 3513
8b70b953 3514 gdb_assert (this_cu->debug_type_section->readin);
348e048f 3515 process_psymtab_comp_unit (objfile, this_cu,
8b70b953
TT
3516 this_cu->debug_type_section->buffer,
3517 (this_cu->debug_type_section->buffer
3518 + this_cu->offset),
3519 this_cu->debug_type_section->size);
348e048f
DE
3520
3521 return 1;
3522}
3523
3524/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3525 Build partial symbol tables for the .debug_types comp-units. */
3526
3527static void
3528build_type_psymtabs (struct objfile *objfile)
3529{
3530 if (! create_debug_types_hash_table (objfile))
3531 return;
3532
3533 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3534 process_type_comp_unit, objfile);
3535}
3536
60606b2c
TT
3537/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3538
3539static void
3540psymtabs_addrmap_cleanup (void *o)
3541{
3542 struct objfile *objfile = o;
ec61707d 3543
60606b2c
TT
3544 objfile->psymtabs_addrmap = NULL;
3545}
3546
93311388
DE
3547/* Build the partial symbol table by doing a quick pass through the
3548 .debug_info and .debug_abbrev sections. */
72bf9492 3549
93311388 3550static void
c67a9c90 3551dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3552{
93311388 3553 gdb_byte *info_ptr;
60606b2c
TT
3554 struct cleanup *back_to, *addrmap_cleanup;
3555 struct obstack temp_obstack;
93311388 3556
98bfdba5
PA
3557 dwarf2_per_objfile->reading_partial_symbols = 1;
3558
be391dca 3559 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3560 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3561
93311388
DE
3562 /* Any cached compilation units will be linked by the per-objfile
3563 read_in_chain. Make sure to free them when we're done. */
3564 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3565
348e048f
DE
3566 build_type_psymtabs (objfile);
3567
93311388 3568 create_all_comp_units (objfile);
c906108c 3569
60606b2c
TT
3570 /* Create a temporary address map on a temporary obstack. We later
3571 copy this to the final obstack. */
3572 obstack_init (&temp_obstack);
3573 make_cleanup_obstack_free (&temp_obstack);
3574 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3575 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3576
93311388
DE
3577 /* Since the objects we're extracting from .debug_info vary in
3578 length, only the individual functions to extract them (like
3579 read_comp_unit_head and load_partial_die) can really know whether
3580 the buffer is large enough to hold another complete object.
c906108c 3581
93311388
DE
3582 At the moment, they don't actually check that. If .debug_info
3583 holds just one extra byte after the last compilation unit's dies,
3584 then read_comp_unit_head will happily read off the end of the
3585 buffer. read_partial_die is similarly casual. Those functions
3586 should be fixed.
c906108c 3587
93311388
DE
3588 For this loop condition, simply checking whether there's any data
3589 left at all should be sufficient. */
c906108c 3590
93311388
DE
3591 while (info_ptr < (dwarf2_per_objfile->info.buffer
3592 + dwarf2_per_objfile->info.size))
3593 {
3594 struct dwarf2_per_cu_data *this_cu;
dd373385 3595
3e43a32a
MS
3596 this_cu = dwarf2_find_comp_unit (info_ptr
3597 - dwarf2_per_objfile->info.buffer,
93311388 3598 objfile);
aaa75496 3599
93311388
DE
3600 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3601 dwarf2_per_objfile->info.buffer,
3602 info_ptr,
3603 dwarf2_per_objfile->info.size);
c906108c 3604 }
ff013f42
JK
3605
3606 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3607 &objfile->objfile_obstack);
60606b2c 3608 discard_cleanups (addrmap_cleanup);
ff013f42 3609
ae038cb0
DJ
3610 do_cleanups (back_to);
3611}
3612
93311388 3613/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3614
3615static void
93311388
DE
3616load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3617 struct objfile *objfile)
ae038cb0
DJ
3618{
3619 bfd *abfd = objfile->obfd;
fe1b8b76 3620 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3621 struct die_info *comp_unit_die;
ae038cb0 3622 struct dwarf2_cu *cu;
1d9ec526 3623 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3624 int has_children;
3625 struct die_reader_specs reader_specs;
98bfdba5 3626 int read_cu = 0;
ae038cb0 3627
8b70b953 3628 gdb_assert (! this_cu->debug_type_section);
348e048f 3629
be391dca 3630 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3631 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3632 beg_of_comp_unit = info_ptr;
3633
98bfdba5
PA
3634 if (this_cu->cu == NULL)
3635 {
9816fde3
JK
3636 cu = xmalloc (sizeof (*cu));
3637 init_one_comp_unit (cu, objfile);
ae038cb0 3638
98bfdba5 3639 read_cu = 1;
ae038cb0 3640
98bfdba5
PA
3641 /* If an error occurs while loading, release our storage. */
3642 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3643
98bfdba5
PA
3644 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3645 dwarf2_per_objfile->info.buffer,
3646 dwarf2_per_objfile->info.size,
3647 abfd);
ae038cb0 3648
98bfdba5
PA
3649 /* Complete the cu_header. */
3650 cu->header.offset = this_cu->offset;
3651 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3652
3653 /* Link this compilation unit into the compilation unit tree. */
3654 this_cu->cu = cu;
3655 cu->per_cu = this_cu;
98bfdba5
PA
3656
3657 /* Link this CU into read_in_chain. */
3658 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3659 dwarf2_per_objfile->read_in_chain = this_cu;
3660 }
3661 else
3662 {
3663 cu = this_cu->cu;
3664 info_ptr += cu->header.first_die_offset;
3665 }
ae038cb0
DJ
3666
3667 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3668 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3669 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3670 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3671
3672 /* Read the compilation unit die. */
d85a05f0
DJ
3673 init_cu_die_reader (&reader_specs, cu);
3674 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3675 &has_children);
ae038cb0 3676
9816fde3 3677 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3678
ae038cb0
DJ
3679 /* Check if comp unit has_children.
3680 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3681 If not, there's no more debug_info for this comp unit. */
d85a05f0 3682 if (has_children)
93311388 3683 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3684
98bfdba5
PA
3685 do_cleanups (free_abbrevs_cleanup);
3686
3687 if (read_cu)
3688 {
3689 /* We've successfully allocated this compilation unit. Let our
3690 caller clean it up when finished with it. */
3691 discard_cleanups (free_cu_cleanup);
3692 }
ae038cb0
DJ
3693}
3694
3695/* Create a list of all compilation units in OBJFILE. We do this only
3696 if an inter-comp-unit reference is found; presumably if there is one,
3697 there will be many, and one will occur early in the .debug_info section.
3698 So there's no point in building this list incrementally. */
3699
3700static void
3701create_all_comp_units (struct objfile *objfile)
3702{
3703 int n_allocated;
3704 int n_comp_units;
3705 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3706 gdb_byte *info_ptr;
3707
3708 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3709 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3710
3711 n_comp_units = 0;
3712 n_allocated = 10;
3713 all_comp_units = xmalloc (n_allocated
3714 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3715
3e43a32a
MS
3716 while (info_ptr < dwarf2_per_objfile->info.buffer
3717 + dwarf2_per_objfile->info.size)
ae038cb0 3718 {
c764a876 3719 unsigned int length, initial_length_size;
ae038cb0 3720 struct dwarf2_per_cu_data *this_cu;
c764a876 3721 unsigned int offset;
ae038cb0 3722
dce234bc 3723 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3724
3725 /* Read just enough information to find out where the next
3726 compilation unit is. */
c764a876
DE
3727 length = read_initial_length (objfile->obfd, info_ptr,
3728 &initial_length_size);
ae038cb0
DJ
3729
3730 /* Save the compilation unit for later lookup. */
3731 this_cu = obstack_alloc (&objfile->objfile_obstack,
3732 sizeof (struct dwarf2_per_cu_data));
3733 memset (this_cu, 0, sizeof (*this_cu));
3734 this_cu->offset = offset;
c764a876 3735 this_cu->length = length + initial_length_size;
9291a0cd 3736 this_cu->objfile = objfile;
ae038cb0
DJ
3737
3738 if (n_comp_units == n_allocated)
3739 {
3740 n_allocated *= 2;
3741 all_comp_units = xrealloc (all_comp_units,
3742 n_allocated
3743 * sizeof (struct dwarf2_per_cu_data *));
3744 }
3745 all_comp_units[n_comp_units++] = this_cu;
3746
3747 info_ptr = info_ptr + this_cu->length;
3748 }
3749
3750 dwarf2_per_objfile->all_comp_units
3751 = obstack_alloc (&objfile->objfile_obstack,
3752 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3753 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3754 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3755 xfree (all_comp_units);
3756 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3757}
3758
5734ee8b
DJ
3759/* Process all loaded DIEs for compilation unit CU, starting at
3760 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3761 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3762 DW_AT_ranges). If NEED_PC is set, then this function will set
3763 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3764 and record the covered ranges in the addrmap. */
c906108c 3765
72bf9492
DJ
3766static void
3767scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3768 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3769{
72bf9492 3770 struct partial_die_info *pdi;
c906108c 3771
91c24f0a
DC
3772 /* Now, march along the PDI's, descending into ones which have
3773 interesting children but skipping the children of the other ones,
3774 until we reach the end of the compilation unit. */
c906108c 3775
72bf9492 3776 pdi = first_die;
91c24f0a 3777
72bf9492
DJ
3778 while (pdi != NULL)
3779 {
3780 fixup_partial_die (pdi, cu);
c906108c 3781
f55ee35c 3782 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3783 children, so we need to look at them. Ditto for anonymous
3784 enums. */
933c6fe4 3785
72bf9492 3786 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3787 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3788 {
72bf9492 3789 switch (pdi->tag)
c906108c
SS
3790 {
3791 case DW_TAG_subprogram:
5734ee8b 3792 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3793 break;
72929c62 3794 case DW_TAG_constant:
c906108c
SS
3795 case DW_TAG_variable:
3796 case DW_TAG_typedef:
91c24f0a 3797 case DW_TAG_union_type:
72bf9492 3798 if (!pdi->is_declaration)
63d06c5c 3799 {
72bf9492 3800 add_partial_symbol (pdi, cu);
63d06c5c
DC
3801 }
3802 break;
c906108c 3803 case DW_TAG_class_type:
680b30c7 3804 case DW_TAG_interface_type:
c906108c 3805 case DW_TAG_structure_type:
72bf9492 3806 if (!pdi->is_declaration)
c906108c 3807 {
72bf9492 3808 add_partial_symbol (pdi, cu);
c906108c
SS
3809 }
3810 break;
91c24f0a 3811 case DW_TAG_enumeration_type:
72bf9492
DJ
3812 if (!pdi->is_declaration)
3813 add_partial_enumeration (pdi, cu);
c906108c
SS
3814 break;
3815 case DW_TAG_base_type:
a02abb62 3816 case DW_TAG_subrange_type:
c906108c 3817 /* File scope base type definitions are added to the partial
c5aa993b 3818 symbol table. */
72bf9492 3819 add_partial_symbol (pdi, cu);
c906108c 3820 break;
d9fa45fe 3821 case DW_TAG_namespace:
5734ee8b 3822 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3823 break;
5d7cb8df
JK
3824 case DW_TAG_module:
3825 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3826 break;
c906108c
SS
3827 default:
3828 break;
3829 }
3830 }
3831
72bf9492
DJ
3832 /* If the die has a sibling, skip to the sibling. */
3833
3834 pdi = pdi->die_sibling;
3835 }
3836}
3837
3838/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3839
72bf9492 3840 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3841 name is concatenated with "::" and the partial DIE's name. For
3842 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3843 Enumerators are an exception; they use the scope of their parent
3844 enumeration type, i.e. the name of the enumeration type is not
3845 prepended to the enumerator.
91c24f0a 3846
72bf9492
DJ
3847 There are two complexities. One is DW_AT_specification; in this
3848 case "parent" means the parent of the target of the specification,
3849 instead of the direct parent of the DIE. The other is compilers
3850 which do not emit DW_TAG_namespace; in this case we try to guess
3851 the fully qualified name of structure types from their members'
3852 linkage names. This must be done using the DIE's children rather
3853 than the children of any DW_AT_specification target. We only need
3854 to do this for structures at the top level, i.e. if the target of
3855 any DW_AT_specification (if any; otherwise the DIE itself) does not
3856 have a parent. */
3857
3858/* Compute the scope prefix associated with PDI's parent, in
3859 compilation unit CU. The result will be allocated on CU's
3860 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3861 field. NULL is returned if no prefix is necessary. */
3862static char *
3863partial_die_parent_scope (struct partial_die_info *pdi,
3864 struct dwarf2_cu *cu)
3865{
3866 char *grandparent_scope;
3867 struct partial_die_info *parent, *real_pdi;
91c24f0a 3868
72bf9492
DJ
3869 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3870 then this means the parent of the specification DIE. */
3871
3872 real_pdi = pdi;
72bf9492 3873 while (real_pdi->has_specification)
10b3939b 3874 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3875
3876 parent = real_pdi->die_parent;
3877 if (parent == NULL)
3878 return NULL;
3879
3880 if (parent->scope_set)
3881 return parent->scope;
3882
3883 fixup_partial_die (parent, cu);
3884
10b3939b 3885 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3886
acebe513
UW
3887 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3888 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3889 Work around this problem here. */
3890 if (cu->language == language_cplus
6e70227d 3891 && parent->tag == DW_TAG_namespace
acebe513
UW
3892 && strcmp (parent->name, "::") == 0
3893 && grandparent_scope == NULL)
3894 {
3895 parent->scope = NULL;
3896 parent->scope_set = 1;
3897 return NULL;
3898 }
3899
9c6c53f7
SA
3900 if (pdi->tag == DW_TAG_enumerator)
3901 /* Enumerators should not get the name of the enumeration as a prefix. */
3902 parent->scope = grandparent_scope;
3903 else if (parent->tag == DW_TAG_namespace
f55ee35c 3904 || parent->tag == DW_TAG_module
72bf9492
DJ
3905 || parent->tag == DW_TAG_structure_type
3906 || parent->tag == DW_TAG_class_type
680b30c7 3907 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3908 || parent->tag == DW_TAG_union_type
3909 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3910 {
3911 if (grandparent_scope == NULL)
3912 parent->scope = parent->name;
3913 else
3e43a32a
MS
3914 parent->scope = typename_concat (&cu->comp_unit_obstack,
3915 grandparent_scope,
f55ee35c 3916 parent->name, 0, cu);
72bf9492 3917 }
72bf9492
DJ
3918 else
3919 {
3920 /* FIXME drow/2004-04-01: What should we be doing with
3921 function-local names? For partial symbols, we should probably be
3922 ignoring them. */
3923 complaint (&symfile_complaints,
e2e0b3e5 3924 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3925 parent->tag, pdi->offset);
3926 parent->scope = grandparent_scope;
c906108c
SS
3927 }
3928
72bf9492
DJ
3929 parent->scope_set = 1;
3930 return parent->scope;
3931}
3932
3933/* Return the fully scoped name associated with PDI, from compilation unit
3934 CU. The result will be allocated with malloc. */
3935static char *
3936partial_die_full_name (struct partial_die_info *pdi,
3937 struct dwarf2_cu *cu)
3938{
3939 char *parent_scope;
3940
98bfdba5
PA
3941 /* If this is a template instantiation, we can not work out the
3942 template arguments from partial DIEs. So, unfortunately, we have
3943 to go through the full DIEs. At least any work we do building
3944 types here will be reused if full symbols are loaded later. */
3945 if (pdi->has_template_arguments)
3946 {
3947 fixup_partial_die (pdi, cu);
3948
3949 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3950 {
3951 struct die_info *die;
3952 struct attribute attr;
3953 struct dwarf2_cu *ref_cu = cu;
3954
3955 attr.name = 0;
3956 attr.form = DW_FORM_ref_addr;
3957 attr.u.addr = pdi->offset;
3958 die = follow_die_ref (NULL, &attr, &ref_cu);
3959
3960 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3961 }
3962 }
3963
72bf9492
DJ
3964 parent_scope = partial_die_parent_scope (pdi, cu);
3965 if (parent_scope == NULL)
3966 return NULL;
3967 else
f55ee35c 3968 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3969}
3970
3971static void
72bf9492 3972add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3973{
e7c27a73 3974 struct objfile *objfile = cu->objfile;
c906108c 3975 CORE_ADDR addr = 0;
decbce07 3976 char *actual_name = NULL;
5c4e30ca 3977 const struct partial_symbol *psym = NULL;
e142c38c 3978 CORE_ADDR baseaddr;
72bf9492 3979 int built_actual_name = 0;
e142c38c
DJ
3980
3981 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3982
94af9270
KS
3983 actual_name = partial_die_full_name (pdi, cu);
3984 if (actual_name)
3985 built_actual_name = 1;
63d06c5c 3986
72bf9492
DJ
3987 if (actual_name == NULL)
3988 actual_name = pdi->name;
3989
c906108c
SS
3990 switch (pdi->tag)
3991 {
3992 case DW_TAG_subprogram:
2cfa0c8d 3993 if (pdi->is_external || cu->language == language_ada)
c906108c 3994 {
2cfa0c8d
JB
3995 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3996 of the global scope. But in Ada, we want to be able to access
3997 nested procedures globally. So all Ada subprograms are stored
3998 in the global scope. */
f47fb265 3999 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4000 mst_text, objfile); */
f47fb265
MS
4001 add_psymbol_to_list (actual_name, strlen (actual_name),
4002 built_actual_name,
4003 VAR_DOMAIN, LOC_BLOCK,
4004 &objfile->global_psymbols,
4005 0, pdi->lowpc + baseaddr,
4006 cu->language, objfile);
c906108c
SS
4007 }
4008 else
4009 {
f47fb265 4010 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4011 mst_file_text, objfile); */
f47fb265
MS
4012 add_psymbol_to_list (actual_name, strlen (actual_name),
4013 built_actual_name,
4014 VAR_DOMAIN, LOC_BLOCK,
4015 &objfile->static_psymbols,
4016 0, pdi->lowpc + baseaddr,
4017 cu->language, objfile);
c906108c
SS
4018 }
4019 break;
72929c62
JB
4020 case DW_TAG_constant:
4021 {
4022 struct psymbol_allocation_list *list;
4023
4024 if (pdi->is_external)
4025 list = &objfile->global_psymbols;
4026 else
4027 list = &objfile->static_psymbols;
f47fb265
MS
4028 add_psymbol_to_list (actual_name, strlen (actual_name),
4029 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4030 list, 0, 0, cu->language, objfile);
72929c62
JB
4031 }
4032 break;
c906108c 4033 case DW_TAG_variable:
caac4577
JG
4034 if (pdi->locdesc)
4035 addr = decode_locdesc (pdi->locdesc, cu);
4036
4037 if (pdi->locdesc
4038 && addr == 0
4039 && !dwarf2_per_objfile->has_section_at_zero)
4040 {
4041 /* A global or static variable may also have been stripped
4042 out by the linker if unused, in which case its address
4043 will be nullified; do not add such variables into partial
4044 symbol table then. */
4045 }
4046 else if (pdi->is_external)
c906108c
SS
4047 {
4048 /* Global Variable.
4049 Don't enter into the minimal symbol tables as there is
4050 a minimal symbol table entry from the ELF symbols already.
4051 Enter into partial symbol table if it has a location
4052 descriptor or a type.
4053 If the location descriptor is missing, new_symbol will create
4054 a LOC_UNRESOLVED symbol, the address of the variable will then
4055 be determined from the minimal symbol table whenever the variable
4056 is referenced.
4057 The address for the partial symbol table entry is not
4058 used by GDB, but it comes in handy for debugging partial symbol
4059 table building. */
4060
c906108c 4061 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4062 add_psymbol_to_list (actual_name, strlen (actual_name),
4063 built_actual_name,
4064 VAR_DOMAIN, LOC_STATIC,
4065 &objfile->global_psymbols,
4066 0, addr + baseaddr,
4067 cu->language, objfile);
c906108c
SS
4068 }
4069 else
4070 {
0963b4bd 4071 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4072 if (pdi->locdesc == NULL)
decbce07
MS
4073 {
4074 if (built_actual_name)
4075 xfree (actual_name);
4076 return;
4077 }
f47fb265 4078 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4079 mst_file_data, objfile); */
f47fb265
MS
4080 add_psymbol_to_list (actual_name, strlen (actual_name),
4081 built_actual_name,
4082 VAR_DOMAIN, LOC_STATIC,
4083 &objfile->static_psymbols,
4084 0, addr + baseaddr,
4085 cu->language, objfile);
c906108c
SS
4086 }
4087 break;
4088 case DW_TAG_typedef:
4089 case DW_TAG_base_type:
a02abb62 4090 case DW_TAG_subrange_type:
38d518c9 4091 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4092 built_actual_name,
176620f1 4093 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4094 &objfile->static_psymbols,
e142c38c 4095 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4096 break;
72bf9492
DJ
4097 case DW_TAG_namespace:
4098 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4099 built_actual_name,
72bf9492
DJ
4100 VAR_DOMAIN, LOC_TYPEDEF,
4101 &objfile->global_psymbols,
4102 0, (CORE_ADDR) 0, cu->language, objfile);
4103 break;
c906108c 4104 case DW_TAG_class_type:
680b30c7 4105 case DW_TAG_interface_type:
c906108c
SS
4106 case DW_TAG_structure_type:
4107 case DW_TAG_union_type:
4108 case DW_TAG_enumeration_type:
fa4028e9
JB
4109 /* Skip external references. The DWARF standard says in the section
4110 about "Structure, Union, and Class Type Entries": "An incomplete
4111 structure, union or class type is represented by a structure,
4112 union or class entry that does not have a byte size attribute
4113 and that has a DW_AT_declaration attribute." */
4114 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4115 {
4116 if (built_actual_name)
4117 xfree (actual_name);
4118 return;
4119 }
fa4028e9 4120
63d06c5c
DC
4121 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4122 static vs. global. */
38d518c9 4123 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4124 built_actual_name,
176620f1 4125 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4126 (cu->language == language_cplus
4127 || cu->language == language_java)
63d06c5c
DC
4128 ? &objfile->global_psymbols
4129 : &objfile->static_psymbols,
e142c38c 4130 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4131
c906108c
SS
4132 break;
4133 case DW_TAG_enumerator:
38d518c9 4134 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4135 built_actual_name,
176620f1 4136 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4137 (cu->language == language_cplus
4138 || cu->language == language_java)
f6fe98ef
DJ
4139 ? &objfile->global_psymbols
4140 : &objfile->static_psymbols,
e142c38c 4141 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4142 break;
4143 default:
4144 break;
4145 }
5c4e30ca 4146
72bf9492
DJ
4147 if (built_actual_name)
4148 xfree (actual_name);
c906108c
SS
4149}
4150
5c4e30ca
DC
4151/* Read a partial die corresponding to a namespace; also, add a symbol
4152 corresponding to that namespace to the symbol table. NAMESPACE is
4153 the name of the enclosing namespace. */
91c24f0a 4154
72bf9492
DJ
4155static void
4156add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4157 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4158 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4159{
72bf9492 4160 /* Add a symbol for the namespace. */
e7c27a73 4161
72bf9492 4162 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4163
4164 /* Now scan partial symbols in that namespace. */
4165
91c24f0a 4166 if (pdi->has_children)
5734ee8b 4167 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4168}
4169
5d7cb8df
JK
4170/* Read a partial die corresponding to a Fortran module. */
4171
4172static void
4173add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4174 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4175{
f55ee35c 4176 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4177
4178 if (pdi->has_children)
4179 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4180}
4181
bc30ff58
JB
4182/* Read a partial die corresponding to a subprogram and create a partial
4183 symbol for that subprogram. When the CU language allows it, this
4184 routine also defines a partial symbol for each nested subprogram
4185 that this subprogram contains.
6e70227d 4186
bc30ff58
JB
4187 DIE my also be a lexical block, in which case we simply search
4188 recursively for suprograms defined inside that lexical block.
4189 Again, this is only performed when the CU language allows this
4190 type of definitions. */
4191
4192static void
4193add_partial_subprogram (struct partial_die_info *pdi,
4194 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4195 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4196{
4197 if (pdi->tag == DW_TAG_subprogram)
4198 {
4199 if (pdi->has_pc_info)
4200 {
4201 if (pdi->lowpc < *lowpc)
4202 *lowpc = pdi->lowpc;
4203 if (pdi->highpc > *highpc)
4204 *highpc = pdi->highpc;
5734ee8b
DJ
4205 if (need_pc)
4206 {
4207 CORE_ADDR baseaddr;
4208 struct objfile *objfile = cu->objfile;
4209
4210 baseaddr = ANOFFSET (objfile->section_offsets,
4211 SECT_OFF_TEXT (objfile));
4212 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4213 pdi->lowpc + baseaddr,
4214 pdi->highpc - 1 + baseaddr,
9291a0cd 4215 cu->per_cu->v.psymtab);
5734ee8b 4216 }
bc30ff58 4217 if (!pdi->is_declaration)
e8d05480
JB
4218 /* Ignore subprogram DIEs that do not have a name, they are
4219 illegal. Do not emit a complaint at this point, we will
4220 do so when we convert this psymtab into a symtab. */
4221 if (pdi->name)
4222 add_partial_symbol (pdi, cu);
bc30ff58
JB
4223 }
4224 }
6e70227d 4225
bc30ff58
JB
4226 if (! pdi->has_children)
4227 return;
4228
4229 if (cu->language == language_ada)
4230 {
4231 pdi = pdi->die_child;
4232 while (pdi != NULL)
4233 {
4234 fixup_partial_die (pdi, cu);
4235 if (pdi->tag == DW_TAG_subprogram
4236 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4237 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4238 pdi = pdi->die_sibling;
4239 }
4240 }
4241}
4242
91c24f0a
DC
4243/* Read a partial die corresponding to an enumeration type. */
4244
72bf9492
DJ
4245static void
4246add_partial_enumeration (struct partial_die_info *enum_pdi,
4247 struct dwarf2_cu *cu)
91c24f0a 4248{
72bf9492 4249 struct partial_die_info *pdi;
91c24f0a
DC
4250
4251 if (enum_pdi->name != NULL)
72bf9492
DJ
4252 add_partial_symbol (enum_pdi, cu);
4253
4254 pdi = enum_pdi->die_child;
4255 while (pdi)
91c24f0a 4256 {
72bf9492 4257 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4258 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4259 else
72bf9492
DJ
4260 add_partial_symbol (pdi, cu);
4261 pdi = pdi->die_sibling;
91c24f0a 4262 }
91c24f0a
DC
4263}
4264
4bb7a0a7
DJ
4265/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4266 Return the corresponding abbrev, or NULL if the number is zero (indicating
4267 an empty DIE). In either case *BYTES_READ will be set to the length of
4268 the initial number. */
4269
4270static struct abbrev_info *
fe1b8b76 4271peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4272 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4273{
4274 bfd *abfd = cu->objfile->obfd;
4275 unsigned int abbrev_number;
4276 struct abbrev_info *abbrev;
4277
4278 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4279
4280 if (abbrev_number == 0)
4281 return NULL;
4282
4283 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4284 if (!abbrev)
4285 {
3e43a32a
MS
4286 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4287 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4288 }
4289
4290 return abbrev;
4291}
4292
93311388
DE
4293/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4294 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4295 DIE. Any children of the skipped DIEs will also be skipped. */
4296
fe1b8b76 4297static gdb_byte *
93311388 4298skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4299{
4300 struct abbrev_info *abbrev;
4301 unsigned int bytes_read;
4302
4303 while (1)
4304 {
4305 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4306 if (abbrev == NULL)
4307 return info_ptr + bytes_read;
4308 else
93311388 4309 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4310 }
4311}
4312
93311388
DE
4313/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4314 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4315 abbrev corresponding to that skipped uleb128 should be passed in
4316 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4317 children. */
4318
fe1b8b76 4319static gdb_byte *
93311388
DE
4320skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4321 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4322{
4323 unsigned int bytes_read;
4324 struct attribute attr;
4325 bfd *abfd = cu->objfile->obfd;
4326 unsigned int form, i;
4327
4328 for (i = 0; i < abbrev->num_attrs; i++)
4329 {
4330 /* The only abbrev we care about is DW_AT_sibling. */
4331 if (abbrev->attrs[i].name == DW_AT_sibling)
4332 {
4333 read_attribute (&attr, &abbrev->attrs[i],
4334 abfd, info_ptr, cu);
4335 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4336 complaint (&symfile_complaints,
4337 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4338 else
93311388 4339 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4340 }
4341
4342 /* If it isn't DW_AT_sibling, skip this attribute. */
4343 form = abbrev->attrs[i].form;
4344 skip_attribute:
4345 switch (form)
4346 {
4bb7a0a7 4347 case DW_FORM_ref_addr:
ae411497
TT
4348 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4349 and later it is offset sized. */
4350 if (cu->header.version == 2)
4351 info_ptr += cu->header.addr_size;
4352 else
4353 info_ptr += cu->header.offset_size;
4354 break;
4355 case DW_FORM_addr:
4bb7a0a7
DJ
4356 info_ptr += cu->header.addr_size;
4357 break;
4358 case DW_FORM_data1:
4359 case DW_FORM_ref1:
4360 case DW_FORM_flag:
4361 info_ptr += 1;
4362 break;
2dc7f7b3
TT
4363 case DW_FORM_flag_present:
4364 break;
4bb7a0a7
DJ
4365 case DW_FORM_data2:
4366 case DW_FORM_ref2:
4367 info_ptr += 2;
4368 break;
4369 case DW_FORM_data4:
4370 case DW_FORM_ref4:
4371 info_ptr += 4;
4372 break;
4373 case DW_FORM_data8:
4374 case DW_FORM_ref8:
55f1336d 4375 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4376 info_ptr += 8;
4377 break;
4378 case DW_FORM_string:
9b1c24c8 4379 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4380 info_ptr += bytes_read;
4381 break;
2dc7f7b3 4382 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4383 case DW_FORM_strp:
4384 info_ptr += cu->header.offset_size;
4385 break;
2dc7f7b3 4386 case DW_FORM_exprloc:
4bb7a0a7
DJ
4387 case DW_FORM_block:
4388 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4389 info_ptr += bytes_read;
4390 break;
4391 case DW_FORM_block1:
4392 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4393 break;
4394 case DW_FORM_block2:
4395 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4396 break;
4397 case DW_FORM_block4:
4398 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4399 break;
4400 case DW_FORM_sdata:
4401 case DW_FORM_udata:
4402 case DW_FORM_ref_udata:
4403 info_ptr = skip_leb128 (abfd, info_ptr);
4404 break;
4405 case DW_FORM_indirect:
4406 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4407 info_ptr += bytes_read;
4408 /* We need to continue parsing from here, so just go back to
4409 the top. */
4410 goto skip_attribute;
4411
4412 default:
3e43a32a
MS
4413 error (_("Dwarf Error: Cannot handle %s "
4414 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4415 dwarf_form_name (form),
4416 bfd_get_filename (abfd));
4417 }
4418 }
4419
4420 if (abbrev->has_children)
93311388 4421 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4422 else
4423 return info_ptr;
4424}
4425
93311388
DE
4426/* Locate ORIG_PDI's sibling.
4427 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4428 in BUFFER. */
91c24f0a 4429
fe1b8b76 4430static gdb_byte *
93311388
DE
4431locate_pdi_sibling (struct partial_die_info *orig_pdi,
4432 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4433 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4434{
4435 /* Do we know the sibling already? */
72bf9492 4436
91c24f0a
DC
4437 if (orig_pdi->sibling)
4438 return orig_pdi->sibling;
4439
4440 /* Are there any children to deal with? */
4441
4442 if (!orig_pdi->has_children)
4443 return info_ptr;
4444
4bb7a0a7 4445 /* Skip the children the long way. */
91c24f0a 4446
93311388 4447 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4448}
4449
c906108c
SS
4450/* Expand this partial symbol table into a full symbol table. */
4451
4452static void
fba45db2 4453dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4454{
c906108c
SS
4455 if (pst != NULL)
4456 {
4457 if (pst->readin)
4458 {
3e43a32a
MS
4459 warning (_("bug: psymtab for %s is already read in."),
4460 pst->filename);
c906108c
SS
4461 }
4462 else
4463 {
4464 if (info_verbose)
4465 {
3e43a32a
MS
4466 printf_filtered (_("Reading in symbols for %s..."),
4467 pst->filename);
c906108c
SS
4468 gdb_flush (gdb_stdout);
4469 }
4470
10b3939b
DJ
4471 /* Restore our global data. */
4472 dwarf2_per_objfile = objfile_data (pst->objfile,
4473 dwarf2_objfile_data_key);
4474
b2ab525c
KB
4475 /* If this psymtab is constructed from a debug-only objfile, the
4476 has_section_at_zero flag will not necessarily be correct. We
4477 can get the correct value for this flag by looking at the data
4478 associated with the (presumably stripped) associated objfile. */
4479 if (pst->objfile->separate_debug_objfile_backlink)
4480 {
4481 struct dwarf2_per_objfile *dpo_backlink
4482 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4483 dwarf2_objfile_data_key);
9a619af0 4484
b2ab525c
KB
4485 dwarf2_per_objfile->has_section_at_zero
4486 = dpo_backlink->has_section_at_zero;
4487 }
4488
98bfdba5
PA
4489 dwarf2_per_objfile->reading_partial_symbols = 0;
4490
c906108c
SS
4491 psymtab_to_symtab_1 (pst);
4492
4493 /* Finish up the debug error message. */
4494 if (info_verbose)
a3f17187 4495 printf_filtered (_("done.\n"));
c906108c
SS
4496 }
4497 }
4498}
4499
10b3939b
DJ
4500/* Add PER_CU to the queue. */
4501
4502static void
03dd20cc 4503queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4504{
4505 struct dwarf2_queue_item *item;
4506
4507 per_cu->queued = 1;
4508 item = xmalloc (sizeof (*item));
4509 item->per_cu = per_cu;
4510 item->next = NULL;
4511
4512 if (dwarf2_queue == NULL)
4513 dwarf2_queue = item;
4514 else
4515 dwarf2_queue_tail->next = item;
4516
4517 dwarf2_queue_tail = item;
4518}
4519
4520/* Process the queue. */
4521
4522static void
4523process_queue (struct objfile *objfile)
4524{
4525 struct dwarf2_queue_item *item, *next_item;
4526
03dd20cc
DJ
4527 /* The queue starts out with one item, but following a DIE reference
4528 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4529 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4530 {
9291a0cd
TT
4531 if (dwarf2_per_objfile->using_index
4532 ? !item->per_cu->v.quick->symtab
4533 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4534 process_full_comp_unit (item->per_cu);
4535
4536 item->per_cu->queued = 0;
4537 next_item = item->next;
4538 xfree (item);
4539 }
4540
4541 dwarf2_queue_tail = NULL;
4542}
4543
4544/* Free all allocated queue entries. This function only releases anything if
4545 an error was thrown; if the queue was processed then it would have been
4546 freed as we went along. */
4547
4548static void
4549dwarf2_release_queue (void *dummy)
4550{
4551 struct dwarf2_queue_item *item, *last;
4552
4553 item = dwarf2_queue;
4554 while (item)
4555 {
4556 /* Anything still marked queued is likely to be in an
4557 inconsistent state, so discard it. */
4558 if (item->per_cu->queued)
4559 {
4560 if (item->per_cu->cu != NULL)
4561 free_one_cached_comp_unit (item->per_cu->cu);
4562 item->per_cu->queued = 0;
4563 }
4564
4565 last = item;
4566 item = item->next;
4567 xfree (last);
4568 }
4569
4570 dwarf2_queue = dwarf2_queue_tail = NULL;
4571}
4572
4573/* Read in full symbols for PST, and anything it depends on. */
4574
c906108c 4575static void
fba45db2 4576psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4577{
10b3939b 4578 struct dwarf2_per_cu_data *per_cu;
c906108c 4579 struct cleanup *back_to;
aaa75496
JB
4580 int i;
4581
4582 for (i = 0; i < pst->number_of_dependencies; i++)
4583 if (!pst->dependencies[i]->readin)
4584 {
4585 /* Inform about additional files that need to be read in. */
4586 if (info_verbose)
4587 {
a3f17187 4588 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4589 fputs_filtered (" ", gdb_stdout);
4590 wrap_here ("");
4591 fputs_filtered ("and ", gdb_stdout);
4592 wrap_here ("");
4593 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4594 wrap_here (""); /* Flush output. */
aaa75496
JB
4595 gdb_flush (gdb_stdout);
4596 }
4597 psymtab_to_symtab_1 (pst->dependencies[i]);
4598 }
4599
e38df1d0 4600 per_cu = pst->read_symtab_private;
10b3939b
DJ
4601
4602 if (per_cu == NULL)
aaa75496
JB
4603 {
4604 /* It's an include file, no symbols to read for it.
4605 Everything is in the parent symtab. */
4606 pst->readin = 1;
4607 return;
4608 }
c906108c 4609
9291a0cd 4610 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4611}
4612
93311388 4613/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4614
93311388 4615static void
3e43a32a
MS
4616load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4617 struct objfile *objfile)
10b3939b 4618{
31ffec48 4619 bfd *abfd = objfile->obfd;
10b3939b 4620 struct dwarf2_cu *cu;
c764a876 4621 unsigned int offset;
93311388 4622 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4623 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4624 struct attribute *attr;
98bfdba5 4625 int read_cu = 0;
6502dd73 4626
8b70b953 4627 gdb_assert (! per_cu->debug_type_section);
348e048f 4628
c906108c 4629 /* Set local variables from the partial symbol table info. */
10b3939b 4630 offset = per_cu->offset;
6502dd73 4631
be391dca 4632 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4633 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4634 beg_of_comp_unit = info_ptr;
63d06c5c 4635
98bfdba5
PA
4636 if (per_cu->cu == NULL)
4637 {
9816fde3
JK
4638 cu = xmalloc (sizeof (*cu));
4639 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4640
4641 read_cu = 1;
c906108c 4642
98bfdba5
PA
4643 /* If an error occurs while loading, release our storage. */
4644 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4645
98bfdba5
PA
4646 /* Read in the comp_unit header. */
4647 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4648
98bfdba5
PA
4649 /* Complete the cu_header. */
4650 cu->header.offset = offset;
4651 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4652
98bfdba5
PA
4653 /* Read the abbrevs for this compilation unit. */
4654 dwarf2_read_abbrevs (abfd, cu);
4655 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4656
98bfdba5
PA
4657 /* Link this compilation unit into the compilation unit tree. */
4658 per_cu->cu = cu;
4659 cu->per_cu = per_cu;
98bfdba5
PA
4660
4661 /* Link this CU into read_in_chain. */
4662 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4663 dwarf2_per_objfile->read_in_chain = per_cu;
4664 }
4665 else
4666 {
4667 cu = per_cu->cu;
4668 info_ptr += cu->header.first_die_offset;
4669 }
e142c38c 4670
93311388 4671 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4672
4673 /* We try not to read any attributes in this function, because not
4674 all objfiles needed for references have been loaded yet, and symbol
4675 table processing isn't initialized. But we have to set the CU language,
4676 or we won't be able to build types correctly. */
9816fde3 4677 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4678
a6c727b2
DJ
4679 /* Similarly, if we do not read the producer, we can not apply
4680 producer-specific interpretation. */
4681 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4682 if (attr)
4683 cu->producer = DW_STRING (attr);
4684
98bfdba5
PA
4685 if (read_cu)
4686 {
4687 do_cleanups (free_abbrevs_cleanup);
e142c38c 4688
98bfdba5
PA
4689 /* We've successfully allocated this compilation unit. Let our
4690 caller clean it up when finished with it. */
4691 discard_cleanups (free_cu_cleanup);
4692 }
10b3939b
DJ
4693}
4694
3da10d80
KS
4695/* Add a DIE to the delayed physname list. */
4696
4697static void
4698add_to_method_list (struct type *type, int fnfield_index, int index,
4699 const char *name, struct die_info *die,
4700 struct dwarf2_cu *cu)
4701{
4702 struct delayed_method_info mi;
4703 mi.type = type;
4704 mi.fnfield_index = fnfield_index;
4705 mi.index = index;
4706 mi.name = name;
4707 mi.die = die;
4708 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4709}
4710
4711/* A cleanup for freeing the delayed method list. */
4712
4713static void
4714free_delayed_list (void *ptr)
4715{
4716 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4717 if (cu->method_list != NULL)
4718 {
4719 VEC_free (delayed_method_info, cu->method_list);
4720 cu->method_list = NULL;
4721 }
4722}
4723
4724/* Compute the physnames of any methods on the CU's method list.
4725
4726 The computation of method physnames is delayed in order to avoid the
4727 (bad) condition that one of the method's formal parameters is of an as yet
4728 incomplete type. */
4729
4730static void
4731compute_delayed_physnames (struct dwarf2_cu *cu)
4732{
4733 int i;
4734 struct delayed_method_info *mi;
4735 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4736 {
1d06ead6 4737 const char *physname;
3da10d80
KS
4738 struct fn_fieldlist *fn_flp
4739 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4740 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4741 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4742 }
4743}
4744
10b3939b
DJ
4745/* Generate full symbol information for PST and CU, whose DIEs have
4746 already been loaded into memory. */
4747
4748static void
4749process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4750{
10b3939b 4751 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4752 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4753 CORE_ADDR lowpc, highpc;
4754 struct symtab *symtab;
3da10d80 4755 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4756 CORE_ADDR baseaddr;
4757
4758 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4759
10b3939b
DJ
4760 buildsym_init ();
4761 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4762 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4763
4764 cu->list_in_scope = &file_symbols;
c906108c
SS
4765
4766 /* Do line number decoding in read_file_scope () */
10b3939b 4767 process_die (cu->dies, cu);
c906108c 4768
3da10d80
KS
4769 /* Now that we have processed all the DIEs in the CU, all the types
4770 should be complete, and it should now be safe to compute all of the
4771 physnames. */
4772 compute_delayed_physnames (cu);
4773 do_cleanups (delayed_list_cleanup);
4774
fae299cd
DC
4775 /* Some compilers don't define a DW_AT_high_pc attribute for the
4776 compilation unit. If the DW_AT_high_pc is missing, synthesize
4777 it, by scanning the DIE's below the compilation unit. */
10b3939b 4778 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4779
613e1657 4780 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4781
8be455d7 4782 if (symtab != NULL)
c906108c 4783 {
df15bd07 4784 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 4785
8be455d7
JK
4786 /* Set symtab language to language from DW_AT_language. If the
4787 compilation is from a C file generated by language preprocessors, do
4788 not set the language if it was already deduced by start_subfile. */
4789 if (!(cu->language == language_c && symtab->language != language_c))
4790 symtab->language = cu->language;
4791
4792 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4793 produce DW_AT_location with location lists but it can be possibly
4794 invalid without -fvar-tracking.
4795
4796 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4797 needed, it would be wrong due to missing DW_AT_producer there.
4798
4799 Still one can confuse GDB by using non-standard GCC compilation
4800 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4801 */
4632c0d0 4802 if (cu->has_loclist && gcc_4_minor >= 0)
8be455d7 4803 symtab->locations_valid = 1;
e0d00bc7
JK
4804
4805 if (gcc_4_minor >= 5)
4806 symtab->epilogue_unwind_valid = 1;
96408a79
SA
4807
4808 symtab->call_site_htab = cu->call_site_htab;
c906108c 4809 }
9291a0cd
TT
4810
4811 if (dwarf2_per_objfile->using_index)
4812 per_cu->v.quick->symtab = symtab;
4813 else
4814 {
4815 struct partial_symtab *pst = per_cu->v.psymtab;
4816 pst->symtab = symtab;
4817 pst->readin = 1;
4818 }
c906108c
SS
4819
4820 do_cleanups (back_to);
4821}
4822
4823/* Process a die and its children. */
4824
4825static void
e7c27a73 4826process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4827{
4828 switch (die->tag)
4829 {
4830 case DW_TAG_padding:
4831 break;
4832 case DW_TAG_compile_unit:
e7c27a73 4833 read_file_scope (die, cu);
c906108c 4834 break;
348e048f
DE
4835 case DW_TAG_type_unit:
4836 read_type_unit_scope (die, cu);
4837 break;
c906108c 4838 case DW_TAG_subprogram:
c906108c 4839 case DW_TAG_inlined_subroutine:
edb3359d 4840 read_func_scope (die, cu);
c906108c
SS
4841 break;
4842 case DW_TAG_lexical_block:
14898363
L
4843 case DW_TAG_try_block:
4844 case DW_TAG_catch_block:
e7c27a73 4845 read_lexical_block_scope (die, cu);
c906108c 4846 break;
96408a79
SA
4847 case DW_TAG_GNU_call_site:
4848 read_call_site_scope (die, cu);
4849 break;
c906108c 4850 case DW_TAG_class_type:
680b30c7 4851 case DW_TAG_interface_type:
c906108c
SS
4852 case DW_TAG_structure_type:
4853 case DW_TAG_union_type:
134d01f1 4854 process_structure_scope (die, cu);
c906108c
SS
4855 break;
4856 case DW_TAG_enumeration_type:
134d01f1 4857 process_enumeration_scope (die, cu);
c906108c 4858 break;
134d01f1 4859
f792889a
DJ
4860 /* These dies have a type, but processing them does not create
4861 a symbol or recurse to process the children. Therefore we can
4862 read them on-demand through read_type_die. */
c906108c 4863 case DW_TAG_subroutine_type:
72019c9c 4864 case DW_TAG_set_type:
c906108c 4865 case DW_TAG_array_type:
c906108c 4866 case DW_TAG_pointer_type:
c906108c 4867 case DW_TAG_ptr_to_member_type:
c906108c 4868 case DW_TAG_reference_type:
c906108c 4869 case DW_TAG_string_type:
c906108c 4870 break;
134d01f1 4871
c906108c 4872 case DW_TAG_base_type:
a02abb62 4873 case DW_TAG_subrange_type:
cb249c71 4874 case DW_TAG_typedef:
134d01f1
DJ
4875 /* Add a typedef symbol for the type definition, if it has a
4876 DW_AT_name. */
f792889a 4877 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4878 break;
c906108c 4879 case DW_TAG_common_block:
e7c27a73 4880 read_common_block (die, cu);
c906108c
SS
4881 break;
4882 case DW_TAG_common_inclusion:
4883 break;
d9fa45fe 4884 case DW_TAG_namespace:
63d06c5c 4885 processing_has_namespace_info = 1;
e7c27a73 4886 read_namespace (die, cu);
d9fa45fe 4887 break;
5d7cb8df 4888 case DW_TAG_module:
f55ee35c 4889 processing_has_namespace_info = 1;
5d7cb8df
JK
4890 read_module (die, cu);
4891 break;
d9fa45fe
DC
4892 case DW_TAG_imported_declaration:
4893 case DW_TAG_imported_module:
63d06c5c 4894 processing_has_namespace_info = 1;
27aa8d6a
SW
4895 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4896 || cu->language != language_fortran))
4897 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4898 dwarf_tag_name (die->tag));
4899 read_import_statement (die, cu);
d9fa45fe 4900 break;
c906108c 4901 default:
e7c27a73 4902 new_symbol (die, NULL, cu);
c906108c
SS
4903 break;
4904 }
4905}
4906
94af9270
KS
4907/* A helper function for dwarf2_compute_name which determines whether DIE
4908 needs to have the name of the scope prepended to the name listed in the
4909 die. */
4910
4911static int
4912die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4913{
1c809c68
TT
4914 struct attribute *attr;
4915
94af9270
KS
4916 switch (die->tag)
4917 {
4918 case DW_TAG_namespace:
4919 case DW_TAG_typedef:
4920 case DW_TAG_class_type:
4921 case DW_TAG_interface_type:
4922 case DW_TAG_structure_type:
4923 case DW_TAG_union_type:
4924 case DW_TAG_enumeration_type:
4925 case DW_TAG_enumerator:
4926 case DW_TAG_subprogram:
4927 case DW_TAG_member:
4928 return 1;
4929
4930 case DW_TAG_variable:
c2b0a229 4931 case DW_TAG_constant:
94af9270
KS
4932 /* We only need to prefix "globally" visible variables. These include
4933 any variable marked with DW_AT_external or any variable that
4934 lives in a namespace. [Variables in anonymous namespaces
4935 require prefixing, but they are not DW_AT_external.] */
4936
4937 if (dwarf2_attr (die, DW_AT_specification, cu))
4938 {
4939 struct dwarf2_cu *spec_cu = cu;
9a619af0 4940
94af9270
KS
4941 return die_needs_namespace (die_specification (die, &spec_cu),
4942 spec_cu);
4943 }
4944
1c809c68 4945 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4946 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4947 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4948 return 0;
4949 /* A variable in a lexical block of some kind does not need a
4950 namespace, even though in C++ such variables may be external
4951 and have a mangled name. */
4952 if (die->parent->tag == DW_TAG_lexical_block
4953 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4954 || die->parent->tag == DW_TAG_catch_block
4955 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4956 return 0;
4957 return 1;
94af9270
KS
4958
4959 default:
4960 return 0;
4961 }
4962}
4963
98bfdba5
PA
4964/* Retrieve the last character from a mem_file. */
4965
4966static void
4967do_ui_file_peek_last (void *object, const char *buffer, long length)
4968{
4969 char *last_char_p = (char *) object;
4970
4971 if (length > 0)
4972 *last_char_p = buffer[length - 1];
4973}
4974
94af9270
KS
4975/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4976 compute the physname for the object, which include a method's
4977 formal parameters (C++/Java) and return type (Java).
4978
af6b7be1
JB
4979 For Ada, return the DIE's linkage name rather than the fully qualified
4980 name. PHYSNAME is ignored..
4981
94af9270
KS
4982 The result is allocated on the objfile_obstack and canonicalized. */
4983
4984static const char *
4985dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4986 int physname)
4987{
4988 if (name == NULL)
4989 name = dwarf2_name (die, cu);
4990
f55ee35c
JK
4991 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4992 compute it by typename_concat inside GDB. */
4993 if (cu->language == language_ada
4994 || (cu->language == language_fortran && physname))
4995 {
4996 /* For Ada unit, we prefer the linkage name over the name, as
4997 the former contains the exported name, which the user expects
4998 to be able to reference. Ideally, we want the user to be able
4999 to reference this entity using either natural or linkage name,
5000 but we haven't started looking at this enhancement yet. */
5001 struct attribute *attr;
5002
5003 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5004 if (attr == NULL)
5005 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5006 if (attr && DW_STRING (attr))
5007 return DW_STRING (attr);
5008 }
5009
94af9270
KS
5010 /* These are the only languages we know how to qualify names in. */
5011 if (name != NULL
f55ee35c
JK
5012 && (cu->language == language_cplus || cu->language == language_java
5013 || cu->language == language_fortran))
94af9270
KS
5014 {
5015 if (die_needs_namespace (die, cu))
5016 {
5017 long length;
5018 char *prefix;
5019 struct ui_file *buf;
5020
5021 prefix = determine_prefix (die, cu);
5022 buf = mem_fileopen ();
5023 if (*prefix != '\0')
5024 {
f55ee35c
JK
5025 char *prefixed_name = typename_concat (NULL, prefix, name,
5026 physname, cu);
9a619af0 5027
94af9270
KS
5028 fputs_unfiltered (prefixed_name, buf);
5029 xfree (prefixed_name);
5030 }
5031 else
62d5b8da 5032 fputs_unfiltered (name, buf);
94af9270 5033
98bfdba5
PA
5034 /* Template parameters may be specified in the DIE's DW_AT_name, or
5035 as children with DW_TAG_template_type_param or
5036 DW_TAG_value_type_param. If the latter, add them to the name
5037 here. If the name already has template parameters, then
5038 skip this step; some versions of GCC emit both, and
5039 it is more efficient to use the pre-computed name.
5040
5041 Something to keep in mind about this process: it is very
5042 unlikely, or in some cases downright impossible, to produce
5043 something that will match the mangled name of a function.
5044 If the definition of the function has the same debug info,
5045 we should be able to match up with it anyway. But fallbacks
5046 using the minimal symbol, for instance to find a method
5047 implemented in a stripped copy of libstdc++, will not work.
5048 If we do not have debug info for the definition, we will have to
5049 match them up some other way.
5050
5051 When we do name matching there is a related problem with function
5052 templates; two instantiated function templates are allowed to
5053 differ only by their return types, which we do not add here. */
5054
5055 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5056 {
5057 struct attribute *attr;
5058 struct die_info *child;
5059 int first = 1;
5060
5061 die->building_fullname = 1;
5062
5063 for (child = die->child; child != NULL; child = child->sibling)
5064 {
5065 struct type *type;
5066 long value;
5067 gdb_byte *bytes;
5068 struct dwarf2_locexpr_baton *baton;
5069 struct value *v;
5070
5071 if (child->tag != DW_TAG_template_type_param
5072 && child->tag != DW_TAG_template_value_param)
5073 continue;
5074
5075 if (first)
5076 {
5077 fputs_unfiltered ("<", buf);
5078 first = 0;
5079 }
5080 else
5081 fputs_unfiltered (", ", buf);
5082
5083 attr = dwarf2_attr (child, DW_AT_type, cu);
5084 if (attr == NULL)
5085 {
5086 complaint (&symfile_complaints,
5087 _("template parameter missing DW_AT_type"));
5088 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5089 continue;
5090 }
5091 type = die_type (child, cu);
5092
5093 if (child->tag == DW_TAG_template_type_param)
5094 {
5095 c_print_type (type, "", buf, -1, 0);
5096 continue;
5097 }
5098
5099 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5100 if (attr == NULL)
5101 {
5102 complaint (&symfile_complaints,
3e43a32a
MS
5103 _("template parameter missing "
5104 "DW_AT_const_value"));
98bfdba5
PA
5105 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5106 continue;
5107 }
5108
5109 dwarf2_const_value_attr (attr, type, name,
5110 &cu->comp_unit_obstack, cu,
5111 &value, &bytes, &baton);
5112
5113 if (TYPE_NOSIGN (type))
5114 /* GDB prints characters as NUMBER 'CHAR'. If that's
5115 changed, this can use value_print instead. */
5116 c_printchar (value, type, buf);
5117 else
5118 {
5119 struct value_print_options opts;
5120
5121 if (baton != NULL)
5122 v = dwarf2_evaluate_loc_desc (type, NULL,
5123 baton->data,
5124 baton->size,
5125 baton->per_cu);
5126 else if (bytes != NULL)
5127 {
5128 v = allocate_value (type);
5129 memcpy (value_contents_writeable (v), bytes,
5130 TYPE_LENGTH (type));
5131 }
5132 else
5133 v = value_from_longest (type, value);
5134
3e43a32a
MS
5135 /* Specify decimal so that we do not depend on
5136 the radix. */
98bfdba5
PA
5137 get_formatted_print_options (&opts, 'd');
5138 opts.raw = 1;
5139 value_print (v, buf, &opts);
5140 release_value (v);
5141 value_free (v);
5142 }
5143 }
5144
5145 die->building_fullname = 0;
5146
5147 if (!first)
5148 {
5149 /* Close the argument list, with a space if necessary
5150 (nested templates). */
5151 char last_char = '\0';
5152 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5153 if (last_char == '>')
5154 fputs_unfiltered (" >", buf);
5155 else
5156 fputs_unfiltered (">", buf);
5157 }
5158 }
5159
94af9270
KS
5160 /* For Java and C++ methods, append formal parameter type
5161 information, if PHYSNAME. */
6e70227d 5162
94af9270
KS
5163 if (physname && die->tag == DW_TAG_subprogram
5164 && (cu->language == language_cplus
5165 || cu->language == language_java))
5166 {
5167 struct type *type = read_type_die (die, cu);
5168
3167638f 5169 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5170
5171 if (cu->language == language_java)
5172 {
5173 /* For java, we must append the return type to method
0963b4bd 5174 names. */
94af9270
KS
5175 if (die->tag == DW_TAG_subprogram)
5176 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5177 0, 0);
5178 }
5179 else if (cu->language == language_cplus)
5180 {
60430eff
DJ
5181 /* Assume that an artificial first parameter is
5182 "this", but do not crash if it is not. RealView
5183 marks unnamed (and thus unused) parameters as
5184 artificial; there is no way to differentiate
5185 the two cases. */
94af9270
KS
5186 if (TYPE_NFIELDS (type) > 0
5187 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5188 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5189 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5190 0))))
94af9270
KS
5191 fputs_unfiltered (" const", buf);
5192 }
5193 }
5194
5195 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5196 &length);
5197 ui_file_delete (buf);
5198
5199 if (cu->language == language_cplus)
5200 {
5201 char *cname
5202 = dwarf2_canonicalize_name (name, cu,
5203 &cu->objfile->objfile_obstack);
9a619af0 5204
94af9270
KS
5205 if (cname != NULL)
5206 name = cname;
5207 }
5208 }
5209 }
5210
5211 return name;
5212}
5213
0114d602
DJ
5214/* Return the fully qualified name of DIE, based on its DW_AT_name.
5215 If scope qualifiers are appropriate they will be added. The result
5216 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5217 not have a name. NAME may either be from a previous call to
5218 dwarf2_name or NULL.
5219
0963b4bd 5220 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5221
5222static const char *
94af9270 5223dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5224{
94af9270
KS
5225 return dwarf2_compute_name (name, die, cu, 0);
5226}
0114d602 5227
94af9270
KS
5228/* Construct a physname for the given DIE in CU. NAME may either be
5229 from a previous call to dwarf2_name or NULL. The result will be
5230 allocated on the objfile_objstack or NULL if the DIE does not have a
5231 name.
0114d602 5232
94af9270 5233 The output string will be canonicalized (if C++/Java). */
0114d602 5234
94af9270
KS
5235static const char *
5236dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5237{
900e11f9
JK
5238 struct attribute *attr;
5239 const char *retval, *mangled = NULL, *canon = NULL;
5240 struct cleanup *back_to;
5241 int need_copy = 1;
5242
5243 /* In this case dwarf2_compute_name is just a shortcut not building anything
5244 on its own. */
5245 if (!die_needs_namespace (die, cu))
5246 return dwarf2_compute_name (name, die, cu, 1);
5247
5248 back_to = make_cleanup (null_cleanup, NULL);
5249
5250 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5251 if (!attr)
5252 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5253
5254 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5255 has computed. */
5256 if (attr && DW_STRING (attr))
5257 {
5258 char *demangled;
5259
5260 mangled = DW_STRING (attr);
5261
5262 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5263 type. It is easier for GDB users to search for such functions as
5264 `name(params)' than `long name(params)'. In such case the minimal
5265 symbol names do not match the full symbol names but for template
5266 functions there is never a need to look up their definition from their
5267 declaration so the only disadvantage remains the minimal symbol
5268 variant `long name(params)' does not have the proper inferior type.
5269 */
5270
5271 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5272 | (cu->language == language_java
5273 ? DMGL_JAVA | DMGL_RET_POSTFIX
5274 : DMGL_RET_DROP)));
5275 if (demangled)
5276 {
5277 make_cleanup (xfree, demangled);
5278 canon = demangled;
5279 }
5280 else
5281 {
5282 canon = mangled;
5283 need_copy = 0;
5284 }
5285 }
5286
5287 if (canon == NULL || check_physname)
5288 {
5289 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5290
5291 if (canon != NULL && strcmp (physname, canon) != 0)
5292 {
5293 /* It may not mean a bug in GDB. The compiler could also
5294 compute DW_AT_linkage_name incorrectly. But in such case
5295 GDB would need to be bug-to-bug compatible. */
5296
5297 complaint (&symfile_complaints,
5298 _("Computed physname <%s> does not match demangled <%s> "
5299 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5300 physname, canon, mangled, die->offset, cu->objfile->name);
5301
5302 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5303 is available here - over computed PHYSNAME. It is safer
5304 against both buggy GDB and buggy compilers. */
5305
5306 retval = canon;
5307 }
5308 else
5309 {
5310 retval = physname;
5311 need_copy = 0;
5312 }
5313 }
5314 else
5315 retval = canon;
5316
5317 if (need_copy)
5318 retval = obsavestring (retval, strlen (retval),
5319 &cu->objfile->objfile_obstack);
5320
5321 do_cleanups (back_to);
5322 return retval;
0114d602
DJ
5323}
5324
27aa8d6a
SW
5325/* Read the import statement specified by the given die and record it. */
5326
5327static void
5328read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5329{
5330 struct attribute *import_attr;
32019081 5331 struct die_info *imported_die, *child_die;
de4affc9 5332 struct dwarf2_cu *imported_cu;
27aa8d6a 5333 const char *imported_name;
794684b6 5334 const char *imported_name_prefix;
13387711
SW
5335 const char *canonical_name;
5336 const char *import_alias;
5337 const char *imported_declaration = NULL;
794684b6 5338 const char *import_prefix;
32019081
JK
5339 VEC (const_char_ptr) *excludes = NULL;
5340 struct cleanup *cleanups;
13387711
SW
5341
5342 char *temp;
27aa8d6a
SW
5343
5344 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5345 if (import_attr == NULL)
5346 {
5347 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5348 dwarf_tag_name (die->tag));
5349 return;
5350 }
5351
de4affc9
CC
5352 imported_cu = cu;
5353 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5354 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5355 if (imported_name == NULL)
5356 {
5357 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5358
5359 The import in the following code:
5360 namespace A
5361 {
5362 typedef int B;
5363 }
5364
5365 int main ()
5366 {
5367 using A::B;
5368 B b;
5369 return b;
5370 }
5371
5372 ...
5373 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5374 <52> DW_AT_decl_file : 1
5375 <53> DW_AT_decl_line : 6
5376 <54> DW_AT_import : <0x75>
5377 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5378 <59> DW_AT_name : B
5379 <5b> DW_AT_decl_file : 1
5380 <5c> DW_AT_decl_line : 2
5381 <5d> DW_AT_type : <0x6e>
5382 ...
5383 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5384 <76> DW_AT_byte_size : 4
5385 <77> DW_AT_encoding : 5 (signed)
5386
5387 imports the wrong die ( 0x75 instead of 0x58 ).
5388 This case will be ignored until the gcc bug is fixed. */
5389 return;
5390 }
5391
82856980
SW
5392 /* Figure out the local name after import. */
5393 import_alias = dwarf2_name (die, cu);
27aa8d6a 5394
794684b6
SW
5395 /* Figure out where the statement is being imported to. */
5396 import_prefix = determine_prefix (die, cu);
5397
5398 /* Figure out what the scope of the imported die is and prepend it
5399 to the name of the imported die. */
de4affc9 5400 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5401
f55ee35c
JK
5402 if (imported_die->tag != DW_TAG_namespace
5403 && imported_die->tag != DW_TAG_module)
794684b6 5404 {
13387711
SW
5405 imported_declaration = imported_name;
5406 canonical_name = imported_name_prefix;
794684b6 5407 }
13387711 5408 else if (strlen (imported_name_prefix) > 0)
794684b6 5409 {
13387711
SW
5410 temp = alloca (strlen (imported_name_prefix)
5411 + 2 + strlen (imported_name) + 1);
5412 strcpy (temp, imported_name_prefix);
5413 strcat (temp, "::");
5414 strcat (temp, imported_name);
5415 canonical_name = temp;
794684b6 5416 }
13387711
SW
5417 else
5418 canonical_name = imported_name;
794684b6 5419
32019081
JK
5420 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5421
5422 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5423 for (child_die = die->child; child_die && child_die->tag;
5424 child_die = sibling_die (child_die))
5425 {
5426 /* DWARF-4: A Fortran use statement with a “rename list” may be
5427 represented by an imported module entry with an import attribute
5428 referring to the module and owned entries corresponding to those
5429 entities that are renamed as part of being imported. */
5430
5431 if (child_die->tag != DW_TAG_imported_declaration)
5432 {
5433 complaint (&symfile_complaints,
5434 _("child DW_TAG_imported_declaration expected "
5435 "- DIE at 0x%x [in module %s]"),
5436 child_die->offset, cu->objfile->name);
5437 continue;
5438 }
5439
5440 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5441 if (import_attr == NULL)
5442 {
5443 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5444 dwarf_tag_name (child_die->tag));
5445 continue;
5446 }
5447
5448 imported_cu = cu;
5449 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5450 &imported_cu);
5451 imported_name = dwarf2_name (imported_die, imported_cu);
5452 if (imported_name == NULL)
5453 {
5454 complaint (&symfile_complaints,
5455 _("child DW_TAG_imported_declaration has unknown "
5456 "imported name - DIE at 0x%x [in module %s]"),
5457 child_die->offset, cu->objfile->name);
5458 continue;
5459 }
5460
5461 VEC_safe_push (const_char_ptr, excludes, imported_name);
5462
5463 process_die (child_die, cu);
5464 }
5465
c0cc3a76
SW
5466 cp_add_using_directive (import_prefix,
5467 canonical_name,
5468 import_alias,
13387711 5469 imported_declaration,
32019081 5470 excludes,
c0cc3a76 5471 &cu->objfile->objfile_obstack);
32019081
JK
5472
5473 do_cleanups (cleanups);
27aa8d6a
SW
5474}
5475
5fb290d7 5476static void
e142c38c 5477initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5478{
e142c38c 5479 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5480}
5481
ae2de4f8
DE
5482/* Cleanup function for read_file_scope. */
5483
cb1df416
DJ
5484static void
5485free_cu_line_header (void *arg)
5486{
5487 struct dwarf2_cu *cu = arg;
5488
5489 free_line_header (cu->line_header);
5490 cu->line_header = NULL;
5491}
5492
9291a0cd
TT
5493static void
5494find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5495 char **name, char **comp_dir)
5496{
5497 struct attribute *attr;
5498
5499 *name = NULL;
5500 *comp_dir = NULL;
5501
5502 /* Find the filename. Do not use dwarf2_name here, since the filename
5503 is not a source language identifier. */
5504 attr = dwarf2_attr (die, DW_AT_name, cu);
5505 if (attr)
5506 {
5507 *name = DW_STRING (attr);
5508 }
5509
5510 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5511 if (attr)
5512 *comp_dir = DW_STRING (attr);
5513 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5514 {
5515 *comp_dir = ldirname (*name);
5516 if (*comp_dir != NULL)
5517 make_cleanup (xfree, *comp_dir);
5518 }
5519 if (*comp_dir != NULL)
5520 {
5521 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5522 directory, get rid of it. */
5523 char *cp = strchr (*comp_dir, ':');
5524
5525 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5526 *comp_dir = cp + 1;
5527 }
5528
5529 if (*name == NULL)
5530 *name = "<unknown>";
5531}
5532
2ab95328
TT
5533/* Handle DW_AT_stmt_list for a compilation unit. */
5534
5535static void
5536handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5537 const char *comp_dir)
5538{
5539 struct attribute *attr;
5540 struct objfile *objfile = cu->objfile;
5541 bfd *abfd = objfile->obfd;
5542
5543 /* Decode line number information if present. We do this before
5544 processing child DIEs, so that the line header table is available
5545 for DW_AT_decl_file. */
5546 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5547 if (attr)
5548 {
5549 unsigned int line_offset = DW_UNSND (attr);
5550 struct line_header *line_header
5551 = dwarf_decode_line_header (line_offset, abfd, cu);
5552
5553 if (line_header)
5554 {
5555 cu->line_header = line_header;
5556 make_cleanup (free_cu_line_header, cu);
5557 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5558 }
5559 }
5560}
5561
ae2de4f8
DE
5562/* Process DW_TAG_compile_unit. */
5563
c906108c 5564static void
e7c27a73 5565read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5566{
e7c27a73 5567 struct objfile *objfile = cu->objfile;
debd256d 5568 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5569 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5570 CORE_ADDR highpc = ((CORE_ADDR) 0);
5571 struct attribute *attr;
e1024ff1 5572 char *name = NULL;
c906108c
SS
5573 char *comp_dir = NULL;
5574 struct die_info *child_die;
5575 bfd *abfd = objfile->obfd;
e142c38c 5576 CORE_ADDR baseaddr;
6e70227d 5577
e142c38c 5578 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5579
fae299cd 5580 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5581
5582 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5583 from finish_block. */
2acceee2 5584 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5585 lowpc = highpc;
5586 lowpc += baseaddr;
5587 highpc += baseaddr;
5588
9291a0cd 5589 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5590
e142c38c 5591 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5592 if (attr)
5593 {
e142c38c 5594 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5595 }
5596
b0f35d58 5597 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5598 if (attr)
b0f35d58 5599 cu->producer = DW_STRING (attr);
303b6f5d 5600
f4b8a18d
KW
5601 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5602 standardised yet. As a workaround for the language detection we fall
5603 back to the DW_AT_producer string. */
5604 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5605 cu->language = language_opencl;
5606
0963b4bd 5607 /* We assume that we're processing GCC output. */
c906108c 5608 processing_gcc_compilation = 2;
c906108c 5609
df8a16a1
DJ
5610 processing_has_namespace_info = 0;
5611
c906108c
SS
5612 start_symtab (name, comp_dir, lowpc);
5613 record_debugformat ("DWARF 2");
303b6f5d 5614 record_producer (cu->producer);
c906108c 5615
e142c38c 5616 initialize_cu_func_list (cu);
c906108c 5617
2ab95328 5618 handle_DW_AT_stmt_list (die, cu, comp_dir);
debd256d 5619
cb1df416
DJ
5620 /* Process all dies in compilation unit. */
5621 if (die->child != NULL)
5622 {
5623 child_die = die->child;
5624 while (child_die && child_die->tag)
5625 {
5626 process_die (child_die, cu);
5627 child_die = sibling_die (child_die);
5628 }
5629 }
5630
2e276125
JB
5631 /* Decode macro information, if present. Dwarf 2 macro information
5632 refers to information in the line number info statement program
5633 header, so we can only read it if we've read the header
5634 successfully. */
cf2c3c16 5635 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
2ab95328 5636 if (attr && cu->line_header)
2e276125 5637 {
cf2c3c16
TT
5638 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5639 complaint (&symfile_complaints,
5640 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5641
5642 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5643 comp_dir, abfd, cu,
5644 &dwarf2_per_objfile->macro, 1);
5645 }
5646 else
5647 {
5648 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5649 if (attr && cu->line_header)
5650 {
5651 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5652
cf2c3c16
TT
5653 dwarf_decode_macros (cu->line_header, macro_offset,
5654 comp_dir, abfd, cu,
5655 &dwarf2_per_objfile->macinfo, 0);
5656 }
2e276125 5657 }
debd256d 5658 do_cleanups (back_to);
5fb290d7
DJ
5659}
5660
ae2de4f8
DE
5661/* Process DW_TAG_type_unit.
5662 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5663 actual type being defined by this TU. In this case the first top
5664 level sibling is there to provide context only. */
5665
5666static void
5667read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5668{
5669 struct objfile *objfile = cu->objfile;
5670 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5671 CORE_ADDR lowpc;
5672 struct attribute *attr;
5673 char *name = NULL;
5674 char *comp_dir = NULL;
5675 struct die_info *child_die;
5676 bfd *abfd = objfile->obfd;
348e048f
DE
5677
5678 /* start_symtab needs a low pc, but we don't really have one.
5679 Do what read_file_scope would do in the absence of such info. */
5680 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5681
5682 /* Find the filename. Do not use dwarf2_name here, since the filename
5683 is not a source language identifier. */
5684 attr = dwarf2_attr (die, DW_AT_name, cu);
5685 if (attr)
5686 name = DW_STRING (attr);
5687
5688 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5689 if (attr)
5690 comp_dir = DW_STRING (attr);
5691 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5692 {
5693 comp_dir = ldirname (name);
5694 if (comp_dir != NULL)
5695 make_cleanup (xfree, comp_dir);
5696 }
5697
5698 if (name == NULL)
5699 name = "<unknown>";
5700
5701 attr = dwarf2_attr (die, DW_AT_language, cu);
5702 if (attr)
5703 set_cu_language (DW_UNSND (attr), cu);
5704
5705 /* This isn't technically needed today. It is done for symmetry
5706 with read_file_scope. */
5707 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5708 if (attr)
348e048f
DE
5709 cu->producer = DW_STRING (attr);
5710
0963b4bd 5711 /* We assume that we're processing GCC output. */
348e048f
DE
5712 processing_gcc_compilation = 2;
5713
5714 processing_has_namespace_info = 0;
5715
5716 start_symtab (name, comp_dir, lowpc);
5717 record_debugformat ("DWARF 2");
5718 record_producer (cu->producer);
5719
2ab95328
TT
5720 handle_DW_AT_stmt_list (die, cu, comp_dir);
5721
348e048f
DE
5722 /* Process the dies in the type unit. */
5723 if (die->child == NULL)
5724 {
5725 dump_die_for_error (die);
5726 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5727 bfd_get_filename (abfd));
5728 }
5729
5730 child_die = die->child;
5731
5732 while (child_die && child_die->tag)
5733 {
5734 process_die (child_die, cu);
5735
5736 child_die = sibling_die (child_die);
5737 }
5738
5739 do_cleanups (back_to);
5740}
5741
5fb290d7 5742static void
e142c38c
DJ
5743add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5744 struct dwarf2_cu *cu)
5fb290d7
DJ
5745{
5746 struct function_range *thisfn;
5747
5748 thisfn = (struct function_range *)
7b5a2f43 5749 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5750 thisfn->name = name;
5751 thisfn->lowpc = lowpc;
5752 thisfn->highpc = highpc;
5753 thisfn->seen_line = 0;
5754 thisfn->next = NULL;
5755
e142c38c
DJ
5756 if (cu->last_fn == NULL)
5757 cu->first_fn = thisfn;
5fb290d7 5758 else
e142c38c 5759 cu->last_fn->next = thisfn;
5fb290d7 5760
e142c38c 5761 cu->last_fn = thisfn;
c906108c
SS
5762}
5763
d389af10
JK
5764/* qsort helper for inherit_abstract_dies. */
5765
5766static int
5767unsigned_int_compar (const void *ap, const void *bp)
5768{
5769 unsigned int a = *(unsigned int *) ap;
5770 unsigned int b = *(unsigned int *) bp;
5771
5772 return (a > b) - (b > a);
5773}
5774
5775/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5776 Inherit only the children of the DW_AT_abstract_origin DIE not being
5777 already referenced by DW_AT_abstract_origin from the children of the
5778 current DIE. */
d389af10
JK
5779
5780static void
5781inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5782{
5783 struct die_info *child_die;
5784 unsigned die_children_count;
5785 /* CU offsets which were referenced by children of the current DIE. */
5786 unsigned *offsets;
5787 unsigned *offsets_end, *offsetp;
5788 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5789 struct die_info *origin_die;
5790 /* Iterator of the ORIGIN_DIE children. */
5791 struct die_info *origin_child_die;
5792 struct cleanup *cleanups;
5793 struct attribute *attr;
cd02d79d
PA
5794 struct dwarf2_cu *origin_cu;
5795 struct pending **origin_previous_list_in_scope;
d389af10
JK
5796
5797 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5798 if (!attr)
5799 return;
5800
cd02d79d
PA
5801 /* Note that following die references may follow to a die in a
5802 different cu. */
5803
5804 origin_cu = cu;
5805 origin_die = follow_die_ref (die, attr, &origin_cu);
5806
5807 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5808 symbols in. */
5809 origin_previous_list_in_scope = origin_cu->list_in_scope;
5810 origin_cu->list_in_scope = cu->list_in_scope;
5811
edb3359d
DJ
5812 if (die->tag != origin_die->tag
5813 && !(die->tag == DW_TAG_inlined_subroutine
5814 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5815 complaint (&symfile_complaints,
5816 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5817 die->offset, origin_die->offset);
5818
5819 child_die = die->child;
5820 die_children_count = 0;
5821 while (child_die && child_die->tag)
5822 {
5823 child_die = sibling_die (child_die);
5824 die_children_count++;
5825 }
5826 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5827 cleanups = make_cleanup (xfree, offsets);
5828
5829 offsets_end = offsets;
5830 child_die = die->child;
5831 while (child_die && child_die->tag)
5832 {
c38f313d
DJ
5833 /* For each CHILD_DIE, find the corresponding child of
5834 ORIGIN_DIE. If there is more than one layer of
5835 DW_AT_abstract_origin, follow them all; there shouldn't be,
5836 but GCC versions at least through 4.4 generate this (GCC PR
5837 40573). */
5838 struct die_info *child_origin_die = child_die;
cd02d79d 5839 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5840
c38f313d
DJ
5841 while (1)
5842 {
cd02d79d
PA
5843 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5844 child_origin_cu);
c38f313d
DJ
5845 if (attr == NULL)
5846 break;
cd02d79d
PA
5847 child_origin_die = follow_die_ref (child_origin_die, attr,
5848 &child_origin_cu);
c38f313d
DJ
5849 }
5850
d389af10
JK
5851 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5852 counterpart may exist. */
c38f313d 5853 if (child_origin_die != child_die)
d389af10 5854 {
edb3359d
DJ
5855 if (child_die->tag != child_origin_die->tag
5856 && !(child_die->tag == DW_TAG_inlined_subroutine
5857 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5858 complaint (&symfile_complaints,
5859 _("Child DIE 0x%x and its abstract origin 0x%x have "
5860 "different tags"), child_die->offset,
5861 child_origin_die->offset);
c38f313d
DJ
5862 if (child_origin_die->parent != origin_die)
5863 complaint (&symfile_complaints,
5864 _("Child DIE 0x%x and its abstract origin 0x%x have "
5865 "different parents"), child_die->offset,
5866 child_origin_die->offset);
5867 else
5868 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5869 }
5870 child_die = sibling_die (child_die);
5871 }
5872 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5873 unsigned_int_compar);
5874 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5875 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5876 complaint (&symfile_complaints,
5877 _("Multiple children of DIE 0x%x refer "
5878 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5879 die->offset, *offsetp);
5880
5881 offsetp = offsets;
5882 origin_child_die = origin_die->child;
5883 while (origin_child_die && origin_child_die->tag)
5884 {
5885 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5886 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5887 offsetp++;
5888 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5889 {
5890 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5891 process_die (origin_child_die, origin_cu);
d389af10
JK
5892 }
5893 origin_child_die = sibling_die (origin_child_die);
5894 }
cd02d79d 5895 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5896
5897 do_cleanups (cleanups);
5898}
5899
c906108c 5900static void
e7c27a73 5901read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5902{
e7c27a73 5903 struct objfile *objfile = cu->objfile;
52f0bd74 5904 struct context_stack *new;
c906108c
SS
5905 CORE_ADDR lowpc;
5906 CORE_ADDR highpc;
5907 struct die_info *child_die;
edb3359d 5908 struct attribute *attr, *call_line, *call_file;
c906108c 5909 char *name;
e142c38c 5910 CORE_ADDR baseaddr;
801e3a5b 5911 struct block *block;
edb3359d 5912 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5913 VEC (symbolp) *template_args = NULL;
5914 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5915
5916 if (inlined_func)
5917 {
5918 /* If we do not have call site information, we can't show the
5919 caller of this inlined function. That's too confusing, so
5920 only use the scope for local variables. */
5921 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5922 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5923 if (call_line == NULL || call_file == NULL)
5924 {
5925 read_lexical_block_scope (die, cu);
5926 return;
5927 }
5928 }
c906108c 5929
e142c38c
DJ
5930 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5931
94af9270 5932 name = dwarf2_name (die, cu);
c906108c 5933
e8d05480
JB
5934 /* Ignore functions with missing or empty names. These are actually
5935 illegal according to the DWARF standard. */
5936 if (name == NULL)
5937 {
5938 complaint (&symfile_complaints,
5939 _("missing name for subprogram DIE at %d"), die->offset);
5940 return;
5941 }
5942
5943 /* Ignore functions with missing or invalid low and high pc attributes. */
5944 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5945 {
ae4d0c03
PM
5946 attr = dwarf2_attr (die, DW_AT_external, cu);
5947 if (!attr || !DW_UNSND (attr))
5948 complaint (&symfile_complaints,
3e43a32a
MS
5949 _("cannot get low and high bounds "
5950 "for subprogram DIE at %d"),
ae4d0c03 5951 die->offset);
e8d05480
JB
5952 return;
5953 }
c906108c
SS
5954
5955 lowpc += baseaddr;
5956 highpc += baseaddr;
5957
5fb290d7 5958 /* Record the function range for dwarf_decode_lines. */
e142c38c 5959 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5960
34eaf542
TT
5961 /* If we have any template arguments, then we must allocate a
5962 different sort of symbol. */
5963 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5964 {
5965 if (child_die->tag == DW_TAG_template_type_param
5966 || child_die->tag == DW_TAG_template_value_param)
5967 {
5968 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5969 struct template_symbol);
5970 templ_func->base.is_cplus_template_function = 1;
5971 break;
5972 }
5973 }
5974
c906108c 5975 new = push_context (0, lowpc);
34eaf542
TT
5976 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5977 (struct symbol *) templ_func);
4c2df51b 5978
4cecd739
DJ
5979 /* If there is a location expression for DW_AT_frame_base, record
5980 it. */
e142c38c 5981 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5982 if (attr)
c034e007
AC
5983 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5984 expression is being recorded directly in the function's symbol
5985 and not in a separate frame-base object. I guess this hack is
5986 to avoid adding some sort of frame-base adjunct/annex to the
5987 function's symbol :-(. The problem with doing this is that it
5988 results in a function symbol with a location expression that
5989 has nothing to do with the location of the function, ouch! The
5990 relationship should be: a function's symbol has-a frame base; a
5991 frame-base has-a location expression. */
e7c27a73 5992 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5993
e142c38c 5994 cu->list_in_scope = &local_symbols;
c906108c 5995
639d11d3 5996 if (die->child != NULL)
c906108c 5997 {
639d11d3 5998 child_die = die->child;
c906108c
SS
5999 while (child_die && child_die->tag)
6000 {
34eaf542
TT
6001 if (child_die->tag == DW_TAG_template_type_param
6002 || child_die->tag == DW_TAG_template_value_param)
6003 {
6004 struct symbol *arg = new_symbol (child_die, NULL, cu);
6005
f1078f66
DJ
6006 if (arg != NULL)
6007 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6008 }
6009 else
6010 process_die (child_die, cu);
c906108c
SS
6011 child_die = sibling_die (child_die);
6012 }
6013 }
6014
d389af10
JK
6015 inherit_abstract_dies (die, cu);
6016
4a811a97
UW
6017 /* If we have a DW_AT_specification, we might need to import using
6018 directives from the context of the specification DIE. See the
6019 comment in determine_prefix. */
6020 if (cu->language == language_cplus
6021 && dwarf2_attr (die, DW_AT_specification, cu))
6022 {
6023 struct dwarf2_cu *spec_cu = cu;
6024 struct die_info *spec_die = die_specification (die, &spec_cu);
6025
6026 while (spec_die)
6027 {
6028 child_die = spec_die->child;
6029 while (child_die && child_die->tag)
6030 {
6031 if (child_die->tag == DW_TAG_imported_module)
6032 process_die (child_die, spec_cu);
6033 child_die = sibling_die (child_die);
6034 }
6035
6036 /* In some cases, GCC generates specification DIEs that
6037 themselves contain DW_AT_specification attributes. */
6038 spec_die = die_specification (spec_die, &spec_cu);
6039 }
6040 }
6041
c906108c
SS
6042 new = pop_context ();
6043 /* Make a block for the local symbols within. */
801e3a5b
JB
6044 block = finish_block (new->name, &local_symbols, new->old_blocks,
6045 lowpc, highpc, objfile);
6046
df8a16a1 6047 /* For C++, set the block's scope. */
f55ee35c 6048 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 6049 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 6050 determine_prefix (die, cu),
df8a16a1
DJ
6051 processing_has_namespace_info);
6052
801e3a5b
JB
6053 /* If we have address ranges, record them. */
6054 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 6055
34eaf542
TT
6056 /* Attach template arguments to function. */
6057 if (! VEC_empty (symbolp, template_args))
6058 {
6059 gdb_assert (templ_func != NULL);
6060
6061 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6062 templ_func->template_arguments
6063 = obstack_alloc (&objfile->objfile_obstack,
6064 (templ_func->n_template_arguments
6065 * sizeof (struct symbol *)));
6066 memcpy (templ_func->template_arguments,
6067 VEC_address (symbolp, template_args),
6068 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6069 VEC_free (symbolp, template_args);
6070 }
6071
208d8187
JB
6072 /* In C++, we can have functions nested inside functions (e.g., when
6073 a function declares a class that has methods). This means that
6074 when we finish processing a function scope, we may need to go
6075 back to building a containing block's symbol lists. */
6076 local_symbols = new->locals;
6077 param_symbols = new->params;
27aa8d6a 6078 using_directives = new->using_directives;
208d8187 6079
921e78cf
JB
6080 /* If we've finished processing a top-level function, subsequent
6081 symbols go in the file symbol list. */
6082 if (outermost_context_p ())
e142c38c 6083 cu->list_in_scope = &file_symbols;
c906108c
SS
6084}
6085
6086/* Process all the DIES contained within a lexical block scope. Start
6087 a new scope, process the dies, and then close the scope. */
6088
6089static void
e7c27a73 6090read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6091{
e7c27a73 6092 struct objfile *objfile = cu->objfile;
52f0bd74 6093 struct context_stack *new;
c906108c
SS
6094 CORE_ADDR lowpc, highpc;
6095 struct die_info *child_die;
e142c38c
DJ
6096 CORE_ADDR baseaddr;
6097
6098 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6099
6100 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6101 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6102 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6103 be nasty. Might be easier to properly extend generic blocks to
af34e669 6104 describe ranges. */
d85a05f0 6105 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6106 return;
6107 lowpc += baseaddr;
6108 highpc += baseaddr;
6109
6110 push_context (0, lowpc);
639d11d3 6111 if (die->child != NULL)
c906108c 6112 {
639d11d3 6113 child_die = die->child;
c906108c
SS
6114 while (child_die && child_die->tag)
6115 {
e7c27a73 6116 process_die (child_die, cu);
c906108c
SS
6117 child_die = sibling_die (child_die);
6118 }
6119 }
6120 new = pop_context ();
6121
8540c487 6122 if (local_symbols != NULL || using_directives != NULL)
c906108c 6123 {
801e3a5b
JB
6124 struct block *block
6125 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6126 highpc, objfile);
6127
6128 /* Note that recording ranges after traversing children, as we
6129 do here, means that recording a parent's ranges entails
6130 walking across all its children's ranges as they appear in
6131 the address map, which is quadratic behavior.
6132
6133 It would be nicer to record the parent's ranges before
6134 traversing its children, simply overriding whatever you find
6135 there. But since we don't even decide whether to create a
6136 block until after we've traversed its children, that's hard
6137 to do. */
6138 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6139 }
6140 local_symbols = new->locals;
27aa8d6a 6141 using_directives = new->using_directives;
c906108c
SS
6142}
6143
96408a79
SA
6144/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6145
6146static void
6147read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6148{
6149 struct objfile *objfile = cu->objfile;
6150 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6151 CORE_ADDR pc, baseaddr;
6152 struct attribute *attr;
6153 struct call_site *call_site, call_site_local;
6154 void **slot;
6155 int nparams;
6156 struct die_info *child_die;
6157
6158 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6159
6160 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6161 if (!attr)
6162 {
6163 complaint (&symfile_complaints,
6164 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6165 "DIE 0x%x [in module %s]"),
6166 die->offset, cu->objfile->name);
6167 return;
6168 }
6169 pc = DW_ADDR (attr) + baseaddr;
6170
6171 if (cu->call_site_htab == NULL)
6172 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6173 NULL, &objfile->objfile_obstack,
6174 hashtab_obstack_allocate, NULL);
6175 call_site_local.pc = pc;
6176 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6177 if (*slot != NULL)
6178 {
6179 complaint (&symfile_complaints,
6180 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6181 "DIE 0x%x [in module %s]"),
6182 paddress (gdbarch, pc), die->offset, cu->objfile->name);
6183 return;
6184 }
6185
6186 /* Count parameters at the caller. */
6187
6188 nparams = 0;
6189 for (child_die = die->child; child_die && child_die->tag;
6190 child_die = sibling_die (child_die))
6191 {
6192 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6193 {
6194 complaint (&symfile_complaints,
6195 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6196 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6197 child_die->tag, child_die->offset, cu->objfile->name);
6198 continue;
6199 }
6200
6201 nparams++;
6202 }
6203
6204 call_site = obstack_alloc (&objfile->objfile_obstack,
6205 (sizeof (*call_site)
6206 + (sizeof (*call_site->parameter)
6207 * (nparams - 1))));
6208 *slot = call_site;
6209 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6210 call_site->pc = pc;
6211
6212 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6213 {
6214 struct die_info *func_die;
6215
6216 /* Skip also over DW_TAG_inlined_subroutine. */
6217 for (func_die = die->parent;
6218 func_die && func_die->tag != DW_TAG_subprogram
6219 && func_die->tag != DW_TAG_subroutine_type;
6220 func_die = func_die->parent);
6221
6222 /* DW_AT_GNU_all_call_sites is a superset
6223 of DW_AT_GNU_all_tail_call_sites. */
6224 if (func_die
6225 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6226 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6227 {
6228 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6229 not complete. But keep CALL_SITE for look ups via call_site_htab,
6230 both the initial caller containing the real return address PC and
6231 the final callee containing the current PC of a chain of tail
6232 calls do not need to have the tail call list complete. But any
6233 function candidate for a virtual tail call frame searched via
6234 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6235 determined unambiguously. */
6236 }
6237 else
6238 {
6239 struct type *func_type = NULL;
6240
6241 if (func_die)
6242 func_type = get_die_type (func_die, cu);
6243 if (func_type != NULL)
6244 {
6245 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6246
6247 /* Enlist this call site to the function. */
6248 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6249 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6250 }
6251 else
6252 complaint (&symfile_complaints,
6253 _("Cannot find function owning DW_TAG_GNU_call_site "
6254 "DIE 0x%x [in module %s]"),
6255 die->offset, cu->objfile->name);
6256 }
6257 }
6258
6259 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6260 if (attr == NULL)
6261 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6262 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6263 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6264 /* Keep NULL DWARF_BLOCK. */;
6265 else if (attr_form_is_block (attr))
6266 {
6267 struct dwarf2_locexpr_baton *dlbaton;
6268
6269 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6270 dlbaton->data = DW_BLOCK (attr)->data;
6271 dlbaton->size = DW_BLOCK (attr)->size;
6272 dlbaton->per_cu = cu->per_cu;
6273
6274 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6275 }
6276 else if (is_ref_attr (attr))
6277 {
6278 struct objfile *objfile = cu->objfile;
6279 struct dwarf2_cu *target_cu = cu;
6280 struct die_info *target_die;
6281
6282 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6283 gdb_assert (target_cu->objfile == objfile);
6284 if (die_is_declaration (target_die, target_cu))
6285 {
6286 const char *target_physname;
6287
6288 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6289 if (target_physname == NULL)
6290 complaint (&symfile_complaints,
6291 _("DW_AT_GNU_call_site_target target DIE has invalid "
6292 "physname, for referencing DIE 0x%x [in module %s]"),
6293 die->offset, cu->objfile->name);
6294 else
6295 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6296 }
6297 else
6298 {
6299 CORE_ADDR lowpc;
6300
6301 /* DW_AT_entry_pc should be preferred. */
6302 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6303 complaint (&symfile_complaints,
6304 _("DW_AT_GNU_call_site_target target DIE has invalid "
6305 "low pc, for referencing DIE 0x%x [in module %s]"),
6306 die->offset, cu->objfile->name);
6307 else
6308 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6309 }
6310 }
6311 else
6312 complaint (&symfile_complaints,
6313 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6314 "block nor reference, for DIE 0x%x [in module %s]"),
6315 die->offset, cu->objfile->name);
6316
6317 call_site->per_cu = cu->per_cu;
6318
6319 for (child_die = die->child;
6320 child_die && child_die->tag;
6321 child_die = sibling_die (child_die))
6322 {
6323 struct dwarf2_locexpr_baton *dlbaton;
6324 struct call_site_parameter *parameter;
6325
6326 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6327 {
6328 /* Already printed the complaint above. */
6329 continue;
6330 }
6331
6332 gdb_assert (call_site->parameter_count < nparams);
6333 parameter = &call_site->parameter[call_site->parameter_count];
6334
6335 /* DW_AT_location specifies the register number. Value of the data
6336 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6337
6338 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6339 if (!attr || !attr_form_is_block (attr))
6340 {
6341 complaint (&symfile_complaints,
6342 _("No DW_FORM_block* DW_AT_location for "
6343 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6344 child_die->offset, cu->objfile->name);
6345 continue;
6346 }
6347 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6348 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6349 if (parameter->dwarf_reg == -1
6350 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6351 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6352 &parameter->fb_offset))
6353 {
6354 complaint (&symfile_complaints,
6355 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6356 "for DW_FORM_block* DW_AT_location for "
6357 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6358 child_die->offset, cu->objfile->name);
6359 continue;
6360 }
6361
6362 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6363 if (!attr_form_is_block (attr))
6364 {
6365 complaint (&symfile_complaints,
6366 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6367 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6368 child_die->offset, cu->objfile->name);
6369 continue;
6370 }
6371 parameter->value = DW_BLOCK (attr)->data;
6372 parameter->value_size = DW_BLOCK (attr)->size;
6373
6374 /* Parameters are not pre-cleared by memset above. */
6375 parameter->data_value = NULL;
6376 parameter->data_value_size = 0;
6377 call_site->parameter_count++;
6378
6379 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6380 if (attr)
6381 {
6382 if (!attr_form_is_block (attr))
6383 complaint (&symfile_complaints,
6384 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6385 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6386 child_die->offset, cu->objfile->name);
6387 else
6388 {
6389 parameter->data_value = DW_BLOCK (attr)->data;
6390 parameter->data_value_size = DW_BLOCK (attr)->size;
6391 }
6392 }
6393 }
6394}
6395
43039443 6396/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6397 Return 1 if the attributes are present and valid, otherwise, return 0.
6398 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6399
6400static int
6401dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6402 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6403 struct partial_symtab *ranges_pst)
43039443
JK
6404{
6405 struct objfile *objfile = cu->objfile;
6406 struct comp_unit_head *cu_header = &cu->header;
6407 bfd *obfd = objfile->obfd;
6408 unsigned int addr_size = cu_header->addr_size;
6409 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6410 /* Base address selection entry. */
6411 CORE_ADDR base;
6412 int found_base;
6413 unsigned int dummy;
6414 gdb_byte *buffer;
6415 CORE_ADDR marker;
6416 int low_set;
6417 CORE_ADDR low = 0;
6418 CORE_ADDR high = 0;
ff013f42 6419 CORE_ADDR baseaddr;
43039443 6420
d00adf39
DE
6421 found_base = cu->base_known;
6422 base = cu->base_address;
43039443 6423
be391dca 6424 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6425 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6426 {
6427 complaint (&symfile_complaints,
6428 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6429 offset);
6430 return 0;
6431 }
dce234bc 6432 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6433
6434 /* Read in the largest possible address. */
6435 marker = read_address (obfd, buffer, cu, &dummy);
6436 if ((marker & mask) == mask)
6437 {
6438 /* If we found the largest possible address, then
6439 read the base address. */
6440 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6441 buffer += 2 * addr_size;
6442 offset += 2 * addr_size;
6443 found_base = 1;
6444 }
6445
6446 low_set = 0;
6447
e7030f15 6448 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6449
43039443
JK
6450 while (1)
6451 {
6452 CORE_ADDR range_beginning, range_end;
6453
6454 range_beginning = read_address (obfd, buffer, cu, &dummy);
6455 buffer += addr_size;
6456 range_end = read_address (obfd, buffer, cu, &dummy);
6457 buffer += addr_size;
6458 offset += 2 * addr_size;
6459
6460 /* An end of list marker is a pair of zero addresses. */
6461 if (range_beginning == 0 && range_end == 0)
6462 /* Found the end of list entry. */
6463 break;
6464
6465 /* Each base address selection entry is a pair of 2 values.
6466 The first is the largest possible address, the second is
6467 the base address. Check for a base address here. */
6468 if ((range_beginning & mask) == mask)
6469 {
6470 /* If we found the largest possible address, then
6471 read the base address. */
6472 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6473 found_base = 1;
6474 continue;
6475 }
6476
6477 if (!found_base)
6478 {
6479 /* We have no valid base address for the ranges
6480 data. */
6481 complaint (&symfile_complaints,
6482 _("Invalid .debug_ranges data (no base address)"));
6483 return 0;
6484 }
6485
9277c30c
UW
6486 if (range_beginning > range_end)
6487 {
6488 /* Inverted range entries are invalid. */
6489 complaint (&symfile_complaints,
6490 _("Invalid .debug_ranges data (inverted range)"));
6491 return 0;
6492 }
6493
6494 /* Empty range entries have no effect. */
6495 if (range_beginning == range_end)
6496 continue;
6497
43039443
JK
6498 range_beginning += base;
6499 range_end += base;
6500
9277c30c 6501 if (ranges_pst != NULL)
ff013f42 6502 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6503 range_beginning + baseaddr,
6504 range_end - 1 + baseaddr,
ff013f42
JK
6505 ranges_pst);
6506
43039443
JK
6507 /* FIXME: This is recording everything as a low-high
6508 segment of consecutive addresses. We should have a
6509 data structure for discontiguous block ranges
6510 instead. */
6511 if (! low_set)
6512 {
6513 low = range_beginning;
6514 high = range_end;
6515 low_set = 1;
6516 }
6517 else
6518 {
6519 if (range_beginning < low)
6520 low = range_beginning;
6521 if (range_end > high)
6522 high = range_end;
6523 }
6524 }
6525
6526 if (! low_set)
6527 /* If the first entry is an end-of-list marker, the range
6528 describes an empty scope, i.e. no instructions. */
6529 return 0;
6530
6531 if (low_return)
6532 *low_return = low;
6533 if (high_return)
6534 *high_return = high;
6535 return 1;
6536}
6537
af34e669
DJ
6538/* Get low and high pc attributes from a die. Return 1 if the attributes
6539 are present and valid, otherwise, return 0. Return -1 if the range is
6540 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6541static int
af34e669 6542dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6543 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6544 struct partial_symtab *pst)
c906108c
SS
6545{
6546 struct attribute *attr;
af34e669
DJ
6547 CORE_ADDR low = 0;
6548 CORE_ADDR high = 0;
6549 int ret = 0;
c906108c 6550
e142c38c 6551 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6552 if (attr)
af34e669
DJ
6553 {
6554 high = DW_ADDR (attr);
e142c38c 6555 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6556 if (attr)
6557 low = DW_ADDR (attr);
6558 else
6559 /* Found high w/o low attribute. */
6560 return 0;
6561
6562 /* Found consecutive range of addresses. */
6563 ret = 1;
6564 }
c906108c 6565 else
af34e669 6566 {
e142c38c 6567 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6568 if (attr != NULL)
6569 {
af34e669 6570 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6571 .debug_ranges section. */
d85a05f0 6572 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6573 return 0;
43039443 6574 /* Found discontinuous range of addresses. */
af34e669
DJ
6575 ret = -1;
6576 }
6577 }
c906108c 6578
9373cf26
JK
6579 /* read_partial_die has also the strict LOW < HIGH requirement. */
6580 if (high <= low)
c906108c
SS
6581 return 0;
6582
6583 /* When using the GNU linker, .gnu.linkonce. sections are used to
6584 eliminate duplicate copies of functions and vtables and such.
6585 The linker will arbitrarily choose one and discard the others.
6586 The AT_*_pc values for such functions refer to local labels in
6587 these sections. If the section from that file was discarded, the
6588 labels are not in the output, so the relocs get a value of 0.
6589 If this is a discarded function, mark the pc bounds as invalid,
6590 so that GDB will ignore it. */
72dca2f5 6591 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6592 return 0;
6593
6594 *lowpc = low;
96408a79
SA
6595 if (highpc)
6596 *highpc = high;
af34e669 6597 return ret;
c906108c
SS
6598}
6599
b084d499
JB
6600/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6601 its low and high PC addresses. Do nothing if these addresses could not
6602 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6603 and HIGHPC to the high address if greater than HIGHPC. */
6604
6605static void
6606dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6607 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6608 struct dwarf2_cu *cu)
6609{
6610 CORE_ADDR low, high;
6611 struct die_info *child = die->child;
6612
d85a05f0 6613 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6614 {
6615 *lowpc = min (*lowpc, low);
6616 *highpc = max (*highpc, high);
6617 }
6618
6619 /* If the language does not allow nested subprograms (either inside
6620 subprograms or lexical blocks), we're done. */
6621 if (cu->language != language_ada)
6622 return;
6e70227d 6623
b084d499
JB
6624 /* Check all the children of the given DIE. If it contains nested
6625 subprograms, then check their pc bounds. Likewise, we need to
6626 check lexical blocks as well, as they may also contain subprogram
6627 definitions. */
6628 while (child && child->tag)
6629 {
6630 if (child->tag == DW_TAG_subprogram
6631 || child->tag == DW_TAG_lexical_block)
6632 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6633 child = sibling_die (child);
6634 }
6635}
6636
fae299cd
DC
6637/* Get the low and high pc's represented by the scope DIE, and store
6638 them in *LOWPC and *HIGHPC. If the correct values can't be
6639 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6640
6641static void
6642get_scope_pc_bounds (struct die_info *die,
6643 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6644 struct dwarf2_cu *cu)
6645{
6646 CORE_ADDR best_low = (CORE_ADDR) -1;
6647 CORE_ADDR best_high = (CORE_ADDR) 0;
6648 CORE_ADDR current_low, current_high;
6649
d85a05f0 6650 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6651 {
6652 best_low = current_low;
6653 best_high = current_high;
6654 }
6655 else
6656 {
6657 struct die_info *child = die->child;
6658
6659 while (child && child->tag)
6660 {
6661 switch (child->tag) {
6662 case DW_TAG_subprogram:
b084d499 6663 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6664 break;
6665 case DW_TAG_namespace:
f55ee35c 6666 case DW_TAG_module:
fae299cd
DC
6667 /* FIXME: carlton/2004-01-16: Should we do this for
6668 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6669 that current GCC's always emit the DIEs corresponding
6670 to definitions of methods of classes as children of a
6671 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6672 the DIEs giving the declarations, which could be
6673 anywhere). But I don't see any reason why the
6674 standards says that they have to be there. */
6675 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6676
6677 if (current_low != ((CORE_ADDR) -1))
6678 {
6679 best_low = min (best_low, current_low);
6680 best_high = max (best_high, current_high);
6681 }
6682 break;
6683 default:
0963b4bd 6684 /* Ignore. */
fae299cd
DC
6685 break;
6686 }
6687
6688 child = sibling_die (child);
6689 }
6690 }
6691
6692 *lowpc = best_low;
6693 *highpc = best_high;
6694}
6695
801e3a5b
JB
6696/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6697 in DIE. */
6698static void
6699dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6700 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6701{
6702 struct attribute *attr;
6703
6704 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6705 if (attr)
6706 {
6707 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6708
801e3a5b
JB
6709 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6710 if (attr)
6711 {
6712 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6713
801e3a5b
JB
6714 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6715 }
6716 }
6717
6718 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6719 if (attr)
6720 {
6721 bfd *obfd = cu->objfile->obfd;
6722
6723 /* The value of the DW_AT_ranges attribute is the offset of the
6724 address range list in the .debug_ranges section. */
6725 unsigned long offset = DW_UNSND (attr);
dce234bc 6726 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6727
6728 /* For some target architectures, but not others, the
6729 read_address function sign-extends the addresses it returns.
6730 To recognize base address selection entries, we need a
6731 mask. */
6732 unsigned int addr_size = cu->header.addr_size;
6733 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6734
6735 /* The base address, to which the next pair is relative. Note
6736 that this 'base' is a DWARF concept: most entries in a range
6737 list are relative, to reduce the number of relocs against the
6738 debugging information. This is separate from this function's
6739 'baseaddr' argument, which GDB uses to relocate debugging
6740 information from a shared library based on the address at
6741 which the library was loaded. */
d00adf39
DE
6742 CORE_ADDR base = cu->base_address;
6743 int base_known = cu->base_known;
801e3a5b 6744
be391dca 6745 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6746 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6747 {
6748 complaint (&symfile_complaints,
6749 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6750 offset);
6751 return;
6752 }
6753
6754 for (;;)
6755 {
6756 unsigned int bytes_read;
6757 CORE_ADDR start, end;
6758
6759 start = read_address (obfd, buffer, cu, &bytes_read);
6760 buffer += bytes_read;
6761 end = read_address (obfd, buffer, cu, &bytes_read);
6762 buffer += bytes_read;
6763
6764 /* Did we find the end of the range list? */
6765 if (start == 0 && end == 0)
6766 break;
6767
6768 /* Did we find a base address selection entry? */
6769 else if ((start & base_select_mask) == base_select_mask)
6770 {
6771 base = end;
6772 base_known = 1;
6773 }
6774
6775 /* We found an ordinary address range. */
6776 else
6777 {
6778 if (!base_known)
6779 {
6780 complaint (&symfile_complaints,
3e43a32a
MS
6781 _("Invalid .debug_ranges data "
6782 "(no base address)"));
801e3a5b
JB
6783 return;
6784 }
6785
9277c30c
UW
6786 if (start > end)
6787 {
6788 /* Inverted range entries are invalid. */
6789 complaint (&symfile_complaints,
6790 _("Invalid .debug_ranges data "
6791 "(inverted range)"));
6792 return;
6793 }
6794
6795 /* Empty range entries have no effect. */
6796 if (start == end)
6797 continue;
6798
6e70227d
DE
6799 record_block_range (block,
6800 baseaddr + base + start,
801e3a5b
JB
6801 baseaddr + base + end - 1);
6802 }
6803 }
6804 }
6805}
6806
60d5a603
JK
6807/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6808 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6809 during 4.6.0 experimental. */
6810
6811static int
6812producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6813{
6814 const char *cs;
6815 int major, minor, release;
6816
6817 if (cu->producer == NULL)
6818 {
6819 /* For unknown compilers expect their behavior is DWARF version
6820 compliant.
6821
6822 GCC started to support .debug_types sections by -gdwarf-4 since
6823 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6824 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6825 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6826 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6827
6828 return 0;
6829 }
6830
6831 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6832
6833 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6834 {
6835 /* For non-GCC compilers expect their behavior is DWARF version
6836 compliant. */
6837
6838 return 0;
6839 }
6840 cs = &cu->producer[strlen ("GNU ")];
6841 while (*cs && !isdigit (*cs))
6842 cs++;
6843 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6844 {
6845 /* Not recognized as GCC. */
6846
6847 return 0;
6848 }
6849
6850 return major < 4 || (major == 4 && minor < 6);
6851}
6852
6853/* Return the default accessibility type if it is not overriden by
6854 DW_AT_accessibility. */
6855
6856static enum dwarf_access_attribute
6857dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6858{
6859 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6860 {
6861 /* The default DWARF 2 accessibility for members is public, the default
6862 accessibility for inheritance is private. */
6863
6864 if (die->tag != DW_TAG_inheritance)
6865 return DW_ACCESS_public;
6866 else
6867 return DW_ACCESS_private;
6868 }
6869 else
6870 {
6871 /* DWARF 3+ defines the default accessibility a different way. The same
6872 rules apply now for DW_TAG_inheritance as for the members and it only
6873 depends on the container kind. */
6874
6875 if (die->parent->tag == DW_TAG_class_type)
6876 return DW_ACCESS_private;
6877 else
6878 return DW_ACCESS_public;
6879 }
6880}
6881
74ac6d43
TT
6882/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6883 offset. If the attribute was not found return 0, otherwise return
6884 1. If it was found but could not properly be handled, set *OFFSET
6885 to 0. */
6886
6887static int
6888handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6889 LONGEST *offset)
6890{
6891 struct attribute *attr;
6892
6893 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6894 if (attr != NULL)
6895 {
6896 *offset = 0;
6897
6898 /* Note that we do not check for a section offset first here.
6899 This is because DW_AT_data_member_location is new in DWARF 4,
6900 so if we see it, we can assume that a constant form is really
6901 a constant and not a section offset. */
6902 if (attr_form_is_constant (attr))
6903 *offset = dwarf2_get_attr_constant_value (attr, 0);
6904 else if (attr_form_is_section_offset (attr))
6905 dwarf2_complex_location_expr_complaint ();
6906 else if (attr_form_is_block (attr))
6907 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6908 else
6909 dwarf2_complex_location_expr_complaint ();
6910
6911 return 1;
6912 }
6913
6914 return 0;
6915}
6916
c906108c
SS
6917/* Add an aggregate field to the field list. */
6918
6919static void
107d2387 6920dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6921 struct dwarf2_cu *cu)
6e70227d 6922{
e7c27a73 6923 struct objfile *objfile = cu->objfile;
5e2b427d 6924 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6925 struct nextfield *new_field;
6926 struct attribute *attr;
6927 struct field *fp;
6928 char *fieldname = "";
6929
6930 /* Allocate a new field list entry and link it in. */
6931 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6932 make_cleanup (xfree, new_field);
c906108c 6933 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6934
6935 if (die->tag == DW_TAG_inheritance)
6936 {
6937 new_field->next = fip->baseclasses;
6938 fip->baseclasses = new_field;
6939 }
6940 else
6941 {
6942 new_field->next = fip->fields;
6943 fip->fields = new_field;
6944 }
c906108c
SS
6945 fip->nfields++;
6946
e142c38c 6947 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6948 if (attr)
6949 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
6950 else
6951 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
6952 if (new_field->accessibility != DW_ACCESS_public)
6953 fip->non_public_fields = 1;
60d5a603 6954
e142c38c 6955 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6956 if (attr)
6957 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
6958 else
6959 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
6960
6961 fp = &new_field->field;
a9a9bd0f 6962
e142c38c 6963 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6964 {
74ac6d43
TT
6965 LONGEST offset;
6966
a9a9bd0f 6967 /* Data member other than a C++ static data member. */
6e70227d 6968
c906108c 6969 /* Get type of field. */
e7c27a73 6970 fp->type = die_type (die, cu);
c906108c 6971
d6a843b5 6972 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6973
c906108c 6974 /* Get bit size of field (zero if none). */
e142c38c 6975 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6976 if (attr)
6977 {
6978 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6979 }
6980 else
6981 {
6982 FIELD_BITSIZE (*fp) = 0;
6983 }
6984
6985 /* Get bit offset of field. */
74ac6d43
TT
6986 if (handle_data_member_location (die, cu, &offset))
6987 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 6988 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6989 if (attr)
6990 {
5e2b427d 6991 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6992 {
6993 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6994 additional bit offset from the MSB of the containing
6995 anonymous object to the MSB of the field. We don't
6996 have to do anything special since we don't need to
6997 know the size of the anonymous object. */
c906108c
SS
6998 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6999 }
7000 else
7001 {
7002 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
7003 MSB of the anonymous object, subtract off the number of
7004 bits from the MSB of the field to the MSB of the
7005 object, and then subtract off the number of bits of
7006 the field itself. The result is the bit offset of
7007 the LSB of the field. */
c906108c
SS
7008 int anonymous_size;
7009 int bit_offset = DW_UNSND (attr);
7010
e142c38c 7011 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7012 if (attr)
7013 {
7014 /* The size of the anonymous object containing
7015 the bit field is explicit, so use the
7016 indicated size (in bytes). */
7017 anonymous_size = DW_UNSND (attr);
7018 }
7019 else
7020 {
7021 /* The size of the anonymous object containing
7022 the bit field must be inferred from the type
7023 attribute of the data member containing the
7024 bit field. */
7025 anonymous_size = TYPE_LENGTH (fp->type);
7026 }
7027 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7028 - bit_offset - FIELD_BITSIZE (*fp);
7029 }
7030 }
7031
7032 /* Get name of field. */
39cbfefa
DJ
7033 fieldname = dwarf2_name (die, cu);
7034 if (fieldname == NULL)
7035 fieldname = "";
d8151005
DJ
7036
7037 /* The name is already allocated along with this objfile, so we don't
7038 need to duplicate it for the type. */
7039 fp->name = fieldname;
c906108c
SS
7040
7041 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 7042 pointer or virtual base class pointer) to private. */
e142c38c 7043 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 7044 {
d48cc9dd 7045 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
7046 new_field->accessibility = DW_ACCESS_private;
7047 fip->non_public_fields = 1;
7048 }
7049 }
a9a9bd0f 7050 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 7051 {
a9a9bd0f
DC
7052 /* C++ static member. */
7053
7054 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7055 is a declaration, but all versions of G++ as of this writing
7056 (so through at least 3.2.1) incorrectly generate
7057 DW_TAG_variable tags. */
6e70227d 7058
ff355380 7059 const char *physname;
c906108c 7060
a9a9bd0f 7061 /* Get name of field. */
39cbfefa
DJ
7062 fieldname = dwarf2_name (die, cu);
7063 if (fieldname == NULL)
c906108c
SS
7064 return;
7065
254e6b9e 7066 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
7067 if (attr
7068 /* Only create a symbol if this is an external value.
7069 new_symbol checks this and puts the value in the global symbol
7070 table, which we want. If it is not external, new_symbol
7071 will try to put the value in cu->list_in_scope which is wrong. */
7072 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
7073 {
7074 /* A static const member, not much different than an enum as far as
7075 we're concerned, except that we can support more types. */
7076 new_symbol (die, NULL, cu);
7077 }
7078
2df3850c 7079 /* Get physical name. */
ff355380 7080 physname = dwarf2_physname (fieldname, die, cu);
c906108c 7081
d8151005
DJ
7082 /* The name is already allocated along with this objfile, so we don't
7083 need to duplicate it for the type. */
7084 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 7085 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 7086 FIELD_NAME (*fp) = fieldname;
c906108c
SS
7087 }
7088 else if (die->tag == DW_TAG_inheritance)
7089 {
74ac6d43 7090 LONGEST offset;
d4b96c9a 7091
74ac6d43
TT
7092 /* C++ base class field. */
7093 if (handle_data_member_location (die, cu, &offset))
7094 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 7095 FIELD_BITSIZE (*fp) = 0;
e7c27a73 7096 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
7097 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7098 fip->nbaseclasses++;
7099 }
7100}
7101
98751a41
JK
7102/* Add a typedef defined in the scope of the FIP's class. */
7103
7104static void
7105dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7106 struct dwarf2_cu *cu)
6e70227d 7107{
98751a41 7108 struct objfile *objfile = cu->objfile;
98751a41
JK
7109 struct typedef_field_list *new_field;
7110 struct attribute *attr;
7111 struct typedef_field *fp;
7112 char *fieldname = "";
7113
7114 /* Allocate a new field list entry and link it in. */
7115 new_field = xzalloc (sizeof (*new_field));
7116 make_cleanup (xfree, new_field);
7117
7118 gdb_assert (die->tag == DW_TAG_typedef);
7119
7120 fp = &new_field->field;
7121
7122 /* Get name of field. */
7123 fp->name = dwarf2_name (die, cu);
7124 if (fp->name == NULL)
7125 return;
7126
7127 fp->type = read_type_die (die, cu);
7128
7129 new_field->next = fip->typedef_field_list;
7130 fip->typedef_field_list = new_field;
7131 fip->typedef_field_list_count++;
7132}
7133
c906108c
SS
7134/* Create the vector of fields, and attach it to the type. */
7135
7136static void
fba45db2 7137dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7138 struct dwarf2_cu *cu)
c906108c
SS
7139{
7140 int nfields = fip->nfields;
7141
7142 /* Record the field count, allocate space for the array of fields,
7143 and create blank accessibility bitfields if necessary. */
7144 TYPE_NFIELDS (type) = nfields;
7145 TYPE_FIELDS (type) = (struct field *)
7146 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7147 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7148
b4ba55a1 7149 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
7150 {
7151 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7152
7153 TYPE_FIELD_PRIVATE_BITS (type) =
7154 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7155 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7156
7157 TYPE_FIELD_PROTECTED_BITS (type) =
7158 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7159 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7160
774b6a14
TT
7161 TYPE_FIELD_IGNORE_BITS (type) =
7162 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7163 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
7164 }
7165
7166 /* If the type has baseclasses, allocate and clear a bit vector for
7167 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 7168 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
7169 {
7170 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 7171 unsigned char *pointer;
c906108c
SS
7172
7173 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
7174 pointer = TYPE_ALLOC (type, num_bytes);
7175 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
7176 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7177 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7178 }
7179
3e43a32a
MS
7180 /* Copy the saved-up fields into the field vector. Start from the head of
7181 the list, adding to the tail of the field array, so that they end up in
7182 the same order in the array in which they were added to the list. */
c906108c
SS
7183 while (nfields-- > 0)
7184 {
7d0ccb61
DJ
7185 struct nextfield *fieldp;
7186
7187 if (fip->fields)
7188 {
7189 fieldp = fip->fields;
7190 fip->fields = fieldp->next;
7191 }
7192 else
7193 {
7194 fieldp = fip->baseclasses;
7195 fip->baseclasses = fieldp->next;
7196 }
7197
7198 TYPE_FIELD (type, nfields) = fieldp->field;
7199 switch (fieldp->accessibility)
c906108c 7200 {
c5aa993b 7201 case DW_ACCESS_private:
b4ba55a1
JB
7202 if (cu->language != language_ada)
7203 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 7204 break;
c906108c 7205
c5aa993b 7206 case DW_ACCESS_protected:
b4ba55a1
JB
7207 if (cu->language != language_ada)
7208 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 7209 break;
c906108c 7210
c5aa993b
JM
7211 case DW_ACCESS_public:
7212 break;
c906108c 7213
c5aa993b
JM
7214 default:
7215 /* Unknown accessibility. Complain and treat it as public. */
7216 {
e2e0b3e5 7217 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 7218 fieldp->accessibility);
c5aa993b
JM
7219 }
7220 break;
c906108c
SS
7221 }
7222 if (nfields < fip->nbaseclasses)
7223 {
7d0ccb61 7224 switch (fieldp->virtuality)
c906108c 7225 {
c5aa993b
JM
7226 case DW_VIRTUALITY_virtual:
7227 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 7228 if (cu->language == language_ada)
a73c6dcd 7229 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
7230 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7231 break;
c906108c
SS
7232 }
7233 }
c906108c
SS
7234 }
7235}
7236
c906108c
SS
7237/* Add a member function to the proper fieldlist. */
7238
7239static void
107d2387 7240dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 7241 struct type *type, struct dwarf2_cu *cu)
c906108c 7242{
e7c27a73 7243 struct objfile *objfile = cu->objfile;
c906108c
SS
7244 struct attribute *attr;
7245 struct fnfieldlist *flp;
7246 int i;
7247 struct fn_field *fnp;
7248 char *fieldname;
c906108c 7249 struct nextfnfield *new_fnfield;
f792889a 7250 struct type *this_type;
60d5a603 7251 enum dwarf_access_attribute accessibility;
c906108c 7252
b4ba55a1 7253 if (cu->language == language_ada)
a73c6dcd 7254 error (_("unexpected member function in Ada type"));
b4ba55a1 7255
2df3850c 7256 /* Get name of member function. */
39cbfefa
DJ
7257 fieldname = dwarf2_name (die, cu);
7258 if (fieldname == NULL)
2df3850c 7259 return;
c906108c 7260
c906108c
SS
7261 /* Look up member function name in fieldlist. */
7262 for (i = 0; i < fip->nfnfields; i++)
7263 {
27bfe10e 7264 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
7265 break;
7266 }
7267
7268 /* Create new list element if necessary. */
7269 if (i < fip->nfnfields)
7270 flp = &fip->fnfieldlists[i];
7271 else
7272 {
7273 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7274 {
7275 fip->fnfieldlists = (struct fnfieldlist *)
7276 xrealloc (fip->fnfieldlists,
7277 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7278 * sizeof (struct fnfieldlist));
c906108c 7279 if (fip->nfnfields == 0)
c13c43fd 7280 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
7281 }
7282 flp = &fip->fnfieldlists[fip->nfnfields];
7283 flp->name = fieldname;
7284 flp->length = 0;
7285 flp->head = NULL;
3da10d80 7286 i = fip->nfnfields++;
c906108c
SS
7287 }
7288
7289 /* Create a new member function field and chain it to the field list
0963b4bd 7290 entry. */
c906108c 7291 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 7292 make_cleanup (xfree, new_fnfield);
c906108c
SS
7293 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7294 new_fnfield->next = flp->head;
7295 flp->head = new_fnfield;
7296 flp->length++;
7297
7298 /* Fill in the member function field info. */
7299 fnp = &new_fnfield->fnfield;
3da10d80
KS
7300
7301 /* Delay processing of the physname until later. */
7302 if (cu->language == language_cplus || cu->language == language_java)
7303 {
7304 add_to_method_list (type, i, flp->length - 1, fieldname,
7305 die, cu);
7306 }
7307 else
7308 {
1d06ead6 7309 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
7310 fnp->physname = physname ? physname : "";
7311 }
7312
c906108c 7313 fnp->type = alloc_type (objfile);
f792889a
DJ
7314 this_type = read_type_die (die, cu);
7315 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 7316 {
f792889a 7317 int nparams = TYPE_NFIELDS (this_type);
c906108c 7318
f792889a 7319 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7320 of the method itself (TYPE_CODE_METHOD). */
7321 smash_to_method_type (fnp->type, type,
f792889a
DJ
7322 TYPE_TARGET_TYPE (this_type),
7323 TYPE_FIELDS (this_type),
7324 TYPE_NFIELDS (this_type),
7325 TYPE_VARARGS (this_type));
c906108c
SS
7326
7327 /* Handle static member functions.
c5aa993b 7328 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7329 member functions. G++ helps GDB by marking the first
7330 parameter for non-static member functions (which is the this
7331 pointer) as artificial. We obtain this information from
7332 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7333 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7334 fnp->voffset = VOFFSET_STATIC;
7335 }
7336 else
e2e0b3e5 7337 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7338 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7339
7340 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7341 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7342 fnp->fcontext = die_containing_type (die, cu);
c906108c 7343
3e43a32a
MS
7344 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7345 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7346
7347 /* Get accessibility. */
e142c38c 7348 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7349 if (attr)
60d5a603
JK
7350 accessibility = DW_UNSND (attr);
7351 else
7352 accessibility = dwarf2_default_access_attribute (die, cu);
7353 switch (accessibility)
c906108c 7354 {
60d5a603
JK
7355 case DW_ACCESS_private:
7356 fnp->is_private = 1;
7357 break;
7358 case DW_ACCESS_protected:
7359 fnp->is_protected = 1;
7360 break;
c906108c
SS
7361 }
7362
b02dede2 7363 /* Check for artificial methods. */
e142c38c 7364 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7365 if (attr && DW_UNSND (attr) != 0)
7366 fnp->is_artificial = 1;
7367
0d564a31 7368 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7369 function. For older versions of GCC, this is an offset in the
7370 appropriate virtual table, as specified by DW_AT_containing_type.
7371 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7372 to the object address. */
7373
e142c38c 7374 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7375 if (attr)
8e19ed76 7376 {
aec5aa8b 7377 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7378 {
aec5aa8b
TT
7379 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7380 {
7381 /* Old-style GCC. */
7382 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7383 }
7384 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7385 || (DW_BLOCK (attr)->size > 1
7386 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7387 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7388 {
7389 struct dwarf_block blk;
7390 int offset;
7391
7392 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7393 ? 1 : 2);
7394 blk.size = DW_BLOCK (attr)->size - offset;
7395 blk.data = DW_BLOCK (attr)->data + offset;
7396 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7397 if ((fnp->voffset % cu->header.addr_size) != 0)
7398 dwarf2_complex_location_expr_complaint ();
7399 else
7400 fnp->voffset /= cu->header.addr_size;
7401 fnp->voffset += 2;
7402 }
7403 else
7404 dwarf2_complex_location_expr_complaint ();
7405
7406 if (!fnp->fcontext)
7407 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7408 }
3690dd37 7409 else if (attr_form_is_section_offset (attr))
8e19ed76 7410 {
4d3c2250 7411 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7412 }
7413 else
7414 {
4d3c2250
KB
7415 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7416 fieldname);
8e19ed76 7417 }
0d564a31 7418 }
d48cc9dd
DJ
7419 else
7420 {
7421 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7422 if (attr && DW_UNSND (attr))
7423 {
7424 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7425 complaint (&symfile_complaints,
3e43a32a
MS
7426 _("Member function \"%s\" (offset %d) is virtual "
7427 "but the vtable offset is not specified"),
d48cc9dd 7428 fieldname, die->offset);
9655fd1a 7429 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7430 TYPE_CPLUS_DYNAMIC (type) = 1;
7431 }
7432 }
c906108c
SS
7433}
7434
7435/* Create the vector of member function fields, and attach it to the type. */
7436
7437static void
fba45db2 7438dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7439 struct dwarf2_cu *cu)
c906108c
SS
7440{
7441 struct fnfieldlist *flp;
7442 int total_length = 0;
7443 int i;
7444
b4ba55a1 7445 if (cu->language == language_ada)
a73c6dcd 7446 error (_("unexpected member functions in Ada type"));
b4ba55a1 7447
c906108c
SS
7448 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7449 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7450 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7451
7452 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7453 {
7454 struct nextfnfield *nfp = flp->head;
7455 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7456 int k;
7457
7458 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7459 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7460 fn_flp->fn_fields = (struct fn_field *)
7461 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7462 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7463 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7464
7465 total_length += flp->length;
7466 }
7467
7468 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7469 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7470}
7471
1168df01
JB
7472/* Returns non-zero if NAME is the name of a vtable member in CU's
7473 language, zero otherwise. */
7474static int
7475is_vtable_name (const char *name, struct dwarf2_cu *cu)
7476{
7477 static const char vptr[] = "_vptr";
987504bb 7478 static const char vtable[] = "vtable";
1168df01 7479
987504bb
JJ
7480 /* Look for the C++ and Java forms of the vtable. */
7481 if ((cu->language == language_java
7482 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7483 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7484 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7485 return 1;
7486
7487 return 0;
7488}
7489
c0dd20ea 7490/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7491 functions, with the ABI-specified layout. If TYPE describes
7492 such a structure, smash it into a member function type.
61049d3b
DJ
7493
7494 GCC shouldn't do this; it should just output pointer to member DIEs.
7495 This is GCC PR debug/28767. */
c0dd20ea 7496
0b92b5bb
TT
7497static void
7498quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7499{
0b92b5bb 7500 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7501
7502 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7503 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7504 return;
c0dd20ea
DJ
7505
7506 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7507 if (TYPE_FIELD_NAME (type, 0) == NULL
7508 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7509 || TYPE_FIELD_NAME (type, 1) == NULL
7510 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7511 return;
c0dd20ea
DJ
7512
7513 /* Find the type of the method. */
0b92b5bb 7514 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7515 if (pfn_type == NULL
7516 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7517 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7518 return;
c0dd20ea
DJ
7519
7520 /* Look for the "this" argument. */
7521 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7522 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7523 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7524 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7525 return;
c0dd20ea
DJ
7526
7527 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7528 new_type = alloc_type (objfile);
7529 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7530 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7531 TYPE_VARARGS (pfn_type));
0b92b5bb 7532 smash_to_methodptr_type (type, new_type);
c0dd20ea 7533}
1168df01 7534
c906108c 7535/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7536 (definition) to create a type for the structure or union. Fill in
7537 the type's name and general properties; the members will not be
7538 processed until process_structure_type.
c906108c 7539
c767944b
DJ
7540 NOTE: we need to call these functions regardless of whether or not the
7541 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7542 structure or union. This gets the type entered into our set of
7543 user defined types.
7544
7545 However, if the structure is incomplete (an opaque struct/union)
7546 then suppress creating a symbol table entry for it since gdb only
7547 wants to find the one with the complete definition. Note that if
7548 it is complete, we just call new_symbol, which does it's own
7549 checking about whether the struct/union is anonymous or not (and
7550 suppresses creating a symbol table entry itself). */
7551
f792889a 7552static struct type *
134d01f1 7553read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7554{
e7c27a73 7555 struct objfile *objfile = cu->objfile;
c906108c
SS
7556 struct type *type;
7557 struct attribute *attr;
39cbfefa 7558 char *name;
c906108c 7559
348e048f
DE
7560 /* If the definition of this type lives in .debug_types, read that type.
7561 Don't follow DW_AT_specification though, that will take us back up
7562 the chain and we want to go down. */
7563 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7564 if (attr)
7565 {
7566 struct dwarf2_cu *type_cu = cu;
7567 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7568
348e048f
DE
7569 /* We could just recurse on read_structure_type, but we need to call
7570 get_die_type to ensure only one type for this DIE is created.
7571 This is important, for example, because for c++ classes we need
7572 TYPE_NAME set which is only done by new_symbol. Blech. */
7573 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7574
7575 /* TYPE_CU may not be the same as CU.
7576 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7577 return set_die_type (die, type, cu);
7578 }
7579
c0dd20ea 7580 type = alloc_type (objfile);
c906108c 7581 INIT_CPLUS_SPECIFIC (type);
93311388 7582
39cbfefa
DJ
7583 name = dwarf2_name (die, cu);
7584 if (name != NULL)
c906108c 7585 {
987504bb
JJ
7586 if (cu->language == language_cplus
7587 || cu->language == language_java)
63d06c5c 7588 {
3da10d80
KS
7589 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7590
7591 /* dwarf2_full_name might have already finished building the DIE's
7592 type. If so, there is no need to continue. */
7593 if (get_die_type (die, cu) != NULL)
7594 return get_die_type (die, cu);
7595
7596 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7597 if (die->tag == DW_TAG_structure_type
7598 || die->tag == DW_TAG_class_type)
7599 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7600 }
7601 else
7602 {
d8151005
DJ
7603 /* The name is already allocated along with this objfile, so
7604 we don't need to duplicate it for the type. */
94af9270
KS
7605 TYPE_TAG_NAME (type) = (char *) name;
7606 if (die->tag == DW_TAG_class_type)
7607 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7608 }
c906108c
SS
7609 }
7610
7611 if (die->tag == DW_TAG_structure_type)
7612 {
7613 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7614 }
7615 else if (die->tag == DW_TAG_union_type)
7616 {
7617 TYPE_CODE (type) = TYPE_CODE_UNION;
7618 }
7619 else
7620 {
c906108c
SS
7621 TYPE_CODE (type) = TYPE_CODE_CLASS;
7622 }
7623
0cc2414c
TT
7624 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7625 TYPE_DECLARED_CLASS (type) = 1;
7626
e142c38c 7627 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7628 if (attr)
7629 {
7630 TYPE_LENGTH (type) = DW_UNSND (attr);
7631 }
7632 else
7633 {
7634 TYPE_LENGTH (type) = 0;
7635 }
7636
876cecd0 7637 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7638 if (die_is_declaration (die, cu))
876cecd0 7639 TYPE_STUB (type) = 1;
a6c727b2
DJ
7640 else if (attr == NULL && die->child == NULL
7641 && producer_is_realview (cu->producer))
7642 /* RealView does not output the required DW_AT_declaration
7643 on incomplete types. */
7644 TYPE_STUB (type) = 1;
dc718098 7645
c906108c
SS
7646 /* We need to add the type field to the die immediately so we don't
7647 infinitely recurse when dealing with pointers to the structure
0963b4bd 7648 type within the structure itself. */
1c379e20 7649 set_die_type (die, type, cu);
c906108c 7650
7e314c57
JK
7651 /* set_die_type should be already done. */
7652 set_descriptive_type (type, die, cu);
7653
c767944b
DJ
7654 return type;
7655}
7656
7657/* Finish creating a structure or union type, including filling in
7658 its members and creating a symbol for it. */
7659
7660static void
7661process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7662{
7663 struct objfile *objfile = cu->objfile;
7664 struct die_info *child_die = die->child;
7665 struct type *type;
7666
7667 type = get_die_type (die, cu);
7668 if (type == NULL)
7669 type = read_structure_type (die, cu);
7670
e142c38c 7671 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7672 {
7673 struct field_info fi;
7674 struct die_info *child_die;
34eaf542 7675 VEC (symbolp) *template_args = NULL;
c767944b 7676 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7677
7678 memset (&fi, 0, sizeof (struct field_info));
7679
639d11d3 7680 child_die = die->child;
c906108c
SS
7681
7682 while (child_die && child_die->tag)
7683 {
a9a9bd0f
DC
7684 if (child_die->tag == DW_TAG_member
7685 || child_die->tag == DW_TAG_variable)
c906108c 7686 {
a9a9bd0f
DC
7687 /* NOTE: carlton/2002-11-05: A C++ static data member
7688 should be a DW_TAG_member that is a declaration, but
7689 all versions of G++ as of this writing (so through at
7690 least 3.2.1) incorrectly generate DW_TAG_variable
7691 tags for them instead. */
e7c27a73 7692 dwarf2_add_field (&fi, child_die, cu);
c906108c 7693 }
8713b1b1 7694 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7695 {
0963b4bd 7696 /* C++ member function. */
e7c27a73 7697 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7698 }
7699 else if (child_die->tag == DW_TAG_inheritance)
7700 {
7701 /* C++ base class field. */
e7c27a73 7702 dwarf2_add_field (&fi, child_die, cu);
c906108c 7703 }
98751a41
JK
7704 else if (child_die->tag == DW_TAG_typedef)
7705 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7706 else if (child_die->tag == DW_TAG_template_type_param
7707 || child_die->tag == DW_TAG_template_value_param)
7708 {
7709 struct symbol *arg = new_symbol (child_die, NULL, cu);
7710
f1078f66
DJ
7711 if (arg != NULL)
7712 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7713 }
7714
c906108c
SS
7715 child_die = sibling_die (child_die);
7716 }
7717
34eaf542
TT
7718 /* Attach template arguments to type. */
7719 if (! VEC_empty (symbolp, template_args))
7720 {
7721 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7722 TYPE_N_TEMPLATE_ARGUMENTS (type)
7723 = VEC_length (symbolp, template_args);
7724 TYPE_TEMPLATE_ARGUMENTS (type)
7725 = obstack_alloc (&objfile->objfile_obstack,
7726 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7727 * sizeof (struct symbol *)));
7728 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7729 VEC_address (symbolp, template_args),
7730 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7731 * sizeof (struct symbol *)));
7732 VEC_free (symbolp, template_args);
7733 }
7734
c906108c
SS
7735 /* Attach fields and member functions to the type. */
7736 if (fi.nfields)
e7c27a73 7737 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7738 if (fi.nfnfields)
7739 {
e7c27a73 7740 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7741
c5aa993b 7742 /* Get the type which refers to the base class (possibly this
c906108c 7743 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7744 class from the DW_AT_containing_type attribute. This use of
7745 DW_AT_containing_type is a GNU extension. */
c906108c 7746
e142c38c 7747 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7748 {
e7c27a73 7749 struct type *t = die_containing_type (die, cu);
c906108c
SS
7750
7751 TYPE_VPTR_BASETYPE (type) = t;
7752 if (type == t)
7753 {
c906108c
SS
7754 int i;
7755
7756 /* Our own class provides vtbl ptr. */
7757 for (i = TYPE_NFIELDS (t) - 1;
7758 i >= TYPE_N_BASECLASSES (t);
7759 --i)
7760 {
7761 char *fieldname = TYPE_FIELD_NAME (t, i);
7762
1168df01 7763 if (is_vtable_name (fieldname, cu))
c906108c
SS
7764 {
7765 TYPE_VPTR_FIELDNO (type) = i;
7766 break;
7767 }
7768 }
7769
7770 /* Complain if virtual function table field not found. */
7771 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7772 complaint (&symfile_complaints,
3e43a32a
MS
7773 _("virtual function table pointer "
7774 "not found when defining class '%s'"),
4d3c2250
KB
7775 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7776 "");
c906108c
SS
7777 }
7778 else
7779 {
7780 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7781 }
7782 }
f6235d4c
EZ
7783 else if (cu->producer
7784 && strncmp (cu->producer,
7785 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7786 {
7787 /* The IBM XLC compiler does not provide direct indication
7788 of the containing type, but the vtable pointer is
7789 always named __vfp. */
7790
7791 int i;
7792
7793 for (i = TYPE_NFIELDS (type) - 1;
7794 i >= TYPE_N_BASECLASSES (type);
7795 --i)
7796 {
7797 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7798 {
7799 TYPE_VPTR_FIELDNO (type) = i;
7800 TYPE_VPTR_BASETYPE (type) = type;
7801 break;
7802 }
7803 }
7804 }
c906108c 7805 }
98751a41
JK
7806
7807 /* Copy fi.typedef_field_list linked list elements content into the
7808 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7809 if (fi.typedef_field_list)
7810 {
7811 int i = fi.typedef_field_list_count;
7812
a0d7a4ff 7813 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7814 TYPE_TYPEDEF_FIELD_ARRAY (type)
7815 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7816 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7817
7818 /* Reverse the list order to keep the debug info elements order. */
7819 while (--i >= 0)
7820 {
7821 struct typedef_field *dest, *src;
6e70227d 7822
98751a41
JK
7823 dest = &TYPE_TYPEDEF_FIELD (type, i);
7824 src = &fi.typedef_field_list->field;
7825 fi.typedef_field_list = fi.typedef_field_list->next;
7826 *dest = *src;
7827 }
7828 }
c767944b
DJ
7829
7830 do_cleanups (back_to);
eb2a6f42
TT
7831
7832 if (HAVE_CPLUS_STRUCT (type))
7833 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 7834 }
63d06c5c 7835
0b92b5bb
TT
7836 quirk_gcc_member_function_pointer (type, cu->objfile);
7837
90aeadfc
DC
7838 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7839 snapshots) has been known to create a die giving a declaration
7840 for a class that has, as a child, a die giving a definition for a
7841 nested class. So we have to process our children even if the
7842 current die is a declaration. Normally, of course, a declaration
7843 won't have any children at all. */
134d01f1 7844
90aeadfc
DC
7845 while (child_die != NULL && child_die->tag)
7846 {
7847 if (child_die->tag == DW_TAG_member
7848 || child_die->tag == DW_TAG_variable
34eaf542
TT
7849 || child_die->tag == DW_TAG_inheritance
7850 || child_die->tag == DW_TAG_template_value_param
7851 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7852 {
90aeadfc 7853 /* Do nothing. */
134d01f1 7854 }
90aeadfc
DC
7855 else
7856 process_die (child_die, cu);
134d01f1 7857
90aeadfc 7858 child_die = sibling_die (child_die);
134d01f1
DJ
7859 }
7860
fa4028e9
JB
7861 /* Do not consider external references. According to the DWARF standard,
7862 these DIEs are identified by the fact that they have no byte_size
7863 attribute, and a declaration attribute. */
7864 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7865 || !die_is_declaration (die, cu))
c767944b 7866 new_symbol (die, type, cu);
134d01f1
DJ
7867}
7868
7869/* Given a DW_AT_enumeration_type die, set its type. We do not
7870 complete the type's fields yet, or create any symbols. */
c906108c 7871
f792889a 7872static struct type *
134d01f1 7873read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7874{
e7c27a73 7875 struct objfile *objfile = cu->objfile;
c906108c 7876 struct type *type;
c906108c 7877 struct attribute *attr;
0114d602 7878 const char *name;
134d01f1 7879
348e048f
DE
7880 /* If the definition of this type lives in .debug_types, read that type.
7881 Don't follow DW_AT_specification though, that will take us back up
7882 the chain and we want to go down. */
7883 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7884 if (attr)
7885 {
7886 struct dwarf2_cu *type_cu = cu;
7887 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7888
348e048f 7889 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7890
7891 /* TYPE_CU may not be the same as CU.
7892 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7893 return set_die_type (die, type, cu);
7894 }
7895
c906108c
SS
7896 type = alloc_type (objfile);
7897
7898 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7899 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7900 if (name != NULL)
0114d602 7901 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7902
e142c38c 7903 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7904 if (attr)
7905 {
7906 TYPE_LENGTH (type) = DW_UNSND (attr);
7907 }
7908 else
7909 {
7910 TYPE_LENGTH (type) = 0;
7911 }
7912
137033e9
JB
7913 /* The enumeration DIE can be incomplete. In Ada, any type can be
7914 declared as private in the package spec, and then defined only
7915 inside the package body. Such types are known as Taft Amendment
7916 Types. When another package uses such a type, an incomplete DIE
7917 may be generated by the compiler. */
02eb380e 7918 if (die_is_declaration (die, cu))
876cecd0 7919 TYPE_STUB (type) = 1;
02eb380e 7920
f792889a 7921 return set_die_type (die, type, cu);
134d01f1
DJ
7922}
7923
7924/* Given a pointer to a die which begins an enumeration, process all
7925 the dies that define the members of the enumeration, and create the
7926 symbol for the enumeration type.
7927
7928 NOTE: We reverse the order of the element list. */
7929
7930static void
7931process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7932{
f792889a 7933 struct type *this_type;
134d01f1 7934
f792889a
DJ
7935 this_type = get_die_type (die, cu);
7936 if (this_type == NULL)
7937 this_type = read_enumeration_type (die, cu);
9dc481d3 7938
639d11d3 7939 if (die->child != NULL)
c906108c 7940 {
9dc481d3
DE
7941 struct die_info *child_die;
7942 struct symbol *sym;
7943 struct field *fields = NULL;
7944 int num_fields = 0;
7945 int unsigned_enum = 1;
7946 char *name;
7947
639d11d3 7948 child_die = die->child;
c906108c
SS
7949 while (child_die && child_die->tag)
7950 {
7951 if (child_die->tag != DW_TAG_enumerator)
7952 {
e7c27a73 7953 process_die (child_die, cu);
c906108c
SS
7954 }
7955 else
7956 {
39cbfefa
DJ
7957 name = dwarf2_name (child_die, cu);
7958 if (name)
c906108c 7959 {
f792889a 7960 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7961 if (SYMBOL_VALUE (sym) < 0)
7962 unsigned_enum = 0;
7963
7964 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7965 {
7966 fields = (struct field *)
7967 xrealloc (fields,
7968 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7969 * sizeof (struct field));
c906108c
SS
7970 }
7971
3567439c 7972 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7973 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7974 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7975 FIELD_BITSIZE (fields[num_fields]) = 0;
7976
7977 num_fields++;
7978 }
7979 }
7980
7981 child_die = sibling_die (child_die);
7982 }
7983
7984 if (num_fields)
7985 {
f792889a
DJ
7986 TYPE_NFIELDS (this_type) = num_fields;
7987 TYPE_FIELDS (this_type) = (struct field *)
7988 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7989 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7990 sizeof (struct field) * num_fields);
b8c9b27d 7991 xfree (fields);
c906108c
SS
7992 }
7993 if (unsigned_enum)
876cecd0 7994 TYPE_UNSIGNED (this_type) = 1;
c906108c 7995 }
134d01f1 7996
6c83ed52
TT
7997 /* If we are reading an enum from a .debug_types unit, and the enum
7998 is a declaration, and the enum is not the signatured type in the
7999 unit, then we do not want to add a symbol for it. Adding a
8000 symbol would in some cases obscure the true definition of the
8001 enum, giving users an incomplete type when the definition is
8002 actually available. Note that we do not want to do this for all
8003 enums which are just declarations, because C++0x allows forward
8004 enum declarations. */
8005 if (cu->per_cu->debug_type_section
8006 && die_is_declaration (die, cu))
8007 {
8008 struct signatured_type *type_sig;
8009
8010 type_sig
8011 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8012 cu->per_cu->debug_type_section,
8013 cu->per_cu->offset);
8014 if (type_sig->type_offset != die->offset)
8015 return;
8016 }
8017
f792889a 8018 new_symbol (die, this_type, cu);
c906108c
SS
8019}
8020
8021/* Extract all information from a DW_TAG_array_type DIE and put it in
8022 the DIE's type field. For now, this only handles one dimensional
8023 arrays. */
8024
f792889a 8025static struct type *
e7c27a73 8026read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8027{
e7c27a73 8028 struct objfile *objfile = cu->objfile;
c906108c 8029 struct die_info *child_die;
7e314c57 8030 struct type *type;
c906108c
SS
8031 struct type *element_type, *range_type, *index_type;
8032 struct type **range_types = NULL;
8033 struct attribute *attr;
8034 int ndim = 0;
8035 struct cleanup *back_to;
39cbfefa 8036 char *name;
c906108c 8037
e7c27a73 8038 element_type = die_type (die, cu);
c906108c 8039
7e314c57
JK
8040 /* The die_type call above may have already set the type for this DIE. */
8041 type = get_die_type (die, cu);
8042 if (type)
8043 return type;
8044
c906108c
SS
8045 /* Irix 6.2 native cc creates array types without children for
8046 arrays with unspecified length. */
639d11d3 8047 if (die->child == NULL)
c906108c 8048 {
46bf5051 8049 index_type = objfile_type (objfile)->builtin_int;
c906108c 8050 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
8051 type = create_array_type (NULL, element_type, range_type);
8052 return set_die_type (die, type, cu);
c906108c
SS
8053 }
8054
8055 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 8056 child_die = die->child;
c906108c
SS
8057 while (child_die && child_die->tag)
8058 {
8059 if (child_die->tag == DW_TAG_subrange_type)
8060 {
f792889a 8061 struct type *child_type = read_type_die (child_die, cu);
9a619af0 8062
f792889a 8063 if (child_type != NULL)
a02abb62 8064 {
0963b4bd
MS
8065 /* The range type was succesfully read. Save it for the
8066 array type creation. */
a02abb62
JB
8067 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8068 {
8069 range_types = (struct type **)
8070 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8071 * sizeof (struct type *));
8072 if (ndim == 0)
8073 make_cleanup (free_current_contents, &range_types);
8074 }
f792889a 8075 range_types[ndim++] = child_type;
a02abb62 8076 }
c906108c
SS
8077 }
8078 child_die = sibling_die (child_die);
8079 }
8080
8081 /* Dwarf2 dimensions are output from left to right, create the
8082 necessary array types in backwards order. */
7ca2d3a3 8083
c906108c 8084 type = element_type;
7ca2d3a3
DL
8085
8086 if (read_array_order (die, cu) == DW_ORD_col_major)
8087 {
8088 int i = 0;
9a619af0 8089
7ca2d3a3
DL
8090 while (i < ndim)
8091 type = create_array_type (NULL, type, range_types[i++]);
8092 }
8093 else
8094 {
8095 while (ndim-- > 0)
8096 type = create_array_type (NULL, type, range_types[ndim]);
8097 }
c906108c 8098
f5f8a009
EZ
8099 /* Understand Dwarf2 support for vector types (like they occur on
8100 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8101 array type. This is not part of the Dwarf2/3 standard yet, but a
8102 custom vendor extension. The main difference between a regular
8103 array and the vector variant is that vectors are passed by value
8104 to functions. */
e142c38c 8105 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 8106 if (attr)
ea37ba09 8107 make_vector_type (type);
f5f8a009 8108
dbc98a8b
KW
8109 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8110 implementation may choose to implement triple vectors using this
8111 attribute. */
8112 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8113 if (attr)
8114 {
8115 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8116 TYPE_LENGTH (type) = DW_UNSND (attr);
8117 else
3e43a32a
MS
8118 complaint (&symfile_complaints,
8119 _("DW_AT_byte_size for array type smaller "
8120 "than the total size of elements"));
dbc98a8b
KW
8121 }
8122
39cbfefa
DJ
8123 name = dwarf2_name (die, cu);
8124 if (name)
8125 TYPE_NAME (type) = name;
6e70227d 8126
0963b4bd 8127 /* Install the type in the die. */
7e314c57
JK
8128 set_die_type (die, type, cu);
8129
8130 /* set_die_type should be already done. */
b4ba55a1
JB
8131 set_descriptive_type (type, die, cu);
8132
c906108c
SS
8133 do_cleanups (back_to);
8134
7e314c57 8135 return type;
c906108c
SS
8136}
8137
7ca2d3a3 8138static enum dwarf_array_dim_ordering
6e70227d 8139read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
8140{
8141 struct attribute *attr;
8142
8143 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8144
8145 if (attr) return DW_SND (attr);
8146
0963b4bd
MS
8147 /* GNU F77 is a special case, as at 08/2004 array type info is the
8148 opposite order to the dwarf2 specification, but data is still
8149 laid out as per normal fortran.
7ca2d3a3 8150
0963b4bd
MS
8151 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8152 version checking. */
7ca2d3a3 8153
905e0470
PM
8154 if (cu->language == language_fortran
8155 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
8156 {
8157 return DW_ORD_row_major;
8158 }
8159
6e70227d 8160 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
8161 {
8162 case array_column_major:
8163 return DW_ORD_col_major;
8164 case array_row_major:
8165 default:
8166 return DW_ORD_row_major;
8167 };
8168}
8169
72019c9c 8170/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 8171 the DIE's type field. */
72019c9c 8172
f792889a 8173static struct type *
72019c9c
GM
8174read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8175{
7e314c57
JK
8176 struct type *domain_type, *set_type;
8177 struct attribute *attr;
f792889a 8178
7e314c57
JK
8179 domain_type = die_type (die, cu);
8180
8181 /* The die_type call above may have already set the type for this DIE. */
8182 set_type = get_die_type (die, cu);
8183 if (set_type)
8184 return set_type;
8185
8186 set_type = create_set_type (NULL, domain_type);
8187
8188 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
8189 if (attr)
8190 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 8191
f792889a 8192 return set_die_type (die, set_type, cu);
72019c9c 8193}
7ca2d3a3 8194
c906108c
SS
8195/* First cut: install each common block member as a global variable. */
8196
8197static void
e7c27a73 8198read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8199{
8200 struct die_info *child_die;
8201 struct attribute *attr;
8202 struct symbol *sym;
8203 CORE_ADDR base = (CORE_ADDR) 0;
8204
e142c38c 8205 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8206 if (attr)
8207 {
0963b4bd 8208 /* Support the .debug_loc offsets. */
8e19ed76
PS
8209 if (attr_form_is_block (attr))
8210 {
e7c27a73 8211 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 8212 }
3690dd37 8213 else if (attr_form_is_section_offset (attr))
8e19ed76 8214 {
4d3c2250 8215 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8216 }
8217 else
8218 {
4d3c2250
KB
8219 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8220 "common block member");
8e19ed76 8221 }
c906108c 8222 }
639d11d3 8223 if (die->child != NULL)
c906108c 8224 {
639d11d3 8225 child_die = die->child;
c906108c
SS
8226 while (child_die && child_die->tag)
8227 {
74ac6d43
TT
8228 LONGEST offset;
8229
e7c27a73 8230 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
8231 if (sym != NULL
8232 && handle_data_member_location (child_die, cu, &offset))
c906108c 8233 {
74ac6d43 8234 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
8235 add_symbol_to_list (sym, &global_symbols);
8236 }
8237 child_die = sibling_die (child_die);
8238 }
8239 }
8240}
8241
0114d602 8242/* Create a type for a C++ namespace. */
d9fa45fe 8243
0114d602
DJ
8244static struct type *
8245read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 8246{
e7c27a73 8247 struct objfile *objfile = cu->objfile;
0114d602 8248 const char *previous_prefix, *name;
9219021c 8249 int is_anonymous;
0114d602
DJ
8250 struct type *type;
8251
8252 /* For extensions, reuse the type of the original namespace. */
8253 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8254 {
8255 struct die_info *ext_die;
8256 struct dwarf2_cu *ext_cu = cu;
9a619af0 8257
0114d602
DJ
8258 ext_die = dwarf2_extension (die, &ext_cu);
8259 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
8260
8261 /* EXT_CU may not be the same as CU.
8262 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
8263 return set_die_type (die, type, cu);
8264 }
9219021c 8265
e142c38c 8266 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
8267
8268 /* Now build the name of the current namespace. */
8269
0114d602
DJ
8270 previous_prefix = determine_prefix (die, cu);
8271 if (previous_prefix[0] != '\0')
8272 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 8273 previous_prefix, name, 0, cu);
0114d602
DJ
8274
8275 /* Create the type. */
8276 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8277 objfile);
8278 TYPE_NAME (type) = (char *) name;
8279 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8280
60531b24 8281 return set_die_type (die, type, cu);
0114d602
DJ
8282}
8283
8284/* Read a C++ namespace. */
8285
8286static void
8287read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8288{
8289 struct objfile *objfile = cu->objfile;
0114d602 8290 int is_anonymous;
9219021c 8291
5c4e30ca
DC
8292 /* Add a symbol associated to this if we haven't seen the namespace
8293 before. Also, add a using directive if it's an anonymous
8294 namespace. */
9219021c 8295
f2f0e013 8296 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
8297 {
8298 struct type *type;
8299
0114d602 8300 type = read_type_die (die, cu);
e7c27a73 8301 new_symbol (die, type, cu);
5c4e30ca 8302
e8e80198 8303 namespace_name (die, &is_anonymous, cu);
5c4e30ca 8304 if (is_anonymous)
0114d602
DJ
8305 {
8306 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 8307
c0cc3a76 8308 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 8309 NULL, NULL, &objfile->objfile_obstack);
0114d602 8310 }
5c4e30ca 8311 }
9219021c 8312
639d11d3 8313 if (die->child != NULL)
d9fa45fe 8314 {
639d11d3 8315 struct die_info *child_die = die->child;
6e70227d 8316
d9fa45fe
DC
8317 while (child_die && child_die->tag)
8318 {
e7c27a73 8319 process_die (child_die, cu);
d9fa45fe
DC
8320 child_die = sibling_die (child_die);
8321 }
8322 }
38d518c9
EZ
8323}
8324
f55ee35c
JK
8325/* Read a Fortran module as type. This DIE can be only a declaration used for
8326 imported module. Still we need that type as local Fortran "use ... only"
8327 declaration imports depend on the created type in determine_prefix. */
8328
8329static struct type *
8330read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8331{
8332 struct objfile *objfile = cu->objfile;
8333 char *module_name;
8334 struct type *type;
8335
8336 module_name = dwarf2_name (die, cu);
8337 if (!module_name)
3e43a32a
MS
8338 complaint (&symfile_complaints,
8339 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
8340 die->offset);
8341 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8342
8343 /* determine_prefix uses TYPE_TAG_NAME. */
8344 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8345
8346 return set_die_type (die, type, cu);
8347}
8348
5d7cb8df
JK
8349/* Read a Fortran module. */
8350
8351static void
8352read_module (struct die_info *die, struct dwarf2_cu *cu)
8353{
8354 struct die_info *child_die = die->child;
8355
5d7cb8df
JK
8356 while (child_die && child_die->tag)
8357 {
8358 process_die (child_die, cu);
8359 child_die = sibling_die (child_die);
8360 }
8361}
8362
38d518c9
EZ
8363/* Return the name of the namespace represented by DIE. Set
8364 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8365 namespace. */
8366
8367static const char *
e142c38c 8368namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8369{
8370 struct die_info *current_die;
8371 const char *name = NULL;
8372
8373 /* Loop through the extensions until we find a name. */
8374
8375 for (current_die = die;
8376 current_die != NULL;
f2f0e013 8377 current_die = dwarf2_extension (die, &cu))
38d518c9 8378 {
e142c38c 8379 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8380 if (name != NULL)
8381 break;
8382 }
8383
8384 /* Is it an anonymous namespace? */
8385
8386 *is_anonymous = (name == NULL);
8387 if (*is_anonymous)
2b1dbab0 8388 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8389
8390 return name;
d9fa45fe
DC
8391}
8392
c906108c
SS
8393/* Extract all information from a DW_TAG_pointer_type DIE and add to
8394 the user defined type vector. */
8395
f792889a 8396static struct type *
e7c27a73 8397read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8398{
5e2b427d 8399 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8400 struct comp_unit_head *cu_header = &cu->header;
c906108c 8401 struct type *type;
8b2dbe47
KB
8402 struct attribute *attr_byte_size;
8403 struct attribute *attr_address_class;
8404 int byte_size, addr_class;
7e314c57
JK
8405 struct type *target_type;
8406
8407 target_type = die_type (die, cu);
c906108c 8408
7e314c57
JK
8409 /* The die_type call above may have already set the type for this DIE. */
8410 type = get_die_type (die, cu);
8411 if (type)
8412 return type;
8413
8414 type = lookup_pointer_type (target_type);
8b2dbe47 8415
e142c38c 8416 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8417 if (attr_byte_size)
8418 byte_size = DW_UNSND (attr_byte_size);
c906108c 8419 else
8b2dbe47
KB
8420 byte_size = cu_header->addr_size;
8421
e142c38c 8422 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8423 if (attr_address_class)
8424 addr_class = DW_UNSND (attr_address_class);
8425 else
8426 addr_class = DW_ADDR_none;
8427
8428 /* If the pointer size or address class is different than the
8429 default, create a type variant marked as such and set the
8430 length accordingly. */
8431 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8432 {
5e2b427d 8433 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8434 {
8435 int type_flags;
8436
849957d9 8437 type_flags = gdbarch_address_class_type_flags
5e2b427d 8438 (gdbarch, byte_size, addr_class);
876cecd0
TT
8439 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8440 == 0);
8b2dbe47
KB
8441 type = make_type_with_address_space (type, type_flags);
8442 }
8443 else if (TYPE_LENGTH (type) != byte_size)
8444 {
3e43a32a
MS
8445 complaint (&symfile_complaints,
8446 _("invalid pointer size %d"), byte_size);
8b2dbe47 8447 }
6e70227d 8448 else
9a619af0
MS
8449 {
8450 /* Should we also complain about unhandled address classes? */
8451 }
c906108c 8452 }
8b2dbe47
KB
8453
8454 TYPE_LENGTH (type) = byte_size;
f792889a 8455 return set_die_type (die, type, cu);
c906108c
SS
8456}
8457
8458/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8459 the user defined type vector. */
8460
f792889a 8461static struct type *
e7c27a73 8462read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8463{
8464 struct type *type;
8465 struct type *to_type;
8466 struct type *domain;
8467
e7c27a73
DJ
8468 to_type = die_type (die, cu);
8469 domain = die_containing_type (die, cu);
0d5de010 8470
7e314c57
JK
8471 /* The calls above may have already set the type for this DIE. */
8472 type = get_die_type (die, cu);
8473 if (type)
8474 return type;
8475
0d5de010
DJ
8476 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8477 type = lookup_methodptr_type (to_type);
8478 else
8479 type = lookup_memberptr_type (to_type, domain);
c906108c 8480
f792889a 8481 return set_die_type (die, type, cu);
c906108c
SS
8482}
8483
8484/* Extract all information from a DW_TAG_reference_type DIE and add to
8485 the user defined type vector. */
8486
f792889a 8487static struct type *
e7c27a73 8488read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8489{
e7c27a73 8490 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8491 struct type *type, *target_type;
c906108c
SS
8492 struct attribute *attr;
8493
7e314c57
JK
8494 target_type = die_type (die, cu);
8495
8496 /* The die_type call above may have already set the type for this DIE. */
8497 type = get_die_type (die, cu);
8498 if (type)
8499 return type;
8500
8501 type = lookup_reference_type (target_type);
e142c38c 8502 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8503 if (attr)
8504 {
8505 TYPE_LENGTH (type) = DW_UNSND (attr);
8506 }
8507 else
8508 {
107d2387 8509 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8510 }
f792889a 8511 return set_die_type (die, type, cu);
c906108c
SS
8512}
8513
f792889a 8514static struct type *
e7c27a73 8515read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8516{
f792889a 8517 struct type *base_type, *cv_type;
c906108c 8518
e7c27a73 8519 base_type = die_type (die, cu);
7e314c57
JK
8520
8521 /* The die_type call above may have already set the type for this DIE. */
8522 cv_type = get_die_type (die, cu);
8523 if (cv_type)
8524 return cv_type;
8525
2f608a3a
KW
8526 /* In case the const qualifier is applied to an array type, the element type
8527 is so qualified, not the array type (section 6.7.3 of C99). */
8528 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8529 {
8530 struct type *el_type, *inner_array;
8531
8532 base_type = copy_type (base_type);
8533 inner_array = base_type;
8534
8535 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8536 {
8537 TYPE_TARGET_TYPE (inner_array) =
8538 copy_type (TYPE_TARGET_TYPE (inner_array));
8539 inner_array = TYPE_TARGET_TYPE (inner_array);
8540 }
8541
8542 el_type = TYPE_TARGET_TYPE (inner_array);
8543 TYPE_TARGET_TYPE (inner_array) =
8544 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8545
8546 return set_die_type (die, base_type, cu);
8547 }
8548
f792889a
DJ
8549 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8550 return set_die_type (die, cv_type, cu);
c906108c
SS
8551}
8552
f792889a 8553static struct type *
e7c27a73 8554read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8555{
f792889a 8556 struct type *base_type, *cv_type;
c906108c 8557
e7c27a73 8558 base_type = die_type (die, cu);
7e314c57
JK
8559
8560 /* The die_type call above may have already set the type for this DIE. */
8561 cv_type = get_die_type (die, cu);
8562 if (cv_type)
8563 return cv_type;
8564
f792889a
DJ
8565 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8566 return set_die_type (die, cv_type, cu);
c906108c
SS
8567}
8568
8569/* Extract all information from a DW_TAG_string_type DIE and add to
8570 the user defined type vector. It isn't really a user defined type,
8571 but it behaves like one, with other DIE's using an AT_user_def_type
8572 attribute to reference it. */
8573
f792889a 8574static struct type *
e7c27a73 8575read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8576{
e7c27a73 8577 struct objfile *objfile = cu->objfile;
3b7538c0 8578 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8579 struct type *type, *range_type, *index_type, *char_type;
8580 struct attribute *attr;
8581 unsigned int length;
8582
e142c38c 8583 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8584 if (attr)
8585 {
8586 length = DW_UNSND (attr);
8587 }
8588 else
8589 {
0963b4bd 8590 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8591 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8592 if (attr)
8593 {
8594 length = DW_UNSND (attr);
8595 }
8596 else
8597 {
8598 length = 1;
8599 }
c906108c 8600 }
6ccb9162 8601
46bf5051 8602 index_type = objfile_type (objfile)->builtin_int;
c906108c 8603 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8604 char_type = language_string_char_type (cu->language_defn, gdbarch);
8605 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8606
f792889a 8607 return set_die_type (die, type, cu);
c906108c
SS
8608}
8609
8610/* Handle DIES due to C code like:
8611
8612 struct foo
c5aa993b
JM
8613 {
8614 int (*funcp)(int a, long l);
8615 int b;
8616 };
c906108c 8617
0963b4bd 8618 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8619
f792889a 8620static struct type *
e7c27a73 8621read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8622{
0963b4bd
MS
8623 struct type *type; /* Type that this function returns. */
8624 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8625 struct attribute *attr;
8626
e7c27a73 8627 type = die_type (die, cu);
7e314c57
JK
8628
8629 /* The die_type call above may have already set the type for this DIE. */
8630 ftype = get_die_type (die, cu);
8631 if (ftype)
8632 return ftype;
8633
0c8b41f1 8634 ftype = lookup_function_type (type);
c906108c 8635
5b8101ae 8636 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8637 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8638 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8639 || cu->language == language_cplus
5b8101ae
PM
8640 || cu->language == language_java
8641 || cu->language == language_pascal)
876cecd0 8642 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8643 else if (producer_is_realview (cu->producer))
8644 /* RealView does not emit DW_AT_prototyped. We can not
8645 distinguish prototyped and unprototyped functions; default to
8646 prototyped, since that is more common in modern code (and
8647 RealView warns about unprototyped functions). */
8648 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8649
c055b101
CV
8650 /* Store the calling convention in the type if it's available in
8651 the subroutine die. Otherwise set the calling convention to
8652 the default value DW_CC_normal. */
8653 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8654 if (attr)
8655 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8656 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8657 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8658 else
8659 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8660
8661 /* We need to add the subroutine type to the die immediately so
8662 we don't infinitely recurse when dealing with parameters
0963b4bd 8663 declared as the same subroutine type. */
76c10ea2 8664 set_die_type (die, ftype, cu);
6e70227d 8665
639d11d3 8666 if (die->child != NULL)
c906108c 8667 {
8072405b 8668 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 8669 struct die_info *child_die;
8072405b 8670 int nparams, iparams;
c906108c
SS
8671
8672 /* Count the number of parameters.
8673 FIXME: GDB currently ignores vararg functions, but knows about
8674 vararg member functions. */
8072405b 8675 nparams = 0;
639d11d3 8676 child_die = die->child;
c906108c
SS
8677 while (child_die && child_die->tag)
8678 {
8679 if (child_die->tag == DW_TAG_formal_parameter)
8680 nparams++;
8681 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8682 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8683 child_die = sibling_die (child_die);
8684 }
8685
8686 /* Allocate storage for parameters and fill them in. */
8687 TYPE_NFIELDS (ftype) = nparams;
8688 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8689 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8690
8072405b
JK
8691 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8692 even if we error out during the parameters reading below. */
8693 for (iparams = 0; iparams < nparams; iparams++)
8694 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8695
8696 iparams = 0;
639d11d3 8697 child_die = die->child;
c906108c
SS
8698 while (child_die && child_die->tag)
8699 {
8700 if (child_die->tag == DW_TAG_formal_parameter)
8701 {
3ce3b1ba
PA
8702 struct type *arg_type;
8703
8704 /* DWARF version 2 has no clean way to discern C++
8705 static and non-static member functions. G++ helps
8706 GDB by marking the first parameter for non-static
8707 member functions (which is the this pointer) as
8708 artificial. We pass this information to
8709 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8710
8711 DWARF version 3 added DW_AT_object_pointer, which GCC
8712 4.5 does not yet generate. */
e142c38c 8713 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8714 if (attr)
8715 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8716 else
418835cc
KS
8717 {
8718 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8719
8720 /* GCC/43521: In java, the formal parameter
8721 "this" is sometimes not marked with DW_AT_artificial. */
8722 if (cu->language == language_java)
8723 {
8724 const char *name = dwarf2_name (child_die, cu);
9a619af0 8725
418835cc
KS
8726 if (name && !strcmp (name, "this"))
8727 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8728 }
8729 }
3ce3b1ba
PA
8730 arg_type = die_type (child_die, cu);
8731
8732 /* RealView does not mark THIS as const, which the testsuite
8733 expects. GCC marks THIS as const in method definitions,
8734 but not in the class specifications (GCC PR 43053). */
8735 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8736 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8737 {
8738 int is_this = 0;
8739 struct dwarf2_cu *arg_cu = cu;
8740 const char *name = dwarf2_name (child_die, cu);
8741
8742 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8743 if (attr)
8744 {
8745 /* If the compiler emits this, use it. */
8746 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8747 is_this = 1;
8748 }
8749 else if (name && strcmp (name, "this") == 0)
8750 /* Function definitions will have the argument names. */
8751 is_this = 1;
8752 else if (name == NULL && iparams == 0)
8753 /* Declarations may not have the names, so like
8754 elsewhere in GDB, assume an artificial first
8755 argument is "this". */
8756 is_this = 1;
8757
8758 if (is_this)
8759 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8760 arg_type, 0);
8761 }
8762
8763 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8764 iparams++;
8765 }
8766 child_die = sibling_die (child_die);
8767 }
8768 }
8769
76c10ea2 8770 return ftype;
c906108c
SS
8771}
8772
f792889a 8773static struct type *
e7c27a73 8774read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8775{
e7c27a73 8776 struct objfile *objfile = cu->objfile;
0114d602 8777 const char *name = NULL;
f792889a 8778 struct type *this_type;
c906108c 8779
94af9270 8780 name = dwarf2_full_name (NULL, die, cu);
f792889a 8781 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8782 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8783 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8784 set_die_type (die, this_type, cu);
8785 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8786 return this_type;
c906108c
SS
8787}
8788
8789/* Find a representation of a given base type and install
8790 it in the TYPE field of the die. */
8791
f792889a 8792static struct type *
e7c27a73 8793read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8794{
e7c27a73 8795 struct objfile *objfile = cu->objfile;
c906108c
SS
8796 struct type *type;
8797 struct attribute *attr;
8798 int encoding = 0, size = 0;
39cbfefa 8799 char *name;
6ccb9162
UW
8800 enum type_code code = TYPE_CODE_INT;
8801 int type_flags = 0;
8802 struct type *target_type = NULL;
c906108c 8803
e142c38c 8804 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8805 if (attr)
8806 {
8807 encoding = DW_UNSND (attr);
8808 }
e142c38c 8809 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8810 if (attr)
8811 {
8812 size = DW_UNSND (attr);
8813 }
39cbfefa 8814 name = dwarf2_name (die, cu);
6ccb9162 8815 if (!name)
c906108c 8816 {
6ccb9162
UW
8817 complaint (&symfile_complaints,
8818 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8819 }
6ccb9162
UW
8820
8821 switch (encoding)
c906108c 8822 {
6ccb9162
UW
8823 case DW_ATE_address:
8824 /* Turn DW_ATE_address into a void * pointer. */
8825 code = TYPE_CODE_PTR;
8826 type_flags |= TYPE_FLAG_UNSIGNED;
8827 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8828 break;
8829 case DW_ATE_boolean:
8830 code = TYPE_CODE_BOOL;
8831 type_flags |= TYPE_FLAG_UNSIGNED;
8832 break;
8833 case DW_ATE_complex_float:
8834 code = TYPE_CODE_COMPLEX;
8835 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8836 break;
8837 case DW_ATE_decimal_float:
8838 code = TYPE_CODE_DECFLOAT;
8839 break;
8840 case DW_ATE_float:
8841 code = TYPE_CODE_FLT;
8842 break;
8843 case DW_ATE_signed:
8844 break;
8845 case DW_ATE_unsigned:
8846 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8847 if (cu->language == language_fortran
8848 && name
8849 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8850 code = TYPE_CODE_CHAR;
6ccb9162
UW
8851 break;
8852 case DW_ATE_signed_char:
6e70227d 8853 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8854 || cu->language == language_pascal
8855 || cu->language == language_fortran)
6ccb9162
UW
8856 code = TYPE_CODE_CHAR;
8857 break;
8858 case DW_ATE_unsigned_char:
868a0084 8859 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8860 || cu->language == language_pascal
8861 || cu->language == language_fortran)
6ccb9162
UW
8862 code = TYPE_CODE_CHAR;
8863 type_flags |= TYPE_FLAG_UNSIGNED;
8864 break;
75079b2b
TT
8865 case DW_ATE_UTF:
8866 /* We just treat this as an integer and then recognize the
8867 type by name elsewhere. */
8868 break;
8869
6ccb9162
UW
8870 default:
8871 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8872 dwarf_type_encoding_name (encoding));
8873 break;
c906108c 8874 }
6ccb9162 8875
0114d602
DJ
8876 type = init_type (code, size, type_flags, NULL, objfile);
8877 TYPE_NAME (type) = name;
6ccb9162
UW
8878 TYPE_TARGET_TYPE (type) = target_type;
8879
0114d602 8880 if (name && strcmp (name, "char") == 0)
876cecd0 8881 TYPE_NOSIGN (type) = 1;
0114d602 8882
f792889a 8883 return set_die_type (die, type, cu);
c906108c
SS
8884}
8885
a02abb62
JB
8886/* Read the given DW_AT_subrange DIE. */
8887
f792889a 8888static struct type *
a02abb62
JB
8889read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8890{
8891 struct type *base_type;
8892 struct type *range_type;
8893 struct attribute *attr;
43bbcdc2
PH
8894 LONGEST low = 0;
8895 LONGEST high = -1;
39cbfefa 8896 char *name;
43bbcdc2 8897 LONGEST negative_mask;
e77813c8 8898
a02abb62 8899 base_type = die_type (die, cu);
953ac07e
JK
8900 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8901 check_typedef (base_type);
a02abb62 8902
7e314c57
JK
8903 /* The die_type call above may have already set the type for this DIE. */
8904 range_type = get_die_type (die, cu);
8905 if (range_type)
8906 return range_type;
8907
e142c38c 8908 if (cu->language == language_fortran)
6e70227d 8909 {
a02abb62
JB
8910 /* FORTRAN implies a lower bound of 1, if not given. */
8911 low = 1;
8912 }
8913
dd5e6932
DJ
8914 /* FIXME: For variable sized arrays either of these could be
8915 a variable rather than a constant value. We'll allow it,
8916 but we don't know how to handle it. */
e142c38c 8917 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8918 if (attr)
8919 low = dwarf2_get_attr_constant_value (attr, 0);
8920
e142c38c 8921 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8922 if (attr)
6e70227d 8923 {
d48323d8 8924 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
8925 {
8926 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8927 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8928 FIXME: GDB does not yet know how to handle dynamic
8929 arrays properly, treat them as arrays with unspecified
8930 length for now.
8931
8932 FIXME: jimb/2003-09-22: GDB does not really know
8933 how to handle arrays of unspecified length
8934 either; we just represent them as zero-length
8935 arrays. Choose an appropriate upper bound given
8936 the lower bound we've computed above. */
8937 high = low - 1;
8938 }
8939 else
8940 high = dwarf2_get_attr_constant_value (attr, 1);
8941 }
e77813c8
PM
8942 else
8943 {
8944 attr = dwarf2_attr (die, DW_AT_count, cu);
8945 if (attr)
8946 {
8947 int count = dwarf2_get_attr_constant_value (attr, 1);
8948 high = low + count - 1;
8949 }
c2ff108b
JK
8950 else
8951 {
8952 /* Unspecified array length. */
8953 high = low - 1;
8954 }
e77813c8
PM
8955 }
8956
8957 /* Dwarf-2 specifications explicitly allows to create subrange types
8958 without specifying a base type.
8959 In that case, the base type must be set to the type of
8960 the lower bound, upper bound or count, in that order, if any of these
8961 three attributes references an object that has a type.
8962 If no base type is found, the Dwarf-2 specifications say that
8963 a signed integer type of size equal to the size of an address should
8964 be used.
8965 For the following C code: `extern char gdb_int [];'
8966 GCC produces an empty range DIE.
8967 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 8968 high bound or count are not yet handled by this code. */
e77813c8
PM
8969 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8970 {
8971 struct objfile *objfile = cu->objfile;
8972 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8973 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8974 struct type *int_type = objfile_type (objfile)->builtin_int;
8975
8976 /* Test "int", "long int", and "long long int" objfile types,
8977 and select the first one having a size above or equal to the
8978 architecture address size. */
8979 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8980 base_type = int_type;
8981 else
8982 {
8983 int_type = objfile_type (objfile)->builtin_long;
8984 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8985 base_type = int_type;
8986 else
8987 {
8988 int_type = objfile_type (objfile)->builtin_long_long;
8989 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8990 base_type = int_type;
8991 }
8992 }
8993 }
a02abb62 8994
6e70227d 8995 negative_mask =
43bbcdc2
PH
8996 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8997 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8998 low |= negative_mask;
8999 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9000 high |= negative_mask;
9001
a02abb62
JB
9002 range_type = create_range_type (NULL, base_type, low, high);
9003
bbb0eef6
JK
9004 /* Mark arrays with dynamic length at least as an array of unspecified
9005 length. GDB could check the boundary but before it gets implemented at
9006 least allow accessing the array elements. */
d48323d8 9007 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
9008 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9009
c2ff108b
JK
9010 /* Ada expects an empty array on no boundary attributes. */
9011 if (attr == NULL && cu->language != language_ada)
9012 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9013
39cbfefa
DJ
9014 name = dwarf2_name (die, cu);
9015 if (name)
9016 TYPE_NAME (range_type) = name;
6e70227d 9017
e142c38c 9018 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
9019 if (attr)
9020 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9021
7e314c57
JK
9022 set_die_type (die, range_type, cu);
9023
9024 /* set_die_type should be already done. */
b4ba55a1
JB
9025 set_descriptive_type (range_type, die, cu);
9026
7e314c57 9027 return range_type;
a02abb62 9028}
6e70227d 9029
f792889a 9030static struct type *
81a17f79
JB
9031read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9032{
9033 struct type *type;
81a17f79 9034
81a17f79
JB
9035 /* For now, we only support the C meaning of an unspecified type: void. */
9036
0114d602
DJ
9037 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9038 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 9039
f792889a 9040 return set_die_type (die, type, cu);
81a17f79 9041}
a02abb62 9042
51545339
DJ
9043/* Trivial hash function for die_info: the hash value of a DIE
9044 is its offset in .debug_info for this objfile. */
9045
9046static hashval_t
9047die_hash (const void *item)
9048{
9049 const struct die_info *die = item;
9a619af0 9050
51545339
DJ
9051 return die->offset;
9052}
9053
9054/* Trivial comparison function for die_info structures: two DIEs
9055 are equal if they have the same offset. */
9056
9057static int
9058die_eq (const void *item_lhs, const void *item_rhs)
9059{
9060 const struct die_info *die_lhs = item_lhs;
9061 const struct die_info *die_rhs = item_rhs;
9a619af0 9062
51545339
DJ
9063 return die_lhs->offset == die_rhs->offset;
9064}
9065
c906108c
SS
9066/* Read a whole compilation unit into a linked list of dies. */
9067
f9aca02d 9068static struct die_info *
93311388 9069read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 9070{
93311388 9071 struct die_reader_specs reader_specs;
98bfdba5 9072 int read_abbrevs = 0;
1d9ec526 9073 struct cleanup *back_to = NULL;
98bfdba5
PA
9074 struct die_info *die;
9075
9076 if (cu->dwarf2_abbrevs == NULL)
9077 {
9078 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
9079 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9080 read_abbrevs = 1;
9081 }
93311388 9082
348e048f 9083 gdb_assert (cu->die_hash == NULL);
51545339
DJ
9084 cu->die_hash
9085 = htab_create_alloc_ex (cu->header.length / 12,
9086 die_hash,
9087 die_eq,
9088 NULL,
9089 &cu->comp_unit_obstack,
9090 hashtab_obstack_allocate,
9091 dummy_obstack_deallocate);
9092
93311388
DE
9093 init_cu_die_reader (&reader_specs, cu);
9094
98bfdba5
PA
9095 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9096
9097 if (read_abbrevs)
9098 do_cleanups (back_to);
9099
9100 return die;
639d11d3
DC
9101}
9102
d97bc12b
DE
9103/* Main entry point for reading a DIE and all children.
9104 Read the DIE and dump it if requested. */
9105
9106static struct die_info *
93311388
DE
9107read_die_and_children (const struct die_reader_specs *reader,
9108 gdb_byte *info_ptr,
d97bc12b
DE
9109 gdb_byte **new_info_ptr,
9110 struct die_info *parent)
9111{
93311388 9112 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
9113 new_info_ptr, parent);
9114
9115 if (dwarf2_die_debug)
9116 {
348e048f
DE
9117 fprintf_unfiltered (gdb_stdlog,
9118 "\nRead die from %s of %s:\n",
8b70b953
TT
9119 (reader->cu->per_cu->debug_type_section
9120 ? ".debug_types"
9121 : ".debug_info"),
348e048f 9122 reader->abfd->filename);
d97bc12b
DE
9123 dump_die (result, dwarf2_die_debug);
9124 }
9125
9126 return result;
9127}
9128
639d11d3
DC
9129/* Read a single die and all its descendents. Set the die's sibling
9130 field to NULL; set other fields in the die correctly, and set all
9131 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9132 location of the info_ptr after reading all of those dies. PARENT
9133 is the parent of the die in question. */
9134
9135static struct die_info *
93311388
DE
9136read_die_and_children_1 (const struct die_reader_specs *reader,
9137 gdb_byte *info_ptr,
d97bc12b
DE
9138 gdb_byte **new_info_ptr,
9139 struct die_info *parent)
639d11d3
DC
9140{
9141 struct die_info *die;
fe1b8b76 9142 gdb_byte *cur_ptr;
639d11d3
DC
9143 int has_children;
9144
93311388 9145 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
9146 if (die == NULL)
9147 {
9148 *new_info_ptr = cur_ptr;
9149 return NULL;
9150 }
93311388 9151 store_in_ref_table (die, reader->cu);
639d11d3
DC
9152
9153 if (has_children)
348e048f 9154 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
9155 else
9156 {
9157 die->child = NULL;
9158 *new_info_ptr = cur_ptr;
9159 }
9160
9161 die->sibling = NULL;
9162 die->parent = parent;
9163 return die;
9164}
9165
9166/* Read a die, all of its descendents, and all of its siblings; set
9167 all of the fields of all of the dies correctly. Arguments are as
9168 in read_die_and_children. */
9169
9170static struct die_info *
93311388
DE
9171read_die_and_siblings (const struct die_reader_specs *reader,
9172 gdb_byte *info_ptr,
fe1b8b76 9173 gdb_byte **new_info_ptr,
639d11d3
DC
9174 struct die_info *parent)
9175{
9176 struct die_info *first_die, *last_sibling;
fe1b8b76 9177 gdb_byte *cur_ptr;
639d11d3 9178
c906108c 9179 cur_ptr = info_ptr;
639d11d3
DC
9180 first_die = last_sibling = NULL;
9181
9182 while (1)
c906108c 9183 {
639d11d3 9184 struct die_info *die
93311388 9185 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 9186
1d325ec1 9187 if (die == NULL)
c906108c 9188 {
639d11d3
DC
9189 *new_info_ptr = cur_ptr;
9190 return first_die;
c906108c 9191 }
1d325ec1
DJ
9192
9193 if (!first_die)
9194 first_die = die;
c906108c 9195 else
1d325ec1
DJ
9196 last_sibling->sibling = die;
9197
9198 last_sibling = die;
c906108c 9199 }
c906108c
SS
9200}
9201
93311388
DE
9202/* Read the die from the .debug_info section buffer. Set DIEP to
9203 point to a newly allocated die with its information, except for its
9204 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9205 whether the die has children or not. */
9206
9207static gdb_byte *
9208read_full_die (const struct die_reader_specs *reader,
9209 struct die_info **diep, gdb_byte *info_ptr,
9210 int *has_children)
9211{
9212 unsigned int abbrev_number, bytes_read, i, offset;
9213 struct abbrev_info *abbrev;
9214 struct die_info *die;
9215 struct dwarf2_cu *cu = reader->cu;
9216 bfd *abfd = reader->abfd;
9217
9218 offset = info_ptr - reader->buffer;
9219 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9220 info_ptr += bytes_read;
9221 if (!abbrev_number)
9222 {
9223 *diep = NULL;
9224 *has_children = 0;
9225 return info_ptr;
9226 }
9227
9228 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9229 if (!abbrev)
348e048f
DE
9230 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9231 abbrev_number,
9232 bfd_get_filename (abfd));
9233
93311388
DE
9234 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9235 die->offset = offset;
9236 die->tag = abbrev->tag;
9237 die->abbrev = abbrev_number;
9238
9239 die->num_attrs = abbrev->num_attrs;
9240
9241 for (i = 0; i < abbrev->num_attrs; ++i)
9242 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9243 abfd, info_ptr, cu);
9244
9245 *diep = die;
9246 *has_children = abbrev->has_children;
9247 return info_ptr;
9248}
9249
c906108c
SS
9250/* In DWARF version 2, the description of the debugging information is
9251 stored in a separate .debug_abbrev section. Before we read any
9252 dies from a section we read in all abbreviations and install them
72bf9492
DJ
9253 in a hash table. This function also sets flags in CU describing
9254 the data found in the abbrev table. */
c906108c
SS
9255
9256static void
e7c27a73 9257dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 9258{
e7c27a73 9259 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 9260 gdb_byte *abbrev_ptr;
c906108c
SS
9261 struct abbrev_info *cur_abbrev;
9262 unsigned int abbrev_number, bytes_read, abbrev_name;
9263 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
9264 struct attr_abbrev *cur_attrs;
9265 unsigned int allocated_attrs;
c906108c 9266
0963b4bd 9267 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
9268 obstack_init (&cu->abbrev_obstack);
9269 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9270 (ABBREV_HASH_SIZE
9271 * sizeof (struct abbrev_info *)));
9272 memset (cu->dwarf2_abbrevs, 0,
9273 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 9274
be391dca
TT
9275 dwarf2_read_section (dwarf2_per_objfile->objfile,
9276 &dwarf2_per_objfile->abbrev);
dce234bc 9277 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
9278 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9279 abbrev_ptr += bytes_read;
9280
f3dd6933
DJ
9281 allocated_attrs = ATTR_ALLOC_CHUNK;
9282 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 9283
0963b4bd 9284 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
9285 while (abbrev_number)
9286 {
f3dd6933 9287 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
9288
9289 /* read in abbrev header */
9290 cur_abbrev->number = abbrev_number;
9291 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9292 abbrev_ptr += bytes_read;
9293 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9294 abbrev_ptr += 1;
9295
72bf9492
DJ
9296 if (cur_abbrev->tag == DW_TAG_namespace)
9297 cu->has_namespace_info = 1;
9298
c906108c
SS
9299 /* now read in declarations */
9300 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9301 abbrev_ptr += bytes_read;
9302 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9303 abbrev_ptr += bytes_read;
9304 while (abbrev_name)
9305 {
f3dd6933 9306 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 9307 {
f3dd6933
DJ
9308 allocated_attrs += ATTR_ALLOC_CHUNK;
9309 cur_attrs
9310 = xrealloc (cur_attrs, (allocated_attrs
9311 * sizeof (struct attr_abbrev)));
c906108c 9312 }
ae038cb0
DJ
9313
9314 /* Record whether this compilation unit might have
9315 inter-compilation-unit references. If we don't know what form
9316 this attribute will have, then it might potentially be a
9317 DW_FORM_ref_addr, so we conservatively expect inter-CU
9318 references. */
9319
9320 if (abbrev_form == DW_FORM_ref_addr
9321 || abbrev_form == DW_FORM_indirect)
9322 cu->has_form_ref_addr = 1;
9323
f3dd6933
DJ
9324 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9325 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
9326 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9327 abbrev_ptr += bytes_read;
9328 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9329 abbrev_ptr += bytes_read;
9330 }
9331
f3dd6933
DJ
9332 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9333 (cur_abbrev->num_attrs
9334 * sizeof (struct attr_abbrev)));
9335 memcpy (cur_abbrev->attrs, cur_attrs,
9336 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9337
c906108c 9338 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9339 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9340 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9341
9342 /* Get next abbreviation.
9343 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9344 always properly terminated with an abbrev number of 0.
9345 Exit loop if we encounter an abbreviation which we have
9346 already read (which means we are about to read the abbreviations
9347 for the next compile unit) or if the end of the abbreviation
9348 table is reached. */
dce234bc
PP
9349 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9350 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9351 break;
9352 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9353 abbrev_ptr += bytes_read;
e7c27a73 9354 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9355 break;
9356 }
f3dd6933
DJ
9357
9358 xfree (cur_attrs);
c906108c
SS
9359}
9360
f3dd6933 9361/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9362
c906108c 9363static void
f3dd6933 9364dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9365{
f3dd6933 9366 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9367
f3dd6933
DJ
9368 obstack_free (&cu->abbrev_obstack, NULL);
9369 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9370}
9371
9372/* Lookup an abbrev_info structure in the abbrev hash table. */
9373
9374static struct abbrev_info *
e7c27a73 9375dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9376{
9377 unsigned int hash_number;
9378 struct abbrev_info *abbrev;
9379
9380 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9381 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9382
9383 while (abbrev)
9384 {
9385 if (abbrev->number == number)
9386 return abbrev;
9387 else
9388 abbrev = abbrev->next;
9389 }
9390 return NULL;
9391}
9392
72bf9492
DJ
9393/* Returns nonzero if TAG represents a type that we might generate a partial
9394 symbol for. */
9395
9396static int
9397is_type_tag_for_partial (int tag)
9398{
9399 switch (tag)
9400 {
9401#if 0
9402 /* Some types that would be reasonable to generate partial symbols for,
9403 that we don't at present. */
9404 case DW_TAG_array_type:
9405 case DW_TAG_file_type:
9406 case DW_TAG_ptr_to_member_type:
9407 case DW_TAG_set_type:
9408 case DW_TAG_string_type:
9409 case DW_TAG_subroutine_type:
9410#endif
9411 case DW_TAG_base_type:
9412 case DW_TAG_class_type:
680b30c7 9413 case DW_TAG_interface_type:
72bf9492
DJ
9414 case DW_TAG_enumeration_type:
9415 case DW_TAG_structure_type:
9416 case DW_TAG_subrange_type:
9417 case DW_TAG_typedef:
9418 case DW_TAG_union_type:
9419 return 1;
9420 default:
9421 return 0;
9422 }
9423}
9424
9425/* Load all DIEs that are interesting for partial symbols into memory. */
9426
9427static struct partial_die_info *
93311388
DE
9428load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9429 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
9430{
9431 struct partial_die_info *part_die;
9432 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9433 struct abbrev_info *abbrev;
9434 unsigned int bytes_read;
5afb4e99 9435 unsigned int load_all = 0;
72bf9492
DJ
9436
9437 int nesting_level = 1;
9438
9439 parent_die = NULL;
9440 last_die = NULL;
9441
5afb4e99
DJ
9442 if (cu->per_cu && cu->per_cu->load_all_dies)
9443 load_all = 1;
9444
72bf9492
DJ
9445 cu->partial_dies
9446 = htab_create_alloc_ex (cu->header.length / 12,
9447 partial_die_hash,
9448 partial_die_eq,
9449 NULL,
9450 &cu->comp_unit_obstack,
9451 hashtab_obstack_allocate,
9452 dummy_obstack_deallocate);
9453
9454 part_die = obstack_alloc (&cu->comp_unit_obstack,
9455 sizeof (struct partial_die_info));
9456
9457 while (1)
9458 {
9459 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9460
9461 /* A NULL abbrev means the end of a series of children. */
9462 if (abbrev == NULL)
9463 {
9464 if (--nesting_level == 0)
9465 {
9466 /* PART_DIE was probably the last thing allocated on the
9467 comp_unit_obstack, so we could call obstack_free
9468 here. We don't do that because the waste is small,
9469 and will be cleaned up when we're done with this
9470 compilation unit. This way, we're also more robust
9471 against other users of the comp_unit_obstack. */
9472 return first_die;
9473 }
9474 info_ptr += bytes_read;
9475 last_die = parent_die;
9476 parent_die = parent_die->die_parent;
9477 continue;
9478 }
9479
98bfdba5
PA
9480 /* Check for template arguments. We never save these; if
9481 they're seen, we just mark the parent, and go on our way. */
9482 if (parent_die != NULL
9483 && cu->language == language_cplus
9484 && (abbrev->tag == DW_TAG_template_type_param
9485 || abbrev->tag == DW_TAG_template_value_param))
9486 {
9487 parent_die->has_template_arguments = 1;
9488
9489 if (!load_all)
9490 {
9491 /* We don't need a partial DIE for the template argument. */
9492 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9493 cu);
9494 continue;
9495 }
9496 }
9497
9498 /* We only recurse into subprograms looking for template arguments.
9499 Skip their other children. */
9500 if (!load_all
9501 && cu->language == language_cplus
9502 && parent_die != NULL
9503 && parent_die->tag == DW_TAG_subprogram)
9504 {
9505 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9506 continue;
9507 }
9508
5afb4e99
DJ
9509 /* Check whether this DIE is interesting enough to save. Normally
9510 we would not be interested in members here, but there may be
9511 later variables referencing them via DW_AT_specification (for
9512 static members). */
9513 if (!load_all
9514 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9515 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9516 && abbrev->tag != DW_TAG_enumerator
9517 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9518 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9519 && abbrev->tag != DW_TAG_variable
5afb4e99 9520 && abbrev->tag != DW_TAG_namespace
f55ee35c 9521 && abbrev->tag != DW_TAG_module
5afb4e99 9522 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9523 {
9524 /* Otherwise we skip to the next sibling, if any. */
93311388 9525 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9526 continue;
9527 }
9528
93311388
DE
9529 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9530 buffer, info_ptr, cu);
72bf9492
DJ
9531
9532 /* This two-pass algorithm for processing partial symbols has a
9533 high cost in cache pressure. Thus, handle some simple cases
9534 here which cover the majority of C partial symbols. DIEs
9535 which neither have specification tags in them, nor could have
9536 specification tags elsewhere pointing at them, can simply be
9537 processed and discarded.
9538
9539 This segment is also optional; scan_partial_symbols and
9540 add_partial_symbol will handle these DIEs if we chain
9541 them in normally. When compilers which do not emit large
9542 quantities of duplicate debug information are more common,
9543 this code can probably be removed. */
9544
9545 /* Any complete simple types at the top level (pretty much all
9546 of them, for a language without namespaces), can be processed
9547 directly. */
9548 if (parent_die == NULL
9549 && part_die->has_specification == 0
9550 && part_die->is_declaration == 0
d8228535 9551 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9552 || part_die->tag == DW_TAG_base_type
9553 || part_die->tag == DW_TAG_subrange_type))
9554 {
9555 if (building_psymtab && part_die->name != NULL)
04a679b8 9556 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
9557 VAR_DOMAIN, LOC_TYPEDEF,
9558 &cu->objfile->static_psymbols,
9559 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 9560 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9561 continue;
9562 }
9563
d8228535
JK
9564 /* The exception for DW_TAG_typedef with has_children above is
9565 a workaround of GCC PR debug/47510. In the case of this complaint
9566 type_name_no_tag_or_error will error on such types later.
9567
9568 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9569 it could not find the child DIEs referenced later, this is checked
9570 above. In correct DWARF DW_TAG_typedef should have no children. */
9571
9572 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9573 complaint (&symfile_complaints,
9574 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9575 "- DIE at 0x%x [in module %s]"),
9576 part_die->offset, cu->objfile->name);
9577
72bf9492
DJ
9578 /* If we're at the second level, and we're an enumerator, and
9579 our parent has no specification (meaning possibly lives in a
9580 namespace elsewhere), then we can add the partial symbol now
9581 instead of queueing it. */
9582 if (part_die->tag == DW_TAG_enumerator
9583 && parent_die != NULL
9584 && parent_die->die_parent == NULL
9585 && parent_die->tag == DW_TAG_enumeration_type
9586 && parent_die->has_specification == 0)
9587 {
9588 if (part_die->name == NULL)
3e43a32a
MS
9589 complaint (&symfile_complaints,
9590 _("malformed enumerator DIE ignored"));
72bf9492 9591 else if (building_psymtab)
04a679b8 9592 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9593 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9594 (cu->language == language_cplus
9595 || cu->language == language_java)
72bf9492
DJ
9596 ? &cu->objfile->global_psymbols
9597 : &cu->objfile->static_psymbols,
9598 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9599
93311388 9600 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9601 continue;
9602 }
9603
9604 /* We'll save this DIE so link it in. */
9605 part_die->die_parent = parent_die;
9606 part_die->die_sibling = NULL;
9607 part_die->die_child = NULL;
9608
9609 if (last_die && last_die == parent_die)
9610 last_die->die_child = part_die;
9611 else if (last_die)
9612 last_die->die_sibling = part_die;
9613
9614 last_die = part_die;
9615
9616 if (first_die == NULL)
9617 first_die = part_die;
9618
9619 /* Maybe add the DIE to the hash table. Not all DIEs that we
9620 find interesting need to be in the hash table, because we
9621 also have the parent/sibling/child chains; only those that we
9622 might refer to by offset later during partial symbol reading.
9623
9624 For now this means things that might have be the target of a
9625 DW_AT_specification, DW_AT_abstract_origin, or
9626 DW_AT_extension. DW_AT_extension will refer only to
9627 namespaces; DW_AT_abstract_origin refers to functions (and
9628 many things under the function DIE, but we do not recurse
9629 into function DIEs during partial symbol reading) and
9630 possibly variables as well; DW_AT_specification refers to
9631 declarations. Declarations ought to have the DW_AT_declaration
9632 flag. It happens that GCC forgets to put it in sometimes, but
9633 only for functions, not for types.
9634
9635 Adding more things than necessary to the hash table is harmless
9636 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9637 wasted time in find_partial_die, when we reread the compilation
9638 unit with load_all_dies set. */
72bf9492 9639
5afb4e99 9640 if (load_all
72929c62 9641 || abbrev->tag == DW_TAG_constant
5afb4e99 9642 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9643 || abbrev->tag == DW_TAG_variable
9644 || abbrev->tag == DW_TAG_namespace
9645 || part_die->is_declaration)
9646 {
9647 void **slot;
9648
9649 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9650 part_die->offset, INSERT);
9651 *slot = part_die;
9652 }
9653
9654 part_die = obstack_alloc (&cu->comp_unit_obstack,
9655 sizeof (struct partial_die_info));
9656
9657 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9658 we have no reason to follow the children of structures; for other
98bfdba5
PA
9659 languages we have to, so that we can get at method physnames
9660 to infer fully qualified class names, for DW_AT_specification,
9661 and for C++ template arguments. For C++, we also look one level
9662 inside functions to find template arguments (if the name of the
9663 function does not already contain the template arguments).
bc30ff58
JB
9664
9665 For Ada, we need to scan the children of subprograms and lexical
9666 blocks as well because Ada allows the definition of nested
9667 entities that could be interesting for the debugger, such as
9668 nested subprograms for instance. */
72bf9492 9669 if (last_die->has_children
5afb4e99
DJ
9670 && (load_all
9671 || last_die->tag == DW_TAG_namespace
f55ee35c 9672 || last_die->tag == DW_TAG_module
72bf9492 9673 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9674 || (cu->language == language_cplus
9675 && last_die->tag == DW_TAG_subprogram
9676 && (last_die->name == NULL
9677 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9678 || (cu->language != language_c
9679 && (last_die->tag == DW_TAG_class_type
680b30c7 9680 || last_die->tag == DW_TAG_interface_type
72bf9492 9681 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9682 || last_die->tag == DW_TAG_union_type))
9683 || (cu->language == language_ada
9684 && (last_die->tag == DW_TAG_subprogram
9685 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9686 {
9687 nesting_level++;
9688 parent_die = last_die;
9689 continue;
9690 }
9691
9692 /* Otherwise we skip to the next sibling, if any. */
93311388 9693 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9694
9695 /* Back to the top, do it again. */
9696 }
9697}
9698
c906108c
SS
9699/* Read a minimal amount of information into the minimal die structure. */
9700
fe1b8b76 9701static gdb_byte *
72bf9492
DJ
9702read_partial_die (struct partial_die_info *part_die,
9703 struct abbrev_info *abbrev,
9704 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9705 gdb_byte *buffer, gdb_byte *info_ptr,
9706 struct dwarf2_cu *cu)
c906108c 9707{
fa238c03 9708 unsigned int i;
c906108c 9709 struct attribute attr;
c5aa993b 9710 int has_low_pc_attr = 0;
c906108c
SS
9711 int has_high_pc_attr = 0;
9712
72bf9492 9713 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9714
93311388 9715 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9716
9717 info_ptr += abbrev_len;
9718
9719 if (abbrev == NULL)
9720 return info_ptr;
9721
c906108c
SS
9722 part_die->tag = abbrev->tag;
9723 part_die->has_children = abbrev->has_children;
c906108c
SS
9724
9725 for (i = 0; i < abbrev->num_attrs; ++i)
9726 {
e7c27a73 9727 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9728
9729 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9730 partial symbol table. */
c906108c
SS
9731 switch (attr.name)
9732 {
9733 case DW_AT_name:
71c25dea
TT
9734 switch (part_die->tag)
9735 {
9736 case DW_TAG_compile_unit:
348e048f 9737 case DW_TAG_type_unit:
71c25dea
TT
9738 /* Compilation units have a DW_AT_name that is a filename, not
9739 a source language identifier. */
9740 case DW_TAG_enumeration_type:
9741 case DW_TAG_enumerator:
9742 /* These tags always have simple identifiers already; no need
9743 to canonicalize them. */
9744 part_die->name = DW_STRING (&attr);
9745 break;
9746 default:
9747 part_die->name
9748 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9749 &cu->objfile->objfile_obstack);
71c25dea
TT
9750 break;
9751 }
c906108c 9752 break;
31ef98ae 9753 case DW_AT_linkage_name:
c906108c 9754 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9755 /* Note that both forms of linkage name might appear. We
9756 assume they will be the same, and we only store the last
9757 one we see. */
94af9270
KS
9758 if (cu->language == language_ada)
9759 part_die->name = DW_STRING (&attr);
abc72ce4 9760 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9761 break;
9762 case DW_AT_low_pc:
9763 has_low_pc_attr = 1;
9764 part_die->lowpc = DW_ADDR (&attr);
9765 break;
9766 case DW_AT_high_pc:
9767 has_high_pc_attr = 1;
9768 part_die->highpc = DW_ADDR (&attr);
9769 break;
9770 case DW_AT_location:
0963b4bd 9771 /* Support the .debug_loc offsets. */
8e19ed76
PS
9772 if (attr_form_is_block (&attr))
9773 {
9774 part_die->locdesc = DW_BLOCK (&attr);
9775 }
3690dd37 9776 else if (attr_form_is_section_offset (&attr))
8e19ed76 9777 {
4d3c2250 9778 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9779 }
9780 else
9781 {
4d3c2250
KB
9782 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9783 "partial symbol information");
8e19ed76 9784 }
c906108c 9785 break;
c906108c
SS
9786 case DW_AT_external:
9787 part_die->is_external = DW_UNSND (&attr);
9788 break;
9789 case DW_AT_declaration:
9790 part_die->is_declaration = DW_UNSND (&attr);
9791 break;
9792 case DW_AT_type:
9793 part_die->has_type = 1;
9794 break;
9795 case DW_AT_abstract_origin:
9796 case DW_AT_specification:
72bf9492
DJ
9797 case DW_AT_extension:
9798 part_die->has_specification = 1;
c764a876 9799 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9800 break;
9801 case DW_AT_sibling:
9802 /* Ignore absolute siblings, they might point outside of
9803 the current compile unit. */
9804 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9805 complaint (&symfile_complaints,
9806 _("ignoring absolute DW_AT_sibling"));
c906108c 9807 else
93311388 9808 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9809 break;
fa4028e9
JB
9810 case DW_AT_byte_size:
9811 part_die->has_byte_size = 1;
9812 break;
68511cec
CES
9813 case DW_AT_calling_convention:
9814 /* DWARF doesn't provide a way to identify a program's source-level
9815 entry point. DW_AT_calling_convention attributes are only meant
9816 to describe functions' calling conventions.
9817
9818 However, because it's a necessary piece of information in
9819 Fortran, and because DW_CC_program is the only piece of debugging
9820 information whose definition refers to a 'main program' at all,
9821 several compilers have begun marking Fortran main programs with
9822 DW_CC_program --- even when those functions use the standard
9823 calling conventions.
9824
9825 So until DWARF specifies a way to provide this information and
9826 compilers pick up the new representation, we'll support this
9827 practice. */
9828 if (DW_UNSND (&attr) == DW_CC_program
9829 && cu->language == language_fortran)
01f8c46d
JK
9830 {
9831 set_main_name (part_die->name);
9832
9833 /* As this DIE has a static linkage the name would be difficult
9834 to look up later. */
9835 language_of_main = language_fortran;
9836 }
68511cec 9837 break;
c906108c
SS
9838 default:
9839 break;
9840 }
9841 }
9842
9373cf26
JK
9843 if (has_low_pc_attr && has_high_pc_attr)
9844 {
9845 /* When using the GNU linker, .gnu.linkonce. sections are used to
9846 eliminate duplicate copies of functions and vtables and such.
9847 The linker will arbitrarily choose one and discard the others.
9848 The AT_*_pc values for such functions refer to local labels in
9849 these sections. If the section from that file was discarded, the
9850 labels are not in the output, so the relocs get a value of 0.
9851 If this is a discarded function, mark the pc bounds as invalid,
9852 so that GDB will ignore it. */
9853 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9854 {
9855 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9856
9857 complaint (&symfile_complaints,
9858 _("DW_AT_low_pc %s is zero "
9859 "for DIE at 0x%x [in module %s]"),
9860 paddress (gdbarch, part_die->lowpc),
9861 part_die->offset, cu->objfile->name);
9862 }
9863 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9864 else if (part_die->lowpc >= part_die->highpc)
9865 {
9866 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9867
9868 complaint (&symfile_complaints,
9869 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9870 "for DIE at 0x%x [in module %s]"),
9871 paddress (gdbarch, part_die->lowpc),
9872 paddress (gdbarch, part_die->highpc),
9873 part_die->offset, cu->objfile->name);
9874 }
9875 else
9876 part_die->has_pc_info = 1;
9877 }
85cbf3d3 9878
c906108c
SS
9879 return info_ptr;
9880}
9881
72bf9492
DJ
9882/* Find a cached partial DIE at OFFSET in CU. */
9883
9884static struct partial_die_info *
c764a876 9885find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9886{
9887 struct partial_die_info *lookup_die = NULL;
9888 struct partial_die_info part_die;
9889
9890 part_die.offset = offset;
9891 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9892
72bf9492
DJ
9893 return lookup_die;
9894}
9895
348e048f
DE
9896/* Find a partial DIE at OFFSET, which may or may not be in CU,
9897 except in the case of .debug_types DIEs which do not reference
9898 outside their CU (they do however referencing other types via
55f1336d 9899 DW_FORM_ref_sig8). */
72bf9492
DJ
9900
9901static struct partial_die_info *
c764a876 9902find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9903{
5afb4e99
DJ
9904 struct dwarf2_per_cu_data *per_cu = NULL;
9905 struct partial_die_info *pd = NULL;
72bf9492 9906
8b70b953 9907 if (cu->per_cu->debug_type_section)
348e048f
DE
9908 {
9909 pd = find_partial_die_in_comp_unit (offset, cu);
9910 if (pd != NULL)
9911 return pd;
9912 goto not_found;
9913 }
9914
45452591 9915 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9916 {
9917 pd = find_partial_die_in_comp_unit (offset, cu);
9918 if (pd != NULL)
9919 return pd;
9920 }
72bf9492 9921
ae038cb0
DJ
9922 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9923
98bfdba5
PA
9924 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9925 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9926
9927 per_cu->cu->last_used = 0;
5afb4e99
DJ
9928 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9929
9930 if (pd == NULL && per_cu->load_all_dies == 0)
9931 {
9932 struct cleanup *back_to;
9933 struct partial_die_info comp_unit_die;
9934 struct abbrev_info *abbrev;
9935 unsigned int bytes_read;
9936 char *info_ptr;
9937
9938 per_cu->load_all_dies = 1;
9939
9940 /* Re-read the DIEs. */
9941 back_to = make_cleanup (null_cleanup, 0);
9942 if (per_cu->cu->dwarf2_abbrevs == NULL)
9943 {
9944 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9945 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9946 }
dce234bc 9947 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9948 + per_cu->cu->header.offset
9949 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9950 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9951 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9952 per_cu->cu->objfile->obfd,
9953 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9954 per_cu->cu);
9955 if (comp_unit_die.has_children)
93311388
DE
9956 load_partial_dies (per_cu->cu->objfile->obfd,
9957 dwarf2_per_objfile->info.buffer, info_ptr,
9958 0, per_cu->cu);
5afb4e99
DJ
9959 do_cleanups (back_to);
9960
9961 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9962 }
9963
348e048f
DE
9964 not_found:
9965
5afb4e99
DJ
9966 if (pd == NULL)
9967 internal_error (__FILE__, __LINE__,
3e43a32a
MS
9968 _("could not find partial DIE 0x%x "
9969 "in cache [from module %s]\n"),
5afb4e99
DJ
9970 offset, bfd_get_filename (cu->objfile->obfd));
9971 return pd;
72bf9492
DJ
9972}
9973
abc72ce4
DE
9974/* See if we can figure out if the class lives in a namespace. We do
9975 this by looking for a member function; its demangled name will
9976 contain namespace info, if there is any. */
9977
9978static void
9979guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9980 struct dwarf2_cu *cu)
9981{
9982 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9983 what template types look like, because the demangler
9984 frequently doesn't give the same name as the debug info. We
9985 could fix this by only using the demangled name to get the
9986 prefix (but see comment in read_structure_type). */
9987
9988 struct partial_die_info *real_pdi;
9989 struct partial_die_info *child_pdi;
9990
9991 /* If this DIE (this DIE's specification, if any) has a parent, then
9992 we should not do this. We'll prepend the parent's fully qualified
9993 name when we create the partial symbol. */
9994
9995 real_pdi = struct_pdi;
9996 while (real_pdi->has_specification)
9997 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9998
9999 if (real_pdi->die_parent != NULL)
10000 return;
10001
10002 for (child_pdi = struct_pdi->die_child;
10003 child_pdi != NULL;
10004 child_pdi = child_pdi->die_sibling)
10005 {
10006 if (child_pdi->tag == DW_TAG_subprogram
10007 && child_pdi->linkage_name != NULL)
10008 {
10009 char *actual_class_name
10010 = language_class_name_from_physname (cu->language_defn,
10011 child_pdi->linkage_name);
10012 if (actual_class_name != NULL)
10013 {
10014 struct_pdi->name
10015 = obsavestring (actual_class_name,
10016 strlen (actual_class_name),
10017 &cu->objfile->objfile_obstack);
10018 xfree (actual_class_name);
10019 }
10020 break;
10021 }
10022 }
10023}
10024
72bf9492
DJ
10025/* Adjust PART_DIE before generating a symbol for it. This function
10026 may set the is_external flag or change the DIE's name. */
10027
10028static void
10029fixup_partial_die (struct partial_die_info *part_die,
10030 struct dwarf2_cu *cu)
10031{
abc72ce4
DE
10032 /* Once we've fixed up a die, there's no point in doing so again.
10033 This also avoids a memory leak if we were to call
10034 guess_partial_die_structure_name multiple times. */
10035 if (part_die->fixup_called)
10036 return;
10037
72bf9492
DJ
10038 /* If we found a reference attribute and the DIE has no name, try
10039 to find a name in the referred to DIE. */
10040
10041 if (part_die->name == NULL && part_die->has_specification)
10042 {
10043 struct partial_die_info *spec_die;
72bf9492 10044
10b3939b 10045 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 10046
10b3939b 10047 fixup_partial_die (spec_die, cu);
72bf9492
DJ
10048
10049 if (spec_die->name)
10050 {
10051 part_die->name = spec_die->name;
10052
10053 /* Copy DW_AT_external attribute if it is set. */
10054 if (spec_die->is_external)
10055 part_die->is_external = spec_die->is_external;
10056 }
10057 }
10058
10059 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
10060
10061 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 10062 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 10063
abc72ce4
DE
10064 /* If there is no parent die to provide a namespace, and there are
10065 children, see if we can determine the namespace from their linkage
10066 name.
10067 NOTE: We need to do this even if cu->has_namespace_info != 0.
10068 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10069 if (cu->language == language_cplus
8b70b953 10070 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
10071 && part_die->die_parent == NULL
10072 && part_die->has_children
10073 && (part_die->tag == DW_TAG_class_type
10074 || part_die->tag == DW_TAG_structure_type
10075 || part_die->tag == DW_TAG_union_type))
10076 guess_partial_die_structure_name (part_die, cu);
10077
53832f31
TT
10078 /* GCC might emit a nameless struct or union that has a linkage
10079 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10080 if (part_die->name == NULL
96408a79
SA
10081 && (part_die->tag == DW_TAG_class_type
10082 || part_die->tag == DW_TAG_interface_type
10083 || part_die->tag == DW_TAG_structure_type
10084 || part_die->tag == DW_TAG_union_type)
53832f31
TT
10085 && part_die->linkage_name != NULL)
10086 {
10087 char *demangled;
10088
10089 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10090 if (demangled)
10091 {
96408a79
SA
10092 const char *base;
10093
10094 /* Strip any leading namespaces/classes, keep only the base name.
10095 DW_AT_name for named DIEs does not contain the prefixes. */
10096 base = strrchr (demangled, ':');
10097 if (base && base > demangled && base[-1] == ':')
10098 base++;
10099 else
10100 base = demangled;
10101
10102 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
10103 &cu->objfile->objfile_obstack);
10104 xfree (demangled);
10105 }
10106 }
10107
abc72ce4 10108 part_die->fixup_called = 1;
72bf9492
DJ
10109}
10110
a8329558 10111/* Read an attribute value described by an attribute form. */
c906108c 10112
fe1b8b76 10113static gdb_byte *
a8329558 10114read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 10115 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 10116 struct dwarf2_cu *cu)
c906108c 10117{
e7c27a73 10118 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10119 unsigned int bytes_read;
10120 struct dwarf_block *blk;
10121
a8329558
KW
10122 attr->form = form;
10123 switch (form)
c906108c 10124 {
c906108c 10125 case DW_FORM_ref_addr:
ae411497
TT
10126 if (cu->header.version == 2)
10127 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10128 else
3e43a32a
MS
10129 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10130 &cu->header, &bytes_read);
ae411497
TT
10131 info_ptr += bytes_read;
10132 break;
10133 case DW_FORM_addr:
e7c27a73 10134 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 10135 info_ptr += bytes_read;
c906108c
SS
10136 break;
10137 case DW_FORM_block2:
7b5a2f43 10138 blk = dwarf_alloc_block (cu);
c906108c
SS
10139 blk->size = read_2_bytes (abfd, info_ptr);
10140 info_ptr += 2;
10141 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10142 info_ptr += blk->size;
10143 DW_BLOCK (attr) = blk;
10144 break;
10145 case DW_FORM_block4:
7b5a2f43 10146 blk = dwarf_alloc_block (cu);
c906108c
SS
10147 blk->size = read_4_bytes (abfd, info_ptr);
10148 info_ptr += 4;
10149 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10150 info_ptr += blk->size;
10151 DW_BLOCK (attr) = blk;
10152 break;
10153 case DW_FORM_data2:
10154 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10155 info_ptr += 2;
10156 break;
10157 case DW_FORM_data4:
10158 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10159 info_ptr += 4;
10160 break;
10161 case DW_FORM_data8:
10162 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10163 info_ptr += 8;
10164 break;
2dc7f7b3
TT
10165 case DW_FORM_sec_offset:
10166 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10167 info_ptr += bytes_read;
10168 break;
c906108c 10169 case DW_FORM_string:
9b1c24c8 10170 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 10171 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
10172 info_ptr += bytes_read;
10173 break;
4bdf3d34
JJ
10174 case DW_FORM_strp:
10175 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10176 &bytes_read);
8285870a 10177 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
10178 info_ptr += bytes_read;
10179 break;
2dc7f7b3 10180 case DW_FORM_exprloc:
c906108c 10181 case DW_FORM_block:
7b5a2f43 10182 blk = dwarf_alloc_block (cu);
c906108c
SS
10183 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10184 info_ptr += bytes_read;
10185 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10186 info_ptr += blk->size;
10187 DW_BLOCK (attr) = blk;
10188 break;
10189 case DW_FORM_block1:
7b5a2f43 10190 blk = dwarf_alloc_block (cu);
c906108c
SS
10191 blk->size = read_1_byte (abfd, info_ptr);
10192 info_ptr += 1;
10193 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10194 info_ptr += blk->size;
10195 DW_BLOCK (attr) = blk;
10196 break;
10197 case DW_FORM_data1:
10198 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10199 info_ptr += 1;
10200 break;
10201 case DW_FORM_flag:
10202 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10203 info_ptr += 1;
10204 break;
2dc7f7b3
TT
10205 case DW_FORM_flag_present:
10206 DW_UNSND (attr) = 1;
10207 break;
c906108c
SS
10208 case DW_FORM_sdata:
10209 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10210 info_ptr += bytes_read;
10211 break;
10212 case DW_FORM_udata:
10213 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10214 info_ptr += bytes_read;
10215 break;
10216 case DW_FORM_ref1:
10b3939b 10217 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
10218 info_ptr += 1;
10219 break;
10220 case DW_FORM_ref2:
10b3939b 10221 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
10222 info_ptr += 2;
10223 break;
10224 case DW_FORM_ref4:
10b3939b 10225 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
10226 info_ptr += 4;
10227 break;
613e1657 10228 case DW_FORM_ref8:
10b3939b 10229 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
10230 info_ptr += 8;
10231 break;
55f1336d 10232 case DW_FORM_ref_sig8:
348e048f
DE
10233 /* Convert the signature to something we can record in DW_UNSND
10234 for later lookup.
10235 NOTE: This is NULL if the type wasn't found. */
10236 DW_SIGNATURED_TYPE (attr) =
10237 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10238 info_ptr += 8;
10239 break;
c906108c 10240 case DW_FORM_ref_udata:
10b3939b
DJ
10241 DW_ADDR (attr) = (cu->header.offset
10242 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
10243 info_ptr += bytes_read;
10244 break;
c906108c 10245 case DW_FORM_indirect:
a8329558
KW
10246 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10247 info_ptr += bytes_read;
e7c27a73 10248 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 10249 break;
c906108c 10250 default:
8a3fe4f8 10251 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
10252 dwarf_form_name (form),
10253 bfd_get_filename (abfd));
c906108c 10254 }
28e94949
JB
10255
10256 /* We have seen instances where the compiler tried to emit a byte
10257 size attribute of -1 which ended up being encoded as an unsigned
10258 0xffffffff. Although 0xffffffff is technically a valid size value,
10259 an object of this size seems pretty unlikely so we can relatively
10260 safely treat these cases as if the size attribute was invalid and
10261 treat them as zero by default. */
10262 if (attr->name == DW_AT_byte_size
10263 && form == DW_FORM_data4
10264 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
10265 {
10266 complaint
10267 (&symfile_complaints,
43bbcdc2
PH
10268 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10269 hex_string (DW_UNSND (attr)));
01c66ae6
JB
10270 DW_UNSND (attr) = 0;
10271 }
28e94949 10272
c906108c
SS
10273 return info_ptr;
10274}
10275
a8329558
KW
10276/* Read an attribute described by an abbreviated attribute. */
10277
fe1b8b76 10278static gdb_byte *
a8329558 10279read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 10280 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
10281{
10282 attr->name = abbrev->name;
e7c27a73 10283 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
10284}
10285
0963b4bd 10286/* Read dwarf information from a buffer. */
c906108c
SS
10287
10288static unsigned int
fe1b8b76 10289read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 10290{
fe1b8b76 10291 return bfd_get_8 (abfd, buf);
c906108c
SS
10292}
10293
10294static int
fe1b8b76 10295read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 10296{
fe1b8b76 10297 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
10298}
10299
10300static unsigned int
fe1b8b76 10301read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10302{
fe1b8b76 10303 return bfd_get_16 (abfd, buf);
c906108c
SS
10304}
10305
c906108c 10306static unsigned int
fe1b8b76 10307read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10308{
fe1b8b76 10309 return bfd_get_32 (abfd, buf);
c906108c
SS
10310}
10311
93311388 10312static ULONGEST
fe1b8b76 10313read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10314{
fe1b8b76 10315 return bfd_get_64 (abfd, buf);
c906108c
SS
10316}
10317
10318static CORE_ADDR
fe1b8b76 10319read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 10320 unsigned int *bytes_read)
c906108c 10321{
e7c27a73 10322 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10323 CORE_ADDR retval = 0;
10324
107d2387 10325 if (cu_header->signed_addr_p)
c906108c 10326 {
107d2387
AC
10327 switch (cu_header->addr_size)
10328 {
10329 case 2:
fe1b8b76 10330 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
10331 break;
10332 case 4:
fe1b8b76 10333 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
10334 break;
10335 case 8:
fe1b8b76 10336 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
10337 break;
10338 default:
8e65ff28 10339 internal_error (__FILE__, __LINE__,
e2e0b3e5 10340 _("read_address: bad switch, signed [in module %s]"),
659b0389 10341 bfd_get_filename (abfd));
107d2387
AC
10342 }
10343 }
10344 else
10345 {
10346 switch (cu_header->addr_size)
10347 {
10348 case 2:
fe1b8b76 10349 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10350 break;
10351 case 4:
fe1b8b76 10352 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10353 break;
10354 case 8:
fe1b8b76 10355 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10356 break;
10357 default:
8e65ff28 10358 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10359 _("read_address: bad switch, "
10360 "unsigned [in module %s]"),
659b0389 10361 bfd_get_filename (abfd));
107d2387 10362 }
c906108c 10363 }
64367e0a 10364
107d2387
AC
10365 *bytes_read = cu_header->addr_size;
10366 return retval;
c906108c
SS
10367}
10368
f7ef9339 10369/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10370 specification allows the initial length to take up either 4 bytes
10371 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10372 bytes describe the length and all offsets will be 8 bytes in length
10373 instead of 4.
10374
f7ef9339
KB
10375 An older, non-standard 64-bit format is also handled by this
10376 function. The older format in question stores the initial length
10377 as an 8-byte quantity without an escape value. Lengths greater
10378 than 2^32 aren't very common which means that the initial 4 bytes
10379 is almost always zero. Since a length value of zero doesn't make
10380 sense for the 32-bit format, this initial zero can be considered to
10381 be an escape value which indicates the presence of the older 64-bit
10382 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10383 greater than 4GB. If it becomes necessary to handle lengths
10384 somewhat larger than 4GB, we could allow other small values (such
10385 as the non-sensical values of 1, 2, and 3) to also be used as
10386 escape values indicating the presence of the old format.
f7ef9339 10387
917c78fc
MK
10388 The value returned via bytes_read should be used to increment the
10389 relevant pointer after calling read_initial_length().
c764a876 10390
613e1657
KB
10391 [ Note: read_initial_length() and read_offset() are based on the
10392 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10393 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10394 from:
10395
f7ef9339 10396 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10397
613e1657
KB
10398 This document is only a draft and is subject to change. (So beware.)
10399
f7ef9339 10400 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10401 determined empirically by examining 64-bit ELF files produced by
10402 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10403
10404 - Kevin, July 16, 2002
613e1657
KB
10405 ] */
10406
10407static LONGEST
c764a876 10408read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10409{
fe1b8b76 10410 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10411
dd373385 10412 if (length == 0xffffffff)
613e1657 10413 {
fe1b8b76 10414 length = bfd_get_64 (abfd, buf + 4);
613e1657 10415 *bytes_read = 12;
613e1657 10416 }
dd373385 10417 else if (length == 0)
f7ef9339 10418 {
dd373385 10419 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10420 length = bfd_get_64 (abfd, buf);
f7ef9339 10421 *bytes_read = 8;
f7ef9339 10422 }
613e1657
KB
10423 else
10424 {
10425 *bytes_read = 4;
613e1657
KB
10426 }
10427
c764a876
DE
10428 return length;
10429}
dd373385 10430
c764a876
DE
10431/* Cover function for read_initial_length.
10432 Returns the length of the object at BUF, and stores the size of the
10433 initial length in *BYTES_READ and stores the size that offsets will be in
10434 *OFFSET_SIZE.
10435 If the initial length size is not equivalent to that specified in
10436 CU_HEADER then issue a complaint.
10437 This is useful when reading non-comp-unit headers. */
dd373385 10438
c764a876
DE
10439static LONGEST
10440read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10441 const struct comp_unit_head *cu_header,
10442 unsigned int *bytes_read,
10443 unsigned int *offset_size)
10444{
10445 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10446
10447 gdb_assert (cu_header->initial_length_size == 4
10448 || cu_header->initial_length_size == 8
10449 || cu_header->initial_length_size == 12);
10450
10451 if (cu_header->initial_length_size != *bytes_read)
10452 complaint (&symfile_complaints,
10453 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10454
c764a876 10455 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10456 return length;
613e1657
KB
10457}
10458
10459/* Read an offset from the data stream. The size of the offset is
917c78fc 10460 given by cu_header->offset_size. */
613e1657
KB
10461
10462static LONGEST
fe1b8b76 10463read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10464 unsigned int *bytes_read)
c764a876
DE
10465{
10466 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10467
c764a876
DE
10468 *bytes_read = cu_header->offset_size;
10469 return offset;
10470}
10471
10472/* Read an offset from the data stream. */
10473
10474static LONGEST
10475read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10476{
10477 LONGEST retval = 0;
10478
c764a876 10479 switch (offset_size)
613e1657
KB
10480 {
10481 case 4:
fe1b8b76 10482 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10483 break;
10484 case 8:
fe1b8b76 10485 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10486 break;
10487 default:
8e65ff28 10488 internal_error (__FILE__, __LINE__,
c764a876 10489 _("read_offset_1: bad switch [in module %s]"),
659b0389 10490 bfd_get_filename (abfd));
613e1657
KB
10491 }
10492
917c78fc 10493 return retval;
613e1657
KB
10494}
10495
fe1b8b76
JB
10496static gdb_byte *
10497read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10498{
10499 /* If the size of a host char is 8 bits, we can return a pointer
10500 to the buffer, otherwise we have to copy the data to a buffer
10501 allocated on the temporary obstack. */
4bdf3d34 10502 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10503 return buf;
c906108c
SS
10504}
10505
10506static char *
9b1c24c8 10507read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10508{
10509 /* If the size of a host char is 8 bits, we can return a pointer
10510 to the string, otherwise we have to copy the string to a buffer
10511 allocated on the temporary obstack. */
4bdf3d34 10512 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10513 if (*buf == '\0')
10514 {
10515 *bytes_read_ptr = 1;
10516 return NULL;
10517 }
fe1b8b76
JB
10518 *bytes_read_ptr = strlen ((char *) buf) + 1;
10519 return (char *) buf;
4bdf3d34
JJ
10520}
10521
10522static char *
cf2c3c16 10523read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 10524{
be391dca 10525 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10526 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
10527 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10528 bfd_get_filename (abfd));
dce234bc 10529 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
10530 error (_("DW_FORM_strp pointing outside of "
10531 ".debug_str section [in module %s]"),
10532 bfd_get_filename (abfd));
4bdf3d34 10533 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10534 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10535 return NULL;
dce234bc 10536 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10537}
10538
cf2c3c16
TT
10539static char *
10540read_indirect_string (bfd *abfd, gdb_byte *buf,
10541 const struct comp_unit_head *cu_header,
10542 unsigned int *bytes_read_ptr)
10543{
10544 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10545
10546 return read_indirect_string_at_offset (abfd, str_offset);
10547}
10548
ce5d95e1 10549static unsigned long
fe1b8b76 10550read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10551{
ce5d95e1
JB
10552 unsigned long result;
10553 unsigned int num_read;
c906108c
SS
10554 int i, shift;
10555 unsigned char byte;
10556
10557 result = 0;
10558 shift = 0;
10559 num_read = 0;
10560 i = 0;
10561 while (1)
10562 {
fe1b8b76 10563 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10564 buf++;
10565 num_read++;
ce5d95e1 10566 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10567 if ((byte & 128) == 0)
10568 {
10569 break;
10570 }
10571 shift += 7;
10572 }
10573 *bytes_read_ptr = num_read;
10574 return result;
10575}
10576
ce5d95e1 10577static long
fe1b8b76 10578read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10579{
ce5d95e1 10580 long result;
77e0b926 10581 int i, shift, num_read;
c906108c
SS
10582 unsigned char byte;
10583
10584 result = 0;
10585 shift = 0;
c906108c
SS
10586 num_read = 0;
10587 i = 0;
10588 while (1)
10589 {
fe1b8b76 10590 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10591 buf++;
10592 num_read++;
ce5d95e1 10593 result |= ((long)(byte & 127) << shift);
c906108c
SS
10594 shift += 7;
10595 if ((byte & 128) == 0)
10596 {
10597 break;
10598 }
10599 }
77e0b926
DJ
10600 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10601 result |= -(((long)1) << shift);
c906108c
SS
10602 *bytes_read_ptr = num_read;
10603 return result;
10604}
10605
4bb7a0a7
DJ
10606/* Return a pointer to just past the end of an LEB128 number in BUF. */
10607
fe1b8b76
JB
10608static gdb_byte *
10609skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10610{
10611 int byte;
10612
10613 while (1)
10614 {
fe1b8b76 10615 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10616 buf++;
10617 if ((byte & 128) == 0)
10618 return buf;
10619 }
10620}
10621
c906108c 10622static void
e142c38c 10623set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10624{
10625 switch (lang)
10626 {
10627 case DW_LANG_C89:
76bee0cc 10628 case DW_LANG_C99:
c906108c 10629 case DW_LANG_C:
e142c38c 10630 cu->language = language_c;
c906108c
SS
10631 break;
10632 case DW_LANG_C_plus_plus:
e142c38c 10633 cu->language = language_cplus;
c906108c 10634 break;
6aecb9c2
JB
10635 case DW_LANG_D:
10636 cu->language = language_d;
10637 break;
c906108c
SS
10638 case DW_LANG_Fortran77:
10639 case DW_LANG_Fortran90:
b21b22e0 10640 case DW_LANG_Fortran95:
e142c38c 10641 cu->language = language_fortran;
c906108c
SS
10642 break;
10643 case DW_LANG_Mips_Assembler:
e142c38c 10644 cu->language = language_asm;
c906108c 10645 break;
bebd888e 10646 case DW_LANG_Java:
e142c38c 10647 cu->language = language_java;
bebd888e 10648 break;
c906108c 10649 case DW_LANG_Ada83:
8aaf0b47 10650 case DW_LANG_Ada95:
bc5f45f8
JB
10651 cu->language = language_ada;
10652 break;
72019c9c
GM
10653 case DW_LANG_Modula2:
10654 cu->language = language_m2;
10655 break;
fe8e67fd
PM
10656 case DW_LANG_Pascal83:
10657 cu->language = language_pascal;
10658 break;
22566fbd
DJ
10659 case DW_LANG_ObjC:
10660 cu->language = language_objc;
10661 break;
c906108c
SS
10662 case DW_LANG_Cobol74:
10663 case DW_LANG_Cobol85:
c906108c 10664 default:
e142c38c 10665 cu->language = language_minimal;
c906108c
SS
10666 break;
10667 }
e142c38c 10668 cu->language_defn = language_def (cu->language);
c906108c
SS
10669}
10670
10671/* Return the named attribute or NULL if not there. */
10672
10673static struct attribute *
e142c38c 10674dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10675{
10676 unsigned int i;
10677 struct attribute *spec = NULL;
10678
10679 for (i = 0; i < die->num_attrs; ++i)
10680 {
10681 if (die->attrs[i].name == name)
10b3939b 10682 return &die->attrs[i];
c906108c
SS
10683 if (die->attrs[i].name == DW_AT_specification
10684 || die->attrs[i].name == DW_AT_abstract_origin)
10685 spec = &die->attrs[i];
10686 }
c906108c 10687
10b3939b 10688 if (spec)
f2f0e013
DJ
10689 {
10690 die = follow_die_ref (die, spec, &cu);
10691 return dwarf2_attr (die, name, cu);
10692 }
c5aa993b 10693
c906108c
SS
10694 return NULL;
10695}
10696
348e048f
DE
10697/* Return the named attribute or NULL if not there,
10698 but do not follow DW_AT_specification, etc.
10699 This is for use in contexts where we're reading .debug_types dies.
10700 Following DW_AT_specification, DW_AT_abstract_origin will take us
10701 back up the chain, and we want to go down. */
10702
10703static struct attribute *
10704dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10705 struct dwarf2_cu *cu)
10706{
10707 unsigned int i;
10708
10709 for (i = 0; i < die->num_attrs; ++i)
10710 if (die->attrs[i].name == name)
10711 return &die->attrs[i];
10712
10713 return NULL;
10714}
10715
05cf31d1
JB
10716/* Return non-zero iff the attribute NAME is defined for the given DIE,
10717 and holds a non-zero value. This function should only be used for
2dc7f7b3 10718 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10719
10720static int
10721dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10722{
10723 struct attribute *attr = dwarf2_attr (die, name, cu);
10724
10725 return (attr && DW_UNSND (attr));
10726}
10727
3ca72b44 10728static int
e142c38c 10729die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10730{
05cf31d1
JB
10731 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10732 which value is non-zero. However, we have to be careful with
10733 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10734 (via dwarf2_flag_true_p) follows this attribute. So we may
10735 end up accidently finding a declaration attribute that belongs
10736 to a different DIE referenced by the specification attribute,
10737 even though the given DIE does not have a declaration attribute. */
10738 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10739 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10740}
10741
63d06c5c 10742/* Return the die giving the specification for DIE, if there is
f2f0e013 10743 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10744 containing the return value on output. If there is no
10745 specification, but there is an abstract origin, that is
10746 returned. */
63d06c5c
DC
10747
10748static struct die_info *
f2f0e013 10749die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10750{
f2f0e013
DJ
10751 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10752 *spec_cu);
63d06c5c 10753
edb3359d
DJ
10754 if (spec_attr == NULL)
10755 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10756
63d06c5c
DC
10757 if (spec_attr == NULL)
10758 return NULL;
10759 else
f2f0e013 10760 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10761}
c906108c 10762
debd256d 10763/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10764 refers to.
10765 NOTE: This is also used as a "cleanup" function. */
10766
debd256d
JB
10767static void
10768free_line_header (struct line_header *lh)
10769{
10770 if (lh->standard_opcode_lengths)
a8bc7b56 10771 xfree (lh->standard_opcode_lengths);
debd256d
JB
10772
10773 /* Remember that all the lh->file_names[i].name pointers are
10774 pointers into debug_line_buffer, and don't need to be freed. */
10775 if (lh->file_names)
a8bc7b56 10776 xfree (lh->file_names);
debd256d
JB
10777
10778 /* Similarly for the include directory names. */
10779 if (lh->include_dirs)
a8bc7b56 10780 xfree (lh->include_dirs);
debd256d 10781
a8bc7b56 10782 xfree (lh);
debd256d
JB
10783}
10784
debd256d 10785/* Add an entry to LH's include directory table. */
ae2de4f8 10786
debd256d
JB
10787static void
10788add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10789{
debd256d
JB
10790 /* Grow the array if necessary. */
10791 if (lh->include_dirs_size == 0)
c5aa993b 10792 {
debd256d
JB
10793 lh->include_dirs_size = 1; /* for testing */
10794 lh->include_dirs = xmalloc (lh->include_dirs_size
10795 * sizeof (*lh->include_dirs));
10796 }
10797 else if (lh->num_include_dirs >= lh->include_dirs_size)
10798 {
10799 lh->include_dirs_size *= 2;
10800 lh->include_dirs = xrealloc (lh->include_dirs,
10801 (lh->include_dirs_size
10802 * sizeof (*lh->include_dirs)));
c5aa993b 10803 }
c906108c 10804
debd256d
JB
10805 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10806}
6e70227d 10807
debd256d 10808/* Add an entry to LH's file name table. */
ae2de4f8 10809
debd256d
JB
10810static void
10811add_file_name (struct line_header *lh,
10812 char *name,
10813 unsigned int dir_index,
10814 unsigned int mod_time,
10815 unsigned int length)
10816{
10817 struct file_entry *fe;
10818
10819 /* Grow the array if necessary. */
10820 if (lh->file_names_size == 0)
10821 {
10822 lh->file_names_size = 1; /* for testing */
10823 lh->file_names = xmalloc (lh->file_names_size
10824 * sizeof (*lh->file_names));
10825 }
10826 else if (lh->num_file_names >= lh->file_names_size)
10827 {
10828 lh->file_names_size *= 2;
10829 lh->file_names = xrealloc (lh->file_names,
10830 (lh->file_names_size
10831 * sizeof (*lh->file_names)));
10832 }
10833
10834 fe = &lh->file_names[lh->num_file_names++];
10835 fe->name = name;
10836 fe->dir_index = dir_index;
10837 fe->mod_time = mod_time;
10838 fe->length = length;
aaa75496 10839 fe->included_p = 0;
cb1df416 10840 fe->symtab = NULL;
debd256d 10841}
6e70227d 10842
debd256d 10843/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10844 .debug_line, according to the endianness of ABFD. Return a pointer
10845 to a struct line_header, allocated using xmalloc.
debd256d
JB
10846
10847 NOTE: the strings in the include directory and file name tables of
10848 the returned object point into debug_line_buffer, and must not be
10849 freed. */
ae2de4f8 10850
debd256d
JB
10851static struct line_header *
10852dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10853 struct dwarf2_cu *cu)
debd256d
JB
10854{
10855 struct cleanup *back_to;
10856 struct line_header *lh;
fe1b8b76 10857 gdb_byte *line_ptr;
c764a876 10858 unsigned int bytes_read, offset_size;
debd256d
JB
10859 int i;
10860 char *cur_dir, *cur_file;
10861
be391dca 10862 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10863 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10864 {
e2e0b3e5 10865 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10866 return 0;
10867 }
10868
a738430d
MK
10869 /* Make sure that at least there's room for the total_length field.
10870 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10871 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10872 {
4d3c2250 10873 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10874 return 0;
10875 }
10876
10877 lh = xmalloc (sizeof (*lh));
10878 memset (lh, 0, sizeof (*lh));
10879 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10880 (void *) lh);
10881
dce234bc 10882 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10883
a738430d 10884 /* Read in the header. */
6e70227d 10885 lh->total_length =
c764a876
DE
10886 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10887 &bytes_read, &offset_size);
debd256d 10888 line_ptr += bytes_read;
dce234bc
PP
10889 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10890 + dwarf2_per_objfile->line.size))
debd256d 10891 {
4d3c2250 10892 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10893 return 0;
10894 }
10895 lh->statement_program_end = line_ptr + lh->total_length;
10896 lh->version = read_2_bytes (abfd, line_ptr);
10897 line_ptr += 2;
c764a876
DE
10898 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10899 line_ptr += offset_size;
debd256d
JB
10900 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10901 line_ptr += 1;
2dc7f7b3
TT
10902 if (lh->version >= 4)
10903 {
10904 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10905 line_ptr += 1;
10906 }
10907 else
10908 lh->maximum_ops_per_instruction = 1;
10909
10910 if (lh->maximum_ops_per_instruction == 0)
10911 {
10912 lh->maximum_ops_per_instruction = 1;
10913 complaint (&symfile_complaints,
3e43a32a
MS
10914 _("invalid maximum_ops_per_instruction "
10915 "in `.debug_line' section"));
2dc7f7b3
TT
10916 }
10917
debd256d
JB
10918 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10919 line_ptr += 1;
10920 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10921 line_ptr += 1;
10922 lh->line_range = read_1_byte (abfd, line_ptr);
10923 line_ptr += 1;
10924 lh->opcode_base = read_1_byte (abfd, line_ptr);
10925 line_ptr += 1;
10926 lh->standard_opcode_lengths
fe1b8b76 10927 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10928
10929 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10930 for (i = 1; i < lh->opcode_base; ++i)
10931 {
10932 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10933 line_ptr += 1;
10934 }
10935
a738430d 10936 /* Read directory table. */
9b1c24c8 10937 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10938 {
10939 line_ptr += bytes_read;
10940 add_include_dir (lh, cur_dir);
10941 }
10942 line_ptr += bytes_read;
10943
a738430d 10944 /* Read file name table. */
9b1c24c8 10945 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10946 {
10947 unsigned int dir_index, mod_time, length;
10948
10949 line_ptr += bytes_read;
10950 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10951 line_ptr += bytes_read;
10952 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10953 line_ptr += bytes_read;
10954 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10955 line_ptr += bytes_read;
10956
10957 add_file_name (lh, cur_file, dir_index, mod_time, length);
10958 }
10959 line_ptr += bytes_read;
6e70227d 10960 lh->statement_program_start = line_ptr;
debd256d 10961
dce234bc
PP
10962 if (line_ptr > (dwarf2_per_objfile->line.buffer
10963 + dwarf2_per_objfile->line.size))
4d3c2250 10964 complaint (&symfile_complaints,
3e43a32a
MS
10965 _("line number info header doesn't "
10966 "fit in `.debug_line' section"));
debd256d
JB
10967
10968 discard_cleanups (back_to);
10969 return lh;
10970}
c906108c 10971
5fb290d7
DJ
10972/* This function exists to work around a bug in certain compilers
10973 (particularly GCC 2.95), in which the first line number marker of a
10974 function does not show up until after the prologue, right before
10975 the second line number marker. This function shifts ADDRESS down
10976 to the beginning of the function if necessary, and is called on
10977 addresses passed to record_line. */
10978
10979static CORE_ADDR
e142c38c 10980check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10981{
10982 struct function_range *fn;
10983
10984 /* Find the function_range containing address. */
e142c38c 10985 if (!cu->first_fn)
5fb290d7
DJ
10986 return address;
10987
e142c38c
DJ
10988 if (!cu->cached_fn)
10989 cu->cached_fn = cu->first_fn;
5fb290d7 10990
e142c38c 10991 fn = cu->cached_fn;
5fb290d7
DJ
10992 while (fn)
10993 if (fn->lowpc <= address && fn->highpc > address)
10994 goto found;
10995 else
10996 fn = fn->next;
10997
e142c38c
DJ
10998 fn = cu->first_fn;
10999 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
11000 if (fn->lowpc <= address && fn->highpc > address)
11001 goto found;
11002 else
11003 fn = fn->next;
11004
11005 return address;
11006
11007 found:
11008 if (fn->seen_line)
11009 return address;
11010 if (address != fn->lowpc)
4d3c2250 11011 complaint (&symfile_complaints,
e2e0b3e5 11012 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 11013 (unsigned long) address, fn->name);
5fb290d7
DJ
11014 fn->seen_line = 1;
11015 return fn->lowpc;
11016}
11017
c6da4cef
DE
11018/* Subroutine of dwarf_decode_lines to simplify it.
11019 Return the file name of the psymtab for included file FILE_INDEX
11020 in line header LH of PST.
11021 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11022 If space for the result is malloc'd, it will be freed by a cleanup.
11023 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11024
11025static char *
11026psymtab_include_file_name (const struct line_header *lh, int file_index,
11027 const struct partial_symtab *pst,
11028 const char *comp_dir)
11029{
11030 const struct file_entry fe = lh->file_names [file_index];
11031 char *include_name = fe.name;
11032 char *include_name_to_compare = include_name;
11033 char *dir_name = NULL;
72b9f47f
TT
11034 const char *pst_filename;
11035 char *copied_name = NULL;
c6da4cef
DE
11036 int file_is_pst;
11037
11038 if (fe.dir_index)
11039 dir_name = lh->include_dirs[fe.dir_index - 1];
11040
11041 if (!IS_ABSOLUTE_PATH (include_name)
11042 && (dir_name != NULL || comp_dir != NULL))
11043 {
11044 /* Avoid creating a duplicate psymtab for PST.
11045 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11046 Before we do the comparison, however, we need to account
11047 for DIR_NAME and COMP_DIR.
11048 First prepend dir_name (if non-NULL). If we still don't
11049 have an absolute path prepend comp_dir (if non-NULL).
11050 However, the directory we record in the include-file's
11051 psymtab does not contain COMP_DIR (to match the
11052 corresponding symtab(s)).
11053
11054 Example:
11055
11056 bash$ cd /tmp
11057 bash$ gcc -g ./hello.c
11058 include_name = "hello.c"
11059 dir_name = "."
11060 DW_AT_comp_dir = comp_dir = "/tmp"
11061 DW_AT_name = "./hello.c" */
11062
11063 if (dir_name != NULL)
11064 {
11065 include_name = concat (dir_name, SLASH_STRING,
11066 include_name, (char *)NULL);
11067 include_name_to_compare = include_name;
11068 make_cleanup (xfree, include_name);
11069 }
11070 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11071 {
11072 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11073 include_name, (char *)NULL);
11074 }
11075 }
11076
11077 pst_filename = pst->filename;
11078 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11079 {
72b9f47f
TT
11080 copied_name = concat (pst->dirname, SLASH_STRING,
11081 pst_filename, (char *)NULL);
11082 pst_filename = copied_name;
c6da4cef
DE
11083 }
11084
1e3fad37 11085 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
11086
11087 if (include_name_to_compare != include_name)
11088 xfree (include_name_to_compare);
72b9f47f
TT
11089 if (copied_name != NULL)
11090 xfree (copied_name);
c6da4cef
DE
11091
11092 if (file_is_pst)
11093 return NULL;
11094 return include_name;
11095}
11096
c91513d8
PP
11097/* Ignore this record_line request. */
11098
11099static void
11100noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11101{
11102 return;
11103}
11104
aaa75496
JB
11105/* Decode the Line Number Program (LNP) for the given line_header
11106 structure and CU. The actual information extracted and the type
11107 of structures created from the LNP depends on the value of PST.
11108
11109 1. If PST is NULL, then this procedure uses the data from the program
11110 to create all necessary symbol tables, and their linetables.
6e70227d 11111
aaa75496
JB
11112 2. If PST is not NULL, this procedure reads the program to determine
11113 the list of files included by the unit represented by PST, and
c6da4cef
DE
11114 builds all the associated partial symbol tables.
11115
11116 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11117 It is used for relative paths in the line table.
11118 NOTE: When processing partial symtabs (pst != NULL),
11119 comp_dir == pst->dirname.
11120
11121 NOTE: It is important that psymtabs have the same file name (via strcmp)
11122 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11123 symtab we don't use it in the name of the psymtabs we create.
11124 E.g. expand_line_sal requires this when finding psymtabs to expand.
11125 A good testcase for this is mb-inline.exp. */
debd256d 11126
c906108c 11127static void
72b9f47f 11128dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 11129 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 11130{
a8c50c1f 11131 gdb_byte *line_ptr, *extended_end;
fe1b8b76 11132 gdb_byte *line_end;
a8c50c1f 11133 unsigned int bytes_read, extended_len;
c906108c 11134 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
11135 CORE_ADDR baseaddr;
11136 struct objfile *objfile = cu->objfile;
fbf65064 11137 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 11138 const int decode_for_pst_p = (pst != NULL);
cb1df416 11139 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
11140 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11141 = record_line;
e142c38c
DJ
11142
11143 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11144
debd256d
JB
11145 line_ptr = lh->statement_program_start;
11146 line_end = lh->statement_program_end;
c906108c
SS
11147
11148 /* Read the statement sequences until there's nothing left. */
11149 while (line_ptr < line_end)
11150 {
11151 /* state machine registers */
11152 CORE_ADDR address = 0;
11153 unsigned int file = 1;
11154 unsigned int line = 1;
11155 unsigned int column = 0;
debd256d 11156 int is_stmt = lh->default_is_stmt;
c906108c
SS
11157 int basic_block = 0;
11158 int end_sequence = 0;
fbf65064 11159 CORE_ADDR addr;
2dc7f7b3 11160 unsigned char op_index = 0;
c906108c 11161
aaa75496 11162 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 11163 {
aaa75496 11164 /* Start a subfile for the current file of the state machine. */
debd256d
JB
11165 /* lh->include_dirs and lh->file_names are 0-based, but the
11166 directory and file name numbers in the statement program
11167 are 1-based. */
11168 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 11169 char *dir = NULL;
a738430d 11170
debd256d
JB
11171 if (fe->dir_index)
11172 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
11173
11174 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
11175 }
11176
a738430d 11177 /* Decode the table. */
c5aa993b 11178 while (!end_sequence)
c906108c
SS
11179 {
11180 op_code = read_1_byte (abfd, line_ptr);
11181 line_ptr += 1;
59205f5a
JB
11182 if (line_ptr > line_end)
11183 {
11184 dwarf2_debug_line_missing_end_sequence_complaint ();
11185 break;
11186 }
9aa1fe7e 11187
debd256d 11188 if (op_code >= lh->opcode_base)
6e70227d 11189 {
a738430d 11190 /* Special operand. */
debd256d 11191 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
11192 address += (((op_index + (adj_opcode / lh->line_range))
11193 / lh->maximum_ops_per_instruction)
11194 * lh->minimum_instruction_length);
11195 op_index = ((op_index + (adj_opcode / lh->line_range))
11196 % lh->maximum_ops_per_instruction);
debd256d 11197 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 11198 if (lh->num_file_names < file || file == 0)
25e43795 11199 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
11200 /* For now we ignore lines not starting on an
11201 instruction boundary. */
11202 else if (op_index == 0)
25e43795
DJ
11203 {
11204 lh->file_names[file - 1].included_p = 1;
ca5f395d 11205 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11206 {
11207 if (last_subfile != current_subfile)
11208 {
11209 addr = gdbarch_addr_bits_remove (gdbarch, address);
11210 if (last_subfile)
c91513d8 11211 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11212 last_subfile = current_subfile;
11213 }
25e43795 11214 /* Append row to matrix using current values. */
fbf65064
UW
11215 addr = check_cu_functions (address, cu);
11216 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 11217 (*p_record_line) (current_subfile, line, addr);
366da635 11218 }
25e43795 11219 }
ca5f395d 11220 basic_block = 0;
9aa1fe7e
GK
11221 }
11222 else switch (op_code)
c906108c
SS
11223 {
11224 case DW_LNS_extended_op:
3e43a32a
MS
11225 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11226 &bytes_read);
473b7be6 11227 line_ptr += bytes_read;
a8c50c1f 11228 extended_end = line_ptr + extended_len;
c906108c
SS
11229 extended_op = read_1_byte (abfd, line_ptr);
11230 line_ptr += 1;
11231 switch (extended_op)
11232 {
11233 case DW_LNE_end_sequence:
c91513d8 11234 p_record_line = record_line;
c906108c 11235 end_sequence = 1;
c906108c
SS
11236 break;
11237 case DW_LNE_set_address:
e7c27a73 11238 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
11239
11240 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11241 {
11242 /* This line table is for a function which has been
11243 GCd by the linker. Ignore it. PR gdb/12528 */
11244
11245 long line_offset
11246 = line_ptr - dwarf2_per_objfile->line.buffer;
11247
11248 complaint (&symfile_complaints,
11249 _(".debug_line address at offset 0x%lx is 0 "
11250 "[in module %s]"),
11251 line_offset, cu->objfile->name);
11252 p_record_line = noop_record_line;
11253 }
11254
2dc7f7b3 11255 op_index = 0;
107d2387
AC
11256 line_ptr += bytes_read;
11257 address += baseaddr;
c906108c
SS
11258 break;
11259 case DW_LNE_define_file:
debd256d
JB
11260 {
11261 char *cur_file;
11262 unsigned int dir_index, mod_time, length;
6e70227d 11263
3e43a32a
MS
11264 cur_file = read_direct_string (abfd, line_ptr,
11265 &bytes_read);
debd256d
JB
11266 line_ptr += bytes_read;
11267 dir_index =
11268 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11269 line_ptr += bytes_read;
11270 mod_time =
11271 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11272 line_ptr += bytes_read;
11273 length =
11274 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11275 line_ptr += bytes_read;
11276 add_file_name (lh, cur_file, dir_index, mod_time, length);
11277 }
c906108c 11278 break;
d0c6ba3d
CC
11279 case DW_LNE_set_discriminator:
11280 /* The discriminator is not interesting to the debugger;
11281 just ignore it. */
11282 line_ptr = extended_end;
11283 break;
c906108c 11284 default:
4d3c2250 11285 complaint (&symfile_complaints,
e2e0b3e5 11286 _("mangled .debug_line section"));
debd256d 11287 return;
c906108c 11288 }
a8c50c1f
DJ
11289 /* Make sure that we parsed the extended op correctly. If e.g.
11290 we expected a different address size than the producer used,
11291 we may have read the wrong number of bytes. */
11292 if (line_ptr != extended_end)
11293 {
11294 complaint (&symfile_complaints,
11295 _("mangled .debug_line section"));
11296 return;
11297 }
c906108c
SS
11298 break;
11299 case DW_LNS_copy:
59205f5a 11300 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11301 dwarf2_debug_line_missing_file_complaint ();
11302 else
366da635 11303 {
25e43795 11304 lh->file_names[file - 1].included_p = 1;
ca5f395d 11305 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11306 {
11307 if (last_subfile != current_subfile)
11308 {
11309 addr = gdbarch_addr_bits_remove (gdbarch, address);
11310 if (last_subfile)
c91513d8 11311 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11312 last_subfile = current_subfile;
11313 }
11314 addr = check_cu_functions (address, cu);
11315 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 11316 (*p_record_line) (current_subfile, line, addr);
fbf65064 11317 }
366da635 11318 }
c906108c
SS
11319 basic_block = 0;
11320 break;
11321 case DW_LNS_advance_pc:
2dc7f7b3
TT
11322 {
11323 CORE_ADDR adjust
11324 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11325
11326 address += (((op_index + adjust)
11327 / lh->maximum_ops_per_instruction)
11328 * lh->minimum_instruction_length);
11329 op_index = ((op_index + adjust)
11330 % lh->maximum_ops_per_instruction);
11331 line_ptr += bytes_read;
11332 }
c906108c
SS
11333 break;
11334 case DW_LNS_advance_line:
11335 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11336 line_ptr += bytes_read;
11337 break;
11338 case DW_LNS_set_file:
debd256d 11339 {
a738430d
MK
11340 /* The arrays lh->include_dirs and lh->file_names are
11341 0-based, but the directory and file name numbers in
11342 the statement program are 1-based. */
debd256d 11343 struct file_entry *fe;
4f1520fb 11344 char *dir = NULL;
a738430d 11345
debd256d
JB
11346 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11347 line_ptr += bytes_read;
59205f5a 11348 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11349 dwarf2_debug_line_missing_file_complaint ();
11350 else
11351 {
11352 fe = &lh->file_names[file - 1];
11353 if (fe->dir_index)
11354 dir = lh->include_dirs[fe->dir_index - 1];
11355 if (!decode_for_pst_p)
11356 {
11357 last_subfile = current_subfile;
11358 dwarf2_start_subfile (fe->name, dir, comp_dir);
11359 }
11360 }
debd256d 11361 }
c906108c
SS
11362 break;
11363 case DW_LNS_set_column:
11364 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11365 line_ptr += bytes_read;
11366 break;
11367 case DW_LNS_negate_stmt:
11368 is_stmt = (!is_stmt);
11369 break;
11370 case DW_LNS_set_basic_block:
11371 basic_block = 1;
11372 break;
c2c6d25f
JM
11373 /* Add to the address register of the state machine the
11374 address increment value corresponding to special opcode
a738430d
MK
11375 255. I.e., this value is scaled by the minimum
11376 instruction length since special opcode 255 would have
b021a221 11377 scaled the increment. */
c906108c 11378 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11379 {
11380 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11381
11382 address += (((op_index + adjust)
11383 / lh->maximum_ops_per_instruction)
11384 * lh->minimum_instruction_length);
11385 op_index = ((op_index + adjust)
11386 % lh->maximum_ops_per_instruction);
11387 }
c906108c
SS
11388 break;
11389 case DW_LNS_fixed_advance_pc:
11390 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11391 op_index = 0;
c906108c
SS
11392 line_ptr += 2;
11393 break;
9aa1fe7e 11394 default:
a738430d
MK
11395 {
11396 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11397 int i;
a738430d 11398
debd256d 11399 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11400 {
11401 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11402 line_ptr += bytes_read;
11403 }
11404 }
c906108c
SS
11405 }
11406 }
59205f5a
JB
11407 if (lh->num_file_names < file || file == 0)
11408 dwarf2_debug_line_missing_file_complaint ();
11409 else
11410 {
11411 lh->file_names[file - 1].included_p = 1;
11412 if (!decode_for_pst_p)
fbf65064
UW
11413 {
11414 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11415 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11416 }
59205f5a 11417 }
c906108c 11418 }
aaa75496
JB
11419
11420 if (decode_for_pst_p)
11421 {
11422 int file_index;
11423
11424 /* Now that we're done scanning the Line Header Program, we can
11425 create the psymtab of each included file. */
11426 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11427 if (lh->file_names[file_index].included_p == 1)
11428 {
c6da4cef
DE
11429 char *include_name =
11430 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11431 if (include_name != NULL)
aaa75496
JB
11432 dwarf2_create_include_psymtab (include_name, pst, objfile);
11433 }
11434 }
cb1df416
DJ
11435 else
11436 {
11437 /* Make sure a symtab is created for every file, even files
11438 which contain only variables (i.e. no code with associated
11439 line numbers). */
11440
11441 int i;
11442 struct file_entry *fe;
11443
11444 for (i = 0; i < lh->num_file_names; i++)
11445 {
11446 char *dir = NULL;
9a619af0 11447
cb1df416
DJ
11448 fe = &lh->file_names[i];
11449 if (fe->dir_index)
11450 dir = lh->include_dirs[fe->dir_index - 1];
11451 dwarf2_start_subfile (fe->name, dir, comp_dir);
11452
11453 /* Skip the main file; we don't need it, and it must be
11454 allocated last, so that it will show up before the
11455 non-primary symtabs in the objfile's symtab list. */
11456 if (current_subfile == first_subfile)
11457 continue;
11458
11459 if (current_subfile->symtab == NULL)
11460 current_subfile->symtab = allocate_symtab (current_subfile->name,
11461 cu->objfile);
11462 fe->symtab = current_subfile->symtab;
11463 }
11464 }
c906108c
SS
11465}
11466
11467/* Start a subfile for DWARF. FILENAME is the name of the file and
11468 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11469 or NULL if not known. COMP_DIR is the compilation directory for the
11470 linetable's compilation unit or NULL if not known.
c906108c
SS
11471 This routine tries to keep line numbers from identical absolute and
11472 relative file names in a common subfile.
11473
11474 Using the `list' example from the GDB testsuite, which resides in
11475 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11476 of /srcdir/list0.c yields the following debugging information for list0.c:
11477
c5aa993b
JM
11478 DW_AT_name: /srcdir/list0.c
11479 DW_AT_comp_dir: /compdir
357e46e7 11480 files.files[0].name: list0.h
c5aa993b 11481 files.files[0].dir: /srcdir
357e46e7 11482 files.files[1].name: list0.c
c5aa993b 11483 files.files[1].dir: /srcdir
c906108c
SS
11484
11485 The line number information for list0.c has to end up in a single
4f1520fb
FR
11486 subfile, so that `break /srcdir/list0.c:1' works as expected.
11487 start_subfile will ensure that this happens provided that we pass the
11488 concatenation of files.files[1].dir and files.files[1].name as the
11489 subfile's name. */
c906108c
SS
11490
11491static void
3e43a32a
MS
11492dwarf2_start_subfile (char *filename, const char *dirname,
11493 const char *comp_dir)
c906108c 11494{
4f1520fb
FR
11495 char *fullname;
11496
11497 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11498 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11499 second argument to start_subfile. To be consistent, we do the
11500 same here. In order not to lose the line information directory,
11501 we concatenate it to the filename when it makes sense.
11502 Note that the Dwarf3 standard says (speaking of filenames in line
11503 information): ``The directory index is ignored for file names
11504 that represent full path names''. Thus ignoring dirname in the
11505 `else' branch below isn't an issue. */
c906108c 11506
d5166ae1 11507 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11508 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11509 else
11510 fullname = filename;
c906108c 11511
4f1520fb
FR
11512 start_subfile (fullname, comp_dir);
11513
11514 if (fullname != filename)
11515 xfree (fullname);
c906108c
SS
11516}
11517
4c2df51b
DJ
11518static void
11519var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11520 struct dwarf2_cu *cu)
4c2df51b 11521{
e7c27a73
DJ
11522 struct objfile *objfile = cu->objfile;
11523 struct comp_unit_head *cu_header = &cu->header;
11524
4c2df51b
DJ
11525 /* NOTE drow/2003-01-30: There used to be a comment and some special
11526 code here to turn a symbol with DW_AT_external and a
11527 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11528 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11529 with some versions of binutils) where shared libraries could have
11530 relocations against symbols in their debug information - the
11531 minimal symbol would have the right address, but the debug info
11532 would not. It's no longer necessary, because we will explicitly
11533 apply relocations when we read in the debug information now. */
11534
11535 /* A DW_AT_location attribute with no contents indicates that a
11536 variable has been optimized away. */
11537 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11538 {
11539 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11540 return;
11541 }
11542
11543 /* Handle one degenerate form of location expression specially, to
11544 preserve GDB's previous behavior when section offsets are
11545 specified. If this is just a DW_OP_addr then mark this symbol
11546 as LOC_STATIC. */
11547
11548 if (attr_form_is_block (attr)
11549 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11550 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11551 {
891d2f0b 11552 unsigned int dummy;
4c2df51b
DJ
11553
11554 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11555 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11556 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11557 fixup_symbol_section (sym, objfile);
11558 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11559 SYMBOL_SECTION (sym));
4c2df51b
DJ
11560 return;
11561 }
11562
11563 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11564 expression evaluator, and use LOC_COMPUTED only when necessary
11565 (i.e. when the value of a register or memory location is
11566 referenced, or a thread-local block, etc.). Then again, it might
11567 not be worthwhile. I'm assuming that it isn't unless performance
11568 or memory numbers show me otherwise. */
11569
e7c27a73 11570 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11571 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11572
11573 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11574 cu->has_loclist = 1;
4c2df51b
DJ
11575}
11576
c906108c
SS
11577/* Given a pointer to a DWARF information entry, figure out if we need
11578 to make a symbol table entry for it, and if so, create a new entry
11579 and return a pointer to it.
11580 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11581 used the passed type.
11582 If SPACE is not NULL, use it to hold the new symbol. If it is
11583 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11584
11585static struct symbol *
34eaf542
TT
11586new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11587 struct symbol *space)
c906108c 11588{
e7c27a73 11589 struct objfile *objfile = cu->objfile;
c906108c
SS
11590 struct symbol *sym = NULL;
11591 char *name;
11592 struct attribute *attr = NULL;
11593 struct attribute *attr2 = NULL;
e142c38c 11594 CORE_ADDR baseaddr;
e37fd15a
SW
11595 struct pending **list_to_add = NULL;
11596
edb3359d 11597 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11598
11599 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11600
94af9270 11601 name = dwarf2_name (die, cu);
c906108c
SS
11602 if (name)
11603 {
94af9270 11604 const char *linkagename;
34eaf542 11605 int suppress_add = 0;
94af9270 11606
34eaf542
TT
11607 if (space)
11608 sym = space;
11609 else
11610 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11611 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11612
11613 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11614 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11615 linkagename = dwarf2_physname (name, die, cu);
11616 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11617
f55ee35c
JK
11618 /* Fortran does not have mangling standard and the mangling does differ
11619 between gfortran, iFort etc. */
11620 if (cu->language == language_fortran
b250c185 11621 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11622 symbol_set_demangled_name (&(sym->ginfo),
11623 (char *) dwarf2_full_name (name, die, cu),
11624 NULL);
f55ee35c 11625
c906108c 11626 /* Default assumptions.
c5aa993b 11627 Use the passed type or decode it from the die. */
176620f1 11628 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11629 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11630 if (type != NULL)
11631 SYMBOL_TYPE (sym) = type;
11632 else
e7c27a73 11633 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11634 attr = dwarf2_attr (die,
11635 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11636 cu);
c906108c
SS
11637 if (attr)
11638 {
11639 SYMBOL_LINE (sym) = DW_UNSND (attr);
11640 }
cb1df416 11641
edb3359d
DJ
11642 attr = dwarf2_attr (die,
11643 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11644 cu);
cb1df416
DJ
11645 if (attr)
11646 {
11647 int file_index = DW_UNSND (attr);
9a619af0 11648
cb1df416
DJ
11649 if (cu->line_header == NULL
11650 || file_index > cu->line_header->num_file_names)
11651 complaint (&symfile_complaints,
11652 _("file index out of range"));
1c3d648d 11653 else if (file_index > 0)
cb1df416
DJ
11654 {
11655 struct file_entry *fe;
9a619af0 11656
cb1df416
DJ
11657 fe = &cu->line_header->file_names[file_index - 1];
11658 SYMBOL_SYMTAB (sym) = fe->symtab;
11659 }
11660 }
11661
c906108c
SS
11662 switch (die->tag)
11663 {
11664 case DW_TAG_label:
e142c38c 11665 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11666 if (attr)
11667 {
11668 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11669 }
0f5238ed
TT
11670 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11671 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11672 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11673 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11674 break;
11675 case DW_TAG_subprogram:
11676 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11677 finish_block. */
11678 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11679 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11680 if ((attr2 && (DW_UNSND (attr2) != 0))
11681 || cu->language == language_ada)
c906108c 11682 {
2cfa0c8d
JB
11683 /* Subprograms marked external are stored as a global symbol.
11684 Ada subprograms, whether marked external or not, are always
11685 stored as a global symbol, because we want to be able to
11686 access them globally. For instance, we want to be able
11687 to break on a nested subprogram without having to
11688 specify the context. */
e37fd15a 11689 list_to_add = &global_symbols;
c906108c
SS
11690 }
11691 else
11692 {
e37fd15a 11693 list_to_add = cu->list_in_scope;
c906108c
SS
11694 }
11695 break;
edb3359d
DJ
11696 case DW_TAG_inlined_subroutine:
11697 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11698 finish_block. */
11699 SYMBOL_CLASS (sym) = LOC_BLOCK;
11700 SYMBOL_INLINED (sym) = 1;
11701 /* Do not add the symbol to any lists. It will be found via
11702 BLOCK_FUNCTION from the blockvector. */
11703 break;
34eaf542
TT
11704 case DW_TAG_template_value_param:
11705 suppress_add = 1;
11706 /* Fall through. */
72929c62 11707 case DW_TAG_constant:
c906108c 11708 case DW_TAG_variable:
254e6b9e 11709 case DW_TAG_member:
0963b4bd
MS
11710 /* Compilation with minimal debug info may result in
11711 variables with missing type entries. Change the
11712 misleading `void' type to something sensible. */
c906108c 11713 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11714 SYMBOL_TYPE (sym)
46bf5051 11715 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11716
e142c38c 11717 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11718 /* In the case of DW_TAG_member, we should only be called for
11719 static const members. */
11720 if (die->tag == DW_TAG_member)
11721 {
3863f96c
DE
11722 /* dwarf2_add_field uses die_is_declaration,
11723 so we do the same. */
254e6b9e
DE
11724 gdb_assert (die_is_declaration (die, cu));
11725 gdb_assert (attr);
11726 }
c906108c
SS
11727 if (attr)
11728 {
e7c27a73 11729 dwarf2_const_value (attr, sym, cu);
e142c38c 11730 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11731 if (!suppress_add)
34eaf542
TT
11732 {
11733 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11734 list_to_add = &global_symbols;
34eaf542 11735 else
e37fd15a 11736 list_to_add = cu->list_in_scope;
34eaf542 11737 }
c906108c
SS
11738 break;
11739 }
e142c38c 11740 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11741 if (attr)
11742 {
e7c27a73 11743 var_decode_location (attr, sym, cu);
e142c38c 11744 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11745 if (SYMBOL_CLASS (sym) == LOC_STATIC
11746 && SYMBOL_VALUE_ADDRESS (sym) == 0
11747 && !dwarf2_per_objfile->has_section_at_zero)
11748 {
11749 /* When a static variable is eliminated by the linker,
11750 the corresponding debug information is not stripped
11751 out, but the variable address is set to null;
11752 do not add such variables into symbol table. */
11753 }
11754 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11755 {
f55ee35c
JK
11756 /* Workaround gfortran PR debug/40040 - it uses
11757 DW_AT_location for variables in -fPIC libraries which may
11758 get overriden by other libraries/executable and get
11759 a different address. Resolve it by the minimal symbol
11760 which may come from inferior's executable using copy
11761 relocation. Make this workaround only for gfortran as for
11762 other compilers GDB cannot guess the minimal symbol
11763 Fortran mangling kind. */
11764 if (cu->language == language_fortran && die->parent
11765 && die->parent->tag == DW_TAG_module
11766 && cu->producer
11767 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11768 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11769
1c809c68
TT
11770 /* A variable with DW_AT_external is never static,
11771 but it may be block-scoped. */
11772 list_to_add = (cu->list_in_scope == &file_symbols
11773 ? &global_symbols : cu->list_in_scope);
1c809c68 11774 }
c906108c 11775 else
e37fd15a 11776 list_to_add = cu->list_in_scope;
c906108c
SS
11777 }
11778 else
11779 {
11780 /* We do not know the address of this symbol.
c5aa993b
JM
11781 If it is an external symbol and we have type information
11782 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11783 The address of the variable will then be determined from
11784 the minimal symbol table whenever the variable is
11785 referenced. */
e142c38c 11786 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11787 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11788 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11789 {
0fe7935b
DJ
11790 /* A variable with DW_AT_external is never static, but it
11791 may be block-scoped. */
11792 list_to_add = (cu->list_in_scope == &file_symbols
11793 ? &global_symbols : cu->list_in_scope);
11794
c906108c 11795 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11796 }
442ddf59
JK
11797 else if (!die_is_declaration (die, cu))
11798 {
11799 /* Use the default LOC_OPTIMIZED_OUT class. */
11800 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11801 if (!suppress_add)
11802 list_to_add = cu->list_in_scope;
442ddf59 11803 }
c906108c
SS
11804 }
11805 break;
11806 case DW_TAG_formal_parameter:
edb3359d
DJ
11807 /* If we are inside a function, mark this as an argument. If
11808 not, we might be looking at an argument to an inlined function
11809 when we do not have enough information to show inlined frames;
11810 pretend it's a local variable in that case so that the user can
11811 still see it. */
11812 if (context_stack_depth > 0
11813 && context_stack[context_stack_depth - 1].name != NULL)
11814 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11815 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11816 if (attr)
11817 {
e7c27a73 11818 var_decode_location (attr, sym, cu);
c906108c 11819 }
e142c38c 11820 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11821 if (attr)
11822 {
e7c27a73 11823 dwarf2_const_value (attr, sym, cu);
c906108c 11824 }
f346a30d 11825
e37fd15a 11826 list_to_add = cu->list_in_scope;
c906108c
SS
11827 break;
11828 case DW_TAG_unspecified_parameters:
11829 /* From varargs functions; gdb doesn't seem to have any
11830 interest in this information, so just ignore it for now.
11831 (FIXME?) */
11832 break;
34eaf542
TT
11833 case DW_TAG_template_type_param:
11834 suppress_add = 1;
11835 /* Fall through. */
c906108c 11836 case DW_TAG_class_type:
680b30c7 11837 case DW_TAG_interface_type:
c906108c
SS
11838 case DW_TAG_structure_type:
11839 case DW_TAG_union_type:
72019c9c 11840 case DW_TAG_set_type:
c906108c
SS
11841 case DW_TAG_enumeration_type:
11842 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11843 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11844
63d06c5c 11845 {
987504bb 11846 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11847 really ever be static objects: otherwise, if you try
11848 to, say, break of a class's method and you're in a file
11849 which doesn't mention that class, it won't work unless
11850 the check for all static symbols in lookup_symbol_aux
11851 saves you. See the OtherFileClass tests in
11852 gdb.c++/namespace.exp. */
11853
e37fd15a 11854 if (!suppress_add)
34eaf542 11855 {
34eaf542
TT
11856 list_to_add = (cu->list_in_scope == &file_symbols
11857 && (cu->language == language_cplus
11858 || cu->language == language_java)
11859 ? &global_symbols : cu->list_in_scope);
63d06c5c 11860
64382290
TT
11861 /* The semantics of C++ state that "struct foo {
11862 ... }" also defines a typedef for "foo". A Java
11863 class declaration also defines a typedef for the
11864 class. */
11865 if (cu->language == language_cplus
11866 || cu->language == language_java
11867 || cu->language == language_ada)
11868 {
11869 /* The symbol's name is already allocated along
11870 with this objfile, so we don't need to
11871 duplicate it for the type. */
11872 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11873 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11874 }
63d06c5c
DC
11875 }
11876 }
c906108c
SS
11877 break;
11878 case DW_TAG_typedef:
63d06c5c
DC
11879 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11880 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11881 list_to_add = cu->list_in_scope;
63d06c5c 11882 break;
c906108c 11883 case DW_TAG_base_type:
a02abb62 11884 case DW_TAG_subrange_type:
c906108c 11885 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11886 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11887 list_to_add = cu->list_in_scope;
c906108c
SS
11888 break;
11889 case DW_TAG_enumerator:
e142c38c 11890 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11891 if (attr)
11892 {
e7c27a73 11893 dwarf2_const_value (attr, sym, cu);
c906108c 11894 }
63d06c5c
DC
11895 {
11896 /* NOTE: carlton/2003-11-10: See comment above in the
11897 DW_TAG_class_type, etc. block. */
11898
e142c38c 11899 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11900 && (cu->language == language_cplus
11901 || cu->language == language_java)
e142c38c 11902 ? &global_symbols : cu->list_in_scope);
63d06c5c 11903 }
c906108c 11904 break;
5c4e30ca
DC
11905 case DW_TAG_namespace:
11906 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11907 list_to_add = &global_symbols;
5c4e30ca 11908 break;
c906108c
SS
11909 default:
11910 /* Not a tag we recognize. Hopefully we aren't processing
11911 trash data, but since we must specifically ignore things
11912 we don't recognize, there is nothing else we should do at
0963b4bd 11913 this point. */
e2e0b3e5 11914 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11915 dwarf_tag_name (die->tag));
c906108c
SS
11916 break;
11917 }
df8a16a1 11918
e37fd15a
SW
11919 if (suppress_add)
11920 {
11921 sym->hash_next = objfile->template_symbols;
11922 objfile->template_symbols = sym;
11923 list_to_add = NULL;
11924 }
11925
11926 if (list_to_add != NULL)
11927 add_symbol_to_list (sym, list_to_add);
11928
df8a16a1
DJ
11929 /* For the benefit of old versions of GCC, check for anonymous
11930 namespaces based on the demangled name. */
11931 if (!processing_has_namespace_info
94af9270 11932 && cu->language == language_cplus)
df8a16a1 11933 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
11934 }
11935 return (sym);
11936}
11937
34eaf542
TT
11938/* A wrapper for new_symbol_full that always allocates a new symbol. */
11939
11940static struct symbol *
11941new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11942{
11943 return new_symbol_full (die, type, cu, NULL);
11944}
11945
98bfdba5
PA
11946/* Given an attr with a DW_FORM_dataN value in host byte order,
11947 zero-extend it as appropriate for the symbol's type. The DWARF
11948 standard (v4) is not entirely clear about the meaning of using
11949 DW_FORM_dataN for a constant with a signed type, where the type is
11950 wider than the data. The conclusion of a discussion on the DWARF
11951 list was that this is unspecified. We choose to always zero-extend
11952 because that is the interpretation long in use by GCC. */
c906108c 11953
98bfdba5
PA
11954static gdb_byte *
11955dwarf2_const_value_data (struct attribute *attr, struct type *type,
11956 const char *name, struct obstack *obstack,
11957 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11958{
e7c27a73 11959 struct objfile *objfile = cu->objfile;
e17a4113
UW
11960 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11961 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11962 LONGEST l = DW_UNSND (attr);
11963
11964 if (bits < sizeof (*value) * 8)
11965 {
11966 l &= ((LONGEST) 1 << bits) - 1;
11967 *value = l;
11968 }
11969 else if (bits == sizeof (*value) * 8)
11970 *value = l;
11971 else
11972 {
11973 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11974 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11975 return bytes;
11976 }
11977
11978 return NULL;
11979}
11980
11981/* Read a constant value from an attribute. Either set *VALUE, or if
11982 the value does not fit in *VALUE, set *BYTES - either already
11983 allocated on the objfile obstack, or newly allocated on OBSTACK,
11984 or, set *BATON, if we translated the constant to a location
11985 expression. */
11986
11987static void
11988dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11989 const char *name, struct obstack *obstack,
11990 struct dwarf2_cu *cu,
11991 long *value, gdb_byte **bytes,
11992 struct dwarf2_locexpr_baton **baton)
11993{
11994 struct objfile *objfile = cu->objfile;
11995 struct comp_unit_head *cu_header = &cu->header;
c906108c 11996 struct dwarf_block *blk;
98bfdba5
PA
11997 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11998 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11999
12000 *value = 0;
12001 *bytes = NULL;
12002 *baton = NULL;
c906108c
SS
12003
12004 switch (attr->form)
12005 {
12006 case DW_FORM_addr:
ac56253d 12007 {
ac56253d
TT
12008 gdb_byte *data;
12009
98bfdba5
PA
12010 if (TYPE_LENGTH (type) != cu_header->addr_size)
12011 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 12012 cu_header->addr_size,
98bfdba5 12013 TYPE_LENGTH (type));
ac56253d
TT
12014 /* Symbols of this form are reasonably rare, so we just
12015 piggyback on the existing location code rather than writing
12016 a new implementation of symbol_computed_ops. */
98bfdba5
PA
12017 *baton = obstack_alloc (&objfile->objfile_obstack,
12018 sizeof (struct dwarf2_locexpr_baton));
12019 (*baton)->per_cu = cu->per_cu;
12020 gdb_assert ((*baton)->per_cu);
ac56253d 12021
98bfdba5
PA
12022 (*baton)->size = 2 + cu_header->addr_size;
12023 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12024 (*baton)->data = data;
ac56253d
TT
12025
12026 data[0] = DW_OP_addr;
12027 store_unsigned_integer (&data[1], cu_header->addr_size,
12028 byte_order, DW_ADDR (attr));
12029 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 12030 }
c906108c 12031 break;
4ac36638 12032 case DW_FORM_string:
93b5768b 12033 case DW_FORM_strp:
98bfdba5
PA
12034 /* DW_STRING is already allocated on the objfile obstack, point
12035 directly to it. */
12036 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 12037 break;
c906108c
SS
12038 case DW_FORM_block1:
12039 case DW_FORM_block2:
12040 case DW_FORM_block4:
12041 case DW_FORM_block:
2dc7f7b3 12042 case DW_FORM_exprloc:
c906108c 12043 blk = DW_BLOCK (attr);
98bfdba5
PA
12044 if (TYPE_LENGTH (type) != blk->size)
12045 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12046 TYPE_LENGTH (type));
12047 *bytes = blk->data;
c906108c 12048 break;
2df3850c
JM
12049
12050 /* The DW_AT_const_value attributes are supposed to carry the
12051 symbol's value "represented as it would be on the target
12052 architecture." By the time we get here, it's already been
12053 converted to host endianness, so we just need to sign- or
12054 zero-extend it as appropriate. */
12055 case DW_FORM_data1:
3e43a32a
MS
12056 *bytes = dwarf2_const_value_data (attr, type, name,
12057 obstack, cu, value, 8);
2df3850c 12058 break;
c906108c 12059 case DW_FORM_data2:
3e43a32a
MS
12060 *bytes = dwarf2_const_value_data (attr, type, name,
12061 obstack, cu, value, 16);
2df3850c 12062 break;
c906108c 12063 case DW_FORM_data4:
3e43a32a
MS
12064 *bytes = dwarf2_const_value_data (attr, type, name,
12065 obstack, cu, value, 32);
2df3850c 12066 break;
c906108c 12067 case DW_FORM_data8:
3e43a32a
MS
12068 *bytes = dwarf2_const_value_data (attr, type, name,
12069 obstack, cu, value, 64);
2df3850c
JM
12070 break;
12071
c906108c 12072 case DW_FORM_sdata:
98bfdba5 12073 *value = DW_SND (attr);
2df3850c
JM
12074 break;
12075
c906108c 12076 case DW_FORM_udata:
98bfdba5 12077 *value = DW_UNSND (attr);
c906108c 12078 break;
2df3850c 12079
c906108c 12080 default:
4d3c2250 12081 complaint (&symfile_complaints,
e2e0b3e5 12082 _("unsupported const value attribute form: '%s'"),
4d3c2250 12083 dwarf_form_name (attr->form));
98bfdba5 12084 *value = 0;
c906108c
SS
12085 break;
12086 }
12087}
12088
2df3850c 12089
98bfdba5
PA
12090/* Copy constant value from an attribute to a symbol. */
12091
2df3850c 12092static void
98bfdba5
PA
12093dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12094 struct dwarf2_cu *cu)
2df3850c 12095{
98bfdba5
PA
12096 struct objfile *objfile = cu->objfile;
12097 struct comp_unit_head *cu_header = &cu->header;
12098 long value;
12099 gdb_byte *bytes;
12100 struct dwarf2_locexpr_baton *baton;
2df3850c 12101
98bfdba5
PA
12102 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12103 SYMBOL_PRINT_NAME (sym),
12104 &objfile->objfile_obstack, cu,
12105 &value, &bytes, &baton);
2df3850c 12106
98bfdba5
PA
12107 if (baton != NULL)
12108 {
12109 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12110 SYMBOL_LOCATION_BATON (sym) = baton;
12111 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12112 }
12113 else if (bytes != NULL)
12114 {
12115 SYMBOL_VALUE_BYTES (sym) = bytes;
12116 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12117 }
12118 else
12119 {
12120 SYMBOL_VALUE (sym) = value;
12121 SYMBOL_CLASS (sym) = LOC_CONST;
12122 }
2df3850c
JM
12123}
12124
c906108c
SS
12125/* Return the type of the die in question using its DW_AT_type attribute. */
12126
12127static struct type *
e7c27a73 12128die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12129{
c906108c 12130 struct attribute *type_attr;
c906108c 12131
e142c38c 12132 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
12133 if (!type_attr)
12134 {
12135 /* A missing DW_AT_type represents a void type. */
46bf5051 12136 return objfile_type (cu->objfile)->builtin_void;
c906108c 12137 }
348e048f 12138
673bfd45 12139 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12140}
12141
b4ba55a1
JB
12142/* True iff CU's producer generates GNAT Ada auxiliary information
12143 that allows to find parallel types through that information instead
12144 of having to do expensive parallel lookups by type name. */
12145
12146static int
12147need_gnat_info (struct dwarf2_cu *cu)
12148{
12149 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12150 of GNAT produces this auxiliary information, without any indication
12151 that it is produced. Part of enhancing the FSF version of GNAT
12152 to produce that information will be to put in place an indicator
12153 that we can use in order to determine whether the descriptive type
12154 info is available or not. One suggestion that has been made is
12155 to use a new attribute, attached to the CU die. For now, assume
12156 that the descriptive type info is not available. */
12157 return 0;
12158}
12159
b4ba55a1
JB
12160/* Return the auxiliary type of the die in question using its
12161 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12162 attribute is not present. */
12163
12164static struct type *
12165die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12166{
b4ba55a1 12167 struct attribute *type_attr;
b4ba55a1
JB
12168
12169 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12170 if (!type_attr)
12171 return NULL;
12172
673bfd45 12173 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
12174}
12175
12176/* If DIE has a descriptive_type attribute, then set the TYPE's
12177 descriptive type accordingly. */
12178
12179static void
12180set_descriptive_type (struct type *type, struct die_info *die,
12181 struct dwarf2_cu *cu)
12182{
12183 struct type *descriptive_type = die_descriptive_type (die, cu);
12184
12185 if (descriptive_type)
12186 {
12187 ALLOCATE_GNAT_AUX_TYPE (type);
12188 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12189 }
12190}
12191
c906108c
SS
12192/* Return the containing type of the die in question using its
12193 DW_AT_containing_type attribute. */
12194
12195static struct type *
e7c27a73 12196die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12197{
c906108c 12198 struct attribute *type_attr;
c906108c 12199
e142c38c 12200 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
12201 if (!type_attr)
12202 error (_("Dwarf Error: Problem turning containing type into gdb type "
12203 "[in module %s]"), cu->objfile->name);
12204
673bfd45 12205 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12206}
12207
673bfd45
DE
12208/* Look up the type of DIE in CU using its type attribute ATTR.
12209 If there is no type substitute an error marker. */
12210
c906108c 12211static struct type *
673bfd45
DE
12212lookup_die_type (struct die_info *die, struct attribute *attr,
12213 struct dwarf2_cu *cu)
c906108c 12214{
f792889a
DJ
12215 struct type *this_type;
12216
673bfd45
DE
12217 /* First see if we have it cached. */
12218
12219 if (is_ref_attr (attr))
12220 {
12221 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12222
12223 this_type = get_die_type_at_offset (offset, cu->per_cu);
12224 }
55f1336d 12225 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
12226 {
12227 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12228 struct dwarf2_cu *sig_cu;
12229 unsigned int offset;
12230
12231 /* sig_type will be NULL if the signatured type is missing from
12232 the debug info. */
12233 if (sig_type == NULL)
12234 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12235 "at 0x%x [in module %s]"),
12236 die->offset, cu->objfile->name);
12237
8b70b953 12238 gdb_assert (sig_type->per_cu.debug_type_section);
b3c8eb43 12239 offset = sig_type->per_cu.offset + sig_type->type_offset;
673bfd45
DE
12240 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12241 }
12242 else
12243 {
12244 dump_die_for_error (die);
12245 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12246 dwarf_attr_name (attr->name), cu->objfile->name);
12247 }
12248
12249 /* If not cached we need to read it in. */
12250
12251 if (this_type == NULL)
12252 {
12253 struct die_info *type_die;
12254 struct dwarf2_cu *type_cu = cu;
12255
12256 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12257 /* If the type is cached, we should have found it above. */
12258 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12259 this_type = read_type_die_1 (type_die, type_cu);
12260 }
12261
12262 /* If we still don't have a type use an error marker. */
12263
12264 if (this_type == NULL)
c906108c 12265 {
b00fdb78
TT
12266 char *message, *saved;
12267
12268 /* read_type_die already issued a complaint. */
12269 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12270 cu->objfile->name,
12271 cu->header.offset,
12272 die->offset);
12273 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
12274 message, strlen (message));
12275 xfree (message);
12276
12277 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 12278 }
673bfd45 12279
f792889a 12280 return this_type;
c906108c
SS
12281}
12282
673bfd45
DE
12283/* Return the type in DIE, CU.
12284 Returns NULL for invalid types.
12285
12286 This first does a lookup in the appropriate type_hash table,
12287 and only reads the die in if necessary.
12288
12289 NOTE: This can be called when reading in partial or full symbols. */
12290
f792889a 12291static struct type *
e7c27a73 12292read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12293{
f792889a
DJ
12294 struct type *this_type;
12295
12296 this_type = get_die_type (die, cu);
12297 if (this_type)
12298 return this_type;
12299
673bfd45
DE
12300 return read_type_die_1 (die, cu);
12301}
12302
12303/* Read the type in DIE, CU.
12304 Returns NULL for invalid types. */
12305
12306static struct type *
12307read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12308{
12309 struct type *this_type = NULL;
12310
c906108c
SS
12311 switch (die->tag)
12312 {
12313 case DW_TAG_class_type:
680b30c7 12314 case DW_TAG_interface_type:
c906108c
SS
12315 case DW_TAG_structure_type:
12316 case DW_TAG_union_type:
f792889a 12317 this_type = read_structure_type (die, cu);
c906108c
SS
12318 break;
12319 case DW_TAG_enumeration_type:
f792889a 12320 this_type = read_enumeration_type (die, cu);
c906108c
SS
12321 break;
12322 case DW_TAG_subprogram:
12323 case DW_TAG_subroutine_type:
edb3359d 12324 case DW_TAG_inlined_subroutine:
f792889a 12325 this_type = read_subroutine_type (die, cu);
c906108c
SS
12326 break;
12327 case DW_TAG_array_type:
f792889a 12328 this_type = read_array_type (die, cu);
c906108c 12329 break;
72019c9c 12330 case DW_TAG_set_type:
f792889a 12331 this_type = read_set_type (die, cu);
72019c9c 12332 break;
c906108c 12333 case DW_TAG_pointer_type:
f792889a 12334 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12335 break;
12336 case DW_TAG_ptr_to_member_type:
f792889a 12337 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12338 break;
12339 case DW_TAG_reference_type:
f792889a 12340 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12341 break;
12342 case DW_TAG_const_type:
f792889a 12343 this_type = read_tag_const_type (die, cu);
c906108c
SS
12344 break;
12345 case DW_TAG_volatile_type:
f792889a 12346 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12347 break;
12348 case DW_TAG_string_type:
f792889a 12349 this_type = read_tag_string_type (die, cu);
c906108c
SS
12350 break;
12351 case DW_TAG_typedef:
f792889a 12352 this_type = read_typedef (die, cu);
c906108c 12353 break;
a02abb62 12354 case DW_TAG_subrange_type:
f792889a 12355 this_type = read_subrange_type (die, cu);
a02abb62 12356 break;
c906108c 12357 case DW_TAG_base_type:
f792889a 12358 this_type = read_base_type (die, cu);
c906108c 12359 break;
81a17f79 12360 case DW_TAG_unspecified_type:
f792889a 12361 this_type = read_unspecified_type (die, cu);
81a17f79 12362 break;
0114d602
DJ
12363 case DW_TAG_namespace:
12364 this_type = read_namespace_type (die, cu);
12365 break;
f55ee35c
JK
12366 case DW_TAG_module:
12367 this_type = read_module_type (die, cu);
12368 break;
c906108c 12369 default:
3e43a32a
MS
12370 complaint (&symfile_complaints,
12371 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12372 dwarf_tag_name (die->tag));
c906108c
SS
12373 break;
12374 }
63d06c5c 12375
f792889a 12376 return this_type;
63d06c5c
DC
12377}
12378
abc72ce4
DE
12379/* See if we can figure out if the class lives in a namespace. We do
12380 this by looking for a member function; its demangled name will
12381 contain namespace info, if there is any.
12382 Return the computed name or NULL.
12383 Space for the result is allocated on the objfile's obstack.
12384 This is the full-die version of guess_partial_die_structure_name.
12385 In this case we know DIE has no useful parent. */
12386
12387static char *
12388guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12389{
12390 struct die_info *spec_die;
12391 struct dwarf2_cu *spec_cu;
12392 struct die_info *child;
12393
12394 spec_cu = cu;
12395 spec_die = die_specification (die, &spec_cu);
12396 if (spec_die != NULL)
12397 {
12398 die = spec_die;
12399 cu = spec_cu;
12400 }
12401
12402 for (child = die->child;
12403 child != NULL;
12404 child = child->sibling)
12405 {
12406 if (child->tag == DW_TAG_subprogram)
12407 {
12408 struct attribute *attr;
12409
12410 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12411 if (attr == NULL)
12412 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12413 if (attr != NULL)
12414 {
12415 char *actual_name
12416 = language_class_name_from_physname (cu->language_defn,
12417 DW_STRING (attr));
12418 char *name = NULL;
12419
12420 if (actual_name != NULL)
12421 {
12422 char *die_name = dwarf2_name (die, cu);
12423
12424 if (die_name != NULL
12425 && strcmp (die_name, actual_name) != 0)
12426 {
12427 /* Strip off the class name from the full name.
12428 We want the prefix. */
12429 int die_name_len = strlen (die_name);
12430 int actual_name_len = strlen (actual_name);
12431
12432 /* Test for '::' as a sanity check. */
12433 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12434 && actual_name[actual_name_len
12435 - die_name_len - 1] == ':')
abc72ce4
DE
12436 name =
12437 obsavestring (actual_name,
12438 actual_name_len - die_name_len - 2,
12439 &cu->objfile->objfile_obstack);
12440 }
12441 }
12442 xfree (actual_name);
12443 return name;
12444 }
12445 }
12446 }
12447
12448 return NULL;
12449}
12450
96408a79
SA
12451/* GCC might emit a nameless typedef that has a linkage name. Determine the
12452 prefix part in such case. See
12453 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12454
12455static char *
12456anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12457{
12458 struct attribute *attr;
12459 char *base;
12460
12461 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12462 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12463 return NULL;
12464
12465 attr = dwarf2_attr (die, DW_AT_name, cu);
12466 if (attr != NULL && DW_STRING (attr) != NULL)
12467 return NULL;
12468
12469 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12470 if (attr == NULL)
12471 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12472 if (attr == NULL || DW_STRING (attr) == NULL)
12473 return NULL;
12474
12475 /* dwarf2_name had to be already called. */
12476 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12477
12478 /* Strip the base name, keep any leading namespaces/classes. */
12479 base = strrchr (DW_STRING (attr), ':');
12480 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12481 return "";
12482
12483 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12484 &cu->objfile->objfile_obstack);
12485}
12486
fdde2d81 12487/* Return the name of the namespace/class that DIE is defined within,
0114d602 12488 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12489
0114d602
DJ
12490 For example, if we're within the method foo() in the following
12491 code:
12492
12493 namespace N {
12494 class C {
12495 void foo () {
12496 }
12497 };
12498 }
12499
12500 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
12501
12502static char *
e142c38c 12503determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12504{
0114d602
DJ
12505 struct die_info *parent, *spec_die;
12506 struct dwarf2_cu *spec_cu;
12507 struct type *parent_type;
96408a79 12508 char *retval;
63d06c5c 12509
f55ee35c
JK
12510 if (cu->language != language_cplus && cu->language != language_java
12511 && cu->language != language_fortran)
0114d602
DJ
12512 return "";
12513
96408a79
SA
12514 retval = anonymous_struct_prefix (die, cu);
12515 if (retval)
12516 return retval;
12517
0114d602
DJ
12518 /* We have to be careful in the presence of DW_AT_specification.
12519 For example, with GCC 3.4, given the code
12520
12521 namespace N {
12522 void foo() {
12523 // Definition of N::foo.
12524 }
12525 }
12526
12527 then we'll have a tree of DIEs like this:
12528
12529 1: DW_TAG_compile_unit
12530 2: DW_TAG_namespace // N
12531 3: DW_TAG_subprogram // declaration of N::foo
12532 4: DW_TAG_subprogram // definition of N::foo
12533 DW_AT_specification // refers to die #3
12534
12535 Thus, when processing die #4, we have to pretend that we're in
12536 the context of its DW_AT_specification, namely the contex of die
12537 #3. */
12538 spec_cu = cu;
12539 spec_die = die_specification (die, &spec_cu);
12540 if (spec_die == NULL)
12541 parent = die->parent;
12542 else
63d06c5c 12543 {
0114d602
DJ
12544 parent = spec_die->parent;
12545 cu = spec_cu;
63d06c5c 12546 }
0114d602
DJ
12547
12548 if (parent == NULL)
12549 return "";
98bfdba5
PA
12550 else if (parent->building_fullname)
12551 {
12552 const char *name;
12553 const char *parent_name;
12554
12555 /* It has been seen on RealView 2.2 built binaries,
12556 DW_TAG_template_type_param types actually _defined_ as
12557 children of the parent class:
12558
12559 enum E {};
12560 template class <class Enum> Class{};
12561 Class<enum E> class_e;
12562
12563 1: DW_TAG_class_type (Class)
12564 2: DW_TAG_enumeration_type (E)
12565 3: DW_TAG_enumerator (enum1:0)
12566 3: DW_TAG_enumerator (enum2:1)
12567 ...
12568 2: DW_TAG_template_type_param
12569 DW_AT_type DW_FORM_ref_udata (E)
12570
12571 Besides being broken debug info, it can put GDB into an
12572 infinite loop. Consider:
12573
12574 When we're building the full name for Class<E>, we'll start
12575 at Class, and go look over its template type parameters,
12576 finding E. We'll then try to build the full name of E, and
12577 reach here. We're now trying to build the full name of E,
12578 and look over the parent DIE for containing scope. In the
12579 broken case, if we followed the parent DIE of E, we'd again
12580 find Class, and once again go look at its template type
12581 arguments, etc., etc. Simply don't consider such parent die
12582 as source-level parent of this die (it can't be, the language
12583 doesn't allow it), and break the loop here. */
12584 name = dwarf2_name (die, cu);
12585 parent_name = dwarf2_name (parent, cu);
12586 complaint (&symfile_complaints,
12587 _("template param type '%s' defined within parent '%s'"),
12588 name ? name : "<unknown>",
12589 parent_name ? parent_name : "<unknown>");
12590 return "";
12591 }
63d06c5c 12592 else
0114d602
DJ
12593 switch (parent->tag)
12594 {
63d06c5c 12595 case DW_TAG_namespace:
0114d602 12596 parent_type = read_type_die (parent, cu);
acebe513
UW
12597 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12598 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12599 Work around this problem here. */
12600 if (cu->language == language_cplus
12601 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12602 return "";
0114d602
DJ
12603 /* We give a name to even anonymous namespaces. */
12604 return TYPE_TAG_NAME (parent_type);
63d06c5c 12605 case DW_TAG_class_type:
680b30c7 12606 case DW_TAG_interface_type:
63d06c5c 12607 case DW_TAG_structure_type:
0114d602 12608 case DW_TAG_union_type:
f55ee35c 12609 case DW_TAG_module:
0114d602
DJ
12610 parent_type = read_type_die (parent, cu);
12611 if (TYPE_TAG_NAME (parent_type) != NULL)
12612 return TYPE_TAG_NAME (parent_type);
12613 else
12614 /* An anonymous structure is only allowed non-static data
12615 members; no typedefs, no member functions, et cetera.
12616 So it does not need a prefix. */
12617 return "";
abc72ce4
DE
12618 case DW_TAG_compile_unit:
12619 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12620 if (cu->language == language_cplus
8b70b953 12621 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12622 && die->child != NULL
12623 && (die->tag == DW_TAG_class_type
12624 || die->tag == DW_TAG_structure_type
12625 || die->tag == DW_TAG_union_type))
12626 {
12627 char *name = guess_full_die_structure_name (die, cu);
12628 if (name != NULL)
12629 return name;
12630 }
12631 return "";
63d06c5c 12632 default:
8176b9b8 12633 return determine_prefix (parent, cu);
63d06c5c 12634 }
63d06c5c
DC
12635}
12636
3e43a32a
MS
12637/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12638 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12639 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12640 an obconcat, otherwise allocate storage for the result. The CU argument is
12641 used to determine the language and hence, the appropriate separator. */
987504bb 12642
f55ee35c 12643#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12644
12645static char *
f55ee35c
JK
12646typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12647 int physname, struct dwarf2_cu *cu)
63d06c5c 12648{
f55ee35c 12649 const char *lead = "";
5c315b68 12650 const char *sep;
63d06c5c 12651
3e43a32a
MS
12652 if (suffix == NULL || suffix[0] == '\0'
12653 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12654 sep = "";
12655 else if (cu->language == language_java)
12656 sep = ".";
f55ee35c
JK
12657 else if (cu->language == language_fortran && physname)
12658 {
12659 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12660 DW_AT_MIPS_linkage_name is preferred and used instead. */
12661
12662 lead = "__";
12663 sep = "_MOD_";
12664 }
987504bb
JJ
12665 else
12666 sep = "::";
63d06c5c 12667
6dd47d34
DE
12668 if (prefix == NULL)
12669 prefix = "";
12670 if (suffix == NULL)
12671 suffix = "";
12672
987504bb
JJ
12673 if (obs == NULL)
12674 {
3e43a32a
MS
12675 char *retval
12676 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12677
f55ee35c
JK
12678 strcpy (retval, lead);
12679 strcat (retval, prefix);
6dd47d34
DE
12680 strcat (retval, sep);
12681 strcat (retval, suffix);
63d06c5c
DC
12682 return retval;
12683 }
987504bb
JJ
12684 else
12685 {
12686 /* We have an obstack. */
f55ee35c 12687 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12688 }
63d06c5c
DC
12689}
12690
c906108c
SS
12691/* Return sibling of die, NULL if no sibling. */
12692
f9aca02d 12693static struct die_info *
fba45db2 12694sibling_die (struct die_info *die)
c906108c 12695{
639d11d3 12696 return die->sibling;
c906108c
SS
12697}
12698
71c25dea
TT
12699/* Get name of a die, return NULL if not found. */
12700
12701static char *
12702dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12703 struct obstack *obstack)
12704{
12705 if (name && cu->language == language_cplus)
12706 {
12707 char *canon_name = cp_canonicalize_string (name);
12708
12709 if (canon_name != NULL)
12710 {
12711 if (strcmp (canon_name, name) != 0)
12712 name = obsavestring (canon_name, strlen (canon_name),
12713 obstack);
12714 xfree (canon_name);
12715 }
12716 }
12717
12718 return name;
c906108c
SS
12719}
12720
9219021c
DC
12721/* Get name of a die, return NULL if not found. */
12722
12723static char *
e142c38c 12724dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12725{
12726 struct attribute *attr;
12727
e142c38c 12728 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12729 if ((!attr || !DW_STRING (attr))
12730 && die->tag != DW_TAG_class_type
12731 && die->tag != DW_TAG_interface_type
12732 && die->tag != DW_TAG_structure_type
12733 && die->tag != DW_TAG_union_type)
71c25dea
TT
12734 return NULL;
12735
12736 switch (die->tag)
12737 {
12738 case DW_TAG_compile_unit:
12739 /* Compilation units have a DW_AT_name that is a filename, not
12740 a source language identifier. */
12741 case DW_TAG_enumeration_type:
12742 case DW_TAG_enumerator:
12743 /* These tags always have simple identifiers already; no need
12744 to canonicalize them. */
12745 return DW_STRING (attr);
907af001 12746
418835cc
KS
12747 case DW_TAG_subprogram:
12748 /* Java constructors will all be named "<init>", so return
12749 the class name when we see this special case. */
12750 if (cu->language == language_java
12751 && DW_STRING (attr) != NULL
12752 && strcmp (DW_STRING (attr), "<init>") == 0)
12753 {
12754 struct dwarf2_cu *spec_cu = cu;
12755 struct die_info *spec_die;
12756
12757 /* GCJ will output '<init>' for Java constructor names.
12758 For this special case, return the name of the parent class. */
12759
12760 /* GCJ may output suprogram DIEs with AT_specification set.
12761 If so, use the name of the specified DIE. */
12762 spec_die = die_specification (die, &spec_cu);
12763 if (spec_die != NULL)
12764 return dwarf2_name (spec_die, spec_cu);
12765
12766 do
12767 {
12768 die = die->parent;
12769 if (die->tag == DW_TAG_class_type)
12770 return dwarf2_name (die, cu);
12771 }
12772 while (die->tag != DW_TAG_compile_unit);
12773 }
907af001
UW
12774 break;
12775
12776 case DW_TAG_class_type:
12777 case DW_TAG_interface_type:
12778 case DW_TAG_structure_type:
12779 case DW_TAG_union_type:
12780 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12781 structures or unions. These were of the form "._%d" in GCC 4.1,
12782 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12783 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12784 if (attr && DW_STRING (attr)
12785 && (strncmp (DW_STRING (attr), "._", 2) == 0
12786 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12787 return NULL;
53832f31
TT
12788
12789 /* GCC might emit a nameless typedef that has a linkage name. See
12790 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12791 if (!attr || DW_STRING (attr) == NULL)
12792 {
df5c6c50 12793 char *demangled = NULL;
53832f31
TT
12794
12795 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12796 if (attr == NULL)
12797 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12798
12799 if (attr == NULL || DW_STRING (attr) == NULL)
12800 return NULL;
12801
df5c6c50
JK
12802 /* Avoid demangling DW_STRING (attr) the second time on a second
12803 call for the same DIE. */
12804 if (!DW_STRING_IS_CANONICAL (attr))
12805 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12806
12807 if (demangled)
12808 {
96408a79
SA
12809 char *base;
12810
53832f31 12811 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
12812 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12813 &cu->objfile->objfile_obstack);
53832f31
TT
12814 DW_STRING_IS_CANONICAL (attr) = 1;
12815 xfree (demangled);
96408a79
SA
12816
12817 /* Strip any leading namespaces/classes, keep only the base name.
12818 DW_AT_name for named DIEs does not contain the prefixes. */
12819 base = strrchr (DW_STRING (attr), ':');
12820 if (base && base > DW_STRING (attr) && base[-1] == ':')
12821 return &base[1];
12822 else
12823 return DW_STRING (attr);
53832f31
TT
12824 }
12825 }
907af001
UW
12826 break;
12827
71c25dea 12828 default:
907af001
UW
12829 break;
12830 }
12831
12832 if (!DW_STRING_IS_CANONICAL (attr))
12833 {
12834 DW_STRING (attr)
12835 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12836 &cu->objfile->objfile_obstack);
12837 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12838 }
907af001 12839 return DW_STRING (attr);
9219021c
DC
12840}
12841
12842/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12843 is none. *EXT_CU is the CU containing DIE on input, and the CU
12844 containing the return value on output. */
9219021c
DC
12845
12846static struct die_info *
f2f0e013 12847dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12848{
12849 struct attribute *attr;
9219021c 12850
f2f0e013 12851 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12852 if (attr == NULL)
12853 return NULL;
12854
f2f0e013 12855 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12856}
12857
c906108c
SS
12858/* Convert a DIE tag into its string name. */
12859
12860static char *
aa1ee363 12861dwarf_tag_name (unsigned tag)
c906108c
SS
12862{
12863 switch (tag)
12864 {
12865 case DW_TAG_padding:
12866 return "DW_TAG_padding";
12867 case DW_TAG_array_type:
12868 return "DW_TAG_array_type";
12869 case DW_TAG_class_type:
12870 return "DW_TAG_class_type";
12871 case DW_TAG_entry_point:
12872 return "DW_TAG_entry_point";
12873 case DW_TAG_enumeration_type:
12874 return "DW_TAG_enumeration_type";
12875 case DW_TAG_formal_parameter:
12876 return "DW_TAG_formal_parameter";
12877 case DW_TAG_imported_declaration:
12878 return "DW_TAG_imported_declaration";
12879 case DW_TAG_label:
12880 return "DW_TAG_label";
12881 case DW_TAG_lexical_block:
12882 return "DW_TAG_lexical_block";
12883 case DW_TAG_member:
12884 return "DW_TAG_member";
12885 case DW_TAG_pointer_type:
12886 return "DW_TAG_pointer_type";
12887 case DW_TAG_reference_type:
12888 return "DW_TAG_reference_type";
12889 case DW_TAG_compile_unit:
12890 return "DW_TAG_compile_unit";
12891 case DW_TAG_string_type:
12892 return "DW_TAG_string_type";
12893 case DW_TAG_structure_type:
12894 return "DW_TAG_structure_type";
12895 case DW_TAG_subroutine_type:
12896 return "DW_TAG_subroutine_type";
12897 case DW_TAG_typedef:
12898 return "DW_TAG_typedef";
12899 case DW_TAG_union_type:
12900 return "DW_TAG_union_type";
12901 case DW_TAG_unspecified_parameters:
12902 return "DW_TAG_unspecified_parameters";
12903 case DW_TAG_variant:
12904 return "DW_TAG_variant";
12905 case DW_TAG_common_block:
12906 return "DW_TAG_common_block";
12907 case DW_TAG_common_inclusion:
12908 return "DW_TAG_common_inclusion";
12909 case DW_TAG_inheritance:
12910 return "DW_TAG_inheritance";
12911 case DW_TAG_inlined_subroutine:
12912 return "DW_TAG_inlined_subroutine";
12913 case DW_TAG_module:
12914 return "DW_TAG_module";
12915 case DW_TAG_ptr_to_member_type:
12916 return "DW_TAG_ptr_to_member_type";
12917 case DW_TAG_set_type:
12918 return "DW_TAG_set_type";
12919 case DW_TAG_subrange_type:
12920 return "DW_TAG_subrange_type";
12921 case DW_TAG_with_stmt:
12922 return "DW_TAG_with_stmt";
12923 case DW_TAG_access_declaration:
12924 return "DW_TAG_access_declaration";
12925 case DW_TAG_base_type:
12926 return "DW_TAG_base_type";
12927 case DW_TAG_catch_block:
12928 return "DW_TAG_catch_block";
12929 case DW_TAG_const_type:
12930 return "DW_TAG_const_type";
12931 case DW_TAG_constant:
12932 return "DW_TAG_constant";
12933 case DW_TAG_enumerator:
12934 return "DW_TAG_enumerator";
12935 case DW_TAG_file_type:
12936 return "DW_TAG_file_type";
12937 case DW_TAG_friend:
12938 return "DW_TAG_friend";
12939 case DW_TAG_namelist:
12940 return "DW_TAG_namelist";
12941 case DW_TAG_namelist_item:
12942 return "DW_TAG_namelist_item";
12943 case DW_TAG_packed_type:
12944 return "DW_TAG_packed_type";
12945 case DW_TAG_subprogram:
12946 return "DW_TAG_subprogram";
12947 case DW_TAG_template_type_param:
12948 return "DW_TAG_template_type_param";
12949 case DW_TAG_template_value_param:
12950 return "DW_TAG_template_value_param";
12951 case DW_TAG_thrown_type:
12952 return "DW_TAG_thrown_type";
12953 case DW_TAG_try_block:
12954 return "DW_TAG_try_block";
12955 case DW_TAG_variant_part:
12956 return "DW_TAG_variant_part";
12957 case DW_TAG_variable:
12958 return "DW_TAG_variable";
12959 case DW_TAG_volatile_type:
12960 return "DW_TAG_volatile_type";
d9fa45fe
DC
12961 case DW_TAG_dwarf_procedure:
12962 return "DW_TAG_dwarf_procedure";
12963 case DW_TAG_restrict_type:
12964 return "DW_TAG_restrict_type";
12965 case DW_TAG_interface_type:
12966 return "DW_TAG_interface_type";
12967 case DW_TAG_namespace:
12968 return "DW_TAG_namespace";
12969 case DW_TAG_imported_module:
12970 return "DW_TAG_imported_module";
12971 case DW_TAG_unspecified_type:
12972 return "DW_TAG_unspecified_type";
12973 case DW_TAG_partial_unit:
12974 return "DW_TAG_partial_unit";
12975 case DW_TAG_imported_unit:
12976 return "DW_TAG_imported_unit";
b7619582
GF
12977 case DW_TAG_condition:
12978 return "DW_TAG_condition";
12979 case DW_TAG_shared_type:
12980 return "DW_TAG_shared_type";
348e048f
DE
12981 case DW_TAG_type_unit:
12982 return "DW_TAG_type_unit";
c906108c
SS
12983 case DW_TAG_MIPS_loop:
12984 return "DW_TAG_MIPS_loop";
b7619582
GF
12985 case DW_TAG_HP_array_descriptor:
12986 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12987 case DW_TAG_format_label:
12988 return "DW_TAG_format_label";
12989 case DW_TAG_function_template:
12990 return "DW_TAG_function_template";
12991 case DW_TAG_class_template:
12992 return "DW_TAG_class_template";
b7619582
GF
12993 case DW_TAG_GNU_BINCL:
12994 return "DW_TAG_GNU_BINCL";
12995 case DW_TAG_GNU_EINCL:
12996 return "DW_TAG_GNU_EINCL";
12997 case DW_TAG_upc_shared_type:
12998 return "DW_TAG_upc_shared_type";
12999 case DW_TAG_upc_strict_type:
13000 return "DW_TAG_upc_strict_type";
13001 case DW_TAG_upc_relaxed_type:
13002 return "DW_TAG_upc_relaxed_type";
13003 case DW_TAG_PGI_kanji_type:
13004 return "DW_TAG_PGI_kanji_type";
13005 case DW_TAG_PGI_interface_block:
13006 return "DW_TAG_PGI_interface_block";
96408a79
SA
13007 case DW_TAG_GNU_call_site:
13008 return "DW_TAG_GNU_call_site";
c906108c
SS
13009 default:
13010 return "DW_TAG_<unknown>";
13011 }
13012}
13013
13014/* Convert a DWARF attribute code into its string name. */
13015
13016static char *
aa1ee363 13017dwarf_attr_name (unsigned attr)
c906108c
SS
13018{
13019 switch (attr)
13020 {
13021 case DW_AT_sibling:
13022 return "DW_AT_sibling";
13023 case DW_AT_location:
13024 return "DW_AT_location";
13025 case DW_AT_name:
13026 return "DW_AT_name";
13027 case DW_AT_ordering:
13028 return "DW_AT_ordering";
13029 case DW_AT_subscr_data:
13030 return "DW_AT_subscr_data";
13031 case DW_AT_byte_size:
13032 return "DW_AT_byte_size";
13033 case DW_AT_bit_offset:
13034 return "DW_AT_bit_offset";
13035 case DW_AT_bit_size:
13036 return "DW_AT_bit_size";
13037 case DW_AT_element_list:
13038 return "DW_AT_element_list";
13039 case DW_AT_stmt_list:
13040 return "DW_AT_stmt_list";
13041 case DW_AT_low_pc:
13042 return "DW_AT_low_pc";
13043 case DW_AT_high_pc:
13044 return "DW_AT_high_pc";
13045 case DW_AT_language:
13046 return "DW_AT_language";
13047 case DW_AT_member:
13048 return "DW_AT_member";
13049 case DW_AT_discr:
13050 return "DW_AT_discr";
13051 case DW_AT_discr_value:
13052 return "DW_AT_discr_value";
13053 case DW_AT_visibility:
13054 return "DW_AT_visibility";
13055 case DW_AT_import:
13056 return "DW_AT_import";
13057 case DW_AT_string_length:
13058 return "DW_AT_string_length";
13059 case DW_AT_common_reference:
13060 return "DW_AT_common_reference";
13061 case DW_AT_comp_dir:
13062 return "DW_AT_comp_dir";
13063 case DW_AT_const_value:
13064 return "DW_AT_const_value";
13065 case DW_AT_containing_type:
13066 return "DW_AT_containing_type";
13067 case DW_AT_default_value:
13068 return "DW_AT_default_value";
13069 case DW_AT_inline:
13070 return "DW_AT_inline";
13071 case DW_AT_is_optional:
13072 return "DW_AT_is_optional";
13073 case DW_AT_lower_bound:
13074 return "DW_AT_lower_bound";
13075 case DW_AT_producer:
13076 return "DW_AT_producer";
13077 case DW_AT_prototyped:
13078 return "DW_AT_prototyped";
13079 case DW_AT_return_addr:
13080 return "DW_AT_return_addr";
13081 case DW_AT_start_scope:
13082 return "DW_AT_start_scope";
09fa0d7c
JK
13083 case DW_AT_bit_stride:
13084 return "DW_AT_bit_stride";
c906108c
SS
13085 case DW_AT_upper_bound:
13086 return "DW_AT_upper_bound";
13087 case DW_AT_abstract_origin:
13088 return "DW_AT_abstract_origin";
13089 case DW_AT_accessibility:
13090 return "DW_AT_accessibility";
13091 case DW_AT_address_class:
13092 return "DW_AT_address_class";
13093 case DW_AT_artificial:
13094 return "DW_AT_artificial";
13095 case DW_AT_base_types:
13096 return "DW_AT_base_types";
13097 case DW_AT_calling_convention:
13098 return "DW_AT_calling_convention";
13099 case DW_AT_count:
13100 return "DW_AT_count";
13101 case DW_AT_data_member_location:
13102 return "DW_AT_data_member_location";
13103 case DW_AT_decl_column:
13104 return "DW_AT_decl_column";
13105 case DW_AT_decl_file:
13106 return "DW_AT_decl_file";
13107 case DW_AT_decl_line:
13108 return "DW_AT_decl_line";
13109 case DW_AT_declaration:
13110 return "DW_AT_declaration";
13111 case DW_AT_discr_list:
13112 return "DW_AT_discr_list";
13113 case DW_AT_encoding:
13114 return "DW_AT_encoding";
13115 case DW_AT_external:
13116 return "DW_AT_external";
13117 case DW_AT_frame_base:
13118 return "DW_AT_frame_base";
13119 case DW_AT_friend:
13120 return "DW_AT_friend";
13121 case DW_AT_identifier_case:
13122 return "DW_AT_identifier_case";
13123 case DW_AT_macro_info:
13124 return "DW_AT_macro_info";
13125 case DW_AT_namelist_items:
13126 return "DW_AT_namelist_items";
13127 case DW_AT_priority:
13128 return "DW_AT_priority";
13129 case DW_AT_segment:
13130 return "DW_AT_segment";
13131 case DW_AT_specification:
13132 return "DW_AT_specification";
13133 case DW_AT_static_link:
13134 return "DW_AT_static_link";
13135 case DW_AT_type:
13136 return "DW_AT_type";
13137 case DW_AT_use_location:
13138 return "DW_AT_use_location";
13139 case DW_AT_variable_parameter:
13140 return "DW_AT_variable_parameter";
13141 case DW_AT_virtuality:
13142 return "DW_AT_virtuality";
13143 case DW_AT_vtable_elem_location:
13144 return "DW_AT_vtable_elem_location";
b7619582 13145 /* DWARF 3 values. */
d9fa45fe
DC
13146 case DW_AT_allocated:
13147 return "DW_AT_allocated";
13148 case DW_AT_associated:
13149 return "DW_AT_associated";
13150 case DW_AT_data_location:
13151 return "DW_AT_data_location";
09fa0d7c
JK
13152 case DW_AT_byte_stride:
13153 return "DW_AT_byte_stride";
d9fa45fe
DC
13154 case DW_AT_entry_pc:
13155 return "DW_AT_entry_pc";
13156 case DW_AT_use_UTF8:
13157 return "DW_AT_use_UTF8";
13158 case DW_AT_extension:
13159 return "DW_AT_extension";
13160 case DW_AT_ranges:
13161 return "DW_AT_ranges";
13162 case DW_AT_trampoline:
13163 return "DW_AT_trampoline";
13164 case DW_AT_call_column:
13165 return "DW_AT_call_column";
13166 case DW_AT_call_file:
13167 return "DW_AT_call_file";
13168 case DW_AT_call_line:
13169 return "DW_AT_call_line";
b7619582
GF
13170 case DW_AT_description:
13171 return "DW_AT_description";
13172 case DW_AT_binary_scale:
13173 return "DW_AT_binary_scale";
13174 case DW_AT_decimal_scale:
13175 return "DW_AT_decimal_scale";
13176 case DW_AT_small:
13177 return "DW_AT_small";
13178 case DW_AT_decimal_sign:
13179 return "DW_AT_decimal_sign";
13180 case DW_AT_digit_count:
13181 return "DW_AT_digit_count";
13182 case DW_AT_picture_string:
13183 return "DW_AT_picture_string";
13184 case DW_AT_mutable:
13185 return "DW_AT_mutable";
13186 case DW_AT_threads_scaled:
13187 return "DW_AT_threads_scaled";
13188 case DW_AT_explicit:
13189 return "DW_AT_explicit";
13190 case DW_AT_object_pointer:
13191 return "DW_AT_object_pointer";
13192 case DW_AT_endianity:
13193 return "DW_AT_endianity";
13194 case DW_AT_elemental:
13195 return "DW_AT_elemental";
13196 case DW_AT_pure:
13197 return "DW_AT_pure";
13198 case DW_AT_recursive:
13199 return "DW_AT_recursive";
348e048f
DE
13200 /* DWARF 4 values. */
13201 case DW_AT_signature:
13202 return "DW_AT_signature";
31ef98ae
TT
13203 case DW_AT_linkage_name:
13204 return "DW_AT_linkage_name";
b7619582 13205 /* SGI/MIPS extensions. */
c764a876 13206#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
13207 case DW_AT_MIPS_fde:
13208 return "DW_AT_MIPS_fde";
c764a876 13209#endif
c906108c
SS
13210 case DW_AT_MIPS_loop_begin:
13211 return "DW_AT_MIPS_loop_begin";
13212 case DW_AT_MIPS_tail_loop_begin:
13213 return "DW_AT_MIPS_tail_loop_begin";
13214 case DW_AT_MIPS_epilog_begin:
13215 return "DW_AT_MIPS_epilog_begin";
13216 case DW_AT_MIPS_loop_unroll_factor:
13217 return "DW_AT_MIPS_loop_unroll_factor";
13218 case DW_AT_MIPS_software_pipeline_depth:
13219 return "DW_AT_MIPS_software_pipeline_depth";
13220 case DW_AT_MIPS_linkage_name:
13221 return "DW_AT_MIPS_linkage_name";
b7619582
GF
13222 case DW_AT_MIPS_stride:
13223 return "DW_AT_MIPS_stride";
13224 case DW_AT_MIPS_abstract_name:
13225 return "DW_AT_MIPS_abstract_name";
13226 case DW_AT_MIPS_clone_origin:
13227 return "DW_AT_MIPS_clone_origin";
13228 case DW_AT_MIPS_has_inlines:
13229 return "DW_AT_MIPS_has_inlines";
b7619582 13230 /* HP extensions. */
c764a876 13231#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
13232 case DW_AT_HP_block_index:
13233 return "DW_AT_HP_block_index";
c764a876 13234#endif
b7619582
GF
13235 case DW_AT_HP_unmodifiable:
13236 return "DW_AT_HP_unmodifiable";
13237 case DW_AT_HP_actuals_stmt_list:
13238 return "DW_AT_HP_actuals_stmt_list";
13239 case DW_AT_HP_proc_per_section:
13240 return "DW_AT_HP_proc_per_section";
13241 case DW_AT_HP_raw_data_ptr:
13242 return "DW_AT_HP_raw_data_ptr";
13243 case DW_AT_HP_pass_by_reference:
13244 return "DW_AT_HP_pass_by_reference";
13245 case DW_AT_HP_opt_level:
13246 return "DW_AT_HP_opt_level";
13247 case DW_AT_HP_prof_version_id:
13248 return "DW_AT_HP_prof_version_id";
13249 case DW_AT_HP_opt_flags:
13250 return "DW_AT_HP_opt_flags";
13251 case DW_AT_HP_cold_region_low_pc:
13252 return "DW_AT_HP_cold_region_low_pc";
13253 case DW_AT_HP_cold_region_high_pc:
13254 return "DW_AT_HP_cold_region_high_pc";
13255 case DW_AT_HP_all_variables_modifiable:
13256 return "DW_AT_HP_all_variables_modifiable";
13257 case DW_AT_HP_linkage_name:
13258 return "DW_AT_HP_linkage_name";
13259 case DW_AT_HP_prof_flags:
13260 return "DW_AT_HP_prof_flags";
13261 /* GNU extensions. */
c906108c
SS
13262 case DW_AT_sf_names:
13263 return "DW_AT_sf_names";
13264 case DW_AT_src_info:
13265 return "DW_AT_src_info";
13266 case DW_AT_mac_info:
13267 return "DW_AT_mac_info";
13268 case DW_AT_src_coords:
13269 return "DW_AT_src_coords";
13270 case DW_AT_body_begin:
13271 return "DW_AT_body_begin";
13272 case DW_AT_body_end:
13273 return "DW_AT_body_end";
f5f8a009
EZ
13274 case DW_AT_GNU_vector:
13275 return "DW_AT_GNU_vector";
2de00c64
DE
13276 case DW_AT_GNU_odr_signature:
13277 return "DW_AT_GNU_odr_signature";
b7619582
GF
13278 /* VMS extensions. */
13279 case DW_AT_VMS_rtnbeg_pd_address:
13280 return "DW_AT_VMS_rtnbeg_pd_address";
13281 /* UPC extension. */
13282 case DW_AT_upc_threads_scaled:
13283 return "DW_AT_upc_threads_scaled";
13284 /* PGI (STMicroelectronics) extensions. */
13285 case DW_AT_PGI_lbase:
13286 return "DW_AT_PGI_lbase";
13287 case DW_AT_PGI_soffset:
13288 return "DW_AT_PGI_soffset";
13289 case DW_AT_PGI_lstride:
13290 return "DW_AT_PGI_lstride";
c906108c
SS
13291 default:
13292 return "DW_AT_<unknown>";
13293 }
13294}
13295
13296/* Convert a DWARF value form code into its string name. */
13297
13298static char *
aa1ee363 13299dwarf_form_name (unsigned form)
c906108c
SS
13300{
13301 switch (form)
13302 {
13303 case DW_FORM_addr:
13304 return "DW_FORM_addr";
13305 case DW_FORM_block2:
13306 return "DW_FORM_block2";
13307 case DW_FORM_block4:
13308 return "DW_FORM_block4";
13309 case DW_FORM_data2:
13310 return "DW_FORM_data2";
13311 case DW_FORM_data4:
13312 return "DW_FORM_data4";
13313 case DW_FORM_data8:
13314 return "DW_FORM_data8";
13315 case DW_FORM_string:
13316 return "DW_FORM_string";
13317 case DW_FORM_block:
13318 return "DW_FORM_block";
13319 case DW_FORM_block1:
13320 return "DW_FORM_block1";
13321 case DW_FORM_data1:
13322 return "DW_FORM_data1";
13323 case DW_FORM_flag:
13324 return "DW_FORM_flag";
13325 case DW_FORM_sdata:
13326 return "DW_FORM_sdata";
13327 case DW_FORM_strp:
13328 return "DW_FORM_strp";
13329 case DW_FORM_udata:
13330 return "DW_FORM_udata";
13331 case DW_FORM_ref_addr:
13332 return "DW_FORM_ref_addr";
13333 case DW_FORM_ref1:
13334 return "DW_FORM_ref1";
13335 case DW_FORM_ref2:
13336 return "DW_FORM_ref2";
13337 case DW_FORM_ref4:
13338 return "DW_FORM_ref4";
13339 case DW_FORM_ref8:
13340 return "DW_FORM_ref8";
13341 case DW_FORM_ref_udata:
13342 return "DW_FORM_ref_udata";
13343 case DW_FORM_indirect:
13344 return "DW_FORM_indirect";
348e048f
DE
13345 case DW_FORM_sec_offset:
13346 return "DW_FORM_sec_offset";
13347 case DW_FORM_exprloc:
13348 return "DW_FORM_exprloc";
13349 case DW_FORM_flag_present:
13350 return "DW_FORM_flag_present";
55f1336d
TT
13351 case DW_FORM_ref_sig8:
13352 return "DW_FORM_ref_sig8";
c906108c
SS
13353 default:
13354 return "DW_FORM_<unknown>";
13355 }
13356}
13357
13358/* Convert a DWARF stack opcode into its string name. */
13359
9eae7c52 13360const char *
b1bfef65 13361dwarf_stack_op_name (unsigned op)
c906108c
SS
13362{
13363 switch (op)
13364 {
13365 case DW_OP_addr:
13366 return "DW_OP_addr";
13367 case DW_OP_deref:
13368 return "DW_OP_deref";
13369 case DW_OP_const1u:
13370 return "DW_OP_const1u";
13371 case DW_OP_const1s:
13372 return "DW_OP_const1s";
13373 case DW_OP_const2u:
13374 return "DW_OP_const2u";
13375 case DW_OP_const2s:
13376 return "DW_OP_const2s";
13377 case DW_OP_const4u:
13378 return "DW_OP_const4u";
13379 case DW_OP_const4s:
13380 return "DW_OP_const4s";
13381 case DW_OP_const8u:
13382 return "DW_OP_const8u";
13383 case DW_OP_const8s:
13384 return "DW_OP_const8s";
13385 case DW_OP_constu:
13386 return "DW_OP_constu";
13387 case DW_OP_consts:
13388 return "DW_OP_consts";
13389 case DW_OP_dup:
13390 return "DW_OP_dup";
13391 case DW_OP_drop:
13392 return "DW_OP_drop";
13393 case DW_OP_over:
13394 return "DW_OP_over";
13395 case DW_OP_pick:
13396 return "DW_OP_pick";
13397 case DW_OP_swap:
13398 return "DW_OP_swap";
13399 case DW_OP_rot:
13400 return "DW_OP_rot";
13401 case DW_OP_xderef:
13402 return "DW_OP_xderef";
13403 case DW_OP_abs:
13404 return "DW_OP_abs";
13405 case DW_OP_and:
13406 return "DW_OP_and";
13407 case DW_OP_div:
13408 return "DW_OP_div";
13409 case DW_OP_minus:
13410 return "DW_OP_minus";
13411 case DW_OP_mod:
13412 return "DW_OP_mod";
13413 case DW_OP_mul:
13414 return "DW_OP_mul";
13415 case DW_OP_neg:
13416 return "DW_OP_neg";
13417 case DW_OP_not:
13418 return "DW_OP_not";
13419 case DW_OP_or:
13420 return "DW_OP_or";
13421 case DW_OP_plus:
13422 return "DW_OP_plus";
13423 case DW_OP_plus_uconst:
13424 return "DW_OP_plus_uconst";
13425 case DW_OP_shl:
13426 return "DW_OP_shl";
13427 case DW_OP_shr:
13428 return "DW_OP_shr";
13429 case DW_OP_shra:
13430 return "DW_OP_shra";
13431 case DW_OP_xor:
13432 return "DW_OP_xor";
13433 case DW_OP_bra:
13434 return "DW_OP_bra";
13435 case DW_OP_eq:
13436 return "DW_OP_eq";
13437 case DW_OP_ge:
13438 return "DW_OP_ge";
13439 case DW_OP_gt:
13440 return "DW_OP_gt";
13441 case DW_OP_le:
13442 return "DW_OP_le";
13443 case DW_OP_lt:
13444 return "DW_OP_lt";
13445 case DW_OP_ne:
13446 return "DW_OP_ne";
13447 case DW_OP_skip:
13448 return "DW_OP_skip";
13449 case DW_OP_lit0:
13450 return "DW_OP_lit0";
13451 case DW_OP_lit1:
13452 return "DW_OP_lit1";
13453 case DW_OP_lit2:
13454 return "DW_OP_lit2";
13455 case DW_OP_lit3:
13456 return "DW_OP_lit3";
13457 case DW_OP_lit4:
13458 return "DW_OP_lit4";
13459 case DW_OP_lit5:
13460 return "DW_OP_lit5";
13461 case DW_OP_lit6:
13462 return "DW_OP_lit6";
13463 case DW_OP_lit7:
13464 return "DW_OP_lit7";
13465 case DW_OP_lit8:
13466 return "DW_OP_lit8";
13467 case DW_OP_lit9:
13468 return "DW_OP_lit9";
13469 case DW_OP_lit10:
13470 return "DW_OP_lit10";
13471 case DW_OP_lit11:
13472 return "DW_OP_lit11";
13473 case DW_OP_lit12:
13474 return "DW_OP_lit12";
13475 case DW_OP_lit13:
13476 return "DW_OP_lit13";
13477 case DW_OP_lit14:
13478 return "DW_OP_lit14";
13479 case DW_OP_lit15:
13480 return "DW_OP_lit15";
13481 case DW_OP_lit16:
13482 return "DW_OP_lit16";
13483 case DW_OP_lit17:
13484 return "DW_OP_lit17";
13485 case DW_OP_lit18:
13486 return "DW_OP_lit18";
13487 case DW_OP_lit19:
13488 return "DW_OP_lit19";
13489 case DW_OP_lit20:
13490 return "DW_OP_lit20";
13491 case DW_OP_lit21:
13492 return "DW_OP_lit21";
13493 case DW_OP_lit22:
13494 return "DW_OP_lit22";
13495 case DW_OP_lit23:
13496 return "DW_OP_lit23";
13497 case DW_OP_lit24:
13498 return "DW_OP_lit24";
13499 case DW_OP_lit25:
13500 return "DW_OP_lit25";
13501 case DW_OP_lit26:
13502 return "DW_OP_lit26";
13503 case DW_OP_lit27:
13504 return "DW_OP_lit27";
13505 case DW_OP_lit28:
13506 return "DW_OP_lit28";
13507 case DW_OP_lit29:
13508 return "DW_OP_lit29";
13509 case DW_OP_lit30:
13510 return "DW_OP_lit30";
13511 case DW_OP_lit31:
13512 return "DW_OP_lit31";
13513 case DW_OP_reg0:
13514 return "DW_OP_reg0";
13515 case DW_OP_reg1:
13516 return "DW_OP_reg1";
13517 case DW_OP_reg2:
13518 return "DW_OP_reg2";
13519 case DW_OP_reg3:
13520 return "DW_OP_reg3";
13521 case DW_OP_reg4:
13522 return "DW_OP_reg4";
13523 case DW_OP_reg5:
13524 return "DW_OP_reg5";
13525 case DW_OP_reg6:
13526 return "DW_OP_reg6";
13527 case DW_OP_reg7:
13528 return "DW_OP_reg7";
13529 case DW_OP_reg8:
13530 return "DW_OP_reg8";
13531 case DW_OP_reg9:
13532 return "DW_OP_reg9";
13533 case DW_OP_reg10:
13534 return "DW_OP_reg10";
13535 case DW_OP_reg11:
13536 return "DW_OP_reg11";
13537 case DW_OP_reg12:
13538 return "DW_OP_reg12";
13539 case DW_OP_reg13:
13540 return "DW_OP_reg13";
13541 case DW_OP_reg14:
13542 return "DW_OP_reg14";
13543 case DW_OP_reg15:
13544 return "DW_OP_reg15";
13545 case DW_OP_reg16:
13546 return "DW_OP_reg16";
13547 case DW_OP_reg17:
13548 return "DW_OP_reg17";
13549 case DW_OP_reg18:
13550 return "DW_OP_reg18";
13551 case DW_OP_reg19:
13552 return "DW_OP_reg19";
13553 case DW_OP_reg20:
13554 return "DW_OP_reg20";
13555 case DW_OP_reg21:
13556 return "DW_OP_reg21";
13557 case DW_OP_reg22:
13558 return "DW_OP_reg22";
13559 case DW_OP_reg23:
13560 return "DW_OP_reg23";
13561 case DW_OP_reg24:
13562 return "DW_OP_reg24";
13563 case DW_OP_reg25:
13564 return "DW_OP_reg25";
13565 case DW_OP_reg26:
13566 return "DW_OP_reg26";
13567 case DW_OP_reg27:
13568 return "DW_OP_reg27";
13569 case DW_OP_reg28:
13570 return "DW_OP_reg28";
13571 case DW_OP_reg29:
13572 return "DW_OP_reg29";
13573 case DW_OP_reg30:
13574 return "DW_OP_reg30";
13575 case DW_OP_reg31:
13576 return "DW_OP_reg31";
13577 case DW_OP_breg0:
13578 return "DW_OP_breg0";
13579 case DW_OP_breg1:
13580 return "DW_OP_breg1";
13581 case DW_OP_breg2:
13582 return "DW_OP_breg2";
13583 case DW_OP_breg3:
13584 return "DW_OP_breg3";
13585 case DW_OP_breg4:
13586 return "DW_OP_breg4";
13587 case DW_OP_breg5:
13588 return "DW_OP_breg5";
13589 case DW_OP_breg6:
13590 return "DW_OP_breg6";
13591 case DW_OP_breg7:
13592 return "DW_OP_breg7";
13593 case DW_OP_breg8:
13594 return "DW_OP_breg8";
13595 case DW_OP_breg9:
13596 return "DW_OP_breg9";
13597 case DW_OP_breg10:
13598 return "DW_OP_breg10";
13599 case DW_OP_breg11:
13600 return "DW_OP_breg11";
13601 case DW_OP_breg12:
13602 return "DW_OP_breg12";
13603 case DW_OP_breg13:
13604 return "DW_OP_breg13";
13605 case DW_OP_breg14:
13606 return "DW_OP_breg14";
13607 case DW_OP_breg15:
13608 return "DW_OP_breg15";
13609 case DW_OP_breg16:
13610 return "DW_OP_breg16";
13611 case DW_OP_breg17:
13612 return "DW_OP_breg17";
13613 case DW_OP_breg18:
13614 return "DW_OP_breg18";
13615 case DW_OP_breg19:
13616 return "DW_OP_breg19";
13617 case DW_OP_breg20:
13618 return "DW_OP_breg20";
13619 case DW_OP_breg21:
13620 return "DW_OP_breg21";
13621 case DW_OP_breg22:
13622 return "DW_OP_breg22";
13623 case DW_OP_breg23:
13624 return "DW_OP_breg23";
13625 case DW_OP_breg24:
13626 return "DW_OP_breg24";
13627 case DW_OP_breg25:
13628 return "DW_OP_breg25";
13629 case DW_OP_breg26:
13630 return "DW_OP_breg26";
13631 case DW_OP_breg27:
13632 return "DW_OP_breg27";
13633 case DW_OP_breg28:
13634 return "DW_OP_breg28";
13635 case DW_OP_breg29:
13636 return "DW_OP_breg29";
13637 case DW_OP_breg30:
13638 return "DW_OP_breg30";
13639 case DW_OP_breg31:
13640 return "DW_OP_breg31";
13641 case DW_OP_regx:
13642 return "DW_OP_regx";
13643 case DW_OP_fbreg:
13644 return "DW_OP_fbreg";
13645 case DW_OP_bregx:
13646 return "DW_OP_bregx";
13647 case DW_OP_piece:
13648 return "DW_OP_piece";
13649 case DW_OP_deref_size:
13650 return "DW_OP_deref_size";
13651 case DW_OP_xderef_size:
13652 return "DW_OP_xderef_size";
13653 case DW_OP_nop:
13654 return "DW_OP_nop";
b7619582 13655 /* DWARF 3 extensions. */
ed348acc
EZ
13656 case DW_OP_push_object_address:
13657 return "DW_OP_push_object_address";
13658 case DW_OP_call2:
13659 return "DW_OP_call2";
13660 case DW_OP_call4:
13661 return "DW_OP_call4";
13662 case DW_OP_call_ref:
13663 return "DW_OP_call_ref";
b7619582
GF
13664 case DW_OP_form_tls_address:
13665 return "DW_OP_form_tls_address";
13666 case DW_OP_call_frame_cfa:
13667 return "DW_OP_call_frame_cfa";
13668 case DW_OP_bit_piece:
13669 return "DW_OP_bit_piece";
9eae7c52
TT
13670 /* DWARF 4 extensions. */
13671 case DW_OP_implicit_value:
13672 return "DW_OP_implicit_value";
13673 case DW_OP_stack_value:
13674 return "DW_OP_stack_value";
13675 /* GNU extensions. */
ed348acc
EZ
13676 case DW_OP_GNU_push_tls_address:
13677 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13678 case DW_OP_GNU_uninit:
13679 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13680 case DW_OP_GNU_implicit_pointer:
13681 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13682 case DW_OP_GNU_entry_value:
13683 return "DW_OP_GNU_entry_value";
13684 case DW_OP_GNU_const_type:
13685 return "DW_OP_GNU_const_type";
13686 case DW_OP_GNU_regval_type:
13687 return "DW_OP_GNU_regval_type";
13688 case DW_OP_GNU_deref_type:
13689 return "DW_OP_GNU_deref_type";
13690 case DW_OP_GNU_convert:
13691 return "DW_OP_GNU_convert";
13692 case DW_OP_GNU_reinterpret:
13693 return "DW_OP_GNU_reinterpret";
c906108c 13694 default:
b1bfef65 13695 return NULL;
c906108c
SS
13696 }
13697}
13698
13699static char *
fba45db2 13700dwarf_bool_name (unsigned mybool)
c906108c
SS
13701{
13702 if (mybool)
13703 return "TRUE";
13704 else
13705 return "FALSE";
13706}
13707
13708/* Convert a DWARF type code into its string name. */
13709
13710static char *
aa1ee363 13711dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13712{
13713 switch (enc)
13714 {
b7619582
GF
13715 case DW_ATE_void:
13716 return "DW_ATE_void";
c906108c
SS
13717 case DW_ATE_address:
13718 return "DW_ATE_address";
13719 case DW_ATE_boolean:
13720 return "DW_ATE_boolean";
13721 case DW_ATE_complex_float:
13722 return "DW_ATE_complex_float";
13723 case DW_ATE_float:
13724 return "DW_ATE_float";
13725 case DW_ATE_signed:
13726 return "DW_ATE_signed";
13727 case DW_ATE_signed_char:
13728 return "DW_ATE_signed_char";
13729 case DW_ATE_unsigned:
13730 return "DW_ATE_unsigned";
13731 case DW_ATE_unsigned_char:
13732 return "DW_ATE_unsigned_char";
b7619582 13733 /* DWARF 3. */
d9fa45fe
DC
13734 case DW_ATE_imaginary_float:
13735 return "DW_ATE_imaginary_float";
b7619582
GF
13736 case DW_ATE_packed_decimal:
13737 return "DW_ATE_packed_decimal";
13738 case DW_ATE_numeric_string:
13739 return "DW_ATE_numeric_string";
13740 case DW_ATE_edited:
13741 return "DW_ATE_edited";
13742 case DW_ATE_signed_fixed:
13743 return "DW_ATE_signed_fixed";
13744 case DW_ATE_unsigned_fixed:
13745 return "DW_ATE_unsigned_fixed";
13746 case DW_ATE_decimal_float:
13747 return "DW_ATE_decimal_float";
75079b2b
TT
13748 /* DWARF 4. */
13749 case DW_ATE_UTF:
13750 return "DW_ATE_UTF";
b7619582
GF
13751 /* HP extensions. */
13752 case DW_ATE_HP_float80:
13753 return "DW_ATE_HP_float80";
13754 case DW_ATE_HP_complex_float80:
13755 return "DW_ATE_HP_complex_float80";
13756 case DW_ATE_HP_float128:
13757 return "DW_ATE_HP_float128";
13758 case DW_ATE_HP_complex_float128:
13759 return "DW_ATE_HP_complex_float128";
13760 case DW_ATE_HP_floathpintel:
13761 return "DW_ATE_HP_floathpintel";
13762 case DW_ATE_HP_imaginary_float80:
13763 return "DW_ATE_HP_imaginary_float80";
13764 case DW_ATE_HP_imaginary_float128:
13765 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13766 default:
13767 return "DW_ATE_<unknown>";
13768 }
13769}
13770
0963b4bd 13771/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13772
13773#if 0
13774static char *
aa1ee363 13775dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13776{
13777 switch (cfi_opc)
13778 {
13779 case DW_CFA_advance_loc:
13780 return "DW_CFA_advance_loc";
13781 case DW_CFA_offset:
13782 return "DW_CFA_offset";
13783 case DW_CFA_restore:
13784 return "DW_CFA_restore";
13785 case DW_CFA_nop:
13786 return "DW_CFA_nop";
13787 case DW_CFA_set_loc:
13788 return "DW_CFA_set_loc";
13789 case DW_CFA_advance_loc1:
13790 return "DW_CFA_advance_loc1";
13791 case DW_CFA_advance_loc2:
13792 return "DW_CFA_advance_loc2";
13793 case DW_CFA_advance_loc4:
13794 return "DW_CFA_advance_loc4";
13795 case DW_CFA_offset_extended:
13796 return "DW_CFA_offset_extended";
13797 case DW_CFA_restore_extended:
13798 return "DW_CFA_restore_extended";
13799 case DW_CFA_undefined:
13800 return "DW_CFA_undefined";
13801 case DW_CFA_same_value:
13802 return "DW_CFA_same_value";
13803 case DW_CFA_register:
13804 return "DW_CFA_register";
13805 case DW_CFA_remember_state:
13806 return "DW_CFA_remember_state";
13807 case DW_CFA_restore_state:
13808 return "DW_CFA_restore_state";
13809 case DW_CFA_def_cfa:
13810 return "DW_CFA_def_cfa";
13811 case DW_CFA_def_cfa_register:
13812 return "DW_CFA_def_cfa_register";
13813 case DW_CFA_def_cfa_offset:
13814 return "DW_CFA_def_cfa_offset";
b7619582 13815 /* DWARF 3. */
985cb1a3
JM
13816 case DW_CFA_def_cfa_expression:
13817 return "DW_CFA_def_cfa_expression";
13818 case DW_CFA_expression:
13819 return "DW_CFA_expression";
13820 case DW_CFA_offset_extended_sf:
13821 return "DW_CFA_offset_extended_sf";
13822 case DW_CFA_def_cfa_sf:
13823 return "DW_CFA_def_cfa_sf";
13824 case DW_CFA_def_cfa_offset_sf:
13825 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13826 case DW_CFA_val_offset:
13827 return "DW_CFA_val_offset";
13828 case DW_CFA_val_offset_sf:
13829 return "DW_CFA_val_offset_sf";
13830 case DW_CFA_val_expression:
13831 return "DW_CFA_val_expression";
13832 /* SGI/MIPS specific. */
c906108c
SS
13833 case DW_CFA_MIPS_advance_loc8:
13834 return "DW_CFA_MIPS_advance_loc8";
b7619582 13835 /* GNU extensions. */
985cb1a3
JM
13836 case DW_CFA_GNU_window_save:
13837 return "DW_CFA_GNU_window_save";
13838 case DW_CFA_GNU_args_size:
13839 return "DW_CFA_GNU_args_size";
13840 case DW_CFA_GNU_negative_offset_extended:
13841 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13842 default:
13843 return "DW_CFA_<unknown>";
13844 }
13845}
13846#endif
13847
f9aca02d 13848static void
d97bc12b 13849dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13850{
13851 unsigned int i;
13852
d97bc12b
DE
13853 print_spaces (indent, f);
13854 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13855 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13856
13857 if (die->parent != NULL)
13858 {
13859 print_spaces (indent, f);
13860 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13861 die->parent->offset);
13862 }
13863
13864 print_spaces (indent, f);
13865 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13866 dwarf_bool_name (die->child != NULL));
c906108c 13867
d97bc12b
DE
13868 print_spaces (indent, f);
13869 fprintf_unfiltered (f, " attributes:\n");
13870
c906108c
SS
13871 for (i = 0; i < die->num_attrs; ++i)
13872 {
d97bc12b
DE
13873 print_spaces (indent, f);
13874 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13875 dwarf_attr_name (die->attrs[i].name),
13876 dwarf_form_name (die->attrs[i].form));
d97bc12b 13877
c906108c
SS
13878 switch (die->attrs[i].form)
13879 {
13880 case DW_FORM_ref_addr:
13881 case DW_FORM_addr:
d97bc12b 13882 fprintf_unfiltered (f, "address: ");
5af949e3 13883 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13884 break;
13885 case DW_FORM_block2:
13886 case DW_FORM_block4:
13887 case DW_FORM_block:
13888 case DW_FORM_block1:
3e43a32a
MS
13889 fprintf_unfiltered (f, "block: size %d",
13890 DW_BLOCK (&die->attrs[i])->size);
c906108c 13891 break;
2dc7f7b3
TT
13892 case DW_FORM_exprloc:
13893 fprintf_unfiltered (f, "expression: size %u",
13894 DW_BLOCK (&die->attrs[i])->size);
13895 break;
10b3939b
DJ
13896 case DW_FORM_ref1:
13897 case DW_FORM_ref2:
13898 case DW_FORM_ref4:
d97bc12b 13899 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13900 (long) (DW_ADDR (&die->attrs[i])));
13901 break;
c906108c
SS
13902 case DW_FORM_data1:
13903 case DW_FORM_data2:
13904 case DW_FORM_data4:
ce5d95e1 13905 case DW_FORM_data8:
c906108c
SS
13906 case DW_FORM_udata:
13907 case DW_FORM_sdata:
43bbcdc2
PH
13908 fprintf_unfiltered (f, "constant: %s",
13909 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13910 break;
2dc7f7b3
TT
13911 case DW_FORM_sec_offset:
13912 fprintf_unfiltered (f, "section offset: %s",
13913 pulongest (DW_UNSND (&die->attrs[i])));
13914 break;
55f1336d 13915 case DW_FORM_ref_sig8:
348e048f
DE
13916 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13917 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b3c8eb43 13918 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
348e048f
DE
13919 else
13920 fprintf_unfiltered (f, "signatured type, offset: unknown");
13921 break;
c906108c 13922 case DW_FORM_string:
4bdf3d34 13923 case DW_FORM_strp:
8285870a 13924 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13925 DW_STRING (&die->attrs[i])
8285870a
JK
13926 ? DW_STRING (&die->attrs[i]) : "",
13927 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13928 break;
13929 case DW_FORM_flag:
13930 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13931 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13932 else
d97bc12b 13933 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13934 break;
2dc7f7b3
TT
13935 case DW_FORM_flag_present:
13936 fprintf_unfiltered (f, "flag: TRUE");
13937 break;
a8329558 13938 case DW_FORM_indirect:
0963b4bd
MS
13939 /* The reader will have reduced the indirect form to
13940 the "base form" so this form should not occur. */
3e43a32a
MS
13941 fprintf_unfiltered (f,
13942 "unexpected attribute form: DW_FORM_indirect");
a8329558 13943 break;
c906108c 13944 default:
d97bc12b 13945 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13946 die->attrs[i].form);
d97bc12b 13947 break;
c906108c 13948 }
d97bc12b 13949 fprintf_unfiltered (f, "\n");
c906108c
SS
13950 }
13951}
13952
f9aca02d 13953static void
d97bc12b 13954dump_die_for_error (struct die_info *die)
c906108c 13955{
d97bc12b
DE
13956 dump_die_shallow (gdb_stderr, 0, die);
13957}
13958
13959static void
13960dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13961{
13962 int indent = level * 4;
13963
13964 gdb_assert (die != NULL);
13965
13966 if (level >= max_level)
13967 return;
13968
13969 dump_die_shallow (f, indent, die);
13970
13971 if (die->child != NULL)
c906108c 13972 {
d97bc12b
DE
13973 print_spaces (indent, f);
13974 fprintf_unfiltered (f, " Children:");
13975 if (level + 1 < max_level)
13976 {
13977 fprintf_unfiltered (f, "\n");
13978 dump_die_1 (f, level + 1, max_level, die->child);
13979 }
13980 else
13981 {
3e43a32a
MS
13982 fprintf_unfiltered (f,
13983 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13984 }
13985 }
13986
13987 if (die->sibling != NULL && level > 0)
13988 {
13989 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13990 }
13991}
13992
d97bc12b
DE
13993/* This is called from the pdie macro in gdbinit.in.
13994 It's not static so gcc will keep a copy callable from gdb. */
13995
13996void
13997dump_die (struct die_info *die, int max_level)
13998{
13999 dump_die_1 (gdb_stdlog, 0, max_level, die);
14000}
14001
f9aca02d 14002static void
51545339 14003store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14004{
51545339 14005 void **slot;
c906108c 14006
51545339
DJ
14007 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14008
14009 *slot = die;
c906108c
SS
14010}
14011
93311388
DE
14012static int
14013is_ref_attr (struct attribute *attr)
c906108c 14014{
c906108c
SS
14015 switch (attr->form)
14016 {
14017 case DW_FORM_ref_addr:
c906108c
SS
14018 case DW_FORM_ref1:
14019 case DW_FORM_ref2:
14020 case DW_FORM_ref4:
613e1657 14021 case DW_FORM_ref8:
c906108c 14022 case DW_FORM_ref_udata:
93311388 14023 return 1;
c906108c 14024 default:
93311388 14025 return 0;
c906108c 14026 }
93311388
DE
14027}
14028
14029static unsigned int
14030dwarf2_get_ref_die_offset (struct attribute *attr)
14031{
14032 if (is_ref_attr (attr))
14033 return DW_ADDR (attr);
14034
14035 complaint (&symfile_complaints,
14036 _("unsupported die ref attribute form: '%s'"),
14037 dwarf_form_name (attr->form));
14038 return 0;
c906108c
SS
14039}
14040
43bbcdc2
PH
14041/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14042 * the value held by the attribute is not constant. */
a02abb62 14043
43bbcdc2 14044static LONGEST
a02abb62
JB
14045dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14046{
14047 if (attr->form == DW_FORM_sdata)
14048 return DW_SND (attr);
14049 else if (attr->form == DW_FORM_udata
14050 || attr->form == DW_FORM_data1
14051 || attr->form == DW_FORM_data2
14052 || attr->form == DW_FORM_data4
14053 || attr->form == DW_FORM_data8)
14054 return DW_UNSND (attr);
14055 else
14056 {
3e43a32a
MS
14057 complaint (&symfile_complaints,
14058 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14059 dwarf_form_name (attr->form));
14060 return default_value;
14061 }
14062}
14063
03dd20cc 14064/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14065 unit and add it to our queue.
14066 The result is non-zero if PER_CU was queued, otherwise the result is zero
14067 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14068
348e048f 14069static int
03dd20cc
DJ
14070maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14071 struct dwarf2_per_cu_data *per_cu)
14072{
98bfdba5
PA
14073 /* We may arrive here during partial symbol reading, if we need full
14074 DIEs to process an unusual case (e.g. template arguments). Do
14075 not queue PER_CU, just tell our caller to load its DIEs. */
14076 if (dwarf2_per_objfile->reading_partial_symbols)
14077 {
14078 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14079 return 1;
14080 return 0;
14081 }
14082
03dd20cc
DJ
14083 /* Mark the dependence relation so that we don't flush PER_CU
14084 too early. */
14085 dwarf2_add_dependence (this_cu, per_cu);
14086
14087 /* If it's already on the queue, we have nothing to do. */
14088 if (per_cu->queued)
348e048f 14089 return 0;
03dd20cc
DJ
14090
14091 /* If the compilation unit is already loaded, just mark it as
14092 used. */
14093 if (per_cu->cu != NULL)
14094 {
14095 per_cu->cu->last_used = 0;
348e048f 14096 return 0;
03dd20cc
DJ
14097 }
14098
14099 /* Add it to the queue. */
14100 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
14101
14102 return 1;
14103}
14104
14105/* Follow reference or signature attribute ATTR of SRC_DIE.
14106 On entry *REF_CU is the CU of SRC_DIE.
14107 On exit *REF_CU is the CU of the result. */
14108
14109static struct die_info *
14110follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14111 struct dwarf2_cu **ref_cu)
14112{
14113 struct die_info *die;
14114
14115 if (is_ref_attr (attr))
14116 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14117 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14118 die = follow_die_sig (src_die, attr, ref_cu);
14119 else
14120 {
14121 dump_die_for_error (src_die);
14122 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14123 (*ref_cu)->objfile->name);
14124 }
14125
14126 return die;
03dd20cc
DJ
14127}
14128
5c631832 14129/* Follow reference OFFSET.
673bfd45
DE
14130 On entry *REF_CU is the CU of the source die referencing OFFSET.
14131 On exit *REF_CU is the CU of the result.
14132 Returns NULL if OFFSET is invalid. */
f504f079 14133
f9aca02d 14134static struct die_info *
5c631832 14135follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 14136{
10b3939b 14137 struct die_info temp_die;
f2f0e013 14138 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14139
348e048f
DE
14140 gdb_assert (cu->per_cu != NULL);
14141
98bfdba5
PA
14142 target_cu = cu;
14143
8b70b953 14144 if (cu->per_cu->debug_type_section)
348e048f
DE
14145 {
14146 /* .debug_types CUs cannot reference anything outside their CU.
14147 If they need to, they have to reference a signatured type via
55f1336d 14148 DW_FORM_ref_sig8. */
348e048f 14149 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14150 return NULL;
348e048f
DE
14151 }
14152 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14153 {
14154 struct dwarf2_per_cu_data *per_cu;
9a619af0 14155
45452591 14156 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14157
14158 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
14159 if (maybe_queue_comp_unit (cu, per_cu))
14160 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 14161
10b3939b
DJ
14162 target_cu = per_cu->cu;
14163 }
98bfdba5
PA
14164 else if (cu->dies == NULL)
14165 {
14166 /* We're loading full DIEs during partial symbol reading. */
14167 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14168 load_full_comp_unit (cu->per_cu, cu->objfile);
14169 }
c906108c 14170
f2f0e013 14171 *ref_cu = target_cu;
51545339 14172 temp_die.offset = offset;
5c631832
JK
14173 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14174}
10b3939b 14175
5c631832
JK
14176/* Follow reference attribute ATTR of SRC_DIE.
14177 On entry *REF_CU is the CU of SRC_DIE.
14178 On exit *REF_CU is the CU of the result. */
14179
14180static struct die_info *
14181follow_die_ref (struct die_info *src_die, struct attribute *attr,
14182 struct dwarf2_cu **ref_cu)
14183{
14184 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14185 struct dwarf2_cu *cu = *ref_cu;
14186 struct die_info *die;
14187
14188 die = follow_die_offset (offset, ref_cu);
14189 if (!die)
14190 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14191 "at 0x%x [in module %s]"),
14192 offset, src_die->offset, cu->objfile->name);
348e048f 14193
5c631832
JK
14194 return die;
14195}
14196
d83e736b
JK
14197/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14198 Returned value is intended for DW_OP_call*. Returned
14199 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14200
14201struct dwarf2_locexpr_baton
14202dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
14203 struct dwarf2_per_cu_data *per_cu,
14204 CORE_ADDR (*get_frame_pc) (void *baton),
14205 void *baton)
5c631832 14206{
918dd910 14207 struct dwarf2_cu *cu;
5c631832
JK
14208 struct die_info *die;
14209 struct attribute *attr;
14210 struct dwarf2_locexpr_baton retval;
14211
8cf6f0b1
TT
14212 dw2_setup (per_cu->objfile);
14213
918dd910
JK
14214 if (per_cu->cu == NULL)
14215 load_cu (per_cu);
14216 cu = per_cu->cu;
14217
5c631832
JK
14218 die = follow_die_offset (offset, &cu);
14219 if (!die)
14220 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14221 offset, per_cu->cu->objfile->name);
14222
14223 attr = dwarf2_attr (die, DW_AT_location, cu);
14224 if (!attr)
14225 {
14226 /* DWARF: "If there is no such attribute, then there is no effect.". */
14227
14228 retval.data = NULL;
14229 retval.size = 0;
14230 }
8cf6f0b1
TT
14231 else if (attr_form_is_section_offset (attr))
14232 {
14233 struct dwarf2_loclist_baton loclist_baton;
14234 CORE_ADDR pc = (*get_frame_pc) (baton);
14235 size_t size;
14236
14237 fill_in_loclist_baton (cu, &loclist_baton, attr);
14238
14239 retval.data = dwarf2_find_location_expression (&loclist_baton,
14240 &size, pc);
14241 retval.size = size;
14242 }
5c631832
JK
14243 else
14244 {
14245 if (!attr_form_is_block (attr))
14246 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14247 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14248 offset, per_cu->cu->objfile->name);
14249
14250 retval.data = DW_BLOCK (attr)->data;
14251 retval.size = DW_BLOCK (attr)->size;
14252 }
14253 retval.per_cu = cu->per_cu;
918dd910 14254
918dd910
JK
14255 age_cached_comp_units ();
14256
5c631832 14257 return retval;
348e048f
DE
14258}
14259
8a9b8146
TT
14260/* Return the type of the DIE at DIE_OFFSET in the CU named by
14261 PER_CU. */
14262
14263struct type *
14264dwarf2_get_die_type (unsigned int die_offset,
14265 struct dwarf2_per_cu_data *per_cu)
14266{
8a9b8146 14267 dw2_setup (per_cu->objfile);
9ff3b74f 14268 return get_die_type_at_offset (die_offset, per_cu);
8a9b8146
TT
14269}
14270
348e048f
DE
14271/* Follow the signature attribute ATTR in SRC_DIE.
14272 On entry *REF_CU is the CU of SRC_DIE.
14273 On exit *REF_CU is the CU of the result. */
14274
14275static struct die_info *
14276follow_die_sig (struct die_info *src_die, struct attribute *attr,
14277 struct dwarf2_cu **ref_cu)
14278{
14279 struct objfile *objfile = (*ref_cu)->objfile;
14280 struct die_info temp_die;
14281 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14282 struct dwarf2_cu *sig_cu;
14283 struct die_info *die;
14284
14285 /* sig_type will be NULL if the signatured type is missing from
14286 the debug info. */
14287 if (sig_type == NULL)
14288 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14289 "at 0x%x [in module %s]"),
14290 src_die->offset, objfile->name);
14291
14292 /* If necessary, add it to the queue and load its DIEs. */
14293
14294 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14295 read_signatured_type (objfile, sig_type);
14296
14297 gdb_assert (sig_type->per_cu.cu != NULL);
14298
14299 sig_cu = sig_type->per_cu.cu;
14300 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14301 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14302 if (die)
14303 {
14304 *ref_cu = sig_cu;
14305 return die;
14306 }
14307
3e43a32a
MS
14308 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14309 "from DIE at 0x%x [in module %s]"),
348e048f
DE
14310 sig_type->type_offset, src_die->offset, objfile->name);
14311}
14312
14313/* Given an offset of a signatured type, return its signatured_type. */
14314
14315static struct signatured_type *
8b70b953
TT
14316lookup_signatured_type_at_offset (struct objfile *objfile,
14317 struct dwarf2_section_info *section,
14318 unsigned int offset)
348e048f 14319{
8b70b953 14320 gdb_byte *info_ptr = section->buffer + offset;
348e048f
DE
14321 unsigned int length, initial_length_size;
14322 unsigned int sig_offset;
14323 struct signatured_type find_entry, *type_sig;
14324
14325 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14326 sig_offset = (initial_length_size
14327 + 2 /*version*/
14328 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14329 + 1 /*address_size*/);
14330 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14331 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14332
14333 /* This is only used to lookup previously recorded types.
14334 If we didn't find it, it's our bug. */
14335 gdb_assert (type_sig != NULL);
b3c8eb43 14336 gdb_assert (offset == type_sig->per_cu.offset);
348e048f
DE
14337
14338 return type_sig;
14339}
14340
14341/* Read in signatured type at OFFSET and build its CU and die(s). */
14342
14343static void
14344read_signatured_type_at_offset (struct objfile *objfile,
8b70b953 14345 struct dwarf2_section_info *sect,
348e048f
DE
14346 unsigned int offset)
14347{
14348 struct signatured_type *type_sig;
14349
8b70b953 14350 dwarf2_read_section (objfile, sect);
be391dca 14351
348e048f
DE
14352 /* We have the section offset, but we need the signature to do the
14353 hash table lookup. */
8b70b953 14354 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f
DE
14355
14356 gdb_assert (type_sig->per_cu.cu == NULL);
14357
14358 read_signatured_type (objfile, type_sig);
14359
14360 gdb_assert (type_sig->per_cu.cu != NULL);
14361}
14362
14363/* Read in a signatured type and build its CU and DIEs. */
14364
14365static void
14366read_signatured_type (struct objfile *objfile,
14367 struct signatured_type *type_sig)
14368{
1fd400ff 14369 gdb_byte *types_ptr;
348e048f
DE
14370 struct die_reader_specs reader_specs;
14371 struct dwarf2_cu *cu;
14372 ULONGEST signature;
14373 struct cleanup *back_to, *free_cu_cleanup;
8b70b953 14374 struct dwarf2_section_info *section = type_sig->per_cu.debug_type_section;
348e048f 14375
8b70b953
TT
14376 dwarf2_read_section (objfile, section);
14377 types_ptr = section->buffer + type_sig->per_cu.offset;
1fd400ff 14378
348e048f
DE
14379 gdb_assert (type_sig->per_cu.cu == NULL);
14380
9816fde3
JK
14381 cu = xmalloc (sizeof (*cu));
14382 init_one_comp_unit (cu, objfile);
14383
348e048f
DE
14384 type_sig->per_cu.cu = cu;
14385 cu->per_cu = &type_sig->per_cu;
14386
14387 /* If an error occurs while loading, release our storage. */
14388 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14389
8b70b953 14390 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
348e048f
DE
14391 types_ptr, objfile->obfd);
14392 gdb_assert (signature == type_sig->signature);
14393
14394 cu->die_hash
14395 = htab_create_alloc_ex (cu->header.length / 12,
14396 die_hash,
14397 die_eq,
14398 NULL,
14399 &cu->comp_unit_obstack,
14400 hashtab_obstack_allocate,
14401 dummy_obstack_deallocate);
14402
14403 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14404 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14405
14406 init_cu_die_reader (&reader_specs, cu);
14407
14408 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14409 NULL /*parent*/);
14410
14411 /* We try not to read any attributes in this function, because not
14412 all objfiles needed for references have been loaded yet, and symbol
14413 table processing isn't initialized. But we have to set the CU language,
14414 or we won't be able to build types correctly. */
9816fde3 14415 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14416
14417 do_cleanups (back_to);
14418
14419 /* We've successfully allocated this compilation unit. Let our caller
14420 clean it up when finished with it. */
14421 discard_cleanups (free_cu_cleanup);
14422
14423 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14424 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
14425}
14426
3c6e0cb3
JK
14427/* Workaround as dwarf_expr_context_funcs.read_mem implementation before
14428 a proper runtime DWARF expressions evaluator gets implemented.
14429 Otherwise gnuv3_baseclass_offset would error by:
14430 Expected a negative vbase offset (old compiler?) */
14431
14432static void
14433decode_locdesc_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr,
14434 size_t length)
14435{
14436 struct dwarf_expr_context *ctx = baton;
14437 struct gdbarch *gdbarch = ctx->gdbarch;
14438 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
14439
14440 memset (buf, 0, length);
14441
14442 if (TYPE_LENGTH (ptr_type) == length)
14443 store_typed_address (buf, ptr_type, addr);
14444}
14445
14446static const struct dwarf_expr_context_funcs decode_locdesc_ctx_funcs =
14447{
14448 ctx_no_read_reg,
14449 decode_locdesc_read_mem,
14450 ctx_no_get_frame_base,
14451 ctx_no_get_frame_cfa,
14452 ctx_no_get_frame_pc,
14453 ctx_no_get_tls_address,
14454 ctx_no_dwarf_call,
14455 ctx_no_get_base_type
14456};
14457
c906108c
SS
14458/* Decode simple location descriptions.
14459 Given a pointer to a dwarf block that defines a location, compute
14460 the location and return the value.
14461
4cecd739
DJ
14462 NOTE drow/2003-11-18: This function is called in two situations
14463 now: for the address of static or global variables (partial symbols
14464 only) and for offsets into structures which are expected to be
14465 (more or less) constant. The partial symbol case should go away,
14466 and only the constant case should remain. That will let this
14467 function complain more accurately. A few special modes are allowed
14468 without complaint for global variables (for instance, global
14469 register values and thread-local values).
c906108c
SS
14470
14471 A location description containing no operations indicates that the
4cecd739 14472 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14473 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14474 callers will only want a very basic result and this can become a
3c6e0cb3 14475 complaint. */
c906108c
SS
14476
14477static CORE_ADDR
e7c27a73 14478decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14479{
e7c27a73 14480 struct objfile *objfile = cu->objfile;
3c6e0cb3
JK
14481 struct dwarf_expr_context *ctx;
14482 struct cleanup *old_chain;
14483 volatile struct gdb_exception ex;
c906108c 14484
3c6e0cb3
JK
14485 ctx = new_dwarf_expr_context ();
14486 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
14487 make_cleanup_value_free_to_mark (value_mark ());
f1bea926 14488
3c6e0cb3
JK
14489 ctx->gdbarch = get_objfile_arch (objfile);
14490 ctx->addr_size = cu->header.addr_size;
96408a79 14491 ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (cu->per_cu);
3c6e0cb3
JK
14492 ctx->offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14493 ctx->baton = ctx;
14494 ctx->funcs = &decode_locdesc_ctx_funcs;
c906108c 14495
3c6e0cb3
JK
14496 /* DW_AT_data_member_location expects the structure address to be pushed on
14497 the stack. Simulate the offset by address 0. */
14498 dwarf_expr_push_address (ctx, 0, 0);
c906108c 14499
3c6e0cb3
JK
14500 TRY_CATCH (ex, RETURN_MASK_ERROR)
14501 {
14502 dwarf_expr_eval (ctx, blk->data, blk->size);
14503 }
14504 if (ex.reason < 0)
14505 {
14506 if (ex.message)
14507 complaint (&symfile_complaints, "%s", ex.message);
14508 }
14509 else if (ctx->num_pieces == 0)
14510 switch (ctx->location)
14511 {
14512 /* The returned number will be bogus, just do not complain for locations
14513 in global registers - it is here only a partial symbol address. */
14514 case DWARF_VALUE_REGISTER:
d53d4ac5 14515
3c6e0cb3
JK
14516 case DWARF_VALUE_MEMORY:
14517 case DWARF_VALUE_STACK:
d53d4ac5 14518 {
3c6e0cb3 14519 CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
d53d4ac5 14520
3c6e0cb3
JK
14521 do_cleanups (old_chain);
14522 return address;
d53d4ac5 14523 }
3c6e0cb3
JK
14524 }
14525
14526 do_cleanups (old_chain);
14527 dwarf2_complex_location_expr_complaint ();
14528 return 0;
c906108c
SS
14529}
14530
14531/* memory allocation interface */
14532
c906108c 14533static struct dwarf_block *
7b5a2f43 14534dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14535{
14536 struct dwarf_block *blk;
14537
14538 blk = (struct dwarf_block *)
7b5a2f43 14539 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14540 return (blk);
14541}
14542
14543static struct abbrev_info *
f3dd6933 14544dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14545{
14546 struct abbrev_info *abbrev;
14547
f3dd6933
DJ
14548 abbrev = (struct abbrev_info *)
14549 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14550 memset (abbrev, 0, sizeof (struct abbrev_info));
14551 return (abbrev);
14552}
14553
14554static struct die_info *
b60c80d6 14555dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14556{
14557 struct die_info *die;
b60c80d6
DJ
14558 size_t size = sizeof (struct die_info);
14559
14560 if (num_attrs > 1)
14561 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14562
b60c80d6 14563 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14564 memset (die, 0, sizeof (struct die_info));
14565 return (die);
14566}
2e276125
JB
14567
14568\f
14569/* Macro support. */
14570
2e276125
JB
14571/* Return the full name of file number I in *LH's file name table.
14572 Use COMP_DIR as the name of the current directory of the
14573 compilation. The result is allocated using xmalloc; the caller is
14574 responsible for freeing it. */
14575static char *
14576file_full_name (int file, struct line_header *lh, const char *comp_dir)
14577{
6a83a1e6
EZ
14578 /* Is the file number a valid index into the line header's file name
14579 table? Remember that file numbers start with one, not zero. */
14580 if (1 <= file && file <= lh->num_file_names)
14581 {
14582 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14583
6a83a1e6
EZ
14584 if (IS_ABSOLUTE_PATH (fe->name))
14585 return xstrdup (fe->name);
14586 else
14587 {
14588 const char *dir;
14589 int dir_len;
14590 char *full_name;
14591
14592 if (fe->dir_index)
14593 dir = lh->include_dirs[fe->dir_index - 1];
14594 else
14595 dir = comp_dir;
14596
14597 if (dir)
14598 {
14599 dir_len = strlen (dir);
14600 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14601 strcpy (full_name, dir);
14602 full_name[dir_len] = '/';
14603 strcpy (full_name + dir_len + 1, fe->name);
14604 return full_name;
14605 }
14606 else
14607 return xstrdup (fe->name);
14608 }
14609 }
2e276125
JB
14610 else
14611 {
6a83a1e6
EZ
14612 /* The compiler produced a bogus file number. We can at least
14613 record the macro definitions made in the file, even if we
14614 won't be able to find the file by name. */
14615 char fake_name[80];
9a619af0 14616
6a83a1e6 14617 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14618
6e70227d 14619 complaint (&symfile_complaints,
6a83a1e6
EZ
14620 _("bad file number in macro information (%d)"),
14621 file);
2e276125 14622
6a83a1e6 14623 return xstrdup (fake_name);
2e276125
JB
14624 }
14625}
14626
14627
14628static struct macro_source_file *
14629macro_start_file (int file, int line,
14630 struct macro_source_file *current_file,
14631 const char *comp_dir,
14632 struct line_header *lh, struct objfile *objfile)
14633{
14634 /* The full name of this source file. */
14635 char *full_name = file_full_name (file, lh, comp_dir);
14636
14637 /* We don't create a macro table for this compilation unit
14638 at all until we actually get a filename. */
14639 if (! pending_macros)
4a146b47 14640 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14641 objfile->macro_cache);
2e276125
JB
14642
14643 if (! current_file)
14644 /* If we have no current file, then this must be the start_file
14645 directive for the compilation unit's main source file. */
14646 current_file = macro_set_main (pending_macros, full_name);
14647 else
14648 current_file = macro_include (current_file, line, full_name);
14649
14650 xfree (full_name);
6e70227d 14651
2e276125
JB
14652 return current_file;
14653}
14654
14655
14656/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14657 followed by a null byte. */
14658static char *
14659copy_string (const char *buf, int len)
14660{
14661 char *s = xmalloc (len + 1);
9a619af0 14662
2e276125
JB
14663 memcpy (s, buf, len);
14664 s[len] = '\0';
2e276125
JB
14665 return s;
14666}
14667
14668
14669static const char *
14670consume_improper_spaces (const char *p, const char *body)
14671{
14672 if (*p == ' ')
14673 {
4d3c2250 14674 complaint (&symfile_complaints,
3e43a32a
MS
14675 _("macro definition contains spaces "
14676 "in formal argument list:\n`%s'"),
4d3c2250 14677 body);
2e276125
JB
14678
14679 while (*p == ' ')
14680 p++;
14681 }
14682
14683 return p;
14684}
14685
14686
14687static void
14688parse_macro_definition (struct macro_source_file *file, int line,
14689 const char *body)
14690{
14691 const char *p;
14692
14693 /* The body string takes one of two forms. For object-like macro
14694 definitions, it should be:
14695
14696 <macro name> " " <definition>
14697
14698 For function-like macro definitions, it should be:
14699
14700 <macro name> "() " <definition>
14701 or
14702 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14703
14704 Spaces may appear only where explicitly indicated, and in the
14705 <definition>.
14706
14707 The Dwarf 2 spec says that an object-like macro's name is always
14708 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14709 the space when the macro's definition is the empty string.
2e276125
JB
14710
14711 The Dwarf 2 spec says that there should be no spaces between the
14712 formal arguments in a function-like macro's formal argument list,
14713 but versions of GCC around March 2002 include spaces after the
14714 commas. */
14715
14716
14717 /* Find the extent of the macro name. The macro name is terminated
14718 by either a space or null character (for an object-like macro) or
14719 an opening paren (for a function-like macro). */
14720 for (p = body; *p; p++)
14721 if (*p == ' ' || *p == '(')
14722 break;
14723
14724 if (*p == ' ' || *p == '\0')
14725 {
14726 /* It's an object-like macro. */
14727 int name_len = p - body;
14728 char *name = copy_string (body, name_len);
14729 const char *replacement;
14730
14731 if (*p == ' ')
14732 replacement = body + name_len + 1;
14733 else
14734 {
4d3c2250 14735 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14736 replacement = body + name_len;
14737 }
6e70227d 14738
2e276125
JB
14739 macro_define_object (file, line, name, replacement);
14740
14741 xfree (name);
14742 }
14743 else if (*p == '(')
14744 {
14745 /* It's a function-like macro. */
14746 char *name = copy_string (body, p - body);
14747 int argc = 0;
14748 int argv_size = 1;
14749 char **argv = xmalloc (argv_size * sizeof (*argv));
14750
14751 p++;
14752
14753 p = consume_improper_spaces (p, body);
14754
14755 /* Parse the formal argument list. */
14756 while (*p && *p != ')')
14757 {
14758 /* Find the extent of the current argument name. */
14759 const char *arg_start = p;
14760
14761 while (*p && *p != ',' && *p != ')' && *p != ' ')
14762 p++;
14763
14764 if (! *p || p == arg_start)
4d3c2250 14765 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14766 else
14767 {
14768 /* Make sure argv has room for the new argument. */
14769 if (argc >= argv_size)
14770 {
14771 argv_size *= 2;
14772 argv = xrealloc (argv, argv_size * sizeof (*argv));
14773 }
14774
14775 argv[argc++] = copy_string (arg_start, p - arg_start);
14776 }
14777
14778 p = consume_improper_spaces (p, body);
14779
14780 /* Consume the comma, if present. */
14781 if (*p == ',')
14782 {
14783 p++;
14784
14785 p = consume_improper_spaces (p, body);
14786 }
14787 }
14788
14789 if (*p == ')')
14790 {
14791 p++;
14792
14793 if (*p == ' ')
14794 /* Perfectly formed definition, no complaints. */
14795 macro_define_function (file, line, name,
6e70227d 14796 argc, (const char **) argv,
2e276125
JB
14797 p + 1);
14798 else if (*p == '\0')
14799 {
14800 /* Complain, but do define it. */
4d3c2250 14801 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14802 macro_define_function (file, line, name,
6e70227d 14803 argc, (const char **) argv,
2e276125
JB
14804 p);
14805 }
14806 else
14807 /* Just complain. */
4d3c2250 14808 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14809 }
14810 else
14811 /* Just complain. */
4d3c2250 14812 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14813
14814 xfree (name);
14815 {
14816 int i;
14817
14818 for (i = 0; i < argc; i++)
14819 xfree (argv[i]);
14820 }
14821 xfree (argv);
14822 }
14823 else
4d3c2250 14824 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14825}
14826
cf2c3c16
TT
14827/* Skip some bytes from BYTES according to the form given in FORM.
14828 Returns the new pointer. */
2e276125 14829
cf2c3c16
TT
14830static gdb_byte *
14831skip_form_bytes (bfd *abfd, gdb_byte *bytes,
14832 enum dwarf_form form,
14833 unsigned int offset_size,
14834 struct dwarf2_section_info *section)
2e276125 14835{
cf2c3c16 14836 unsigned int bytes_read;
2e276125 14837
cf2c3c16 14838 switch (form)
2e276125 14839 {
cf2c3c16
TT
14840 case DW_FORM_data1:
14841 case DW_FORM_flag:
14842 ++bytes;
14843 break;
14844
14845 case DW_FORM_data2:
14846 bytes += 2;
14847 break;
14848
14849 case DW_FORM_data4:
14850 bytes += 4;
14851 break;
14852
14853 case DW_FORM_data8:
14854 bytes += 8;
14855 break;
14856
14857 case DW_FORM_string:
14858 read_direct_string (abfd, bytes, &bytes_read);
14859 bytes += bytes_read;
14860 break;
14861
14862 case DW_FORM_sec_offset:
14863 case DW_FORM_strp:
14864 bytes += offset_size;
14865 break;
14866
14867 case DW_FORM_block:
14868 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
14869 bytes += bytes_read;
14870 break;
14871
14872 case DW_FORM_block1:
14873 bytes += 1 + read_1_byte (abfd, bytes);
14874 break;
14875 case DW_FORM_block2:
14876 bytes += 2 + read_2_bytes (abfd, bytes);
14877 break;
14878 case DW_FORM_block4:
14879 bytes += 4 + read_4_bytes (abfd, bytes);
14880 break;
14881
14882 case DW_FORM_sdata:
14883 case DW_FORM_udata:
14884 bytes = skip_leb128 (abfd, bytes);
14885 break;
14886
14887 default:
14888 {
14889 complain:
14890 complaint (&symfile_complaints,
14891 _("invalid form 0x%x in `%s'"),
14892 form,
14893 section->asection->name);
14894 return NULL;
14895 }
2e276125
JB
14896 }
14897
cf2c3c16
TT
14898 return bytes;
14899}
757a13d0 14900
cf2c3c16
TT
14901/* A helper for dwarf_decode_macros that handles skipping an unknown
14902 opcode. Returns an updated pointer to the macro data buffer; or,
14903 on error, issues a complaint and returns NULL. */
757a13d0 14904
cf2c3c16
TT
14905static gdb_byte *
14906skip_unknown_opcode (unsigned int opcode,
14907 gdb_byte **opcode_definitions,
14908 gdb_byte *mac_ptr,
14909 bfd *abfd,
14910 unsigned int offset_size,
14911 struct dwarf2_section_info *section)
14912{
14913 unsigned int bytes_read, i;
14914 unsigned long arg;
14915 gdb_byte *defn;
2e276125 14916
cf2c3c16 14917 if (opcode_definitions[opcode] == NULL)
2e276125 14918 {
cf2c3c16
TT
14919 complaint (&symfile_complaints,
14920 _("unrecognized DW_MACFINO opcode 0x%x"),
14921 opcode);
14922 return NULL;
14923 }
2e276125 14924
cf2c3c16
TT
14925 defn = opcode_definitions[opcode];
14926 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
14927 defn += bytes_read;
2e276125 14928
cf2c3c16
TT
14929 for (i = 0; i < arg; ++i)
14930 {
14931 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
14932 if (mac_ptr == NULL)
14933 {
14934 /* skip_form_bytes already issued the complaint. */
14935 return NULL;
14936 }
14937 }
757a13d0 14938
cf2c3c16
TT
14939 return mac_ptr;
14940}
757a13d0 14941
cf2c3c16
TT
14942/* A helper function which parses the header of a macro section.
14943 If the macro section is the extended (for now called "GNU") type,
14944 then this updates *OFFSET_SIZE. Returns a pointer to just after
14945 the header, or issues a complaint and returns NULL on error. */
757a13d0 14946
cf2c3c16
TT
14947static gdb_byte *
14948dwarf_parse_macro_header (gdb_byte **opcode_definitions,
14949 bfd *abfd,
14950 gdb_byte *mac_ptr,
14951 unsigned int *offset_size,
14952 int section_is_gnu)
14953{
14954 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 14955
cf2c3c16
TT
14956 if (section_is_gnu)
14957 {
14958 unsigned int version, flags;
757a13d0 14959
cf2c3c16
TT
14960 version = read_2_bytes (abfd, mac_ptr);
14961 if (version != 4)
14962 {
14963 complaint (&symfile_complaints,
14964 _("unrecognized version `%d' in .debug_macro section"),
14965 version);
14966 return NULL;
14967 }
14968 mac_ptr += 2;
757a13d0 14969
cf2c3c16
TT
14970 flags = read_1_byte (abfd, mac_ptr);
14971 ++mac_ptr;
14972 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 14973
cf2c3c16
TT
14974 if ((flags & 2) != 0)
14975 /* We don't need the line table offset. */
14976 mac_ptr += *offset_size;
757a13d0 14977
cf2c3c16
TT
14978 /* Vendor opcode descriptions. */
14979 if ((flags & 4) != 0)
14980 {
14981 unsigned int i, count;
757a13d0 14982
cf2c3c16
TT
14983 count = read_1_byte (abfd, mac_ptr);
14984 ++mac_ptr;
14985 for (i = 0; i < count; ++i)
14986 {
14987 unsigned int opcode, bytes_read;
14988 unsigned long arg;
14989
14990 opcode = read_1_byte (abfd, mac_ptr);
14991 ++mac_ptr;
14992 opcode_definitions[opcode] = mac_ptr;
14993 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14994 mac_ptr += bytes_read;
14995 mac_ptr += arg;
14996 }
757a13d0 14997 }
cf2c3c16 14998 }
757a13d0 14999
cf2c3c16
TT
15000 return mac_ptr;
15001}
757a13d0 15002
cf2c3c16
TT
15003/* A helper for dwarf_decode_macros that handles the GNU extensions,
15004 including DW_GNU_MACINFO_transparent_include. */
15005
15006static void
15007dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15008 struct macro_source_file *current_file,
15009 struct line_header *lh, char *comp_dir,
15010 struct dwarf2_section_info *section,
15011 int section_is_gnu,
15012 unsigned int offset_size,
15013 struct objfile *objfile)
15014{
15015 enum dwarf_macro_record_type macinfo_type;
15016 int at_commandline;
15017 gdb_byte *opcode_definitions[256];
757a13d0 15018
cf2c3c16
TT
15019 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15020 &offset_size, section_is_gnu);
15021 if (mac_ptr == NULL)
15022 {
15023 /* We already issued a complaint. */
15024 return;
15025 }
757a13d0
JK
15026
15027 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15028 GDB is still reading the definitions from command line. First
15029 DW_MACINFO_start_file will need to be ignored as it was already executed
15030 to create CURRENT_FILE for the main source holding also the command line
15031 definitions. On first met DW_MACINFO_start_file this flag is reset to
15032 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15033
15034 at_commandline = 1;
15035
15036 do
15037 {
15038 /* Do we at least have room for a macinfo type byte? */
15039 if (mac_ptr >= mac_end)
15040 {
cf2c3c16 15041 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
15042 break;
15043 }
15044
15045 macinfo_type = read_1_byte (abfd, mac_ptr);
15046 mac_ptr++;
15047
cf2c3c16
TT
15048 /* Note that we rely on the fact that the corresponding GNU and
15049 DWARF constants are the same. */
757a13d0
JK
15050 switch (macinfo_type)
15051 {
15052 /* A zero macinfo type indicates the end of the macro
15053 information. */
15054 case 0:
15055 break;
2e276125 15056
cf2c3c16
TT
15057 case DW_MACRO_GNU_define:
15058 case DW_MACRO_GNU_undef:
15059 case DW_MACRO_GNU_define_indirect:
15060 case DW_MACRO_GNU_undef_indirect:
2e276125 15061 {
891d2f0b 15062 unsigned int bytes_read;
2e276125
JB
15063 int line;
15064 char *body;
cf2c3c16 15065 int is_define;
2e276125 15066
cf2c3c16
TT
15067 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15068 mac_ptr += bytes_read;
15069
15070 if (macinfo_type == DW_MACRO_GNU_define
15071 || macinfo_type == DW_MACRO_GNU_undef)
15072 {
15073 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15074 mac_ptr += bytes_read;
15075 }
15076 else
15077 {
15078 LONGEST str_offset;
15079
15080 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15081 mac_ptr += offset_size;
2e276125 15082
cf2c3c16
TT
15083 body = read_indirect_string_at_offset (abfd, str_offset);
15084 }
15085
15086 is_define = (macinfo_type == DW_MACRO_GNU_define
15087 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 15088 if (! current_file)
757a13d0
JK
15089 {
15090 /* DWARF violation as no main source is present. */
15091 complaint (&symfile_complaints,
15092 _("debug info with no main source gives macro %s "
15093 "on line %d: %s"),
cf2c3c16
TT
15094 is_define ? _("definition") : _("undefinition"),
15095 line, body);
757a13d0
JK
15096 break;
15097 }
3e43a32a
MS
15098 if ((line == 0 && !at_commandline)
15099 || (line != 0 && at_commandline))
4d3c2250 15100 complaint (&symfile_complaints,
757a13d0
JK
15101 _("debug info gives %s macro %s with %s line %d: %s"),
15102 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 15103 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
15104 line == 0 ? _("zero") : _("non-zero"), line, body);
15105
cf2c3c16 15106 if (is_define)
757a13d0 15107 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
15108 else
15109 {
15110 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15111 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15112 macro_undef (current_file, line, body);
15113 }
2e276125
JB
15114 }
15115 break;
15116
cf2c3c16 15117 case DW_MACRO_GNU_start_file:
2e276125 15118 {
891d2f0b 15119 unsigned int bytes_read;
2e276125
JB
15120 int line, file;
15121
15122 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15123 mac_ptr += bytes_read;
15124 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15125 mac_ptr += bytes_read;
15126
3e43a32a
MS
15127 if ((line == 0 && !at_commandline)
15128 || (line != 0 && at_commandline))
757a13d0
JK
15129 complaint (&symfile_complaints,
15130 _("debug info gives source %d included "
15131 "from %s at %s line %d"),
15132 file, at_commandline ? _("command-line") : _("file"),
15133 line == 0 ? _("zero") : _("non-zero"), line);
15134
15135 if (at_commandline)
15136 {
cf2c3c16
TT
15137 /* This DW_MACRO_GNU_start_file was executed in the
15138 pass one. */
757a13d0
JK
15139 at_commandline = 0;
15140 }
15141 else
15142 current_file = macro_start_file (file, line,
15143 current_file, comp_dir,
cf2c3c16 15144 lh, objfile);
2e276125
JB
15145 }
15146 break;
15147
cf2c3c16 15148 case DW_MACRO_GNU_end_file:
2e276125 15149 if (! current_file)
4d3c2250 15150 complaint (&symfile_complaints,
3e43a32a
MS
15151 _("macro debug info has an unmatched "
15152 "`close_file' directive"));
2e276125
JB
15153 else
15154 {
15155 current_file = current_file->included_by;
15156 if (! current_file)
15157 {
cf2c3c16 15158 enum dwarf_macro_record_type next_type;
2e276125
JB
15159
15160 /* GCC circa March 2002 doesn't produce the zero
15161 type byte marking the end of the compilation
15162 unit. Complain if it's not there, but exit no
15163 matter what. */
15164
15165 /* Do we at least have room for a macinfo type byte? */
15166 if (mac_ptr >= mac_end)
15167 {
cf2c3c16 15168 dwarf2_macros_too_long_complaint (section);
2e276125
JB
15169 return;
15170 }
15171
15172 /* We don't increment mac_ptr here, so this is just
15173 a look-ahead. */
15174 next_type = read_1_byte (abfd, mac_ptr);
15175 if (next_type != 0)
4d3c2250 15176 complaint (&symfile_complaints,
3e43a32a
MS
15177 _("no terminating 0-type entry for "
15178 "macros in `.debug_macinfo' section"));
2e276125
JB
15179
15180 return;
15181 }
15182 }
15183 break;
15184
cf2c3c16
TT
15185 case DW_MACRO_GNU_transparent_include:
15186 {
15187 LONGEST offset;
15188
15189 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15190 mac_ptr += offset_size;
15191
15192 dwarf_decode_macro_bytes (abfd,
15193 section->buffer + offset,
15194 mac_end, current_file,
15195 lh, comp_dir,
15196 section, section_is_gnu,
15197 offset_size, objfile);
15198 }
15199 break;
15200
2e276125 15201 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
15202 if (!section_is_gnu)
15203 {
15204 unsigned int bytes_read;
15205 int constant;
2e276125 15206
cf2c3c16
TT
15207 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15208 mac_ptr += bytes_read;
15209 read_direct_string (abfd, mac_ptr, &bytes_read);
15210 mac_ptr += bytes_read;
2e276125 15211
cf2c3c16
TT
15212 /* We don't recognize any vendor extensions. */
15213 break;
15214 }
15215 /* FALLTHROUGH */
15216
15217 default:
15218 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15219 mac_ptr, abfd, offset_size,
15220 section);
15221 if (mac_ptr == NULL)
15222 return;
15223 break;
2e276125 15224 }
757a13d0 15225 } while (macinfo_type != 0);
2e276125 15226}
8e19ed76 15227
cf2c3c16
TT
15228static void
15229dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15230 char *comp_dir, bfd *abfd,
15231 struct dwarf2_cu *cu,
15232 struct dwarf2_section_info *section,
15233 int section_is_gnu)
15234{
15235 gdb_byte *mac_ptr, *mac_end;
15236 struct macro_source_file *current_file = 0;
15237 enum dwarf_macro_record_type macinfo_type;
15238 unsigned int offset_size = cu->header.offset_size;
15239 gdb_byte *opcode_definitions[256];
15240
15241 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15242 if (section->buffer == NULL)
15243 {
15244 complaint (&symfile_complaints, _("missing %s section"),
15245 section->asection->name);
15246 return;
15247 }
15248
15249 /* First pass: Find the name of the base filename.
15250 This filename is needed in order to process all macros whose definition
15251 (or undefinition) comes from the command line. These macros are defined
15252 before the first DW_MACINFO_start_file entry, and yet still need to be
15253 associated to the base file.
15254
15255 To determine the base file name, we scan the macro definitions until we
15256 reach the first DW_MACINFO_start_file entry. We then initialize
15257 CURRENT_FILE accordingly so that any macro definition found before the
15258 first DW_MACINFO_start_file can still be associated to the base file. */
15259
15260 mac_ptr = section->buffer + offset;
15261 mac_end = section->buffer + section->size;
15262
15263 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15264 &offset_size, section_is_gnu);
15265 if (mac_ptr == NULL)
15266 {
15267 /* We already issued a complaint. */
15268 return;
15269 }
15270
15271 do
15272 {
15273 /* Do we at least have room for a macinfo type byte? */
15274 if (mac_ptr >= mac_end)
15275 {
15276 /* Complaint is printed during the second pass as GDB will probably
15277 stop the first pass earlier upon finding
15278 DW_MACINFO_start_file. */
15279 break;
15280 }
15281
15282 macinfo_type = read_1_byte (abfd, mac_ptr);
15283 mac_ptr++;
15284
15285 /* Note that we rely on the fact that the corresponding GNU and
15286 DWARF constants are the same. */
15287 switch (macinfo_type)
15288 {
15289 /* A zero macinfo type indicates the end of the macro
15290 information. */
15291 case 0:
15292 break;
15293
15294 case DW_MACRO_GNU_define:
15295 case DW_MACRO_GNU_undef:
15296 /* Only skip the data by MAC_PTR. */
15297 {
15298 unsigned int bytes_read;
15299
15300 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15301 mac_ptr += bytes_read;
15302 read_direct_string (abfd, mac_ptr, &bytes_read);
15303 mac_ptr += bytes_read;
15304 }
15305 break;
15306
15307 case DW_MACRO_GNU_start_file:
15308 {
15309 unsigned int bytes_read;
15310 int line, file;
15311
15312 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15313 mac_ptr += bytes_read;
15314 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15315 mac_ptr += bytes_read;
15316
15317 current_file = macro_start_file (file, line, current_file,
15318 comp_dir, lh, cu->objfile);
15319 }
15320 break;
15321
15322 case DW_MACRO_GNU_end_file:
15323 /* No data to skip by MAC_PTR. */
15324 break;
15325
15326 case DW_MACRO_GNU_define_indirect:
15327 case DW_MACRO_GNU_undef_indirect:
15328 {
15329 unsigned int bytes_read;
15330
15331 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15332 mac_ptr += bytes_read;
15333 mac_ptr += offset_size;
15334 }
15335 break;
15336
15337 case DW_MACRO_GNU_transparent_include:
15338 /* Note that, according to the spec, a transparent include
15339 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15340 skip this opcode. */
15341 mac_ptr += offset_size;
15342 break;
15343
15344 case DW_MACINFO_vendor_ext:
15345 /* Only skip the data by MAC_PTR. */
15346 if (!section_is_gnu)
15347 {
15348 unsigned int bytes_read;
15349
15350 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15351 mac_ptr += bytes_read;
15352 read_direct_string (abfd, mac_ptr, &bytes_read);
15353 mac_ptr += bytes_read;
15354 }
15355 /* FALLTHROUGH */
15356
15357 default:
15358 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15359 mac_ptr, abfd, offset_size,
15360 section);
15361 if (mac_ptr == NULL)
15362 return;
15363 break;
15364 }
15365 } while (macinfo_type != 0 && current_file == NULL);
15366
15367 /* Second pass: Process all entries.
15368
15369 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15370 command-line macro definitions/undefinitions. This flag is unset when we
15371 reach the first DW_MACINFO_start_file entry. */
15372
15373 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15374 current_file, lh, comp_dir, section, section_is_gnu,
15375 offset_size, cu->objfile);
15376}
15377
8e19ed76 15378/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 15379 if so return true else false. */
8e19ed76
PS
15380static int
15381attr_form_is_block (struct attribute *attr)
15382{
15383 return (attr == NULL ? 0 :
15384 attr->form == DW_FORM_block1
15385 || attr->form == DW_FORM_block2
15386 || attr->form == DW_FORM_block4
2dc7f7b3
TT
15387 || attr->form == DW_FORM_block
15388 || attr->form == DW_FORM_exprloc);
8e19ed76 15389}
4c2df51b 15390
c6a0999f
JB
15391/* Return non-zero if ATTR's value is a section offset --- classes
15392 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15393 You may use DW_UNSND (attr) to retrieve such offsets.
15394
15395 Section 7.5.4, "Attribute Encodings", explains that no attribute
15396 may have a value that belongs to more than one of these classes; it
15397 would be ambiguous if we did, because we use the same forms for all
15398 of them. */
3690dd37
JB
15399static int
15400attr_form_is_section_offset (struct attribute *attr)
15401{
15402 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
15403 || attr->form == DW_FORM_data8
15404 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
15405}
15406
15407
15408/* Return non-zero if ATTR's value falls in the 'constant' class, or
15409 zero otherwise. When this function returns true, you can apply
15410 dwarf2_get_attr_constant_value to it.
15411
15412 However, note that for some attributes you must check
15413 attr_form_is_section_offset before using this test. DW_FORM_data4
15414 and DW_FORM_data8 are members of both the constant class, and of
15415 the classes that contain offsets into other debug sections
15416 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15417 that, if an attribute's can be either a constant or one of the
15418 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15419 taken as section offsets, not constants. */
15420static int
15421attr_form_is_constant (struct attribute *attr)
15422{
15423 switch (attr->form)
15424 {
15425 case DW_FORM_sdata:
15426 case DW_FORM_udata:
15427 case DW_FORM_data1:
15428 case DW_FORM_data2:
15429 case DW_FORM_data4:
15430 case DW_FORM_data8:
15431 return 1;
15432 default:
15433 return 0;
15434 }
15435}
15436
8cf6f0b1
TT
15437/* A helper function that fills in a dwarf2_loclist_baton. */
15438
15439static void
15440fill_in_loclist_baton (struct dwarf2_cu *cu,
15441 struct dwarf2_loclist_baton *baton,
15442 struct attribute *attr)
15443{
15444 dwarf2_read_section (dwarf2_per_objfile->objfile,
15445 &dwarf2_per_objfile->loc);
15446
15447 baton->per_cu = cu->per_cu;
15448 gdb_assert (baton->per_cu);
15449 /* We don't know how long the location list is, but make sure we
15450 don't run off the edge of the section. */
15451 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15452 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15453 baton->base_address = cu->base_address;
15454}
15455
4c2df51b
DJ
15456static void
15457dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 15458 struct dwarf2_cu *cu)
4c2df51b 15459{
3690dd37 15460 if (attr_form_is_section_offset (attr)
99bcc461
DJ
15461 /* ".debug_loc" may not exist at all, or the offset may be outside
15462 the section. If so, fall through to the complaint in the
15463 other branch. */
9e0ac564
TT
15464 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15465 &dwarf2_per_objfile->loc))
4c2df51b 15466 {
0d53c4c4 15467 struct dwarf2_loclist_baton *baton;
4c2df51b 15468
4a146b47 15469 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 15470 sizeof (struct dwarf2_loclist_baton));
4c2df51b 15471
8cf6f0b1 15472 fill_in_loclist_baton (cu, baton, attr);
be391dca 15473
d00adf39 15474 if (cu->base_known == 0)
0d53c4c4 15475 complaint (&symfile_complaints,
3e43a32a
MS
15476 _("Location list used without "
15477 "specifying the CU base address."));
4c2df51b 15478
768a979c 15479 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
15480 SYMBOL_LOCATION_BATON (sym) = baton;
15481 }
15482 else
15483 {
15484 struct dwarf2_locexpr_baton *baton;
15485
4a146b47 15486 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 15487 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
15488 baton->per_cu = cu->per_cu;
15489 gdb_assert (baton->per_cu);
0d53c4c4
DJ
15490
15491 if (attr_form_is_block (attr))
15492 {
15493 /* Note that we're just copying the block's data pointer
15494 here, not the actual data. We're still pointing into the
6502dd73
DJ
15495 info_buffer for SYM's objfile; right now we never release
15496 that buffer, but when we do clean up properly this may
15497 need to change. */
0d53c4c4
DJ
15498 baton->size = DW_BLOCK (attr)->size;
15499 baton->data = DW_BLOCK (attr)->data;
15500 }
15501 else
15502 {
15503 dwarf2_invalid_attrib_class_complaint ("location description",
15504 SYMBOL_NATURAL_NAME (sym));
15505 baton->size = 0;
15506 baton->data = NULL;
15507 }
6e70227d 15508
768a979c 15509 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
15510 SYMBOL_LOCATION_BATON (sym) = baton;
15511 }
4c2df51b 15512}
6502dd73 15513
9aa1f1e3
TT
15514/* Return the OBJFILE associated with the compilation unit CU. If CU
15515 came from a separate debuginfo file, then the master objfile is
15516 returned. */
ae0d2f24
UW
15517
15518struct objfile *
15519dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15520{
9291a0cd 15521 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15522
15523 /* Return the master objfile, so that we can report and look up the
15524 correct file containing this variable. */
15525 if (objfile->separate_debug_objfile_backlink)
15526 objfile = objfile->separate_debug_objfile_backlink;
15527
15528 return objfile;
15529}
15530
96408a79
SA
15531/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15532 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15533 CU_HEADERP first. */
15534
15535static const struct comp_unit_head *
15536per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15537 struct dwarf2_per_cu_data *per_cu)
15538{
15539 struct objfile *objfile;
15540 struct dwarf2_per_objfile *per_objfile;
15541 gdb_byte *info_ptr;
15542
15543 if (per_cu->cu)
15544 return &per_cu->cu->header;
15545
15546 objfile = per_cu->objfile;
15547 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15548 info_ptr = per_objfile->info.buffer + per_cu->offset;
15549
15550 memset (cu_headerp, 0, sizeof (*cu_headerp));
15551 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15552
15553 return cu_headerp;
15554}
15555
ae0d2f24
UW
15556/* Return the address size given in the compilation unit header for CU. */
15557
15558CORE_ADDR
15559dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15560{
96408a79
SA
15561 struct comp_unit_head cu_header_local;
15562 const struct comp_unit_head *cu_headerp;
c471e790 15563
96408a79
SA
15564 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15565
15566 return cu_headerp->addr_size;
ae0d2f24
UW
15567}
15568
9eae7c52
TT
15569/* Return the offset size given in the compilation unit header for CU. */
15570
15571int
15572dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15573{
96408a79
SA
15574 struct comp_unit_head cu_header_local;
15575 const struct comp_unit_head *cu_headerp;
9c6c53f7 15576
96408a79
SA
15577 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15578
15579 return cu_headerp->offset_size;
15580}
15581
15582/* See its dwarf2loc.h declaration. */
15583
15584int
15585dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15586{
15587 struct comp_unit_head cu_header_local;
15588 const struct comp_unit_head *cu_headerp;
15589
15590 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15591
15592 if (cu_headerp->version == 2)
15593 return cu_headerp->addr_size;
15594 else
15595 return cu_headerp->offset_size;
181cebd4
JK
15596}
15597
9aa1f1e3
TT
15598/* Return the text offset of the CU. The returned offset comes from
15599 this CU's objfile. If this objfile came from a separate debuginfo
15600 file, then the offset may be different from the corresponding
15601 offset in the parent objfile. */
15602
15603CORE_ADDR
15604dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15605{
bb3fa9d0 15606 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
15607
15608 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15609}
15610
348e048f
DE
15611/* Locate the .debug_info compilation unit from CU's objfile which contains
15612 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
15613
15614static struct dwarf2_per_cu_data *
c764a876 15615dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
15616 struct objfile *objfile)
15617{
15618 struct dwarf2_per_cu_data *this_cu;
15619 int low, high;
15620
ae038cb0
DJ
15621 low = 0;
15622 high = dwarf2_per_objfile->n_comp_units - 1;
15623 while (high > low)
15624 {
15625 int mid = low + (high - low) / 2;
9a619af0 15626
ae038cb0
DJ
15627 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15628 high = mid;
15629 else
15630 low = mid + 1;
15631 }
15632 gdb_assert (low == high);
15633 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15634 {
10b3939b 15635 if (low == 0)
8a3fe4f8
AC
15636 error (_("Dwarf Error: could not find partial DIE containing "
15637 "offset 0x%lx [in module %s]"),
10b3939b
DJ
15638 (long) offset, bfd_get_filename (objfile->obfd));
15639
ae038cb0
DJ
15640 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15641 return dwarf2_per_objfile->all_comp_units[low-1];
15642 }
15643 else
15644 {
15645 this_cu = dwarf2_per_objfile->all_comp_units[low];
15646 if (low == dwarf2_per_objfile->n_comp_units - 1
15647 && offset >= this_cu->offset + this_cu->length)
c764a876 15648 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
15649 gdb_assert (offset < this_cu->offset + this_cu->length);
15650 return this_cu;
15651 }
15652}
15653
10b3939b
DJ
15654/* Locate the compilation unit from OBJFILE which is located at exactly
15655 OFFSET. Raises an error on failure. */
15656
ae038cb0 15657static struct dwarf2_per_cu_data *
c764a876 15658dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
15659{
15660 struct dwarf2_per_cu_data *this_cu;
9a619af0 15661
ae038cb0
DJ
15662 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15663 if (this_cu->offset != offset)
c764a876 15664 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
15665 return this_cu;
15666}
15667
9816fde3 15668/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 15669
9816fde3
JK
15670static void
15671init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 15672{
9816fde3 15673 memset (cu, 0, sizeof (*cu));
93311388
DE
15674 cu->objfile = objfile;
15675 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15676}
15677
15678/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15679
15680static void
15681prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15682{
15683 struct attribute *attr;
15684
15685 /* Set the language we're debugging. */
15686 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15687 if (attr)
15688 set_cu_language (DW_UNSND (attr), cu);
15689 else
9cded63f
TT
15690 {
15691 cu->language = language_minimal;
15692 cu->language_defn = language_def (cu->language);
15693 }
93311388
DE
15694}
15695
ae038cb0
DJ
15696/* Release one cached compilation unit, CU. We unlink it from the tree
15697 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15698 the caller is responsible for that.
15699 NOTE: DATA is a void * because this function is also used as a
15700 cleanup routine. */
ae038cb0
DJ
15701
15702static void
15703free_one_comp_unit (void *data)
15704{
15705 struct dwarf2_cu *cu = data;
15706
15707 if (cu->per_cu != NULL)
15708 cu->per_cu->cu = NULL;
15709 cu->per_cu = NULL;
15710
15711 obstack_free (&cu->comp_unit_obstack, NULL);
15712
15713 xfree (cu);
15714}
15715
72bf9492 15716/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
15717 when we're finished with it. We can't free the pointer itself, but be
15718 sure to unlink it from the cache. Also release any associated storage
15719 and perform cache maintenance.
72bf9492
DJ
15720
15721 Only used during partial symbol parsing. */
15722
15723static void
15724free_stack_comp_unit (void *data)
15725{
15726 struct dwarf2_cu *cu = data;
15727
15728 obstack_free (&cu->comp_unit_obstack, NULL);
15729 cu->partial_dies = NULL;
ae038cb0
DJ
15730
15731 if (cu->per_cu != NULL)
15732 {
15733 /* This compilation unit is on the stack in our caller, so we
15734 should not xfree it. Just unlink it. */
15735 cu->per_cu->cu = NULL;
15736 cu->per_cu = NULL;
15737
15738 /* If we had a per-cu pointer, then we may have other compilation
15739 units loaded, so age them now. */
15740 age_cached_comp_units ();
15741 }
15742}
15743
15744/* Free all cached compilation units. */
15745
15746static void
15747free_cached_comp_units (void *data)
15748{
15749 struct dwarf2_per_cu_data *per_cu, **last_chain;
15750
15751 per_cu = dwarf2_per_objfile->read_in_chain;
15752 last_chain = &dwarf2_per_objfile->read_in_chain;
15753 while (per_cu != NULL)
15754 {
15755 struct dwarf2_per_cu_data *next_cu;
15756
15757 next_cu = per_cu->cu->read_in_chain;
15758
15759 free_one_comp_unit (per_cu->cu);
15760 *last_chain = next_cu;
15761
15762 per_cu = next_cu;
15763 }
15764}
15765
15766/* Increase the age counter on each cached compilation unit, and free
15767 any that are too old. */
15768
15769static void
15770age_cached_comp_units (void)
15771{
15772 struct dwarf2_per_cu_data *per_cu, **last_chain;
15773
15774 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15775 per_cu = dwarf2_per_objfile->read_in_chain;
15776 while (per_cu != NULL)
15777 {
15778 per_cu->cu->last_used ++;
15779 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15780 dwarf2_mark (per_cu->cu);
15781 per_cu = per_cu->cu->read_in_chain;
15782 }
15783
15784 per_cu = dwarf2_per_objfile->read_in_chain;
15785 last_chain = &dwarf2_per_objfile->read_in_chain;
15786 while (per_cu != NULL)
15787 {
15788 struct dwarf2_per_cu_data *next_cu;
15789
15790 next_cu = per_cu->cu->read_in_chain;
15791
15792 if (!per_cu->cu->mark)
15793 {
15794 free_one_comp_unit (per_cu->cu);
15795 *last_chain = next_cu;
15796 }
15797 else
15798 last_chain = &per_cu->cu->read_in_chain;
15799
15800 per_cu = next_cu;
15801 }
15802}
15803
15804/* Remove a single compilation unit from the cache. */
15805
15806static void
15807free_one_cached_comp_unit (void *target_cu)
15808{
15809 struct dwarf2_per_cu_data *per_cu, **last_chain;
15810
15811 per_cu = dwarf2_per_objfile->read_in_chain;
15812 last_chain = &dwarf2_per_objfile->read_in_chain;
15813 while (per_cu != NULL)
15814 {
15815 struct dwarf2_per_cu_data *next_cu;
15816
15817 next_cu = per_cu->cu->read_in_chain;
15818
15819 if (per_cu->cu == target_cu)
15820 {
15821 free_one_comp_unit (per_cu->cu);
15822 *last_chain = next_cu;
15823 break;
15824 }
15825 else
15826 last_chain = &per_cu->cu->read_in_chain;
15827
15828 per_cu = next_cu;
15829 }
15830}
15831
fe3e1990
DJ
15832/* Release all extra memory associated with OBJFILE. */
15833
15834void
15835dwarf2_free_objfile (struct objfile *objfile)
15836{
15837 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15838
15839 if (dwarf2_per_objfile == NULL)
15840 return;
15841
15842 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15843 free_cached_comp_units (NULL);
15844
7b9f3c50
DE
15845 if (dwarf2_per_objfile->quick_file_names_table)
15846 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 15847
fe3e1990
DJ
15848 /* Everything else should be on the objfile obstack. */
15849}
15850
1c379e20
DJ
15851/* A pair of DIE offset and GDB type pointer. We store these
15852 in a hash table separate from the DIEs, and preserve them
15853 when the DIEs are flushed out of cache. */
15854
15855struct dwarf2_offset_and_type
15856{
15857 unsigned int offset;
15858 struct type *type;
15859};
15860
15861/* Hash function for a dwarf2_offset_and_type. */
15862
15863static hashval_t
15864offset_and_type_hash (const void *item)
15865{
15866 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 15867
1c379e20
DJ
15868 return ofs->offset;
15869}
15870
15871/* Equality function for a dwarf2_offset_and_type. */
15872
15873static int
15874offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15875{
15876 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15877 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 15878
1c379e20
DJ
15879 return ofs_lhs->offset == ofs_rhs->offset;
15880}
15881
15882/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
15883 table if necessary. For convenience, return TYPE.
15884
15885 The DIEs reading must have careful ordering to:
15886 * Not cause infite loops trying to read in DIEs as a prerequisite for
15887 reading current DIE.
15888 * Not trying to dereference contents of still incompletely read in types
15889 while reading in other DIEs.
15890 * Enable referencing still incompletely read in types just by a pointer to
15891 the type without accessing its fields.
15892
15893 Therefore caller should follow these rules:
15894 * Try to fetch any prerequisite types we may need to build this DIE type
15895 before building the type and calling set_die_type.
e71ec853 15896 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
15897 possible before fetching more types to complete the current type.
15898 * Make the type as complete as possible before fetching more types. */
1c379e20 15899
f792889a 15900static struct type *
1c379e20
DJ
15901set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15902{
15903 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
15904 struct objfile *objfile = cu->objfile;
15905 htab_t *type_hash_ptr;
1c379e20 15906
b4ba55a1
JB
15907 /* For Ada types, make sure that the gnat-specific data is always
15908 initialized (if not already set). There are a few types where
15909 we should not be doing so, because the type-specific area is
15910 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15911 where the type-specific area is used to store the floatformat).
15912 But this is not a problem, because the gnat-specific information
15913 is actually not needed for these types. */
15914 if (need_gnat_info (cu)
15915 && TYPE_CODE (type) != TYPE_CODE_FUNC
15916 && TYPE_CODE (type) != TYPE_CODE_FLT
15917 && !HAVE_GNAT_AUX_INFO (type))
15918 INIT_GNAT_SPECIFIC (type);
15919
8b70b953 15920 if (cu->per_cu->debug_type_section)
673bfd45
DE
15921 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15922 else
15923 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15924
15925 if (*type_hash_ptr == NULL)
f792889a 15926 {
673bfd45
DE
15927 *type_hash_ptr
15928 = htab_create_alloc_ex (127,
f792889a
DJ
15929 offset_and_type_hash,
15930 offset_and_type_eq,
15931 NULL,
673bfd45 15932 &objfile->objfile_obstack,
f792889a
DJ
15933 hashtab_obstack_allocate,
15934 dummy_obstack_deallocate);
f792889a 15935 }
1c379e20
DJ
15936
15937 ofs.offset = die->offset;
15938 ofs.type = type;
15939 slot = (struct dwarf2_offset_and_type **)
673bfd45 15940 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
15941 if (*slot)
15942 complaint (&symfile_complaints,
15943 _("A problem internal to GDB: DIE 0x%x has type already set"),
15944 die->offset);
673bfd45 15945 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 15946 **slot = ofs;
f792889a 15947 return type;
1c379e20
DJ
15948}
15949
673bfd45
DE
15950/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15951 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
15952
15953static struct type *
673bfd45
DE
15954get_die_type_at_offset (unsigned int offset,
15955 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
15956{
15957 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 15958 htab_t type_hash;
f792889a 15959
8b70b953 15960 if (per_cu->debug_type_section)
673bfd45
DE
15961 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15962 else
15963 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
15964 if (type_hash == NULL)
15965 return NULL;
1c379e20 15966
673bfd45 15967 ofs.offset = offset;
1c379e20
DJ
15968 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15969 if (slot)
15970 return slot->type;
15971 else
15972 return NULL;
15973}
15974
673bfd45
DE
15975/* Look up the type for DIE in the appropriate type_hash table,
15976 or return NULL if DIE does not have a saved type. */
15977
15978static struct type *
15979get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15980{
15981 return get_die_type_at_offset (die->offset, cu->per_cu);
15982}
15983
10b3939b
DJ
15984/* Add a dependence relationship from CU to REF_PER_CU. */
15985
15986static void
15987dwarf2_add_dependence (struct dwarf2_cu *cu,
15988 struct dwarf2_per_cu_data *ref_per_cu)
15989{
15990 void **slot;
15991
15992 if (cu->dependencies == NULL)
15993 cu->dependencies
15994 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15995 NULL, &cu->comp_unit_obstack,
15996 hashtab_obstack_allocate,
15997 dummy_obstack_deallocate);
15998
15999 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16000 if (*slot == NULL)
16001 *slot = ref_per_cu;
16002}
1c379e20 16003
f504f079
DE
16004/* Subroutine of dwarf2_mark to pass to htab_traverse.
16005 Set the mark field in every compilation unit in the
ae038cb0
DJ
16006 cache that we must keep because we are keeping CU. */
16007
10b3939b
DJ
16008static int
16009dwarf2_mark_helper (void **slot, void *data)
16010{
16011 struct dwarf2_per_cu_data *per_cu;
16012
16013 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16014
16015 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16016 reading of the chain. As such dependencies remain valid it is not much
16017 useful to track and undo them during QUIT cleanups. */
16018 if (per_cu->cu == NULL)
16019 return 1;
16020
10b3939b
DJ
16021 if (per_cu->cu->mark)
16022 return 1;
16023 per_cu->cu->mark = 1;
16024
16025 if (per_cu->cu->dependencies != NULL)
16026 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16027
16028 return 1;
16029}
16030
f504f079
DE
16031/* Set the mark field in CU and in every other compilation unit in the
16032 cache that we must keep because we are keeping CU. */
16033
ae038cb0
DJ
16034static void
16035dwarf2_mark (struct dwarf2_cu *cu)
16036{
16037 if (cu->mark)
16038 return;
16039 cu->mark = 1;
10b3939b
DJ
16040 if (cu->dependencies != NULL)
16041 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
16042}
16043
16044static void
16045dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16046{
16047 while (per_cu)
16048 {
16049 per_cu->cu->mark = 0;
16050 per_cu = per_cu->cu->read_in_chain;
16051 }
72bf9492
DJ
16052}
16053
72bf9492
DJ
16054/* Trivial hash function for partial_die_info: the hash value of a DIE
16055 is its offset in .debug_info for this objfile. */
16056
16057static hashval_t
16058partial_die_hash (const void *item)
16059{
16060 const struct partial_die_info *part_die = item;
9a619af0 16061
72bf9492
DJ
16062 return part_die->offset;
16063}
16064
16065/* Trivial comparison function for partial_die_info structures: two DIEs
16066 are equal if they have the same offset. */
16067
16068static int
16069partial_die_eq (const void *item_lhs, const void *item_rhs)
16070{
16071 const struct partial_die_info *part_die_lhs = item_lhs;
16072 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 16073
72bf9492
DJ
16074 return part_die_lhs->offset == part_die_rhs->offset;
16075}
16076
ae038cb0
DJ
16077static struct cmd_list_element *set_dwarf2_cmdlist;
16078static struct cmd_list_element *show_dwarf2_cmdlist;
16079
16080static void
16081set_dwarf2_cmd (char *args, int from_tty)
16082{
16083 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16084}
16085
16086static void
16087show_dwarf2_cmd (char *args, int from_tty)
6e70227d 16088{
ae038cb0
DJ
16089 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16090}
16091
dce234bc
PP
16092/* If section described by INFO was mmapped, munmap it now. */
16093
16094static void
16095munmap_section_buffer (struct dwarf2_section_info *info)
16096{
b315ab21 16097 if (info->map_addr != NULL)
dce234bc
PP
16098 {
16099#ifdef HAVE_MMAP
b315ab21 16100 int res;
9a619af0 16101
b315ab21
TG
16102 res = munmap (info->map_addr, info->map_len);
16103 gdb_assert (res == 0);
dce234bc
PP
16104#else
16105 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 16106 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
16107#endif
16108 }
16109}
16110
16111/* munmap debug sections for OBJFILE, if necessary. */
16112
16113static void
c1bd65d0 16114dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
16115{
16116 struct dwarf2_per_objfile *data = d;
8b70b953
TT
16117 int ix;
16118 struct dwarf2_section_info *section;
9a619af0 16119
16be1145
DE
16120 /* This is sorted according to the order they're defined in to make it easier
16121 to keep in sync. */
dce234bc
PP
16122 munmap_section_buffer (&data->info);
16123 munmap_section_buffer (&data->abbrev);
16124 munmap_section_buffer (&data->line);
16be1145 16125 munmap_section_buffer (&data->loc);
dce234bc 16126 munmap_section_buffer (&data->macinfo);
cf2c3c16 16127 munmap_section_buffer (&data->macro);
16be1145 16128 munmap_section_buffer (&data->str);
dce234bc 16129 munmap_section_buffer (&data->ranges);
dce234bc
PP
16130 munmap_section_buffer (&data->frame);
16131 munmap_section_buffer (&data->eh_frame);
9291a0cd 16132 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
16133
16134 for (ix = 0;
16135 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16136 ++ix)
16137 munmap_section_buffer (section);
16138
16139 VEC_free (dwarf2_section_info_def, data->types);
9291a0cd
TT
16140}
16141
16142\f
ae2de4f8 16143/* The "save gdb-index" command. */
9291a0cd
TT
16144
16145/* The contents of the hash table we create when building the string
16146 table. */
16147struct strtab_entry
16148{
16149 offset_type offset;
16150 const char *str;
16151};
16152
559a7a62
JK
16153/* Hash function for a strtab_entry.
16154
16155 Function is used only during write_hash_table so no index format backward
16156 compatibility is needed. */
b89be57b 16157
9291a0cd
TT
16158static hashval_t
16159hash_strtab_entry (const void *e)
16160{
16161 const struct strtab_entry *entry = e;
559a7a62 16162 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
16163}
16164
16165/* Equality function for a strtab_entry. */
b89be57b 16166
9291a0cd
TT
16167static int
16168eq_strtab_entry (const void *a, const void *b)
16169{
16170 const struct strtab_entry *ea = a;
16171 const struct strtab_entry *eb = b;
16172 return !strcmp (ea->str, eb->str);
16173}
16174
16175/* Create a strtab_entry hash table. */
b89be57b 16176
9291a0cd
TT
16177static htab_t
16178create_strtab (void)
16179{
16180 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16181 xfree, xcalloc, xfree);
16182}
16183
16184/* Add a string to the constant pool. Return the string's offset in
16185 host order. */
b89be57b 16186
9291a0cd
TT
16187static offset_type
16188add_string (htab_t table, struct obstack *cpool, const char *str)
16189{
16190 void **slot;
16191 struct strtab_entry entry;
16192 struct strtab_entry *result;
16193
16194 entry.str = str;
16195 slot = htab_find_slot (table, &entry, INSERT);
16196 if (*slot)
16197 result = *slot;
16198 else
16199 {
16200 result = XNEW (struct strtab_entry);
16201 result->offset = obstack_object_size (cpool);
16202 result->str = str;
16203 obstack_grow_str0 (cpool, str);
16204 *slot = result;
16205 }
16206 return result->offset;
16207}
16208
16209/* An entry in the symbol table. */
16210struct symtab_index_entry
16211{
16212 /* The name of the symbol. */
16213 const char *name;
16214 /* The offset of the name in the constant pool. */
16215 offset_type index_offset;
16216 /* A sorted vector of the indices of all the CUs that hold an object
16217 of this name. */
16218 VEC (offset_type) *cu_indices;
16219};
16220
16221/* The symbol table. This is a power-of-2-sized hash table. */
16222struct mapped_symtab
16223{
16224 offset_type n_elements;
16225 offset_type size;
16226 struct symtab_index_entry **data;
16227};
16228
16229/* Hash function for a symtab_index_entry. */
b89be57b 16230
9291a0cd
TT
16231static hashval_t
16232hash_symtab_entry (const void *e)
16233{
16234 const struct symtab_index_entry *entry = e;
16235 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16236 sizeof (offset_type) * VEC_length (offset_type,
16237 entry->cu_indices),
16238 0);
16239}
16240
16241/* Equality function for a symtab_index_entry. */
b89be57b 16242
9291a0cd
TT
16243static int
16244eq_symtab_entry (const void *a, const void *b)
16245{
16246 const struct symtab_index_entry *ea = a;
16247 const struct symtab_index_entry *eb = b;
16248 int len = VEC_length (offset_type, ea->cu_indices);
16249 if (len != VEC_length (offset_type, eb->cu_indices))
16250 return 0;
16251 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16252 VEC_address (offset_type, eb->cu_indices),
16253 sizeof (offset_type) * len);
16254}
16255
16256/* Destroy a symtab_index_entry. */
b89be57b 16257
9291a0cd
TT
16258static void
16259delete_symtab_entry (void *p)
16260{
16261 struct symtab_index_entry *entry = p;
16262 VEC_free (offset_type, entry->cu_indices);
16263 xfree (entry);
16264}
16265
16266/* Create a hash table holding symtab_index_entry objects. */
b89be57b 16267
9291a0cd 16268static htab_t
3876f04e 16269create_symbol_hash_table (void)
9291a0cd
TT
16270{
16271 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16272 delete_symtab_entry, xcalloc, xfree);
16273}
16274
16275/* Create a new mapped symtab object. */
b89be57b 16276
9291a0cd
TT
16277static struct mapped_symtab *
16278create_mapped_symtab (void)
16279{
16280 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16281 symtab->n_elements = 0;
16282 symtab->size = 1024;
16283 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16284 return symtab;
16285}
16286
16287/* Destroy a mapped_symtab. */
b89be57b 16288
9291a0cd
TT
16289static void
16290cleanup_mapped_symtab (void *p)
16291{
16292 struct mapped_symtab *symtab = p;
16293 /* The contents of the array are freed when the other hash table is
16294 destroyed. */
16295 xfree (symtab->data);
16296 xfree (symtab);
16297}
16298
16299/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
16300 the slot.
16301
16302 Function is used only during write_hash_table so no index format backward
16303 compatibility is needed. */
b89be57b 16304
9291a0cd
TT
16305static struct symtab_index_entry **
16306find_slot (struct mapped_symtab *symtab, const char *name)
16307{
559a7a62 16308 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
16309
16310 index = hash & (symtab->size - 1);
16311 step = ((hash * 17) & (symtab->size - 1)) | 1;
16312
16313 for (;;)
16314 {
16315 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16316 return &symtab->data[index];
16317 index = (index + step) & (symtab->size - 1);
16318 }
16319}
16320
16321/* Expand SYMTAB's hash table. */
b89be57b 16322
9291a0cd
TT
16323static void
16324hash_expand (struct mapped_symtab *symtab)
16325{
16326 offset_type old_size = symtab->size;
16327 offset_type i;
16328 struct symtab_index_entry **old_entries = symtab->data;
16329
16330 symtab->size *= 2;
16331 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16332
16333 for (i = 0; i < old_size; ++i)
16334 {
16335 if (old_entries[i])
16336 {
16337 struct symtab_index_entry **slot = find_slot (symtab,
16338 old_entries[i]->name);
16339 *slot = old_entries[i];
16340 }
16341 }
16342
16343 xfree (old_entries);
16344}
16345
16346/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16347 is the index of the CU in which the symbol appears. */
b89be57b 16348
9291a0cd
TT
16349static void
16350add_index_entry (struct mapped_symtab *symtab, const char *name,
16351 offset_type cu_index)
16352{
16353 struct symtab_index_entry **slot;
16354
16355 ++symtab->n_elements;
16356 if (4 * symtab->n_elements / 3 >= symtab->size)
16357 hash_expand (symtab);
16358
16359 slot = find_slot (symtab, name);
16360 if (!*slot)
16361 {
16362 *slot = XNEW (struct symtab_index_entry);
16363 (*slot)->name = name;
16364 (*slot)->cu_indices = NULL;
16365 }
16366 /* Don't push an index twice. Due to how we add entries we only
16367 have to check the last one. */
16368 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 16369 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
16370 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16371}
16372
16373/* Add a vector of indices to the constant pool. */
b89be57b 16374
9291a0cd 16375static offset_type
3876f04e 16376add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
16377 struct symtab_index_entry *entry)
16378{
16379 void **slot;
16380
3876f04e 16381 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
16382 if (!*slot)
16383 {
16384 offset_type len = VEC_length (offset_type, entry->cu_indices);
16385 offset_type val = MAYBE_SWAP (len);
16386 offset_type iter;
16387 int i;
16388
16389 *slot = entry;
16390 entry->index_offset = obstack_object_size (cpool);
16391
16392 obstack_grow (cpool, &val, sizeof (val));
16393 for (i = 0;
16394 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16395 ++i)
16396 {
16397 val = MAYBE_SWAP (iter);
16398 obstack_grow (cpool, &val, sizeof (val));
16399 }
16400 }
16401 else
16402 {
16403 struct symtab_index_entry *old_entry = *slot;
16404 entry->index_offset = old_entry->index_offset;
16405 entry = old_entry;
16406 }
16407 return entry->index_offset;
16408}
16409
16410/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16411 constant pool entries going into the obstack CPOOL. */
b89be57b 16412
9291a0cd
TT
16413static void
16414write_hash_table (struct mapped_symtab *symtab,
16415 struct obstack *output, struct obstack *cpool)
16416{
16417 offset_type i;
3876f04e 16418 htab_t symbol_hash_table;
9291a0cd
TT
16419 htab_t str_table;
16420
3876f04e 16421 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 16422 str_table = create_strtab ();
3876f04e 16423
9291a0cd
TT
16424 /* We add all the index vectors to the constant pool first, to
16425 ensure alignment is ok. */
16426 for (i = 0; i < symtab->size; ++i)
16427 {
16428 if (symtab->data[i])
3876f04e 16429 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
16430 }
16431
16432 /* Now write out the hash table. */
16433 for (i = 0; i < symtab->size; ++i)
16434 {
16435 offset_type str_off, vec_off;
16436
16437 if (symtab->data[i])
16438 {
16439 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16440 vec_off = symtab->data[i]->index_offset;
16441 }
16442 else
16443 {
16444 /* While 0 is a valid constant pool index, it is not valid
16445 to have 0 for both offsets. */
16446 str_off = 0;
16447 vec_off = 0;
16448 }
16449
16450 str_off = MAYBE_SWAP (str_off);
16451 vec_off = MAYBE_SWAP (vec_off);
16452
16453 obstack_grow (output, &str_off, sizeof (str_off));
16454 obstack_grow (output, &vec_off, sizeof (vec_off));
16455 }
16456
16457 htab_delete (str_table);
3876f04e 16458 htab_delete (symbol_hash_table);
9291a0cd
TT
16459}
16460
0a5429f6
DE
16461/* Struct to map psymtab to CU index in the index file. */
16462struct psymtab_cu_index_map
16463{
16464 struct partial_symtab *psymtab;
16465 unsigned int cu_index;
16466};
16467
16468static hashval_t
16469hash_psymtab_cu_index (const void *item)
16470{
16471 const struct psymtab_cu_index_map *map = item;
16472
16473 return htab_hash_pointer (map->psymtab);
16474}
16475
16476static int
16477eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16478{
16479 const struct psymtab_cu_index_map *lhs = item_lhs;
16480 const struct psymtab_cu_index_map *rhs = item_rhs;
16481
16482 return lhs->psymtab == rhs->psymtab;
16483}
16484
16485/* Helper struct for building the address table. */
16486struct addrmap_index_data
16487{
16488 struct objfile *objfile;
16489 struct obstack *addr_obstack;
16490 htab_t cu_index_htab;
16491
16492 /* Non-zero if the previous_* fields are valid.
16493 We can't write an entry until we see the next entry (since it is only then
16494 that we know the end of the entry). */
16495 int previous_valid;
16496 /* Index of the CU in the table of all CUs in the index file. */
16497 unsigned int previous_cu_index;
0963b4bd 16498 /* Start address of the CU. */
0a5429f6
DE
16499 CORE_ADDR previous_cu_start;
16500};
16501
16502/* Write an address entry to OBSTACK. */
b89be57b 16503
9291a0cd 16504static void
0a5429f6
DE
16505add_address_entry (struct objfile *objfile, struct obstack *obstack,
16506 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 16507{
0a5429f6 16508 offset_type cu_index_to_write;
9291a0cd
TT
16509 char addr[8];
16510 CORE_ADDR baseaddr;
16511
16512 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16513
0a5429f6
DE
16514 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16515 obstack_grow (obstack, addr, 8);
16516 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16517 obstack_grow (obstack, addr, 8);
16518 cu_index_to_write = MAYBE_SWAP (cu_index);
16519 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16520}
16521
16522/* Worker function for traversing an addrmap to build the address table. */
16523
16524static int
16525add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16526{
16527 struct addrmap_index_data *data = datap;
16528 struct partial_symtab *pst = obj;
16529 offset_type cu_index;
16530 void **slot;
16531
16532 if (data->previous_valid)
16533 add_address_entry (data->objfile, data->addr_obstack,
16534 data->previous_cu_start, start_addr,
16535 data->previous_cu_index);
16536
16537 data->previous_cu_start = start_addr;
16538 if (pst != NULL)
16539 {
16540 struct psymtab_cu_index_map find_map, *map;
16541 find_map.psymtab = pst;
16542 map = htab_find (data->cu_index_htab, &find_map);
16543 gdb_assert (map != NULL);
16544 data->previous_cu_index = map->cu_index;
16545 data->previous_valid = 1;
16546 }
16547 else
16548 data->previous_valid = 0;
16549
16550 return 0;
16551}
16552
16553/* Write OBJFILE's address map to OBSTACK.
16554 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16555 in the index file. */
16556
16557static void
16558write_address_map (struct objfile *objfile, struct obstack *obstack,
16559 htab_t cu_index_htab)
16560{
16561 struct addrmap_index_data addrmap_index_data;
16562
16563 /* When writing the address table, we have to cope with the fact that
16564 the addrmap iterator only provides the start of a region; we have to
16565 wait until the next invocation to get the start of the next region. */
16566
16567 addrmap_index_data.objfile = objfile;
16568 addrmap_index_data.addr_obstack = obstack;
16569 addrmap_index_data.cu_index_htab = cu_index_htab;
16570 addrmap_index_data.previous_valid = 0;
16571
16572 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16573 &addrmap_index_data);
16574
16575 /* It's highly unlikely the last entry (end address = 0xff...ff)
16576 is valid, but we should still handle it.
16577 The end address is recorded as the start of the next region, but that
16578 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16579 anyway. */
16580 if (addrmap_index_data.previous_valid)
16581 add_address_entry (objfile, obstack,
16582 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16583 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16584}
16585
16586/* Add a list of partial symbols to SYMTAB. */
b89be57b 16587
9291a0cd
TT
16588static void
16589write_psymbols (struct mapped_symtab *symtab,
987d643c 16590 htab_t psyms_seen,
9291a0cd
TT
16591 struct partial_symbol **psymp,
16592 int count,
987d643c
TT
16593 offset_type cu_index,
16594 int is_static)
9291a0cd
TT
16595{
16596 for (; count-- > 0; ++psymp)
16597 {
987d643c
TT
16598 void **slot, *lookup;
16599
9291a0cd
TT
16600 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16601 error (_("Ada is not currently supported by the index"));
987d643c
TT
16602
16603 /* We only want to add a given psymbol once. However, we also
16604 want to account for whether it is global or static. So, we
16605 may add it twice, using slightly different values. */
16606 if (is_static)
16607 {
16608 uintptr_t val = 1 | (uintptr_t) *psymp;
16609
16610 lookup = (void *) val;
16611 }
16612 else
16613 lookup = *psymp;
16614
16615 /* Only add a given psymbol once. */
16616 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16617 if (!*slot)
16618 {
16619 *slot = lookup;
16620 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
16621 }
9291a0cd
TT
16622 }
16623}
16624
16625/* Write the contents of an ("unfinished") obstack to FILE. Throw an
16626 exception if there is an error. */
b89be57b 16627
9291a0cd
TT
16628static void
16629write_obstack (FILE *file, struct obstack *obstack)
16630{
16631 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16632 file)
16633 != obstack_object_size (obstack))
16634 error (_("couldn't data write to file"));
16635}
16636
16637/* Unlink a file if the argument is not NULL. */
b89be57b 16638
9291a0cd
TT
16639static void
16640unlink_if_set (void *p)
16641{
16642 char **filename = p;
16643 if (*filename)
16644 unlink (*filename);
16645}
16646
1fd400ff
TT
16647/* A helper struct used when iterating over debug_types. */
16648struct signatured_type_index_data
16649{
16650 struct objfile *objfile;
16651 struct mapped_symtab *symtab;
16652 struct obstack *types_list;
987d643c 16653 htab_t psyms_seen;
1fd400ff
TT
16654 int cu_index;
16655};
16656
16657/* A helper function that writes a single signatured_type to an
16658 obstack. */
b89be57b 16659
1fd400ff
TT
16660static int
16661write_one_signatured_type (void **slot, void *d)
16662{
16663 struct signatured_type_index_data *info = d;
16664 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
16665 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16666 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
16667 gdb_byte val[8];
16668
16669 write_psymbols (info->symtab,
987d643c 16670 info->psyms_seen,
3e43a32a
MS
16671 info->objfile->global_psymbols.list
16672 + psymtab->globals_offset,
987d643c
TT
16673 psymtab->n_global_syms, info->cu_index,
16674 0);
1fd400ff 16675 write_psymbols (info->symtab,
987d643c 16676 info->psyms_seen,
3e43a32a
MS
16677 info->objfile->static_psymbols.list
16678 + psymtab->statics_offset,
987d643c
TT
16679 psymtab->n_static_syms, info->cu_index,
16680 1);
1fd400ff 16681
b3c8eb43 16682 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
1fd400ff
TT
16683 obstack_grow (info->types_list, val, 8);
16684 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16685 obstack_grow (info->types_list, val, 8);
16686 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16687 obstack_grow (info->types_list, val, 8);
16688
16689 ++info->cu_index;
16690
16691 return 1;
16692}
16693
9291a0cd 16694/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16695
9291a0cd
TT
16696static void
16697write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16698{
16699 struct cleanup *cleanup;
16700 char *filename, *cleanup_filename;
1fd400ff
TT
16701 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16702 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16703 int i;
16704 FILE *out_file;
16705 struct mapped_symtab *symtab;
16706 offset_type val, size_of_contents, total_len;
16707 struct stat st;
16708 char buf[8];
987d643c 16709 htab_t psyms_seen;
0a5429f6
DE
16710 htab_t cu_index_htab;
16711 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 16712
b4f2f049 16713 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 16714 return;
b4f2f049 16715
9291a0cd
TT
16716 if (dwarf2_per_objfile->using_index)
16717 error (_("Cannot use an index to create the index"));
16718
8b70b953
TT
16719 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16720 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16721
9291a0cd 16722 if (stat (objfile->name, &st) < 0)
7e17e088 16723 perror_with_name (objfile->name);
9291a0cd
TT
16724
16725 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16726 INDEX_SUFFIX, (char *) NULL);
16727 cleanup = make_cleanup (xfree, filename);
16728
16729 out_file = fopen (filename, "wb");
16730 if (!out_file)
16731 error (_("Can't open `%s' for writing"), filename);
16732
16733 cleanup_filename = filename;
16734 make_cleanup (unlink_if_set, &cleanup_filename);
16735
16736 symtab = create_mapped_symtab ();
16737 make_cleanup (cleanup_mapped_symtab, symtab);
16738
16739 obstack_init (&addr_obstack);
16740 make_cleanup_obstack_free (&addr_obstack);
16741
16742 obstack_init (&cu_list);
16743 make_cleanup_obstack_free (&cu_list);
16744
1fd400ff
TT
16745 obstack_init (&types_cu_list);
16746 make_cleanup_obstack_free (&types_cu_list);
16747
987d643c
TT
16748 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16749 NULL, xcalloc, xfree);
96408a79 16750 make_cleanup_htab_delete (psyms_seen);
987d643c 16751
0a5429f6
DE
16752 /* While we're scanning CU's create a table that maps a psymtab pointer
16753 (which is what addrmap records) to its index (which is what is recorded
16754 in the index file). This will later be needed to write the address
16755 table. */
16756 cu_index_htab = htab_create_alloc (100,
16757 hash_psymtab_cu_index,
16758 eq_psymtab_cu_index,
16759 NULL, xcalloc, xfree);
96408a79 16760 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
16761 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16762 xmalloc (sizeof (struct psymtab_cu_index_map)
16763 * dwarf2_per_objfile->n_comp_units);
16764 make_cleanup (xfree, psymtab_cu_index_map);
16765
16766 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
16767 work here. Also, the debug_types entries do not appear in
16768 all_comp_units, but only in their own hash table. */
9291a0cd
TT
16769 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16770 {
3e43a32a
MS
16771 struct dwarf2_per_cu_data *per_cu
16772 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 16773 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 16774 gdb_byte val[8];
0a5429f6
DE
16775 struct psymtab_cu_index_map *map;
16776 void **slot;
9291a0cd
TT
16777
16778 write_psymbols (symtab,
987d643c 16779 psyms_seen,
9291a0cd 16780 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
16781 psymtab->n_global_syms, i,
16782 0);
9291a0cd 16783 write_psymbols (symtab,
987d643c 16784 psyms_seen,
9291a0cd 16785 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
16786 psymtab->n_static_syms, i,
16787 1);
9291a0cd 16788
0a5429f6
DE
16789 map = &psymtab_cu_index_map[i];
16790 map->psymtab = psymtab;
16791 map->cu_index = i;
16792 slot = htab_find_slot (cu_index_htab, map, INSERT);
16793 gdb_assert (slot != NULL);
16794 gdb_assert (*slot == NULL);
16795 *slot = map;
9291a0cd 16796
e254ef6a 16797 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 16798 obstack_grow (&cu_list, val, 8);
e254ef6a 16799 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
16800 obstack_grow (&cu_list, val, 8);
16801 }
16802
0a5429f6
DE
16803 /* Dump the address map. */
16804 write_address_map (objfile, &addr_obstack, cu_index_htab);
16805
1fd400ff
TT
16806 /* Write out the .debug_type entries, if any. */
16807 if (dwarf2_per_objfile->signatured_types)
16808 {
16809 struct signatured_type_index_data sig_data;
16810
16811 sig_data.objfile = objfile;
16812 sig_data.symtab = symtab;
16813 sig_data.types_list = &types_cu_list;
987d643c 16814 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
16815 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16816 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16817 write_one_signatured_type, &sig_data);
16818 }
16819
9291a0cd
TT
16820 obstack_init (&constant_pool);
16821 make_cleanup_obstack_free (&constant_pool);
16822 obstack_init (&symtab_obstack);
16823 make_cleanup_obstack_free (&symtab_obstack);
16824 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16825
16826 obstack_init (&contents);
16827 make_cleanup_obstack_free (&contents);
1fd400ff 16828 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
16829 total_len = size_of_contents;
16830
16831 /* The version number. */
559a7a62 16832 val = MAYBE_SWAP (5);
9291a0cd
TT
16833 obstack_grow (&contents, &val, sizeof (val));
16834
16835 /* The offset of the CU list from the start of the file. */
16836 val = MAYBE_SWAP (total_len);
16837 obstack_grow (&contents, &val, sizeof (val));
16838 total_len += obstack_object_size (&cu_list);
16839
1fd400ff
TT
16840 /* The offset of the types CU list from the start of the file. */
16841 val = MAYBE_SWAP (total_len);
16842 obstack_grow (&contents, &val, sizeof (val));
16843 total_len += obstack_object_size (&types_cu_list);
16844
9291a0cd
TT
16845 /* The offset of the address table from the start of the file. */
16846 val = MAYBE_SWAP (total_len);
16847 obstack_grow (&contents, &val, sizeof (val));
16848 total_len += obstack_object_size (&addr_obstack);
16849
16850 /* The offset of the symbol table from the start of the file. */
16851 val = MAYBE_SWAP (total_len);
16852 obstack_grow (&contents, &val, sizeof (val));
16853 total_len += obstack_object_size (&symtab_obstack);
16854
16855 /* The offset of the constant pool from the start of the file. */
16856 val = MAYBE_SWAP (total_len);
16857 obstack_grow (&contents, &val, sizeof (val));
16858 total_len += obstack_object_size (&constant_pool);
16859
16860 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16861
16862 write_obstack (out_file, &contents);
16863 write_obstack (out_file, &cu_list);
1fd400ff 16864 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
16865 write_obstack (out_file, &addr_obstack);
16866 write_obstack (out_file, &symtab_obstack);
16867 write_obstack (out_file, &constant_pool);
16868
16869 fclose (out_file);
16870
16871 /* We want to keep the file, so we set cleanup_filename to NULL
16872 here. See unlink_if_set. */
16873 cleanup_filename = NULL;
16874
16875 do_cleanups (cleanup);
16876}
16877
90476074
TT
16878/* Implementation of the `save gdb-index' command.
16879
16880 Note that the file format used by this command is documented in the
16881 GDB manual. Any changes here must be documented there. */
11570e71 16882
9291a0cd
TT
16883static void
16884save_gdb_index_command (char *arg, int from_tty)
16885{
16886 struct objfile *objfile;
16887
16888 if (!arg || !*arg)
96d19272 16889 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
16890
16891 ALL_OBJFILES (objfile)
16892 {
16893 struct stat st;
16894
16895 /* If the objfile does not correspond to an actual file, skip it. */
16896 if (stat (objfile->name, &st) < 0)
16897 continue;
16898
16899 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16900 if (dwarf2_per_objfile)
16901 {
16902 volatile struct gdb_exception except;
16903
16904 TRY_CATCH (except, RETURN_MASK_ERROR)
16905 {
16906 write_psymtabs_to_index (objfile, arg);
16907 }
16908 if (except.reason < 0)
16909 exception_fprintf (gdb_stderr, except,
16910 _("Error while writing index for `%s': "),
16911 objfile->name);
16912 }
16913 }
dce234bc
PP
16914}
16915
9291a0cd
TT
16916\f
16917
9eae7c52
TT
16918int dwarf2_always_disassemble;
16919
16920static void
16921show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16922 struct cmd_list_element *c, const char *value)
16923{
3e43a32a
MS
16924 fprintf_filtered (file,
16925 _("Whether to always disassemble "
16926 "DWARF expressions is %s.\n"),
9eae7c52
TT
16927 value);
16928}
16929
900e11f9
JK
16930static void
16931show_check_physname (struct ui_file *file, int from_tty,
16932 struct cmd_list_element *c, const char *value)
16933{
16934 fprintf_filtered (file,
16935 _("Whether to check \"physname\" is %s.\n"),
16936 value);
16937}
16938
6502dd73
DJ
16939void _initialize_dwarf2_read (void);
16940
16941void
16942_initialize_dwarf2_read (void)
16943{
96d19272
JK
16944 struct cmd_list_element *c;
16945
dce234bc 16946 dwarf2_objfile_data_key
c1bd65d0 16947 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 16948
1bedd215
AC
16949 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16950Set DWARF 2 specific variables.\n\
16951Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16952 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16953 0/*allow-unknown*/, &maintenance_set_cmdlist);
16954
1bedd215
AC
16955 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16956Show DWARF 2 specific variables\n\
16957Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16958 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16959 0/*allow-unknown*/, &maintenance_show_cmdlist);
16960
16961 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
16962 &dwarf2_max_cache_age, _("\
16963Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16964Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16965A higher limit means that cached compilation units will be stored\n\
16966in memory longer, and more total memory will be used. Zero disables\n\
16967caching, which can slow down startup."),
2c5b56ce 16968 NULL,
920d2a44 16969 show_dwarf2_max_cache_age,
2c5b56ce 16970 &set_dwarf2_cmdlist,
ae038cb0 16971 &show_dwarf2_cmdlist);
d97bc12b 16972
9eae7c52
TT
16973 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16974 &dwarf2_always_disassemble, _("\
16975Set whether `info address' always disassembles DWARF expressions."), _("\
16976Show whether `info address' always disassembles DWARF expressions."), _("\
16977When enabled, DWARF expressions are always printed in an assembly-like\n\
16978syntax. When disabled, expressions will be printed in a more\n\
16979conversational style, when possible."),
16980 NULL,
16981 show_dwarf2_always_disassemble,
16982 &set_dwarf2_cmdlist,
16983 &show_dwarf2_cmdlist);
16984
d97bc12b
DE
16985 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16986Set debugging of the dwarf2 DIE reader."), _("\
16987Show debugging of the dwarf2 DIE reader."), _("\
16988When enabled (non-zero), DIEs are dumped after they are read in.\n\
16989The value is the maximum depth to print."),
16990 NULL,
16991 NULL,
16992 &setdebuglist, &showdebuglist);
9291a0cd 16993
900e11f9
JK
16994 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
16995Set cross-checking of \"physname\" code against demangler."), _("\
16996Show cross-checking of \"physname\" code against demangler."), _("\
16997When enabled, GDB's internal \"physname\" code is checked against\n\
16998the demangler."),
16999 NULL, show_check_physname,
17000 &setdebuglist, &showdebuglist);
17001
96d19272 17002 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 17003 _("\
fc1a9d6e 17004Save a gdb-index file.\n\
11570e71 17005Usage: save gdb-index DIRECTORY"),
96d19272
JK
17006 &save_cmdlist);
17007 set_cmd_completer (c, filename_completer);
6502dd73 17008}