PR ld/12975
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
6aba47ca 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
7b6bb8da 4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
0fb0cc75 5 Free Software Foundation, Inc.
c906108c
SS
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 12 support.
c906108c 13
c5aa993b 14 This file is part of GDB.
c906108c 15
c5aa993b
JM
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
a9762ec7
JB
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
c906108c 20
a9762ec7
JB
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
c906108c 25
c5aa993b 26 You should have received a copy of the GNU General Public License
a9762ec7 27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
28
29#include "defs.h"
30#include "bfd.h"
c906108c
SS
31#include "symtab.h"
32#include "gdbtypes.h"
c906108c 33#include "objfiles.h"
fa8f86ff 34#include "dwarf2.h"
c906108c
SS
35#include "buildsym.h"
36#include "demangle.h"
37#include "expression.h"
d5166ae1 38#include "filenames.h" /* for DOSish file names */
2e276125 39#include "macrotab.h"
c906108c
SS
40#include "language.h"
41#include "complaints.h"
357e46e7 42#include "bcache.h"
4c2df51b
DJ
43#include "dwarf2expr.h"
44#include "dwarf2loc.h"
9219021c 45#include "cp-support.h"
72bf9492 46#include "hashtab.h"
ae038cb0
DJ
47#include "command.h"
48#include "gdbcmd.h"
edb3359d 49#include "block.h"
ff013f42 50#include "addrmap.h"
94af9270
KS
51#include "typeprint.h"
52#include "jv-lang.h"
ccefe4c4 53#include "psympriv.h"
9291a0cd
TT
54#include "exceptions.h"
55#include "gdb_stat.h"
96d19272 56#include "completer.h"
34eaf542 57#include "vec.h"
98bfdba5
PA
58#include "c-lang.h"
59#include "valprint.h"
60d5a603 60#include <ctype.h>
4c2df51b 61
c906108c
SS
62#include <fcntl.h>
63#include "gdb_string.h"
4bdf3d34 64#include "gdb_assert.h"
c906108c 65#include <sys/types.h>
233a11ab
CS
66#ifdef HAVE_ZLIB_H
67#include <zlib.h>
68#endif
dce234bc
PP
69#ifdef HAVE_MMAP
70#include <sys/mman.h>
85d9bd0e
TT
71#ifndef MAP_FAILED
72#define MAP_FAILED ((void *) -1)
73#endif
dce234bc 74#endif
d8151005 75
34eaf542
TT
76typedef struct symbol *symbolp;
77DEF_VEC_P (symbolp);
78
107d2387 79#if 0
357e46e7 80/* .debug_info header for a compilation unit
c906108c
SS
81 Because of alignment constraints, this structure has padding and cannot
82 be mapped directly onto the beginning of the .debug_info section. */
83typedef struct comp_unit_header
84 {
85 unsigned int length; /* length of the .debug_info
86 contribution */
87 unsigned short version; /* version number -- 2 for DWARF
88 version 2 */
89 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
90 unsigned char addr_size; /* byte size of an address -- 4 */
91 }
92_COMP_UNIT_HEADER;
93#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 94#endif
c906108c 95
c906108c
SS
96/* .debug_line statement program prologue
97 Because of alignment constraints, this structure has padding and cannot
98 be mapped directly onto the beginning of the .debug_info section. */
99typedef struct statement_prologue
100 {
101 unsigned int total_length; /* byte length of the statement
102 information */
103 unsigned short version; /* version number -- 2 for DWARF
104 version 2 */
105 unsigned int prologue_length; /* # bytes between prologue &
106 stmt program */
107 unsigned char minimum_instruction_length; /* byte size of
108 smallest instr */
109 unsigned char default_is_stmt; /* initial value of is_stmt
110 register */
111 char line_base;
112 unsigned char line_range;
113 unsigned char opcode_base; /* number assigned to first special
114 opcode */
115 unsigned char *standard_opcode_lengths;
116 }
117_STATEMENT_PROLOGUE;
118
d97bc12b
DE
119/* When non-zero, dump DIEs after they are read in. */
120static int dwarf2_die_debug = 0;
121
900e11f9
JK
122/* When non-zero, cross-check physname against demangler. */
123static int check_physname = 0;
124
dce234bc
PP
125static int pagesize;
126
df8a16a1
DJ
127/* When set, the file that we're processing is known to have debugging
128 info for C++ namespaces. GCC 3.3.x did not produce this information,
129 but later versions do. */
130
131static int processing_has_namespace_info;
132
6502dd73
DJ
133static const struct objfile_data *dwarf2_objfile_data_key;
134
dce234bc
PP
135struct dwarf2_section_info
136{
137 asection *asection;
138 gdb_byte *buffer;
139 bfd_size_type size;
b315ab21
TG
140 /* Not NULL if the section was actually mmapped. */
141 void *map_addr;
142 /* Page aligned size of mmapped area. */
143 bfd_size_type map_len;
be391dca
TT
144 /* True if we have tried to read this section. */
145 int readin;
dce234bc
PP
146};
147
8b70b953
TT
148typedef struct dwarf2_section_info dwarf2_section_info_def;
149DEF_VEC_O (dwarf2_section_info_def);
150
9291a0cd
TT
151/* All offsets in the index are of this type. It must be
152 architecture-independent. */
153typedef uint32_t offset_type;
154
155DEF_VEC_I (offset_type);
156
157/* A description of the mapped index. The file format is described in
158 a comment by the code that writes the index. */
159struct mapped_index
160{
559a7a62
JK
161 /* Index data format version. */
162 int version;
163
9291a0cd
TT
164 /* The total length of the buffer. */
165 off_t total_size;
b11b1f88 166
9291a0cd
TT
167 /* A pointer to the address table data. */
168 const gdb_byte *address_table;
b11b1f88 169
9291a0cd
TT
170 /* Size of the address table data in bytes. */
171 offset_type address_table_size;
b11b1f88 172
3876f04e
DE
173 /* The symbol table, implemented as a hash table. */
174 const offset_type *symbol_table;
b11b1f88 175
9291a0cd 176 /* Size in slots, each slot is 2 offset_types. */
3876f04e 177 offset_type symbol_table_slots;
b11b1f88 178
9291a0cd
TT
179 /* A pointer to the constant pool. */
180 const char *constant_pool;
181};
182
6502dd73
DJ
183struct dwarf2_per_objfile
184{
dce234bc
PP
185 struct dwarf2_section_info info;
186 struct dwarf2_section_info abbrev;
187 struct dwarf2_section_info line;
dce234bc
PP
188 struct dwarf2_section_info loc;
189 struct dwarf2_section_info macinfo;
cf2c3c16 190 struct dwarf2_section_info macro;
dce234bc
PP
191 struct dwarf2_section_info str;
192 struct dwarf2_section_info ranges;
193 struct dwarf2_section_info frame;
194 struct dwarf2_section_info eh_frame;
9291a0cd 195 struct dwarf2_section_info gdb_index;
ae038cb0 196
8b70b953
TT
197 VEC (dwarf2_section_info_def) *types;
198
be391dca
TT
199 /* Back link. */
200 struct objfile *objfile;
201
10b3939b
DJ
202 /* A list of all the compilation units. This is used to locate
203 the target compilation unit of a particular reference. */
ae038cb0
DJ
204 struct dwarf2_per_cu_data **all_comp_units;
205
206 /* The number of compilation units in ALL_COMP_UNITS. */
207 int n_comp_units;
208
1fd400ff
TT
209 /* The number of .debug_types-related CUs. */
210 int n_type_comp_units;
211
212 /* The .debug_types-related CUs. */
213 struct dwarf2_per_cu_data **type_comp_units;
214
ae038cb0
DJ
215 /* A chain of compilation units that are currently read in, so that
216 they can be freed later. */
217 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 218
348e048f
DE
219 /* A table mapping .debug_types signatures to its signatured_type entry.
220 This is NULL if the .debug_types section hasn't been read in yet. */
221 htab_t signatured_types;
222
72dca2f5
FR
223 /* A flag indicating wether this objfile has a section loaded at a
224 VMA of 0. */
225 int has_section_at_zero;
9291a0cd 226
ae2de4f8
DE
227 /* True if we are using the mapped index,
228 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
229 unsigned char using_index;
230
ae2de4f8 231 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 232 struct mapped_index *index_table;
98bfdba5 233
7b9f3c50
DE
234 /* When using index_table, this keeps track of all quick_file_names entries.
235 TUs can share line table entries with CUs or other TUs, and there can be
236 a lot more TUs than unique line tables, so we maintain a separate table
237 of all line table entries to support the sharing. */
238 htab_t quick_file_names_table;
239
98bfdba5
PA
240 /* Set during partial symbol reading, to prevent queueing of full
241 symbols. */
242 int reading_partial_symbols;
673bfd45
DE
243
244 /* Table mapping type .debug_info DIE offsets to types.
245 This is NULL if not allocated yet.
246 It (currently) makes sense to allocate debug_types_type_hash lazily.
247 To keep things simple we allocate both lazily. */
248 htab_t debug_info_type_hash;
249
250 /* Table mapping type .debug_types DIE offsets to types.
251 This is NULL if not allocated yet. */
252 htab_t debug_types_type_hash;
6502dd73
DJ
253};
254
255static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 256
251d32d9 257/* Default names of the debugging sections. */
c906108c 258
233a11ab
CS
259/* Note that if the debugging section has been compressed, it might
260 have a name like .zdebug_info. */
261
251d32d9
TG
262static const struct dwarf2_debug_sections dwarf2_elf_names = {
263 { ".debug_info", ".zdebug_info" },
264 { ".debug_abbrev", ".zdebug_abbrev" },
265 { ".debug_line", ".zdebug_line" },
266 { ".debug_loc", ".zdebug_loc" },
267 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 268 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
269 { ".debug_str", ".zdebug_str" },
270 { ".debug_ranges", ".zdebug_ranges" },
271 { ".debug_types", ".zdebug_types" },
272 { ".debug_frame", ".zdebug_frame" },
273 { ".eh_frame", NULL },
24d3216f
TT
274 { ".gdb_index", ".zgdb_index" },
275 23
251d32d9 276};
c906108c
SS
277
278/* local data types */
279
0963b4bd 280/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
281#ifndef ABBREV_HASH_SIZE
282#define ABBREV_HASH_SIZE 121
283#endif
284
107d2387
AC
285/* The data in a compilation unit header, after target2host
286 translation, looks like this. */
c906108c 287struct comp_unit_head
a738430d 288{
c764a876 289 unsigned int length;
a738430d 290 short version;
a738430d
MK
291 unsigned char addr_size;
292 unsigned char signed_addr_p;
9cbfa09e 293 unsigned int abbrev_offset;
57349743 294
a738430d
MK
295 /* Size of file offsets; either 4 or 8. */
296 unsigned int offset_size;
57349743 297
a738430d
MK
298 /* Size of the length field; either 4 or 12. */
299 unsigned int initial_length_size;
57349743 300
a738430d
MK
301 /* Offset to the first byte of this compilation unit header in the
302 .debug_info section, for resolving relative reference dies. */
303 unsigned int offset;
57349743 304
d00adf39
DE
305 /* Offset to first die in this cu from the start of the cu.
306 This will be the first byte following the compilation unit header. */
307 unsigned int first_die_offset;
a738430d 308};
c906108c 309
3da10d80
KS
310/* Type used for delaying computation of method physnames.
311 See comments for compute_delayed_physnames. */
312struct delayed_method_info
313{
314 /* The type to which the method is attached, i.e., its parent class. */
315 struct type *type;
316
317 /* The index of the method in the type's function fieldlists. */
318 int fnfield_index;
319
320 /* The index of the method in the fieldlist. */
321 int index;
322
323 /* The name of the DIE. */
324 const char *name;
325
326 /* The DIE associated with this method. */
327 struct die_info *die;
328};
329
330typedef struct delayed_method_info delayed_method_info;
331DEF_VEC_O (delayed_method_info);
332
e7c27a73
DJ
333/* Internal state when decoding a particular compilation unit. */
334struct dwarf2_cu
335{
336 /* The objfile containing this compilation unit. */
337 struct objfile *objfile;
338
d00adf39 339 /* The header of the compilation unit. */
e7c27a73 340 struct comp_unit_head header;
e142c38c 341
d00adf39
DE
342 /* Base address of this compilation unit. */
343 CORE_ADDR base_address;
344
345 /* Non-zero if base_address has been set. */
346 int base_known;
347
e142c38c
DJ
348 struct function_range *first_fn, *last_fn, *cached_fn;
349
350 /* The language we are debugging. */
351 enum language language;
352 const struct language_defn *language_defn;
353
b0f35d58
DL
354 const char *producer;
355
e142c38c
DJ
356 /* The generic symbol table building routines have separate lists for
357 file scope symbols and all all other scopes (local scopes). So
358 we need to select the right one to pass to add_symbol_to_list().
359 We do it by keeping a pointer to the correct list in list_in_scope.
360
361 FIXME: The original dwarf code just treated the file scope as the
362 first local scope, and all other local scopes as nested local
363 scopes, and worked fine. Check to see if we really need to
364 distinguish these in buildsym.c. */
365 struct pending **list_in_scope;
366
f3dd6933
DJ
367 /* DWARF abbreviation table associated with this compilation unit. */
368 struct abbrev_info **dwarf2_abbrevs;
369
370 /* Storage for the abbrev table. */
371 struct obstack abbrev_obstack;
72bf9492
DJ
372
373 /* Hash table holding all the loaded partial DIEs. */
374 htab_t partial_dies;
375
376 /* Storage for things with the same lifetime as this read-in compilation
377 unit, including partial DIEs. */
378 struct obstack comp_unit_obstack;
379
ae038cb0
DJ
380 /* When multiple dwarf2_cu structures are living in memory, this field
381 chains them all together, so that they can be released efficiently.
382 We will probably also want a generation counter so that most-recently-used
383 compilation units are cached... */
384 struct dwarf2_per_cu_data *read_in_chain;
385
386 /* Backchain to our per_cu entry if the tree has been built. */
387 struct dwarf2_per_cu_data *per_cu;
388
389 /* How many compilation units ago was this CU last referenced? */
390 int last_used;
391
10b3939b 392 /* A hash table of die offsets for following references. */
51545339 393 htab_t die_hash;
10b3939b
DJ
394
395 /* Full DIEs if read in. */
396 struct die_info *dies;
397
398 /* A set of pointers to dwarf2_per_cu_data objects for compilation
399 units referenced by this one. Only set during full symbol processing;
400 partial symbol tables do not have dependencies. */
401 htab_t dependencies;
402
cb1df416
DJ
403 /* Header data from the line table, during full symbol processing. */
404 struct line_header *line_header;
405
3da10d80
KS
406 /* A list of methods which need to have physnames computed
407 after all type information has been read. */
408 VEC (delayed_method_info) *method_list;
409
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;
1d6edc3c
JK
725
726 /* Valid only if SIZE is not zero. */
fe1b8b76 727 gdb_byte *data;
c906108c
SS
728 };
729
c906108c
SS
730#ifndef ATTR_ALLOC_CHUNK
731#define ATTR_ALLOC_CHUNK 4
732#endif
733
c906108c
SS
734/* Allocate fields for structs, unions and enums in this size. */
735#ifndef DW_FIELD_ALLOC_CHUNK
736#define DW_FIELD_ALLOC_CHUNK 4
737#endif
738
c906108c
SS
739/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
740 but this would require a corresponding change in unpack_field_as_long
741 and friends. */
742static int bits_per_byte = 8;
743
744/* The routines that read and process dies for a C struct or C++ class
745 pass lists of data member fields and lists of member function fields
746 in an instance of a field_info structure, as defined below. */
747struct field_info
c5aa993b 748 {
0963b4bd 749 /* List of data member and baseclasses fields. */
c5aa993b
JM
750 struct nextfield
751 {
752 struct nextfield *next;
753 int accessibility;
754 int virtuality;
755 struct field field;
756 }
7d0ccb61 757 *fields, *baseclasses;
c906108c 758
7d0ccb61 759 /* Number of fields (including baseclasses). */
c5aa993b 760 int nfields;
c906108c 761
c5aa993b
JM
762 /* Number of baseclasses. */
763 int nbaseclasses;
c906108c 764
c5aa993b
JM
765 /* Set if the accesibility of one of the fields is not public. */
766 int non_public_fields;
c906108c 767
c5aa993b
JM
768 /* Member function fields array, entries are allocated in the order they
769 are encountered in the object file. */
770 struct nextfnfield
771 {
772 struct nextfnfield *next;
773 struct fn_field fnfield;
774 }
775 *fnfields;
c906108c 776
c5aa993b
JM
777 /* Member function fieldlist array, contains name of possibly overloaded
778 member function, number of overloaded member functions and a pointer
779 to the head of the member function field chain. */
780 struct fnfieldlist
781 {
782 char *name;
783 int length;
784 struct nextfnfield *head;
785 }
786 *fnfieldlists;
c906108c 787
c5aa993b
JM
788 /* Number of entries in the fnfieldlists array. */
789 int nfnfields;
98751a41
JK
790
791 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
792 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
793 struct typedef_field_list
794 {
795 struct typedef_field field;
796 struct typedef_field_list *next;
797 }
798 *typedef_field_list;
799 unsigned typedef_field_list_count;
c5aa993b 800 };
c906108c 801
10b3939b
DJ
802/* One item on the queue of compilation units to read in full symbols
803 for. */
804struct dwarf2_queue_item
805{
806 struct dwarf2_per_cu_data *per_cu;
807 struct dwarf2_queue_item *next;
808};
809
810/* The current queue. */
811static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
812
ae038cb0
DJ
813/* Loaded secondary compilation units are kept in memory until they
814 have not been referenced for the processing of this many
815 compilation units. Set this to zero to disable caching. Cache
816 sizes of up to at least twenty will improve startup time for
817 typical inter-CU-reference binaries, at an obvious memory cost. */
818static int dwarf2_max_cache_age = 5;
920d2a44
AC
819static void
820show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
821 struct cmd_list_element *c, const char *value)
822{
3e43a32a
MS
823 fprintf_filtered (file, _("The upper bound on the age of cached "
824 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
825 value);
826}
827
ae038cb0 828
0963b4bd 829/* Various complaints about symbol reading that don't abort the process. */
c906108c 830
4d3c2250
KB
831static void
832dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 833{
4d3c2250 834 complaint (&symfile_complaints,
e2e0b3e5 835 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
836}
837
25e43795
DJ
838static void
839dwarf2_debug_line_missing_file_complaint (void)
840{
841 complaint (&symfile_complaints,
842 _(".debug_line section has line data without a file"));
843}
844
59205f5a
JB
845static void
846dwarf2_debug_line_missing_end_sequence_complaint (void)
847{
848 complaint (&symfile_complaints,
3e43a32a
MS
849 _(".debug_line section has line "
850 "program sequence without an end"));
59205f5a
JB
851}
852
4d3c2250
KB
853static void
854dwarf2_complex_location_expr_complaint (void)
2e276125 855{
e2e0b3e5 856 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
857}
858
4d3c2250
KB
859static void
860dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
861 int arg3)
2e276125 862{
4d3c2250 863 complaint (&symfile_complaints,
3e43a32a
MS
864 _("const value length mismatch for '%s', got %d, expected %d"),
865 arg1, arg2, arg3);
4d3c2250
KB
866}
867
868static void
cf2c3c16 869dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
2e276125 870{
4d3c2250 871 complaint (&symfile_complaints,
cf2c3c16
TT
872 _("macro info runs off end of `%s' section"),
873 section->asection->name);
4d3c2250
KB
874}
875
876static void
877dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 878{
4d3c2250 879 complaint (&symfile_complaints,
3e43a32a
MS
880 _("macro debug info contains a "
881 "malformed macro definition:\n`%s'"),
4d3c2250
KB
882 arg1);
883}
884
885static void
886dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 887{
4d3c2250 888 complaint (&symfile_complaints,
3e43a32a
MS
889 _("invalid attribute class or form for '%s' in '%s'"),
890 arg1, arg2);
4d3c2250 891}
c906108c 892
c906108c
SS
893/* local function prototypes */
894
4efb68b1 895static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 896
aaa75496
JB
897static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
898 struct objfile *);
899
918dd910
JK
900static void dwarf2_find_base_address (struct die_info *die,
901 struct dwarf2_cu *cu);
902
c67a9c90 903static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 904
72bf9492
DJ
905static void scan_partial_symbols (struct partial_die_info *,
906 CORE_ADDR *, CORE_ADDR *,
5734ee8b 907 int, struct dwarf2_cu *);
c906108c 908
72bf9492
DJ
909static void add_partial_symbol (struct partial_die_info *,
910 struct dwarf2_cu *);
63d06c5c 911
72bf9492
DJ
912static void add_partial_namespace (struct partial_die_info *pdi,
913 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 914 int need_pc, struct dwarf2_cu *cu);
63d06c5c 915
5d7cb8df
JK
916static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
917 CORE_ADDR *highpc, int need_pc,
918 struct dwarf2_cu *cu);
919
72bf9492
DJ
920static void add_partial_enumeration (struct partial_die_info *enum_pdi,
921 struct dwarf2_cu *cu);
91c24f0a 922
bc30ff58
JB
923static void add_partial_subprogram (struct partial_die_info *pdi,
924 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 925 int need_pc, struct dwarf2_cu *cu);
bc30ff58 926
fe1b8b76 927static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
928 gdb_byte *buffer, gdb_byte *info_ptr,
929 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 930
a14ed312 931static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 932
a14ed312 933static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 934
e7c27a73 935static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 936
f3dd6933 937static void dwarf2_free_abbrev_table (void *);
c906108c 938
fe1b8b76 939static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 940 struct dwarf2_cu *);
72bf9492 941
57349743 942static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 943 struct dwarf2_cu *);
c906108c 944
93311388
DE
945static struct partial_die_info *load_partial_dies (bfd *,
946 gdb_byte *, gdb_byte *,
947 int, struct dwarf2_cu *);
72bf9492 948
fe1b8b76 949static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
950 struct abbrev_info *abbrev,
951 unsigned int, bfd *,
952 gdb_byte *, gdb_byte *,
953 struct dwarf2_cu *);
c906108c 954
c764a876 955static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 956 struct dwarf2_cu *);
72bf9492
DJ
957
958static void fixup_partial_die (struct partial_die_info *,
959 struct dwarf2_cu *);
960
fe1b8b76
JB
961static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
962 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 963
fe1b8b76
JB
964static gdb_byte *read_attribute_value (struct attribute *, unsigned,
965 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 966
fe1b8b76 967static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 968
fe1b8b76 969static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 970
fe1b8b76 971static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 972
fe1b8b76 973static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 974
93311388 975static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 976
fe1b8b76 977static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 978 unsigned int *);
c906108c 979
c764a876
DE
980static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
981
982static LONGEST read_checked_initial_length_and_offset
983 (bfd *, gdb_byte *, const struct comp_unit_head *,
984 unsigned int *, unsigned int *);
613e1657 985
fe1b8b76 986static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
987 unsigned int *);
988
989static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 990
fe1b8b76 991static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 992
9b1c24c8 993static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 994
fe1b8b76
JB
995static char *read_indirect_string (bfd *, gdb_byte *,
996 const struct comp_unit_head *,
997 unsigned int *);
4bdf3d34 998
fe1b8b76 999static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1000
fe1b8b76 1001static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1002
fe1b8b76 1003static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 1004
e142c38c 1005static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1006
e142c38c
DJ
1007static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1008 struct dwarf2_cu *);
c906108c 1009
348e048f
DE
1010static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1011 unsigned int,
1012 struct dwarf2_cu *);
1013
05cf31d1
JB
1014static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1015 struct dwarf2_cu *cu);
1016
e142c38c 1017static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1018
e142c38c 1019static struct die_info *die_specification (struct die_info *die,
f2f0e013 1020 struct dwarf2_cu **);
63d06c5c 1021
debd256d
JB
1022static void free_line_header (struct line_header *lh);
1023
aaa75496
JB
1024static void add_file_name (struct line_header *, char *, unsigned int,
1025 unsigned int, unsigned int);
1026
debd256d
JB
1027static struct line_header *(dwarf_decode_line_header
1028 (unsigned int offset,
e7c27a73 1029 bfd *abfd, struct dwarf2_cu *cu));
debd256d 1030
72b9f47f 1031static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 1032 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1033
72b9f47f 1034static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1035
a14ed312 1036static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1037 struct dwarf2_cu *);
c906108c 1038
34eaf542
TT
1039static struct symbol *new_symbol_full (struct die_info *, struct type *,
1040 struct dwarf2_cu *, struct symbol *);
1041
a14ed312 1042static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1043 struct dwarf2_cu *);
c906108c 1044
98bfdba5
PA
1045static void dwarf2_const_value_attr (struct attribute *attr,
1046 struct type *type,
1047 const char *name,
1048 struct obstack *obstack,
1049 struct dwarf2_cu *cu, long *value,
1050 gdb_byte **bytes,
1051 struct dwarf2_locexpr_baton **baton);
2df3850c 1052
e7c27a73 1053static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1054
b4ba55a1
JB
1055static int need_gnat_info (struct dwarf2_cu *);
1056
3e43a32a
MS
1057static struct type *die_descriptive_type (struct die_info *,
1058 struct dwarf2_cu *);
b4ba55a1
JB
1059
1060static void set_descriptive_type (struct type *, struct die_info *,
1061 struct dwarf2_cu *);
1062
e7c27a73
DJ
1063static struct type *die_containing_type (struct die_info *,
1064 struct dwarf2_cu *);
c906108c 1065
673bfd45
DE
1066static struct type *lookup_die_type (struct die_info *, struct attribute *,
1067 struct dwarf2_cu *);
c906108c 1068
f792889a 1069static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1070
673bfd45
DE
1071static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1072
086ed43d 1073static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1074
6e70227d 1075static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1076 const char *suffix, int physname,
1077 struct dwarf2_cu *cu);
63d06c5c 1078
e7c27a73 1079static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1080
348e048f
DE
1081static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1082
e7c27a73 1083static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1084
e7c27a73 1085static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1086
96408a79
SA
1087static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1088
ff013f42
JK
1089static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1090 struct dwarf2_cu *, struct partial_symtab *);
1091
a14ed312 1092static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1093 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1094 struct partial_symtab *);
c906108c 1095
fae299cd
DC
1096static void get_scope_pc_bounds (struct die_info *,
1097 CORE_ADDR *, CORE_ADDR *,
1098 struct dwarf2_cu *);
1099
801e3a5b
JB
1100static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1101 CORE_ADDR, struct dwarf2_cu *);
1102
a14ed312 1103static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1104 struct dwarf2_cu *);
c906108c 1105
a14ed312 1106static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1107 struct type *, struct dwarf2_cu *);
c906108c 1108
a14ed312 1109static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1110 struct die_info *, struct type *,
e7c27a73 1111 struct dwarf2_cu *);
c906108c 1112
a14ed312 1113static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1114 struct type *,
1115 struct dwarf2_cu *);
c906108c 1116
134d01f1 1117static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1118
e7c27a73 1119static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1120
e7c27a73 1121static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1122
5d7cb8df
JK
1123static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1124
27aa8d6a
SW
1125static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1126
f55ee35c
JK
1127static struct type *read_module_type (struct die_info *die,
1128 struct dwarf2_cu *cu);
1129
38d518c9 1130static const char *namespace_name (struct die_info *die,
e142c38c 1131 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1132
134d01f1 1133static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1134
e7c27a73 1135static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1136
6e70227d 1137static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1138 struct dwarf2_cu *);
1139
93311388 1140static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1141
93311388
DE
1142static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1143 gdb_byte *info_ptr,
d97bc12b
DE
1144 gdb_byte **new_info_ptr,
1145 struct die_info *parent);
1146
93311388
DE
1147static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1148 gdb_byte *info_ptr,
fe1b8b76 1149 gdb_byte **new_info_ptr,
639d11d3
DC
1150 struct die_info *parent);
1151
93311388
DE
1152static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1153 gdb_byte *info_ptr,
fe1b8b76 1154 gdb_byte **new_info_ptr,
639d11d3
DC
1155 struct die_info *parent);
1156
93311388
DE
1157static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1158 struct die_info **, gdb_byte *,
1159 int *);
1160
e7c27a73 1161static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1162
71c25dea
TT
1163static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1164 struct obstack *);
1165
e142c38c 1166static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1167
98bfdba5
PA
1168static const char *dwarf2_full_name (char *name,
1169 struct die_info *die,
1170 struct dwarf2_cu *cu);
1171
e142c38c 1172static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1173 struct dwarf2_cu **);
9219021c 1174
a14ed312 1175static char *dwarf_tag_name (unsigned int);
c906108c 1176
a14ed312 1177static char *dwarf_attr_name (unsigned int);
c906108c 1178
a14ed312 1179static char *dwarf_form_name (unsigned int);
c906108c 1180
a14ed312 1181static char *dwarf_bool_name (unsigned int);
c906108c 1182
a14ed312 1183static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1184
1185#if 0
a14ed312 1186static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1187#endif
1188
f9aca02d 1189static struct die_info *sibling_die (struct die_info *);
c906108c 1190
d97bc12b
DE
1191static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1192
1193static void dump_die_for_error (struct die_info *);
1194
1195static void dump_die_1 (struct ui_file *, int level, int max_level,
1196 struct die_info *);
c906108c 1197
d97bc12b 1198/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1199
51545339 1200static void store_in_ref_table (struct die_info *,
10b3939b 1201 struct dwarf2_cu *);
c906108c 1202
93311388
DE
1203static int is_ref_attr (struct attribute *);
1204
c764a876 1205static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1206
43bbcdc2 1207static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1208
348e048f
DE
1209static struct die_info *follow_die_ref_or_sig (struct die_info *,
1210 struct attribute *,
1211 struct dwarf2_cu **);
1212
10b3939b
DJ
1213static struct die_info *follow_die_ref (struct die_info *,
1214 struct attribute *,
f2f0e013 1215 struct dwarf2_cu **);
c906108c 1216
348e048f
DE
1217static struct die_info *follow_die_sig (struct die_info *,
1218 struct attribute *,
1219 struct dwarf2_cu **);
1220
6c83ed52
TT
1221static struct signatured_type *lookup_signatured_type_at_offset
1222 (struct objfile *objfile,
1223 struct dwarf2_section_info *section,
1224 unsigned int offset);
1225
348e048f 1226static void read_signatured_type_at_offset (struct objfile *objfile,
8b70b953 1227 struct dwarf2_section_info *sect,
348e048f
DE
1228 unsigned int offset);
1229
1230static void read_signatured_type (struct objfile *,
1231 struct signatured_type *type_sig);
1232
c906108c
SS
1233/* memory allocation interface */
1234
7b5a2f43 1235static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1236
f3dd6933 1237static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1238
b60c80d6 1239static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1240
e142c38c 1241static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1242
e142c38c
DJ
1243static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1244 struct dwarf2_cu *);
5fb290d7 1245
2e276125 1246static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1247 char *, bfd *, struct dwarf2_cu *,
1248 struct dwarf2_section_info *,
1249 int);
2e276125 1250
8e19ed76
PS
1251static int attr_form_is_block (struct attribute *);
1252
3690dd37
JB
1253static int attr_form_is_section_offset (struct attribute *);
1254
1255static int attr_form_is_constant (struct attribute *);
1256
8cf6f0b1
TT
1257static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1258 struct dwarf2_loclist_baton *baton,
1259 struct attribute *attr);
1260
93e7bd98
DJ
1261static void dwarf2_symbol_mark_computed (struct attribute *attr,
1262 struct symbol *sym,
1263 struct dwarf2_cu *cu);
4c2df51b 1264
93311388
DE
1265static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1266 struct abbrev_info *abbrev,
1267 struct dwarf2_cu *cu);
4bb7a0a7 1268
72bf9492
DJ
1269static void free_stack_comp_unit (void *);
1270
72bf9492
DJ
1271static hashval_t partial_die_hash (const void *item);
1272
1273static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1274
ae038cb0 1275static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1276 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1277
1278static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1279 (unsigned int offset, struct objfile *objfile);
ae038cb0 1280
9816fde3
JK
1281static void init_one_comp_unit (struct dwarf2_cu *cu,
1282 struct objfile *objfile);
1283
1284static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1285 struct die_info *comp_unit_die);
93311388 1286
ae038cb0
DJ
1287static void free_one_comp_unit (void *);
1288
1289static void free_cached_comp_units (void *);
1290
1291static void age_cached_comp_units (void);
1292
1293static void free_one_cached_comp_unit (void *);
1294
f792889a
DJ
1295static struct type *set_die_type (struct die_info *, struct type *,
1296 struct dwarf2_cu *);
1c379e20 1297
ae038cb0
DJ
1298static void create_all_comp_units (struct objfile *);
1299
1fd400ff
TT
1300static int create_debug_types_hash_table (struct objfile *objfile);
1301
93311388
DE
1302static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1303 struct objfile *);
10b3939b
DJ
1304
1305static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1306
1307static void dwarf2_add_dependence (struct dwarf2_cu *,
1308 struct dwarf2_per_cu_data *);
1309
ae038cb0
DJ
1310static void dwarf2_mark (struct dwarf2_cu *);
1311
1312static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1313
673bfd45
DE
1314static struct type *get_die_type_at_offset (unsigned int,
1315 struct dwarf2_per_cu_data *per_cu);
1316
f792889a 1317static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1318
9291a0cd
TT
1319static void dwarf2_release_queue (void *dummy);
1320
1321static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1322 struct objfile *objfile);
1323
1324static void process_queue (struct objfile *objfile);
1325
1326static void find_file_and_directory (struct die_info *die,
1327 struct dwarf2_cu *cu,
1328 char **name, char **comp_dir);
1329
1330static char *file_full_name (int file, struct line_header *lh,
1331 const char *comp_dir);
1332
1333static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1334 gdb_byte *info_ptr,
1335 gdb_byte *buffer,
1336 unsigned int buffer_size,
1337 bfd *abfd);
1338
1339static void init_cu_die_reader (struct die_reader_specs *reader,
1340 struct dwarf2_cu *cu);
1341
673bfd45 1342static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1343
9291a0cd
TT
1344#if WORDS_BIGENDIAN
1345
1346/* Convert VALUE between big- and little-endian. */
1347static offset_type
1348byte_swap (offset_type value)
1349{
1350 offset_type result;
1351
1352 result = (value & 0xff) << 24;
1353 result |= (value & 0xff00) << 8;
1354 result |= (value & 0xff0000) >> 8;
1355 result |= (value & 0xff000000) >> 24;
1356 return result;
1357}
1358
1359#define MAYBE_SWAP(V) byte_swap (V)
1360
1361#else
1362#define MAYBE_SWAP(V) (V)
1363#endif /* WORDS_BIGENDIAN */
1364
1365/* The suffix for an index file. */
1366#define INDEX_SUFFIX ".gdb-index"
1367
3da10d80
KS
1368static const char *dwarf2_physname (char *name, struct die_info *die,
1369 struct dwarf2_cu *cu);
1370
c906108c 1371/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1372 information and return true if we have enough to do something.
1373 NAMES points to the dwarf2 section names, or is NULL if the standard
1374 ELF names are used. */
c906108c
SS
1375
1376int
251d32d9
TG
1377dwarf2_has_info (struct objfile *objfile,
1378 const struct dwarf2_debug_sections *names)
c906108c 1379{
be391dca
TT
1380 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1381 if (!dwarf2_per_objfile)
1382 {
1383 /* Initialize per-objfile state. */
1384 struct dwarf2_per_objfile *data
1385 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1386
be391dca
TT
1387 memset (data, 0, sizeof (*data));
1388 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1389 dwarf2_per_objfile = data;
6502dd73 1390
251d32d9
TG
1391 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1392 (void *) names);
be391dca
TT
1393 dwarf2_per_objfile->objfile = objfile;
1394 }
1395 return (dwarf2_per_objfile->info.asection != NULL
1396 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1397}
1398
251d32d9
TG
1399/* When loading sections, we look either for uncompressed section or for
1400 compressed section names. */
233a11ab
CS
1401
1402static int
251d32d9
TG
1403section_is_p (const char *section_name,
1404 const struct dwarf2_section_names *names)
233a11ab 1405{
251d32d9
TG
1406 if (names->normal != NULL
1407 && strcmp (section_name, names->normal) == 0)
1408 return 1;
1409 if (names->compressed != NULL
1410 && strcmp (section_name, names->compressed) == 0)
1411 return 1;
1412 return 0;
233a11ab
CS
1413}
1414
c906108c
SS
1415/* This function is mapped across the sections and remembers the
1416 offset and size of each of the debugging sections we are interested
1417 in. */
1418
1419static void
251d32d9 1420dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1421{
251d32d9
TG
1422 const struct dwarf2_debug_sections *names;
1423
1424 if (vnames == NULL)
1425 names = &dwarf2_elf_names;
1426 else
1427 names = (const struct dwarf2_debug_sections *) vnames;
1428
1429 if (section_is_p (sectp->name, &names->info))
c906108c 1430 {
dce234bc
PP
1431 dwarf2_per_objfile->info.asection = sectp;
1432 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1433 }
251d32d9 1434 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1435 {
dce234bc
PP
1436 dwarf2_per_objfile->abbrev.asection = sectp;
1437 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1438 }
251d32d9 1439 else if (section_is_p (sectp->name, &names->line))
c906108c 1440 {
dce234bc
PP
1441 dwarf2_per_objfile->line.asection = sectp;
1442 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1443 }
251d32d9 1444 else if (section_is_p (sectp->name, &names->loc))
c906108c 1445 {
dce234bc
PP
1446 dwarf2_per_objfile->loc.asection = sectp;
1447 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1448 }
251d32d9 1449 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1450 {
dce234bc
PP
1451 dwarf2_per_objfile->macinfo.asection = sectp;
1452 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1453 }
cf2c3c16
TT
1454 else if (section_is_p (sectp->name, &names->macro))
1455 {
1456 dwarf2_per_objfile->macro.asection = sectp;
1457 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1458 }
251d32d9 1459 else if (section_is_p (sectp->name, &names->str))
c906108c 1460 {
dce234bc
PP
1461 dwarf2_per_objfile->str.asection = sectp;
1462 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1463 }
251d32d9 1464 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1465 {
dce234bc
PP
1466 dwarf2_per_objfile->frame.asection = sectp;
1467 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1468 }
251d32d9 1469 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1470 {
3799ccc6 1471 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1472
3799ccc6
EZ
1473 if (aflag & SEC_HAS_CONTENTS)
1474 {
dce234bc
PP
1475 dwarf2_per_objfile->eh_frame.asection = sectp;
1476 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1477 }
b6af0555 1478 }
251d32d9 1479 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1480 {
dce234bc
PP
1481 dwarf2_per_objfile->ranges.asection = sectp;
1482 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1483 }
251d32d9 1484 else if (section_is_p (sectp->name, &names->types))
348e048f 1485 {
8b70b953
TT
1486 struct dwarf2_section_info type_section;
1487
1488 memset (&type_section, 0, sizeof (type_section));
1489 type_section.asection = sectp;
1490 type_section.size = bfd_get_section_size (sectp);
1491
1492 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1493 &type_section);
348e048f 1494 }
251d32d9 1495 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1496 {
1497 dwarf2_per_objfile->gdb_index.asection = sectp;
1498 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1499 }
dce234bc 1500
72dca2f5
FR
1501 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1502 && bfd_section_vma (abfd, sectp) == 0)
1503 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1504}
1505
dce234bc
PP
1506/* Decompress a section that was compressed using zlib. Store the
1507 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1508
1509static void
dce234bc
PP
1510zlib_decompress_section (struct objfile *objfile, asection *sectp,
1511 gdb_byte **outbuf, bfd_size_type *outsize)
1512{
1513 bfd *abfd = objfile->obfd;
1514#ifndef HAVE_ZLIB_H
1515 error (_("Support for zlib-compressed DWARF data (from '%s') "
1516 "is disabled in this copy of GDB"),
1517 bfd_get_filename (abfd));
1518#else
1519 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1520 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1521 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1522 bfd_size_type uncompressed_size;
1523 gdb_byte *uncompressed_buffer;
1524 z_stream strm;
1525 int rc;
1526 int header_size = 12;
1527
1528 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1529 || bfd_bread (compressed_buffer,
1530 compressed_size, abfd) != compressed_size)
dce234bc
PP
1531 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1532 bfd_get_filename (abfd));
1533
1534 /* Read the zlib header. In this case, it should be "ZLIB" followed
1535 by the uncompressed section size, 8 bytes in big-endian order. */
1536 if (compressed_size < header_size
1537 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1538 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1539 bfd_get_filename (abfd));
1540 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1541 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1542 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1543 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1544 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1545 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1546 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1547 uncompressed_size += compressed_buffer[11];
1548
1549 /* It is possible the section consists of several compressed
1550 buffers concatenated together, so we uncompress in a loop. */
1551 strm.zalloc = NULL;
1552 strm.zfree = NULL;
1553 strm.opaque = NULL;
1554 strm.avail_in = compressed_size - header_size;
1555 strm.next_in = (Bytef*) compressed_buffer + header_size;
1556 strm.avail_out = uncompressed_size;
1557 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1558 uncompressed_size);
1559 rc = inflateInit (&strm);
1560 while (strm.avail_in > 0)
1561 {
1562 if (rc != Z_OK)
1563 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1564 bfd_get_filename (abfd), rc);
1565 strm.next_out = ((Bytef*) uncompressed_buffer
1566 + (uncompressed_size - strm.avail_out));
1567 rc = inflate (&strm, Z_FINISH);
1568 if (rc != Z_STREAM_END)
1569 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1570 bfd_get_filename (abfd), rc);
1571 rc = inflateReset (&strm);
1572 }
1573 rc = inflateEnd (&strm);
1574 if (rc != Z_OK
1575 || strm.avail_out != 0)
1576 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1577 bfd_get_filename (abfd), rc);
1578
affddf13 1579 do_cleanups (cleanup);
dce234bc
PP
1580 *outbuf = uncompressed_buffer;
1581 *outsize = uncompressed_size;
1582#endif
233a11ab
CS
1583}
1584
9e0ac564
TT
1585/* A helper function that decides whether a section is empty. */
1586
1587static int
1588dwarf2_section_empty_p (struct dwarf2_section_info *info)
1589{
1590 return info->asection == NULL || info->size == 0;
1591}
1592
dce234bc
PP
1593/* Read the contents of the section SECTP from object file specified by
1594 OBJFILE, store info about the section into INFO.
1595 If the section is compressed, uncompress it before returning. */
c906108c 1596
dce234bc
PP
1597static void
1598dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1599{
dce234bc
PP
1600 bfd *abfd = objfile->obfd;
1601 asection *sectp = info->asection;
1602 gdb_byte *buf, *retbuf;
1603 unsigned char header[4];
c906108c 1604
be391dca
TT
1605 if (info->readin)
1606 return;
dce234bc 1607 info->buffer = NULL;
b315ab21 1608 info->map_addr = NULL;
be391dca 1609 info->readin = 1;
188dd5d6 1610
9e0ac564 1611 if (dwarf2_section_empty_p (info))
dce234bc 1612 return;
c906108c 1613
dce234bc
PP
1614 /* Check if the file has a 4-byte header indicating compression. */
1615 if (info->size > sizeof (header)
1616 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1617 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1618 {
1619 /* Upon decompression, update the buffer and its size. */
1620 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1621 {
1622 zlib_decompress_section (objfile, sectp, &info->buffer,
1623 &info->size);
1624 return;
1625 }
1626 }
4bdf3d34 1627
dce234bc
PP
1628#ifdef HAVE_MMAP
1629 if (pagesize == 0)
1630 pagesize = getpagesize ();
2e276125 1631
dce234bc
PP
1632 /* Only try to mmap sections which are large enough: we don't want to
1633 waste space due to fragmentation. Also, only try mmap for sections
1634 without relocations. */
1635
1636 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1637 {
b315ab21
TG
1638 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1639 MAP_PRIVATE, sectp->filepos,
1640 &info->map_addr, &info->map_len);
dce234bc 1641
b315ab21 1642 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1643 {
be391dca 1644#if HAVE_POSIX_MADVISE
b315ab21 1645 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1646#endif
dce234bc
PP
1647 return;
1648 }
1649 }
1650#endif
1651
1652 /* If we get here, we are a normal, not-compressed section. */
1653 info->buffer = buf
1654 = obstack_alloc (&objfile->objfile_obstack, info->size);
1655
1656 /* When debugging .o files, we may need to apply relocations; see
1657 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1658 We never compress sections in .o files, so we only need to
1659 try this when the section is not compressed. */
ac8035ab 1660 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1661 if (retbuf != NULL)
1662 {
1663 info->buffer = retbuf;
1664 return;
1665 }
1666
1667 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1668 || bfd_bread (buf, info->size, abfd) != info->size)
1669 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1670 bfd_get_filename (abfd));
1671}
1672
9e0ac564
TT
1673/* A helper function that returns the size of a section in a safe way.
1674 If you are positive that the section has been read before using the
1675 size, then it is safe to refer to the dwarf2_section_info object's
1676 "size" field directly. In other cases, you must call this
1677 function, because for compressed sections the size field is not set
1678 correctly until the section has been read. */
1679
1680static bfd_size_type
1681dwarf2_section_size (struct objfile *objfile,
1682 struct dwarf2_section_info *info)
1683{
1684 if (!info->readin)
1685 dwarf2_read_section (objfile, info);
1686 return info->size;
1687}
1688
dce234bc 1689/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1690 SECTION_NAME. */
af34e669 1691
dce234bc 1692void
3017a003
TG
1693dwarf2_get_section_info (struct objfile *objfile,
1694 enum dwarf2_section_enum sect,
dce234bc
PP
1695 asection **sectp, gdb_byte **bufp,
1696 bfd_size_type *sizep)
1697{
1698 struct dwarf2_per_objfile *data
1699 = objfile_data (objfile, dwarf2_objfile_data_key);
1700 struct dwarf2_section_info *info;
a3b2a86b
TT
1701
1702 /* We may see an objfile without any DWARF, in which case we just
1703 return nothing. */
1704 if (data == NULL)
1705 {
1706 *sectp = NULL;
1707 *bufp = NULL;
1708 *sizep = 0;
1709 return;
1710 }
3017a003
TG
1711 switch (sect)
1712 {
1713 case DWARF2_DEBUG_FRAME:
1714 info = &data->frame;
1715 break;
1716 case DWARF2_EH_FRAME:
1717 info = &data->eh_frame;
1718 break;
1719 default:
1720 gdb_assert_not_reached ("unexpected section");
1721 }
dce234bc 1722
9e0ac564 1723 dwarf2_read_section (objfile, info);
dce234bc
PP
1724
1725 *sectp = info->asection;
1726 *bufp = info->buffer;
1727 *sizep = info->size;
1728}
1729
9291a0cd 1730\f
7b9f3c50
DE
1731/* DWARF quick_symbols_functions support. */
1732
1733/* TUs can share .debug_line entries, and there can be a lot more TUs than
1734 unique line tables, so we maintain a separate table of all .debug_line
1735 derived entries to support the sharing.
1736 All the quick functions need is the list of file names. We discard the
1737 line_header when we're done and don't need to record it here. */
1738struct quick_file_names
1739{
1740 /* The offset in .debug_line of the line table. We hash on this. */
1741 unsigned int offset;
1742
1743 /* The number of entries in file_names, real_names. */
1744 unsigned int num_file_names;
1745
1746 /* The file names from the line table, after being run through
1747 file_full_name. */
1748 const char **file_names;
1749
1750 /* The file names from the line table after being run through
1751 gdb_realpath. These are computed lazily. */
1752 const char **real_names;
1753};
1754
1755/* When using the index (and thus not using psymtabs), each CU has an
1756 object of this type. This is used to hold information needed by
1757 the various "quick" methods. */
1758struct dwarf2_per_cu_quick_data
1759{
1760 /* The file table. This can be NULL if there was no file table
1761 or it's currently not read in.
1762 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1763 struct quick_file_names *file_names;
1764
1765 /* The corresponding symbol table. This is NULL if symbols for this
1766 CU have not yet been read. */
1767 struct symtab *symtab;
1768
1769 /* A temporary mark bit used when iterating over all CUs in
1770 expand_symtabs_matching. */
1771 unsigned int mark : 1;
1772
1773 /* True if we've tried to read the file table and found there isn't one.
1774 There will be no point in trying to read it again next time. */
1775 unsigned int no_file_data : 1;
1776};
1777
1778/* Hash function for a quick_file_names. */
1779
1780static hashval_t
1781hash_file_name_entry (const void *e)
1782{
1783 const struct quick_file_names *file_data = e;
1784
1785 return file_data->offset;
1786}
1787
1788/* Equality function for a quick_file_names. */
1789
1790static int
1791eq_file_name_entry (const void *a, const void *b)
1792{
1793 const struct quick_file_names *ea = a;
1794 const struct quick_file_names *eb = b;
1795
1796 return ea->offset == eb->offset;
1797}
1798
1799/* Delete function for a quick_file_names. */
1800
1801static void
1802delete_file_name_entry (void *e)
1803{
1804 struct quick_file_names *file_data = e;
1805 int i;
1806
1807 for (i = 0; i < file_data->num_file_names; ++i)
1808 {
1809 xfree ((void*) file_data->file_names[i]);
1810 if (file_data->real_names)
1811 xfree ((void*) file_data->real_names[i]);
1812 }
1813
1814 /* The space for the struct itself lives on objfile_obstack,
1815 so we don't free it here. */
1816}
1817
1818/* Create a quick_file_names hash table. */
1819
1820static htab_t
1821create_quick_file_names_table (unsigned int nr_initial_entries)
1822{
1823 return htab_create_alloc (nr_initial_entries,
1824 hash_file_name_entry, eq_file_name_entry,
1825 delete_file_name_entry, xcalloc, xfree);
1826}
9291a0cd 1827
918dd910
JK
1828/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1829 have to be created afterwards. You should call age_cached_comp_units after
1830 processing PER_CU->CU. dw2_setup must have been already called. */
1831
1832static void
1833load_cu (struct dwarf2_per_cu_data *per_cu)
1834{
8b70b953
TT
1835 if (per_cu->debug_type_section)
1836 read_signatured_type_at_offset (per_cu->objfile,
1837 per_cu->debug_type_section,
1838 per_cu->offset);
918dd910
JK
1839 else
1840 load_full_comp_unit (per_cu, per_cu->objfile);
1841
1842 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1843
1844 gdb_assert (per_cu->cu != NULL);
1845}
1846
9291a0cd
TT
1847/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1848 this CU came. */
2fdf6df6 1849
9291a0cd
TT
1850static void
1851dw2_do_instantiate_symtab (struct objfile *objfile,
1852 struct dwarf2_per_cu_data *per_cu)
1853{
1854 struct cleanup *back_to;
1855
1856 back_to = make_cleanup (dwarf2_release_queue, NULL);
1857
1858 queue_comp_unit (per_cu, objfile);
1859
918dd910 1860 load_cu (per_cu);
9291a0cd
TT
1861
1862 process_queue (objfile);
1863
1864 /* Age the cache, releasing compilation units that have not
1865 been used recently. */
1866 age_cached_comp_units ();
1867
1868 do_cleanups (back_to);
1869}
1870
1871/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1872 the objfile from which this CU came. Returns the resulting symbol
1873 table. */
2fdf6df6 1874
9291a0cd
TT
1875static struct symtab *
1876dw2_instantiate_symtab (struct objfile *objfile,
1877 struct dwarf2_per_cu_data *per_cu)
1878{
1879 if (!per_cu->v.quick->symtab)
1880 {
1881 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1882 increment_reading_symtab ();
1883 dw2_do_instantiate_symtab (objfile, per_cu);
1884 do_cleanups (back_to);
1885 }
1886 return per_cu->v.quick->symtab;
1887}
1888
1fd400ff 1889/* Return the CU given its index. */
2fdf6df6 1890
1fd400ff
TT
1891static struct dwarf2_per_cu_data *
1892dw2_get_cu (int index)
1893{
1894 if (index >= dwarf2_per_objfile->n_comp_units)
1895 {
1896 index -= dwarf2_per_objfile->n_comp_units;
1897 return dwarf2_per_objfile->type_comp_units[index];
1898 }
1899 return dwarf2_per_objfile->all_comp_units[index];
1900}
1901
9291a0cd
TT
1902/* A helper function that knows how to read a 64-bit value in a way
1903 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1904 otherwise. */
2fdf6df6 1905
9291a0cd
TT
1906static int
1907extract_cu_value (const char *bytes, ULONGEST *result)
1908{
1909 if (sizeof (ULONGEST) < 8)
1910 {
1911 int i;
1912
1913 /* Ignore the upper 4 bytes if they are all zero. */
1914 for (i = 0; i < 4; ++i)
1915 if (bytes[i + 4] != 0)
1916 return 0;
1917
1918 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1919 }
1920 else
1921 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1922 return 1;
1923}
1924
1925/* Read the CU list from the mapped index, and use it to create all
1926 the CU objects for this objfile. Return 0 if something went wrong,
1927 1 if everything went ok. */
2fdf6df6 1928
9291a0cd 1929static int
1fd400ff
TT
1930create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1931 offset_type cu_list_elements)
9291a0cd
TT
1932{
1933 offset_type i;
9291a0cd
TT
1934
1935 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1936 dwarf2_per_objfile->all_comp_units
1937 = obstack_alloc (&objfile->objfile_obstack,
1938 dwarf2_per_objfile->n_comp_units
1939 * sizeof (struct dwarf2_per_cu_data *));
1940
1941 for (i = 0; i < cu_list_elements; i += 2)
1942 {
1943 struct dwarf2_per_cu_data *the_cu;
1944 ULONGEST offset, length;
1945
1946 if (!extract_cu_value (cu_list, &offset)
1947 || !extract_cu_value (cu_list + 8, &length))
1948 return 0;
1949 cu_list += 2 * 8;
1950
1951 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1952 struct dwarf2_per_cu_data);
1953 the_cu->offset = offset;
1954 the_cu->length = length;
1955 the_cu->objfile = objfile;
1956 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1957 struct dwarf2_per_cu_quick_data);
1958 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1959 }
1960
1961 return 1;
1962}
1963
1fd400ff 1964/* Create the signatured type hash table from the index. */
673bfd45 1965
1fd400ff 1966static int
673bfd45 1967create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 1968 struct dwarf2_section_info *section,
673bfd45
DE
1969 const gdb_byte *bytes,
1970 offset_type elements)
1fd400ff
TT
1971{
1972 offset_type i;
673bfd45 1973 htab_t sig_types_hash;
1fd400ff
TT
1974
1975 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1976 dwarf2_per_objfile->type_comp_units
1977 = obstack_alloc (&objfile->objfile_obstack,
1978 dwarf2_per_objfile->n_type_comp_units
1979 * sizeof (struct dwarf2_per_cu_data *));
1980
673bfd45 1981 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1982
1983 for (i = 0; i < elements; i += 3)
1984 {
1985 struct signatured_type *type_sig;
1986 ULONGEST offset, type_offset, signature;
1987 void **slot;
1988
1989 if (!extract_cu_value (bytes, &offset)
1990 || !extract_cu_value (bytes + 8, &type_offset))
1991 return 0;
1992 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1993 bytes += 3 * 8;
1994
1995 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1996 struct signatured_type);
1997 type_sig->signature = signature;
1fd400ff 1998 type_sig->type_offset = type_offset;
8b70b953 1999 type_sig->per_cu.debug_type_section = section;
1fd400ff
TT
2000 type_sig->per_cu.offset = offset;
2001 type_sig->per_cu.objfile = objfile;
2002 type_sig->per_cu.v.quick
2003 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2004 struct dwarf2_per_cu_quick_data);
2005
673bfd45 2006 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
2007 *slot = type_sig;
2008
2009 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
2010 }
2011
673bfd45 2012 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2013
2014 return 1;
2015}
2016
9291a0cd
TT
2017/* Read the address map data from the mapped index, and use it to
2018 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2019
9291a0cd
TT
2020static void
2021create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2022{
2023 const gdb_byte *iter, *end;
2024 struct obstack temp_obstack;
2025 struct addrmap *mutable_map;
2026 struct cleanup *cleanup;
2027 CORE_ADDR baseaddr;
2028
2029 obstack_init (&temp_obstack);
2030 cleanup = make_cleanup_obstack_free (&temp_obstack);
2031 mutable_map = addrmap_create_mutable (&temp_obstack);
2032
2033 iter = index->address_table;
2034 end = iter + index->address_table_size;
2035
2036 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2037
2038 while (iter < end)
2039 {
2040 ULONGEST hi, lo, cu_index;
2041 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2042 iter += 8;
2043 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2044 iter += 8;
2045 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2046 iter += 4;
2047
2048 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2049 dw2_get_cu (cu_index));
9291a0cd
TT
2050 }
2051
2052 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2053 &objfile->objfile_obstack);
2054 do_cleanups (cleanup);
2055}
2056
59d7bcaf
JK
2057/* The hash function for strings in the mapped index. This is the same as
2058 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2059 implementation. This is necessary because the hash function is tied to the
2060 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2061 SYMBOL_HASH_NEXT.
2062
2063 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2064
9291a0cd 2065static hashval_t
559a7a62 2066mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2067{
2068 const unsigned char *str = (const unsigned char *) p;
2069 hashval_t r = 0;
2070 unsigned char c;
2071
2072 while ((c = *str++) != 0)
559a7a62
JK
2073 {
2074 if (index_version >= 5)
2075 c = tolower (c);
2076 r = r * 67 + c - 113;
2077 }
9291a0cd
TT
2078
2079 return r;
2080}
2081
2082/* Find a slot in the mapped index INDEX for the object named NAME.
2083 If NAME is found, set *VEC_OUT to point to the CU vector in the
2084 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2085
9291a0cd
TT
2086static int
2087find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2088 offset_type **vec_out)
2089{
0cf03b49
JK
2090 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2091 offset_type hash;
9291a0cd 2092 offset_type slot, step;
559a7a62 2093 int (*cmp) (const char *, const char *);
9291a0cd 2094
0cf03b49
JK
2095 if (current_language->la_language == language_cplus
2096 || current_language->la_language == language_java
2097 || current_language->la_language == language_fortran)
2098 {
2099 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2100 not contain any. */
2101 const char *paren = strchr (name, '(');
2102
2103 if (paren)
2104 {
2105 char *dup;
2106
2107 dup = xmalloc (paren - name + 1);
2108 memcpy (dup, name, paren - name);
2109 dup[paren - name] = 0;
2110
2111 make_cleanup (xfree, dup);
2112 name = dup;
2113 }
2114 }
2115
559a7a62
JK
2116 /* Index version 4 did not support case insensitive searches. But the
2117 indexes for case insensitive languages are built in lowercase, therefore
2118 simulate our NAME being searched is also lowercased. */
2119 hash = mapped_index_string_hash ((index->version == 4
2120 && case_sensitivity == case_sensitive_off
2121 ? 5 : index->version),
2122 name);
2123
3876f04e
DE
2124 slot = hash & (index->symbol_table_slots - 1);
2125 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2126 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2127
2128 for (;;)
2129 {
2130 /* Convert a slot number to an offset into the table. */
2131 offset_type i = 2 * slot;
2132 const char *str;
3876f04e 2133 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2134 {
2135 do_cleanups (back_to);
2136 return 0;
2137 }
9291a0cd 2138
3876f04e 2139 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2140 if (!cmp (name, str))
9291a0cd
TT
2141 {
2142 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2143 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2144 do_cleanups (back_to);
9291a0cd
TT
2145 return 1;
2146 }
2147
3876f04e 2148 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2149 }
2150}
2151
2152/* Read the index file. If everything went ok, initialize the "quick"
2153 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2154
9291a0cd
TT
2155static int
2156dwarf2_read_index (struct objfile *objfile)
2157{
9291a0cd
TT
2158 char *addr;
2159 struct mapped_index *map;
b3b272e1 2160 offset_type *metadata;
ac0b195c
KW
2161 const gdb_byte *cu_list;
2162 const gdb_byte *types_list = NULL;
2163 offset_type version, cu_list_elements;
2164 offset_type types_list_elements = 0;
1fd400ff 2165 int i;
9291a0cd 2166
9e0ac564 2167 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2168 return 0;
82430852
JK
2169
2170 /* Older elfutils strip versions could keep the section in the main
2171 executable while splitting it for the separate debug info file. */
2172 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2173 & SEC_HAS_CONTENTS) == 0)
2174 return 0;
2175
9291a0cd
TT
2176 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2177
2178 addr = dwarf2_per_objfile->gdb_index.buffer;
2179 /* Version check. */
1fd400ff 2180 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2181 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2182 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2183 contained incomplete addrmap. So, it seems better to just ignore such
559a7a62
JK
2184 indices. Index version 4 uses a different hash function than index
2185 version 5 and later. */
831adc1f 2186 if (version < 4)
9291a0cd 2187 return 0;
594e8718
JK
2188 /* Indexes with higher version than the one supported by GDB may be no
2189 longer backward compatible. */
559a7a62 2190 if (version > 5)
594e8718 2191 return 0;
9291a0cd
TT
2192
2193 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2194 map->version = version;
b3b272e1 2195 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2196
2197 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2198
2199 i = 0;
2200 cu_list = addr + MAYBE_SWAP (metadata[i]);
2201 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2202 / 8);
1fd400ff
TT
2203 ++i;
2204
987d643c
TT
2205 types_list = addr + MAYBE_SWAP (metadata[i]);
2206 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2207 - MAYBE_SWAP (metadata[i]))
2208 / 8);
2209 ++i;
1fd400ff
TT
2210
2211 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2212 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2213 - MAYBE_SWAP (metadata[i]));
2214 ++i;
2215
3876f04e
DE
2216 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2217 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2218 - MAYBE_SWAP (metadata[i]))
2219 / (2 * sizeof (offset_type)));
1fd400ff 2220 ++i;
9291a0cd 2221
1fd400ff
TT
2222 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2223
2224 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2225 return 0;
2226
8b70b953
TT
2227 if (types_list_elements)
2228 {
2229 struct dwarf2_section_info *section;
2230
2231 /* We can only handle a single .debug_types when we have an
2232 index. */
2233 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2234 return 0;
2235
2236 section = VEC_index (dwarf2_section_info_def,
2237 dwarf2_per_objfile->types, 0);
2238
2239 if (!create_signatured_type_table_from_index (objfile, section,
2240 types_list,
2241 types_list_elements))
2242 return 0;
2243 }
9291a0cd
TT
2244
2245 create_addrmap_from_index (objfile, map);
2246
2247 dwarf2_per_objfile->index_table = map;
2248 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2249 dwarf2_per_objfile->quick_file_names_table =
2250 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2251
2252 return 1;
2253}
2254
2255/* A helper for the "quick" functions which sets the global
2256 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2257
9291a0cd
TT
2258static void
2259dw2_setup (struct objfile *objfile)
2260{
2261 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2262 gdb_assert (dwarf2_per_objfile);
2263}
2264
2265/* A helper for the "quick" functions which attempts to read the line
2266 table for THIS_CU. */
2fdf6df6 2267
7b9f3c50
DE
2268static struct quick_file_names *
2269dw2_get_file_names (struct objfile *objfile,
2270 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2271{
2272 bfd *abfd = objfile->obfd;
7b9f3c50 2273 struct line_header *lh;
9291a0cd
TT
2274 struct attribute *attr;
2275 struct cleanup *cleanups;
2276 struct die_info *comp_unit_die;
36374493 2277 struct dwarf2_section_info* sec;
adabb602 2278 gdb_byte *info_ptr, *buffer;
9291a0cd
TT
2279 int has_children, i;
2280 struct dwarf2_cu cu;
2281 unsigned int bytes_read, buffer_size;
2282 struct die_reader_specs reader_specs;
2283 char *name, *comp_dir;
7b9f3c50
DE
2284 void **slot;
2285 struct quick_file_names *qfn;
2286 unsigned int line_offset;
9291a0cd 2287
7b9f3c50
DE
2288 if (this_cu->v.quick->file_names != NULL)
2289 return this_cu->v.quick->file_names;
2290 /* If we know there is no line data, no point in looking again. */
2291 if (this_cu->v.quick->no_file_data)
2292 return NULL;
9291a0cd 2293
9816fde3 2294 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2295 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2296
8b70b953
TT
2297 if (this_cu->debug_type_section)
2298 sec = this_cu->debug_type_section;
36374493
DE
2299 else
2300 sec = &dwarf2_per_objfile->info;
2301 dwarf2_read_section (objfile, sec);
2302 buffer_size = sec->size;
2303 buffer = sec->buffer;
9291a0cd 2304 info_ptr = buffer + this_cu->offset;
9291a0cd
TT
2305
2306 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2307 buffer, buffer_size,
2308 abfd);
2309
9291a0cd
TT
2310 this_cu->cu = &cu;
2311 cu.per_cu = this_cu;
2312
2313 dwarf2_read_abbrevs (abfd, &cu);
2314 make_cleanup (dwarf2_free_abbrev_table, &cu);
2315
8b70b953 2316 if (this_cu->debug_type_section)
9291a0cd
TT
2317 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2318 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2319 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2320 &has_children);
9291a0cd 2321
7b9f3c50
DE
2322 lh = NULL;
2323 slot = NULL;
2324 line_offset = 0;
9291a0cd
TT
2325 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2326 if (attr)
2327 {
7b9f3c50
DE
2328 struct quick_file_names find_entry;
2329
2330 line_offset = DW_UNSND (attr);
2331
2332 /* We may have already read in this line header (TU line header sharing).
2333 If we have we're done. */
2334 find_entry.offset = line_offset;
2335 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2336 &find_entry, INSERT);
2337 if (*slot != NULL)
2338 {
2339 do_cleanups (cleanups);
2340 this_cu->v.quick->file_names = *slot;
2341 return *slot;
2342 }
2343
9291a0cd
TT
2344 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2345 }
2346 if (lh == NULL)
2347 {
2348 do_cleanups (cleanups);
7b9f3c50
DE
2349 this_cu->v.quick->no_file_data = 1;
2350 return NULL;
9291a0cd
TT
2351 }
2352
7b9f3c50
DE
2353 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2354 qfn->offset = line_offset;
2355 gdb_assert (slot != NULL);
2356 *slot = qfn;
9291a0cd 2357
7b9f3c50 2358 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2359
7b9f3c50
DE
2360 qfn->num_file_names = lh->num_file_names;
2361 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2362 lh->num_file_names * sizeof (char *));
9291a0cd 2363 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2364 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2365 qfn->real_names = NULL;
9291a0cd 2366
7b9f3c50 2367 free_line_header (lh);
9291a0cd 2368 do_cleanups (cleanups);
7b9f3c50
DE
2369
2370 this_cu->v.quick->file_names = qfn;
2371 return qfn;
9291a0cd
TT
2372}
2373
2374/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2375 real path for a given file name from the line table. */
2fdf6df6 2376
9291a0cd 2377static const char *
7b9f3c50
DE
2378dw2_get_real_path (struct objfile *objfile,
2379 struct quick_file_names *qfn, int index)
9291a0cd 2380{
7b9f3c50
DE
2381 if (qfn->real_names == NULL)
2382 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2383 qfn->num_file_names, sizeof (char *));
9291a0cd 2384
7b9f3c50
DE
2385 if (qfn->real_names[index] == NULL)
2386 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2387
7b9f3c50 2388 return qfn->real_names[index];
9291a0cd
TT
2389}
2390
2391static struct symtab *
2392dw2_find_last_source_symtab (struct objfile *objfile)
2393{
2394 int index;
ae2de4f8 2395
9291a0cd
TT
2396 dw2_setup (objfile);
2397 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2398 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2399}
2400
7b9f3c50
DE
2401/* Traversal function for dw2_forget_cached_source_info. */
2402
2403static int
2404dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2405{
7b9f3c50 2406 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2407
7b9f3c50 2408 if (file_data->real_names)
9291a0cd 2409 {
7b9f3c50 2410 int i;
9291a0cd 2411
7b9f3c50 2412 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2413 {
7b9f3c50
DE
2414 xfree ((void*) file_data->real_names[i]);
2415 file_data->real_names[i] = NULL;
9291a0cd
TT
2416 }
2417 }
7b9f3c50
DE
2418
2419 return 1;
2420}
2421
2422static void
2423dw2_forget_cached_source_info (struct objfile *objfile)
2424{
2425 dw2_setup (objfile);
2426
2427 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2428 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2429}
2430
2431static int
2432dw2_lookup_symtab (struct objfile *objfile, const char *name,
2433 const char *full_path, const char *real_path,
2434 struct symtab **result)
2435{
2436 int i;
2437 int check_basename = lbasename (name) == name;
2438 struct dwarf2_per_cu_data *base_cu = NULL;
2439
2440 dw2_setup (objfile);
ae2de4f8 2441
1fd400ff
TT
2442 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2443 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2444 {
2445 int j;
e254ef6a 2446 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2447 struct quick_file_names *file_data;
9291a0cd 2448
e254ef6a 2449 if (per_cu->v.quick->symtab)
9291a0cd
TT
2450 continue;
2451
7b9f3c50
DE
2452 file_data = dw2_get_file_names (objfile, per_cu);
2453 if (file_data == NULL)
9291a0cd
TT
2454 continue;
2455
7b9f3c50 2456 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2457 {
7b9f3c50 2458 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2459
2460 if (FILENAME_CMP (name, this_name) == 0)
2461 {
e254ef6a 2462 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2463 return 1;
2464 }
2465
2466 if (check_basename && ! base_cu
2467 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2468 base_cu = per_cu;
9291a0cd
TT
2469
2470 if (full_path != NULL)
2471 {
7b9f3c50
DE
2472 const char *this_real_name = dw2_get_real_path (objfile,
2473 file_data, j);
9291a0cd 2474
7b9f3c50
DE
2475 if (this_real_name != NULL
2476 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2477 {
e254ef6a 2478 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2479 return 1;
2480 }
2481 }
2482
2483 if (real_path != NULL)
2484 {
7b9f3c50
DE
2485 const char *this_real_name = dw2_get_real_path (objfile,
2486 file_data, j);
9291a0cd 2487
7b9f3c50
DE
2488 if (this_real_name != NULL
2489 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2490 {
74dd2ca6
DE
2491 *result = dw2_instantiate_symtab (objfile, per_cu);
2492 return 1;
9291a0cd
TT
2493 }
2494 }
2495 }
2496 }
2497
2498 if (base_cu)
2499 {
2500 *result = dw2_instantiate_symtab (objfile, base_cu);
2501 return 1;
2502 }
2503
2504 return 0;
2505}
2506
2507static struct symtab *
2508dw2_lookup_symbol (struct objfile *objfile, int block_index,
2509 const char *name, domain_enum domain)
2510{
774b6a14 2511 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2512 instead. */
2513 return NULL;
2514}
2515
2516/* A helper function that expands all symtabs that hold an object
2517 named NAME. */
2fdf6df6 2518
9291a0cd
TT
2519static void
2520dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2521{
2522 dw2_setup (objfile);
2523
ae2de4f8 2524 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2525 if (dwarf2_per_objfile->index_table)
2526 {
2527 offset_type *vec;
2528
2529 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2530 name, &vec))
2531 {
2532 offset_type i, len = MAYBE_SWAP (*vec);
2533 for (i = 0; i < len; ++i)
2534 {
2535 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2536 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2537
e254ef6a 2538 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2539 }
2540 }
2541 }
2542}
2543
774b6a14
TT
2544static void
2545dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2546 enum block_enum block_kind, const char *name,
774b6a14 2547 domain_enum domain)
9291a0cd 2548{
774b6a14 2549 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2550}
2551
2552static void
2553dw2_print_stats (struct objfile *objfile)
2554{
2555 int i, count;
2556
2557 dw2_setup (objfile);
2558 count = 0;
1fd400ff
TT
2559 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2560 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2561 {
e254ef6a 2562 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2563
e254ef6a 2564 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2565 ++count;
2566 }
2567 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2568}
2569
2570static void
2571dw2_dump (struct objfile *objfile)
2572{
2573 /* Nothing worth printing. */
2574}
2575
2576static void
2577dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2578 struct section_offsets *delta)
2579{
2580 /* There's nothing to relocate here. */
2581}
2582
2583static void
2584dw2_expand_symtabs_for_function (struct objfile *objfile,
2585 const char *func_name)
2586{
2587 dw2_do_expand_symtabs_matching (objfile, func_name);
2588}
2589
2590static void
2591dw2_expand_all_symtabs (struct objfile *objfile)
2592{
2593 int i;
2594
2595 dw2_setup (objfile);
1fd400ff
TT
2596
2597 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2598 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2599 {
e254ef6a 2600 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2601
e254ef6a 2602 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2603 }
2604}
2605
2606static void
2607dw2_expand_symtabs_with_filename (struct objfile *objfile,
2608 const char *filename)
2609{
2610 int i;
2611
2612 dw2_setup (objfile);
d4637a04
DE
2613
2614 /* We don't need to consider type units here.
2615 This is only called for examining code, e.g. expand_line_sal.
2616 There can be an order of magnitude (or more) more type units
2617 than comp units, and we avoid them if we can. */
2618
2619 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2620 {
2621 int j;
e254ef6a 2622 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2623 struct quick_file_names *file_data;
9291a0cd 2624
e254ef6a 2625 if (per_cu->v.quick->symtab)
9291a0cd
TT
2626 continue;
2627
7b9f3c50
DE
2628 file_data = dw2_get_file_names (objfile, per_cu);
2629 if (file_data == NULL)
9291a0cd
TT
2630 continue;
2631
7b9f3c50 2632 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2633 {
7b9f3c50 2634 const char *this_name = file_data->file_names[j];
1ef75ecc 2635 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2636 {
e254ef6a 2637 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2638 break;
2639 }
2640 }
2641 }
2642}
2643
dd786858 2644static const char *
9291a0cd
TT
2645dw2_find_symbol_file (struct objfile *objfile, const char *name)
2646{
e254ef6a 2647 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2648 offset_type *vec;
7b9f3c50 2649 struct quick_file_names *file_data;
9291a0cd
TT
2650
2651 dw2_setup (objfile);
2652
ae2de4f8 2653 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2654 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2655 {
2656 struct symtab *s;
2657
2658 ALL_OBJFILE_SYMTABS (objfile, s)
2659 if (s->primary)
2660 {
2661 struct blockvector *bv = BLOCKVECTOR (s);
2662 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2663 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2664
2665 if (sym)
2666 return sym->symtab->filename;
2667 }
2668 return NULL;
2669 }
9291a0cd
TT
2670
2671 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2672 name, &vec))
2673 return NULL;
2674
2675 /* Note that this just looks at the very first one named NAME -- but
2676 actually we are looking for a function. find_main_filename
2677 should be rewritten so that it doesn't require a custom hook. It
2678 could just use the ordinary symbol tables. */
2679 /* vec[0] is the length, which must always be >0. */
e254ef6a 2680 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2681
7b9f3c50
DE
2682 file_data = dw2_get_file_names (objfile, per_cu);
2683 if (file_data == NULL)
9291a0cd
TT
2684 return NULL;
2685
7b9f3c50 2686 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2687}
2688
2689static void
40658b94
PH
2690dw2_map_matching_symbols (const char * name, domain_enum namespace,
2691 struct objfile *objfile, int global,
2692 int (*callback) (struct block *,
2693 struct symbol *, void *),
2edb89d3
JK
2694 void *data, symbol_compare_ftype *match,
2695 symbol_compare_ftype *ordered_compare)
9291a0cd 2696{
40658b94 2697 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2698 current language is Ada for a non-Ada objfile using GNU index. As Ada
2699 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2700}
2701
2702static void
2703dw2_expand_symtabs_matching (struct objfile *objfile,
2704 int (*file_matcher) (const char *, void *),
2705 int (*name_matcher) (const char *, void *),
8903c50d 2706 enum search_domain kind,
9291a0cd
TT
2707 void *data)
2708{
2709 int i;
2710 offset_type iter;
4b5246aa 2711 struct mapped_index *index;
9291a0cd
TT
2712
2713 dw2_setup (objfile);
ae2de4f8
DE
2714
2715 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2716 if (!dwarf2_per_objfile->index_table)
2717 return;
4b5246aa 2718 index = dwarf2_per_objfile->index_table;
9291a0cd 2719
7b08b9eb
JK
2720 if (file_matcher != NULL)
2721 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2722 + dwarf2_per_objfile->n_type_comp_units); ++i)
2723 {
2724 int j;
2725 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2726 struct quick_file_names *file_data;
2727
2728 per_cu->v.quick->mark = 0;
2729 if (per_cu->v.quick->symtab)
2730 continue;
2731
2732 file_data = dw2_get_file_names (objfile, per_cu);
2733 if (file_data == NULL)
2734 continue;
2735
2736 for (j = 0; j < file_data->num_file_names; ++j)
2737 {
2738 if (file_matcher (file_data->file_names[j], data))
2739 {
2740 per_cu->v.quick->mark = 1;
2741 break;
2742 }
2743 }
2744 }
9291a0cd 2745
3876f04e 2746 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2747 {
2748 offset_type idx = 2 * iter;
2749 const char *name;
2750 offset_type *vec, vec_len, vec_idx;
2751
3876f04e 2752 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2753 continue;
2754
3876f04e 2755 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2756
2757 if (! (*name_matcher) (name, data))
2758 continue;
2759
2760 /* The name was matched, now expand corresponding CUs that were
2761 marked. */
4b5246aa 2762 vec = (offset_type *) (index->constant_pool
3876f04e 2763 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2764 vec_len = MAYBE_SWAP (vec[0]);
2765 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2766 {
e254ef6a 2767 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2768
e254ef6a 2769 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2770 if (file_matcher == NULL || per_cu->v.quick->mark)
e254ef6a 2771 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2772 }
2773 }
2774}
2775
2776static struct symtab *
2777dw2_find_pc_sect_symtab (struct objfile *objfile,
2778 struct minimal_symbol *msymbol,
2779 CORE_ADDR pc,
2780 struct obj_section *section,
2781 int warn_if_readin)
2782{
2783 struct dwarf2_per_cu_data *data;
2784
2785 dw2_setup (objfile);
2786
2787 if (!objfile->psymtabs_addrmap)
2788 return NULL;
2789
2790 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2791 if (!data)
2792 return NULL;
2793
2794 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2795 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2796 paddress (get_objfile_arch (objfile), pc));
2797
2798 return dw2_instantiate_symtab (objfile, data);
2799}
2800
9291a0cd 2801static void
44b13c5a 2802dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
9291a0cd
TT
2803 void *data)
2804{
2805 int i;
2806
2807 dw2_setup (objfile);
ae2de4f8 2808
1fd400ff
TT
2809 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2810 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2811 {
2812 int j;
e254ef6a 2813 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2814 struct quick_file_names *file_data;
9291a0cd 2815
e254ef6a 2816 if (per_cu->v.quick->symtab)
9291a0cd
TT
2817 continue;
2818
7b9f3c50
DE
2819 file_data = dw2_get_file_names (objfile, per_cu);
2820 if (file_data == NULL)
9291a0cd
TT
2821 continue;
2822
7b9f3c50 2823 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2824 {
7b9f3c50
DE
2825 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2826 j);
2827 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2828 }
2829 }
2830}
2831
2832static int
2833dw2_has_symbols (struct objfile *objfile)
2834{
2835 return 1;
2836}
2837
2838const struct quick_symbol_functions dwarf2_gdb_index_functions =
2839{
2840 dw2_has_symbols,
2841 dw2_find_last_source_symtab,
2842 dw2_forget_cached_source_info,
2843 dw2_lookup_symtab,
2844 dw2_lookup_symbol,
774b6a14 2845 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2846 dw2_print_stats,
2847 dw2_dump,
2848 dw2_relocate,
2849 dw2_expand_symtabs_for_function,
2850 dw2_expand_all_symtabs,
2851 dw2_expand_symtabs_with_filename,
2852 dw2_find_symbol_file,
40658b94 2853 dw2_map_matching_symbols,
9291a0cd
TT
2854 dw2_expand_symtabs_matching,
2855 dw2_find_pc_sect_symtab,
9291a0cd
TT
2856 dw2_map_symbol_filenames
2857};
2858
2859/* Initialize for reading DWARF for this objfile. Return 0 if this
2860 file will use psymtabs, or 1 if using the GNU index. */
2861
2862int
2863dwarf2_initialize_objfile (struct objfile *objfile)
2864{
2865 /* If we're about to read full symbols, don't bother with the
2866 indices. In this case we also don't care if some other debug
2867 format is making psymtabs, because they are all about to be
2868 expanded anyway. */
2869 if ((objfile->flags & OBJF_READNOW))
2870 {
2871 int i;
2872
2873 dwarf2_per_objfile->using_index = 1;
2874 create_all_comp_units (objfile);
1fd400ff 2875 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2876 dwarf2_per_objfile->quick_file_names_table =
2877 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2878
1fd400ff
TT
2879 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2880 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2881 {
e254ef6a 2882 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2883
e254ef6a
DE
2884 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2885 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2886 }
2887
2888 /* Return 1 so that gdb sees the "quick" functions. However,
2889 these functions will be no-ops because we will have expanded
2890 all symtabs. */
2891 return 1;
2892 }
2893
2894 if (dwarf2_read_index (objfile))
2895 return 1;
2896
9291a0cd
TT
2897 return 0;
2898}
2899
2900\f
2901
dce234bc
PP
2902/* Build a partial symbol table. */
2903
2904void
f29dff0a 2905dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2906{
f29dff0a 2907 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2908 {
2909 init_psymbol_list (objfile, 1024);
2910 }
2911
d146bf1e 2912 dwarf2_build_psymtabs_hard (objfile);
c906108c 2913}
c906108c 2914
45452591
DE
2915/* Return TRUE if OFFSET is within CU_HEADER. */
2916
2917static inline int
2918offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2919{
2920 unsigned int bottom = cu_header->offset;
2921 unsigned int top = (cu_header->offset
2922 + cu_header->length
2923 + cu_header->initial_length_size);
9a619af0 2924
45452591
DE
2925 return (offset >= bottom && offset < top);
2926}
2927
93311388
DE
2928/* Read in the comp unit header information from the debug_info at info_ptr.
2929 NOTE: This leaves members offset, first_die_offset to be filled in
2930 by the caller. */
107d2387 2931
fe1b8b76 2932static gdb_byte *
107d2387 2933read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2934 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2935{
2936 int signed_addr;
891d2f0b 2937 unsigned int bytes_read;
c764a876
DE
2938
2939 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2940 cu_header->initial_length_size = bytes_read;
2941 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2942 info_ptr += bytes_read;
107d2387
AC
2943 cu_header->version = read_2_bytes (abfd, info_ptr);
2944 info_ptr += 2;
613e1657 2945 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2946 &bytes_read);
613e1657 2947 info_ptr += bytes_read;
107d2387
AC
2948 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2949 info_ptr += 1;
2950 signed_addr = bfd_get_sign_extend_vma (abfd);
2951 if (signed_addr < 0)
8e65ff28 2952 internal_error (__FILE__, __LINE__,
e2e0b3e5 2953 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2954 cu_header->signed_addr_p = signed_addr;
c764a876 2955
107d2387
AC
2956 return info_ptr;
2957}
2958
adabb602
DE
2959/* Read in a CU header and perform some basic error checking. */
2960
fe1b8b76
JB
2961static gdb_byte *
2962partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2963 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2964 bfd *abfd)
2965{
fe1b8b76 2966 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 2967
adabb602
DE
2968 header->offset = beg_of_comp_unit - buffer;
2969
72bf9492
DJ
2970 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2971
adabb602
DE
2972 header->first_die_offset = info_ptr - beg_of_comp_unit;
2973
2dc7f7b3 2974 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2975 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2976 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2977 bfd_get_filename (abfd));
72bf9492 2978
9e0ac564
TT
2979 if (header->abbrev_offset
2980 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2981 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
2982 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2983 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2984 (long) header->abbrev_offset,
93311388 2985 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2986 bfd_get_filename (abfd));
2987
2988 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2989 > buffer + buffer_size)
8a3fe4f8
AC
2990 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2991 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2992 (long) header->length,
93311388 2993 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2994 bfd_get_filename (abfd));
2995
2996 return info_ptr;
2997}
2998
348e048f
DE
2999/* Read in the types comp unit header information from .debug_types entry at
3000 types_ptr. The result is a pointer to one past the end of the header. */
3001
3002static gdb_byte *
3003read_type_comp_unit_head (struct comp_unit_head *cu_header,
8b70b953 3004 struct dwarf2_section_info *section,
348e048f
DE
3005 ULONGEST *signature,
3006 gdb_byte *types_ptr, bfd *abfd)
3007{
348e048f
DE
3008 gdb_byte *initial_types_ptr = types_ptr;
3009
8b70b953
TT
3010 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3011 cu_header->offset = types_ptr - section->buffer;
348e048f
DE
3012
3013 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3014
3015 *signature = read_8_bytes (abfd, types_ptr);
3016 types_ptr += 8;
3017 types_ptr += cu_header->offset_size;
3018 cu_header->first_die_offset = types_ptr - initial_types_ptr;
3019
3020 return types_ptr;
3021}
3022
aaa75496
JB
3023/* Allocate a new partial symtab for file named NAME and mark this new
3024 partial symtab as being an include of PST. */
3025
3026static void
3027dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3028 struct objfile *objfile)
3029{
3030 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3031
3032 subpst->section_offsets = pst->section_offsets;
3033 subpst->textlow = 0;
3034 subpst->texthigh = 0;
3035
3036 subpst->dependencies = (struct partial_symtab **)
3037 obstack_alloc (&objfile->objfile_obstack,
3038 sizeof (struct partial_symtab *));
3039 subpst->dependencies[0] = pst;
3040 subpst->number_of_dependencies = 1;
3041
3042 subpst->globals_offset = 0;
3043 subpst->n_global_syms = 0;
3044 subpst->statics_offset = 0;
3045 subpst->n_static_syms = 0;
3046 subpst->symtab = NULL;
3047 subpst->read_symtab = pst->read_symtab;
3048 subpst->readin = 0;
3049
3050 /* No private part is necessary for include psymtabs. This property
3051 can be used to differentiate between such include psymtabs and
10b3939b 3052 the regular ones. */
58a9656e 3053 subpst->read_symtab_private = NULL;
aaa75496
JB
3054}
3055
3056/* Read the Line Number Program data and extract the list of files
3057 included by the source file represented by PST. Build an include
d85a05f0 3058 partial symtab for each of these included files. */
aaa75496
JB
3059
3060static void
3061dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 3062 struct die_info *die,
aaa75496
JB
3063 struct partial_symtab *pst)
3064{
3065 struct objfile *objfile = cu->objfile;
3066 bfd *abfd = objfile->obfd;
d85a05f0
DJ
3067 struct line_header *lh = NULL;
3068 struct attribute *attr;
aaa75496 3069
d85a05f0
DJ
3070 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3071 if (attr)
3072 {
3073 unsigned int line_offset = DW_UNSND (attr);
9a619af0 3074
d85a05f0
DJ
3075 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3076 }
aaa75496
JB
3077 if (lh == NULL)
3078 return; /* No linetable, so no includes. */
3079
c6da4cef
DE
3080 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3081 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
3082
3083 free_line_header (lh);
3084}
3085
348e048f
DE
3086static hashval_t
3087hash_type_signature (const void *item)
3088{
3089 const struct signatured_type *type_sig = item;
9a619af0 3090
348e048f
DE
3091 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3092 return type_sig->signature;
3093}
3094
3095static int
3096eq_type_signature (const void *item_lhs, const void *item_rhs)
3097{
3098 const struct signatured_type *lhs = item_lhs;
3099 const struct signatured_type *rhs = item_rhs;
9a619af0 3100
348e048f
DE
3101 return lhs->signature == rhs->signature;
3102}
3103
1fd400ff
TT
3104/* Allocate a hash table for signatured types. */
3105
3106static htab_t
673bfd45 3107allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3108{
3109 return htab_create_alloc_ex (41,
3110 hash_type_signature,
3111 eq_type_signature,
3112 NULL,
3113 &objfile->objfile_obstack,
3114 hashtab_obstack_allocate,
3115 dummy_obstack_deallocate);
3116}
3117
3118/* A helper function to add a signatured type CU to a list. */
3119
3120static int
3121add_signatured_type_cu_to_list (void **slot, void *datum)
3122{
3123 struct signatured_type *sigt = *slot;
3124 struct dwarf2_per_cu_data ***datap = datum;
3125
3126 **datap = &sigt->per_cu;
3127 ++*datap;
3128
3129 return 1;
3130}
3131
348e048f
DE
3132/* Create the hash table of all entries in the .debug_types section.
3133 The result is zero if there is an error (e.g. missing .debug_types section),
3134 otherwise non-zero. */
3135
3136static int
3137create_debug_types_hash_table (struct objfile *objfile)
3138{
8b70b953 3139 htab_t types_htab = NULL;
1fd400ff 3140 struct dwarf2_per_cu_data **iter;
8b70b953
TT
3141 int ix;
3142 struct dwarf2_section_info *section;
348e048f 3143
8b70b953 3144 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
348e048f
DE
3145 {
3146 dwarf2_per_objfile->signatured_types = NULL;
3147 return 0;
3148 }
3149
8b70b953
TT
3150 for (ix = 0;
3151 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3152 ix, section);
3153 ++ix)
3154 {
3155 gdb_byte *info_ptr, *end_ptr;
348e048f 3156
8b70b953
TT
3157 dwarf2_read_section (objfile, section);
3158 info_ptr = section->buffer;
348e048f 3159
8b70b953
TT
3160 if (info_ptr == NULL)
3161 continue;
348e048f 3162
8b70b953
TT
3163 if (types_htab == NULL)
3164 types_htab = allocate_signatured_type_table (objfile);
348e048f 3165
8b70b953
TT
3166 if (dwarf2_die_debug)
3167 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3168
3169 end_ptr = info_ptr + section->size;
3170 while (info_ptr < end_ptr)
3171 {
3172 unsigned int offset;
3173 unsigned int offset_size;
3174 unsigned int type_offset;
3175 unsigned int length, initial_length_size;
3176 unsigned short version;
3177 ULONGEST signature;
3178 struct signatured_type *type_sig;
3179 void **slot;
3180 gdb_byte *ptr = info_ptr;
348e048f 3181
8b70b953 3182 offset = ptr - section->buffer;
348e048f 3183
8b70b953
TT
3184 /* We need to read the type's signature in order to build the hash
3185 table, but we don't need to read anything else just yet. */
348e048f 3186
8b70b953
TT
3187 /* Sanity check to ensure entire cu is present. */
3188 length = read_initial_length (objfile->obfd, ptr,
3189 &initial_length_size);
3190 if (ptr + length + initial_length_size > end_ptr)
3191 {
3192 complaint (&symfile_complaints,
3193 _("debug type entry runs off end "
3194 "of `.debug_types' section, ignored"));
3195 break;
3196 }
348e048f 3197
8b70b953
TT
3198 offset_size = initial_length_size == 4 ? 4 : 8;
3199 ptr += initial_length_size;
3200 version = bfd_get_16 (objfile->obfd, ptr);
3201 ptr += 2;
3202 ptr += offset_size; /* abbrev offset */
3203 ptr += 1; /* address size */
3204 signature = bfd_get_64 (objfile->obfd, ptr);
3205 ptr += 8;
3206 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3207
3208 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3209 memset (type_sig, 0, sizeof (*type_sig));
3210 type_sig->signature = signature;
3211 type_sig->type_offset = type_offset;
3212 type_sig->per_cu.objfile = objfile;
3213 type_sig->per_cu.debug_type_section = section;
3214 type_sig->per_cu.offset = offset;
3215
3216 slot = htab_find_slot (types_htab, type_sig, INSERT);
3217 gdb_assert (slot != NULL);
3218 if (*slot != NULL)
3219 {
3220 const struct signatured_type *dup_sig = *slot;
b3c8eb43 3221
8b70b953
TT
3222 complaint (&symfile_complaints,
3223 _("debug type entry at offset 0x%x is duplicate to the "
3224 "entry at offset 0x%x, signature 0x%s"),
3225 offset, dup_sig->per_cu.offset,
3226 phex (signature, sizeof (signature)));
3227 gdb_assert (signature == dup_sig->signature);
3228 }
3229 *slot = type_sig;
348e048f 3230
8b70b953
TT
3231 if (dwarf2_die_debug)
3232 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3233 offset, phex (signature, sizeof (signature)));
348e048f 3234
8b70b953
TT
3235 info_ptr = info_ptr + initial_length_size + length;
3236 }
348e048f
DE
3237 }
3238
3239 dwarf2_per_objfile->signatured_types = types_htab;
3240
1fd400ff
TT
3241 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3242 dwarf2_per_objfile->type_comp_units
3243 = obstack_alloc (&objfile->objfile_obstack,
3244 dwarf2_per_objfile->n_type_comp_units
3245 * sizeof (struct dwarf2_per_cu_data *));
3246 iter = &dwarf2_per_objfile->type_comp_units[0];
3247 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3248 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3249 == dwarf2_per_objfile->n_type_comp_units);
3250
348e048f
DE
3251 return 1;
3252}
3253
3254/* Lookup a signature based type.
3255 Returns NULL if SIG is not present in the table. */
3256
3257static struct signatured_type *
3258lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3259{
3260 struct signatured_type find_entry, *entry;
3261
3262 if (dwarf2_per_objfile->signatured_types == NULL)
3263 {
3264 complaint (&symfile_complaints,
55f1336d 3265 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3266 return 0;
3267 }
3268
3269 find_entry.signature = sig;
3270 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3271 return entry;
3272}
3273
d85a05f0
DJ
3274/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3275
3276static void
3277init_cu_die_reader (struct die_reader_specs *reader,
3278 struct dwarf2_cu *cu)
3279{
3280 reader->abfd = cu->objfile->obfd;
3281 reader->cu = cu;
8b70b953 3282 if (cu->per_cu->debug_type_section)
be391dca 3283 {
8b70b953
TT
3284 gdb_assert (cu->per_cu->debug_type_section->readin);
3285 reader->buffer = cu->per_cu->debug_type_section->buffer;
be391dca 3286 }
d85a05f0 3287 else
be391dca
TT
3288 {
3289 gdb_assert (dwarf2_per_objfile->info.readin);
3290 reader->buffer = dwarf2_per_objfile->info.buffer;
3291 }
d85a05f0
DJ
3292}
3293
3294/* Find the base address of the compilation unit for range lists and
3295 location lists. It will normally be specified by DW_AT_low_pc.
3296 In DWARF-3 draft 4, the base address could be overridden by
3297 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3298 compilation units with discontinuous ranges. */
3299
3300static void
3301dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3302{
3303 struct attribute *attr;
3304
3305 cu->base_known = 0;
3306 cu->base_address = 0;
3307
3308 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3309 if (attr)
3310 {
3311 cu->base_address = DW_ADDR (attr);
3312 cu->base_known = 1;
3313 }
3314 else
3315 {
3316 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3317 if (attr)
3318 {
3319 cu->base_address = DW_ADDR (attr);
3320 cu->base_known = 1;
3321 }
3322 }
3323}
3324
348e048f
DE
3325/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3326 to combine the common parts.
93311388 3327 Process a compilation unit for a psymtab.
348e048f
DE
3328 BUFFER is a pointer to the beginning of the dwarf section buffer,
3329 either .debug_info or debug_types.
93311388
DE
3330 INFO_PTR is a pointer to the start of the CU.
3331 Returns a pointer to the next CU. */
aaa75496 3332
93311388
DE
3333static gdb_byte *
3334process_psymtab_comp_unit (struct objfile *objfile,
3335 struct dwarf2_per_cu_data *this_cu,
3336 gdb_byte *buffer, gdb_byte *info_ptr,
3337 unsigned int buffer_size)
c906108c 3338{
c906108c 3339 bfd *abfd = objfile->obfd;
93311388 3340 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3341 struct die_info *comp_unit_die;
c906108c 3342 struct partial_symtab *pst;
5734ee8b 3343 CORE_ADDR baseaddr;
93311388
DE
3344 struct cleanup *back_to_inner;
3345 struct dwarf2_cu cu;
d85a05f0
DJ
3346 int has_children, has_pc_info;
3347 struct attribute *attr;
d85a05f0
DJ
3348 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3349 struct die_reader_specs reader_specs;
3e2a0cee 3350 const char *filename;
c906108c 3351
9816fde3 3352 init_one_comp_unit (&cu, objfile);
93311388 3353 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3354
93311388
DE
3355 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3356 buffer, buffer_size,
3357 abfd);
10b3939b 3358
93311388 3359 cu.list_in_scope = &file_symbols;
af703f96 3360
328c9494
DJ
3361 /* If this compilation unit was already read in, free the
3362 cached copy in order to read it in again. This is
3363 necessary because we skipped some symbols when we first
3364 read in the compilation unit (see load_partial_dies).
3365 This problem could be avoided, but the benefit is
3366 unclear. */
3367 if (this_cu->cu != NULL)
3368 free_one_cached_comp_unit (this_cu->cu);
3369
3370 /* Note that this is a pointer to our stack frame, being
3371 added to a global data structure. It will be cleaned up
3372 in free_stack_comp_unit when we finish with this
3373 compilation unit. */
3374 this_cu->cu = &cu;
d85a05f0
DJ
3375 cu.per_cu = this_cu;
3376
93311388
DE
3377 /* Read the abbrevs for this compilation unit into a table. */
3378 dwarf2_read_abbrevs (abfd, &cu);
3379 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3380
93311388 3381 /* Read the compilation unit die. */
8b70b953 3382 if (this_cu->debug_type_section)
348e048f 3383 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3384 init_cu_die_reader (&reader_specs, &cu);
3385 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3386 &has_children);
93311388 3387
8b70b953 3388 if (this_cu->debug_type_section)
348e048f 3389 {
b3c8eb43
JK
3390 /* LENGTH has not been set yet for type units. */
3391 gdb_assert (this_cu->offset == cu.header.offset);
348e048f
DE
3392 this_cu->length = cu.header.length + cu.header.initial_length_size;
3393 }
d85a05f0 3394 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3395 {
93311388
DE
3396 info_ptr = (beg_of_comp_unit + cu.header.length
3397 + cu.header.initial_length_size);
3398 do_cleanups (back_to_inner);
3399 return info_ptr;
3400 }
72bf9492 3401
9816fde3 3402 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3403
93311388 3404 /* Allocate a new partial symbol table structure. */
d85a05f0 3405 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3406 if (attr == NULL || !DW_STRING (attr))
3407 filename = "";
3408 else
3409 filename = DW_STRING (attr);
93311388 3410 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3411 filename,
93311388
DE
3412 /* TEXTLOW and TEXTHIGH are set below. */
3413 0,
3414 objfile->global_psymbols.next,
3415 objfile->static_psymbols.next);
72bf9492 3416
d85a05f0
DJ
3417 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3418 if (attr != NULL)
3419 pst->dirname = DW_STRING (attr);
72bf9492 3420
e38df1d0 3421 pst->read_symtab_private = this_cu;
72bf9492 3422
93311388 3423 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3424
0963b4bd 3425 /* Store the function that reads in the rest of the symbol table. */
93311388 3426 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3427
9291a0cd 3428 this_cu->v.psymtab = pst;
c906108c 3429
d85a05f0
DJ
3430 dwarf2_find_base_address (comp_unit_die, &cu);
3431
93311388
DE
3432 /* Possibly set the default values of LOWPC and HIGHPC from
3433 `DW_AT_ranges'. */
d85a05f0
DJ
3434 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3435 &best_highpc, &cu, pst);
3436 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3437 /* Store the contiguous range if it is not empty; it can be empty for
3438 CUs with no code. */
3439 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3440 best_lowpc + baseaddr,
3441 best_highpc + baseaddr - 1, pst);
93311388
DE
3442
3443 /* Check if comp unit has_children.
3444 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3445 If not, there's no more debug_info for this comp unit. */
d85a05f0 3446 if (has_children)
93311388
DE
3447 {
3448 struct partial_die_info *first_die;
3449 CORE_ADDR lowpc, highpc;
31ffec48 3450
93311388
DE
3451 lowpc = ((CORE_ADDR) -1);
3452 highpc = ((CORE_ADDR) 0);
c906108c 3453
93311388 3454 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3455
93311388 3456 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3457 ! has_pc_info, &cu);
57c22c6c 3458
93311388
DE
3459 /* If we didn't find a lowpc, set it to highpc to avoid
3460 complaints from `maint check'. */
3461 if (lowpc == ((CORE_ADDR) -1))
3462 lowpc = highpc;
10b3939b 3463
93311388
DE
3464 /* If the compilation unit didn't have an explicit address range,
3465 then use the information extracted from its child dies. */
d85a05f0 3466 if (! has_pc_info)
93311388 3467 {
d85a05f0
DJ
3468 best_lowpc = lowpc;
3469 best_highpc = highpc;
93311388
DE
3470 }
3471 }
d85a05f0
DJ
3472 pst->textlow = best_lowpc + baseaddr;
3473 pst->texthigh = best_highpc + baseaddr;
c906108c 3474
93311388
DE
3475 pst->n_global_syms = objfile->global_psymbols.next -
3476 (objfile->global_psymbols.list + pst->globals_offset);
3477 pst->n_static_syms = objfile->static_psymbols.next -
3478 (objfile->static_psymbols.list + pst->statics_offset);
3479 sort_pst_symbols (pst);
c906108c 3480
93311388
DE
3481 info_ptr = (beg_of_comp_unit + cu.header.length
3482 + cu.header.initial_length_size);
ae038cb0 3483
8b70b953 3484 if (this_cu->debug_type_section)
348e048f
DE
3485 {
3486 /* It's not clear we want to do anything with stmt lists here.
3487 Waiting to see what gcc ultimately does. */
3488 }
d85a05f0 3489 else
93311388
DE
3490 {
3491 /* Get the list of files included in the current compilation unit,
3492 and build a psymtab for each of them. */
d85a05f0 3493 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3494 }
ae038cb0 3495
93311388 3496 do_cleanups (back_to_inner);
ae038cb0 3497
93311388
DE
3498 return info_ptr;
3499}
ff013f42 3500
348e048f
DE
3501/* Traversal function for htab_traverse_noresize.
3502 Process one .debug_types comp-unit. */
3503
3504static int
3505process_type_comp_unit (void **slot, void *info)
3506{
3507 struct signatured_type *entry = (struct signatured_type *) *slot;
3508 struct objfile *objfile = (struct objfile *) info;
3509 struct dwarf2_per_cu_data *this_cu;
3510
3511 this_cu = &entry->per_cu;
348e048f 3512
8b70b953 3513 gdb_assert (this_cu->debug_type_section->readin);
348e048f 3514 process_psymtab_comp_unit (objfile, this_cu,
8b70b953
TT
3515 this_cu->debug_type_section->buffer,
3516 (this_cu->debug_type_section->buffer
3517 + this_cu->offset),
3518 this_cu->debug_type_section->size);
348e048f
DE
3519
3520 return 1;
3521}
3522
3523/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3524 Build partial symbol tables for the .debug_types comp-units. */
3525
3526static void
3527build_type_psymtabs (struct objfile *objfile)
3528{
3529 if (! create_debug_types_hash_table (objfile))
3530 return;
3531
3532 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3533 process_type_comp_unit, objfile);
3534}
3535
60606b2c
TT
3536/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3537
3538static void
3539psymtabs_addrmap_cleanup (void *o)
3540{
3541 struct objfile *objfile = o;
ec61707d 3542
60606b2c
TT
3543 objfile->psymtabs_addrmap = NULL;
3544}
3545
93311388
DE
3546/* Build the partial symbol table by doing a quick pass through the
3547 .debug_info and .debug_abbrev sections. */
72bf9492 3548
93311388 3549static void
c67a9c90 3550dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3551{
93311388 3552 gdb_byte *info_ptr;
60606b2c
TT
3553 struct cleanup *back_to, *addrmap_cleanup;
3554 struct obstack temp_obstack;
93311388 3555
98bfdba5
PA
3556 dwarf2_per_objfile->reading_partial_symbols = 1;
3557
be391dca 3558 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3559 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3560
93311388
DE
3561 /* Any cached compilation units will be linked by the per-objfile
3562 read_in_chain. Make sure to free them when we're done. */
3563 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3564
348e048f
DE
3565 build_type_psymtabs (objfile);
3566
93311388 3567 create_all_comp_units (objfile);
c906108c 3568
60606b2c
TT
3569 /* Create a temporary address map on a temporary obstack. We later
3570 copy this to the final obstack. */
3571 obstack_init (&temp_obstack);
3572 make_cleanup_obstack_free (&temp_obstack);
3573 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3574 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3575
93311388
DE
3576 /* Since the objects we're extracting from .debug_info vary in
3577 length, only the individual functions to extract them (like
3578 read_comp_unit_head and load_partial_die) can really know whether
3579 the buffer is large enough to hold another complete object.
c906108c 3580
93311388
DE
3581 At the moment, they don't actually check that. If .debug_info
3582 holds just one extra byte after the last compilation unit's dies,
3583 then read_comp_unit_head will happily read off the end of the
3584 buffer. read_partial_die is similarly casual. Those functions
3585 should be fixed.
c906108c 3586
93311388
DE
3587 For this loop condition, simply checking whether there's any data
3588 left at all should be sufficient. */
c906108c 3589
93311388
DE
3590 while (info_ptr < (dwarf2_per_objfile->info.buffer
3591 + dwarf2_per_objfile->info.size))
3592 {
3593 struct dwarf2_per_cu_data *this_cu;
dd373385 3594
3e43a32a
MS
3595 this_cu = dwarf2_find_comp_unit (info_ptr
3596 - dwarf2_per_objfile->info.buffer,
93311388 3597 objfile);
aaa75496 3598
93311388
DE
3599 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3600 dwarf2_per_objfile->info.buffer,
3601 info_ptr,
3602 dwarf2_per_objfile->info.size);
c906108c 3603 }
ff013f42
JK
3604
3605 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3606 &objfile->objfile_obstack);
60606b2c 3607 discard_cleanups (addrmap_cleanup);
ff013f42 3608
ae038cb0
DJ
3609 do_cleanups (back_to);
3610}
3611
93311388 3612/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3613
3614static void
93311388
DE
3615load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3616 struct objfile *objfile)
ae038cb0
DJ
3617{
3618 bfd *abfd = objfile->obfd;
adabb602 3619 gdb_byte *info_ptr;
d85a05f0 3620 struct die_info *comp_unit_die;
ae038cb0 3621 struct dwarf2_cu *cu;
1d9ec526 3622 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3623 int has_children;
3624 struct die_reader_specs reader_specs;
98bfdba5 3625 int read_cu = 0;
ae038cb0 3626
8b70b953 3627 gdb_assert (! this_cu->debug_type_section);
348e048f 3628
be391dca 3629 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3630 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0 3631
98bfdba5
PA
3632 if (this_cu->cu == NULL)
3633 {
9816fde3
JK
3634 cu = xmalloc (sizeof (*cu));
3635 init_one_comp_unit (cu, objfile);
ae038cb0 3636
98bfdba5 3637 read_cu = 1;
ae038cb0 3638
98bfdba5
PA
3639 /* If an error occurs while loading, release our storage. */
3640 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3641
98bfdba5
PA
3642 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3643 dwarf2_per_objfile->info.buffer,
3644 dwarf2_per_objfile->info.size,
3645 abfd);
ae038cb0 3646
98bfdba5
PA
3647 /* Link this compilation unit into the compilation unit tree. */
3648 this_cu->cu = cu;
3649 cu->per_cu = this_cu;
98bfdba5
PA
3650
3651 /* Link this CU into read_in_chain. */
3652 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3653 dwarf2_per_objfile->read_in_chain = this_cu;
3654 }
3655 else
3656 {
3657 cu = this_cu->cu;
3658 info_ptr += cu->header.first_die_offset;
3659 }
ae038cb0
DJ
3660
3661 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3662 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3663 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3664 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3665
3666 /* Read the compilation unit die. */
d85a05f0
DJ
3667 init_cu_die_reader (&reader_specs, cu);
3668 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3669 &has_children);
ae038cb0 3670
9816fde3 3671 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3672
ae038cb0
DJ
3673 /* Check if comp unit has_children.
3674 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3675 If not, there's no more debug_info for this comp unit. */
d85a05f0 3676 if (has_children)
93311388 3677 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3678
98bfdba5
PA
3679 do_cleanups (free_abbrevs_cleanup);
3680
3681 if (read_cu)
3682 {
3683 /* We've successfully allocated this compilation unit. Let our
3684 caller clean it up when finished with it. */
3685 discard_cleanups (free_cu_cleanup);
3686 }
ae038cb0
DJ
3687}
3688
3689/* Create a list of all compilation units in OBJFILE. We do this only
3690 if an inter-comp-unit reference is found; presumably if there is one,
3691 there will be many, and one will occur early in the .debug_info section.
3692 So there's no point in building this list incrementally. */
3693
3694static void
3695create_all_comp_units (struct objfile *objfile)
3696{
3697 int n_allocated;
3698 int n_comp_units;
3699 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3700 gdb_byte *info_ptr;
3701
3702 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3703 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3704
3705 n_comp_units = 0;
3706 n_allocated = 10;
3707 all_comp_units = xmalloc (n_allocated
3708 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3709
3e43a32a
MS
3710 while (info_ptr < dwarf2_per_objfile->info.buffer
3711 + dwarf2_per_objfile->info.size)
ae038cb0 3712 {
c764a876 3713 unsigned int length, initial_length_size;
ae038cb0 3714 struct dwarf2_per_cu_data *this_cu;
c764a876 3715 unsigned int offset;
ae038cb0 3716
dce234bc 3717 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3718
3719 /* Read just enough information to find out where the next
3720 compilation unit is. */
c764a876
DE
3721 length = read_initial_length (objfile->obfd, info_ptr,
3722 &initial_length_size);
ae038cb0
DJ
3723
3724 /* Save the compilation unit for later lookup. */
3725 this_cu = obstack_alloc (&objfile->objfile_obstack,
3726 sizeof (struct dwarf2_per_cu_data));
3727 memset (this_cu, 0, sizeof (*this_cu));
3728 this_cu->offset = offset;
c764a876 3729 this_cu->length = length + initial_length_size;
9291a0cd 3730 this_cu->objfile = objfile;
ae038cb0
DJ
3731
3732 if (n_comp_units == n_allocated)
3733 {
3734 n_allocated *= 2;
3735 all_comp_units = xrealloc (all_comp_units,
3736 n_allocated
3737 * sizeof (struct dwarf2_per_cu_data *));
3738 }
3739 all_comp_units[n_comp_units++] = this_cu;
3740
3741 info_ptr = info_ptr + this_cu->length;
3742 }
3743
3744 dwarf2_per_objfile->all_comp_units
3745 = obstack_alloc (&objfile->objfile_obstack,
3746 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3747 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3748 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3749 xfree (all_comp_units);
3750 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3751}
3752
5734ee8b
DJ
3753/* Process all loaded DIEs for compilation unit CU, starting at
3754 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3755 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3756 DW_AT_ranges). If NEED_PC is set, then this function will set
3757 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3758 and record the covered ranges in the addrmap. */
c906108c 3759
72bf9492
DJ
3760static void
3761scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3762 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3763{
72bf9492 3764 struct partial_die_info *pdi;
c906108c 3765
91c24f0a
DC
3766 /* Now, march along the PDI's, descending into ones which have
3767 interesting children but skipping the children of the other ones,
3768 until we reach the end of the compilation unit. */
c906108c 3769
72bf9492 3770 pdi = first_die;
91c24f0a 3771
72bf9492
DJ
3772 while (pdi != NULL)
3773 {
3774 fixup_partial_die (pdi, cu);
c906108c 3775
f55ee35c 3776 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3777 children, so we need to look at them. Ditto for anonymous
3778 enums. */
933c6fe4 3779
72bf9492 3780 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3781 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3782 {
72bf9492 3783 switch (pdi->tag)
c906108c
SS
3784 {
3785 case DW_TAG_subprogram:
5734ee8b 3786 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3787 break;
72929c62 3788 case DW_TAG_constant:
c906108c
SS
3789 case DW_TAG_variable:
3790 case DW_TAG_typedef:
91c24f0a 3791 case DW_TAG_union_type:
72bf9492 3792 if (!pdi->is_declaration)
63d06c5c 3793 {
72bf9492 3794 add_partial_symbol (pdi, cu);
63d06c5c
DC
3795 }
3796 break;
c906108c 3797 case DW_TAG_class_type:
680b30c7 3798 case DW_TAG_interface_type:
c906108c 3799 case DW_TAG_structure_type:
72bf9492 3800 if (!pdi->is_declaration)
c906108c 3801 {
72bf9492 3802 add_partial_symbol (pdi, cu);
c906108c
SS
3803 }
3804 break;
91c24f0a 3805 case DW_TAG_enumeration_type:
72bf9492
DJ
3806 if (!pdi->is_declaration)
3807 add_partial_enumeration (pdi, cu);
c906108c
SS
3808 break;
3809 case DW_TAG_base_type:
a02abb62 3810 case DW_TAG_subrange_type:
c906108c 3811 /* File scope base type definitions are added to the partial
c5aa993b 3812 symbol table. */
72bf9492 3813 add_partial_symbol (pdi, cu);
c906108c 3814 break;
d9fa45fe 3815 case DW_TAG_namespace:
5734ee8b 3816 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3817 break;
5d7cb8df
JK
3818 case DW_TAG_module:
3819 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3820 break;
c906108c
SS
3821 default:
3822 break;
3823 }
3824 }
3825
72bf9492
DJ
3826 /* If the die has a sibling, skip to the sibling. */
3827
3828 pdi = pdi->die_sibling;
3829 }
3830}
3831
3832/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3833
72bf9492 3834 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3835 name is concatenated with "::" and the partial DIE's name. For
3836 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3837 Enumerators are an exception; they use the scope of their parent
3838 enumeration type, i.e. the name of the enumeration type is not
3839 prepended to the enumerator.
91c24f0a 3840
72bf9492
DJ
3841 There are two complexities. One is DW_AT_specification; in this
3842 case "parent" means the parent of the target of the specification,
3843 instead of the direct parent of the DIE. The other is compilers
3844 which do not emit DW_TAG_namespace; in this case we try to guess
3845 the fully qualified name of structure types from their members'
3846 linkage names. This must be done using the DIE's children rather
3847 than the children of any DW_AT_specification target. We only need
3848 to do this for structures at the top level, i.e. if the target of
3849 any DW_AT_specification (if any; otherwise the DIE itself) does not
3850 have a parent. */
3851
3852/* Compute the scope prefix associated with PDI's parent, in
3853 compilation unit CU. The result will be allocated on CU's
3854 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3855 field. NULL is returned if no prefix is necessary. */
3856static char *
3857partial_die_parent_scope (struct partial_die_info *pdi,
3858 struct dwarf2_cu *cu)
3859{
3860 char *grandparent_scope;
3861 struct partial_die_info *parent, *real_pdi;
91c24f0a 3862
72bf9492
DJ
3863 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3864 then this means the parent of the specification DIE. */
3865
3866 real_pdi = pdi;
72bf9492 3867 while (real_pdi->has_specification)
10b3939b 3868 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3869
3870 parent = real_pdi->die_parent;
3871 if (parent == NULL)
3872 return NULL;
3873
3874 if (parent->scope_set)
3875 return parent->scope;
3876
3877 fixup_partial_die (parent, cu);
3878
10b3939b 3879 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3880
acebe513
UW
3881 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3882 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3883 Work around this problem here. */
3884 if (cu->language == language_cplus
6e70227d 3885 && parent->tag == DW_TAG_namespace
acebe513
UW
3886 && strcmp (parent->name, "::") == 0
3887 && grandparent_scope == NULL)
3888 {
3889 parent->scope = NULL;
3890 parent->scope_set = 1;
3891 return NULL;
3892 }
3893
9c6c53f7
SA
3894 if (pdi->tag == DW_TAG_enumerator)
3895 /* Enumerators should not get the name of the enumeration as a prefix. */
3896 parent->scope = grandparent_scope;
3897 else if (parent->tag == DW_TAG_namespace
f55ee35c 3898 || parent->tag == DW_TAG_module
72bf9492
DJ
3899 || parent->tag == DW_TAG_structure_type
3900 || parent->tag == DW_TAG_class_type
680b30c7 3901 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3902 || parent->tag == DW_TAG_union_type
3903 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3904 {
3905 if (grandparent_scope == NULL)
3906 parent->scope = parent->name;
3907 else
3e43a32a
MS
3908 parent->scope = typename_concat (&cu->comp_unit_obstack,
3909 grandparent_scope,
f55ee35c 3910 parent->name, 0, cu);
72bf9492 3911 }
72bf9492
DJ
3912 else
3913 {
3914 /* FIXME drow/2004-04-01: What should we be doing with
3915 function-local names? For partial symbols, we should probably be
3916 ignoring them. */
3917 complaint (&symfile_complaints,
e2e0b3e5 3918 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3919 parent->tag, pdi->offset);
3920 parent->scope = grandparent_scope;
c906108c
SS
3921 }
3922
72bf9492
DJ
3923 parent->scope_set = 1;
3924 return parent->scope;
3925}
3926
3927/* Return the fully scoped name associated with PDI, from compilation unit
3928 CU. The result will be allocated with malloc. */
3929static char *
3930partial_die_full_name (struct partial_die_info *pdi,
3931 struct dwarf2_cu *cu)
3932{
3933 char *parent_scope;
3934
98bfdba5
PA
3935 /* If this is a template instantiation, we can not work out the
3936 template arguments from partial DIEs. So, unfortunately, we have
3937 to go through the full DIEs. At least any work we do building
3938 types here will be reused if full symbols are loaded later. */
3939 if (pdi->has_template_arguments)
3940 {
3941 fixup_partial_die (pdi, cu);
3942
3943 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3944 {
3945 struct die_info *die;
3946 struct attribute attr;
3947 struct dwarf2_cu *ref_cu = cu;
3948
3949 attr.name = 0;
3950 attr.form = DW_FORM_ref_addr;
3951 attr.u.addr = pdi->offset;
3952 die = follow_die_ref (NULL, &attr, &ref_cu);
3953
3954 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3955 }
3956 }
3957
72bf9492
DJ
3958 parent_scope = partial_die_parent_scope (pdi, cu);
3959 if (parent_scope == NULL)
3960 return NULL;
3961 else
f55ee35c 3962 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3963}
3964
3965static void
72bf9492 3966add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3967{
e7c27a73 3968 struct objfile *objfile = cu->objfile;
c906108c 3969 CORE_ADDR addr = 0;
decbce07 3970 char *actual_name = NULL;
5c4e30ca 3971 const struct partial_symbol *psym = NULL;
e142c38c 3972 CORE_ADDR baseaddr;
72bf9492 3973 int built_actual_name = 0;
e142c38c
DJ
3974
3975 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3976
94af9270
KS
3977 actual_name = partial_die_full_name (pdi, cu);
3978 if (actual_name)
3979 built_actual_name = 1;
63d06c5c 3980
72bf9492
DJ
3981 if (actual_name == NULL)
3982 actual_name = pdi->name;
3983
c906108c
SS
3984 switch (pdi->tag)
3985 {
3986 case DW_TAG_subprogram:
2cfa0c8d 3987 if (pdi->is_external || cu->language == language_ada)
c906108c 3988 {
2cfa0c8d
JB
3989 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3990 of the global scope. But in Ada, we want to be able to access
3991 nested procedures globally. So all Ada subprograms are stored
3992 in the global scope. */
f47fb265 3993 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3994 mst_text, objfile); */
f47fb265
MS
3995 add_psymbol_to_list (actual_name, strlen (actual_name),
3996 built_actual_name,
3997 VAR_DOMAIN, LOC_BLOCK,
3998 &objfile->global_psymbols,
3999 0, pdi->lowpc + baseaddr,
4000 cu->language, objfile);
c906108c
SS
4001 }
4002 else
4003 {
f47fb265 4004 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4005 mst_file_text, objfile); */
f47fb265
MS
4006 add_psymbol_to_list (actual_name, strlen (actual_name),
4007 built_actual_name,
4008 VAR_DOMAIN, LOC_BLOCK,
4009 &objfile->static_psymbols,
4010 0, pdi->lowpc + baseaddr,
4011 cu->language, objfile);
c906108c
SS
4012 }
4013 break;
72929c62
JB
4014 case DW_TAG_constant:
4015 {
4016 struct psymbol_allocation_list *list;
4017
4018 if (pdi->is_external)
4019 list = &objfile->global_psymbols;
4020 else
4021 list = &objfile->static_psymbols;
f47fb265
MS
4022 add_psymbol_to_list (actual_name, strlen (actual_name),
4023 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4024 list, 0, 0, cu->language, objfile);
72929c62
JB
4025 }
4026 break;
c906108c 4027 case DW_TAG_variable:
caac4577
JG
4028 if (pdi->locdesc)
4029 addr = decode_locdesc (pdi->locdesc, cu);
4030
4031 if (pdi->locdesc
4032 && addr == 0
4033 && !dwarf2_per_objfile->has_section_at_zero)
4034 {
4035 /* A global or static variable may also have been stripped
4036 out by the linker if unused, in which case its address
4037 will be nullified; do not add such variables into partial
4038 symbol table then. */
4039 }
4040 else if (pdi->is_external)
c906108c
SS
4041 {
4042 /* Global Variable.
4043 Don't enter into the minimal symbol tables as there is
4044 a minimal symbol table entry from the ELF symbols already.
4045 Enter into partial symbol table if it has a location
4046 descriptor or a type.
4047 If the location descriptor is missing, new_symbol will create
4048 a LOC_UNRESOLVED symbol, the address of the variable will then
4049 be determined from the minimal symbol table whenever the variable
4050 is referenced.
4051 The address for the partial symbol table entry is not
4052 used by GDB, but it comes in handy for debugging partial symbol
4053 table building. */
4054
c906108c 4055 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4056 add_psymbol_to_list (actual_name, strlen (actual_name),
4057 built_actual_name,
4058 VAR_DOMAIN, LOC_STATIC,
4059 &objfile->global_psymbols,
4060 0, addr + baseaddr,
4061 cu->language, objfile);
c906108c
SS
4062 }
4063 else
4064 {
0963b4bd 4065 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4066 if (pdi->locdesc == NULL)
decbce07
MS
4067 {
4068 if (built_actual_name)
4069 xfree (actual_name);
4070 return;
4071 }
f47fb265 4072 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4073 mst_file_data, objfile); */
f47fb265
MS
4074 add_psymbol_to_list (actual_name, strlen (actual_name),
4075 built_actual_name,
4076 VAR_DOMAIN, LOC_STATIC,
4077 &objfile->static_psymbols,
4078 0, addr + baseaddr,
4079 cu->language, objfile);
c906108c
SS
4080 }
4081 break;
4082 case DW_TAG_typedef:
4083 case DW_TAG_base_type:
a02abb62 4084 case DW_TAG_subrange_type:
38d518c9 4085 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4086 built_actual_name,
176620f1 4087 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4088 &objfile->static_psymbols,
e142c38c 4089 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4090 break;
72bf9492
DJ
4091 case DW_TAG_namespace:
4092 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4093 built_actual_name,
72bf9492
DJ
4094 VAR_DOMAIN, LOC_TYPEDEF,
4095 &objfile->global_psymbols,
4096 0, (CORE_ADDR) 0, cu->language, objfile);
4097 break;
c906108c 4098 case DW_TAG_class_type:
680b30c7 4099 case DW_TAG_interface_type:
c906108c
SS
4100 case DW_TAG_structure_type:
4101 case DW_TAG_union_type:
4102 case DW_TAG_enumeration_type:
fa4028e9
JB
4103 /* Skip external references. The DWARF standard says in the section
4104 about "Structure, Union, and Class Type Entries": "An incomplete
4105 structure, union or class type is represented by a structure,
4106 union or class entry that does not have a byte size attribute
4107 and that has a DW_AT_declaration attribute." */
4108 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4109 {
4110 if (built_actual_name)
4111 xfree (actual_name);
4112 return;
4113 }
fa4028e9 4114
63d06c5c
DC
4115 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4116 static vs. global. */
38d518c9 4117 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4118 built_actual_name,
176620f1 4119 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4120 (cu->language == language_cplus
4121 || cu->language == language_java)
63d06c5c
DC
4122 ? &objfile->global_psymbols
4123 : &objfile->static_psymbols,
e142c38c 4124 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4125
c906108c
SS
4126 break;
4127 case DW_TAG_enumerator:
38d518c9 4128 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4129 built_actual_name,
176620f1 4130 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4131 (cu->language == language_cplus
4132 || cu->language == language_java)
f6fe98ef
DJ
4133 ? &objfile->global_psymbols
4134 : &objfile->static_psymbols,
e142c38c 4135 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4136 break;
4137 default:
4138 break;
4139 }
5c4e30ca 4140
72bf9492
DJ
4141 if (built_actual_name)
4142 xfree (actual_name);
c906108c
SS
4143}
4144
5c4e30ca
DC
4145/* Read a partial die corresponding to a namespace; also, add a symbol
4146 corresponding to that namespace to the symbol table. NAMESPACE is
4147 the name of the enclosing namespace. */
91c24f0a 4148
72bf9492
DJ
4149static void
4150add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4151 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4152 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4153{
72bf9492 4154 /* Add a symbol for the namespace. */
e7c27a73 4155
72bf9492 4156 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4157
4158 /* Now scan partial symbols in that namespace. */
4159
91c24f0a 4160 if (pdi->has_children)
5734ee8b 4161 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4162}
4163
5d7cb8df
JK
4164/* Read a partial die corresponding to a Fortran module. */
4165
4166static void
4167add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4168 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4169{
f55ee35c 4170 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4171
4172 if (pdi->has_children)
4173 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4174}
4175
bc30ff58
JB
4176/* Read a partial die corresponding to a subprogram and create a partial
4177 symbol for that subprogram. When the CU language allows it, this
4178 routine also defines a partial symbol for each nested subprogram
4179 that this subprogram contains.
6e70227d 4180
bc30ff58
JB
4181 DIE my also be a lexical block, in which case we simply search
4182 recursively for suprograms defined inside that lexical block.
4183 Again, this is only performed when the CU language allows this
4184 type of definitions. */
4185
4186static void
4187add_partial_subprogram (struct partial_die_info *pdi,
4188 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4189 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4190{
4191 if (pdi->tag == DW_TAG_subprogram)
4192 {
4193 if (pdi->has_pc_info)
4194 {
4195 if (pdi->lowpc < *lowpc)
4196 *lowpc = pdi->lowpc;
4197 if (pdi->highpc > *highpc)
4198 *highpc = pdi->highpc;
5734ee8b
DJ
4199 if (need_pc)
4200 {
4201 CORE_ADDR baseaddr;
4202 struct objfile *objfile = cu->objfile;
4203
4204 baseaddr = ANOFFSET (objfile->section_offsets,
4205 SECT_OFF_TEXT (objfile));
4206 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4207 pdi->lowpc + baseaddr,
4208 pdi->highpc - 1 + baseaddr,
9291a0cd 4209 cu->per_cu->v.psymtab);
5734ee8b 4210 }
bc30ff58 4211 if (!pdi->is_declaration)
e8d05480
JB
4212 /* Ignore subprogram DIEs that do not have a name, they are
4213 illegal. Do not emit a complaint at this point, we will
4214 do so when we convert this psymtab into a symtab. */
4215 if (pdi->name)
4216 add_partial_symbol (pdi, cu);
bc30ff58
JB
4217 }
4218 }
6e70227d 4219
bc30ff58
JB
4220 if (! pdi->has_children)
4221 return;
4222
4223 if (cu->language == language_ada)
4224 {
4225 pdi = pdi->die_child;
4226 while (pdi != NULL)
4227 {
4228 fixup_partial_die (pdi, cu);
4229 if (pdi->tag == DW_TAG_subprogram
4230 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4231 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4232 pdi = pdi->die_sibling;
4233 }
4234 }
4235}
4236
91c24f0a
DC
4237/* Read a partial die corresponding to an enumeration type. */
4238
72bf9492
DJ
4239static void
4240add_partial_enumeration (struct partial_die_info *enum_pdi,
4241 struct dwarf2_cu *cu)
91c24f0a 4242{
72bf9492 4243 struct partial_die_info *pdi;
91c24f0a
DC
4244
4245 if (enum_pdi->name != NULL)
72bf9492
DJ
4246 add_partial_symbol (enum_pdi, cu);
4247
4248 pdi = enum_pdi->die_child;
4249 while (pdi)
91c24f0a 4250 {
72bf9492 4251 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4252 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4253 else
72bf9492
DJ
4254 add_partial_symbol (pdi, cu);
4255 pdi = pdi->die_sibling;
91c24f0a 4256 }
91c24f0a
DC
4257}
4258
4bb7a0a7
DJ
4259/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4260 Return the corresponding abbrev, or NULL if the number is zero (indicating
4261 an empty DIE). In either case *BYTES_READ will be set to the length of
4262 the initial number. */
4263
4264static struct abbrev_info *
fe1b8b76 4265peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4266 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4267{
4268 bfd *abfd = cu->objfile->obfd;
4269 unsigned int abbrev_number;
4270 struct abbrev_info *abbrev;
4271
4272 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4273
4274 if (abbrev_number == 0)
4275 return NULL;
4276
4277 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4278 if (!abbrev)
4279 {
3e43a32a
MS
4280 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4281 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4282 }
4283
4284 return abbrev;
4285}
4286
93311388
DE
4287/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4288 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4289 DIE. Any children of the skipped DIEs will also be skipped. */
4290
fe1b8b76 4291static gdb_byte *
93311388 4292skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4293{
4294 struct abbrev_info *abbrev;
4295 unsigned int bytes_read;
4296
4297 while (1)
4298 {
4299 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4300 if (abbrev == NULL)
4301 return info_ptr + bytes_read;
4302 else
93311388 4303 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4304 }
4305}
4306
93311388
DE
4307/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4308 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4309 abbrev corresponding to that skipped uleb128 should be passed in
4310 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4311 children. */
4312
fe1b8b76 4313static gdb_byte *
93311388
DE
4314skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4315 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4316{
4317 unsigned int bytes_read;
4318 struct attribute attr;
4319 bfd *abfd = cu->objfile->obfd;
4320 unsigned int form, i;
4321
4322 for (i = 0; i < abbrev->num_attrs; i++)
4323 {
4324 /* The only abbrev we care about is DW_AT_sibling. */
4325 if (abbrev->attrs[i].name == DW_AT_sibling)
4326 {
4327 read_attribute (&attr, &abbrev->attrs[i],
4328 abfd, info_ptr, cu);
4329 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4330 complaint (&symfile_complaints,
4331 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4332 else
93311388 4333 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4334 }
4335
4336 /* If it isn't DW_AT_sibling, skip this attribute. */
4337 form = abbrev->attrs[i].form;
4338 skip_attribute:
4339 switch (form)
4340 {
4bb7a0a7 4341 case DW_FORM_ref_addr:
ae411497
TT
4342 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4343 and later it is offset sized. */
4344 if (cu->header.version == 2)
4345 info_ptr += cu->header.addr_size;
4346 else
4347 info_ptr += cu->header.offset_size;
4348 break;
4349 case DW_FORM_addr:
4bb7a0a7
DJ
4350 info_ptr += cu->header.addr_size;
4351 break;
4352 case DW_FORM_data1:
4353 case DW_FORM_ref1:
4354 case DW_FORM_flag:
4355 info_ptr += 1;
4356 break;
2dc7f7b3
TT
4357 case DW_FORM_flag_present:
4358 break;
4bb7a0a7
DJ
4359 case DW_FORM_data2:
4360 case DW_FORM_ref2:
4361 info_ptr += 2;
4362 break;
4363 case DW_FORM_data4:
4364 case DW_FORM_ref4:
4365 info_ptr += 4;
4366 break;
4367 case DW_FORM_data8:
4368 case DW_FORM_ref8:
55f1336d 4369 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4370 info_ptr += 8;
4371 break;
4372 case DW_FORM_string:
9b1c24c8 4373 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4374 info_ptr += bytes_read;
4375 break;
2dc7f7b3 4376 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4377 case DW_FORM_strp:
4378 info_ptr += cu->header.offset_size;
4379 break;
2dc7f7b3 4380 case DW_FORM_exprloc:
4bb7a0a7
DJ
4381 case DW_FORM_block:
4382 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4383 info_ptr += bytes_read;
4384 break;
4385 case DW_FORM_block1:
4386 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4387 break;
4388 case DW_FORM_block2:
4389 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4390 break;
4391 case DW_FORM_block4:
4392 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4393 break;
4394 case DW_FORM_sdata:
4395 case DW_FORM_udata:
4396 case DW_FORM_ref_udata:
4397 info_ptr = skip_leb128 (abfd, info_ptr);
4398 break;
4399 case DW_FORM_indirect:
4400 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4401 info_ptr += bytes_read;
4402 /* We need to continue parsing from here, so just go back to
4403 the top. */
4404 goto skip_attribute;
4405
4406 default:
3e43a32a
MS
4407 error (_("Dwarf Error: Cannot handle %s "
4408 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4409 dwarf_form_name (form),
4410 bfd_get_filename (abfd));
4411 }
4412 }
4413
4414 if (abbrev->has_children)
93311388 4415 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4416 else
4417 return info_ptr;
4418}
4419
93311388
DE
4420/* Locate ORIG_PDI's sibling.
4421 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4422 in BUFFER. */
91c24f0a 4423
fe1b8b76 4424static gdb_byte *
93311388
DE
4425locate_pdi_sibling (struct partial_die_info *orig_pdi,
4426 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4427 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4428{
4429 /* Do we know the sibling already? */
72bf9492 4430
91c24f0a
DC
4431 if (orig_pdi->sibling)
4432 return orig_pdi->sibling;
4433
4434 /* Are there any children to deal with? */
4435
4436 if (!orig_pdi->has_children)
4437 return info_ptr;
4438
4bb7a0a7 4439 /* Skip the children the long way. */
91c24f0a 4440
93311388 4441 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4442}
4443
c906108c
SS
4444/* Expand this partial symbol table into a full symbol table. */
4445
4446static void
fba45db2 4447dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4448{
c906108c
SS
4449 if (pst != NULL)
4450 {
4451 if (pst->readin)
4452 {
3e43a32a
MS
4453 warning (_("bug: psymtab for %s is already read in."),
4454 pst->filename);
c906108c
SS
4455 }
4456 else
4457 {
4458 if (info_verbose)
4459 {
3e43a32a
MS
4460 printf_filtered (_("Reading in symbols for %s..."),
4461 pst->filename);
c906108c
SS
4462 gdb_flush (gdb_stdout);
4463 }
4464
10b3939b
DJ
4465 /* Restore our global data. */
4466 dwarf2_per_objfile = objfile_data (pst->objfile,
4467 dwarf2_objfile_data_key);
4468
b2ab525c
KB
4469 /* If this psymtab is constructed from a debug-only objfile, the
4470 has_section_at_zero flag will not necessarily be correct. We
4471 can get the correct value for this flag by looking at the data
4472 associated with the (presumably stripped) associated objfile. */
4473 if (pst->objfile->separate_debug_objfile_backlink)
4474 {
4475 struct dwarf2_per_objfile *dpo_backlink
4476 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4477 dwarf2_objfile_data_key);
9a619af0 4478
b2ab525c
KB
4479 dwarf2_per_objfile->has_section_at_zero
4480 = dpo_backlink->has_section_at_zero;
4481 }
4482
98bfdba5
PA
4483 dwarf2_per_objfile->reading_partial_symbols = 0;
4484
c906108c
SS
4485 psymtab_to_symtab_1 (pst);
4486
4487 /* Finish up the debug error message. */
4488 if (info_verbose)
a3f17187 4489 printf_filtered (_("done.\n"));
c906108c
SS
4490 }
4491 }
4492}
4493
10b3939b
DJ
4494/* Add PER_CU to the queue. */
4495
4496static void
03dd20cc 4497queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4498{
4499 struct dwarf2_queue_item *item;
4500
4501 per_cu->queued = 1;
4502 item = xmalloc (sizeof (*item));
4503 item->per_cu = per_cu;
4504 item->next = NULL;
4505
4506 if (dwarf2_queue == NULL)
4507 dwarf2_queue = item;
4508 else
4509 dwarf2_queue_tail->next = item;
4510
4511 dwarf2_queue_tail = item;
4512}
4513
4514/* Process the queue. */
4515
4516static void
4517process_queue (struct objfile *objfile)
4518{
4519 struct dwarf2_queue_item *item, *next_item;
4520
03dd20cc
DJ
4521 /* The queue starts out with one item, but following a DIE reference
4522 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4523 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4524 {
9291a0cd
TT
4525 if (dwarf2_per_objfile->using_index
4526 ? !item->per_cu->v.quick->symtab
4527 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4528 process_full_comp_unit (item->per_cu);
4529
4530 item->per_cu->queued = 0;
4531 next_item = item->next;
4532 xfree (item);
4533 }
4534
4535 dwarf2_queue_tail = NULL;
4536}
4537
4538/* Free all allocated queue entries. This function only releases anything if
4539 an error was thrown; if the queue was processed then it would have been
4540 freed as we went along. */
4541
4542static void
4543dwarf2_release_queue (void *dummy)
4544{
4545 struct dwarf2_queue_item *item, *last;
4546
4547 item = dwarf2_queue;
4548 while (item)
4549 {
4550 /* Anything still marked queued is likely to be in an
4551 inconsistent state, so discard it. */
4552 if (item->per_cu->queued)
4553 {
4554 if (item->per_cu->cu != NULL)
4555 free_one_cached_comp_unit (item->per_cu->cu);
4556 item->per_cu->queued = 0;
4557 }
4558
4559 last = item;
4560 item = item->next;
4561 xfree (last);
4562 }
4563
4564 dwarf2_queue = dwarf2_queue_tail = NULL;
4565}
4566
4567/* Read in full symbols for PST, and anything it depends on. */
4568
c906108c 4569static void
fba45db2 4570psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4571{
10b3939b 4572 struct dwarf2_per_cu_data *per_cu;
c906108c 4573 struct cleanup *back_to;
aaa75496
JB
4574 int i;
4575
4576 for (i = 0; i < pst->number_of_dependencies; i++)
4577 if (!pst->dependencies[i]->readin)
4578 {
4579 /* Inform about additional files that need to be read in. */
4580 if (info_verbose)
4581 {
a3f17187 4582 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4583 fputs_filtered (" ", gdb_stdout);
4584 wrap_here ("");
4585 fputs_filtered ("and ", gdb_stdout);
4586 wrap_here ("");
4587 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4588 wrap_here (""); /* Flush output. */
aaa75496
JB
4589 gdb_flush (gdb_stdout);
4590 }
4591 psymtab_to_symtab_1 (pst->dependencies[i]);
4592 }
4593
e38df1d0 4594 per_cu = pst->read_symtab_private;
10b3939b
DJ
4595
4596 if (per_cu == NULL)
aaa75496
JB
4597 {
4598 /* It's an include file, no symbols to read for it.
4599 Everything is in the parent symtab. */
4600 pst->readin = 1;
4601 return;
4602 }
c906108c 4603
9291a0cd 4604 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4605}
4606
93311388 4607/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4608
93311388 4609static void
3e43a32a
MS
4610load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4611 struct objfile *objfile)
10b3939b 4612{
31ffec48 4613 bfd *abfd = objfile->obfd;
10b3939b 4614 struct dwarf2_cu *cu;
c764a876 4615 unsigned int offset;
93311388 4616 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4617 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4618 struct attribute *attr;
98bfdba5 4619 int read_cu = 0;
6502dd73 4620
8b70b953 4621 gdb_assert (! per_cu->debug_type_section);
348e048f 4622
c906108c 4623 /* Set local variables from the partial symbol table info. */
10b3939b 4624 offset = per_cu->offset;
6502dd73 4625
be391dca 4626 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4627 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4628 beg_of_comp_unit = info_ptr;
63d06c5c 4629
98bfdba5
PA
4630 if (per_cu->cu == NULL)
4631 {
9816fde3
JK
4632 cu = xmalloc (sizeof (*cu));
4633 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4634
4635 read_cu = 1;
c906108c 4636
98bfdba5
PA
4637 /* If an error occurs while loading, release our storage. */
4638 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4639
98bfdba5
PA
4640 /* Read in the comp_unit header. */
4641 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4642
98bfdba5
PA
4643 /* Complete the cu_header. */
4644 cu->header.offset = offset;
4645 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4646
98bfdba5
PA
4647 /* Read the abbrevs for this compilation unit. */
4648 dwarf2_read_abbrevs (abfd, cu);
4649 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4650
98bfdba5
PA
4651 /* Link this compilation unit into the compilation unit tree. */
4652 per_cu->cu = cu;
4653 cu->per_cu = per_cu;
98bfdba5
PA
4654
4655 /* Link this CU into read_in_chain. */
4656 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4657 dwarf2_per_objfile->read_in_chain = per_cu;
4658 }
4659 else
4660 {
4661 cu = per_cu->cu;
4662 info_ptr += cu->header.first_die_offset;
4663 }
e142c38c 4664
93311388 4665 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4666
4667 /* We try not to read any attributes in this function, because not
4668 all objfiles needed for references have been loaded yet, and symbol
4669 table processing isn't initialized. But we have to set the CU language,
4670 or we won't be able to build types correctly. */
9816fde3 4671 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4672
a6c727b2
DJ
4673 /* Similarly, if we do not read the producer, we can not apply
4674 producer-specific interpretation. */
4675 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4676 if (attr)
4677 cu->producer = DW_STRING (attr);
4678
98bfdba5
PA
4679 if (read_cu)
4680 {
4681 do_cleanups (free_abbrevs_cleanup);
e142c38c 4682
98bfdba5
PA
4683 /* We've successfully allocated this compilation unit. Let our
4684 caller clean it up when finished with it. */
4685 discard_cleanups (free_cu_cleanup);
4686 }
10b3939b
DJ
4687}
4688
3da10d80
KS
4689/* Add a DIE to the delayed physname list. */
4690
4691static void
4692add_to_method_list (struct type *type, int fnfield_index, int index,
4693 const char *name, struct die_info *die,
4694 struct dwarf2_cu *cu)
4695{
4696 struct delayed_method_info mi;
4697 mi.type = type;
4698 mi.fnfield_index = fnfield_index;
4699 mi.index = index;
4700 mi.name = name;
4701 mi.die = die;
4702 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4703}
4704
4705/* A cleanup for freeing the delayed method list. */
4706
4707static void
4708free_delayed_list (void *ptr)
4709{
4710 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4711 if (cu->method_list != NULL)
4712 {
4713 VEC_free (delayed_method_info, cu->method_list);
4714 cu->method_list = NULL;
4715 }
4716}
4717
4718/* Compute the physnames of any methods on the CU's method list.
4719
4720 The computation of method physnames is delayed in order to avoid the
4721 (bad) condition that one of the method's formal parameters is of an as yet
4722 incomplete type. */
4723
4724static void
4725compute_delayed_physnames (struct dwarf2_cu *cu)
4726{
4727 int i;
4728 struct delayed_method_info *mi;
4729 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4730 {
1d06ead6 4731 const char *physname;
3da10d80
KS
4732 struct fn_fieldlist *fn_flp
4733 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4734 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4735 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4736 }
4737}
4738
10b3939b
DJ
4739/* Generate full symbol information for PST and CU, whose DIEs have
4740 already been loaded into memory. */
4741
4742static void
4743process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4744{
10b3939b 4745 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4746 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4747 CORE_ADDR lowpc, highpc;
4748 struct symtab *symtab;
3da10d80 4749 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4750 CORE_ADDR baseaddr;
4751
4752 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4753
10b3939b
DJ
4754 buildsym_init ();
4755 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4756 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4757
4758 cu->list_in_scope = &file_symbols;
c906108c
SS
4759
4760 /* Do line number decoding in read_file_scope () */
10b3939b 4761 process_die (cu->dies, cu);
c906108c 4762
3da10d80
KS
4763 /* Now that we have processed all the DIEs in the CU, all the types
4764 should be complete, and it should now be safe to compute all of the
4765 physnames. */
4766 compute_delayed_physnames (cu);
4767 do_cleanups (delayed_list_cleanup);
4768
fae299cd
DC
4769 /* Some compilers don't define a DW_AT_high_pc attribute for the
4770 compilation unit. If the DW_AT_high_pc is missing, synthesize
4771 it, by scanning the DIE's below the compilation unit. */
10b3939b 4772 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4773
613e1657 4774 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4775
8be455d7 4776 if (symtab != NULL)
c906108c 4777 {
df15bd07 4778 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 4779
8be455d7
JK
4780 /* Set symtab language to language from DW_AT_language. If the
4781 compilation is from a C file generated by language preprocessors, do
4782 not set the language if it was already deduced by start_subfile. */
4783 if (!(cu->language == language_c && symtab->language != language_c))
4784 symtab->language = cu->language;
4785
4786 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4787 produce DW_AT_location with location lists but it can be possibly
4788 invalid without -fvar-tracking.
4789
4790 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4791 needed, it would be wrong due to missing DW_AT_producer there.
4792
4793 Still one can confuse GDB by using non-standard GCC compilation
4794 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4795 */
4632c0d0 4796 if (cu->has_loclist && gcc_4_minor >= 0)
8be455d7 4797 symtab->locations_valid = 1;
e0d00bc7
JK
4798
4799 if (gcc_4_minor >= 5)
4800 symtab->epilogue_unwind_valid = 1;
96408a79
SA
4801
4802 symtab->call_site_htab = cu->call_site_htab;
c906108c 4803 }
9291a0cd
TT
4804
4805 if (dwarf2_per_objfile->using_index)
4806 per_cu->v.quick->symtab = symtab;
4807 else
4808 {
4809 struct partial_symtab *pst = per_cu->v.psymtab;
4810 pst->symtab = symtab;
4811 pst->readin = 1;
4812 }
c906108c
SS
4813
4814 do_cleanups (back_to);
4815}
4816
4817/* Process a die and its children. */
4818
4819static void
e7c27a73 4820process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4821{
4822 switch (die->tag)
4823 {
4824 case DW_TAG_padding:
4825 break;
4826 case DW_TAG_compile_unit:
e7c27a73 4827 read_file_scope (die, cu);
c906108c 4828 break;
348e048f
DE
4829 case DW_TAG_type_unit:
4830 read_type_unit_scope (die, cu);
4831 break;
c906108c 4832 case DW_TAG_subprogram:
c906108c 4833 case DW_TAG_inlined_subroutine:
edb3359d 4834 read_func_scope (die, cu);
c906108c
SS
4835 break;
4836 case DW_TAG_lexical_block:
14898363
L
4837 case DW_TAG_try_block:
4838 case DW_TAG_catch_block:
e7c27a73 4839 read_lexical_block_scope (die, cu);
c906108c 4840 break;
96408a79
SA
4841 case DW_TAG_GNU_call_site:
4842 read_call_site_scope (die, cu);
4843 break;
c906108c 4844 case DW_TAG_class_type:
680b30c7 4845 case DW_TAG_interface_type:
c906108c
SS
4846 case DW_TAG_structure_type:
4847 case DW_TAG_union_type:
134d01f1 4848 process_structure_scope (die, cu);
c906108c
SS
4849 break;
4850 case DW_TAG_enumeration_type:
134d01f1 4851 process_enumeration_scope (die, cu);
c906108c 4852 break;
134d01f1 4853
f792889a
DJ
4854 /* These dies have a type, but processing them does not create
4855 a symbol or recurse to process the children. Therefore we can
4856 read them on-demand through read_type_die. */
c906108c 4857 case DW_TAG_subroutine_type:
72019c9c 4858 case DW_TAG_set_type:
c906108c 4859 case DW_TAG_array_type:
c906108c 4860 case DW_TAG_pointer_type:
c906108c 4861 case DW_TAG_ptr_to_member_type:
c906108c 4862 case DW_TAG_reference_type:
c906108c 4863 case DW_TAG_string_type:
c906108c 4864 break;
134d01f1 4865
c906108c 4866 case DW_TAG_base_type:
a02abb62 4867 case DW_TAG_subrange_type:
cb249c71 4868 case DW_TAG_typedef:
134d01f1
DJ
4869 /* Add a typedef symbol for the type definition, if it has a
4870 DW_AT_name. */
f792889a 4871 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4872 break;
c906108c 4873 case DW_TAG_common_block:
e7c27a73 4874 read_common_block (die, cu);
c906108c
SS
4875 break;
4876 case DW_TAG_common_inclusion:
4877 break;
d9fa45fe 4878 case DW_TAG_namespace:
63d06c5c 4879 processing_has_namespace_info = 1;
e7c27a73 4880 read_namespace (die, cu);
d9fa45fe 4881 break;
5d7cb8df 4882 case DW_TAG_module:
f55ee35c 4883 processing_has_namespace_info = 1;
5d7cb8df
JK
4884 read_module (die, cu);
4885 break;
d9fa45fe
DC
4886 case DW_TAG_imported_declaration:
4887 case DW_TAG_imported_module:
63d06c5c 4888 processing_has_namespace_info = 1;
27aa8d6a
SW
4889 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4890 || cu->language != language_fortran))
4891 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4892 dwarf_tag_name (die->tag));
4893 read_import_statement (die, cu);
d9fa45fe 4894 break;
c906108c 4895 default:
e7c27a73 4896 new_symbol (die, NULL, cu);
c906108c
SS
4897 break;
4898 }
4899}
4900
94af9270
KS
4901/* A helper function for dwarf2_compute_name which determines whether DIE
4902 needs to have the name of the scope prepended to the name listed in the
4903 die. */
4904
4905static int
4906die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4907{
1c809c68
TT
4908 struct attribute *attr;
4909
94af9270
KS
4910 switch (die->tag)
4911 {
4912 case DW_TAG_namespace:
4913 case DW_TAG_typedef:
4914 case DW_TAG_class_type:
4915 case DW_TAG_interface_type:
4916 case DW_TAG_structure_type:
4917 case DW_TAG_union_type:
4918 case DW_TAG_enumeration_type:
4919 case DW_TAG_enumerator:
4920 case DW_TAG_subprogram:
4921 case DW_TAG_member:
4922 return 1;
4923
4924 case DW_TAG_variable:
c2b0a229 4925 case DW_TAG_constant:
94af9270
KS
4926 /* We only need to prefix "globally" visible variables. These include
4927 any variable marked with DW_AT_external or any variable that
4928 lives in a namespace. [Variables in anonymous namespaces
4929 require prefixing, but they are not DW_AT_external.] */
4930
4931 if (dwarf2_attr (die, DW_AT_specification, cu))
4932 {
4933 struct dwarf2_cu *spec_cu = cu;
9a619af0 4934
94af9270
KS
4935 return die_needs_namespace (die_specification (die, &spec_cu),
4936 spec_cu);
4937 }
4938
1c809c68 4939 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4940 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4941 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4942 return 0;
4943 /* A variable in a lexical block of some kind does not need a
4944 namespace, even though in C++ such variables may be external
4945 and have a mangled name. */
4946 if (die->parent->tag == DW_TAG_lexical_block
4947 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4948 || die->parent->tag == DW_TAG_catch_block
4949 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4950 return 0;
4951 return 1;
94af9270
KS
4952
4953 default:
4954 return 0;
4955 }
4956}
4957
98bfdba5
PA
4958/* Retrieve the last character from a mem_file. */
4959
4960static void
4961do_ui_file_peek_last (void *object, const char *buffer, long length)
4962{
4963 char *last_char_p = (char *) object;
4964
4965 if (length > 0)
4966 *last_char_p = buffer[length - 1];
4967}
4968
94af9270
KS
4969/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4970 compute the physname for the object, which include a method's
4971 formal parameters (C++/Java) and return type (Java).
4972
af6b7be1
JB
4973 For Ada, return the DIE's linkage name rather than the fully qualified
4974 name. PHYSNAME is ignored..
4975
94af9270
KS
4976 The result is allocated on the objfile_obstack and canonicalized. */
4977
4978static const char *
4979dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4980 int physname)
4981{
4982 if (name == NULL)
4983 name = dwarf2_name (die, cu);
4984
f55ee35c
JK
4985 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4986 compute it by typename_concat inside GDB. */
4987 if (cu->language == language_ada
4988 || (cu->language == language_fortran && physname))
4989 {
4990 /* For Ada unit, we prefer the linkage name over the name, as
4991 the former contains the exported name, which the user expects
4992 to be able to reference. Ideally, we want the user to be able
4993 to reference this entity using either natural or linkage name,
4994 but we haven't started looking at this enhancement yet. */
4995 struct attribute *attr;
4996
4997 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4998 if (attr == NULL)
4999 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5000 if (attr && DW_STRING (attr))
5001 return DW_STRING (attr);
5002 }
5003
94af9270
KS
5004 /* These are the only languages we know how to qualify names in. */
5005 if (name != NULL
f55ee35c
JK
5006 && (cu->language == language_cplus || cu->language == language_java
5007 || cu->language == language_fortran))
94af9270
KS
5008 {
5009 if (die_needs_namespace (die, cu))
5010 {
5011 long length;
5012 char *prefix;
5013 struct ui_file *buf;
5014
5015 prefix = determine_prefix (die, cu);
5016 buf = mem_fileopen ();
5017 if (*prefix != '\0')
5018 {
f55ee35c
JK
5019 char *prefixed_name = typename_concat (NULL, prefix, name,
5020 physname, cu);
9a619af0 5021
94af9270
KS
5022 fputs_unfiltered (prefixed_name, buf);
5023 xfree (prefixed_name);
5024 }
5025 else
62d5b8da 5026 fputs_unfiltered (name, buf);
94af9270 5027
98bfdba5
PA
5028 /* Template parameters may be specified in the DIE's DW_AT_name, or
5029 as children with DW_TAG_template_type_param or
5030 DW_TAG_value_type_param. If the latter, add them to the name
5031 here. If the name already has template parameters, then
5032 skip this step; some versions of GCC emit both, and
5033 it is more efficient to use the pre-computed name.
5034
5035 Something to keep in mind about this process: it is very
5036 unlikely, or in some cases downright impossible, to produce
5037 something that will match the mangled name of a function.
5038 If the definition of the function has the same debug info,
5039 we should be able to match up with it anyway. But fallbacks
5040 using the minimal symbol, for instance to find a method
5041 implemented in a stripped copy of libstdc++, will not work.
5042 If we do not have debug info for the definition, we will have to
5043 match them up some other way.
5044
5045 When we do name matching there is a related problem with function
5046 templates; two instantiated function templates are allowed to
5047 differ only by their return types, which we do not add here. */
5048
5049 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5050 {
5051 struct attribute *attr;
5052 struct die_info *child;
5053 int first = 1;
5054
5055 die->building_fullname = 1;
5056
5057 for (child = die->child; child != NULL; child = child->sibling)
5058 {
5059 struct type *type;
5060 long value;
5061 gdb_byte *bytes;
5062 struct dwarf2_locexpr_baton *baton;
5063 struct value *v;
5064
5065 if (child->tag != DW_TAG_template_type_param
5066 && child->tag != DW_TAG_template_value_param)
5067 continue;
5068
5069 if (first)
5070 {
5071 fputs_unfiltered ("<", buf);
5072 first = 0;
5073 }
5074 else
5075 fputs_unfiltered (", ", buf);
5076
5077 attr = dwarf2_attr (child, DW_AT_type, cu);
5078 if (attr == NULL)
5079 {
5080 complaint (&symfile_complaints,
5081 _("template parameter missing DW_AT_type"));
5082 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5083 continue;
5084 }
5085 type = die_type (child, cu);
5086
5087 if (child->tag == DW_TAG_template_type_param)
5088 {
5089 c_print_type (type, "", buf, -1, 0);
5090 continue;
5091 }
5092
5093 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5094 if (attr == NULL)
5095 {
5096 complaint (&symfile_complaints,
3e43a32a
MS
5097 _("template parameter missing "
5098 "DW_AT_const_value"));
98bfdba5
PA
5099 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5100 continue;
5101 }
5102
5103 dwarf2_const_value_attr (attr, type, name,
5104 &cu->comp_unit_obstack, cu,
5105 &value, &bytes, &baton);
5106
5107 if (TYPE_NOSIGN (type))
5108 /* GDB prints characters as NUMBER 'CHAR'. If that's
5109 changed, this can use value_print instead. */
5110 c_printchar (value, type, buf);
5111 else
5112 {
5113 struct value_print_options opts;
5114
5115 if (baton != NULL)
5116 v = dwarf2_evaluate_loc_desc (type, NULL,
5117 baton->data,
5118 baton->size,
5119 baton->per_cu);
5120 else if (bytes != NULL)
5121 {
5122 v = allocate_value (type);
5123 memcpy (value_contents_writeable (v), bytes,
5124 TYPE_LENGTH (type));
5125 }
5126 else
5127 v = value_from_longest (type, value);
5128
3e43a32a
MS
5129 /* Specify decimal so that we do not depend on
5130 the radix. */
98bfdba5
PA
5131 get_formatted_print_options (&opts, 'd');
5132 opts.raw = 1;
5133 value_print (v, buf, &opts);
5134 release_value (v);
5135 value_free (v);
5136 }
5137 }
5138
5139 die->building_fullname = 0;
5140
5141 if (!first)
5142 {
5143 /* Close the argument list, with a space if necessary
5144 (nested templates). */
5145 char last_char = '\0';
5146 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5147 if (last_char == '>')
5148 fputs_unfiltered (" >", buf);
5149 else
5150 fputs_unfiltered (">", buf);
5151 }
5152 }
5153
94af9270
KS
5154 /* For Java and C++ methods, append formal parameter type
5155 information, if PHYSNAME. */
6e70227d 5156
94af9270
KS
5157 if (physname && die->tag == DW_TAG_subprogram
5158 && (cu->language == language_cplus
5159 || cu->language == language_java))
5160 {
5161 struct type *type = read_type_die (die, cu);
5162
3167638f 5163 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5164
5165 if (cu->language == language_java)
5166 {
5167 /* For java, we must append the return type to method
0963b4bd 5168 names. */
94af9270
KS
5169 if (die->tag == DW_TAG_subprogram)
5170 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5171 0, 0);
5172 }
5173 else if (cu->language == language_cplus)
5174 {
60430eff
DJ
5175 /* Assume that an artificial first parameter is
5176 "this", but do not crash if it is not. RealView
5177 marks unnamed (and thus unused) parameters as
5178 artificial; there is no way to differentiate
5179 the two cases. */
94af9270
KS
5180 if (TYPE_NFIELDS (type) > 0
5181 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5182 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5183 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5184 0))))
94af9270
KS
5185 fputs_unfiltered (" const", buf);
5186 }
5187 }
5188
5189 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5190 &length);
5191 ui_file_delete (buf);
5192
5193 if (cu->language == language_cplus)
5194 {
5195 char *cname
5196 = dwarf2_canonicalize_name (name, cu,
5197 &cu->objfile->objfile_obstack);
9a619af0 5198
94af9270
KS
5199 if (cname != NULL)
5200 name = cname;
5201 }
5202 }
5203 }
5204
5205 return name;
5206}
5207
0114d602
DJ
5208/* Return the fully qualified name of DIE, based on its DW_AT_name.
5209 If scope qualifiers are appropriate they will be added. The result
5210 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5211 not have a name. NAME may either be from a previous call to
5212 dwarf2_name or NULL.
5213
0963b4bd 5214 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5215
5216static const char *
94af9270 5217dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5218{
94af9270
KS
5219 return dwarf2_compute_name (name, die, cu, 0);
5220}
0114d602 5221
94af9270
KS
5222/* Construct a physname for the given DIE in CU. NAME may either be
5223 from a previous call to dwarf2_name or NULL. The result will be
5224 allocated on the objfile_objstack or NULL if the DIE does not have a
5225 name.
0114d602 5226
94af9270 5227 The output string will be canonicalized (if C++/Java). */
0114d602 5228
94af9270
KS
5229static const char *
5230dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5231{
900e11f9
JK
5232 struct attribute *attr;
5233 const char *retval, *mangled = NULL, *canon = NULL;
5234 struct cleanup *back_to;
5235 int need_copy = 1;
5236
5237 /* In this case dwarf2_compute_name is just a shortcut not building anything
5238 on its own. */
5239 if (!die_needs_namespace (die, cu))
5240 return dwarf2_compute_name (name, die, cu, 1);
5241
5242 back_to = make_cleanup (null_cleanup, NULL);
5243
5244 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5245 if (!attr)
5246 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5247
5248 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5249 has computed. */
5250 if (attr && DW_STRING (attr))
5251 {
5252 char *demangled;
5253
5254 mangled = DW_STRING (attr);
5255
5256 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5257 type. It is easier for GDB users to search for such functions as
5258 `name(params)' than `long name(params)'. In such case the minimal
5259 symbol names do not match the full symbol names but for template
5260 functions there is never a need to look up their definition from their
5261 declaration so the only disadvantage remains the minimal symbol
5262 variant `long name(params)' does not have the proper inferior type.
5263 */
5264
5265 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5266 | (cu->language == language_java
5267 ? DMGL_JAVA | DMGL_RET_POSTFIX
5268 : DMGL_RET_DROP)));
5269 if (demangled)
5270 {
5271 make_cleanup (xfree, demangled);
5272 canon = demangled;
5273 }
5274 else
5275 {
5276 canon = mangled;
5277 need_copy = 0;
5278 }
5279 }
5280
5281 if (canon == NULL || check_physname)
5282 {
5283 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5284
5285 if (canon != NULL && strcmp (physname, canon) != 0)
5286 {
5287 /* It may not mean a bug in GDB. The compiler could also
5288 compute DW_AT_linkage_name incorrectly. But in such case
5289 GDB would need to be bug-to-bug compatible. */
5290
5291 complaint (&symfile_complaints,
5292 _("Computed physname <%s> does not match demangled <%s> "
5293 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5294 physname, canon, mangled, die->offset, cu->objfile->name);
5295
5296 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5297 is available here - over computed PHYSNAME. It is safer
5298 against both buggy GDB and buggy compilers. */
5299
5300 retval = canon;
5301 }
5302 else
5303 {
5304 retval = physname;
5305 need_copy = 0;
5306 }
5307 }
5308 else
5309 retval = canon;
5310
5311 if (need_copy)
5312 retval = obsavestring (retval, strlen (retval),
5313 &cu->objfile->objfile_obstack);
5314
5315 do_cleanups (back_to);
5316 return retval;
0114d602
DJ
5317}
5318
27aa8d6a
SW
5319/* Read the import statement specified by the given die and record it. */
5320
5321static void
5322read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5323{
5324 struct attribute *import_attr;
32019081 5325 struct die_info *imported_die, *child_die;
de4affc9 5326 struct dwarf2_cu *imported_cu;
27aa8d6a 5327 const char *imported_name;
794684b6 5328 const char *imported_name_prefix;
13387711
SW
5329 const char *canonical_name;
5330 const char *import_alias;
5331 const char *imported_declaration = NULL;
794684b6 5332 const char *import_prefix;
32019081
JK
5333 VEC (const_char_ptr) *excludes = NULL;
5334 struct cleanup *cleanups;
13387711
SW
5335
5336 char *temp;
27aa8d6a
SW
5337
5338 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5339 if (import_attr == NULL)
5340 {
5341 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5342 dwarf_tag_name (die->tag));
5343 return;
5344 }
5345
de4affc9
CC
5346 imported_cu = cu;
5347 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5348 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5349 if (imported_name == NULL)
5350 {
5351 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5352
5353 The import in the following code:
5354 namespace A
5355 {
5356 typedef int B;
5357 }
5358
5359 int main ()
5360 {
5361 using A::B;
5362 B b;
5363 return b;
5364 }
5365
5366 ...
5367 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5368 <52> DW_AT_decl_file : 1
5369 <53> DW_AT_decl_line : 6
5370 <54> DW_AT_import : <0x75>
5371 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5372 <59> DW_AT_name : B
5373 <5b> DW_AT_decl_file : 1
5374 <5c> DW_AT_decl_line : 2
5375 <5d> DW_AT_type : <0x6e>
5376 ...
5377 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5378 <76> DW_AT_byte_size : 4
5379 <77> DW_AT_encoding : 5 (signed)
5380
5381 imports the wrong die ( 0x75 instead of 0x58 ).
5382 This case will be ignored until the gcc bug is fixed. */
5383 return;
5384 }
5385
82856980
SW
5386 /* Figure out the local name after import. */
5387 import_alias = dwarf2_name (die, cu);
27aa8d6a 5388
794684b6
SW
5389 /* Figure out where the statement is being imported to. */
5390 import_prefix = determine_prefix (die, cu);
5391
5392 /* Figure out what the scope of the imported die is and prepend it
5393 to the name of the imported die. */
de4affc9 5394 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5395
f55ee35c
JK
5396 if (imported_die->tag != DW_TAG_namespace
5397 && imported_die->tag != DW_TAG_module)
794684b6 5398 {
13387711
SW
5399 imported_declaration = imported_name;
5400 canonical_name = imported_name_prefix;
794684b6 5401 }
13387711 5402 else if (strlen (imported_name_prefix) > 0)
794684b6 5403 {
13387711
SW
5404 temp = alloca (strlen (imported_name_prefix)
5405 + 2 + strlen (imported_name) + 1);
5406 strcpy (temp, imported_name_prefix);
5407 strcat (temp, "::");
5408 strcat (temp, imported_name);
5409 canonical_name = temp;
794684b6 5410 }
13387711
SW
5411 else
5412 canonical_name = imported_name;
794684b6 5413
32019081
JK
5414 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5415
5416 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5417 for (child_die = die->child; child_die && child_die->tag;
5418 child_die = sibling_die (child_die))
5419 {
5420 /* DWARF-4: A Fortran use statement with a “rename list” may be
5421 represented by an imported module entry with an import attribute
5422 referring to the module and owned entries corresponding to those
5423 entities that are renamed as part of being imported. */
5424
5425 if (child_die->tag != DW_TAG_imported_declaration)
5426 {
5427 complaint (&symfile_complaints,
5428 _("child DW_TAG_imported_declaration expected "
5429 "- DIE at 0x%x [in module %s]"),
5430 child_die->offset, cu->objfile->name);
5431 continue;
5432 }
5433
5434 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5435 if (import_attr == NULL)
5436 {
5437 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5438 dwarf_tag_name (child_die->tag));
5439 continue;
5440 }
5441
5442 imported_cu = cu;
5443 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5444 &imported_cu);
5445 imported_name = dwarf2_name (imported_die, imported_cu);
5446 if (imported_name == NULL)
5447 {
5448 complaint (&symfile_complaints,
5449 _("child DW_TAG_imported_declaration has unknown "
5450 "imported name - DIE at 0x%x [in module %s]"),
5451 child_die->offset, cu->objfile->name);
5452 continue;
5453 }
5454
5455 VEC_safe_push (const_char_ptr, excludes, imported_name);
5456
5457 process_die (child_die, cu);
5458 }
5459
c0cc3a76
SW
5460 cp_add_using_directive (import_prefix,
5461 canonical_name,
5462 import_alias,
13387711 5463 imported_declaration,
32019081 5464 excludes,
c0cc3a76 5465 &cu->objfile->objfile_obstack);
32019081
JK
5466
5467 do_cleanups (cleanups);
27aa8d6a
SW
5468}
5469
5fb290d7 5470static void
e142c38c 5471initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5472{
e142c38c 5473 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5474}
5475
ae2de4f8
DE
5476/* Cleanup function for read_file_scope. */
5477
cb1df416
DJ
5478static void
5479free_cu_line_header (void *arg)
5480{
5481 struct dwarf2_cu *cu = arg;
5482
5483 free_line_header (cu->line_header);
5484 cu->line_header = NULL;
5485}
5486
9291a0cd
TT
5487static void
5488find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5489 char **name, char **comp_dir)
5490{
5491 struct attribute *attr;
5492
5493 *name = NULL;
5494 *comp_dir = NULL;
5495
5496 /* Find the filename. Do not use dwarf2_name here, since the filename
5497 is not a source language identifier. */
5498 attr = dwarf2_attr (die, DW_AT_name, cu);
5499 if (attr)
5500 {
5501 *name = DW_STRING (attr);
5502 }
5503
5504 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5505 if (attr)
5506 *comp_dir = DW_STRING (attr);
5507 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5508 {
5509 *comp_dir = ldirname (*name);
5510 if (*comp_dir != NULL)
5511 make_cleanup (xfree, *comp_dir);
5512 }
5513 if (*comp_dir != NULL)
5514 {
5515 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5516 directory, get rid of it. */
5517 char *cp = strchr (*comp_dir, ':');
5518
5519 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5520 *comp_dir = cp + 1;
5521 }
5522
5523 if (*name == NULL)
5524 *name = "<unknown>";
5525}
5526
2ab95328
TT
5527/* Handle DW_AT_stmt_list for a compilation unit. */
5528
5529static void
5530handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5531 const char *comp_dir)
5532{
5533 struct attribute *attr;
5534 struct objfile *objfile = cu->objfile;
5535 bfd *abfd = objfile->obfd;
5536
5537 /* Decode line number information if present. We do this before
5538 processing child DIEs, so that the line header table is available
5539 for DW_AT_decl_file. */
5540 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5541 if (attr)
5542 {
5543 unsigned int line_offset = DW_UNSND (attr);
5544 struct line_header *line_header
5545 = dwarf_decode_line_header (line_offset, abfd, cu);
5546
5547 if (line_header)
5548 {
5549 cu->line_header = line_header;
5550 make_cleanup (free_cu_line_header, cu);
5551 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5552 }
5553 }
5554}
5555
ae2de4f8
DE
5556/* Process DW_TAG_compile_unit. */
5557
c906108c 5558static void
e7c27a73 5559read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5560{
e7c27a73 5561 struct objfile *objfile = cu->objfile;
debd256d 5562 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5563 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5564 CORE_ADDR highpc = ((CORE_ADDR) 0);
5565 struct attribute *attr;
e1024ff1 5566 char *name = NULL;
c906108c
SS
5567 char *comp_dir = NULL;
5568 struct die_info *child_die;
5569 bfd *abfd = objfile->obfd;
e142c38c 5570 CORE_ADDR baseaddr;
6e70227d 5571
e142c38c 5572 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5573
fae299cd 5574 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5575
5576 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5577 from finish_block. */
2acceee2 5578 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5579 lowpc = highpc;
5580 lowpc += baseaddr;
5581 highpc += baseaddr;
5582
9291a0cd 5583 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5584
e142c38c 5585 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5586 if (attr)
5587 {
e142c38c 5588 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5589 }
5590
b0f35d58 5591 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5592 if (attr)
b0f35d58 5593 cu->producer = DW_STRING (attr);
303b6f5d 5594
f4b8a18d
KW
5595 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5596 standardised yet. As a workaround for the language detection we fall
5597 back to the DW_AT_producer string. */
5598 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5599 cu->language = language_opencl;
5600
0963b4bd 5601 /* We assume that we're processing GCC output. */
c906108c 5602 processing_gcc_compilation = 2;
c906108c 5603
df8a16a1
DJ
5604 processing_has_namespace_info = 0;
5605
c906108c
SS
5606 start_symtab (name, comp_dir, lowpc);
5607 record_debugformat ("DWARF 2");
303b6f5d 5608 record_producer (cu->producer);
c906108c 5609
e142c38c 5610 initialize_cu_func_list (cu);
c906108c 5611
2ab95328 5612 handle_DW_AT_stmt_list (die, cu, comp_dir);
debd256d 5613
cb1df416
DJ
5614 /* Process all dies in compilation unit. */
5615 if (die->child != NULL)
5616 {
5617 child_die = die->child;
5618 while (child_die && child_die->tag)
5619 {
5620 process_die (child_die, cu);
5621 child_die = sibling_die (child_die);
5622 }
5623 }
5624
2e276125
JB
5625 /* Decode macro information, if present. Dwarf 2 macro information
5626 refers to information in the line number info statement program
5627 header, so we can only read it if we've read the header
5628 successfully. */
cf2c3c16 5629 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
2ab95328 5630 if (attr && cu->line_header)
2e276125 5631 {
cf2c3c16
TT
5632 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5633 complaint (&symfile_complaints,
5634 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5635
5636 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5637 comp_dir, abfd, cu,
5638 &dwarf2_per_objfile->macro, 1);
5639 }
5640 else
5641 {
5642 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5643 if (attr && cu->line_header)
5644 {
5645 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5646
cf2c3c16
TT
5647 dwarf_decode_macros (cu->line_header, macro_offset,
5648 comp_dir, abfd, cu,
5649 &dwarf2_per_objfile->macinfo, 0);
5650 }
2e276125 5651 }
debd256d 5652 do_cleanups (back_to);
5fb290d7
DJ
5653}
5654
ae2de4f8
DE
5655/* Process DW_TAG_type_unit.
5656 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5657 actual type being defined by this TU. In this case the first top
5658 level sibling is there to provide context only. */
5659
5660static void
5661read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5662{
5663 struct objfile *objfile = cu->objfile;
5664 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5665 CORE_ADDR lowpc;
5666 struct attribute *attr;
5667 char *name = NULL;
5668 char *comp_dir = NULL;
5669 struct die_info *child_die;
5670 bfd *abfd = objfile->obfd;
348e048f
DE
5671
5672 /* start_symtab needs a low pc, but we don't really have one.
5673 Do what read_file_scope would do in the absence of such info. */
5674 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5675
5676 /* Find the filename. Do not use dwarf2_name here, since the filename
5677 is not a source language identifier. */
5678 attr = dwarf2_attr (die, DW_AT_name, cu);
5679 if (attr)
5680 name = DW_STRING (attr);
5681
5682 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5683 if (attr)
5684 comp_dir = DW_STRING (attr);
5685 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5686 {
5687 comp_dir = ldirname (name);
5688 if (comp_dir != NULL)
5689 make_cleanup (xfree, comp_dir);
5690 }
5691
5692 if (name == NULL)
5693 name = "<unknown>";
5694
5695 attr = dwarf2_attr (die, DW_AT_language, cu);
5696 if (attr)
5697 set_cu_language (DW_UNSND (attr), cu);
5698
5699 /* This isn't technically needed today. It is done for symmetry
5700 with read_file_scope. */
5701 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5702 if (attr)
348e048f
DE
5703 cu->producer = DW_STRING (attr);
5704
0963b4bd 5705 /* We assume that we're processing GCC output. */
348e048f
DE
5706 processing_gcc_compilation = 2;
5707
5708 processing_has_namespace_info = 0;
5709
5710 start_symtab (name, comp_dir, lowpc);
5711 record_debugformat ("DWARF 2");
5712 record_producer (cu->producer);
5713
2ab95328
TT
5714 handle_DW_AT_stmt_list (die, cu, comp_dir);
5715
348e048f
DE
5716 /* Process the dies in the type unit. */
5717 if (die->child == NULL)
5718 {
5719 dump_die_for_error (die);
5720 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5721 bfd_get_filename (abfd));
5722 }
5723
5724 child_die = die->child;
5725
5726 while (child_die && child_die->tag)
5727 {
5728 process_die (child_die, cu);
5729
5730 child_die = sibling_die (child_die);
5731 }
5732
5733 do_cleanups (back_to);
5734}
5735
5fb290d7 5736static void
e142c38c
DJ
5737add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5738 struct dwarf2_cu *cu)
5fb290d7
DJ
5739{
5740 struct function_range *thisfn;
5741
5742 thisfn = (struct function_range *)
7b5a2f43 5743 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5744 thisfn->name = name;
5745 thisfn->lowpc = lowpc;
5746 thisfn->highpc = highpc;
5747 thisfn->seen_line = 0;
5748 thisfn->next = NULL;
5749
e142c38c
DJ
5750 if (cu->last_fn == NULL)
5751 cu->first_fn = thisfn;
5fb290d7 5752 else
e142c38c 5753 cu->last_fn->next = thisfn;
5fb290d7 5754
e142c38c 5755 cu->last_fn = thisfn;
c906108c
SS
5756}
5757
d389af10
JK
5758/* qsort helper for inherit_abstract_dies. */
5759
5760static int
5761unsigned_int_compar (const void *ap, const void *bp)
5762{
5763 unsigned int a = *(unsigned int *) ap;
5764 unsigned int b = *(unsigned int *) bp;
5765
5766 return (a > b) - (b > a);
5767}
5768
5769/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5770 Inherit only the children of the DW_AT_abstract_origin DIE not being
5771 already referenced by DW_AT_abstract_origin from the children of the
5772 current DIE. */
d389af10
JK
5773
5774static void
5775inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5776{
5777 struct die_info *child_die;
5778 unsigned die_children_count;
5779 /* CU offsets which were referenced by children of the current DIE. */
5780 unsigned *offsets;
5781 unsigned *offsets_end, *offsetp;
5782 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5783 struct die_info *origin_die;
5784 /* Iterator of the ORIGIN_DIE children. */
5785 struct die_info *origin_child_die;
5786 struct cleanup *cleanups;
5787 struct attribute *attr;
cd02d79d
PA
5788 struct dwarf2_cu *origin_cu;
5789 struct pending **origin_previous_list_in_scope;
d389af10
JK
5790
5791 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5792 if (!attr)
5793 return;
5794
cd02d79d
PA
5795 /* Note that following die references may follow to a die in a
5796 different cu. */
5797
5798 origin_cu = cu;
5799 origin_die = follow_die_ref (die, attr, &origin_cu);
5800
5801 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5802 symbols in. */
5803 origin_previous_list_in_scope = origin_cu->list_in_scope;
5804 origin_cu->list_in_scope = cu->list_in_scope;
5805
edb3359d
DJ
5806 if (die->tag != origin_die->tag
5807 && !(die->tag == DW_TAG_inlined_subroutine
5808 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5809 complaint (&symfile_complaints,
5810 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5811 die->offset, origin_die->offset);
5812
5813 child_die = die->child;
5814 die_children_count = 0;
5815 while (child_die && child_die->tag)
5816 {
5817 child_die = sibling_die (child_die);
5818 die_children_count++;
5819 }
5820 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5821 cleanups = make_cleanup (xfree, offsets);
5822
5823 offsets_end = offsets;
5824 child_die = die->child;
5825 while (child_die && child_die->tag)
5826 {
c38f313d
DJ
5827 /* For each CHILD_DIE, find the corresponding child of
5828 ORIGIN_DIE. If there is more than one layer of
5829 DW_AT_abstract_origin, follow them all; there shouldn't be,
5830 but GCC versions at least through 4.4 generate this (GCC PR
5831 40573). */
5832 struct die_info *child_origin_die = child_die;
cd02d79d 5833 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5834
c38f313d
DJ
5835 while (1)
5836 {
cd02d79d
PA
5837 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5838 child_origin_cu);
c38f313d
DJ
5839 if (attr == NULL)
5840 break;
cd02d79d
PA
5841 child_origin_die = follow_die_ref (child_origin_die, attr,
5842 &child_origin_cu);
c38f313d
DJ
5843 }
5844
d389af10
JK
5845 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5846 counterpart may exist. */
c38f313d 5847 if (child_origin_die != child_die)
d389af10 5848 {
edb3359d
DJ
5849 if (child_die->tag != child_origin_die->tag
5850 && !(child_die->tag == DW_TAG_inlined_subroutine
5851 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5852 complaint (&symfile_complaints,
5853 _("Child DIE 0x%x and its abstract origin 0x%x have "
5854 "different tags"), child_die->offset,
5855 child_origin_die->offset);
c38f313d
DJ
5856 if (child_origin_die->parent != origin_die)
5857 complaint (&symfile_complaints,
5858 _("Child DIE 0x%x and its abstract origin 0x%x have "
5859 "different parents"), child_die->offset,
5860 child_origin_die->offset);
5861 else
5862 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5863 }
5864 child_die = sibling_die (child_die);
5865 }
5866 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5867 unsigned_int_compar);
5868 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5869 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5870 complaint (&symfile_complaints,
5871 _("Multiple children of DIE 0x%x refer "
5872 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5873 die->offset, *offsetp);
5874
5875 offsetp = offsets;
5876 origin_child_die = origin_die->child;
5877 while (origin_child_die && origin_child_die->tag)
5878 {
5879 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5880 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5881 offsetp++;
5882 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5883 {
5884 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5885 process_die (origin_child_die, origin_cu);
d389af10
JK
5886 }
5887 origin_child_die = sibling_die (origin_child_die);
5888 }
cd02d79d 5889 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5890
5891 do_cleanups (cleanups);
5892}
5893
c906108c 5894static void
e7c27a73 5895read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5896{
e7c27a73 5897 struct objfile *objfile = cu->objfile;
52f0bd74 5898 struct context_stack *new;
c906108c
SS
5899 CORE_ADDR lowpc;
5900 CORE_ADDR highpc;
5901 struct die_info *child_die;
edb3359d 5902 struct attribute *attr, *call_line, *call_file;
c906108c 5903 char *name;
e142c38c 5904 CORE_ADDR baseaddr;
801e3a5b 5905 struct block *block;
edb3359d 5906 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5907 VEC (symbolp) *template_args = NULL;
5908 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5909
5910 if (inlined_func)
5911 {
5912 /* If we do not have call site information, we can't show the
5913 caller of this inlined function. That's too confusing, so
5914 only use the scope for local variables. */
5915 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5916 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5917 if (call_line == NULL || call_file == NULL)
5918 {
5919 read_lexical_block_scope (die, cu);
5920 return;
5921 }
5922 }
c906108c 5923
e142c38c
DJ
5924 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5925
94af9270 5926 name = dwarf2_name (die, cu);
c906108c 5927
e8d05480
JB
5928 /* Ignore functions with missing or empty names. These are actually
5929 illegal according to the DWARF standard. */
5930 if (name == NULL)
5931 {
5932 complaint (&symfile_complaints,
5933 _("missing name for subprogram DIE at %d"), die->offset);
5934 return;
5935 }
5936
5937 /* Ignore functions with missing or invalid low and high pc attributes. */
5938 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5939 {
ae4d0c03
PM
5940 attr = dwarf2_attr (die, DW_AT_external, cu);
5941 if (!attr || !DW_UNSND (attr))
5942 complaint (&symfile_complaints,
3e43a32a
MS
5943 _("cannot get low and high bounds "
5944 "for subprogram DIE at %d"),
ae4d0c03 5945 die->offset);
e8d05480
JB
5946 return;
5947 }
c906108c
SS
5948
5949 lowpc += baseaddr;
5950 highpc += baseaddr;
5951
5fb290d7 5952 /* Record the function range for dwarf_decode_lines. */
e142c38c 5953 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5954
34eaf542
TT
5955 /* If we have any template arguments, then we must allocate a
5956 different sort of symbol. */
5957 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5958 {
5959 if (child_die->tag == DW_TAG_template_type_param
5960 || child_die->tag == DW_TAG_template_value_param)
5961 {
5962 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5963 struct template_symbol);
5964 templ_func->base.is_cplus_template_function = 1;
5965 break;
5966 }
5967 }
5968
c906108c 5969 new = push_context (0, lowpc);
34eaf542
TT
5970 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5971 (struct symbol *) templ_func);
4c2df51b 5972
4cecd739
DJ
5973 /* If there is a location expression for DW_AT_frame_base, record
5974 it. */
e142c38c 5975 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5976 if (attr)
c034e007
AC
5977 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5978 expression is being recorded directly in the function's symbol
5979 and not in a separate frame-base object. I guess this hack is
5980 to avoid adding some sort of frame-base adjunct/annex to the
5981 function's symbol :-(. The problem with doing this is that it
5982 results in a function symbol with a location expression that
5983 has nothing to do with the location of the function, ouch! The
5984 relationship should be: a function's symbol has-a frame base; a
5985 frame-base has-a location expression. */
e7c27a73 5986 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5987
e142c38c 5988 cu->list_in_scope = &local_symbols;
c906108c 5989
639d11d3 5990 if (die->child != NULL)
c906108c 5991 {
639d11d3 5992 child_die = die->child;
c906108c
SS
5993 while (child_die && child_die->tag)
5994 {
34eaf542
TT
5995 if (child_die->tag == DW_TAG_template_type_param
5996 || child_die->tag == DW_TAG_template_value_param)
5997 {
5998 struct symbol *arg = new_symbol (child_die, NULL, cu);
5999
f1078f66
DJ
6000 if (arg != NULL)
6001 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6002 }
6003 else
6004 process_die (child_die, cu);
c906108c
SS
6005 child_die = sibling_die (child_die);
6006 }
6007 }
6008
d389af10
JK
6009 inherit_abstract_dies (die, cu);
6010
4a811a97
UW
6011 /* If we have a DW_AT_specification, we might need to import using
6012 directives from the context of the specification DIE. See the
6013 comment in determine_prefix. */
6014 if (cu->language == language_cplus
6015 && dwarf2_attr (die, DW_AT_specification, cu))
6016 {
6017 struct dwarf2_cu *spec_cu = cu;
6018 struct die_info *spec_die = die_specification (die, &spec_cu);
6019
6020 while (spec_die)
6021 {
6022 child_die = spec_die->child;
6023 while (child_die && child_die->tag)
6024 {
6025 if (child_die->tag == DW_TAG_imported_module)
6026 process_die (child_die, spec_cu);
6027 child_die = sibling_die (child_die);
6028 }
6029
6030 /* In some cases, GCC generates specification DIEs that
6031 themselves contain DW_AT_specification attributes. */
6032 spec_die = die_specification (spec_die, &spec_cu);
6033 }
6034 }
6035
c906108c
SS
6036 new = pop_context ();
6037 /* Make a block for the local symbols within. */
801e3a5b
JB
6038 block = finish_block (new->name, &local_symbols, new->old_blocks,
6039 lowpc, highpc, objfile);
6040
df8a16a1 6041 /* For C++, set the block's scope. */
f55ee35c 6042 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 6043 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 6044 determine_prefix (die, cu),
df8a16a1
DJ
6045 processing_has_namespace_info);
6046
801e3a5b
JB
6047 /* If we have address ranges, record them. */
6048 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 6049
34eaf542
TT
6050 /* Attach template arguments to function. */
6051 if (! VEC_empty (symbolp, template_args))
6052 {
6053 gdb_assert (templ_func != NULL);
6054
6055 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6056 templ_func->template_arguments
6057 = obstack_alloc (&objfile->objfile_obstack,
6058 (templ_func->n_template_arguments
6059 * sizeof (struct symbol *)));
6060 memcpy (templ_func->template_arguments,
6061 VEC_address (symbolp, template_args),
6062 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6063 VEC_free (symbolp, template_args);
6064 }
6065
208d8187
JB
6066 /* In C++, we can have functions nested inside functions (e.g., when
6067 a function declares a class that has methods). This means that
6068 when we finish processing a function scope, we may need to go
6069 back to building a containing block's symbol lists. */
6070 local_symbols = new->locals;
6071 param_symbols = new->params;
27aa8d6a 6072 using_directives = new->using_directives;
208d8187 6073
921e78cf
JB
6074 /* If we've finished processing a top-level function, subsequent
6075 symbols go in the file symbol list. */
6076 if (outermost_context_p ())
e142c38c 6077 cu->list_in_scope = &file_symbols;
c906108c
SS
6078}
6079
6080/* Process all the DIES contained within a lexical block scope. Start
6081 a new scope, process the dies, and then close the scope. */
6082
6083static void
e7c27a73 6084read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6085{
e7c27a73 6086 struct objfile *objfile = cu->objfile;
52f0bd74 6087 struct context_stack *new;
c906108c
SS
6088 CORE_ADDR lowpc, highpc;
6089 struct die_info *child_die;
e142c38c
DJ
6090 CORE_ADDR baseaddr;
6091
6092 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6093
6094 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6095 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6096 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6097 be nasty. Might be easier to properly extend generic blocks to
af34e669 6098 describe ranges. */
d85a05f0 6099 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6100 return;
6101 lowpc += baseaddr;
6102 highpc += baseaddr;
6103
6104 push_context (0, lowpc);
639d11d3 6105 if (die->child != NULL)
c906108c 6106 {
639d11d3 6107 child_die = die->child;
c906108c
SS
6108 while (child_die && child_die->tag)
6109 {
e7c27a73 6110 process_die (child_die, cu);
c906108c
SS
6111 child_die = sibling_die (child_die);
6112 }
6113 }
6114 new = pop_context ();
6115
8540c487 6116 if (local_symbols != NULL || using_directives != NULL)
c906108c 6117 {
801e3a5b
JB
6118 struct block *block
6119 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6120 highpc, objfile);
6121
6122 /* Note that recording ranges after traversing children, as we
6123 do here, means that recording a parent's ranges entails
6124 walking across all its children's ranges as they appear in
6125 the address map, which is quadratic behavior.
6126
6127 It would be nicer to record the parent's ranges before
6128 traversing its children, simply overriding whatever you find
6129 there. But since we don't even decide whether to create a
6130 block until after we've traversed its children, that's hard
6131 to do. */
6132 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6133 }
6134 local_symbols = new->locals;
27aa8d6a 6135 using_directives = new->using_directives;
c906108c
SS
6136}
6137
96408a79
SA
6138/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6139
6140static void
6141read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6142{
6143 struct objfile *objfile = cu->objfile;
6144 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6145 CORE_ADDR pc, baseaddr;
6146 struct attribute *attr;
6147 struct call_site *call_site, call_site_local;
6148 void **slot;
6149 int nparams;
6150 struct die_info *child_die;
6151
6152 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6153
6154 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6155 if (!attr)
6156 {
6157 complaint (&symfile_complaints,
6158 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6159 "DIE 0x%x [in module %s]"),
6160 die->offset, cu->objfile->name);
6161 return;
6162 }
6163 pc = DW_ADDR (attr) + baseaddr;
6164
6165 if (cu->call_site_htab == NULL)
6166 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6167 NULL, &objfile->objfile_obstack,
6168 hashtab_obstack_allocate, NULL);
6169 call_site_local.pc = pc;
6170 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6171 if (*slot != NULL)
6172 {
6173 complaint (&symfile_complaints,
6174 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6175 "DIE 0x%x [in module %s]"),
6176 paddress (gdbarch, pc), die->offset, cu->objfile->name);
6177 return;
6178 }
6179
6180 /* Count parameters at the caller. */
6181
6182 nparams = 0;
6183 for (child_die = die->child; child_die && child_die->tag;
6184 child_die = sibling_die (child_die))
6185 {
6186 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6187 {
6188 complaint (&symfile_complaints,
6189 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6190 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6191 child_die->tag, child_die->offset, cu->objfile->name);
6192 continue;
6193 }
6194
6195 nparams++;
6196 }
6197
6198 call_site = obstack_alloc (&objfile->objfile_obstack,
6199 (sizeof (*call_site)
6200 + (sizeof (*call_site->parameter)
6201 * (nparams - 1))));
6202 *slot = call_site;
6203 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6204 call_site->pc = pc;
6205
6206 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6207 {
6208 struct die_info *func_die;
6209
6210 /* Skip also over DW_TAG_inlined_subroutine. */
6211 for (func_die = die->parent;
6212 func_die && func_die->tag != DW_TAG_subprogram
6213 && func_die->tag != DW_TAG_subroutine_type;
6214 func_die = func_die->parent);
6215
6216 /* DW_AT_GNU_all_call_sites is a superset
6217 of DW_AT_GNU_all_tail_call_sites. */
6218 if (func_die
6219 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6220 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6221 {
6222 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6223 not complete. But keep CALL_SITE for look ups via call_site_htab,
6224 both the initial caller containing the real return address PC and
6225 the final callee containing the current PC of a chain of tail
6226 calls do not need to have the tail call list complete. But any
6227 function candidate for a virtual tail call frame searched via
6228 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6229 determined unambiguously. */
6230 }
6231 else
6232 {
6233 struct type *func_type = NULL;
6234
6235 if (func_die)
6236 func_type = get_die_type (func_die, cu);
6237 if (func_type != NULL)
6238 {
6239 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6240
6241 /* Enlist this call site to the function. */
6242 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6243 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6244 }
6245 else
6246 complaint (&symfile_complaints,
6247 _("Cannot find function owning DW_TAG_GNU_call_site "
6248 "DIE 0x%x [in module %s]"),
6249 die->offset, cu->objfile->name);
6250 }
6251 }
6252
6253 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6254 if (attr == NULL)
6255 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6256 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6257 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6258 /* Keep NULL DWARF_BLOCK. */;
6259 else if (attr_form_is_block (attr))
6260 {
6261 struct dwarf2_locexpr_baton *dlbaton;
6262
6263 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6264 dlbaton->data = DW_BLOCK (attr)->data;
6265 dlbaton->size = DW_BLOCK (attr)->size;
6266 dlbaton->per_cu = cu->per_cu;
6267
6268 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6269 }
6270 else if (is_ref_attr (attr))
6271 {
6272 struct objfile *objfile = cu->objfile;
6273 struct dwarf2_cu *target_cu = cu;
6274 struct die_info *target_die;
6275
6276 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6277 gdb_assert (target_cu->objfile == objfile);
6278 if (die_is_declaration (target_die, target_cu))
6279 {
6280 const char *target_physname;
6281
6282 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6283 if (target_physname == NULL)
6284 complaint (&symfile_complaints,
6285 _("DW_AT_GNU_call_site_target target DIE has invalid "
6286 "physname, for referencing DIE 0x%x [in module %s]"),
6287 die->offset, cu->objfile->name);
6288 else
6289 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6290 }
6291 else
6292 {
6293 CORE_ADDR lowpc;
6294
6295 /* DW_AT_entry_pc should be preferred. */
6296 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6297 complaint (&symfile_complaints,
6298 _("DW_AT_GNU_call_site_target target DIE has invalid "
6299 "low pc, for referencing DIE 0x%x [in module %s]"),
6300 die->offset, cu->objfile->name);
6301 else
6302 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6303 }
6304 }
6305 else
6306 complaint (&symfile_complaints,
6307 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6308 "block nor reference, for DIE 0x%x [in module %s]"),
6309 die->offset, cu->objfile->name);
6310
6311 call_site->per_cu = cu->per_cu;
6312
6313 for (child_die = die->child;
6314 child_die && child_die->tag;
6315 child_die = sibling_die (child_die))
6316 {
6317 struct dwarf2_locexpr_baton *dlbaton;
6318 struct call_site_parameter *parameter;
6319
6320 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6321 {
6322 /* Already printed the complaint above. */
6323 continue;
6324 }
6325
6326 gdb_assert (call_site->parameter_count < nparams);
6327 parameter = &call_site->parameter[call_site->parameter_count];
6328
6329 /* DW_AT_location specifies the register number. Value of the data
6330 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6331
6332 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6333 if (!attr || !attr_form_is_block (attr))
6334 {
6335 complaint (&symfile_complaints,
6336 _("No DW_FORM_block* DW_AT_location for "
6337 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6338 child_die->offset, cu->objfile->name);
6339 continue;
6340 }
6341 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6342 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6343 if (parameter->dwarf_reg == -1
6344 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6345 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6346 &parameter->fb_offset))
6347 {
6348 complaint (&symfile_complaints,
6349 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6350 "for DW_FORM_block* DW_AT_location for "
6351 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6352 child_die->offset, cu->objfile->name);
6353 continue;
6354 }
6355
6356 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6357 if (!attr_form_is_block (attr))
6358 {
6359 complaint (&symfile_complaints,
6360 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6361 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6362 child_die->offset, cu->objfile->name);
6363 continue;
6364 }
6365 parameter->value = DW_BLOCK (attr)->data;
6366 parameter->value_size = DW_BLOCK (attr)->size;
6367
6368 /* Parameters are not pre-cleared by memset above. */
6369 parameter->data_value = NULL;
6370 parameter->data_value_size = 0;
6371 call_site->parameter_count++;
6372
6373 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6374 if (attr)
6375 {
6376 if (!attr_form_is_block (attr))
6377 complaint (&symfile_complaints,
6378 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6379 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6380 child_die->offset, cu->objfile->name);
6381 else
6382 {
6383 parameter->data_value = DW_BLOCK (attr)->data;
6384 parameter->data_value_size = DW_BLOCK (attr)->size;
6385 }
6386 }
6387 }
6388}
6389
43039443 6390/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6391 Return 1 if the attributes are present and valid, otherwise, return 0.
6392 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6393
6394static int
6395dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6396 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6397 struct partial_symtab *ranges_pst)
43039443
JK
6398{
6399 struct objfile *objfile = cu->objfile;
6400 struct comp_unit_head *cu_header = &cu->header;
6401 bfd *obfd = objfile->obfd;
6402 unsigned int addr_size = cu_header->addr_size;
6403 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6404 /* Base address selection entry. */
6405 CORE_ADDR base;
6406 int found_base;
6407 unsigned int dummy;
6408 gdb_byte *buffer;
6409 CORE_ADDR marker;
6410 int low_set;
6411 CORE_ADDR low = 0;
6412 CORE_ADDR high = 0;
ff013f42 6413 CORE_ADDR baseaddr;
43039443 6414
d00adf39
DE
6415 found_base = cu->base_known;
6416 base = cu->base_address;
43039443 6417
be391dca 6418 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6419 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6420 {
6421 complaint (&symfile_complaints,
6422 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6423 offset);
6424 return 0;
6425 }
dce234bc 6426 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6427
6428 /* Read in the largest possible address. */
6429 marker = read_address (obfd, buffer, cu, &dummy);
6430 if ((marker & mask) == mask)
6431 {
6432 /* If we found the largest possible address, then
6433 read the base address. */
6434 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6435 buffer += 2 * addr_size;
6436 offset += 2 * addr_size;
6437 found_base = 1;
6438 }
6439
6440 low_set = 0;
6441
e7030f15 6442 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6443
43039443
JK
6444 while (1)
6445 {
6446 CORE_ADDR range_beginning, range_end;
6447
6448 range_beginning = read_address (obfd, buffer, cu, &dummy);
6449 buffer += addr_size;
6450 range_end = read_address (obfd, buffer, cu, &dummy);
6451 buffer += addr_size;
6452 offset += 2 * addr_size;
6453
6454 /* An end of list marker is a pair of zero addresses. */
6455 if (range_beginning == 0 && range_end == 0)
6456 /* Found the end of list entry. */
6457 break;
6458
6459 /* Each base address selection entry is a pair of 2 values.
6460 The first is the largest possible address, the second is
6461 the base address. Check for a base address here. */
6462 if ((range_beginning & mask) == mask)
6463 {
6464 /* If we found the largest possible address, then
6465 read the base address. */
6466 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6467 found_base = 1;
6468 continue;
6469 }
6470
6471 if (!found_base)
6472 {
6473 /* We have no valid base address for the ranges
6474 data. */
6475 complaint (&symfile_complaints,
6476 _("Invalid .debug_ranges data (no base address)"));
6477 return 0;
6478 }
6479
9277c30c
UW
6480 if (range_beginning > range_end)
6481 {
6482 /* Inverted range entries are invalid. */
6483 complaint (&symfile_complaints,
6484 _("Invalid .debug_ranges data (inverted range)"));
6485 return 0;
6486 }
6487
6488 /* Empty range entries have no effect. */
6489 if (range_beginning == range_end)
6490 continue;
6491
43039443
JK
6492 range_beginning += base;
6493 range_end += base;
6494
9277c30c 6495 if (ranges_pst != NULL)
ff013f42 6496 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6497 range_beginning + baseaddr,
6498 range_end - 1 + baseaddr,
ff013f42
JK
6499 ranges_pst);
6500
43039443
JK
6501 /* FIXME: This is recording everything as a low-high
6502 segment of consecutive addresses. We should have a
6503 data structure for discontiguous block ranges
6504 instead. */
6505 if (! low_set)
6506 {
6507 low = range_beginning;
6508 high = range_end;
6509 low_set = 1;
6510 }
6511 else
6512 {
6513 if (range_beginning < low)
6514 low = range_beginning;
6515 if (range_end > high)
6516 high = range_end;
6517 }
6518 }
6519
6520 if (! low_set)
6521 /* If the first entry is an end-of-list marker, the range
6522 describes an empty scope, i.e. no instructions. */
6523 return 0;
6524
6525 if (low_return)
6526 *low_return = low;
6527 if (high_return)
6528 *high_return = high;
6529 return 1;
6530}
6531
af34e669
DJ
6532/* Get low and high pc attributes from a die. Return 1 if the attributes
6533 are present and valid, otherwise, return 0. Return -1 if the range is
6534 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6535static int
af34e669 6536dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6537 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6538 struct partial_symtab *pst)
c906108c
SS
6539{
6540 struct attribute *attr;
af34e669
DJ
6541 CORE_ADDR low = 0;
6542 CORE_ADDR high = 0;
6543 int ret = 0;
c906108c 6544
e142c38c 6545 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6546 if (attr)
af34e669
DJ
6547 {
6548 high = DW_ADDR (attr);
e142c38c 6549 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6550 if (attr)
6551 low = DW_ADDR (attr);
6552 else
6553 /* Found high w/o low attribute. */
6554 return 0;
6555
6556 /* Found consecutive range of addresses. */
6557 ret = 1;
6558 }
c906108c 6559 else
af34e669 6560 {
e142c38c 6561 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6562 if (attr != NULL)
6563 {
af34e669 6564 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6565 .debug_ranges section. */
d85a05f0 6566 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6567 return 0;
43039443 6568 /* Found discontinuous range of addresses. */
af34e669
DJ
6569 ret = -1;
6570 }
6571 }
c906108c 6572
9373cf26
JK
6573 /* read_partial_die has also the strict LOW < HIGH requirement. */
6574 if (high <= low)
c906108c
SS
6575 return 0;
6576
6577 /* When using the GNU linker, .gnu.linkonce. sections are used to
6578 eliminate duplicate copies of functions and vtables and such.
6579 The linker will arbitrarily choose one and discard the others.
6580 The AT_*_pc values for such functions refer to local labels in
6581 these sections. If the section from that file was discarded, the
6582 labels are not in the output, so the relocs get a value of 0.
6583 If this is a discarded function, mark the pc bounds as invalid,
6584 so that GDB will ignore it. */
72dca2f5 6585 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6586 return 0;
6587
6588 *lowpc = low;
96408a79
SA
6589 if (highpc)
6590 *highpc = high;
af34e669 6591 return ret;
c906108c
SS
6592}
6593
b084d499
JB
6594/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6595 its low and high PC addresses. Do nothing if these addresses could not
6596 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6597 and HIGHPC to the high address if greater than HIGHPC. */
6598
6599static void
6600dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6601 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6602 struct dwarf2_cu *cu)
6603{
6604 CORE_ADDR low, high;
6605 struct die_info *child = die->child;
6606
d85a05f0 6607 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6608 {
6609 *lowpc = min (*lowpc, low);
6610 *highpc = max (*highpc, high);
6611 }
6612
6613 /* If the language does not allow nested subprograms (either inside
6614 subprograms or lexical blocks), we're done. */
6615 if (cu->language != language_ada)
6616 return;
6e70227d 6617
b084d499
JB
6618 /* Check all the children of the given DIE. If it contains nested
6619 subprograms, then check their pc bounds. Likewise, we need to
6620 check lexical blocks as well, as they may also contain subprogram
6621 definitions. */
6622 while (child && child->tag)
6623 {
6624 if (child->tag == DW_TAG_subprogram
6625 || child->tag == DW_TAG_lexical_block)
6626 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6627 child = sibling_die (child);
6628 }
6629}
6630
fae299cd
DC
6631/* Get the low and high pc's represented by the scope DIE, and store
6632 them in *LOWPC and *HIGHPC. If the correct values can't be
6633 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6634
6635static void
6636get_scope_pc_bounds (struct die_info *die,
6637 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6638 struct dwarf2_cu *cu)
6639{
6640 CORE_ADDR best_low = (CORE_ADDR) -1;
6641 CORE_ADDR best_high = (CORE_ADDR) 0;
6642 CORE_ADDR current_low, current_high;
6643
d85a05f0 6644 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6645 {
6646 best_low = current_low;
6647 best_high = current_high;
6648 }
6649 else
6650 {
6651 struct die_info *child = die->child;
6652
6653 while (child && child->tag)
6654 {
6655 switch (child->tag) {
6656 case DW_TAG_subprogram:
b084d499 6657 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6658 break;
6659 case DW_TAG_namespace:
f55ee35c 6660 case DW_TAG_module:
fae299cd
DC
6661 /* FIXME: carlton/2004-01-16: Should we do this for
6662 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6663 that current GCC's always emit the DIEs corresponding
6664 to definitions of methods of classes as children of a
6665 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6666 the DIEs giving the declarations, which could be
6667 anywhere). But I don't see any reason why the
6668 standards says that they have to be there. */
6669 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6670
6671 if (current_low != ((CORE_ADDR) -1))
6672 {
6673 best_low = min (best_low, current_low);
6674 best_high = max (best_high, current_high);
6675 }
6676 break;
6677 default:
0963b4bd 6678 /* Ignore. */
fae299cd
DC
6679 break;
6680 }
6681
6682 child = sibling_die (child);
6683 }
6684 }
6685
6686 *lowpc = best_low;
6687 *highpc = best_high;
6688}
6689
801e3a5b
JB
6690/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6691 in DIE. */
6692static void
6693dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6694 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6695{
6696 struct attribute *attr;
6697
6698 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6699 if (attr)
6700 {
6701 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6702
801e3a5b
JB
6703 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6704 if (attr)
6705 {
6706 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6707
801e3a5b
JB
6708 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6709 }
6710 }
6711
6712 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6713 if (attr)
6714 {
6715 bfd *obfd = cu->objfile->obfd;
6716
6717 /* The value of the DW_AT_ranges attribute is the offset of the
6718 address range list in the .debug_ranges section. */
6719 unsigned long offset = DW_UNSND (attr);
dce234bc 6720 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6721
6722 /* For some target architectures, but not others, the
6723 read_address function sign-extends the addresses it returns.
6724 To recognize base address selection entries, we need a
6725 mask. */
6726 unsigned int addr_size = cu->header.addr_size;
6727 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6728
6729 /* The base address, to which the next pair is relative. Note
6730 that this 'base' is a DWARF concept: most entries in a range
6731 list are relative, to reduce the number of relocs against the
6732 debugging information. This is separate from this function's
6733 'baseaddr' argument, which GDB uses to relocate debugging
6734 information from a shared library based on the address at
6735 which the library was loaded. */
d00adf39
DE
6736 CORE_ADDR base = cu->base_address;
6737 int base_known = cu->base_known;
801e3a5b 6738
be391dca 6739 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6740 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6741 {
6742 complaint (&symfile_complaints,
6743 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6744 offset);
6745 return;
6746 }
6747
6748 for (;;)
6749 {
6750 unsigned int bytes_read;
6751 CORE_ADDR start, end;
6752
6753 start = read_address (obfd, buffer, cu, &bytes_read);
6754 buffer += bytes_read;
6755 end = read_address (obfd, buffer, cu, &bytes_read);
6756 buffer += bytes_read;
6757
6758 /* Did we find the end of the range list? */
6759 if (start == 0 && end == 0)
6760 break;
6761
6762 /* Did we find a base address selection entry? */
6763 else if ((start & base_select_mask) == base_select_mask)
6764 {
6765 base = end;
6766 base_known = 1;
6767 }
6768
6769 /* We found an ordinary address range. */
6770 else
6771 {
6772 if (!base_known)
6773 {
6774 complaint (&symfile_complaints,
3e43a32a
MS
6775 _("Invalid .debug_ranges data "
6776 "(no base address)"));
801e3a5b
JB
6777 return;
6778 }
6779
9277c30c
UW
6780 if (start > end)
6781 {
6782 /* Inverted range entries are invalid. */
6783 complaint (&symfile_complaints,
6784 _("Invalid .debug_ranges data "
6785 "(inverted range)"));
6786 return;
6787 }
6788
6789 /* Empty range entries have no effect. */
6790 if (start == end)
6791 continue;
6792
6e70227d
DE
6793 record_block_range (block,
6794 baseaddr + base + start,
801e3a5b
JB
6795 baseaddr + base + end - 1);
6796 }
6797 }
6798 }
6799}
6800
60d5a603
JK
6801/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6802 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6803 during 4.6.0 experimental. */
6804
6805static int
6806producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6807{
6808 const char *cs;
6809 int major, minor, release;
6810
6811 if (cu->producer == NULL)
6812 {
6813 /* For unknown compilers expect their behavior is DWARF version
6814 compliant.
6815
6816 GCC started to support .debug_types sections by -gdwarf-4 since
6817 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6818 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6819 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6820 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6821
6822 return 0;
6823 }
6824
6825 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6826
6827 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6828 {
6829 /* For non-GCC compilers expect their behavior is DWARF version
6830 compliant. */
6831
6832 return 0;
6833 }
6834 cs = &cu->producer[strlen ("GNU ")];
6835 while (*cs && !isdigit (*cs))
6836 cs++;
6837 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6838 {
6839 /* Not recognized as GCC. */
6840
6841 return 0;
6842 }
6843
6844 return major < 4 || (major == 4 && minor < 6);
6845}
6846
6847/* Return the default accessibility type if it is not overriden by
6848 DW_AT_accessibility. */
6849
6850static enum dwarf_access_attribute
6851dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6852{
6853 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6854 {
6855 /* The default DWARF 2 accessibility for members is public, the default
6856 accessibility for inheritance is private. */
6857
6858 if (die->tag != DW_TAG_inheritance)
6859 return DW_ACCESS_public;
6860 else
6861 return DW_ACCESS_private;
6862 }
6863 else
6864 {
6865 /* DWARF 3+ defines the default accessibility a different way. The same
6866 rules apply now for DW_TAG_inheritance as for the members and it only
6867 depends on the container kind. */
6868
6869 if (die->parent->tag == DW_TAG_class_type)
6870 return DW_ACCESS_private;
6871 else
6872 return DW_ACCESS_public;
6873 }
6874}
6875
74ac6d43
TT
6876/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6877 offset. If the attribute was not found return 0, otherwise return
6878 1. If it was found but could not properly be handled, set *OFFSET
6879 to 0. */
6880
6881static int
6882handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6883 LONGEST *offset)
6884{
6885 struct attribute *attr;
6886
6887 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6888 if (attr != NULL)
6889 {
6890 *offset = 0;
6891
6892 /* Note that we do not check for a section offset first here.
6893 This is because DW_AT_data_member_location is new in DWARF 4,
6894 so if we see it, we can assume that a constant form is really
6895 a constant and not a section offset. */
6896 if (attr_form_is_constant (attr))
6897 *offset = dwarf2_get_attr_constant_value (attr, 0);
6898 else if (attr_form_is_section_offset (attr))
6899 dwarf2_complex_location_expr_complaint ();
6900 else if (attr_form_is_block (attr))
6901 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6902 else
6903 dwarf2_complex_location_expr_complaint ();
6904
6905 return 1;
6906 }
6907
6908 return 0;
6909}
6910
c906108c
SS
6911/* Add an aggregate field to the field list. */
6912
6913static void
107d2387 6914dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6915 struct dwarf2_cu *cu)
6e70227d 6916{
e7c27a73 6917 struct objfile *objfile = cu->objfile;
5e2b427d 6918 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6919 struct nextfield *new_field;
6920 struct attribute *attr;
6921 struct field *fp;
6922 char *fieldname = "";
6923
6924 /* Allocate a new field list entry and link it in. */
6925 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6926 make_cleanup (xfree, new_field);
c906108c 6927 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6928
6929 if (die->tag == DW_TAG_inheritance)
6930 {
6931 new_field->next = fip->baseclasses;
6932 fip->baseclasses = new_field;
6933 }
6934 else
6935 {
6936 new_field->next = fip->fields;
6937 fip->fields = new_field;
6938 }
c906108c
SS
6939 fip->nfields++;
6940
e142c38c 6941 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6942 if (attr)
6943 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
6944 else
6945 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
6946 if (new_field->accessibility != DW_ACCESS_public)
6947 fip->non_public_fields = 1;
60d5a603 6948
e142c38c 6949 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6950 if (attr)
6951 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
6952 else
6953 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
6954
6955 fp = &new_field->field;
a9a9bd0f 6956
e142c38c 6957 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6958 {
74ac6d43
TT
6959 LONGEST offset;
6960
a9a9bd0f 6961 /* Data member other than a C++ static data member. */
6e70227d 6962
c906108c 6963 /* Get type of field. */
e7c27a73 6964 fp->type = die_type (die, cu);
c906108c 6965
d6a843b5 6966 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6967
c906108c 6968 /* Get bit size of field (zero if none). */
e142c38c 6969 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6970 if (attr)
6971 {
6972 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6973 }
6974 else
6975 {
6976 FIELD_BITSIZE (*fp) = 0;
6977 }
6978
6979 /* Get bit offset of field. */
74ac6d43
TT
6980 if (handle_data_member_location (die, cu, &offset))
6981 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 6982 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6983 if (attr)
6984 {
5e2b427d 6985 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6986 {
6987 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6988 additional bit offset from the MSB of the containing
6989 anonymous object to the MSB of the field. We don't
6990 have to do anything special since we don't need to
6991 know the size of the anonymous object. */
c906108c
SS
6992 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6993 }
6994 else
6995 {
6996 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6997 MSB of the anonymous object, subtract off the number of
6998 bits from the MSB of the field to the MSB of the
6999 object, and then subtract off the number of bits of
7000 the field itself. The result is the bit offset of
7001 the LSB of the field. */
c906108c
SS
7002 int anonymous_size;
7003 int bit_offset = DW_UNSND (attr);
7004
e142c38c 7005 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7006 if (attr)
7007 {
7008 /* The size of the anonymous object containing
7009 the bit field is explicit, so use the
7010 indicated size (in bytes). */
7011 anonymous_size = DW_UNSND (attr);
7012 }
7013 else
7014 {
7015 /* The size of the anonymous object containing
7016 the bit field must be inferred from the type
7017 attribute of the data member containing the
7018 bit field. */
7019 anonymous_size = TYPE_LENGTH (fp->type);
7020 }
7021 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7022 - bit_offset - FIELD_BITSIZE (*fp);
7023 }
7024 }
7025
7026 /* Get name of field. */
39cbfefa
DJ
7027 fieldname = dwarf2_name (die, cu);
7028 if (fieldname == NULL)
7029 fieldname = "";
d8151005
DJ
7030
7031 /* The name is already allocated along with this objfile, so we don't
7032 need to duplicate it for the type. */
7033 fp->name = fieldname;
c906108c
SS
7034
7035 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 7036 pointer or virtual base class pointer) to private. */
e142c38c 7037 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 7038 {
d48cc9dd 7039 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
7040 new_field->accessibility = DW_ACCESS_private;
7041 fip->non_public_fields = 1;
7042 }
7043 }
a9a9bd0f 7044 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 7045 {
a9a9bd0f
DC
7046 /* C++ static member. */
7047
7048 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7049 is a declaration, but all versions of G++ as of this writing
7050 (so through at least 3.2.1) incorrectly generate
7051 DW_TAG_variable tags. */
6e70227d 7052
ff355380 7053 const char *physname;
c906108c 7054
a9a9bd0f 7055 /* Get name of field. */
39cbfefa
DJ
7056 fieldname = dwarf2_name (die, cu);
7057 if (fieldname == NULL)
c906108c
SS
7058 return;
7059
254e6b9e 7060 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
7061 if (attr
7062 /* Only create a symbol if this is an external value.
7063 new_symbol checks this and puts the value in the global symbol
7064 table, which we want. If it is not external, new_symbol
7065 will try to put the value in cu->list_in_scope which is wrong. */
7066 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
7067 {
7068 /* A static const member, not much different than an enum as far as
7069 we're concerned, except that we can support more types. */
7070 new_symbol (die, NULL, cu);
7071 }
7072
2df3850c 7073 /* Get physical name. */
ff355380 7074 physname = dwarf2_physname (fieldname, die, cu);
c906108c 7075
d8151005
DJ
7076 /* The name is already allocated along with this objfile, so we don't
7077 need to duplicate it for the type. */
7078 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 7079 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 7080 FIELD_NAME (*fp) = fieldname;
c906108c
SS
7081 }
7082 else if (die->tag == DW_TAG_inheritance)
7083 {
74ac6d43 7084 LONGEST offset;
d4b96c9a 7085
74ac6d43
TT
7086 /* C++ base class field. */
7087 if (handle_data_member_location (die, cu, &offset))
7088 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 7089 FIELD_BITSIZE (*fp) = 0;
e7c27a73 7090 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
7091 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7092 fip->nbaseclasses++;
7093 }
7094}
7095
98751a41
JK
7096/* Add a typedef defined in the scope of the FIP's class. */
7097
7098static void
7099dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7100 struct dwarf2_cu *cu)
6e70227d 7101{
98751a41 7102 struct objfile *objfile = cu->objfile;
98751a41
JK
7103 struct typedef_field_list *new_field;
7104 struct attribute *attr;
7105 struct typedef_field *fp;
7106 char *fieldname = "";
7107
7108 /* Allocate a new field list entry and link it in. */
7109 new_field = xzalloc (sizeof (*new_field));
7110 make_cleanup (xfree, new_field);
7111
7112 gdb_assert (die->tag == DW_TAG_typedef);
7113
7114 fp = &new_field->field;
7115
7116 /* Get name of field. */
7117 fp->name = dwarf2_name (die, cu);
7118 if (fp->name == NULL)
7119 return;
7120
7121 fp->type = read_type_die (die, cu);
7122
7123 new_field->next = fip->typedef_field_list;
7124 fip->typedef_field_list = new_field;
7125 fip->typedef_field_list_count++;
7126}
7127
c906108c
SS
7128/* Create the vector of fields, and attach it to the type. */
7129
7130static void
fba45db2 7131dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7132 struct dwarf2_cu *cu)
c906108c
SS
7133{
7134 int nfields = fip->nfields;
7135
7136 /* Record the field count, allocate space for the array of fields,
7137 and create blank accessibility bitfields if necessary. */
7138 TYPE_NFIELDS (type) = nfields;
7139 TYPE_FIELDS (type) = (struct field *)
7140 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7141 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7142
b4ba55a1 7143 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
7144 {
7145 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7146
7147 TYPE_FIELD_PRIVATE_BITS (type) =
7148 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7149 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7150
7151 TYPE_FIELD_PROTECTED_BITS (type) =
7152 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7153 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7154
774b6a14
TT
7155 TYPE_FIELD_IGNORE_BITS (type) =
7156 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7157 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
7158 }
7159
7160 /* If the type has baseclasses, allocate and clear a bit vector for
7161 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 7162 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
7163 {
7164 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 7165 unsigned char *pointer;
c906108c
SS
7166
7167 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
7168 pointer = TYPE_ALLOC (type, num_bytes);
7169 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
7170 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7171 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7172 }
7173
3e43a32a
MS
7174 /* Copy the saved-up fields into the field vector. Start from the head of
7175 the list, adding to the tail of the field array, so that they end up in
7176 the same order in the array in which they were added to the list. */
c906108c
SS
7177 while (nfields-- > 0)
7178 {
7d0ccb61
DJ
7179 struct nextfield *fieldp;
7180
7181 if (fip->fields)
7182 {
7183 fieldp = fip->fields;
7184 fip->fields = fieldp->next;
7185 }
7186 else
7187 {
7188 fieldp = fip->baseclasses;
7189 fip->baseclasses = fieldp->next;
7190 }
7191
7192 TYPE_FIELD (type, nfields) = fieldp->field;
7193 switch (fieldp->accessibility)
c906108c 7194 {
c5aa993b 7195 case DW_ACCESS_private:
b4ba55a1
JB
7196 if (cu->language != language_ada)
7197 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 7198 break;
c906108c 7199
c5aa993b 7200 case DW_ACCESS_protected:
b4ba55a1
JB
7201 if (cu->language != language_ada)
7202 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 7203 break;
c906108c 7204
c5aa993b
JM
7205 case DW_ACCESS_public:
7206 break;
c906108c 7207
c5aa993b
JM
7208 default:
7209 /* Unknown accessibility. Complain and treat it as public. */
7210 {
e2e0b3e5 7211 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 7212 fieldp->accessibility);
c5aa993b
JM
7213 }
7214 break;
c906108c
SS
7215 }
7216 if (nfields < fip->nbaseclasses)
7217 {
7d0ccb61 7218 switch (fieldp->virtuality)
c906108c 7219 {
c5aa993b
JM
7220 case DW_VIRTUALITY_virtual:
7221 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 7222 if (cu->language == language_ada)
a73c6dcd 7223 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
7224 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7225 break;
c906108c
SS
7226 }
7227 }
c906108c
SS
7228 }
7229}
7230
c906108c
SS
7231/* Add a member function to the proper fieldlist. */
7232
7233static void
107d2387 7234dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 7235 struct type *type, struct dwarf2_cu *cu)
c906108c 7236{
e7c27a73 7237 struct objfile *objfile = cu->objfile;
c906108c
SS
7238 struct attribute *attr;
7239 struct fnfieldlist *flp;
7240 int i;
7241 struct fn_field *fnp;
7242 char *fieldname;
c906108c 7243 struct nextfnfield *new_fnfield;
f792889a 7244 struct type *this_type;
60d5a603 7245 enum dwarf_access_attribute accessibility;
c906108c 7246
b4ba55a1 7247 if (cu->language == language_ada)
a73c6dcd 7248 error (_("unexpected member function in Ada type"));
b4ba55a1 7249
2df3850c 7250 /* Get name of member function. */
39cbfefa
DJ
7251 fieldname = dwarf2_name (die, cu);
7252 if (fieldname == NULL)
2df3850c 7253 return;
c906108c 7254
c906108c
SS
7255 /* Look up member function name in fieldlist. */
7256 for (i = 0; i < fip->nfnfields; i++)
7257 {
27bfe10e 7258 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
7259 break;
7260 }
7261
7262 /* Create new list element if necessary. */
7263 if (i < fip->nfnfields)
7264 flp = &fip->fnfieldlists[i];
7265 else
7266 {
7267 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7268 {
7269 fip->fnfieldlists = (struct fnfieldlist *)
7270 xrealloc (fip->fnfieldlists,
7271 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7272 * sizeof (struct fnfieldlist));
c906108c 7273 if (fip->nfnfields == 0)
c13c43fd 7274 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
7275 }
7276 flp = &fip->fnfieldlists[fip->nfnfields];
7277 flp->name = fieldname;
7278 flp->length = 0;
7279 flp->head = NULL;
3da10d80 7280 i = fip->nfnfields++;
c906108c
SS
7281 }
7282
7283 /* Create a new member function field and chain it to the field list
0963b4bd 7284 entry. */
c906108c 7285 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 7286 make_cleanup (xfree, new_fnfield);
c906108c
SS
7287 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7288 new_fnfield->next = flp->head;
7289 flp->head = new_fnfield;
7290 flp->length++;
7291
7292 /* Fill in the member function field info. */
7293 fnp = &new_fnfield->fnfield;
3da10d80
KS
7294
7295 /* Delay processing of the physname until later. */
7296 if (cu->language == language_cplus || cu->language == language_java)
7297 {
7298 add_to_method_list (type, i, flp->length - 1, fieldname,
7299 die, cu);
7300 }
7301 else
7302 {
1d06ead6 7303 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
7304 fnp->physname = physname ? physname : "";
7305 }
7306
c906108c 7307 fnp->type = alloc_type (objfile);
f792889a
DJ
7308 this_type = read_type_die (die, cu);
7309 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 7310 {
f792889a 7311 int nparams = TYPE_NFIELDS (this_type);
c906108c 7312
f792889a 7313 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7314 of the method itself (TYPE_CODE_METHOD). */
7315 smash_to_method_type (fnp->type, type,
f792889a
DJ
7316 TYPE_TARGET_TYPE (this_type),
7317 TYPE_FIELDS (this_type),
7318 TYPE_NFIELDS (this_type),
7319 TYPE_VARARGS (this_type));
c906108c
SS
7320
7321 /* Handle static member functions.
c5aa993b 7322 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7323 member functions. G++ helps GDB by marking the first
7324 parameter for non-static member functions (which is the this
7325 pointer) as artificial. We obtain this information from
7326 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7327 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7328 fnp->voffset = VOFFSET_STATIC;
7329 }
7330 else
e2e0b3e5 7331 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7332 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7333
7334 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7335 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7336 fnp->fcontext = die_containing_type (die, cu);
c906108c 7337
3e43a32a
MS
7338 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7339 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7340
7341 /* Get accessibility. */
e142c38c 7342 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7343 if (attr)
60d5a603
JK
7344 accessibility = DW_UNSND (attr);
7345 else
7346 accessibility = dwarf2_default_access_attribute (die, cu);
7347 switch (accessibility)
c906108c 7348 {
60d5a603
JK
7349 case DW_ACCESS_private:
7350 fnp->is_private = 1;
7351 break;
7352 case DW_ACCESS_protected:
7353 fnp->is_protected = 1;
7354 break;
c906108c
SS
7355 }
7356
b02dede2 7357 /* Check for artificial methods. */
e142c38c 7358 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7359 if (attr && DW_UNSND (attr) != 0)
7360 fnp->is_artificial = 1;
7361
0d564a31 7362 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7363 function. For older versions of GCC, this is an offset in the
7364 appropriate virtual table, as specified by DW_AT_containing_type.
7365 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7366 to the object address. */
7367
e142c38c 7368 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7369 if (attr)
8e19ed76 7370 {
aec5aa8b 7371 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7372 {
aec5aa8b
TT
7373 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7374 {
7375 /* Old-style GCC. */
7376 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7377 }
7378 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7379 || (DW_BLOCK (attr)->size > 1
7380 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7381 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7382 {
7383 struct dwarf_block blk;
7384 int offset;
7385
7386 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7387 ? 1 : 2);
7388 blk.size = DW_BLOCK (attr)->size - offset;
7389 blk.data = DW_BLOCK (attr)->data + offset;
7390 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7391 if ((fnp->voffset % cu->header.addr_size) != 0)
7392 dwarf2_complex_location_expr_complaint ();
7393 else
7394 fnp->voffset /= cu->header.addr_size;
7395 fnp->voffset += 2;
7396 }
7397 else
7398 dwarf2_complex_location_expr_complaint ();
7399
7400 if (!fnp->fcontext)
7401 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7402 }
3690dd37 7403 else if (attr_form_is_section_offset (attr))
8e19ed76 7404 {
4d3c2250 7405 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7406 }
7407 else
7408 {
4d3c2250
KB
7409 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7410 fieldname);
8e19ed76 7411 }
0d564a31 7412 }
d48cc9dd
DJ
7413 else
7414 {
7415 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7416 if (attr && DW_UNSND (attr))
7417 {
7418 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7419 complaint (&symfile_complaints,
3e43a32a
MS
7420 _("Member function \"%s\" (offset %d) is virtual "
7421 "but the vtable offset is not specified"),
d48cc9dd 7422 fieldname, die->offset);
9655fd1a 7423 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7424 TYPE_CPLUS_DYNAMIC (type) = 1;
7425 }
7426 }
c906108c
SS
7427}
7428
7429/* Create the vector of member function fields, and attach it to the type. */
7430
7431static void
fba45db2 7432dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7433 struct dwarf2_cu *cu)
c906108c
SS
7434{
7435 struct fnfieldlist *flp;
7436 int total_length = 0;
7437 int i;
7438
b4ba55a1 7439 if (cu->language == language_ada)
a73c6dcd 7440 error (_("unexpected member functions in Ada type"));
b4ba55a1 7441
c906108c
SS
7442 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7443 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7444 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7445
7446 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7447 {
7448 struct nextfnfield *nfp = flp->head;
7449 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7450 int k;
7451
7452 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7453 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7454 fn_flp->fn_fields = (struct fn_field *)
7455 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7456 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7457 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7458
7459 total_length += flp->length;
7460 }
7461
7462 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7463 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7464}
7465
1168df01
JB
7466/* Returns non-zero if NAME is the name of a vtable member in CU's
7467 language, zero otherwise. */
7468static int
7469is_vtable_name (const char *name, struct dwarf2_cu *cu)
7470{
7471 static const char vptr[] = "_vptr";
987504bb 7472 static const char vtable[] = "vtable";
1168df01 7473
987504bb
JJ
7474 /* Look for the C++ and Java forms of the vtable. */
7475 if ((cu->language == language_java
7476 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7477 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7478 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7479 return 1;
7480
7481 return 0;
7482}
7483
c0dd20ea 7484/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7485 functions, with the ABI-specified layout. If TYPE describes
7486 such a structure, smash it into a member function type.
61049d3b
DJ
7487
7488 GCC shouldn't do this; it should just output pointer to member DIEs.
7489 This is GCC PR debug/28767. */
c0dd20ea 7490
0b92b5bb
TT
7491static void
7492quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7493{
0b92b5bb 7494 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7495
7496 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7497 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7498 return;
c0dd20ea
DJ
7499
7500 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7501 if (TYPE_FIELD_NAME (type, 0) == NULL
7502 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7503 || TYPE_FIELD_NAME (type, 1) == NULL
7504 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7505 return;
c0dd20ea
DJ
7506
7507 /* Find the type of the method. */
0b92b5bb 7508 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7509 if (pfn_type == NULL
7510 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7511 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7512 return;
c0dd20ea
DJ
7513
7514 /* Look for the "this" argument. */
7515 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7516 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7517 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7518 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7519 return;
c0dd20ea
DJ
7520
7521 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7522 new_type = alloc_type (objfile);
7523 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7524 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7525 TYPE_VARARGS (pfn_type));
0b92b5bb 7526 smash_to_methodptr_type (type, new_type);
c0dd20ea 7527}
1168df01 7528
c906108c 7529/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7530 (definition) to create a type for the structure or union. Fill in
7531 the type's name and general properties; the members will not be
7532 processed until process_structure_type.
c906108c 7533
c767944b
DJ
7534 NOTE: we need to call these functions regardless of whether or not the
7535 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7536 structure or union. This gets the type entered into our set of
7537 user defined types.
7538
7539 However, if the structure is incomplete (an opaque struct/union)
7540 then suppress creating a symbol table entry for it since gdb only
7541 wants to find the one with the complete definition. Note that if
7542 it is complete, we just call new_symbol, which does it's own
7543 checking about whether the struct/union is anonymous or not (and
7544 suppresses creating a symbol table entry itself). */
7545
f792889a 7546static struct type *
134d01f1 7547read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7548{
e7c27a73 7549 struct objfile *objfile = cu->objfile;
c906108c
SS
7550 struct type *type;
7551 struct attribute *attr;
39cbfefa 7552 char *name;
c906108c 7553
348e048f
DE
7554 /* If the definition of this type lives in .debug_types, read that type.
7555 Don't follow DW_AT_specification though, that will take us back up
7556 the chain and we want to go down. */
7557 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7558 if (attr)
7559 {
7560 struct dwarf2_cu *type_cu = cu;
7561 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7562
348e048f
DE
7563 /* We could just recurse on read_structure_type, but we need to call
7564 get_die_type to ensure only one type for this DIE is created.
7565 This is important, for example, because for c++ classes we need
7566 TYPE_NAME set which is only done by new_symbol. Blech. */
7567 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7568
7569 /* TYPE_CU may not be the same as CU.
7570 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7571 return set_die_type (die, type, cu);
7572 }
7573
c0dd20ea 7574 type = alloc_type (objfile);
c906108c 7575 INIT_CPLUS_SPECIFIC (type);
93311388 7576
39cbfefa
DJ
7577 name = dwarf2_name (die, cu);
7578 if (name != NULL)
c906108c 7579 {
987504bb
JJ
7580 if (cu->language == language_cplus
7581 || cu->language == language_java)
63d06c5c 7582 {
3da10d80
KS
7583 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7584
7585 /* dwarf2_full_name might have already finished building the DIE's
7586 type. If so, there is no need to continue. */
7587 if (get_die_type (die, cu) != NULL)
7588 return get_die_type (die, cu);
7589
7590 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7591 if (die->tag == DW_TAG_structure_type
7592 || die->tag == DW_TAG_class_type)
7593 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7594 }
7595 else
7596 {
d8151005
DJ
7597 /* The name is already allocated along with this objfile, so
7598 we don't need to duplicate it for the type. */
94af9270
KS
7599 TYPE_TAG_NAME (type) = (char *) name;
7600 if (die->tag == DW_TAG_class_type)
7601 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7602 }
c906108c
SS
7603 }
7604
7605 if (die->tag == DW_TAG_structure_type)
7606 {
7607 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7608 }
7609 else if (die->tag == DW_TAG_union_type)
7610 {
7611 TYPE_CODE (type) = TYPE_CODE_UNION;
7612 }
7613 else
7614 {
c906108c
SS
7615 TYPE_CODE (type) = TYPE_CODE_CLASS;
7616 }
7617
0cc2414c
TT
7618 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7619 TYPE_DECLARED_CLASS (type) = 1;
7620
e142c38c 7621 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7622 if (attr)
7623 {
7624 TYPE_LENGTH (type) = DW_UNSND (attr);
7625 }
7626 else
7627 {
7628 TYPE_LENGTH (type) = 0;
7629 }
7630
876cecd0 7631 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7632 if (die_is_declaration (die, cu))
876cecd0 7633 TYPE_STUB (type) = 1;
a6c727b2
DJ
7634 else if (attr == NULL && die->child == NULL
7635 && producer_is_realview (cu->producer))
7636 /* RealView does not output the required DW_AT_declaration
7637 on incomplete types. */
7638 TYPE_STUB (type) = 1;
dc718098 7639
c906108c
SS
7640 /* We need to add the type field to the die immediately so we don't
7641 infinitely recurse when dealing with pointers to the structure
0963b4bd 7642 type within the structure itself. */
1c379e20 7643 set_die_type (die, type, cu);
c906108c 7644
7e314c57
JK
7645 /* set_die_type should be already done. */
7646 set_descriptive_type (type, die, cu);
7647
c767944b
DJ
7648 return type;
7649}
7650
7651/* Finish creating a structure or union type, including filling in
7652 its members and creating a symbol for it. */
7653
7654static void
7655process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7656{
7657 struct objfile *objfile = cu->objfile;
7658 struct die_info *child_die = die->child;
7659 struct type *type;
7660
7661 type = get_die_type (die, cu);
7662 if (type == NULL)
7663 type = read_structure_type (die, cu);
7664
e142c38c 7665 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7666 {
7667 struct field_info fi;
7668 struct die_info *child_die;
34eaf542 7669 VEC (symbolp) *template_args = NULL;
c767944b 7670 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7671
7672 memset (&fi, 0, sizeof (struct field_info));
7673
639d11d3 7674 child_die = die->child;
c906108c
SS
7675
7676 while (child_die && child_die->tag)
7677 {
a9a9bd0f
DC
7678 if (child_die->tag == DW_TAG_member
7679 || child_die->tag == DW_TAG_variable)
c906108c 7680 {
a9a9bd0f
DC
7681 /* NOTE: carlton/2002-11-05: A C++ static data member
7682 should be a DW_TAG_member that is a declaration, but
7683 all versions of G++ as of this writing (so through at
7684 least 3.2.1) incorrectly generate DW_TAG_variable
7685 tags for them instead. */
e7c27a73 7686 dwarf2_add_field (&fi, child_die, cu);
c906108c 7687 }
8713b1b1 7688 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7689 {
0963b4bd 7690 /* C++ member function. */
e7c27a73 7691 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7692 }
7693 else if (child_die->tag == DW_TAG_inheritance)
7694 {
7695 /* C++ base class field. */
e7c27a73 7696 dwarf2_add_field (&fi, child_die, cu);
c906108c 7697 }
98751a41
JK
7698 else if (child_die->tag == DW_TAG_typedef)
7699 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7700 else if (child_die->tag == DW_TAG_template_type_param
7701 || child_die->tag == DW_TAG_template_value_param)
7702 {
7703 struct symbol *arg = new_symbol (child_die, NULL, cu);
7704
f1078f66
DJ
7705 if (arg != NULL)
7706 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7707 }
7708
c906108c
SS
7709 child_die = sibling_die (child_die);
7710 }
7711
34eaf542
TT
7712 /* Attach template arguments to type. */
7713 if (! VEC_empty (symbolp, template_args))
7714 {
7715 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7716 TYPE_N_TEMPLATE_ARGUMENTS (type)
7717 = VEC_length (symbolp, template_args);
7718 TYPE_TEMPLATE_ARGUMENTS (type)
7719 = obstack_alloc (&objfile->objfile_obstack,
7720 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7721 * sizeof (struct symbol *)));
7722 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7723 VEC_address (symbolp, template_args),
7724 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7725 * sizeof (struct symbol *)));
7726 VEC_free (symbolp, template_args);
7727 }
7728
c906108c
SS
7729 /* Attach fields and member functions to the type. */
7730 if (fi.nfields)
e7c27a73 7731 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7732 if (fi.nfnfields)
7733 {
e7c27a73 7734 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7735
c5aa993b 7736 /* Get the type which refers to the base class (possibly this
c906108c 7737 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7738 class from the DW_AT_containing_type attribute. This use of
7739 DW_AT_containing_type is a GNU extension. */
c906108c 7740
e142c38c 7741 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7742 {
e7c27a73 7743 struct type *t = die_containing_type (die, cu);
c906108c
SS
7744
7745 TYPE_VPTR_BASETYPE (type) = t;
7746 if (type == t)
7747 {
c906108c
SS
7748 int i;
7749
7750 /* Our own class provides vtbl ptr. */
7751 for (i = TYPE_NFIELDS (t) - 1;
7752 i >= TYPE_N_BASECLASSES (t);
7753 --i)
7754 {
7755 char *fieldname = TYPE_FIELD_NAME (t, i);
7756
1168df01 7757 if (is_vtable_name (fieldname, cu))
c906108c
SS
7758 {
7759 TYPE_VPTR_FIELDNO (type) = i;
7760 break;
7761 }
7762 }
7763
7764 /* Complain if virtual function table field not found. */
7765 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7766 complaint (&symfile_complaints,
3e43a32a
MS
7767 _("virtual function table pointer "
7768 "not found when defining class '%s'"),
4d3c2250
KB
7769 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7770 "");
c906108c
SS
7771 }
7772 else
7773 {
7774 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7775 }
7776 }
f6235d4c
EZ
7777 else if (cu->producer
7778 && strncmp (cu->producer,
7779 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7780 {
7781 /* The IBM XLC compiler does not provide direct indication
7782 of the containing type, but the vtable pointer is
7783 always named __vfp. */
7784
7785 int i;
7786
7787 for (i = TYPE_NFIELDS (type) - 1;
7788 i >= TYPE_N_BASECLASSES (type);
7789 --i)
7790 {
7791 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7792 {
7793 TYPE_VPTR_FIELDNO (type) = i;
7794 TYPE_VPTR_BASETYPE (type) = type;
7795 break;
7796 }
7797 }
7798 }
c906108c 7799 }
98751a41
JK
7800
7801 /* Copy fi.typedef_field_list linked list elements content into the
7802 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7803 if (fi.typedef_field_list)
7804 {
7805 int i = fi.typedef_field_list_count;
7806
a0d7a4ff 7807 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7808 TYPE_TYPEDEF_FIELD_ARRAY (type)
7809 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7810 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7811
7812 /* Reverse the list order to keep the debug info elements order. */
7813 while (--i >= 0)
7814 {
7815 struct typedef_field *dest, *src;
6e70227d 7816
98751a41
JK
7817 dest = &TYPE_TYPEDEF_FIELD (type, i);
7818 src = &fi.typedef_field_list->field;
7819 fi.typedef_field_list = fi.typedef_field_list->next;
7820 *dest = *src;
7821 }
7822 }
c767944b
DJ
7823
7824 do_cleanups (back_to);
eb2a6f42
TT
7825
7826 if (HAVE_CPLUS_STRUCT (type))
7827 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 7828 }
63d06c5c 7829
0b92b5bb
TT
7830 quirk_gcc_member_function_pointer (type, cu->objfile);
7831
90aeadfc
DC
7832 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7833 snapshots) has been known to create a die giving a declaration
7834 for a class that has, as a child, a die giving a definition for a
7835 nested class. So we have to process our children even if the
7836 current die is a declaration. Normally, of course, a declaration
7837 won't have any children at all. */
134d01f1 7838
90aeadfc
DC
7839 while (child_die != NULL && child_die->tag)
7840 {
7841 if (child_die->tag == DW_TAG_member
7842 || child_die->tag == DW_TAG_variable
34eaf542
TT
7843 || child_die->tag == DW_TAG_inheritance
7844 || child_die->tag == DW_TAG_template_value_param
7845 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7846 {
90aeadfc 7847 /* Do nothing. */
134d01f1 7848 }
90aeadfc
DC
7849 else
7850 process_die (child_die, cu);
134d01f1 7851
90aeadfc 7852 child_die = sibling_die (child_die);
134d01f1
DJ
7853 }
7854
fa4028e9
JB
7855 /* Do not consider external references. According to the DWARF standard,
7856 these DIEs are identified by the fact that they have no byte_size
7857 attribute, and a declaration attribute. */
7858 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7859 || !die_is_declaration (die, cu))
c767944b 7860 new_symbol (die, type, cu);
134d01f1
DJ
7861}
7862
7863/* Given a DW_AT_enumeration_type die, set its type. We do not
7864 complete the type's fields yet, or create any symbols. */
c906108c 7865
f792889a 7866static struct type *
134d01f1 7867read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7868{
e7c27a73 7869 struct objfile *objfile = cu->objfile;
c906108c 7870 struct type *type;
c906108c 7871 struct attribute *attr;
0114d602 7872 const char *name;
134d01f1 7873
348e048f
DE
7874 /* If the definition of this type lives in .debug_types, read that type.
7875 Don't follow DW_AT_specification though, that will take us back up
7876 the chain and we want to go down. */
7877 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7878 if (attr)
7879 {
7880 struct dwarf2_cu *type_cu = cu;
7881 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7882
348e048f 7883 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7884
7885 /* TYPE_CU may not be the same as CU.
7886 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7887 return set_die_type (die, type, cu);
7888 }
7889
c906108c
SS
7890 type = alloc_type (objfile);
7891
7892 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7893 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7894 if (name != NULL)
0114d602 7895 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7896
e142c38c 7897 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7898 if (attr)
7899 {
7900 TYPE_LENGTH (type) = DW_UNSND (attr);
7901 }
7902 else
7903 {
7904 TYPE_LENGTH (type) = 0;
7905 }
7906
137033e9
JB
7907 /* The enumeration DIE can be incomplete. In Ada, any type can be
7908 declared as private in the package spec, and then defined only
7909 inside the package body. Such types are known as Taft Amendment
7910 Types. When another package uses such a type, an incomplete DIE
7911 may be generated by the compiler. */
02eb380e 7912 if (die_is_declaration (die, cu))
876cecd0 7913 TYPE_STUB (type) = 1;
02eb380e 7914
f792889a 7915 return set_die_type (die, type, cu);
134d01f1
DJ
7916}
7917
7918/* Given a pointer to a die which begins an enumeration, process all
7919 the dies that define the members of the enumeration, and create the
7920 symbol for the enumeration type.
7921
7922 NOTE: We reverse the order of the element list. */
7923
7924static void
7925process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7926{
f792889a 7927 struct type *this_type;
134d01f1 7928
f792889a
DJ
7929 this_type = get_die_type (die, cu);
7930 if (this_type == NULL)
7931 this_type = read_enumeration_type (die, cu);
9dc481d3 7932
639d11d3 7933 if (die->child != NULL)
c906108c 7934 {
9dc481d3
DE
7935 struct die_info *child_die;
7936 struct symbol *sym;
7937 struct field *fields = NULL;
7938 int num_fields = 0;
7939 int unsigned_enum = 1;
7940 char *name;
7941
639d11d3 7942 child_die = die->child;
c906108c
SS
7943 while (child_die && child_die->tag)
7944 {
7945 if (child_die->tag != DW_TAG_enumerator)
7946 {
e7c27a73 7947 process_die (child_die, cu);
c906108c
SS
7948 }
7949 else
7950 {
39cbfefa
DJ
7951 name = dwarf2_name (child_die, cu);
7952 if (name)
c906108c 7953 {
f792889a 7954 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7955 if (SYMBOL_VALUE (sym) < 0)
7956 unsigned_enum = 0;
7957
7958 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7959 {
7960 fields = (struct field *)
7961 xrealloc (fields,
7962 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7963 * sizeof (struct field));
c906108c
SS
7964 }
7965
3567439c 7966 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7967 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7968 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7969 FIELD_BITSIZE (fields[num_fields]) = 0;
7970
7971 num_fields++;
7972 }
7973 }
7974
7975 child_die = sibling_die (child_die);
7976 }
7977
7978 if (num_fields)
7979 {
f792889a
DJ
7980 TYPE_NFIELDS (this_type) = num_fields;
7981 TYPE_FIELDS (this_type) = (struct field *)
7982 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7983 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7984 sizeof (struct field) * num_fields);
b8c9b27d 7985 xfree (fields);
c906108c
SS
7986 }
7987 if (unsigned_enum)
876cecd0 7988 TYPE_UNSIGNED (this_type) = 1;
c906108c 7989 }
134d01f1 7990
6c83ed52
TT
7991 /* If we are reading an enum from a .debug_types unit, and the enum
7992 is a declaration, and the enum is not the signatured type in the
7993 unit, then we do not want to add a symbol for it. Adding a
7994 symbol would in some cases obscure the true definition of the
7995 enum, giving users an incomplete type when the definition is
7996 actually available. Note that we do not want to do this for all
7997 enums which are just declarations, because C++0x allows forward
7998 enum declarations. */
7999 if (cu->per_cu->debug_type_section
8000 && die_is_declaration (die, cu))
8001 {
8002 struct signatured_type *type_sig;
8003
8004 type_sig
8005 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8006 cu->per_cu->debug_type_section,
8007 cu->per_cu->offset);
8008 if (type_sig->type_offset != die->offset)
8009 return;
8010 }
8011
f792889a 8012 new_symbol (die, this_type, cu);
c906108c
SS
8013}
8014
8015/* Extract all information from a DW_TAG_array_type DIE and put it in
8016 the DIE's type field. For now, this only handles one dimensional
8017 arrays. */
8018
f792889a 8019static struct type *
e7c27a73 8020read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8021{
e7c27a73 8022 struct objfile *objfile = cu->objfile;
c906108c 8023 struct die_info *child_die;
7e314c57 8024 struct type *type;
c906108c
SS
8025 struct type *element_type, *range_type, *index_type;
8026 struct type **range_types = NULL;
8027 struct attribute *attr;
8028 int ndim = 0;
8029 struct cleanup *back_to;
39cbfefa 8030 char *name;
c906108c 8031
e7c27a73 8032 element_type = die_type (die, cu);
c906108c 8033
7e314c57
JK
8034 /* The die_type call above may have already set the type for this DIE. */
8035 type = get_die_type (die, cu);
8036 if (type)
8037 return type;
8038
c906108c
SS
8039 /* Irix 6.2 native cc creates array types without children for
8040 arrays with unspecified length. */
639d11d3 8041 if (die->child == NULL)
c906108c 8042 {
46bf5051 8043 index_type = objfile_type (objfile)->builtin_int;
c906108c 8044 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
8045 type = create_array_type (NULL, element_type, range_type);
8046 return set_die_type (die, type, cu);
c906108c
SS
8047 }
8048
8049 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 8050 child_die = die->child;
c906108c
SS
8051 while (child_die && child_die->tag)
8052 {
8053 if (child_die->tag == DW_TAG_subrange_type)
8054 {
f792889a 8055 struct type *child_type = read_type_die (child_die, cu);
9a619af0 8056
f792889a 8057 if (child_type != NULL)
a02abb62 8058 {
0963b4bd
MS
8059 /* The range type was succesfully read. Save it for the
8060 array type creation. */
a02abb62
JB
8061 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8062 {
8063 range_types = (struct type **)
8064 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8065 * sizeof (struct type *));
8066 if (ndim == 0)
8067 make_cleanup (free_current_contents, &range_types);
8068 }
f792889a 8069 range_types[ndim++] = child_type;
a02abb62 8070 }
c906108c
SS
8071 }
8072 child_die = sibling_die (child_die);
8073 }
8074
8075 /* Dwarf2 dimensions are output from left to right, create the
8076 necessary array types in backwards order. */
7ca2d3a3 8077
c906108c 8078 type = element_type;
7ca2d3a3
DL
8079
8080 if (read_array_order (die, cu) == DW_ORD_col_major)
8081 {
8082 int i = 0;
9a619af0 8083
7ca2d3a3
DL
8084 while (i < ndim)
8085 type = create_array_type (NULL, type, range_types[i++]);
8086 }
8087 else
8088 {
8089 while (ndim-- > 0)
8090 type = create_array_type (NULL, type, range_types[ndim]);
8091 }
c906108c 8092
f5f8a009
EZ
8093 /* Understand Dwarf2 support for vector types (like they occur on
8094 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8095 array type. This is not part of the Dwarf2/3 standard yet, but a
8096 custom vendor extension. The main difference between a regular
8097 array and the vector variant is that vectors are passed by value
8098 to functions. */
e142c38c 8099 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 8100 if (attr)
ea37ba09 8101 make_vector_type (type);
f5f8a009 8102
dbc98a8b
KW
8103 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8104 implementation may choose to implement triple vectors using this
8105 attribute. */
8106 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8107 if (attr)
8108 {
8109 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8110 TYPE_LENGTH (type) = DW_UNSND (attr);
8111 else
3e43a32a
MS
8112 complaint (&symfile_complaints,
8113 _("DW_AT_byte_size for array type smaller "
8114 "than the total size of elements"));
dbc98a8b
KW
8115 }
8116
39cbfefa
DJ
8117 name = dwarf2_name (die, cu);
8118 if (name)
8119 TYPE_NAME (type) = name;
6e70227d 8120
0963b4bd 8121 /* Install the type in the die. */
7e314c57
JK
8122 set_die_type (die, type, cu);
8123
8124 /* set_die_type should be already done. */
b4ba55a1
JB
8125 set_descriptive_type (type, die, cu);
8126
c906108c
SS
8127 do_cleanups (back_to);
8128
7e314c57 8129 return type;
c906108c
SS
8130}
8131
7ca2d3a3 8132static enum dwarf_array_dim_ordering
6e70227d 8133read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
8134{
8135 struct attribute *attr;
8136
8137 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8138
8139 if (attr) return DW_SND (attr);
8140
0963b4bd
MS
8141 /* GNU F77 is a special case, as at 08/2004 array type info is the
8142 opposite order to the dwarf2 specification, but data is still
8143 laid out as per normal fortran.
7ca2d3a3 8144
0963b4bd
MS
8145 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8146 version checking. */
7ca2d3a3 8147
905e0470
PM
8148 if (cu->language == language_fortran
8149 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
8150 {
8151 return DW_ORD_row_major;
8152 }
8153
6e70227d 8154 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
8155 {
8156 case array_column_major:
8157 return DW_ORD_col_major;
8158 case array_row_major:
8159 default:
8160 return DW_ORD_row_major;
8161 };
8162}
8163
72019c9c 8164/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 8165 the DIE's type field. */
72019c9c 8166
f792889a 8167static struct type *
72019c9c
GM
8168read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8169{
7e314c57
JK
8170 struct type *domain_type, *set_type;
8171 struct attribute *attr;
f792889a 8172
7e314c57
JK
8173 domain_type = die_type (die, cu);
8174
8175 /* The die_type call above may have already set the type for this DIE. */
8176 set_type = get_die_type (die, cu);
8177 if (set_type)
8178 return set_type;
8179
8180 set_type = create_set_type (NULL, domain_type);
8181
8182 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
8183 if (attr)
8184 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 8185
f792889a 8186 return set_die_type (die, set_type, cu);
72019c9c 8187}
7ca2d3a3 8188
c906108c
SS
8189/* First cut: install each common block member as a global variable. */
8190
8191static void
e7c27a73 8192read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8193{
8194 struct die_info *child_die;
8195 struct attribute *attr;
8196 struct symbol *sym;
8197 CORE_ADDR base = (CORE_ADDR) 0;
8198
e142c38c 8199 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8200 if (attr)
8201 {
0963b4bd 8202 /* Support the .debug_loc offsets. */
8e19ed76
PS
8203 if (attr_form_is_block (attr))
8204 {
e7c27a73 8205 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 8206 }
3690dd37 8207 else if (attr_form_is_section_offset (attr))
8e19ed76 8208 {
4d3c2250 8209 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8210 }
8211 else
8212 {
4d3c2250
KB
8213 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8214 "common block member");
8e19ed76 8215 }
c906108c 8216 }
639d11d3 8217 if (die->child != NULL)
c906108c 8218 {
639d11d3 8219 child_die = die->child;
c906108c
SS
8220 while (child_die && child_die->tag)
8221 {
74ac6d43
TT
8222 LONGEST offset;
8223
e7c27a73 8224 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
8225 if (sym != NULL
8226 && handle_data_member_location (child_die, cu, &offset))
c906108c 8227 {
74ac6d43 8228 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
8229 add_symbol_to_list (sym, &global_symbols);
8230 }
8231 child_die = sibling_die (child_die);
8232 }
8233 }
8234}
8235
0114d602 8236/* Create a type for a C++ namespace. */
d9fa45fe 8237
0114d602
DJ
8238static struct type *
8239read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 8240{
e7c27a73 8241 struct objfile *objfile = cu->objfile;
0114d602 8242 const char *previous_prefix, *name;
9219021c 8243 int is_anonymous;
0114d602
DJ
8244 struct type *type;
8245
8246 /* For extensions, reuse the type of the original namespace. */
8247 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8248 {
8249 struct die_info *ext_die;
8250 struct dwarf2_cu *ext_cu = cu;
9a619af0 8251
0114d602
DJ
8252 ext_die = dwarf2_extension (die, &ext_cu);
8253 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
8254
8255 /* EXT_CU may not be the same as CU.
8256 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
8257 return set_die_type (die, type, cu);
8258 }
9219021c 8259
e142c38c 8260 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
8261
8262 /* Now build the name of the current namespace. */
8263
0114d602
DJ
8264 previous_prefix = determine_prefix (die, cu);
8265 if (previous_prefix[0] != '\0')
8266 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 8267 previous_prefix, name, 0, cu);
0114d602
DJ
8268
8269 /* Create the type. */
8270 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8271 objfile);
8272 TYPE_NAME (type) = (char *) name;
8273 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8274
60531b24 8275 return set_die_type (die, type, cu);
0114d602
DJ
8276}
8277
8278/* Read a C++ namespace. */
8279
8280static void
8281read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8282{
8283 struct objfile *objfile = cu->objfile;
0114d602 8284 int is_anonymous;
9219021c 8285
5c4e30ca
DC
8286 /* Add a symbol associated to this if we haven't seen the namespace
8287 before. Also, add a using directive if it's an anonymous
8288 namespace. */
9219021c 8289
f2f0e013 8290 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
8291 {
8292 struct type *type;
8293
0114d602 8294 type = read_type_die (die, cu);
e7c27a73 8295 new_symbol (die, type, cu);
5c4e30ca 8296
e8e80198 8297 namespace_name (die, &is_anonymous, cu);
5c4e30ca 8298 if (is_anonymous)
0114d602
DJ
8299 {
8300 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 8301
c0cc3a76 8302 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 8303 NULL, NULL, &objfile->objfile_obstack);
0114d602 8304 }
5c4e30ca 8305 }
9219021c 8306
639d11d3 8307 if (die->child != NULL)
d9fa45fe 8308 {
639d11d3 8309 struct die_info *child_die = die->child;
6e70227d 8310
d9fa45fe
DC
8311 while (child_die && child_die->tag)
8312 {
e7c27a73 8313 process_die (child_die, cu);
d9fa45fe
DC
8314 child_die = sibling_die (child_die);
8315 }
8316 }
38d518c9
EZ
8317}
8318
f55ee35c
JK
8319/* Read a Fortran module as type. This DIE can be only a declaration used for
8320 imported module. Still we need that type as local Fortran "use ... only"
8321 declaration imports depend on the created type in determine_prefix. */
8322
8323static struct type *
8324read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8325{
8326 struct objfile *objfile = cu->objfile;
8327 char *module_name;
8328 struct type *type;
8329
8330 module_name = dwarf2_name (die, cu);
8331 if (!module_name)
3e43a32a
MS
8332 complaint (&symfile_complaints,
8333 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
8334 die->offset);
8335 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8336
8337 /* determine_prefix uses TYPE_TAG_NAME. */
8338 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8339
8340 return set_die_type (die, type, cu);
8341}
8342
5d7cb8df
JK
8343/* Read a Fortran module. */
8344
8345static void
8346read_module (struct die_info *die, struct dwarf2_cu *cu)
8347{
8348 struct die_info *child_die = die->child;
8349
5d7cb8df
JK
8350 while (child_die && child_die->tag)
8351 {
8352 process_die (child_die, cu);
8353 child_die = sibling_die (child_die);
8354 }
8355}
8356
38d518c9
EZ
8357/* Return the name of the namespace represented by DIE. Set
8358 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8359 namespace. */
8360
8361static const char *
e142c38c 8362namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8363{
8364 struct die_info *current_die;
8365 const char *name = NULL;
8366
8367 /* Loop through the extensions until we find a name. */
8368
8369 for (current_die = die;
8370 current_die != NULL;
f2f0e013 8371 current_die = dwarf2_extension (die, &cu))
38d518c9 8372 {
e142c38c 8373 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8374 if (name != NULL)
8375 break;
8376 }
8377
8378 /* Is it an anonymous namespace? */
8379
8380 *is_anonymous = (name == NULL);
8381 if (*is_anonymous)
2b1dbab0 8382 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8383
8384 return name;
d9fa45fe
DC
8385}
8386
c906108c
SS
8387/* Extract all information from a DW_TAG_pointer_type DIE and add to
8388 the user defined type vector. */
8389
f792889a 8390static struct type *
e7c27a73 8391read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8392{
5e2b427d 8393 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8394 struct comp_unit_head *cu_header = &cu->header;
c906108c 8395 struct type *type;
8b2dbe47
KB
8396 struct attribute *attr_byte_size;
8397 struct attribute *attr_address_class;
8398 int byte_size, addr_class;
7e314c57
JK
8399 struct type *target_type;
8400
8401 target_type = die_type (die, cu);
c906108c 8402
7e314c57
JK
8403 /* The die_type call above may have already set the type for this DIE. */
8404 type = get_die_type (die, cu);
8405 if (type)
8406 return type;
8407
8408 type = lookup_pointer_type (target_type);
8b2dbe47 8409
e142c38c 8410 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8411 if (attr_byte_size)
8412 byte_size = DW_UNSND (attr_byte_size);
c906108c 8413 else
8b2dbe47
KB
8414 byte_size = cu_header->addr_size;
8415
e142c38c 8416 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8417 if (attr_address_class)
8418 addr_class = DW_UNSND (attr_address_class);
8419 else
8420 addr_class = DW_ADDR_none;
8421
8422 /* If the pointer size or address class is different than the
8423 default, create a type variant marked as such and set the
8424 length accordingly. */
8425 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8426 {
5e2b427d 8427 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8428 {
8429 int type_flags;
8430
849957d9 8431 type_flags = gdbarch_address_class_type_flags
5e2b427d 8432 (gdbarch, byte_size, addr_class);
876cecd0
TT
8433 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8434 == 0);
8b2dbe47
KB
8435 type = make_type_with_address_space (type, type_flags);
8436 }
8437 else if (TYPE_LENGTH (type) != byte_size)
8438 {
3e43a32a
MS
8439 complaint (&symfile_complaints,
8440 _("invalid pointer size %d"), byte_size);
8b2dbe47 8441 }
6e70227d 8442 else
9a619af0
MS
8443 {
8444 /* Should we also complain about unhandled address classes? */
8445 }
c906108c 8446 }
8b2dbe47
KB
8447
8448 TYPE_LENGTH (type) = byte_size;
f792889a 8449 return set_die_type (die, type, cu);
c906108c
SS
8450}
8451
8452/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8453 the user defined type vector. */
8454
f792889a 8455static struct type *
e7c27a73 8456read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8457{
8458 struct type *type;
8459 struct type *to_type;
8460 struct type *domain;
8461
e7c27a73
DJ
8462 to_type = die_type (die, cu);
8463 domain = die_containing_type (die, cu);
0d5de010 8464
7e314c57
JK
8465 /* The calls above may have already set the type for this DIE. */
8466 type = get_die_type (die, cu);
8467 if (type)
8468 return type;
8469
0d5de010
DJ
8470 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8471 type = lookup_methodptr_type (to_type);
8472 else
8473 type = lookup_memberptr_type (to_type, domain);
c906108c 8474
f792889a 8475 return set_die_type (die, type, cu);
c906108c
SS
8476}
8477
8478/* Extract all information from a DW_TAG_reference_type DIE and add to
8479 the user defined type vector. */
8480
f792889a 8481static struct type *
e7c27a73 8482read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8483{
e7c27a73 8484 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8485 struct type *type, *target_type;
c906108c
SS
8486 struct attribute *attr;
8487
7e314c57
JK
8488 target_type = die_type (die, cu);
8489
8490 /* The die_type call above may have already set the type for this DIE. */
8491 type = get_die_type (die, cu);
8492 if (type)
8493 return type;
8494
8495 type = lookup_reference_type (target_type);
e142c38c 8496 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8497 if (attr)
8498 {
8499 TYPE_LENGTH (type) = DW_UNSND (attr);
8500 }
8501 else
8502 {
107d2387 8503 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8504 }
f792889a 8505 return set_die_type (die, type, cu);
c906108c
SS
8506}
8507
f792889a 8508static struct type *
e7c27a73 8509read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8510{
f792889a 8511 struct type *base_type, *cv_type;
c906108c 8512
e7c27a73 8513 base_type = die_type (die, cu);
7e314c57
JK
8514
8515 /* The die_type call above may have already set the type for this DIE. */
8516 cv_type = get_die_type (die, cu);
8517 if (cv_type)
8518 return cv_type;
8519
2f608a3a
KW
8520 /* In case the const qualifier is applied to an array type, the element type
8521 is so qualified, not the array type (section 6.7.3 of C99). */
8522 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8523 {
8524 struct type *el_type, *inner_array;
8525
8526 base_type = copy_type (base_type);
8527 inner_array = base_type;
8528
8529 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8530 {
8531 TYPE_TARGET_TYPE (inner_array) =
8532 copy_type (TYPE_TARGET_TYPE (inner_array));
8533 inner_array = TYPE_TARGET_TYPE (inner_array);
8534 }
8535
8536 el_type = TYPE_TARGET_TYPE (inner_array);
8537 TYPE_TARGET_TYPE (inner_array) =
8538 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8539
8540 return set_die_type (die, base_type, cu);
8541 }
8542
f792889a
DJ
8543 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8544 return set_die_type (die, cv_type, cu);
c906108c
SS
8545}
8546
f792889a 8547static struct type *
e7c27a73 8548read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8549{
f792889a 8550 struct type *base_type, *cv_type;
c906108c 8551
e7c27a73 8552 base_type = die_type (die, cu);
7e314c57
JK
8553
8554 /* The die_type call above may have already set the type for this DIE. */
8555 cv_type = get_die_type (die, cu);
8556 if (cv_type)
8557 return cv_type;
8558
f792889a
DJ
8559 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8560 return set_die_type (die, cv_type, cu);
c906108c
SS
8561}
8562
8563/* Extract all information from a DW_TAG_string_type DIE and add to
8564 the user defined type vector. It isn't really a user defined type,
8565 but it behaves like one, with other DIE's using an AT_user_def_type
8566 attribute to reference it. */
8567
f792889a 8568static struct type *
e7c27a73 8569read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8570{
e7c27a73 8571 struct objfile *objfile = cu->objfile;
3b7538c0 8572 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8573 struct type *type, *range_type, *index_type, *char_type;
8574 struct attribute *attr;
8575 unsigned int length;
8576
e142c38c 8577 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8578 if (attr)
8579 {
8580 length = DW_UNSND (attr);
8581 }
8582 else
8583 {
0963b4bd 8584 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8585 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8586 if (attr)
8587 {
8588 length = DW_UNSND (attr);
8589 }
8590 else
8591 {
8592 length = 1;
8593 }
c906108c 8594 }
6ccb9162 8595
46bf5051 8596 index_type = objfile_type (objfile)->builtin_int;
c906108c 8597 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8598 char_type = language_string_char_type (cu->language_defn, gdbarch);
8599 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8600
f792889a 8601 return set_die_type (die, type, cu);
c906108c
SS
8602}
8603
8604/* Handle DIES due to C code like:
8605
8606 struct foo
c5aa993b
JM
8607 {
8608 int (*funcp)(int a, long l);
8609 int b;
8610 };
c906108c 8611
0963b4bd 8612 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8613
f792889a 8614static struct type *
e7c27a73 8615read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8616{
0963b4bd
MS
8617 struct type *type; /* Type that this function returns. */
8618 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8619 struct attribute *attr;
8620
e7c27a73 8621 type = die_type (die, cu);
7e314c57
JK
8622
8623 /* The die_type call above may have already set the type for this DIE. */
8624 ftype = get_die_type (die, cu);
8625 if (ftype)
8626 return ftype;
8627
0c8b41f1 8628 ftype = lookup_function_type (type);
c906108c 8629
5b8101ae 8630 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8631 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8632 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8633 || cu->language == language_cplus
5b8101ae
PM
8634 || cu->language == language_java
8635 || cu->language == language_pascal)
876cecd0 8636 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8637 else if (producer_is_realview (cu->producer))
8638 /* RealView does not emit DW_AT_prototyped. We can not
8639 distinguish prototyped and unprototyped functions; default to
8640 prototyped, since that is more common in modern code (and
8641 RealView warns about unprototyped functions). */
8642 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8643
c055b101
CV
8644 /* Store the calling convention in the type if it's available in
8645 the subroutine die. Otherwise set the calling convention to
8646 the default value DW_CC_normal. */
8647 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8648 if (attr)
8649 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8650 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8651 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8652 else
8653 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8654
8655 /* We need to add the subroutine type to the die immediately so
8656 we don't infinitely recurse when dealing with parameters
0963b4bd 8657 declared as the same subroutine type. */
76c10ea2 8658 set_die_type (die, ftype, cu);
6e70227d 8659
639d11d3 8660 if (die->child != NULL)
c906108c 8661 {
8072405b 8662 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 8663 struct die_info *child_die;
8072405b 8664 int nparams, iparams;
c906108c
SS
8665
8666 /* Count the number of parameters.
8667 FIXME: GDB currently ignores vararg functions, but knows about
8668 vararg member functions. */
8072405b 8669 nparams = 0;
639d11d3 8670 child_die = die->child;
c906108c
SS
8671 while (child_die && child_die->tag)
8672 {
8673 if (child_die->tag == DW_TAG_formal_parameter)
8674 nparams++;
8675 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8676 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8677 child_die = sibling_die (child_die);
8678 }
8679
8680 /* Allocate storage for parameters and fill them in. */
8681 TYPE_NFIELDS (ftype) = nparams;
8682 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8683 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8684
8072405b
JK
8685 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8686 even if we error out during the parameters reading below. */
8687 for (iparams = 0; iparams < nparams; iparams++)
8688 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8689
8690 iparams = 0;
639d11d3 8691 child_die = die->child;
c906108c
SS
8692 while (child_die && child_die->tag)
8693 {
8694 if (child_die->tag == DW_TAG_formal_parameter)
8695 {
3ce3b1ba
PA
8696 struct type *arg_type;
8697
8698 /* DWARF version 2 has no clean way to discern C++
8699 static and non-static member functions. G++ helps
8700 GDB by marking the first parameter for non-static
8701 member functions (which is the this pointer) as
8702 artificial. We pass this information to
8703 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8704
8705 DWARF version 3 added DW_AT_object_pointer, which GCC
8706 4.5 does not yet generate. */
e142c38c 8707 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8708 if (attr)
8709 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8710 else
418835cc
KS
8711 {
8712 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8713
8714 /* GCC/43521: In java, the formal parameter
8715 "this" is sometimes not marked with DW_AT_artificial. */
8716 if (cu->language == language_java)
8717 {
8718 const char *name = dwarf2_name (child_die, cu);
9a619af0 8719
418835cc
KS
8720 if (name && !strcmp (name, "this"))
8721 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8722 }
8723 }
3ce3b1ba
PA
8724 arg_type = die_type (child_die, cu);
8725
8726 /* RealView does not mark THIS as const, which the testsuite
8727 expects. GCC marks THIS as const in method definitions,
8728 but not in the class specifications (GCC PR 43053). */
8729 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8730 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8731 {
8732 int is_this = 0;
8733 struct dwarf2_cu *arg_cu = cu;
8734 const char *name = dwarf2_name (child_die, cu);
8735
8736 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8737 if (attr)
8738 {
8739 /* If the compiler emits this, use it. */
8740 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8741 is_this = 1;
8742 }
8743 else if (name && strcmp (name, "this") == 0)
8744 /* Function definitions will have the argument names. */
8745 is_this = 1;
8746 else if (name == NULL && iparams == 0)
8747 /* Declarations may not have the names, so like
8748 elsewhere in GDB, assume an artificial first
8749 argument is "this". */
8750 is_this = 1;
8751
8752 if (is_this)
8753 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8754 arg_type, 0);
8755 }
8756
8757 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8758 iparams++;
8759 }
8760 child_die = sibling_die (child_die);
8761 }
8762 }
8763
76c10ea2 8764 return ftype;
c906108c
SS
8765}
8766
f792889a 8767static struct type *
e7c27a73 8768read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8769{
e7c27a73 8770 struct objfile *objfile = cu->objfile;
0114d602 8771 const char *name = NULL;
f792889a 8772 struct type *this_type;
c906108c 8773
94af9270 8774 name = dwarf2_full_name (NULL, die, cu);
f792889a 8775 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8776 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8777 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8778 set_die_type (die, this_type, cu);
8779 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8780 return this_type;
c906108c
SS
8781}
8782
8783/* Find a representation of a given base type and install
8784 it in the TYPE field of the die. */
8785
f792889a 8786static struct type *
e7c27a73 8787read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8788{
e7c27a73 8789 struct objfile *objfile = cu->objfile;
c906108c
SS
8790 struct type *type;
8791 struct attribute *attr;
8792 int encoding = 0, size = 0;
39cbfefa 8793 char *name;
6ccb9162
UW
8794 enum type_code code = TYPE_CODE_INT;
8795 int type_flags = 0;
8796 struct type *target_type = NULL;
c906108c 8797
e142c38c 8798 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8799 if (attr)
8800 {
8801 encoding = DW_UNSND (attr);
8802 }
e142c38c 8803 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8804 if (attr)
8805 {
8806 size = DW_UNSND (attr);
8807 }
39cbfefa 8808 name = dwarf2_name (die, cu);
6ccb9162 8809 if (!name)
c906108c 8810 {
6ccb9162
UW
8811 complaint (&symfile_complaints,
8812 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8813 }
6ccb9162
UW
8814
8815 switch (encoding)
c906108c 8816 {
6ccb9162
UW
8817 case DW_ATE_address:
8818 /* Turn DW_ATE_address into a void * pointer. */
8819 code = TYPE_CODE_PTR;
8820 type_flags |= TYPE_FLAG_UNSIGNED;
8821 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8822 break;
8823 case DW_ATE_boolean:
8824 code = TYPE_CODE_BOOL;
8825 type_flags |= TYPE_FLAG_UNSIGNED;
8826 break;
8827 case DW_ATE_complex_float:
8828 code = TYPE_CODE_COMPLEX;
8829 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8830 break;
8831 case DW_ATE_decimal_float:
8832 code = TYPE_CODE_DECFLOAT;
8833 break;
8834 case DW_ATE_float:
8835 code = TYPE_CODE_FLT;
8836 break;
8837 case DW_ATE_signed:
8838 break;
8839 case DW_ATE_unsigned:
8840 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8841 if (cu->language == language_fortran
8842 && name
8843 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8844 code = TYPE_CODE_CHAR;
6ccb9162
UW
8845 break;
8846 case DW_ATE_signed_char:
6e70227d 8847 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8848 || cu->language == language_pascal
8849 || cu->language == language_fortran)
6ccb9162
UW
8850 code = TYPE_CODE_CHAR;
8851 break;
8852 case DW_ATE_unsigned_char:
868a0084 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 type_flags |= TYPE_FLAG_UNSIGNED;
8858 break;
75079b2b
TT
8859 case DW_ATE_UTF:
8860 /* We just treat this as an integer and then recognize the
8861 type by name elsewhere. */
8862 break;
8863
6ccb9162
UW
8864 default:
8865 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8866 dwarf_type_encoding_name (encoding));
8867 break;
c906108c 8868 }
6ccb9162 8869
0114d602
DJ
8870 type = init_type (code, size, type_flags, NULL, objfile);
8871 TYPE_NAME (type) = name;
6ccb9162
UW
8872 TYPE_TARGET_TYPE (type) = target_type;
8873
0114d602 8874 if (name && strcmp (name, "char") == 0)
876cecd0 8875 TYPE_NOSIGN (type) = 1;
0114d602 8876
f792889a 8877 return set_die_type (die, type, cu);
c906108c
SS
8878}
8879
a02abb62
JB
8880/* Read the given DW_AT_subrange DIE. */
8881
f792889a 8882static struct type *
a02abb62
JB
8883read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8884{
8885 struct type *base_type;
8886 struct type *range_type;
8887 struct attribute *attr;
43bbcdc2
PH
8888 LONGEST low = 0;
8889 LONGEST high = -1;
39cbfefa 8890 char *name;
43bbcdc2 8891 LONGEST negative_mask;
e77813c8 8892
a02abb62 8893 base_type = die_type (die, cu);
953ac07e
JK
8894 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8895 check_typedef (base_type);
a02abb62 8896
7e314c57
JK
8897 /* The die_type call above may have already set the type for this DIE. */
8898 range_type = get_die_type (die, cu);
8899 if (range_type)
8900 return range_type;
8901
e142c38c 8902 if (cu->language == language_fortran)
6e70227d 8903 {
a02abb62
JB
8904 /* FORTRAN implies a lower bound of 1, if not given. */
8905 low = 1;
8906 }
8907
dd5e6932
DJ
8908 /* FIXME: For variable sized arrays either of these could be
8909 a variable rather than a constant value. We'll allow it,
8910 but we don't know how to handle it. */
e142c38c 8911 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8912 if (attr)
8913 low = dwarf2_get_attr_constant_value (attr, 0);
8914
e142c38c 8915 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8916 if (attr)
6e70227d 8917 {
d48323d8 8918 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
8919 {
8920 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8921 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8922 FIXME: GDB does not yet know how to handle dynamic
8923 arrays properly, treat them as arrays with unspecified
8924 length for now.
8925
8926 FIXME: jimb/2003-09-22: GDB does not really know
8927 how to handle arrays of unspecified length
8928 either; we just represent them as zero-length
8929 arrays. Choose an appropriate upper bound given
8930 the lower bound we've computed above. */
8931 high = low - 1;
8932 }
8933 else
8934 high = dwarf2_get_attr_constant_value (attr, 1);
8935 }
e77813c8
PM
8936 else
8937 {
8938 attr = dwarf2_attr (die, DW_AT_count, cu);
8939 if (attr)
8940 {
8941 int count = dwarf2_get_attr_constant_value (attr, 1);
8942 high = low + count - 1;
8943 }
c2ff108b
JK
8944 else
8945 {
8946 /* Unspecified array length. */
8947 high = low - 1;
8948 }
e77813c8
PM
8949 }
8950
8951 /* Dwarf-2 specifications explicitly allows to create subrange types
8952 without specifying a base type.
8953 In that case, the base type must be set to the type of
8954 the lower bound, upper bound or count, in that order, if any of these
8955 three attributes references an object that has a type.
8956 If no base type is found, the Dwarf-2 specifications say that
8957 a signed integer type of size equal to the size of an address should
8958 be used.
8959 For the following C code: `extern char gdb_int [];'
8960 GCC produces an empty range DIE.
8961 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 8962 high bound or count are not yet handled by this code. */
e77813c8
PM
8963 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8964 {
8965 struct objfile *objfile = cu->objfile;
8966 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8967 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8968 struct type *int_type = objfile_type (objfile)->builtin_int;
8969
8970 /* Test "int", "long int", and "long long int" objfile types,
8971 and select the first one having a size above or equal to the
8972 architecture address size. */
8973 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8974 base_type = int_type;
8975 else
8976 {
8977 int_type = objfile_type (objfile)->builtin_long;
8978 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8979 base_type = int_type;
8980 else
8981 {
8982 int_type = objfile_type (objfile)->builtin_long_long;
8983 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8984 base_type = int_type;
8985 }
8986 }
8987 }
a02abb62 8988
6e70227d 8989 negative_mask =
43bbcdc2
PH
8990 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8991 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8992 low |= negative_mask;
8993 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8994 high |= negative_mask;
8995
a02abb62
JB
8996 range_type = create_range_type (NULL, base_type, low, high);
8997
bbb0eef6
JK
8998 /* Mark arrays with dynamic length at least as an array of unspecified
8999 length. GDB could check the boundary but before it gets implemented at
9000 least allow accessing the array elements. */
d48323d8 9001 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
9002 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9003
c2ff108b
JK
9004 /* Ada expects an empty array on no boundary attributes. */
9005 if (attr == NULL && cu->language != language_ada)
9006 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9007
39cbfefa
DJ
9008 name = dwarf2_name (die, cu);
9009 if (name)
9010 TYPE_NAME (range_type) = name;
6e70227d 9011
e142c38c 9012 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
9013 if (attr)
9014 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9015
7e314c57
JK
9016 set_die_type (die, range_type, cu);
9017
9018 /* set_die_type should be already done. */
b4ba55a1
JB
9019 set_descriptive_type (range_type, die, cu);
9020
7e314c57 9021 return range_type;
a02abb62 9022}
6e70227d 9023
f792889a 9024static struct type *
81a17f79
JB
9025read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9026{
9027 struct type *type;
81a17f79 9028
81a17f79
JB
9029 /* For now, we only support the C meaning of an unspecified type: void. */
9030
0114d602
DJ
9031 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9032 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 9033
f792889a 9034 return set_die_type (die, type, cu);
81a17f79 9035}
a02abb62 9036
51545339
DJ
9037/* Trivial hash function for die_info: the hash value of a DIE
9038 is its offset in .debug_info for this objfile. */
9039
9040static hashval_t
9041die_hash (const void *item)
9042{
9043 const struct die_info *die = item;
9a619af0 9044
51545339
DJ
9045 return die->offset;
9046}
9047
9048/* Trivial comparison function for die_info structures: two DIEs
9049 are equal if they have the same offset. */
9050
9051static int
9052die_eq (const void *item_lhs, const void *item_rhs)
9053{
9054 const struct die_info *die_lhs = item_lhs;
9055 const struct die_info *die_rhs = item_rhs;
9a619af0 9056
51545339
DJ
9057 return die_lhs->offset == die_rhs->offset;
9058}
9059
c906108c
SS
9060/* Read a whole compilation unit into a linked list of dies. */
9061
f9aca02d 9062static struct die_info *
93311388 9063read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 9064{
93311388 9065 struct die_reader_specs reader_specs;
98bfdba5 9066 int read_abbrevs = 0;
1d9ec526 9067 struct cleanup *back_to = NULL;
98bfdba5
PA
9068 struct die_info *die;
9069
9070 if (cu->dwarf2_abbrevs == NULL)
9071 {
9072 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
9073 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9074 read_abbrevs = 1;
9075 }
93311388 9076
348e048f 9077 gdb_assert (cu->die_hash == NULL);
51545339
DJ
9078 cu->die_hash
9079 = htab_create_alloc_ex (cu->header.length / 12,
9080 die_hash,
9081 die_eq,
9082 NULL,
9083 &cu->comp_unit_obstack,
9084 hashtab_obstack_allocate,
9085 dummy_obstack_deallocate);
9086
93311388
DE
9087 init_cu_die_reader (&reader_specs, cu);
9088
98bfdba5
PA
9089 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9090
9091 if (read_abbrevs)
9092 do_cleanups (back_to);
9093
9094 return die;
639d11d3
DC
9095}
9096
d97bc12b
DE
9097/* Main entry point for reading a DIE and all children.
9098 Read the DIE and dump it if requested. */
9099
9100static struct die_info *
93311388
DE
9101read_die_and_children (const struct die_reader_specs *reader,
9102 gdb_byte *info_ptr,
d97bc12b
DE
9103 gdb_byte **new_info_ptr,
9104 struct die_info *parent)
9105{
93311388 9106 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
9107 new_info_ptr, parent);
9108
9109 if (dwarf2_die_debug)
9110 {
348e048f
DE
9111 fprintf_unfiltered (gdb_stdlog,
9112 "\nRead die from %s of %s:\n",
8b70b953
TT
9113 (reader->cu->per_cu->debug_type_section
9114 ? ".debug_types"
9115 : ".debug_info"),
348e048f 9116 reader->abfd->filename);
d97bc12b
DE
9117 dump_die (result, dwarf2_die_debug);
9118 }
9119
9120 return result;
9121}
9122
639d11d3
DC
9123/* Read a single die and all its descendents. Set the die's sibling
9124 field to NULL; set other fields in the die correctly, and set all
9125 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9126 location of the info_ptr after reading all of those dies. PARENT
9127 is the parent of the die in question. */
9128
9129static struct die_info *
93311388
DE
9130read_die_and_children_1 (const struct die_reader_specs *reader,
9131 gdb_byte *info_ptr,
d97bc12b
DE
9132 gdb_byte **new_info_ptr,
9133 struct die_info *parent)
639d11d3
DC
9134{
9135 struct die_info *die;
fe1b8b76 9136 gdb_byte *cur_ptr;
639d11d3
DC
9137 int has_children;
9138
93311388 9139 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
9140 if (die == NULL)
9141 {
9142 *new_info_ptr = cur_ptr;
9143 return NULL;
9144 }
93311388 9145 store_in_ref_table (die, reader->cu);
639d11d3
DC
9146
9147 if (has_children)
348e048f 9148 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
9149 else
9150 {
9151 die->child = NULL;
9152 *new_info_ptr = cur_ptr;
9153 }
9154
9155 die->sibling = NULL;
9156 die->parent = parent;
9157 return die;
9158}
9159
9160/* Read a die, all of its descendents, and all of its siblings; set
9161 all of the fields of all of the dies correctly. Arguments are as
9162 in read_die_and_children. */
9163
9164static struct die_info *
93311388
DE
9165read_die_and_siblings (const struct die_reader_specs *reader,
9166 gdb_byte *info_ptr,
fe1b8b76 9167 gdb_byte **new_info_ptr,
639d11d3
DC
9168 struct die_info *parent)
9169{
9170 struct die_info *first_die, *last_sibling;
fe1b8b76 9171 gdb_byte *cur_ptr;
639d11d3 9172
c906108c 9173 cur_ptr = info_ptr;
639d11d3
DC
9174 first_die = last_sibling = NULL;
9175
9176 while (1)
c906108c 9177 {
639d11d3 9178 struct die_info *die
93311388 9179 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 9180
1d325ec1 9181 if (die == NULL)
c906108c 9182 {
639d11d3
DC
9183 *new_info_ptr = cur_ptr;
9184 return first_die;
c906108c 9185 }
1d325ec1
DJ
9186
9187 if (!first_die)
9188 first_die = die;
c906108c 9189 else
1d325ec1
DJ
9190 last_sibling->sibling = die;
9191
9192 last_sibling = die;
c906108c 9193 }
c906108c
SS
9194}
9195
93311388
DE
9196/* Read the die from the .debug_info section buffer. Set DIEP to
9197 point to a newly allocated die with its information, except for its
9198 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9199 whether the die has children or not. */
9200
9201static gdb_byte *
9202read_full_die (const struct die_reader_specs *reader,
9203 struct die_info **diep, gdb_byte *info_ptr,
9204 int *has_children)
9205{
9206 unsigned int abbrev_number, bytes_read, i, offset;
9207 struct abbrev_info *abbrev;
9208 struct die_info *die;
9209 struct dwarf2_cu *cu = reader->cu;
9210 bfd *abfd = reader->abfd;
9211
9212 offset = info_ptr - reader->buffer;
9213 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9214 info_ptr += bytes_read;
9215 if (!abbrev_number)
9216 {
9217 *diep = NULL;
9218 *has_children = 0;
9219 return info_ptr;
9220 }
9221
9222 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9223 if (!abbrev)
348e048f
DE
9224 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9225 abbrev_number,
9226 bfd_get_filename (abfd));
9227
93311388
DE
9228 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9229 die->offset = offset;
9230 die->tag = abbrev->tag;
9231 die->abbrev = abbrev_number;
9232
9233 die->num_attrs = abbrev->num_attrs;
9234
9235 for (i = 0; i < abbrev->num_attrs; ++i)
9236 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9237 abfd, info_ptr, cu);
9238
9239 *diep = die;
9240 *has_children = abbrev->has_children;
9241 return info_ptr;
9242}
9243
c906108c
SS
9244/* In DWARF version 2, the description of the debugging information is
9245 stored in a separate .debug_abbrev section. Before we read any
9246 dies from a section we read in all abbreviations and install them
72bf9492
DJ
9247 in a hash table. This function also sets flags in CU describing
9248 the data found in the abbrev table. */
c906108c
SS
9249
9250static void
e7c27a73 9251dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 9252{
e7c27a73 9253 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 9254 gdb_byte *abbrev_ptr;
c906108c
SS
9255 struct abbrev_info *cur_abbrev;
9256 unsigned int abbrev_number, bytes_read, abbrev_name;
9257 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
9258 struct attr_abbrev *cur_attrs;
9259 unsigned int allocated_attrs;
c906108c 9260
0963b4bd 9261 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
9262 obstack_init (&cu->abbrev_obstack);
9263 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9264 (ABBREV_HASH_SIZE
9265 * sizeof (struct abbrev_info *)));
9266 memset (cu->dwarf2_abbrevs, 0,
9267 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 9268
be391dca
TT
9269 dwarf2_read_section (dwarf2_per_objfile->objfile,
9270 &dwarf2_per_objfile->abbrev);
dce234bc 9271 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
9272 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9273 abbrev_ptr += bytes_read;
9274
f3dd6933
DJ
9275 allocated_attrs = ATTR_ALLOC_CHUNK;
9276 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 9277
0963b4bd 9278 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
9279 while (abbrev_number)
9280 {
f3dd6933 9281 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
9282
9283 /* read in abbrev header */
9284 cur_abbrev->number = abbrev_number;
9285 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9286 abbrev_ptr += bytes_read;
9287 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9288 abbrev_ptr += 1;
9289
72bf9492
DJ
9290 if (cur_abbrev->tag == DW_TAG_namespace)
9291 cu->has_namespace_info = 1;
9292
c906108c
SS
9293 /* now read in declarations */
9294 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9295 abbrev_ptr += bytes_read;
9296 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9297 abbrev_ptr += bytes_read;
9298 while (abbrev_name)
9299 {
f3dd6933 9300 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 9301 {
f3dd6933
DJ
9302 allocated_attrs += ATTR_ALLOC_CHUNK;
9303 cur_attrs
9304 = xrealloc (cur_attrs, (allocated_attrs
9305 * sizeof (struct attr_abbrev)));
c906108c 9306 }
ae038cb0
DJ
9307
9308 /* Record whether this compilation unit might have
9309 inter-compilation-unit references. If we don't know what form
9310 this attribute will have, then it might potentially be a
9311 DW_FORM_ref_addr, so we conservatively expect inter-CU
9312 references. */
9313
9314 if (abbrev_form == DW_FORM_ref_addr
9315 || abbrev_form == DW_FORM_indirect)
9316 cu->has_form_ref_addr = 1;
9317
f3dd6933
DJ
9318 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9319 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
9320 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9321 abbrev_ptr += bytes_read;
9322 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9323 abbrev_ptr += bytes_read;
9324 }
9325
f3dd6933
DJ
9326 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9327 (cur_abbrev->num_attrs
9328 * sizeof (struct attr_abbrev)));
9329 memcpy (cur_abbrev->attrs, cur_attrs,
9330 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9331
c906108c 9332 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9333 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9334 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9335
9336 /* Get next abbreviation.
9337 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9338 always properly terminated with an abbrev number of 0.
9339 Exit loop if we encounter an abbreviation which we have
9340 already read (which means we are about to read the abbreviations
9341 for the next compile unit) or if the end of the abbreviation
9342 table is reached. */
dce234bc
PP
9343 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9344 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9345 break;
9346 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9347 abbrev_ptr += bytes_read;
e7c27a73 9348 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9349 break;
9350 }
f3dd6933
DJ
9351
9352 xfree (cur_attrs);
c906108c
SS
9353}
9354
f3dd6933 9355/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9356
c906108c 9357static void
f3dd6933 9358dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9359{
f3dd6933 9360 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9361
f3dd6933
DJ
9362 obstack_free (&cu->abbrev_obstack, NULL);
9363 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9364}
9365
9366/* Lookup an abbrev_info structure in the abbrev hash table. */
9367
9368static struct abbrev_info *
e7c27a73 9369dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9370{
9371 unsigned int hash_number;
9372 struct abbrev_info *abbrev;
9373
9374 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9375 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9376
9377 while (abbrev)
9378 {
9379 if (abbrev->number == number)
9380 return abbrev;
9381 else
9382 abbrev = abbrev->next;
9383 }
9384 return NULL;
9385}
9386
72bf9492
DJ
9387/* Returns nonzero if TAG represents a type that we might generate a partial
9388 symbol for. */
9389
9390static int
9391is_type_tag_for_partial (int tag)
9392{
9393 switch (tag)
9394 {
9395#if 0
9396 /* Some types that would be reasonable to generate partial symbols for,
9397 that we don't at present. */
9398 case DW_TAG_array_type:
9399 case DW_TAG_file_type:
9400 case DW_TAG_ptr_to_member_type:
9401 case DW_TAG_set_type:
9402 case DW_TAG_string_type:
9403 case DW_TAG_subroutine_type:
9404#endif
9405 case DW_TAG_base_type:
9406 case DW_TAG_class_type:
680b30c7 9407 case DW_TAG_interface_type:
72bf9492
DJ
9408 case DW_TAG_enumeration_type:
9409 case DW_TAG_structure_type:
9410 case DW_TAG_subrange_type:
9411 case DW_TAG_typedef:
9412 case DW_TAG_union_type:
9413 return 1;
9414 default:
9415 return 0;
9416 }
9417}
9418
9419/* Load all DIEs that are interesting for partial symbols into memory. */
9420
9421static struct partial_die_info *
93311388
DE
9422load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9423 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
9424{
9425 struct partial_die_info *part_die;
9426 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9427 struct abbrev_info *abbrev;
9428 unsigned int bytes_read;
5afb4e99 9429 unsigned int load_all = 0;
72bf9492
DJ
9430
9431 int nesting_level = 1;
9432
9433 parent_die = NULL;
9434 last_die = NULL;
9435
5afb4e99
DJ
9436 if (cu->per_cu && cu->per_cu->load_all_dies)
9437 load_all = 1;
9438
72bf9492
DJ
9439 cu->partial_dies
9440 = htab_create_alloc_ex (cu->header.length / 12,
9441 partial_die_hash,
9442 partial_die_eq,
9443 NULL,
9444 &cu->comp_unit_obstack,
9445 hashtab_obstack_allocate,
9446 dummy_obstack_deallocate);
9447
9448 part_die = obstack_alloc (&cu->comp_unit_obstack,
9449 sizeof (struct partial_die_info));
9450
9451 while (1)
9452 {
9453 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9454
9455 /* A NULL abbrev means the end of a series of children. */
9456 if (abbrev == NULL)
9457 {
9458 if (--nesting_level == 0)
9459 {
9460 /* PART_DIE was probably the last thing allocated on the
9461 comp_unit_obstack, so we could call obstack_free
9462 here. We don't do that because the waste is small,
9463 and will be cleaned up when we're done with this
9464 compilation unit. This way, we're also more robust
9465 against other users of the comp_unit_obstack. */
9466 return first_die;
9467 }
9468 info_ptr += bytes_read;
9469 last_die = parent_die;
9470 parent_die = parent_die->die_parent;
9471 continue;
9472 }
9473
98bfdba5
PA
9474 /* Check for template arguments. We never save these; if
9475 they're seen, we just mark the parent, and go on our way. */
9476 if (parent_die != NULL
9477 && cu->language == language_cplus
9478 && (abbrev->tag == DW_TAG_template_type_param
9479 || abbrev->tag == DW_TAG_template_value_param))
9480 {
9481 parent_die->has_template_arguments = 1;
9482
9483 if (!load_all)
9484 {
9485 /* We don't need a partial DIE for the template argument. */
9486 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9487 cu);
9488 continue;
9489 }
9490 }
9491
9492 /* We only recurse into subprograms looking for template arguments.
9493 Skip their other children. */
9494 if (!load_all
9495 && cu->language == language_cplus
9496 && parent_die != NULL
9497 && parent_die->tag == DW_TAG_subprogram)
9498 {
9499 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9500 continue;
9501 }
9502
5afb4e99
DJ
9503 /* Check whether this DIE is interesting enough to save. Normally
9504 we would not be interested in members here, but there may be
9505 later variables referencing them via DW_AT_specification (for
9506 static members). */
9507 if (!load_all
9508 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9509 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9510 && abbrev->tag != DW_TAG_enumerator
9511 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9512 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9513 && abbrev->tag != DW_TAG_variable
5afb4e99 9514 && abbrev->tag != DW_TAG_namespace
f55ee35c 9515 && abbrev->tag != DW_TAG_module
5afb4e99 9516 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9517 {
9518 /* Otherwise we skip to the next sibling, if any. */
93311388 9519 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9520 continue;
9521 }
9522
93311388
DE
9523 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9524 buffer, info_ptr, cu);
72bf9492
DJ
9525
9526 /* This two-pass algorithm for processing partial symbols has a
9527 high cost in cache pressure. Thus, handle some simple cases
9528 here which cover the majority of C partial symbols. DIEs
9529 which neither have specification tags in them, nor could have
9530 specification tags elsewhere pointing at them, can simply be
9531 processed and discarded.
9532
9533 This segment is also optional; scan_partial_symbols and
9534 add_partial_symbol will handle these DIEs if we chain
9535 them in normally. When compilers which do not emit large
9536 quantities of duplicate debug information are more common,
9537 this code can probably be removed. */
9538
9539 /* Any complete simple types at the top level (pretty much all
9540 of them, for a language without namespaces), can be processed
9541 directly. */
9542 if (parent_die == NULL
9543 && part_die->has_specification == 0
9544 && part_die->is_declaration == 0
d8228535 9545 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9546 || part_die->tag == DW_TAG_base_type
9547 || part_die->tag == DW_TAG_subrange_type))
9548 {
9549 if (building_psymtab && part_die->name != NULL)
04a679b8 9550 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
9551 VAR_DOMAIN, LOC_TYPEDEF,
9552 &cu->objfile->static_psymbols,
9553 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 9554 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9555 continue;
9556 }
9557
d8228535
JK
9558 /* The exception for DW_TAG_typedef with has_children above is
9559 a workaround of GCC PR debug/47510. In the case of this complaint
9560 type_name_no_tag_or_error will error on such types later.
9561
9562 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9563 it could not find the child DIEs referenced later, this is checked
9564 above. In correct DWARF DW_TAG_typedef should have no children. */
9565
9566 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9567 complaint (&symfile_complaints,
9568 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9569 "- DIE at 0x%x [in module %s]"),
9570 part_die->offset, cu->objfile->name);
9571
72bf9492
DJ
9572 /* If we're at the second level, and we're an enumerator, and
9573 our parent has no specification (meaning possibly lives in a
9574 namespace elsewhere), then we can add the partial symbol now
9575 instead of queueing it. */
9576 if (part_die->tag == DW_TAG_enumerator
9577 && parent_die != NULL
9578 && parent_die->die_parent == NULL
9579 && parent_die->tag == DW_TAG_enumeration_type
9580 && parent_die->has_specification == 0)
9581 {
9582 if (part_die->name == NULL)
3e43a32a
MS
9583 complaint (&symfile_complaints,
9584 _("malformed enumerator DIE ignored"));
72bf9492 9585 else if (building_psymtab)
04a679b8 9586 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9587 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9588 (cu->language == language_cplus
9589 || cu->language == language_java)
72bf9492
DJ
9590 ? &cu->objfile->global_psymbols
9591 : &cu->objfile->static_psymbols,
9592 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9593
93311388 9594 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9595 continue;
9596 }
9597
9598 /* We'll save this DIE so link it in. */
9599 part_die->die_parent = parent_die;
9600 part_die->die_sibling = NULL;
9601 part_die->die_child = NULL;
9602
9603 if (last_die && last_die == parent_die)
9604 last_die->die_child = part_die;
9605 else if (last_die)
9606 last_die->die_sibling = part_die;
9607
9608 last_die = part_die;
9609
9610 if (first_die == NULL)
9611 first_die = part_die;
9612
9613 /* Maybe add the DIE to the hash table. Not all DIEs that we
9614 find interesting need to be in the hash table, because we
9615 also have the parent/sibling/child chains; only those that we
9616 might refer to by offset later during partial symbol reading.
9617
9618 For now this means things that might have be the target of a
9619 DW_AT_specification, DW_AT_abstract_origin, or
9620 DW_AT_extension. DW_AT_extension will refer only to
9621 namespaces; DW_AT_abstract_origin refers to functions (and
9622 many things under the function DIE, but we do not recurse
9623 into function DIEs during partial symbol reading) and
9624 possibly variables as well; DW_AT_specification refers to
9625 declarations. Declarations ought to have the DW_AT_declaration
9626 flag. It happens that GCC forgets to put it in sometimes, but
9627 only for functions, not for types.
9628
9629 Adding more things than necessary to the hash table is harmless
9630 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9631 wasted time in find_partial_die, when we reread the compilation
9632 unit with load_all_dies set. */
72bf9492 9633
5afb4e99 9634 if (load_all
72929c62 9635 || abbrev->tag == DW_TAG_constant
5afb4e99 9636 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9637 || abbrev->tag == DW_TAG_variable
9638 || abbrev->tag == DW_TAG_namespace
9639 || part_die->is_declaration)
9640 {
9641 void **slot;
9642
9643 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9644 part_die->offset, INSERT);
9645 *slot = part_die;
9646 }
9647
9648 part_die = obstack_alloc (&cu->comp_unit_obstack,
9649 sizeof (struct partial_die_info));
9650
9651 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9652 we have no reason to follow the children of structures; for other
98bfdba5
PA
9653 languages we have to, so that we can get at method physnames
9654 to infer fully qualified class names, for DW_AT_specification,
9655 and for C++ template arguments. For C++, we also look one level
9656 inside functions to find template arguments (if the name of the
9657 function does not already contain the template arguments).
bc30ff58
JB
9658
9659 For Ada, we need to scan the children of subprograms and lexical
9660 blocks as well because Ada allows the definition of nested
9661 entities that could be interesting for the debugger, such as
9662 nested subprograms for instance. */
72bf9492 9663 if (last_die->has_children
5afb4e99
DJ
9664 && (load_all
9665 || last_die->tag == DW_TAG_namespace
f55ee35c 9666 || last_die->tag == DW_TAG_module
72bf9492 9667 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9668 || (cu->language == language_cplus
9669 && last_die->tag == DW_TAG_subprogram
9670 && (last_die->name == NULL
9671 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9672 || (cu->language != language_c
9673 && (last_die->tag == DW_TAG_class_type
680b30c7 9674 || last_die->tag == DW_TAG_interface_type
72bf9492 9675 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9676 || last_die->tag == DW_TAG_union_type))
9677 || (cu->language == language_ada
9678 && (last_die->tag == DW_TAG_subprogram
9679 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9680 {
9681 nesting_level++;
9682 parent_die = last_die;
9683 continue;
9684 }
9685
9686 /* Otherwise we skip to the next sibling, if any. */
93311388 9687 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9688
9689 /* Back to the top, do it again. */
9690 }
9691}
9692
c906108c
SS
9693/* Read a minimal amount of information into the minimal die structure. */
9694
fe1b8b76 9695static gdb_byte *
72bf9492
DJ
9696read_partial_die (struct partial_die_info *part_die,
9697 struct abbrev_info *abbrev,
9698 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9699 gdb_byte *buffer, gdb_byte *info_ptr,
9700 struct dwarf2_cu *cu)
c906108c 9701{
fa238c03 9702 unsigned int i;
c906108c 9703 struct attribute attr;
c5aa993b 9704 int has_low_pc_attr = 0;
c906108c
SS
9705 int has_high_pc_attr = 0;
9706
72bf9492 9707 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9708
93311388 9709 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9710
9711 info_ptr += abbrev_len;
9712
9713 if (abbrev == NULL)
9714 return info_ptr;
9715
c906108c
SS
9716 part_die->tag = abbrev->tag;
9717 part_die->has_children = abbrev->has_children;
c906108c
SS
9718
9719 for (i = 0; i < abbrev->num_attrs; ++i)
9720 {
e7c27a73 9721 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9722
9723 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9724 partial symbol table. */
c906108c
SS
9725 switch (attr.name)
9726 {
9727 case DW_AT_name:
71c25dea
TT
9728 switch (part_die->tag)
9729 {
9730 case DW_TAG_compile_unit:
348e048f 9731 case DW_TAG_type_unit:
71c25dea
TT
9732 /* Compilation units have a DW_AT_name that is a filename, not
9733 a source language identifier. */
9734 case DW_TAG_enumeration_type:
9735 case DW_TAG_enumerator:
9736 /* These tags always have simple identifiers already; no need
9737 to canonicalize them. */
9738 part_die->name = DW_STRING (&attr);
9739 break;
9740 default:
9741 part_die->name
9742 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9743 &cu->objfile->objfile_obstack);
71c25dea
TT
9744 break;
9745 }
c906108c 9746 break;
31ef98ae 9747 case DW_AT_linkage_name:
c906108c 9748 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9749 /* Note that both forms of linkage name might appear. We
9750 assume they will be the same, and we only store the last
9751 one we see. */
94af9270
KS
9752 if (cu->language == language_ada)
9753 part_die->name = DW_STRING (&attr);
abc72ce4 9754 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9755 break;
9756 case DW_AT_low_pc:
9757 has_low_pc_attr = 1;
9758 part_die->lowpc = DW_ADDR (&attr);
9759 break;
9760 case DW_AT_high_pc:
9761 has_high_pc_attr = 1;
9762 part_die->highpc = DW_ADDR (&attr);
9763 break;
9764 case DW_AT_location:
0963b4bd 9765 /* Support the .debug_loc offsets. */
8e19ed76
PS
9766 if (attr_form_is_block (&attr))
9767 {
9768 part_die->locdesc = DW_BLOCK (&attr);
9769 }
3690dd37 9770 else if (attr_form_is_section_offset (&attr))
8e19ed76 9771 {
4d3c2250 9772 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9773 }
9774 else
9775 {
4d3c2250
KB
9776 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9777 "partial symbol information");
8e19ed76 9778 }
c906108c 9779 break;
c906108c
SS
9780 case DW_AT_external:
9781 part_die->is_external = DW_UNSND (&attr);
9782 break;
9783 case DW_AT_declaration:
9784 part_die->is_declaration = DW_UNSND (&attr);
9785 break;
9786 case DW_AT_type:
9787 part_die->has_type = 1;
9788 break;
9789 case DW_AT_abstract_origin:
9790 case DW_AT_specification:
72bf9492
DJ
9791 case DW_AT_extension:
9792 part_die->has_specification = 1;
c764a876 9793 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9794 break;
9795 case DW_AT_sibling:
9796 /* Ignore absolute siblings, they might point outside of
9797 the current compile unit. */
9798 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9799 complaint (&symfile_complaints,
9800 _("ignoring absolute DW_AT_sibling"));
c906108c 9801 else
93311388 9802 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9803 break;
fa4028e9
JB
9804 case DW_AT_byte_size:
9805 part_die->has_byte_size = 1;
9806 break;
68511cec
CES
9807 case DW_AT_calling_convention:
9808 /* DWARF doesn't provide a way to identify a program's source-level
9809 entry point. DW_AT_calling_convention attributes are only meant
9810 to describe functions' calling conventions.
9811
9812 However, because it's a necessary piece of information in
9813 Fortran, and because DW_CC_program is the only piece of debugging
9814 information whose definition refers to a 'main program' at all,
9815 several compilers have begun marking Fortran main programs with
9816 DW_CC_program --- even when those functions use the standard
9817 calling conventions.
9818
9819 So until DWARF specifies a way to provide this information and
9820 compilers pick up the new representation, we'll support this
9821 practice. */
9822 if (DW_UNSND (&attr) == DW_CC_program
9823 && cu->language == language_fortran)
01f8c46d
JK
9824 {
9825 set_main_name (part_die->name);
9826
9827 /* As this DIE has a static linkage the name would be difficult
9828 to look up later. */
9829 language_of_main = language_fortran;
9830 }
68511cec 9831 break;
c906108c
SS
9832 default:
9833 break;
9834 }
9835 }
9836
9373cf26
JK
9837 if (has_low_pc_attr && has_high_pc_attr)
9838 {
9839 /* When using the GNU linker, .gnu.linkonce. sections are used to
9840 eliminate duplicate copies of functions and vtables and such.
9841 The linker will arbitrarily choose one and discard the others.
9842 The AT_*_pc values for such functions refer to local labels in
9843 these sections. If the section from that file was discarded, the
9844 labels are not in the output, so the relocs get a value of 0.
9845 If this is a discarded function, mark the pc bounds as invalid,
9846 so that GDB will ignore it. */
9847 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9848 {
9849 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9850
9851 complaint (&symfile_complaints,
9852 _("DW_AT_low_pc %s is zero "
9853 "for DIE at 0x%x [in module %s]"),
9854 paddress (gdbarch, part_die->lowpc),
9855 part_die->offset, cu->objfile->name);
9856 }
9857 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9858 else if (part_die->lowpc >= part_die->highpc)
9859 {
9860 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9861
9862 complaint (&symfile_complaints,
9863 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9864 "for DIE at 0x%x [in module %s]"),
9865 paddress (gdbarch, part_die->lowpc),
9866 paddress (gdbarch, part_die->highpc),
9867 part_die->offset, cu->objfile->name);
9868 }
9869 else
9870 part_die->has_pc_info = 1;
9871 }
85cbf3d3 9872
c906108c
SS
9873 return info_ptr;
9874}
9875
72bf9492
DJ
9876/* Find a cached partial DIE at OFFSET in CU. */
9877
9878static struct partial_die_info *
c764a876 9879find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9880{
9881 struct partial_die_info *lookup_die = NULL;
9882 struct partial_die_info part_die;
9883
9884 part_die.offset = offset;
9885 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9886
72bf9492
DJ
9887 return lookup_die;
9888}
9889
348e048f
DE
9890/* Find a partial DIE at OFFSET, which may or may not be in CU,
9891 except in the case of .debug_types DIEs which do not reference
9892 outside their CU (they do however referencing other types via
55f1336d 9893 DW_FORM_ref_sig8). */
72bf9492
DJ
9894
9895static struct partial_die_info *
c764a876 9896find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9897{
5afb4e99
DJ
9898 struct dwarf2_per_cu_data *per_cu = NULL;
9899 struct partial_die_info *pd = NULL;
72bf9492 9900
8b70b953 9901 if (cu->per_cu->debug_type_section)
348e048f
DE
9902 {
9903 pd = find_partial_die_in_comp_unit (offset, cu);
9904 if (pd != NULL)
9905 return pd;
9906 goto not_found;
9907 }
9908
45452591 9909 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9910 {
9911 pd = find_partial_die_in_comp_unit (offset, cu);
9912 if (pd != NULL)
9913 return pd;
9914 }
72bf9492 9915
ae038cb0
DJ
9916 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9917
98bfdba5
PA
9918 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9919 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9920
9921 per_cu->cu->last_used = 0;
5afb4e99
DJ
9922 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9923
9924 if (pd == NULL && per_cu->load_all_dies == 0)
9925 {
9926 struct cleanup *back_to;
9927 struct partial_die_info comp_unit_die;
9928 struct abbrev_info *abbrev;
9929 unsigned int bytes_read;
9930 char *info_ptr;
9931
9932 per_cu->load_all_dies = 1;
9933
9934 /* Re-read the DIEs. */
9935 back_to = make_cleanup (null_cleanup, 0);
9936 if (per_cu->cu->dwarf2_abbrevs == NULL)
9937 {
9938 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9939 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9940 }
dce234bc 9941 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9942 + per_cu->cu->header.offset
9943 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9944 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9945 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9946 per_cu->cu->objfile->obfd,
9947 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9948 per_cu->cu);
9949 if (comp_unit_die.has_children)
93311388
DE
9950 load_partial_dies (per_cu->cu->objfile->obfd,
9951 dwarf2_per_objfile->info.buffer, info_ptr,
9952 0, per_cu->cu);
5afb4e99
DJ
9953 do_cleanups (back_to);
9954
9955 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9956 }
9957
348e048f
DE
9958 not_found:
9959
5afb4e99
DJ
9960 if (pd == NULL)
9961 internal_error (__FILE__, __LINE__,
3e43a32a
MS
9962 _("could not find partial DIE 0x%x "
9963 "in cache [from module %s]\n"),
5afb4e99
DJ
9964 offset, bfd_get_filename (cu->objfile->obfd));
9965 return pd;
72bf9492
DJ
9966}
9967
abc72ce4
DE
9968/* See if we can figure out if the class lives in a namespace. We do
9969 this by looking for a member function; its demangled name will
9970 contain namespace info, if there is any. */
9971
9972static void
9973guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9974 struct dwarf2_cu *cu)
9975{
9976 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9977 what template types look like, because the demangler
9978 frequently doesn't give the same name as the debug info. We
9979 could fix this by only using the demangled name to get the
9980 prefix (but see comment in read_structure_type). */
9981
9982 struct partial_die_info *real_pdi;
9983 struct partial_die_info *child_pdi;
9984
9985 /* If this DIE (this DIE's specification, if any) has a parent, then
9986 we should not do this. We'll prepend the parent's fully qualified
9987 name when we create the partial symbol. */
9988
9989 real_pdi = struct_pdi;
9990 while (real_pdi->has_specification)
9991 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9992
9993 if (real_pdi->die_parent != NULL)
9994 return;
9995
9996 for (child_pdi = struct_pdi->die_child;
9997 child_pdi != NULL;
9998 child_pdi = child_pdi->die_sibling)
9999 {
10000 if (child_pdi->tag == DW_TAG_subprogram
10001 && child_pdi->linkage_name != NULL)
10002 {
10003 char *actual_class_name
10004 = language_class_name_from_physname (cu->language_defn,
10005 child_pdi->linkage_name);
10006 if (actual_class_name != NULL)
10007 {
10008 struct_pdi->name
10009 = obsavestring (actual_class_name,
10010 strlen (actual_class_name),
10011 &cu->objfile->objfile_obstack);
10012 xfree (actual_class_name);
10013 }
10014 break;
10015 }
10016 }
10017}
10018
72bf9492
DJ
10019/* Adjust PART_DIE before generating a symbol for it. This function
10020 may set the is_external flag or change the DIE's name. */
10021
10022static void
10023fixup_partial_die (struct partial_die_info *part_die,
10024 struct dwarf2_cu *cu)
10025{
abc72ce4
DE
10026 /* Once we've fixed up a die, there's no point in doing so again.
10027 This also avoids a memory leak if we were to call
10028 guess_partial_die_structure_name multiple times. */
10029 if (part_die->fixup_called)
10030 return;
10031
72bf9492
DJ
10032 /* If we found a reference attribute and the DIE has no name, try
10033 to find a name in the referred to DIE. */
10034
10035 if (part_die->name == NULL && part_die->has_specification)
10036 {
10037 struct partial_die_info *spec_die;
72bf9492 10038
10b3939b 10039 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 10040
10b3939b 10041 fixup_partial_die (spec_die, cu);
72bf9492
DJ
10042
10043 if (spec_die->name)
10044 {
10045 part_die->name = spec_die->name;
10046
10047 /* Copy DW_AT_external attribute if it is set. */
10048 if (spec_die->is_external)
10049 part_die->is_external = spec_die->is_external;
10050 }
10051 }
10052
10053 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
10054
10055 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 10056 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 10057
abc72ce4
DE
10058 /* If there is no parent die to provide a namespace, and there are
10059 children, see if we can determine the namespace from their linkage
10060 name.
10061 NOTE: We need to do this even if cu->has_namespace_info != 0.
10062 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10063 if (cu->language == language_cplus
8b70b953 10064 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
10065 && part_die->die_parent == NULL
10066 && part_die->has_children
10067 && (part_die->tag == DW_TAG_class_type
10068 || part_die->tag == DW_TAG_structure_type
10069 || part_die->tag == DW_TAG_union_type))
10070 guess_partial_die_structure_name (part_die, cu);
10071
53832f31
TT
10072 /* GCC might emit a nameless struct or union that has a linkage
10073 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10074 if (part_die->name == NULL
96408a79
SA
10075 && (part_die->tag == DW_TAG_class_type
10076 || part_die->tag == DW_TAG_interface_type
10077 || part_die->tag == DW_TAG_structure_type
10078 || part_die->tag == DW_TAG_union_type)
53832f31
TT
10079 && part_die->linkage_name != NULL)
10080 {
10081 char *demangled;
10082
10083 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10084 if (demangled)
10085 {
96408a79
SA
10086 const char *base;
10087
10088 /* Strip any leading namespaces/classes, keep only the base name.
10089 DW_AT_name for named DIEs does not contain the prefixes. */
10090 base = strrchr (demangled, ':');
10091 if (base && base > demangled && base[-1] == ':')
10092 base++;
10093 else
10094 base = demangled;
10095
10096 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
10097 &cu->objfile->objfile_obstack);
10098 xfree (demangled);
10099 }
10100 }
10101
abc72ce4 10102 part_die->fixup_called = 1;
72bf9492
DJ
10103}
10104
a8329558 10105/* Read an attribute value described by an attribute form. */
c906108c 10106
fe1b8b76 10107static gdb_byte *
a8329558 10108read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 10109 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 10110 struct dwarf2_cu *cu)
c906108c 10111{
e7c27a73 10112 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10113 unsigned int bytes_read;
10114 struct dwarf_block *blk;
10115
a8329558
KW
10116 attr->form = form;
10117 switch (form)
c906108c 10118 {
c906108c 10119 case DW_FORM_ref_addr:
ae411497
TT
10120 if (cu->header.version == 2)
10121 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10122 else
3e43a32a
MS
10123 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10124 &cu->header, &bytes_read);
ae411497
TT
10125 info_ptr += bytes_read;
10126 break;
10127 case DW_FORM_addr:
e7c27a73 10128 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 10129 info_ptr += bytes_read;
c906108c
SS
10130 break;
10131 case DW_FORM_block2:
7b5a2f43 10132 blk = dwarf_alloc_block (cu);
c906108c
SS
10133 blk->size = read_2_bytes (abfd, info_ptr);
10134 info_ptr += 2;
10135 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10136 info_ptr += blk->size;
10137 DW_BLOCK (attr) = blk;
10138 break;
10139 case DW_FORM_block4:
7b5a2f43 10140 blk = dwarf_alloc_block (cu);
c906108c
SS
10141 blk->size = read_4_bytes (abfd, info_ptr);
10142 info_ptr += 4;
10143 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10144 info_ptr += blk->size;
10145 DW_BLOCK (attr) = blk;
10146 break;
10147 case DW_FORM_data2:
10148 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10149 info_ptr += 2;
10150 break;
10151 case DW_FORM_data4:
10152 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10153 info_ptr += 4;
10154 break;
10155 case DW_FORM_data8:
10156 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10157 info_ptr += 8;
10158 break;
2dc7f7b3
TT
10159 case DW_FORM_sec_offset:
10160 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10161 info_ptr += bytes_read;
10162 break;
c906108c 10163 case DW_FORM_string:
9b1c24c8 10164 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 10165 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
10166 info_ptr += bytes_read;
10167 break;
4bdf3d34
JJ
10168 case DW_FORM_strp:
10169 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10170 &bytes_read);
8285870a 10171 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
10172 info_ptr += bytes_read;
10173 break;
2dc7f7b3 10174 case DW_FORM_exprloc:
c906108c 10175 case DW_FORM_block:
7b5a2f43 10176 blk = dwarf_alloc_block (cu);
c906108c
SS
10177 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10178 info_ptr += bytes_read;
10179 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10180 info_ptr += blk->size;
10181 DW_BLOCK (attr) = blk;
10182 break;
10183 case DW_FORM_block1:
7b5a2f43 10184 blk = dwarf_alloc_block (cu);
c906108c
SS
10185 blk->size = read_1_byte (abfd, info_ptr);
10186 info_ptr += 1;
10187 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10188 info_ptr += blk->size;
10189 DW_BLOCK (attr) = blk;
10190 break;
10191 case DW_FORM_data1:
10192 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10193 info_ptr += 1;
10194 break;
10195 case DW_FORM_flag:
10196 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10197 info_ptr += 1;
10198 break;
2dc7f7b3
TT
10199 case DW_FORM_flag_present:
10200 DW_UNSND (attr) = 1;
10201 break;
c906108c
SS
10202 case DW_FORM_sdata:
10203 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10204 info_ptr += bytes_read;
10205 break;
10206 case DW_FORM_udata:
10207 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10208 info_ptr += bytes_read;
10209 break;
10210 case DW_FORM_ref1:
10b3939b 10211 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
10212 info_ptr += 1;
10213 break;
10214 case DW_FORM_ref2:
10b3939b 10215 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
10216 info_ptr += 2;
10217 break;
10218 case DW_FORM_ref4:
10b3939b 10219 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
10220 info_ptr += 4;
10221 break;
613e1657 10222 case DW_FORM_ref8:
10b3939b 10223 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
10224 info_ptr += 8;
10225 break;
55f1336d 10226 case DW_FORM_ref_sig8:
348e048f
DE
10227 /* Convert the signature to something we can record in DW_UNSND
10228 for later lookup.
10229 NOTE: This is NULL if the type wasn't found. */
10230 DW_SIGNATURED_TYPE (attr) =
10231 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10232 info_ptr += 8;
10233 break;
c906108c 10234 case DW_FORM_ref_udata:
10b3939b
DJ
10235 DW_ADDR (attr) = (cu->header.offset
10236 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
10237 info_ptr += bytes_read;
10238 break;
c906108c 10239 case DW_FORM_indirect:
a8329558
KW
10240 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10241 info_ptr += bytes_read;
e7c27a73 10242 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 10243 break;
c906108c 10244 default:
8a3fe4f8 10245 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
10246 dwarf_form_name (form),
10247 bfd_get_filename (abfd));
c906108c 10248 }
28e94949
JB
10249
10250 /* We have seen instances where the compiler tried to emit a byte
10251 size attribute of -1 which ended up being encoded as an unsigned
10252 0xffffffff. Although 0xffffffff is technically a valid size value,
10253 an object of this size seems pretty unlikely so we can relatively
10254 safely treat these cases as if the size attribute was invalid and
10255 treat them as zero by default. */
10256 if (attr->name == DW_AT_byte_size
10257 && form == DW_FORM_data4
10258 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
10259 {
10260 complaint
10261 (&symfile_complaints,
43bbcdc2
PH
10262 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10263 hex_string (DW_UNSND (attr)));
01c66ae6
JB
10264 DW_UNSND (attr) = 0;
10265 }
28e94949 10266
c906108c
SS
10267 return info_ptr;
10268}
10269
a8329558
KW
10270/* Read an attribute described by an abbreviated attribute. */
10271
fe1b8b76 10272static gdb_byte *
a8329558 10273read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 10274 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
10275{
10276 attr->name = abbrev->name;
e7c27a73 10277 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
10278}
10279
0963b4bd 10280/* Read dwarf information from a buffer. */
c906108c
SS
10281
10282static unsigned int
fe1b8b76 10283read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 10284{
fe1b8b76 10285 return bfd_get_8 (abfd, buf);
c906108c
SS
10286}
10287
10288static int
fe1b8b76 10289read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 10290{
fe1b8b76 10291 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
10292}
10293
10294static unsigned int
fe1b8b76 10295read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10296{
fe1b8b76 10297 return bfd_get_16 (abfd, buf);
c906108c
SS
10298}
10299
c906108c 10300static unsigned int
fe1b8b76 10301read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10302{
fe1b8b76 10303 return bfd_get_32 (abfd, buf);
c906108c
SS
10304}
10305
93311388 10306static ULONGEST
fe1b8b76 10307read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10308{
fe1b8b76 10309 return bfd_get_64 (abfd, buf);
c906108c
SS
10310}
10311
10312static CORE_ADDR
fe1b8b76 10313read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 10314 unsigned int *bytes_read)
c906108c 10315{
e7c27a73 10316 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10317 CORE_ADDR retval = 0;
10318
107d2387 10319 if (cu_header->signed_addr_p)
c906108c 10320 {
107d2387
AC
10321 switch (cu_header->addr_size)
10322 {
10323 case 2:
fe1b8b76 10324 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
10325 break;
10326 case 4:
fe1b8b76 10327 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
10328 break;
10329 case 8:
fe1b8b76 10330 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
10331 break;
10332 default:
8e65ff28 10333 internal_error (__FILE__, __LINE__,
e2e0b3e5 10334 _("read_address: bad switch, signed [in module %s]"),
659b0389 10335 bfd_get_filename (abfd));
107d2387
AC
10336 }
10337 }
10338 else
10339 {
10340 switch (cu_header->addr_size)
10341 {
10342 case 2:
fe1b8b76 10343 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10344 break;
10345 case 4:
fe1b8b76 10346 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10347 break;
10348 case 8:
fe1b8b76 10349 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10350 break;
10351 default:
8e65ff28 10352 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10353 _("read_address: bad switch, "
10354 "unsigned [in module %s]"),
659b0389 10355 bfd_get_filename (abfd));
107d2387 10356 }
c906108c 10357 }
64367e0a 10358
107d2387
AC
10359 *bytes_read = cu_header->addr_size;
10360 return retval;
c906108c
SS
10361}
10362
f7ef9339 10363/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10364 specification allows the initial length to take up either 4 bytes
10365 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10366 bytes describe the length and all offsets will be 8 bytes in length
10367 instead of 4.
10368
f7ef9339
KB
10369 An older, non-standard 64-bit format is also handled by this
10370 function. The older format in question stores the initial length
10371 as an 8-byte quantity without an escape value. Lengths greater
10372 than 2^32 aren't very common which means that the initial 4 bytes
10373 is almost always zero. Since a length value of zero doesn't make
10374 sense for the 32-bit format, this initial zero can be considered to
10375 be an escape value which indicates the presence of the older 64-bit
10376 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10377 greater than 4GB. If it becomes necessary to handle lengths
10378 somewhat larger than 4GB, we could allow other small values (such
10379 as the non-sensical values of 1, 2, and 3) to also be used as
10380 escape values indicating the presence of the old format.
f7ef9339 10381
917c78fc
MK
10382 The value returned via bytes_read should be used to increment the
10383 relevant pointer after calling read_initial_length().
c764a876 10384
613e1657
KB
10385 [ Note: read_initial_length() and read_offset() are based on the
10386 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10387 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10388 from:
10389
f7ef9339 10390 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10391
613e1657
KB
10392 This document is only a draft and is subject to change. (So beware.)
10393
f7ef9339 10394 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10395 determined empirically by examining 64-bit ELF files produced by
10396 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10397
10398 - Kevin, July 16, 2002
613e1657
KB
10399 ] */
10400
10401static LONGEST
c764a876 10402read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10403{
fe1b8b76 10404 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10405
dd373385 10406 if (length == 0xffffffff)
613e1657 10407 {
fe1b8b76 10408 length = bfd_get_64 (abfd, buf + 4);
613e1657 10409 *bytes_read = 12;
613e1657 10410 }
dd373385 10411 else if (length == 0)
f7ef9339 10412 {
dd373385 10413 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10414 length = bfd_get_64 (abfd, buf);
f7ef9339 10415 *bytes_read = 8;
f7ef9339 10416 }
613e1657
KB
10417 else
10418 {
10419 *bytes_read = 4;
613e1657
KB
10420 }
10421
c764a876
DE
10422 return length;
10423}
dd373385 10424
c764a876
DE
10425/* Cover function for read_initial_length.
10426 Returns the length of the object at BUF, and stores the size of the
10427 initial length in *BYTES_READ and stores the size that offsets will be in
10428 *OFFSET_SIZE.
10429 If the initial length size is not equivalent to that specified in
10430 CU_HEADER then issue a complaint.
10431 This is useful when reading non-comp-unit headers. */
dd373385 10432
c764a876
DE
10433static LONGEST
10434read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10435 const struct comp_unit_head *cu_header,
10436 unsigned int *bytes_read,
10437 unsigned int *offset_size)
10438{
10439 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10440
10441 gdb_assert (cu_header->initial_length_size == 4
10442 || cu_header->initial_length_size == 8
10443 || cu_header->initial_length_size == 12);
10444
10445 if (cu_header->initial_length_size != *bytes_read)
10446 complaint (&symfile_complaints,
10447 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10448
c764a876 10449 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10450 return length;
613e1657
KB
10451}
10452
10453/* Read an offset from the data stream. The size of the offset is
917c78fc 10454 given by cu_header->offset_size. */
613e1657
KB
10455
10456static LONGEST
fe1b8b76 10457read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10458 unsigned int *bytes_read)
c764a876
DE
10459{
10460 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10461
c764a876
DE
10462 *bytes_read = cu_header->offset_size;
10463 return offset;
10464}
10465
10466/* Read an offset from the data stream. */
10467
10468static LONGEST
10469read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10470{
10471 LONGEST retval = 0;
10472
c764a876 10473 switch (offset_size)
613e1657
KB
10474 {
10475 case 4:
fe1b8b76 10476 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10477 break;
10478 case 8:
fe1b8b76 10479 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10480 break;
10481 default:
8e65ff28 10482 internal_error (__FILE__, __LINE__,
c764a876 10483 _("read_offset_1: bad switch [in module %s]"),
659b0389 10484 bfd_get_filename (abfd));
613e1657
KB
10485 }
10486
917c78fc 10487 return retval;
613e1657
KB
10488}
10489
fe1b8b76
JB
10490static gdb_byte *
10491read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10492{
10493 /* If the size of a host char is 8 bits, we can return a pointer
10494 to the buffer, otherwise we have to copy the data to a buffer
10495 allocated on the temporary obstack. */
4bdf3d34 10496 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10497 return buf;
c906108c
SS
10498}
10499
10500static char *
9b1c24c8 10501read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10502{
10503 /* If the size of a host char is 8 bits, we can return a pointer
10504 to the string, otherwise we have to copy the string to a buffer
10505 allocated on the temporary obstack. */
4bdf3d34 10506 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10507 if (*buf == '\0')
10508 {
10509 *bytes_read_ptr = 1;
10510 return NULL;
10511 }
fe1b8b76
JB
10512 *bytes_read_ptr = strlen ((char *) buf) + 1;
10513 return (char *) buf;
4bdf3d34
JJ
10514}
10515
10516static char *
cf2c3c16 10517read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 10518{
be391dca 10519 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10520 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
10521 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10522 bfd_get_filename (abfd));
dce234bc 10523 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
10524 error (_("DW_FORM_strp pointing outside of "
10525 ".debug_str section [in module %s]"),
10526 bfd_get_filename (abfd));
4bdf3d34 10527 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10528 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10529 return NULL;
dce234bc 10530 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10531}
10532
cf2c3c16
TT
10533static char *
10534read_indirect_string (bfd *abfd, gdb_byte *buf,
10535 const struct comp_unit_head *cu_header,
10536 unsigned int *bytes_read_ptr)
10537{
10538 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10539
10540 return read_indirect_string_at_offset (abfd, str_offset);
10541}
10542
ce5d95e1 10543static unsigned long
fe1b8b76 10544read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10545{
ce5d95e1
JB
10546 unsigned long result;
10547 unsigned int num_read;
c906108c
SS
10548 int i, shift;
10549 unsigned char byte;
10550
10551 result = 0;
10552 shift = 0;
10553 num_read = 0;
10554 i = 0;
10555 while (1)
10556 {
fe1b8b76 10557 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10558 buf++;
10559 num_read++;
ce5d95e1 10560 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10561 if ((byte & 128) == 0)
10562 {
10563 break;
10564 }
10565 shift += 7;
10566 }
10567 *bytes_read_ptr = num_read;
10568 return result;
10569}
10570
ce5d95e1 10571static long
fe1b8b76 10572read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10573{
ce5d95e1 10574 long result;
77e0b926 10575 int i, shift, num_read;
c906108c
SS
10576 unsigned char byte;
10577
10578 result = 0;
10579 shift = 0;
c906108c
SS
10580 num_read = 0;
10581 i = 0;
10582 while (1)
10583 {
fe1b8b76 10584 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10585 buf++;
10586 num_read++;
ce5d95e1 10587 result |= ((long)(byte & 127) << shift);
c906108c
SS
10588 shift += 7;
10589 if ((byte & 128) == 0)
10590 {
10591 break;
10592 }
10593 }
77e0b926
DJ
10594 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10595 result |= -(((long)1) << shift);
c906108c
SS
10596 *bytes_read_ptr = num_read;
10597 return result;
10598}
10599
4bb7a0a7
DJ
10600/* Return a pointer to just past the end of an LEB128 number in BUF. */
10601
fe1b8b76
JB
10602static gdb_byte *
10603skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10604{
10605 int byte;
10606
10607 while (1)
10608 {
fe1b8b76 10609 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10610 buf++;
10611 if ((byte & 128) == 0)
10612 return buf;
10613 }
10614}
10615
c906108c 10616static void
e142c38c 10617set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10618{
10619 switch (lang)
10620 {
10621 case DW_LANG_C89:
76bee0cc 10622 case DW_LANG_C99:
c906108c 10623 case DW_LANG_C:
e142c38c 10624 cu->language = language_c;
c906108c
SS
10625 break;
10626 case DW_LANG_C_plus_plus:
e142c38c 10627 cu->language = language_cplus;
c906108c 10628 break;
6aecb9c2
JB
10629 case DW_LANG_D:
10630 cu->language = language_d;
10631 break;
c906108c
SS
10632 case DW_LANG_Fortran77:
10633 case DW_LANG_Fortran90:
b21b22e0 10634 case DW_LANG_Fortran95:
e142c38c 10635 cu->language = language_fortran;
c906108c
SS
10636 break;
10637 case DW_LANG_Mips_Assembler:
e142c38c 10638 cu->language = language_asm;
c906108c 10639 break;
bebd888e 10640 case DW_LANG_Java:
e142c38c 10641 cu->language = language_java;
bebd888e 10642 break;
c906108c 10643 case DW_LANG_Ada83:
8aaf0b47 10644 case DW_LANG_Ada95:
bc5f45f8
JB
10645 cu->language = language_ada;
10646 break;
72019c9c
GM
10647 case DW_LANG_Modula2:
10648 cu->language = language_m2;
10649 break;
fe8e67fd
PM
10650 case DW_LANG_Pascal83:
10651 cu->language = language_pascal;
10652 break;
22566fbd
DJ
10653 case DW_LANG_ObjC:
10654 cu->language = language_objc;
10655 break;
c906108c
SS
10656 case DW_LANG_Cobol74:
10657 case DW_LANG_Cobol85:
c906108c 10658 default:
e142c38c 10659 cu->language = language_minimal;
c906108c
SS
10660 break;
10661 }
e142c38c 10662 cu->language_defn = language_def (cu->language);
c906108c
SS
10663}
10664
10665/* Return the named attribute or NULL if not there. */
10666
10667static struct attribute *
e142c38c 10668dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10669{
10670 unsigned int i;
10671 struct attribute *spec = NULL;
10672
10673 for (i = 0; i < die->num_attrs; ++i)
10674 {
10675 if (die->attrs[i].name == name)
10b3939b 10676 return &die->attrs[i];
c906108c
SS
10677 if (die->attrs[i].name == DW_AT_specification
10678 || die->attrs[i].name == DW_AT_abstract_origin)
10679 spec = &die->attrs[i];
10680 }
c906108c 10681
10b3939b 10682 if (spec)
f2f0e013
DJ
10683 {
10684 die = follow_die_ref (die, spec, &cu);
10685 return dwarf2_attr (die, name, cu);
10686 }
c5aa993b 10687
c906108c
SS
10688 return NULL;
10689}
10690
348e048f
DE
10691/* Return the named attribute or NULL if not there,
10692 but do not follow DW_AT_specification, etc.
10693 This is for use in contexts where we're reading .debug_types dies.
10694 Following DW_AT_specification, DW_AT_abstract_origin will take us
10695 back up the chain, and we want to go down. */
10696
10697static struct attribute *
10698dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10699 struct dwarf2_cu *cu)
10700{
10701 unsigned int i;
10702
10703 for (i = 0; i < die->num_attrs; ++i)
10704 if (die->attrs[i].name == name)
10705 return &die->attrs[i];
10706
10707 return NULL;
10708}
10709
05cf31d1
JB
10710/* Return non-zero iff the attribute NAME is defined for the given DIE,
10711 and holds a non-zero value. This function should only be used for
2dc7f7b3 10712 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10713
10714static int
10715dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10716{
10717 struct attribute *attr = dwarf2_attr (die, name, cu);
10718
10719 return (attr && DW_UNSND (attr));
10720}
10721
3ca72b44 10722static int
e142c38c 10723die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10724{
05cf31d1
JB
10725 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10726 which value is non-zero. However, we have to be careful with
10727 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10728 (via dwarf2_flag_true_p) follows this attribute. So we may
10729 end up accidently finding a declaration attribute that belongs
10730 to a different DIE referenced by the specification attribute,
10731 even though the given DIE does not have a declaration attribute. */
10732 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10733 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10734}
10735
63d06c5c 10736/* Return the die giving the specification for DIE, if there is
f2f0e013 10737 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10738 containing the return value on output. If there is no
10739 specification, but there is an abstract origin, that is
10740 returned. */
63d06c5c
DC
10741
10742static struct die_info *
f2f0e013 10743die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10744{
f2f0e013
DJ
10745 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10746 *spec_cu);
63d06c5c 10747
edb3359d
DJ
10748 if (spec_attr == NULL)
10749 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10750
63d06c5c
DC
10751 if (spec_attr == NULL)
10752 return NULL;
10753 else
f2f0e013 10754 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10755}
c906108c 10756
debd256d 10757/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10758 refers to.
10759 NOTE: This is also used as a "cleanup" function. */
10760
debd256d
JB
10761static void
10762free_line_header (struct line_header *lh)
10763{
10764 if (lh->standard_opcode_lengths)
a8bc7b56 10765 xfree (lh->standard_opcode_lengths);
debd256d
JB
10766
10767 /* Remember that all the lh->file_names[i].name pointers are
10768 pointers into debug_line_buffer, and don't need to be freed. */
10769 if (lh->file_names)
a8bc7b56 10770 xfree (lh->file_names);
debd256d
JB
10771
10772 /* Similarly for the include directory names. */
10773 if (lh->include_dirs)
a8bc7b56 10774 xfree (lh->include_dirs);
debd256d 10775
a8bc7b56 10776 xfree (lh);
debd256d
JB
10777}
10778
debd256d 10779/* Add an entry to LH's include directory table. */
ae2de4f8 10780
debd256d
JB
10781static void
10782add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10783{
debd256d
JB
10784 /* Grow the array if necessary. */
10785 if (lh->include_dirs_size == 0)
c5aa993b 10786 {
debd256d
JB
10787 lh->include_dirs_size = 1; /* for testing */
10788 lh->include_dirs = xmalloc (lh->include_dirs_size
10789 * sizeof (*lh->include_dirs));
10790 }
10791 else if (lh->num_include_dirs >= lh->include_dirs_size)
10792 {
10793 lh->include_dirs_size *= 2;
10794 lh->include_dirs = xrealloc (lh->include_dirs,
10795 (lh->include_dirs_size
10796 * sizeof (*lh->include_dirs)));
c5aa993b 10797 }
c906108c 10798
debd256d
JB
10799 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10800}
6e70227d 10801
debd256d 10802/* Add an entry to LH's file name table. */
ae2de4f8 10803
debd256d
JB
10804static void
10805add_file_name (struct line_header *lh,
10806 char *name,
10807 unsigned int dir_index,
10808 unsigned int mod_time,
10809 unsigned int length)
10810{
10811 struct file_entry *fe;
10812
10813 /* Grow the array if necessary. */
10814 if (lh->file_names_size == 0)
10815 {
10816 lh->file_names_size = 1; /* for testing */
10817 lh->file_names = xmalloc (lh->file_names_size
10818 * sizeof (*lh->file_names));
10819 }
10820 else if (lh->num_file_names >= lh->file_names_size)
10821 {
10822 lh->file_names_size *= 2;
10823 lh->file_names = xrealloc (lh->file_names,
10824 (lh->file_names_size
10825 * sizeof (*lh->file_names)));
10826 }
10827
10828 fe = &lh->file_names[lh->num_file_names++];
10829 fe->name = name;
10830 fe->dir_index = dir_index;
10831 fe->mod_time = mod_time;
10832 fe->length = length;
aaa75496 10833 fe->included_p = 0;
cb1df416 10834 fe->symtab = NULL;
debd256d 10835}
6e70227d 10836
debd256d 10837/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10838 .debug_line, according to the endianness of ABFD. Return a pointer
10839 to a struct line_header, allocated using xmalloc.
debd256d
JB
10840
10841 NOTE: the strings in the include directory and file name tables of
10842 the returned object point into debug_line_buffer, and must not be
10843 freed. */
ae2de4f8 10844
debd256d
JB
10845static struct line_header *
10846dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10847 struct dwarf2_cu *cu)
debd256d
JB
10848{
10849 struct cleanup *back_to;
10850 struct line_header *lh;
fe1b8b76 10851 gdb_byte *line_ptr;
c764a876 10852 unsigned int bytes_read, offset_size;
debd256d
JB
10853 int i;
10854 char *cur_dir, *cur_file;
10855
be391dca 10856 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10857 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10858 {
e2e0b3e5 10859 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10860 return 0;
10861 }
10862
a738430d
MK
10863 /* Make sure that at least there's room for the total_length field.
10864 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10865 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10866 {
4d3c2250 10867 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10868 return 0;
10869 }
10870
10871 lh = xmalloc (sizeof (*lh));
10872 memset (lh, 0, sizeof (*lh));
10873 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10874 (void *) lh);
10875
dce234bc 10876 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10877
a738430d 10878 /* Read in the header. */
6e70227d 10879 lh->total_length =
c764a876
DE
10880 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10881 &bytes_read, &offset_size);
debd256d 10882 line_ptr += bytes_read;
dce234bc
PP
10883 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10884 + dwarf2_per_objfile->line.size))
debd256d 10885 {
4d3c2250 10886 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10887 return 0;
10888 }
10889 lh->statement_program_end = line_ptr + lh->total_length;
10890 lh->version = read_2_bytes (abfd, line_ptr);
10891 line_ptr += 2;
c764a876
DE
10892 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10893 line_ptr += offset_size;
debd256d
JB
10894 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10895 line_ptr += 1;
2dc7f7b3
TT
10896 if (lh->version >= 4)
10897 {
10898 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10899 line_ptr += 1;
10900 }
10901 else
10902 lh->maximum_ops_per_instruction = 1;
10903
10904 if (lh->maximum_ops_per_instruction == 0)
10905 {
10906 lh->maximum_ops_per_instruction = 1;
10907 complaint (&symfile_complaints,
3e43a32a
MS
10908 _("invalid maximum_ops_per_instruction "
10909 "in `.debug_line' section"));
2dc7f7b3
TT
10910 }
10911
debd256d
JB
10912 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10913 line_ptr += 1;
10914 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10915 line_ptr += 1;
10916 lh->line_range = read_1_byte (abfd, line_ptr);
10917 line_ptr += 1;
10918 lh->opcode_base = read_1_byte (abfd, line_ptr);
10919 line_ptr += 1;
10920 lh->standard_opcode_lengths
fe1b8b76 10921 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10922
10923 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10924 for (i = 1; i < lh->opcode_base; ++i)
10925 {
10926 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10927 line_ptr += 1;
10928 }
10929
a738430d 10930 /* Read directory table. */
9b1c24c8 10931 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10932 {
10933 line_ptr += bytes_read;
10934 add_include_dir (lh, cur_dir);
10935 }
10936 line_ptr += bytes_read;
10937
a738430d 10938 /* Read file name table. */
9b1c24c8 10939 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10940 {
10941 unsigned int dir_index, mod_time, length;
10942
10943 line_ptr += bytes_read;
10944 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10945 line_ptr += bytes_read;
10946 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10947 line_ptr += bytes_read;
10948 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10949 line_ptr += bytes_read;
10950
10951 add_file_name (lh, cur_file, dir_index, mod_time, length);
10952 }
10953 line_ptr += bytes_read;
6e70227d 10954 lh->statement_program_start = line_ptr;
debd256d 10955
dce234bc
PP
10956 if (line_ptr > (dwarf2_per_objfile->line.buffer
10957 + dwarf2_per_objfile->line.size))
4d3c2250 10958 complaint (&symfile_complaints,
3e43a32a
MS
10959 _("line number info header doesn't "
10960 "fit in `.debug_line' section"));
debd256d
JB
10961
10962 discard_cleanups (back_to);
10963 return lh;
10964}
c906108c 10965
5fb290d7
DJ
10966/* This function exists to work around a bug in certain compilers
10967 (particularly GCC 2.95), in which the first line number marker of a
10968 function does not show up until after the prologue, right before
10969 the second line number marker. This function shifts ADDRESS down
10970 to the beginning of the function if necessary, and is called on
10971 addresses passed to record_line. */
10972
10973static CORE_ADDR
e142c38c 10974check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10975{
10976 struct function_range *fn;
10977
10978 /* Find the function_range containing address. */
e142c38c 10979 if (!cu->first_fn)
5fb290d7
DJ
10980 return address;
10981
e142c38c
DJ
10982 if (!cu->cached_fn)
10983 cu->cached_fn = cu->first_fn;
5fb290d7 10984
e142c38c 10985 fn = cu->cached_fn;
5fb290d7
DJ
10986 while (fn)
10987 if (fn->lowpc <= address && fn->highpc > address)
10988 goto found;
10989 else
10990 fn = fn->next;
10991
e142c38c
DJ
10992 fn = cu->first_fn;
10993 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10994 if (fn->lowpc <= address && fn->highpc > address)
10995 goto found;
10996 else
10997 fn = fn->next;
10998
10999 return address;
11000
11001 found:
11002 if (fn->seen_line)
11003 return address;
11004 if (address != fn->lowpc)
4d3c2250 11005 complaint (&symfile_complaints,
e2e0b3e5 11006 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 11007 (unsigned long) address, fn->name);
5fb290d7
DJ
11008 fn->seen_line = 1;
11009 return fn->lowpc;
11010}
11011
c6da4cef
DE
11012/* Subroutine of dwarf_decode_lines to simplify it.
11013 Return the file name of the psymtab for included file FILE_INDEX
11014 in line header LH of PST.
11015 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11016 If space for the result is malloc'd, it will be freed by a cleanup.
11017 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11018
11019static char *
11020psymtab_include_file_name (const struct line_header *lh, int file_index,
11021 const struct partial_symtab *pst,
11022 const char *comp_dir)
11023{
11024 const struct file_entry fe = lh->file_names [file_index];
11025 char *include_name = fe.name;
11026 char *include_name_to_compare = include_name;
11027 char *dir_name = NULL;
72b9f47f
TT
11028 const char *pst_filename;
11029 char *copied_name = NULL;
c6da4cef
DE
11030 int file_is_pst;
11031
11032 if (fe.dir_index)
11033 dir_name = lh->include_dirs[fe.dir_index - 1];
11034
11035 if (!IS_ABSOLUTE_PATH (include_name)
11036 && (dir_name != NULL || comp_dir != NULL))
11037 {
11038 /* Avoid creating a duplicate psymtab for PST.
11039 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11040 Before we do the comparison, however, we need to account
11041 for DIR_NAME and COMP_DIR.
11042 First prepend dir_name (if non-NULL). If we still don't
11043 have an absolute path prepend comp_dir (if non-NULL).
11044 However, the directory we record in the include-file's
11045 psymtab does not contain COMP_DIR (to match the
11046 corresponding symtab(s)).
11047
11048 Example:
11049
11050 bash$ cd /tmp
11051 bash$ gcc -g ./hello.c
11052 include_name = "hello.c"
11053 dir_name = "."
11054 DW_AT_comp_dir = comp_dir = "/tmp"
11055 DW_AT_name = "./hello.c" */
11056
11057 if (dir_name != NULL)
11058 {
11059 include_name = concat (dir_name, SLASH_STRING,
11060 include_name, (char *)NULL);
11061 include_name_to_compare = include_name;
11062 make_cleanup (xfree, include_name);
11063 }
11064 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11065 {
11066 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11067 include_name, (char *)NULL);
11068 }
11069 }
11070
11071 pst_filename = pst->filename;
11072 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11073 {
72b9f47f
TT
11074 copied_name = concat (pst->dirname, SLASH_STRING,
11075 pst_filename, (char *)NULL);
11076 pst_filename = copied_name;
c6da4cef
DE
11077 }
11078
1e3fad37 11079 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
11080
11081 if (include_name_to_compare != include_name)
11082 xfree (include_name_to_compare);
72b9f47f
TT
11083 if (copied_name != NULL)
11084 xfree (copied_name);
c6da4cef
DE
11085
11086 if (file_is_pst)
11087 return NULL;
11088 return include_name;
11089}
11090
c91513d8
PP
11091/* Ignore this record_line request. */
11092
11093static void
11094noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11095{
11096 return;
11097}
11098
aaa75496
JB
11099/* Decode the Line Number Program (LNP) for the given line_header
11100 structure and CU. The actual information extracted and the type
11101 of structures created from the LNP depends on the value of PST.
11102
11103 1. If PST is NULL, then this procedure uses the data from the program
11104 to create all necessary symbol tables, and their linetables.
6e70227d 11105
aaa75496
JB
11106 2. If PST is not NULL, this procedure reads the program to determine
11107 the list of files included by the unit represented by PST, and
c6da4cef
DE
11108 builds all the associated partial symbol tables.
11109
11110 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11111 It is used for relative paths in the line table.
11112 NOTE: When processing partial symtabs (pst != NULL),
11113 comp_dir == pst->dirname.
11114
11115 NOTE: It is important that psymtabs have the same file name (via strcmp)
11116 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11117 symtab we don't use it in the name of the psymtabs we create.
11118 E.g. expand_line_sal requires this when finding psymtabs to expand.
11119 A good testcase for this is mb-inline.exp. */
debd256d 11120
c906108c 11121static void
72b9f47f 11122dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 11123 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 11124{
a8c50c1f 11125 gdb_byte *line_ptr, *extended_end;
fe1b8b76 11126 gdb_byte *line_end;
a8c50c1f 11127 unsigned int bytes_read, extended_len;
c906108c 11128 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
11129 CORE_ADDR baseaddr;
11130 struct objfile *objfile = cu->objfile;
fbf65064 11131 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 11132 const int decode_for_pst_p = (pst != NULL);
cb1df416 11133 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
11134 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11135 = record_line;
e142c38c
DJ
11136
11137 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11138
debd256d
JB
11139 line_ptr = lh->statement_program_start;
11140 line_end = lh->statement_program_end;
c906108c
SS
11141
11142 /* Read the statement sequences until there's nothing left. */
11143 while (line_ptr < line_end)
11144 {
11145 /* state machine registers */
11146 CORE_ADDR address = 0;
11147 unsigned int file = 1;
11148 unsigned int line = 1;
11149 unsigned int column = 0;
debd256d 11150 int is_stmt = lh->default_is_stmt;
c906108c
SS
11151 int basic_block = 0;
11152 int end_sequence = 0;
fbf65064 11153 CORE_ADDR addr;
2dc7f7b3 11154 unsigned char op_index = 0;
c906108c 11155
aaa75496 11156 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 11157 {
aaa75496 11158 /* Start a subfile for the current file of the state machine. */
debd256d
JB
11159 /* lh->include_dirs and lh->file_names are 0-based, but the
11160 directory and file name numbers in the statement program
11161 are 1-based. */
11162 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 11163 char *dir = NULL;
a738430d 11164
debd256d
JB
11165 if (fe->dir_index)
11166 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
11167
11168 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
11169 }
11170
a738430d 11171 /* Decode the table. */
c5aa993b 11172 while (!end_sequence)
c906108c
SS
11173 {
11174 op_code = read_1_byte (abfd, line_ptr);
11175 line_ptr += 1;
59205f5a
JB
11176 if (line_ptr > line_end)
11177 {
11178 dwarf2_debug_line_missing_end_sequence_complaint ();
11179 break;
11180 }
9aa1fe7e 11181
debd256d 11182 if (op_code >= lh->opcode_base)
6e70227d 11183 {
a738430d 11184 /* Special operand. */
debd256d 11185 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
11186 address += (((op_index + (adj_opcode / lh->line_range))
11187 / lh->maximum_ops_per_instruction)
11188 * lh->minimum_instruction_length);
11189 op_index = ((op_index + (adj_opcode / lh->line_range))
11190 % lh->maximum_ops_per_instruction);
debd256d 11191 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 11192 if (lh->num_file_names < file || file == 0)
25e43795 11193 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
11194 /* For now we ignore lines not starting on an
11195 instruction boundary. */
11196 else if (op_index == 0)
25e43795
DJ
11197 {
11198 lh->file_names[file - 1].included_p = 1;
ca5f395d 11199 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11200 {
11201 if (last_subfile != current_subfile)
11202 {
11203 addr = gdbarch_addr_bits_remove (gdbarch, address);
11204 if (last_subfile)
c91513d8 11205 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11206 last_subfile = current_subfile;
11207 }
25e43795 11208 /* Append row to matrix using current values. */
fbf65064
UW
11209 addr = check_cu_functions (address, cu);
11210 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 11211 (*p_record_line) (current_subfile, line, addr);
366da635 11212 }
25e43795 11213 }
ca5f395d 11214 basic_block = 0;
9aa1fe7e
GK
11215 }
11216 else switch (op_code)
c906108c
SS
11217 {
11218 case DW_LNS_extended_op:
3e43a32a
MS
11219 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11220 &bytes_read);
473b7be6 11221 line_ptr += bytes_read;
a8c50c1f 11222 extended_end = line_ptr + extended_len;
c906108c
SS
11223 extended_op = read_1_byte (abfd, line_ptr);
11224 line_ptr += 1;
11225 switch (extended_op)
11226 {
11227 case DW_LNE_end_sequence:
c91513d8 11228 p_record_line = record_line;
c906108c 11229 end_sequence = 1;
c906108c
SS
11230 break;
11231 case DW_LNE_set_address:
e7c27a73 11232 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
11233
11234 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11235 {
11236 /* This line table is for a function which has been
11237 GCd by the linker. Ignore it. PR gdb/12528 */
11238
11239 long line_offset
11240 = line_ptr - dwarf2_per_objfile->line.buffer;
11241
11242 complaint (&symfile_complaints,
11243 _(".debug_line address at offset 0x%lx is 0 "
11244 "[in module %s]"),
11245 line_offset, cu->objfile->name);
11246 p_record_line = noop_record_line;
11247 }
11248
2dc7f7b3 11249 op_index = 0;
107d2387
AC
11250 line_ptr += bytes_read;
11251 address += baseaddr;
c906108c
SS
11252 break;
11253 case DW_LNE_define_file:
debd256d
JB
11254 {
11255 char *cur_file;
11256 unsigned int dir_index, mod_time, length;
6e70227d 11257
3e43a32a
MS
11258 cur_file = read_direct_string (abfd, line_ptr,
11259 &bytes_read);
debd256d
JB
11260 line_ptr += bytes_read;
11261 dir_index =
11262 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11263 line_ptr += bytes_read;
11264 mod_time =
11265 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11266 line_ptr += bytes_read;
11267 length =
11268 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11269 line_ptr += bytes_read;
11270 add_file_name (lh, cur_file, dir_index, mod_time, length);
11271 }
c906108c 11272 break;
d0c6ba3d
CC
11273 case DW_LNE_set_discriminator:
11274 /* The discriminator is not interesting to the debugger;
11275 just ignore it. */
11276 line_ptr = extended_end;
11277 break;
c906108c 11278 default:
4d3c2250 11279 complaint (&symfile_complaints,
e2e0b3e5 11280 _("mangled .debug_line section"));
debd256d 11281 return;
c906108c 11282 }
a8c50c1f
DJ
11283 /* Make sure that we parsed the extended op correctly. If e.g.
11284 we expected a different address size than the producer used,
11285 we may have read the wrong number of bytes. */
11286 if (line_ptr != extended_end)
11287 {
11288 complaint (&symfile_complaints,
11289 _("mangled .debug_line section"));
11290 return;
11291 }
c906108c
SS
11292 break;
11293 case DW_LNS_copy:
59205f5a 11294 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11295 dwarf2_debug_line_missing_file_complaint ();
11296 else
366da635 11297 {
25e43795 11298 lh->file_names[file - 1].included_p = 1;
ca5f395d 11299 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11300 {
11301 if (last_subfile != current_subfile)
11302 {
11303 addr = gdbarch_addr_bits_remove (gdbarch, address);
11304 if (last_subfile)
c91513d8 11305 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11306 last_subfile = current_subfile;
11307 }
11308 addr = check_cu_functions (address, cu);
11309 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 11310 (*p_record_line) (current_subfile, line, addr);
fbf65064 11311 }
366da635 11312 }
c906108c
SS
11313 basic_block = 0;
11314 break;
11315 case DW_LNS_advance_pc:
2dc7f7b3
TT
11316 {
11317 CORE_ADDR adjust
11318 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11319
11320 address += (((op_index + adjust)
11321 / lh->maximum_ops_per_instruction)
11322 * lh->minimum_instruction_length);
11323 op_index = ((op_index + adjust)
11324 % lh->maximum_ops_per_instruction);
11325 line_ptr += bytes_read;
11326 }
c906108c
SS
11327 break;
11328 case DW_LNS_advance_line:
11329 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11330 line_ptr += bytes_read;
11331 break;
11332 case DW_LNS_set_file:
debd256d 11333 {
a738430d
MK
11334 /* The arrays lh->include_dirs and lh->file_names are
11335 0-based, but the directory and file name numbers in
11336 the statement program are 1-based. */
debd256d 11337 struct file_entry *fe;
4f1520fb 11338 char *dir = NULL;
a738430d 11339
debd256d
JB
11340 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11341 line_ptr += bytes_read;
59205f5a 11342 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11343 dwarf2_debug_line_missing_file_complaint ();
11344 else
11345 {
11346 fe = &lh->file_names[file - 1];
11347 if (fe->dir_index)
11348 dir = lh->include_dirs[fe->dir_index - 1];
11349 if (!decode_for_pst_p)
11350 {
11351 last_subfile = current_subfile;
11352 dwarf2_start_subfile (fe->name, dir, comp_dir);
11353 }
11354 }
debd256d 11355 }
c906108c
SS
11356 break;
11357 case DW_LNS_set_column:
11358 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11359 line_ptr += bytes_read;
11360 break;
11361 case DW_LNS_negate_stmt:
11362 is_stmt = (!is_stmt);
11363 break;
11364 case DW_LNS_set_basic_block:
11365 basic_block = 1;
11366 break;
c2c6d25f
JM
11367 /* Add to the address register of the state machine the
11368 address increment value corresponding to special opcode
a738430d
MK
11369 255. I.e., this value is scaled by the minimum
11370 instruction length since special opcode 255 would have
b021a221 11371 scaled the increment. */
c906108c 11372 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11373 {
11374 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11375
11376 address += (((op_index + adjust)
11377 / lh->maximum_ops_per_instruction)
11378 * lh->minimum_instruction_length);
11379 op_index = ((op_index + adjust)
11380 % lh->maximum_ops_per_instruction);
11381 }
c906108c
SS
11382 break;
11383 case DW_LNS_fixed_advance_pc:
11384 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11385 op_index = 0;
c906108c
SS
11386 line_ptr += 2;
11387 break;
9aa1fe7e 11388 default:
a738430d
MK
11389 {
11390 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11391 int i;
a738430d 11392
debd256d 11393 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11394 {
11395 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11396 line_ptr += bytes_read;
11397 }
11398 }
c906108c
SS
11399 }
11400 }
59205f5a
JB
11401 if (lh->num_file_names < file || file == 0)
11402 dwarf2_debug_line_missing_file_complaint ();
11403 else
11404 {
11405 lh->file_names[file - 1].included_p = 1;
11406 if (!decode_for_pst_p)
fbf65064
UW
11407 {
11408 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11409 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11410 }
59205f5a 11411 }
c906108c 11412 }
aaa75496
JB
11413
11414 if (decode_for_pst_p)
11415 {
11416 int file_index;
11417
11418 /* Now that we're done scanning the Line Header Program, we can
11419 create the psymtab of each included file. */
11420 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11421 if (lh->file_names[file_index].included_p == 1)
11422 {
c6da4cef
DE
11423 char *include_name =
11424 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11425 if (include_name != NULL)
aaa75496
JB
11426 dwarf2_create_include_psymtab (include_name, pst, objfile);
11427 }
11428 }
cb1df416
DJ
11429 else
11430 {
11431 /* Make sure a symtab is created for every file, even files
11432 which contain only variables (i.e. no code with associated
11433 line numbers). */
11434
11435 int i;
11436 struct file_entry *fe;
11437
11438 for (i = 0; i < lh->num_file_names; i++)
11439 {
11440 char *dir = NULL;
9a619af0 11441
cb1df416
DJ
11442 fe = &lh->file_names[i];
11443 if (fe->dir_index)
11444 dir = lh->include_dirs[fe->dir_index - 1];
11445 dwarf2_start_subfile (fe->name, dir, comp_dir);
11446
11447 /* Skip the main file; we don't need it, and it must be
11448 allocated last, so that it will show up before the
11449 non-primary symtabs in the objfile's symtab list. */
11450 if (current_subfile == first_subfile)
11451 continue;
11452
11453 if (current_subfile->symtab == NULL)
11454 current_subfile->symtab = allocate_symtab (current_subfile->name,
11455 cu->objfile);
11456 fe->symtab = current_subfile->symtab;
11457 }
11458 }
c906108c
SS
11459}
11460
11461/* Start a subfile for DWARF. FILENAME is the name of the file and
11462 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11463 or NULL if not known. COMP_DIR is the compilation directory for the
11464 linetable's compilation unit or NULL if not known.
c906108c
SS
11465 This routine tries to keep line numbers from identical absolute and
11466 relative file names in a common subfile.
11467
11468 Using the `list' example from the GDB testsuite, which resides in
11469 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11470 of /srcdir/list0.c yields the following debugging information for list0.c:
11471
c5aa993b
JM
11472 DW_AT_name: /srcdir/list0.c
11473 DW_AT_comp_dir: /compdir
357e46e7 11474 files.files[0].name: list0.h
c5aa993b 11475 files.files[0].dir: /srcdir
357e46e7 11476 files.files[1].name: list0.c
c5aa993b 11477 files.files[1].dir: /srcdir
c906108c
SS
11478
11479 The line number information for list0.c has to end up in a single
4f1520fb
FR
11480 subfile, so that `break /srcdir/list0.c:1' works as expected.
11481 start_subfile will ensure that this happens provided that we pass the
11482 concatenation of files.files[1].dir and files.files[1].name as the
11483 subfile's name. */
c906108c
SS
11484
11485static void
3e43a32a
MS
11486dwarf2_start_subfile (char *filename, const char *dirname,
11487 const char *comp_dir)
c906108c 11488{
4f1520fb
FR
11489 char *fullname;
11490
11491 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11492 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11493 second argument to start_subfile. To be consistent, we do the
11494 same here. In order not to lose the line information directory,
11495 we concatenate it to the filename when it makes sense.
11496 Note that the Dwarf3 standard says (speaking of filenames in line
11497 information): ``The directory index is ignored for file names
11498 that represent full path names''. Thus ignoring dirname in the
11499 `else' branch below isn't an issue. */
c906108c 11500
d5166ae1 11501 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11502 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11503 else
11504 fullname = filename;
c906108c 11505
4f1520fb
FR
11506 start_subfile (fullname, comp_dir);
11507
11508 if (fullname != filename)
11509 xfree (fullname);
c906108c
SS
11510}
11511
4c2df51b
DJ
11512static void
11513var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11514 struct dwarf2_cu *cu)
4c2df51b 11515{
e7c27a73
DJ
11516 struct objfile *objfile = cu->objfile;
11517 struct comp_unit_head *cu_header = &cu->header;
11518
4c2df51b
DJ
11519 /* NOTE drow/2003-01-30: There used to be a comment and some special
11520 code here to turn a symbol with DW_AT_external and a
11521 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11522 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11523 with some versions of binutils) where shared libraries could have
11524 relocations against symbols in their debug information - the
11525 minimal symbol would have the right address, but the debug info
11526 would not. It's no longer necessary, because we will explicitly
11527 apply relocations when we read in the debug information now. */
11528
11529 /* A DW_AT_location attribute with no contents indicates that a
11530 variable has been optimized away. */
11531 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11532 {
11533 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11534 return;
11535 }
11536
11537 /* Handle one degenerate form of location expression specially, to
11538 preserve GDB's previous behavior when section offsets are
11539 specified. If this is just a DW_OP_addr then mark this symbol
11540 as LOC_STATIC. */
11541
11542 if (attr_form_is_block (attr)
11543 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11544 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11545 {
891d2f0b 11546 unsigned int dummy;
4c2df51b
DJ
11547
11548 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11549 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11550 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11551 fixup_symbol_section (sym, objfile);
11552 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11553 SYMBOL_SECTION (sym));
4c2df51b
DJ
11554 return;
11555 }
11556
11557 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11558 expression evaluator, and use LOC_COMPUTED only when necessary
11559 (i.e. when the value of a register or memory location is
11560 referenced, or a thread-local block, etc.). Then again, it might
11561 not be worthwhile. I'm assuming that it isn't unless performance
11562 or memory numbers show me otherwise. */
11563
e7c27a73 11564 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11565 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11566
11567 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11568 cu->has_loclist = 1;
4c2df51b
DJ
11569}
11570
c906108c
SS
11571/* Given a pointer to a DWARF information entry, figure out if we need
11572 to make a symbol table entry for it, and if so, create a new entry
11573 and return a pointer to it.
11574 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11575 used the passed type.
11576 If SPACE is not NULL, use it to hold the new symbol. If it is
11577 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11578
11579static struct symbol *
34eaf542
TT
11580new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11581 struct symbol *space)
c906108c 11582{
e7c27a73 11583 struct objfile *objfile = cu->objfile;
c906108c
SS
11584 struct symbol *sym = NULL;
11585 char *name;
11586 struct attribute *attr = NULL;
11587 struct attribute *attr2 = NULL;
e142c38c 11588 CORE_ADDR baseaddr;
e37fd15a
SW
11589 struct pending **list_to_add = NULL;
11590
edb3359d 11591 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11592
11593 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11594
94af9270 11595 name = dwarf2_name (die, cu);
c906108c
SS
11596 if (name)
11597 {
94af9270 11598 const char *linkagename;
34eaf542 11599 int suppress_add = 0;
94af9270 11600
34eaf542
TT
11601 if (space)
11602 sym = space;
11603 else
11604 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11605 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11606
11607 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11608 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11609 linkagename = dwarf2_physname (name, die, cu);
11610 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11611
f55ee35c
JK
11612 /* Fortran does not have mangling standard and the mangling does differ
11613 between gfortran, iFort etc. */
11614 if (cu->language == language_fortran
b250c185 11615 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11616 symbol_set_demangled_name (&(sym->ginfo),
11617 (char *) dwarf2_full_name (name, die, cu),
11618 NULL);
f55ee35c 11619
c906108c 11620 /* Default assumptions.
c5aa993b 11621 Use the passed type or decode it from the die. */
176620f1 11622 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11623 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11624 if (type != NULL)
11625 SYMBOL_TYPE (sym) = type;
11626 else
e7c27a73 11627 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11628 attr = dwarf2_attr (die,
11629 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11630 cu);
c906108c
SS
11631 if (attr)
11632 {
11633 SYMBOL_LINE (sym) = DW_UNSND (attr);
11634 }
cb1df416 11635
edb3359d
DJ
11636 attr = dwarf2_attr (die,
11637 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11638 cu);
cb1df416
DJ
11639 if (attr)
11640 {
11641 int file_index = DW_UNSND (attr);
9a619af0 11642
cb1df416
DJ
11643 if (cu->line_header == NULL
11644 || file_index > cu->line_header->num_file_names)
11645 complaint (&symfile_complaints,
11646 _("file index out of range"));
1c3d648d 11647 else if (file_index > 0)
cb1df416
DJ
11648 {
11649 struct file_entry *fe;
9a619af0 11650
cb1df416
DJ
11651 fe = &cu->line_header->file_names[file_index - 1];
11652 SYMBOL_SYMTAB (sym) = fe->symtab;
11653 }
11654 }
11655
c906108c
SS
11656 switch (die->tag)
11657 {
11658 case DW_TAG_label:
e142c38c 11659 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11660 if (attr)
11661 {
11662 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11663 }
0f5238ed
TT
11664 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11665 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11666 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11667 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11668 break;
11669 case DW_TAG_subprogram:
11670 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11671 finish_block. */
11672 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11673 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11674 if ((attr2 && (DW_UNSND (attr2) != 0))
11675 || cu->language == language_ada)
c906108c 11676 {
2cfa0c8d
JB
11677 /* Subprograms marked external are stored as a global symbol.
11678 Ada subprograms, whether marked external or not, are always
11679 stored as a global symbol, because we want to be able to
11680 access them globally. For instance, we want to be able
11681 to break on a nested subprogram without having to
11682 specify the context. */
e37fd15a 11683 list_to_add = &global_symbols;
c906108c
SS
11684 }
11685 else
11686 {
e37fd15a 11687 list_to_add = cu->list_in_scope;
c906108c
SS
11688 }
11689 break;
edb3359d
DJ
11690 case DW_TAG_inlined_subroutine:
11691 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11692 finish_block. */
11693 SYMBOL_CLASS (sym) = LOC_BLOCK;
11694 SYMBOL_INLINED (sym) = 1;
11695 /* Do not add the symbol to any lists. It will be found via
11696 BLOCK_FUNCTION from the blockvector. */
11697 break;
34eaf542
TT
11698 case DW_TAG_template_value_param:
11699 suppress_add = 1;
11700 /* Fall through. */
72929c62 11701 case DW_TAG_constant:
c906108c 11702 case DW_TAG_variable:
254e6b9e 11703 case DW_TAG_member:
0963b4bd
MS
11704 /* Compilation with minimal debug info may result in
11705 variables with missing type entries. Change the
11706 misleading `void' type to something sensible. */
c906108c 11707 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11708 SYMBOL_TYPE (sym)
46bf5051 11709 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11710
e142c38c 11711 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11712 /* In the case of DW_TAG_member, we should only be called for
11713 static const members. */
11714 if (die->tag == DW_TAG_member)
11715 {
3863f96c
DE
11716 /* dwarf2_add_field uses die_is_declaration,
11717 so we do the same. */
254e6b9e
DE
11718 gdb_assert (die_is_declaration (die, cu));
11719 gdb_assert (attr);
11720 }
c906108c
SS
11721 if (attr)
11722 {
e7c27a73 11723 dwarf2_const_value (attr, sym, cu);
e142c38c 11724 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11725 if (!suppress_add)
34eaf542
TT
11726 {
11727 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11728 list_to_add = &global_symbols;
34eaf542 11729 else
e37fd15a 11730 list_to_add = cu->list_in_scope;
34eaf542 11731 }
c906108c
SS
11732 break;
11733 }
e142c38c 11734 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11735 if (attr)
11736 {
e7c27a73 11737 var_decode_location (attr, sym, cu);
e142c38c 11738 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11739 if (SYMBOL_CLASS (sym) == LOC_STATIC
11740 && SYMBOL_VALUE_ADDRESS (sym) == 0
11741 && !dwarf2_per_objfile->has_section_at_zero)
11742 {
11743 /* When a static variable is eliminated by the linker,
11744 the corresponding debug information is not stripped
11745 out, but the variable address is set to null;
11746 do not add such variables into symbol table. */
11747 }
11748 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11749 {
f55ee35c
JK
11750 /* Workaround gfortran PR debug/40040 - it uses
11751 DW_AT_location for variables in -fPIC libraries which may
11752 get overriden by other libraries/executable and get
11753 a different address. Resolve it by the minimal symbol
11754 which may come from inferior's executable using copy
11755 relocation. Make this workaround only for gfortran as for
11756 other compilers GDB cannot guess the minimal symbol
11757 Fortran mangling kind. */
11758 if (cu->language == language_fortran && die->parent
11759 && die->parent->tag == DW_TAG_module
11760 && cu->producer
11761 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11762 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11763
1c809c68
TT
11764 /* A variable with DW_AT_external is never static,
11765 but it may be block-scoped. */
11766 list_to_add = (cu->list_in_scope == &file_symbols
11767 ? &global_symbols : cu->list_in_scope);
1c809c68 11768 }
c906108c 11769 else
e37fd15a 11770 list_to_add = cu->list_in_scope;
c906108c
SS
11771 }
11772 else
11773 {
11774 /* We do not know the address of this symbol.
c5aa993b
JM
11775 If it is an external symbol and we have type information
11776 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11777 The address of the variable will then be determined from
11778 the minimal symbol table whenever the variable is
11779 referenced. */
e142c38c 11780 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11781 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11782 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11783 {
0fe7935b
DJ
11784 /* A variable with DW_AT_external is never static, but it
11785 may be block-scoped. */
11786 list_to_add = (cu->list_in_scope == &file_symbols
11787 ? &global_symbols : cu->list_in_scope);
11788
c906108c 11789 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11790 }
442ddf59
JK
11791 else if (!die_is_declaration (die, cu))
11792 {
11793 /* Use the default LOC_OPTIMIZED_OUT class. */
11794 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11795 if (!suppress_add)
11796 list_to_add = cu->list_in_scope;
442ddf59 11797 }
c906108c
SS
11798 }
11799 break;
11800 case DW_TAG_formal_parameter:
edb3359d
DJ
11801 /* If we are inside a function, mark this as an argument. If
11802 not, we might be looking at an argument to an inlined function
11803 when we do not have enough information to show inlined frames;
11804 pretend it's a local variable in that case so that the user can
11805 still see it. */
11806 if (context_stack_depth > 0
11807 && context_stack[context_stack_depth - 1].name != NULL)
11808 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11809 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11810 if (attr)
11811 {
e7c27a73 11812 var_decode_location (attr, sym, cu);
c906108c 11813 }
e142c38c 11814 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11815 if (attr)
11816 {
e7c27a73 11817 dwarf2_const_value (attr, sym, cu);
c906108c 11818 }
f346a30d 11819
e37fd15a 11820 list_to_add = cu->list_in_scope;
c906108c
SS
11821 break;
11822 case DW_TAG_unspecified_parameters:
11823 /* From varargs functions; gdb doesn't seem to have any
11824 interest in this information, so just ignore it for now.
11825 (FIXME?) */
11826 break;
34eaf542
TT
11827 case DW_TAG_template_type_param:
11828 suppress_add = 1;
11829 /* Fall through. */
c906108c 11830 case DW_TAG_class_type:
680b30c7 11831 case DW_TAG_interface_type:
c906108c
SS
11832 case DW_TAG_structure_type:
11833 case DW_TAG_union_type:
72019c9c 11834 case DW_TAG_set_type:
c906108c
SS
11835 case DW_TAG_enumeration_type:
11836 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11837 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11838
63d06c5c 11839 {
987504bb 11840 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11841 really ever be static objects: otherwise, if you try
11842 to, say, break of a class's method and you're in a file
11843 which doesn't mention that class, it won't work unless
11844 the check for all static symbols in lookup_symbol_aux
11845 saves you. See the OtherFileClass tests in
11846 gdb.c++/namespace.exp. */
11847
e37fd15a 11848 if (!suppress_add)
34eaf542 11849 {
34eaf542
TT
11850 list_to_add = (cu->list_in_scope == &file_symbols
11851 && (cu->language == language_cplus
11852 || cu->language == language_java)
11853 ? &global_symbols : cu->list_in_scope);
63d06c5c 11854
64382290
TT
11855 /* The semantics of C++ state that "struct foo {
11856 ... }" also defines a typedef for "foo". A Java
11857 class declaration also defines a typedef for the
11858 class. */
11859 if (cu->language == language_cplus
11860 || cu->language == language_java
11861 || cu->language == language_ada)
11862 {
11863 /* The symbol's name is already allocated along
11864 with this objfile, so we don't need to
11865 duplicate it for the type. */
11866 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11867 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11868 }
63d06c5c
DC
11869 }
11870 }
c906108c
SS
11871 break;
11872 case DW_TAG_typedef:
63d06c5c
DC
11873 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11874 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11875 list_to_add = cu->list_in_scope;
63d06c5c 11876 break;
c906108c 11877 case DW_TAG_base_type:
a02abb62 11878 case DW_TAG_subrange_type:
c906108c 11879 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11880 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11881 list_to_add = cu->list_in_scope;
c906108c
SS
11882 break;
11883 case DW_TAG_enumerator:
e142c38c 11884 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11885 if (attr)
11886 {
e7c27a73 11887 dwarf2_const_value (attr, sym, cu);
c906108c 11888 }
63d06c5c
DC
11889 {
11890 /* NOTE: carlton/2003-11-10: See comment above in the
11891 DW_TAG_class_type, etc. block. */
11892
e142c38c 11893 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11894 && (cu->language == language_cplus
11895 || cu->language == language_java)
e142c38c 11896 ? &global_symbols : cu->list_in_scope);
63d06c5c 11897 }
c906108c 11898 break;
5c4e30ca
DC
11899 case DW_TAG_namespace:
11900 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11901 list_to_add = &global_symbols;
5c4e30ca 11902 break;
c906108c
SS
11903 default:
11904 /* Not a tag we recognize. Hopefully we aren't processing
11905 trash data, but since we must specifically ignore things
11906 we don't recognize, there is nothing else we should do at
0963b4bd 11907 this point. */
e2e0b3e5 11908 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11909 dwarf_tag_name (die->tag));
c906108c
SS
11910 break;
11911 }
df8a16a1 11912
e37fd15a
SW
11913 if (suppress_add)
11914 {
11915 sym->hash_next = objfile->template_symbols;
11916 objfile->template_symbols = sym;
11917 list_to_add = NULL;
11918 }
11919
11920 if (list_to_add != NULL)
11921 add_symbol_to_list (sym, list_to_add);
11922
df8a16a1
DJ
11923 /* For the benefit of old versions of GCC, check for anonymous
11924 namespaces based on the demangled name. */
11925 if (!processing_has_namespace_info
94af9270 11926 && cu->language == language_cplus)
df8a16a1 11927 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
11928 }
11929 return (sym);
11930}
11931
34eaf542
TT
11932/* A wrapper for new_symbol_full that always allocates a new symbol. */
11933
11934static struct symbol *
11935new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11936{
11937 return new_symbol_full (die, type, cu, NULL);
11938}
11939
98bfdba5
PA
11940/* Given an attr with a DW_FORM_dataN value in host byte order,
11941 zero-extend it as appropriate for the symbol's type. The DWARF
11942 standard (v4) is not entirely clear about the meaning of using
11943 DW_FORM_dataN for a constant with a signed type, where the type is
11944 wider than the data. The conclusion of a discussion on the DWARF
11945 list was that this is unspecified. We choose to always zero-extend
11946 because that is the interpretation long in use by GCC. */
c906108c 11947
98bfdba5
PA
11948static gdb_byte *
11949dwarf2_const_value_data (struct attribute *attr, struct type *type,
11950 const char *name, struct obstack *obstack,
11951 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11952{
e7c27a73 11953 struct objfile *objfile = cu->objfile;
e17a4113
UW
11954 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11955 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11956 LONGEST l = DW_UNSND (attr);
11957
11958 if (bits < sizeof (*value) * 8)
11959 {
11960 l &= ((LONGEST) 1 << bits) - 1;
11961 *value = l;
11962 }
11963 else if (bits == sizeof (*value) * 8)
11964 *value = l;
11965 else
11966 {
11967 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11968 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11969 return bytes;
11970 }
11971
11972 return NULL;
11973}
11974
11975/* Read a constant value from an attribute. Either set *VALUE, or if
11976 the value does not fit in *VALUE, set *BYTES - either already
11977 allocated on the objfile obstack, or newly allocated on OBSTACK,
11978 or, set *BATON, if we translated the constant to a location
11979 expression. */
11980
11981static void
11982dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11983 const char *name, struct obstack *obstack,
11984 struct dwarf2_cu *cu,
11985 long *value, gdb_byte **bytes,
11986 struct dwarf2_locexpr_baton **baton)
11987{
11988 struct objfile *objfile = cu->objfile;
11989 struct comp_unit_head *cu_header = &cu->header;
c906108c 11990 struct dwarf_block *blk;
98bfdba5
PA
11991 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11992 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11993
11994 *value = 0;
11995 *bytes = NULL;
11996 *baton = NULL;
c906108c
SS
11997
11998 switch (attr->form)
11999 {
12000 case DW_FORM_addr:
ac56253d 12001 {
ac56253d
TT
12002 gdb_byte *data;
12003
98bfdba5
PA
12004 if (TYPE_LENGTH (type) != cu_header->addr_size)
12005 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 12006 cu_header->addr_size,
98bfdba5 12007 TYPE_LENGTH (type));
ac56253d
TT
12008 /* Symbols of this form are reasonably rare, so we just
12009 piggyback on the existing location code rather than writing
12010 a new implementation of symbol_computed_ops. */
98bfdba5
PA
12011 *baton = obstack_alloc (&objfile->objfile_obstack,
12012 sizeof (struct dwarf2_locexpr_baton));
12013 (*baton)->per_cu = cu->per_cu;
12014 gdb_assert ((*baton)->per_cu);
ac56253d 12015
98bfdba5
PA
12016 (*baton)->size = 2 + cu_header->addr_size;
12017 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12018 (*baton)->data = data;
ac56253d
TT
12019
12020 data[0] = DW_OP_addr;
12021 store_unsigned_integer (&data[1], cu_header->addr_size,
12022 byte_order, DW_ADDR (attr));
12023 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 12024 }
c906108c 12025 break;
4ac36638 12026 case DW_FORM_string:
93b5768b 12027 case DW_FORM_strp:
98bfdba5
PA
12028 /* DW_STRING is already allocated on the objfile obstack, point
12029 directly to it. */
12030 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 12031 break;
c906108c
SS
12032 case DW_FORM_block1:
12033 case DW_FORM_block2:
12034 case DW_FORM_block4:
12035 case DW_FORM_block:
2dc7f7b3 12036 case DW_FORM_exprloc:
c906108c 12037 blk = DW_BLOCK (attr);
98bfdba5
PA
12038 if (TYPE_LENGTH (type) != blk->size)
12039 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12040 TYPE_LENGTH (type));
12041 *bytes = blk->data;
c906108c 12042 break;
2df3850c
JM
12043
12044 /* The DW_AT_const_value attributes are supposed to carry the
12045 symbol's value "represented as it would be on the target
12046 architecture." By the time we get here, it's already been
12047 converted to host endianness, so we just need to sign- or
12048 zero-extend it as appropriate. */
12049 case DW_FORM_data1:
3e43a32a
MS
12050 *bytes = dwarf2_const_value_data (attr, type, name,
12051 obstack, cu, value, 8);
2df3850c 12052 break;
c906108c 12053 case DW_FORM_data2:
3e43a32a
MS
12054 *bytes = dwarf2_const_value_data (attr, type, name,
12055 obstack, cu, value, 16);
2df3850c 12056 break;
c906108c 12057 case DW_FORM_data4:
3e43a32a
MS
12058 *bytes = dwarf2_const_value_data (attr, type, name,
12059 obstack, cu, value, 32);
2df3850c 12060 break;
c906108c 12061 case DW_FORM_data8:
3e43a32a
MS
12062 *bytes = dwarf2_const_value_data (attr, type, name,
12063 obstack, cu, value, 64);
2df3850c
JM
12064 break;
12065
c906108c 12066 case DW_FORM_sdata:
98bfdba5 12067 *value = DW_SND (attr);
2df3850c
JM
12068 break;
12069
c906108c 12070 case DW_FORM_udata:
98bfdba5 12071 *value = DW_UNSND (attr);
c906108c 12072 break;
2df3850c 12073
c906108c 12074 default:
4d3c2250 12075 complaint (&symfile_complaints,
e2e0b3e5 12076 _("unsupported const value attribute form: '%s'"),
4d3c2250 12077 dwarf_form_name (attr->form));
98bfdba5 12078 *value = 0;
c906108c
SS
12079 break;
12080 }
12081}
12082
2df3850c 12083
98bfdba5
PA
12084/* Copy constant value from an attribute to a symbol. */
12085
2df3850c 12086static void
98bfdba5
PA
12087dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12088 struct dwarf2_cu *cu)
2df3850c 12089{
98bfdba5
PA
12090 struct objfile *objfile = cu->objfile;
12091 struct comp_unit_head *cu_header = &cu->header;
12092 long value;
12093 gdb_byte *bytes;
12094 struct dwarf2_locexpr_baton *baton;
2df3850c 12095
98bfdba5
PA
12096 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12097 SYMBOL_PRINT_NAME (sym),
12098 &objfile->objfile_obstack, cu,
12099 &value, &bytes, &baton);
2df3850c 12100
98bfdba5
PA
12101 if (baton != NULL)
12102 {
12103 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12104 SYMBOL_LOCATION_BATON (sym) = baton;
12105 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12106 }
12107 else if (bytes != NULL)
12108 {
12109 SYMBOL_VALUE_BYTES (sym) = bytes;
12110 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12111 }
12112 else
12113 {
12114 SYMBOL_VALUE (sym) = value;
12115 SYMBOL_CLASS (sym) = LOC_CONST;
12116 }
2df3850c
JM
12117}
12118
c906108c
SS
12119/* Return the type of the die in question using its DW_AT_type attribute. */
12120
12121static struct type *
e7c27a73 12122die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12123{
c906108c 12124 struct attribute *type_attr;
c906108c 12125
e142c38c 12126 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
12127 if (!type_attr)
12128 {
12129 /* A missing DW_AT_type represents a void type. */
46bf5051 12130 return objfile_type (cu->objfile)->builtin_void;
c906108c 12131 }
348e048f 12132
673bfd45 12133 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12134}
12135
b4ba55a1
JB
12136/* True iff CU's producer generates GNAT Ada auxiliary information
12137 that allows to find parallel types through that information instead
12138 of having to do expensive parallel lookups by type name. */
12139
12140static int
12141need_gnat_info (struct dwarf2_cu *cu)
12142{
12143 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12144 of GNAT produces this auxiliary information, without any indication
12145 that it is produced. Part of enhancing the FSF version of GNAT
12146 to produce that information will be to put in place an indicator
12147 that we can use in order to determine whether the descriptive type
12148 info is available or not. One suggestion that has been made is
12149 to use a new attribute, attached to the CU die. For now, assume
12150 that the descriptive type info is not available. */
12151 return 0;
12152}
12153
b4ba55a1
JB
12154/* Return the auxiliary type of the die in question using its
12155 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12156 attribute is not present. */
12157
12158static struct type *
12159die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12160{
b4ba55a1 12161 struct attribute *type_attr;
b4ba55a1
JB
12162
12163 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12164 if (!type_attr)
12165 return NULL;
12166
673bfd45 12167 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
12168}
12169
12170/* If DIE has a descriptive_type attribute, then set the TYPE's
12171 descriptive type accordingly. */
12172
12173static void
12174set_descriptive_type (struct type *type, struct die_info *die,
12175 struct dwarf2_cu *cu)
12176{
12177 struct type *descriptive_type = die_descriptive_type (die, cu);
12178
12179 if (descriptive_type)
12180 {
12181 ALLOCATE_GNAT_AUX_TYPE (type);
12182 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12183 }
12184}
12185
c906108c
SS
12186/* Return the containing type of the die in question using its
12187 DW_AT_containing_type attribute. */
12188
12189static struct type *
e7c27a73 12190die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12191{
c906108c 12192 struct attribute *type_attr;
c906108c 12193
e142c38c 12194 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
12195 if (!type_attr)
12196 error (_("Dwarf Error: Problem turning containing type into gdb type "
12197 "[in module %s]"), cu->objfile->name);
12198
673bfd45 12199 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12200}
12201
673bfd45
DE
12202/* Look up the type of DIE in CU using its type attribute ATTR.
12203 If there is no type substitute an error marker. */
12204
c906108c 12205static struct type *
673bfd45
DE
12206lookup_die_type (struct die_info *die, struct attribute *attr,
12207 struct dwarf2_cu *cu)
c906108c 12208{
f792889a
DJ
12209 struct type *this_type;
12210
673bfd45
DE
12211 /* First see if we have it cached. */
12212
12213 if (is_ref_attr (attr))
12214 {
12215 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12216
12217 this_type = get_die_type_at_offset (offset, cu->per_cu);
12218 }
55f1336d 12219 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
12220 {
12221 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12222 struct dwarf2_cu *sig_cu;
12223 unsigned int offset;
12224
12225 /* sig_type will be NULL if the signatured type is missing from
12226 the debug info. */
12227 if (sig_type == NULL)
12228 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12229 "at 0x%x [in module %s]"),
12230 die->offset, cu->objfile->name);
12231
8b70b953 12232 gdb_assert (sig_type->per_cu.debug_type_section);
b3c8eb43 12233 offset = sig_type->per_cu.offset + sig_type->type_offset;
673bfd45
DE
12234 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12235 }
12236 else
12237 {
12238 dump_die_for_error (die);
12239 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12240 dwarf_attr_name (attr->name), cu->objfile->name);
12241 }
12242
12243 /* If not cached we need to read it in. */
12244
12245 if (this_type == NULL)
12246 {
12247 struct die_info *type_die;
12248 struct dwarf2_cu *type_cu = cu;
12249
12250 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12251 /* If the type is cached, we should have found it above. */
12252 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12253 this_type = read_type_die_1 (type_die, type_cu);
12254 }
12255
12256 /* If we still don't have a type use an error marker. */
12257
12258 if (this_type == NULL)
c906108c 12259 {
b00fdb78
TT
12260 char *message, *saved;
12261
12262 /* read_type_die already issued a complaint. */
12263 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12264 cu->objfile->name,
12265 cu->header.offset,
12266 die->offset);
12267 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
12268 message, strlen (message));
12269 xfree (message);
12270
12271 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 12272 }
673bfd45 12273
f792889a 12274 return this_type;
c906108c
SS
12275}
12276
673bfd45
DE
12277/* Return the type in DIE, CU.
12278 Returns NULL for invalid types.
12279
12280 This first does a lookup in the appropriate type_hash table,
12281 and only reads the die in if necessary.
12282
12283 NOTE: This can be called when reading in partial or full symbols. */
12284
f792889a 12285static struct type *
e7c27a73 12286read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12287{
f792889a
DJ
12288 struct type *this_type;
12289
12290 this_type = get_die_type (die, cu);
12291 if (this_type)
12292 return this_type;
12293
673bfd45
DE
12294 return read_type_die_1 (die, cu);
12295}
12296
12297/* Read the type in DIE, CU.
12298 Returns NULL for invalid types. */
12299
12300static struct type *
12301read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12302{
12303 struct type *this_type = NULL;
12304
c906108c
SS
12305 switch (die->tag)
12306 {
12307 case DW_TAG_class_type:
680b30c7 12308 case DW_TAG_interface_type:
c906108c
SS
12309 case DW_TAG_structure_type:
12310 case DW_TAG_union_type:
f792889a 12311 this_type = read_structure_type (die, cu);
c906108c
SS
12312 break;
12313 case DW_TAG_enumeration_type:
f792889a 12314 this_type = read_enumeration_type (die, cu);
c906108c
SS
12315 break;
12316 case DW_TAG_subprogram:
12317 case DW_TAG_subroutine_type:
edb3359d 12318 case DW_TAG_inlined_subroutine:
f792889a 12319 this_type = read_subroutine_type (die, cu);
c906108c
SS
12320 break;
12321 case DW_TAG_array_type:
f792889a 12322 this_type = read_array_type (die, cu);
c906108c 12323 break;
72019c9c 12324 case DW_TAG_set_type:
f792889a 12325 this_type = read_set_type (die, cu);
72019c9c 12326 break;
c906108c 12327 case DW_TAG_pointer_type:
f792889a 12328 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12329 break;
12330 case DW_TAG_ptr_to_member_type:
f792889a 12331 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12332 break;
12333 case DW_TAG_reference_type:
f792889a 12334 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12335 break;
12336 case DW_TAG_const_type:
f792889a 12337 this_type = read_tag_const_type (die, cu);
c906108c
SS
12338 break;
12339 case DW_TAG_volatile_type:
f792889a 12340 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12341 break;
12342 case DW_TAG_string_type:
f792889a 12343 this_type = read_tag_string_type (die, cu);
c906108c
SS
12344 break;
12345 case DW_TAG_typedef:
f792889a 12346 this_type = read_typedef (die, cu);
c906108c 12347 break;
a02abb62 12348 case DW_TAG_subrange_type:
f792889a 12349 this_type = read_subrange_type (die, cu);
a02abb62 12350 break;
c906108c 12351 case DW_TAG_base_type:
f792889a 12352 this_type = read_base_type (die, cu);
c906108c 12353 break;
81a17f79 12354 case DW_TAG_unspecified_type:
f792889a 12355 this_type = read_unspecified_type (die, cu);
81a17f79 12356 break;
0114d602
DJ
12357 case DW_TAG_namespace:
12358 this_type = read_namespace_type (die, cu);
12359 break;
f55ee35c
JK
12360 case DW_TAG_module:
12361 this_type = read_module_type (die, cu);
12362 break;
c906108c 12363 default:
3e43a32a
MS
12364 complaint (&symfile_complaints,
12365 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12366 dwarf_tag_name (die->tag));
c906108c
SS
12367 break;
12368 }
63d06c5c 12369
f792889a 12370 return this_type;
63d06c5c
DC
12371}
12372
abc72ce4
DE
12373/* See if we can figure out if the class lives in a namespace. We do
12374 this by looking for a member function; its demangled name will
12375 contain namespace info, if there is any.
12376 Return the computed name or NULL.
12377 Space for the result is allocated on the objfile's obstack.
12378 This is the full-die version of guess_partial_die_structure_name.
12379 In this case we know DIE has no useful parent. */
12380
12381static char *
12382guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12383{
12384 struct die_info *spec_die;
12385 struct dwarf2_cu *spec_cu;
12386 struct die_info *child;
12387
12388 spec_cu = cu;
12389 spec_die = die_specification (die, &spec_cu);
12390 if (spec_die != NULL)
12391 {
12392 die = spec_die;
12393 cu = spec_cu;
12394 }
12395
12396 for (child = die->child;
12397 child != NULL;
12398 child = child->sibling)
12399 {
12400 if (child->tag == DW_TAG_subprogram)
12401 {
12402 struct attribute *attr;
12403
12404 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12405 if (attr == NULL)
12406 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12407 if (attr != NULL)
12408 {
12409 char *actual_name
12410 = language_class_name_from_physname (cu->language_defn,
12411 DW_STRING (attr));
12412 char *name = NULL;
12413
12414 if (actual_name != NULL)
12415 {
12416 char *die_name = dwarf2_name (die, cu);
12417
12418 if (die_name != NULL
12419 && strcmp (die_name, actual_name) != 0)
12420 {
12421 /* Strip off the class name from the full name.
12422 We want the prefix. */
12423 int die_name_len = strlen (die_name);
12424 int actual_name_len = strlen (actual_name);
12425
12426 /* Test for '::' as a sanity check. */
12427 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12428 && actual_name[actual_name_len
12429 - die_name_len - 1] == ':')
abc72ce4
DE
12430 name =
12431 obsavestring (actual_name,
12432 actual_name_len - die_name_len - 2,
12433 &cu->objfile->objfile_obstack);
12434 }
12435 }
12436 xfree (actual_name);
12437 return name;
12438 }
12439 }
12440 }
12441
12442 return NULL;
12443}
12444
96408a79
SA
12445/* GCC might emit a nameless typedef that has a linkage name. Determine the
12446 prefix part in such case. See
12447 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12448
12449static char *
12450anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12451{
12452 struct attribute *attr;
12453 char *base;
12454
12455 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12456 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12457 return NULL;
12458
12459 attr = dwarf2_attr (die, DW_AT_name, cu);
12460 if (attr != NULL && DW_STRING (attr) != NULL)
12461 return NULL;
12462
12463 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12464 if (attr == NULL)
12465 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12466 if (attr == NULL || DW_STRING (attr) == NULL)
12467 return NULL;
12468
12469 /* dwarf2_name had to be already called. */
12470 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12471
12472 /* Strip the base name, keep any leading namespaces/classes. */
12473 base = strrchr (DW_STRING (attr), ':');
12474 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12475 return "";
12476
12477 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12478 &cu->objfile->objfile_obstack);
12479}
12480
fdde2d81 12481/* Return the name of the namespace/class that DIE is defined within,
0114d602 12482 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12483
0114d602
DJ
12484 For example, if we're within the method foo() in the following
12485 code:
12486
12487 namespace N {
12488 class C {
12489 void foo () {
12490 }
12491 };
12492 }
12493
12494 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
12495
12496static char *
e142c38c 12497determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12498{
0114d602
DJ
12499 struct die_info *parent, *spec_die;
12500 struct dwarf2_cu *spec_cu;
12501 struct type *parent_type;
96408a79 12502 char *retval;
63d06c5c 12503
f55ee35c
JK
12504 if (cu->language != language_cplus && cu->language != language_java
12505 && cu->language != language_fortran)
0114d602
DJ
12506 return "";
12507
96408a79
SA
12508 retval = anonymous_struct_prefix (die, cu);
12509 if (retval)
12510 return retval;
12511
0114d602
DJ
12512 /* We have to be careful in the presence of DW_AT_specification.
12513 For example, with GCC 3.4, given the code
12514
12515 namespace N {
12516 void foo() {
12517 // Definition of N::foo.
12518 }
12519 }
12520
12521 then we'll have a tree of DIEs like this:
12522
12523 1: DW_TAG_compile_unit
12524 2: DW_TAG_namespace // N
12525 3: DW_TAG_subprogram // declaration of N::foo
12526 4: DW_TAG_subprogram // definition of N::foo
12527 DW_AT_specification // refers to die #3
12528
12529 Thus, when processing die #4, we have to pretend that we're in
12530 the context of its DW_AT_specification, namely the contex of die
12531 #3. */
12532 spec_cu = cu;
12533 spec_die = die_specification (die, &spec_cu);
12534 if (spec_die == NULL)
12535 parent = die->parent;
12536 else
63d06c5c 12537 {
0114d602
DJ
12538 parent = spec_die->parent;
12539 cu = spec_cu;
63d06c5c 12540 }
0114d602
DJ
12541
12542 if (parent == NULL)
12543 return "";
98bfdba5
PA
12544 else if (parent->building_fullname)
12545 {
12546 const char *name;
12547 const char *parent_name;
12548
12549 /* It has been seen on RealView 2.2 built binaries,
12550 DW_TAG_template_type_param types actually _defined_ as
12551 children of the parent class:
12552
12553 enum E {};
12554 template class <class Enum> Class{};
12555 Class<enum E> class_e;
12556
12557 1: DW_TAG_class_type (Class)
12558 2: DW_TAG_enumeration_type (E)
12559 3: DW_TAG_enumerator (enum1:0)
12560 3: DW_TAG_enumerator (enum2:1)
12561 ...
12562 2: DW_TAG_template_type_param
12563 DW_AT_type DW_FORM_ref_udata (E)
12564
12565 Besides being broken debug info, it can put GDB into an
12566 infinite loop. Consider:
12567
12568 When we're building the full name for Class<E>, we'll start
12569 at Class, and go look over its template type parameters,
12570 finding E. We'll then try to build the full name of E, and
12571 reach here. We're now trying to build the full name of E,
12572 and look over the parent DIE for containing scope. In the
12573 broken case, if we followed the parent DIE of E, we'd again
12574 find Class, and once again go look at its template type
12575 arguments, etc., etc. Simply don't consider such parent die
12576 as source-level parent of this die (it can't be, the language
12577 doesn't allow it), and break the loop here. */
12578 name = dwarf2_name (die, cu);
12579 parent_name = dwarf2_name (parent, cu);
12580 complaint (&symfile_complaints,
12581 _("template param type '%s' defined within parent '%s'"),
12582 name ? name : "<unknown>",
12583 parent_name ? parent_name : "<unknown>");
12584 return "";
12585 }
63d06c5c 12586 else
0114d602
DJ
12587 switch (parent->tag)
12588 {
63d06c5c 12589 case DW_TAG_namespace:
0114d602 12590 parent_type = read_type_die (parent, cu);
acebe513
UW
12591 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12592 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12593 Work around this problem here. */
12594 if (cu->language == language_cplus
12595 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12596 return "";
0114d602
DJ
12597 /* We give a name to even anonymous namespaces. */
12598 return TYPE_TAG_NAME (parent_type);
63d06c5c 12599 case DW_TAG_class_type:
680b30c7 12600 case DW_TAG_interface_type:
63d06c5c 12601 case DW_TAG_structure_type:
0114d602 12602 case DW_TAG_union_type:
f55ee35c 12603 case DW_TAG_module:
0114d602
DJ
12604 parent_type = read_type_die (parent, cu);
12605 if (TYPE_TAG_NAME (parent_type) != NULL)
12606 return TYPE_TAG_NAME (parent_type);
12607 else
12608 /* An anonymous structure is only allowed non-static data
12609 members; no typedefs, no member functions, et cetera.
12610 So it does not need a prefix. */
12611 return "";
abc72ce4
DE
12612 case DW_TAG_compile_unit:
12613 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12614 if (cu->language == language_cplus
8b70b953 12615 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12616 && die->child != NULL
12617 && (die->tag == DW_TAG_class_type
12618 || die->tag == DW_TAG_structure_type
12619 || die->tag == DW_TAG_union_type))
12620 {
12621 char *name = guess_full_die_structure_name (die, cu);
12622 if (name != NULL)
12623 return name;
12624 }
12625 return "";
63d06c5c 12626 default:
8176b9b8 12627 return determine_prefix (parent, cu);
63d06c5c 12628 }
63d06c5c
DC
12629}
12630
3e43a32a
MS
12631/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12632 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12633 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12634 an obconcat, otherwise allocate storage for the result. The CU argument is
12635 used to determine the language and hence, the appropriate separator. */
987504bb 12636
f55ee35c 12637#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12638
12639static char *
f55ee35c
JK
12640typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12641 int physname, struct dwarf2_cu *cu)
63d06c5c 12642{
f55ee35c 12643 const char *lead = "";
5c315b68 12644 const char *sep;
63d06c5c 12645
3e43a32a
MS
12646 if (suffix == NULL || suffix[0] == '\0'
12647 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12648 sep = "";
12649 else if (cu->language == language_java)
12650 sep = ".";
f55ee35c
JK
12651 else if (cu->language == language_fortran && physname)
12652 {
12653 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12654 DW_AT_MIPS_linkage_name is preferred and used instead. */
12655
12656 lead = "__";
12657 sep = "_MOD_";
12658 }
987504bb
JJ
12659 else
12660 sep = "::";
63d06c5c 12661
6dd47d34
DE
12662 if (prefix == NULL)
12663 prefix = "";
12664 if (suffix == NULL)
12665 suffix = "";
12666
987504bb
JJ
12667 if (obs == NULL)
12668 {
3e43a32a
MS
12669 char *retval
12670 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12671
f55ee35c
JK
12672 strcpy (retval, lead);
12673 strcat (retval, prefix);
6dd47d34
DE
12674 strcat (retval, sep);
12675 strcat (retval, suffix);
63d06c5c
DC
12676 return retval;
12677 }
987504bb
JJ
12678 else
12679 {
12680 /* We have an obstack. */
f55ee35c 12681 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12682 }
63d06c5c
DC
12683}
12684
c906108c
SS
12685/* Return sibling of die, NULL if no sibling. */
12686
f9aca02d 12687static struct die_info *
fba45db2 12688sibling_die (struct die_info *die)
c906108c 12689{
639d11d3 12690 return die->sibling;
c906108c
SS
12691}
12692
71c25dea
TT
12693/* Get name of a die, return NULL if not found. */
12694
12695static char *
12696dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12697 struct obstack *obstack)
12698{
12699 if (name && cu->language == language_cplus)
12700 {
12701 char *canon_name = cp_canonicalize_string (name);
12702
12703 if (canon_name != NULL)
12704 {
12705 if (strcmp (canon_name, name) != 0)
12706 name = obsavestring (canon_name, strlen (canon_name),
12707 obstack);
12708 xfree (canon_name);
12709 }
12710 }
12711
12712 return name;
c906108c
SS
12713}
12714
9219021c
DC
12715/* Get name of a die, return NULL if not found. */
12716
12717static char *
e142c38c 12718dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12719{
12720 struct attribute *attr;
12721
e142c38c 12722 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12723 if ((!attr || !DW_STRING (attr))
12724 && die->tag != DW_TAG_class_type
12725 && die->tag != DW_TAG_interface_type
12726 && die->tag != DW_TAG_structure_type
12727 && die->tag != DW_TAG_union_type)
71c25dea
TT
12728 return NULL;
12729
12730 switch (die->tag)
12731 {
12732 case DW_TAG_compile_unit:
12733 /* Compilation units have a DW_AT_name that is a filename, not
12734 a source language identifier. */
12735 case DW_TAG_enumeration_type:
12736 case DW_TAG_enumerator:
12737 /* These tags always have simple identifiers already; no need
12738 to canonicalize them. */
12739 return DW_STRING (attr);
907af001 12740
418835cc
KS
12741 case DW_TAG_subprogram:
12742 /* Java constructors will all be named "<init>", so return
12743 the class name when we see this special case. */
12744 if (cu->language == language_java
12745 && DW_STRING (attr) != NULL
12746 && strcmp (DW_STRING (attr), "<init>") == 0)
12747 {
12748 struct dwarf2_cu *spec_cu = cu;
12749 struct die_info *spec_die;
12750
12751 /* GCJ will output '<init>' for Java constructor names.
12752 For this special case, return the name of the parent class. */
12753
12754 /* GCJ may output suprogram DIEs with AT_specification set.
12755 If so, use the name of the specified DIE. */
12756 spec_die = die_specification (die, &spec_cu);
12757 if (spec_die != NULL)
12758 return dwarf2_name (spec_die, spec_cu);
12759
12760 do
12761 {
12762 die = die->parent;
12763 if (die->tag == DW_TAG_class_type)
12764 return dwarf2_name (die, cu);
12765 }
12766 while (die->tag != DW_TAG_compile_unit);
12767 }
907af001
UW
12768 break;
12769
12770 case DW_TAG_class_type:
12771 case DW_TAG_interface_type:
12772 case DW_TAG_structure_type:
12773 case DW_TAG_union_type:
12774 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12775 structures or unions. These were of the form "._%d" in GCC 4.1,
12776 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12777 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12778 if (attr && DW_STRING (attr)
12779 && (strncmp (DW_STRING (attr), "._", 2) == 0
12780 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12781 return NULL;
53832f31
TT
12782
12783 /* GCC might emit a nameless typedef that has a linkage name. See
12784 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12785 if (!attr || DW_STRING (attr) == NULL)
12786 {
df5c6c50 12787 char *demangled = NULL;
53832f31
TT
12788
12789 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12790 if (attr == NULL)
12791 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12792
12793 if (attr == NULL || DW_STRING (attr) == NULL)
12794 return NULL;
12795
df5c6c50
JK
12796 /* Avoid demangling DW_STRING (attr) the second time on a second
12797 call for the same DIE. */
12798 if (!DW_STRING_IS_CANONICAL (attr))
12799 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12800
12801 if (demangled)
12802 {
96408a79
SA
12803 char *base;
12804
53832f31 12805 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
12806 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12807 &cu->objfile->objfile_obstack);
53832f31
TT
12808 DW_STRING_IS_CANONICAL (attr) = 1;
12809 xfree (demangled);
96408a79
SA
12810
12811 /* Strip any leading namespaces/classes, keep only the base name.
12812 DW_AT_name for named DIEs does not contain the prefixes. */
12813 base = strrchr (DW_STRING (attr), ':');
12814 if (base && base > DW_STRING (attr) && base[-1] == ':')
12815 return &base[1];
12816 else
12817 return DW_STRING (attr);
53832f31
TT
12818 }
12819 }
907af001
UW
12820 break;
12821
71c25dea 12822 default:
907af001
UW
12823 break;
12824 }
12825
12826 if (!DW_STRING_IS_CANONICAL (attr))
12827 {
12828 DW_STRING (attr)
12829 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12830 &cu->objfile->objfile_obstack);
12831 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12832 }
907af001 12833 return DW_STRING (attr);
9219021c
DC
12834}
12835
12836/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12837 is none. *EXT_CU is the CU containing DIE on input, and the CU
12838 containing the return value on output. */
9219021c
DC
12839
12840static struct die_info *
f2f0e013 12841dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12842{
12843 struct attribute *attr;
9219021c 12844
f2f0e013 12845 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12846 if (attr == NULL)
12847 return NULL;
12848
f2f0e013 12849 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12850}
12851
c906108c
SS
12852/* Convert a DIE tag into its string name. */
12853
12854static char *
aa1ee363 12855dwarf_tag_name (unsigned tag)
c906108c
SS
12856{
12857 switch (tag)
12858 {
12859 case DW_TAG_padding:
12860 return "DW_TAG_padding";
12861 case DW_TAG_array_type:
12862 return "DW_TAG_array_type";
12863 case DW_TAG_class_type:
12864 return "DW_TAG_class_type";
12865 case DW_TAG_entry_point:
12866 return "DW_TAG_entry_point";
12867 case DW_TAG_enumeration_type:
12868 return "DW_TAG_enumeration_type";
12869 case DW_TAG_formal_parameter:
12870 return "DW_TAG_formal_parameter";
12871 case DW_TAG_imported_declaration:
12872 return "DW_TAG_imported_declaration";
12873 case DW_TAG_label:
12874 return "DW_TAG_label";
12875 case DW_TAG_lexical_block:
12876 return "DW_TAG_lexical_block";
12877 case DW_TAG_member:
12878 return "DW_TAG_member";
12879 case DW_TAG_pointer_type:
12880 return "DW_TAG_pointer_type";
12881 case DW_TAG_reference_type:
12882 return "DW_TAG_reference_type";
12883 case DW_TAG_compile_unit:
12884 return "DW_TAG_compile_unit";
12885 case DW_TAG_string_type:
12886 return "DW_TAG_string_type";
12887 case DW_TAG_structure_type:
12888 return "DW_TAG_structure_type";
12889 case DW_TAG_subroutine_type:
12890 return "DW_TAG_subroutine_type";
12891 case DW_TAG_typedef:
12892 return "DW_TAG_typedef";
12893 case DW_TAG_union_type:
12894 return "DW_TAG_union_type";
12895 case DW_TAG_unspecified_parameters:
12896 return "DW_TAG_unspecified_parameters";
12897 case DW_TAG_variant:
12898 return "DW_TAG_variant";
12899 case DW_TAG_common_block:
12900 return "DW_TAG_common_block";
12901 case DW_TAG_common_inclusion:
12902 return "DW_TAG_common_inclusion";
12903 case DW_TAG_inheritance:
12904 return "DW_TAG_inheritance";
12905 case DW_TAG_inlined_subroutine:
12906 return "DW_TAG_inlined_subroutine";
12907 case DW_TAG_module:
12908 return "DW_TAG_module";
12909 case DW_TAG_ptr_to_member_type:
12910 return "DW_TAG_ptr_to_member_type";
12911 case DW_TAG_set_type:
12912 return "DW_TAG_set_type";
12913 case DW_TAG_subrange_type:
12914 return "DW_TAG_subrange_type";
12915 case DW_TAG_with_stmt:
12916 return "DW_TAG_with_stmt";
12917 case DW_TAG_access_declaration:
12918 return "DW_TAG_access_declaration";
12919 case DW_TAG_base_type:
12920 return "DW_TAG_base_type";
12921 case DW_TAG_catch_block:
12922 return "DW_TAG_catch_block";
12923 case DW_TAG_const_type:
12924 return "DW_TAG_const_type";
12925 case DW_TAG_constant:
12926 return "DW_TAG_constant";
12927 case DW_TAG_enumerator:
12928 return "DW_TAG_enumerator";
12929 case DW_TAG_file_type:
12930 return "DW_TAG_file_type";
12931 case DW_TAG_friend:
12932 return "DW_TAG_friend";
12933 case DW_TAG_namelist:
12934 return "DW_TAG_namelist";
12935 case DW_TAG_namelist_item:
12936 return "DW_TAG_namelist_item";
12937 case DW_TAG_packed_type:
12938 return "DW_TAG_packed_type";
12939 case DW_TAG_subprogram:
12940 return "DW_TAG_subprogram";
12941 case DW_TAG_template_type_param:
12942 return "DW_TAG_template_type_param";
12943 case DW_TAG_template_value_param:
12944 return "DW_TAG_template_value_param";
12945 case DW_TAG_thrown_type:
12946 return "DW_TAG_thrown_type";
12947 case DW_TAG_try_block:
12948 return "DW_TAG_try_block";
12949 case DW_TAG_variant_part:
12950 return "DW_TAG_variant_part";
12951 case DW_TAG_variable:
12952 return "DW_TAG_variable";
12953 case DW_TAG_volatile_type:
12954 return "DW_TAG_volatile_type";
d9fa45fe
DC
12955 case DW_TAG_dwarf_procedure:
12956 return "DW_TAG_dwarf_procedure";
12957 case DW_TAG_restrict_type:
12958 return "DW_TAG_restrict_type";
12959 case DW_TAG_interface_type:
12960 return "DW_TAG_interface_type";
12961 case DW_TAG_namespace:
12962 return "DW_TAG_namespace";
12963 case DW_TAG_imported_module:
12964 return "DW_TAG_imported_module";
12965 case DW_TAG_unspecified_type:
12966 return "DW_TAG_unspecified_type";
12967 case DW_TAG_partial_unit:
12968 return "DW_TAG_partial_unit";
12969 case DW_TAG_imported_unit:
12970 return "DW_TAG_imported_unit";
b7619582
GF
12971 case DW_TAG_condition:
12972 return "DW_TAG_condition";
12973 case DW_TAG_shared_type:
12974 return "DW_TAG_shared_type";
348e048f
DE
12975 case DW_TAG_type_unit:
12976 return "DW_TAG_type_unit";
c906108c
SS
12977 case DW_TAG_MIPS_loop:
12978 return "DW_TAG_MIPS_loop";
b7619582
GF
12979 case DW_TAG_HP_array_descriptor:
12980 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12981 case DW_TAG_format_label:
12982 return "DW_TAG_format_label";
12983 case DW_TAG_function_template:
12984 return "DW_TAG_function_template";
12985 case DW_TAG_class_template:
12986 return "DW_TAG_class_template";
b7619582
GF
12987 case DW_TAG_GNU_BINCL:
12988 return "DW_TAG_GNU_BINCL";
12989 case DW_TAG_GNU_EINCL:
12990 return "DW_TAG_GNU_EINCL";
12991 case DW_TAG_upc_shared_type:
12992 return "DW_TAG_upc_shared_type";
12993 case DW_TAG_upc_strict_type:
12994 return "DW_TAG_upc_strict_type";
12995 case DW_TAG_upc_relaxed_type:
12996 return "DW_TAG_upc_relaxed_type";
12997 case DW_TAG_PGI_kanji_type:
12998 return "DW_TAG_PGI_kanji_type";
12999 case DW_TAG_PGI_interface_block:
13000 return "DW_TAG_PGI_interface_block";
96408a79
SA
13001 case DW_TAG_GNU_call_site:
13002 return "DW_TAG_GNU_call_site";
c906108c
SS
13003 default:
13004 return "DW_TAG_<unknown>";
13005 }
13006}
13007
13008/* Convert a DWARF attribute code into its string name. */
13009
13010static char *
aa1ee363 13011dwarf_attr_name (unsigned attr)
c906108c
SS
13012{
13013 switch (attr)
13014 {
13015 case DW_AT_sibling:
13016 return "DW_AT_sibling";
13017 case DW_AT_location:
13018 return "DW_AT_location";
13019 case DW_AT_name:
13020 return "DW_AT_name";
13021 case DW_AT_ordering:
13022 return "DW_AT_ordering";
13023 case DW_AT_subscr_data:
13024 return "DW_AT_subscr_data";
13025 case DW_AT_byte_size:
13026 return "DW_AT_byte_size";
13027 case DW_AT_bit_offset:
13028 return "DW_AT_bit_offset";
13029 case DW_AT_bit_size:
13030 return "DW_AT_bit_size";
13031 case DW_AT_element_list:
13032 return "DW_AT_element_list";
13033 case DW_AT_stmt_list:
13034 return "DW_AT_stmt_list";
13035 case DW_AT_low_pc:
13036 return "DW_AT_low_pc";
13037 case DW_AT_high_pc:
13038 return "DW_AT_high_pc";
13039 case DW_AT_language:
13040 return "DW_AT_language";
13041 case DW_AT_member:
13042 return "DW_AT_member";
13043 case DW_AT_discr:
13044 return "DW_AT_discr";
13045 case DW_AT_discr_value:
13046 return "DW_AT_discr_value";
13047 case DW_AT_visibility:
13048 return "DW_AT_visibility";
13049 case DW_AT_import:
13050 return "DW_AT_import";
13051 case DW_AT_string_length:
13052 return "DW_AT_string_length";
13053 case DW_AT_common_reference:
13054 return "DW_AT_common_reference";
13055 case DW_AT_comp_dir:
13056 return "DW_AT_comp_dir";
13057 case DW_AT_const_value:
13058 return "DW_AT_const_value";
13059 case DW_AT_containing_type:
13060 return "DW_AT_containing_type";
13061 case DW_AT_default_value:
13062 return "DW_AT_default_value";
13063 case DW_AT_inline:
13064 return "DW_AT_inline";
13065 case DW_AT_is_optional:
13066 return "DW_AT_is_optional";
13067 case DW_AT_lower_bound:
13068 return "DW_AT_lower_bound";
13069 case DW_AT_producer:
13070 return "DW_AT_producer";
13071 case DW_AT_prototyped:
13072 return "DW_AT_prototyped";
13073 case DW_AT_return_addr:
13074 return "DW_AT_return_addr";
13075 case DW_AT_start_scope:
13076 return "DW_AT_start_scope";
09fa0d7c
JK
13077 case DW_AT_bit_stride:
13078 return "DW_AT_bit_stride";
c906108c
SS
13079 case DW_AT_upper_bound:
13080 return "DW_AT_upper_bound";
13081 case DW_AT_abstract_origin:
13082 return "DW_AT_abstract_origin";
13083 case DW_AT_accessibility:
13084 return "DW_AT_accessibility";
13085 case DW_AT_address_class:
13086 return "DW_AT_address_class";
13087 case DW_AT_artificial:
13088 return "DW_AT_artificial";
13089 case DW_AT_base_types:
13090 return "DW_AT_base_types";
13091 case DW_AT_calling_convention:
13092 return "DW_AT_calling_convention";
13093 case DW_AT_count:
13094 return "DW_AT_count";
13095 case DW_AT_data_member_location:
13096 return "DW_AT_data_member_location";
13097 case DW_AT_decl_column:
13098 return "DW_AT_decl_column";
13099 case DW_AT_decl_file:
13100 return "DW_AT_decl_file";
13101 case DW_AT_decl_line:
13102 return "DW_AT_decl_line";
13103 case DW_AT_declaration:
13104 return "DW_AT_declaration";
13105 case DW_AT_discr_list:
13106 return "DW_AT_discr_list";
13107 case DW_AT_encoding:
13108 return "DW_AT_encoding";
13109 case DW_AT_external:
13110 return "DW_AT_external";
13111 case DW_AT_frame_base:
13112 return "DW_AT_frame_base";
13113 case DW_AT_friend:
13114 return "DW_AT_friend";
13115 case DW_AT_identifier_case:
13116 return "DW_AT_identifier_case";
13117 case DW_AT_macro_info:
13118 return "DW_AT_macro_info";
13119 case DW_AT_namelist_items:
13120 return "DW_AT_namelist_items";
13121 case DW_AT_priority:
13122 return "DW_AT_priority";
13123 case DW_AT_segment:
13124 return "DW_AT_segment";
13125 case DW_AT_specification:
13126 return "DW_AT_specification";
13127 case DW_AT_static_link:
13128 return "DW_AT_static_link";
13129 case DW_AT_type:
13130 return "DW_AT_type";
13131 case DW_AT_use_location:
13132 return "DW_AT_use_location";
13133 case DW_AT_variable_parameter:
13134 return "DW_AT_variable_parameter";
13135 case DW_AT_virtuality:
13136 return "DW_AT_virtuality";
13137 case DW_AT_vtable_elem_location:
13138 return "DW_AT_vtable_elem_location";
b7619582 13139 /* DWARF 3 values. */
d9fa45fe
DC
13140 case DW_AT_allocated:
13141 return "DW_AT_allocated";
13142 case DW_AT_associated:
13143 return "DW_AT_associated";
13144 case DW_AT_data_location:
13145 return "DW_AT_data_location";
09fa0d7c
JK
13146 case DW_AT_byte_stride:
13147 return "DW_AT_byte_stride";
d9fa45fe
DC
13148 case DW_AT_entry_pc:
13149 return "DW_AT_entry_pc";
13150 case DW_AT_use_UTF8:
13151 return "DW_AT_use_UTF8";
13152 case DW_AT_extension:
13153 return "DW_AT_extension";
13154 case DW_AT_ranges:
13155 return "DW_AT_ranges";
13156 case DW_AT_trampoline:
13157 return "DW_AT_trampoline";
13158 case DW_AT_call_column:
13159 return "DW_AT_call_column";
13160 case DW_AT_call_file:
13161 return "DW_AT_call_file";
13162 case DW_AT_call_line:
13163 return "DW_AT_call_line";
b7619582
GF
13164 case DW_AT_description:
13165 return "DW_AT_description";
13166 case DW_AT_binary_scale:
13167 return "DW_AT_binary_scale";
13168 case DW_AT_decimal_scale:
13169 return "DW_AT_decimal_scale";
13170 case DW_AT_small:
13171 return "DW_AT_small";
13172 case DW_AT_decimal_sign:
13173 return "DW_AT_decimal_sign";
13174 case DW_AT_digit_count:
13175 return "DW_AT_digit_count";
13176 case DW_AT_picture_string:
13177 return "DW_AT_picture_string";
13178 case DW_AT_mutable:
13179 return "DW_AT_mutable";
13180 case DW_AT_threads_scaled:
13181 return "DW_AT_threads_scaled";
13182 case DW_AT_explicit:
13183 return "DW_AT_explicit";
13184 case DW_AT_object_pointer:
13185 return "DW_AT_object_pointer";
13186 case DW_AT_endianity:
13187 return "DW_AT_endianity";
13188 case DW_AT_elemental:
13189 return "DW_AT_elemental";
13190 case DW_AT_pure:
13191 return "DW_AT_pure";
13192 case DW_AT_recursive:
13193 return "DW_AT_recursive";
348e048f
DE
13194 /* DWARF 4 values. */
13195 case DW_AT_signature:
13196 return "DW_AT_signature";
31ef98ae
TT
13197 case DW_AT_linkage_name:
13198 return "DW_AT_linkage_name";
b7619582 13199 /* SGI/MIPS extensions. */
c764a876 13200#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
13201 case DW_AT_MIPS_fde:
13202 return "DW_AT_MIPS_fde";
c764a876 13203#endif
c906108c
SS
13204 case DW_AT_MIPS_loop_begin:
13205 return "DW_AT_MIPS_loop_begin";
13206 case DW_AT_MIPS_tail_loop_begin:
13207 return "DW_AT_MIPS_tail_loop_begin";
13208 case DW_AT_MIPS_epilog_begin:
13209 return "DW_AT_MIPS_epilog_begin";
13210 case DW_AT_MIPS_loop_unroll_factor:
13211 return "DW_AT_MIPS_loop_unroll_factor";
13212 case DW_AT_MIPS_software_pipeline_depth:
13213 return "DW_AT_MIPS_software_pipeline_depth";
13214 case DW_AT_MIPS_linkage_name:
13215 return "DW_AT_MIPS_linkage_name";
b7619582
GF
13216 case DW_AT_MIPS_stride:
13217 return "DW_AT_MIPS_stride";
13218 case DW_AT_MIPS_abstract_name:
13219 return "DW_AT_MIPS_abstract_name";
13220 case DW_AT_MIPS_clone_origin:
13221 return "DW_AT_MIPS_clone_origin";
13222 case DW_AT_MIPS_has_inlines:
13223 return "DW_AT_MIPS_has_inlines";
b7619582 13224 /* HP extensions. */
c764a876 13225#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
13226 case DW_AT_HP_block_index:
13227 return "DW_AT_HP_block_index";
c764a876 13228#endif
b7619582
GF
13229 case DW_AT_HP_unmodifiable:
13230 return "DW_AT_HP_unmodifiable";
13231 case DW_AT_HP_actuals_stmt_list:
13232 return "DW_AT_HP_actuals_stmt_list";
13233 case DW_AT_HP_proc_per_section:
13234 return "DW_AT_HP_proc_per_section";
13235 case DW_AT_HP_raw_data_ptr:
13236 return "DW_AT_HP_raw_data_ptr";
13237 case DW_AT_HP_pass_by_reference:
13238 return "DW_AT_HP_pass_by_reference";
13239 case DW_AT_HP_opt_level:
13240 return "DW_AT_HP_opt_level";
13241 case DW_AT_HP_prof_version_id:
13242 return "DW_AT_HP_prof_version_id";
13243 case DW_AT_HP_opt_flags:
13244 return "DW_AT_HP_opt_flags";
13245 case DW_AT_HP_cold_region_low_pc:
13246 return "DW_AT_HP_cold_region_low_pc";
13247 case DW_AT_HP_cold_region_high_pc:
13248 return "DW_AT_HP_cold_region_high_pc";
13249 case DW_AT_HP_all_variables_modifiable:
13250 return "DW_AT_HP_all_variables_modifiable";
13251 case DW_AT_HP_linkage_name:
13252 return "DW_AT_HP_linkage_name";
13253 case DW_AT_HP_prof_flags:
13254 return "DW_AT_HP_prof_flags";
13255 /* GNU extensions. */
c906108c
SS
13256 case DW_AT_sf_names:
13257 return "DW_AT_sf_names";
13258 case DW_AT_src_info:
13259 return "DW_AT_src_info";
13260 case DW_AT_mac_info:
13261 return "DW_AT_mac_info";
13262 case DW_AT_src_coords:
13263 return "DW_AT_src_coords";
13264 case DW_AT_body_begin:
13265 return "DW_AT_body_begin";
13266 case DW_AT_body_end:
13267 return "DW_AT_body_end";
f5f8a009
EZ
13268 case DW_AT_GNU_vector:
13269 return "DW_AT_GNU_vector";
2de00c64
DE
13270 case DW_AT_GNU_odr_signature:
13271 return "DW_AT_GNU_odr_signature";
b7619582
GF
13272 /* VMS extensions. */
13273 case DW_AT_VMS_rtnbeg_pd_address:
13274 return "DW_AT_VMS_rtnbeg_pd_address";
13275 /* UPC extension. */
13276 case DW_AT_upc_threads_scaled:
13277 return "DW_AT_upc_threads_scaled";
13278 /* PGI (STMicroelectronics) extensions. */
13279 case DW_AT_PGI_lbase:
13280 return "DW_AT_PGI_lbase";
13281 case DW_AT_PGI_soffset:
13282 return "DW_AT_PGI_soffset";
13283 case DW_AT_PGI_lstride:
13284 return "DW_AT_PGI_lstride";
c906108c
SS
13285 default:
13286 return "DW_AT_<unknown>";
13287 }
13288}
13289
13290/* Convert a DWARF value form code into its string name. */
13291
13292static char *
aa1ee363 13293dwarf_form_name (unsigned form)
c906108c
SS
13294{
13295 switch (form)
13296 {
13297 case DW_FORM_addr:
13298 return "DW_FORM_addr";
13299 case DW_FORM_block2:
13300 return "DW_FORM_block2";
13301 case DW_FORM_block4:
13302 return "DW_FORM_block4";
13303 case DW_FORM_data2:
13304 return "DW_FORM_data2";
13305 case DW_FORM_data4:
13306 return "DW_FORM_data4";
13307 case DW_FORM_data8:
13308 return "DW_FORM_data8";
13309 case DW_FORM_string:
13310 return "DW_FORM_string";
13311 case DW_FORM_block:
13312 return "DW_FORM_block";
13313 case DW_FORM_block1:
13314 return "DW_FORM_block1";
13315 case DW_FORM_data1:
13316 return "DW_FORM_data1";
13317 case DW_FORM_flag:
13318 return "DW_FORM_flag";
13319 case DW_FORM_sdata:
13320 return "DW_FORM_sdata";
13321 case DW_FORM_strp:
13322 return "DW_FORM_strp";
13323 case DW_FORM_udata:
13324 return "DW_FORM_udata";
13325 case DW_FORM_ref_addr:
13326 return "DW_FORM_ref_addr";
13327 case DW_FORM_ref1:
13328 return "DW_FORM_ref1";
13329 case DW_FORM_ref2:
13330 return "DW_FORM_ref2";
13331 case DW_FORM_ref4:
13332 return "DW_FORM_ref4";
13333 case DW_FORM_ref8:
13334 return "DW_FORM_ref8";
13335 case DW_FORM_ref_udata:
13336 return "DW_FORM_ref_udata";
13337 case DW_FORM_indirect:
13338 return "DW_FORM_indirect";
348e048f
DE
13339 case DW_FORM_sec_offset:
13340 return "DW_FORM_sec_offset";
13341 case DW_FORM_exprloc:
13342 return "DW_FORM_exprloc";
13343 case DW_FORM_flag_present:
13344 return "DW_FORM_flag_present";
55f1336d
TT
13345 case DW_FORM_ref_sig8:
13346 return "DW_FORM_ref_sig8";
c906108c
SS
13347 default:
13348 return "DW_FORM_<unknown>";
13349 }
13350}
13351
13352/* Convert a DWARF stack opcode into its string name. */
13353
9eae7c52 13354const char *
b1bfef65 13355dwarf_stack_op_name (unsigned op)
c906108c
SS
13356{
13357 switch (op)
13358 {
13359 case DW_OP_addr:
13360 return "DW_OP_addr";
13361 case DW_OP_deref:
13362 return "DW_OP_deref";
13363 case DW_OP_const1u:
13364 return "DW_OP_const1u";
13365 case DW_OP_const1s:
13366 return "DW_OP_const1s";
13367 case DW_OP_const2u:
13368 return "DW_OP_const2u";
13369 case DW_OP_const2s:
13370 return "DW_OP_const2s";
13371 case DW_OP_const4u:
13372 return "DW_OP_const4u";
13373 case DW_OP_const4s:
13374 return "DW_OP_const4s";
13375 case DW_OP_const8u:
13376 return "DW_OP_const8u";
13377 case DW_OP_const8s:
13378 return "DW_OP_const8s";
13379 case DW_OP_constu:
13380 return "DW_OP_constu";
13381 case DW_OP_consts:
13382 return "DW_OP_consts";
13383 case DW_OP_dup:
13384 return "DW_OP_dup";
13385 case DW_OP_drop:
13386 return "DW_OP_drop";
13387 case DW_OP_over:
13388 return "DW_OP_over";
13389 case DW_OP_pick:
13390 return "DW_OP_pick";
13391 case DW_OP_swap:
13392 return "DW_OP_swap";
13393 case DW_OP_rot:
13394 return "DW_OP_rot";
13395 case DW_OP_xderef:
13396 return "DW_OP_xderef";
13397 case DW_OP_abs:
13398 return "DW_OP_abs";
13399 case DW_OP_and:
13400 return "DW_OP_and";
13401 case DW_OP_div:
13402 return "DW_OP_div";
13403 case DW_OP_minus:
13404 return "DW_OP_minus";
13405 case DW_OP_mod:
13406 return "DW_OP_mod";
13407 case DW_OP_mul:
13408 return "DW_OP_mul";
13409 case DW_OP_neg:
13410 return "DW_OP_neg";
13411 case DW_OP_not:
13412 return "DW_OP_not";
13413 case DW_OP_or:
13414 return "DW_OP_or";
13415 case DW_OP_plus:
13416 return "DW_OP_plus";
13417 case DW_OP_plus_uconst:
13418 return "DW_OP_plus_uconst";
13419 case DW_OP_shl:
13420 return "DW_OP_shl";
13421 case DW_OP_shr:
13422 return "DW_OP_shr";
13423 case DW_OP_shra:
13424 return "DW_OP_shra";
13425 case DW_OP_xor:
13426 return "DW_OP_xor";
13427 case DW_OP_bra:
13428 return "DW_OP_bra";
13429 case DW_OP_eq:
13430 return "DW_OP_eq";
13431 case DW_OP_ge:
13432 return "DW_OP_ge";
13433 case DW_OP_gt:
13434 return "DW_OP_gt";
13435 case DW_OP_le:
13436 return "DW_OP_le";
13437 case DW_OP_lt:
13438 return "DW_OP_lt";
13439 case DW_OP_ne:
13440 return "DW_OP_ne";
13441 case DW_OP_skip:
13442 return "DW_OP_skip";
13443 case DW_OP_lit0:
13444 return "DW_OP_lit0";
13445 case DW_OP_lit1:
13446 return "DW_OP_lit1";
13447 case DW_OP_lit2:
13448 return "DW_OP_lit2";
13449 case DW_OP_lit3:
13450 return "DW_OP_lit3";
13451 case DW_OP_lit4:
13452 return "DW_OP_lit4";
13453 case DW_OP_lit5:
13454 return "DW_OP_lit5";
13455 case DW_OP_lit6:
13456 return "DW_OP_lit6";
13457 case DW_OP_lit7:
13458 return "DW_OP_lit7";
13459 case DW_OP_lit8:
13460 return "DW_OP_lit8";
13461 case DW_OP_lit9:
13462 return "DW_OP_lit9";
13463 case DW_OP_lit10:
13464 return "DW_OP_lit10";
13465 case DW_OP_lit11:
13466 return "DW_OP_lit11";
13467 case DW_OP_lit12:
13468 return "DW_OP_lit12";
13469 case DW_OP_lit13:
13470 return "DW_OP_lit13";
13471 case DW_OP_lit14:
13472 return "DW_OP_lit14";
13473 case DW_OP_lit15:
13474 return "DW_OP_lit15";
13475 case DW_OP_lit16:
13476 return "DW_OP_lit16";
13477 case DW_OP_lit17:
13478 return "DW_OP_lit17";
13479 case DW_OP_lit18:
13480 return "DW_OP_lit18";
13481 case DW_OP_lit19:
13482 return "DW_OP_lit19";
13483 case DW_OP_lit20:
13484 return "DW_OP_lit20";
13485 case DW_OP_lit21:
13486 return "DW_OP_lit21";
13487 case DW_OP_lit22:
13488 return "DW_OP_lit22";
13489 case DW_OP_lit23:
13490 return "DW_OP_lit23";
13491 case DW_OP_lit24:
13492 return "DW_OP_lit24";
13493 case DW_OP_lit25:
13494 return "DW_OP_lit25";
13495 case DW_OP_lit26:
13496 return "DW_OP_lit26";
13497 case DW_OP_lit27:
13498 return "DW_OP_lit27";
13499 case DW_OP_lit28:
13500 return "DW_OP_lit28";
13501 case DW_OP_lit29:
13502 return "DW_OP_lit29";
13503 case DW_OP_lit30:
13504 return "DW_OP_lit30";
13505 case DW_OP_lit31:
13506 return "DW_OP_lit31";
13507 case DW_OP_reg0:
13508 return "DW_OP_reg0";
13509 case DW_OP_reg1:
13510 return "DW_OP_reg1";
13511 case DW_OP_reg2:
13512 return "DW_OP_reg2";
13513 case DW_OP_reg3:
13514 return "DW_OP_reg3";
13515 case DW_OP_reg4:
13516 return "DW_OP_reg4";
13517 case DW_OP_reg5:
13518 return "DW_OP_reg5";
13519 case DW_OP_reg6:
13520 return "DW_OP_reg6";
13521 case DW_OP_reg7:
13522 return "DW_OP_reg7";
13523 case DW_OP_reg8:
13524 return "DW_OP_reg8";
13525 case DW_OP_reg9:
13526 return "DW_OP_reg9";
13527 case DW_OP_reg10:
13528 return "DW_OP_reg10";
13529 case DW_OP_reg11:
13530 return "DW_OP_reg11";
13531 case DW_OP_reg12:
13532 return "DW_OP_reg12";
13533 case DW_OP_reg13:
13534 return "DW_OP_reg13";
13535 case DW_OP_reg14:
13536 return "DW_OP_reg14";
13537 case DW_OP_reg15:
13538 return "DW_OP_reg15";
13539 case DW_OP_reg16:
13540 return "DW_OP_reg16";
13541 case DW_OP_reg17:
13542 return "DW_OP_reg17";
13543 case DW_OP_reg18:
13544 return "DW_OP_reg18";
13545 case DW_OP_reg19:
13546 return "DW_OP_reg19";
13547 case DW_OP_reg20:
13548 return "DW_OP_reg20";
13549 case DW_OP_reg21:
13550 return "DW_OP_reg21";
13551 case DW_OP_reg22:
13552 return "DW_OP_reg22";
13553 case DW_OP_reg23:
13554 return "DW_OP_reg23";
13555 case DW_OP_reg24:
13556 return "DW_OP_reg24";
13557 case DW_OP_reg25:
13558 return "DW_OP_reg25";
13559 case DW_OP_reg26:
13560 return "DW_OP_reg26";
13561 case DW_OP_reg27:
13562 return "DW_OP_reg27";
13563 case DW_OP_reg28:
13564 return "DW_OP_reg28";
13565 case DW_OP_reg29:
13566 return "DW_OP_reg29";
13567 case DW_OP_reg30:
13568 return "DW_OP_reg30";
13569 case DW_OP_reg31:
13570 return "DW_OP_reg31";
13571 case DW_OP_breg0:
13572 return "DW_OP_breg0";
13573 case DW_OP_breg1:
13574 return "DW_OP_breg1";
13575 case DW_OP_breg2:
13576 return "DW_OP_breg2";
13577 case DW_OP_breg3:
13578 return "DW_OP_breg3";
13579 case DW_OP_breg4:
13580 return "DW_OP_breg4";
13581 case DW_OP_breg5:
13582 return "DW_OP_breg5";
13583 case DW_OP_breg6:
13584 return "DW_OP_breg6";
13585 case DW_OP_breg7:
13586 return "DW_OP_breg7";
13587 case DW_OP_breg8:
13588 return "DW_OP_breg8";
13589 case DW_OP_breg9:
13590 return "DW_OP_breg9";
13591 case DW_OP_breg10:
13592 return "DW_OP_breg10";
13593 case DW_OP_breg11:
13594 return "DW_OP_breg11";
13595 case DW_OP_breg12:
13596 return "DW_OP_breg12";
13597 case DW_OP_breg13:
13598 return "DW_OP_breg13";
13599 case DW_OP_breg14:
13600 return "DW_OP_breg14";
13601 case DW_OP_breg15:
13602 return "DW_OP_breg15";
13603 case DW_OP_breg16:
13604 return "DW_OP_breg16";
13605 case DW_OP_breg17:
13606 return "DW_OP_breg17";
13607 case DW_OP_breg18:
13608 return "DW_OP_breg18";
13609 case DW_OP_breg19:
13610 return "DW_OP_breg19";
13611 case DW_OP_breg20:
13612 return "DW_OP_breg20";
13613 case DW_OP_breg21:
13614 return "DW_OP_breg21";
13615 case DW_OP_breg22:
13616 return "DW_OP_breg22";
13617 case DW_OP_breg23:
13618 return "DW_OP_breg23";
13619 case DW_OP_breg24:
13620 return "DW_OP_breg24";
13621 case DW_OP_breg25:
13622 return "DW_OP_breg25";
13623 case DW_OP_breg26:
13624 return "DW_OP_breg26";
13625 case DW_OP_breg27:
13626 return "DW_OP_breg27";
13627 case DW_OP_breg28:
13628 return "DW_OP_breg28";
13629 case DW_OP_breg29:
13630 return "DW_OP_breg29";
13631 case DW_OP_breg30:
13632 return "DW_OP_breg30";
13633 case DW_OP_breg31:
13634 return "DW_OP_breg31";
13635 case DW_OP_regx:
13636 return "DW_OP_regx";
13637 case DW_OP_fbreg:
13638 return "DW_OP_fbreg";
13639 case DW_OP_bregx:
13640 return "DW_OP_bregx";
13641 case DW_OP_piece:
13642 return "DW_OP_piece";
13643 case DW_OP_deref_size:
13644 return "DW_OP_deref_size";
13645 case DW_OP_xderef_size:
13646 return "DW_OP_xderef_size";
13647 case DW_OP_nop:
13648 return "DW_OP_nop";
b7619582 13649 /* DWARF 3 extensions. */
ed348acc
EZ
13650 case DW_OP_push_object_address:
13651 return "DW_OP_push_object_address";
13652 case DW_OP_call2:
13653 return "DW_OP_call2";
13654 case DW_OP_call4:
13655 return "DW_OP_call4";
13656 case DW_OP_call_ref:
13657 return "DW_OP_call_ref";
b7619582
GF
13658 case DW_OP_form_tls_address:
13659 return "DW_OP_form_tls_address";
13660 case DW_OP_call_frame_cfa:
13661 return "DW_OP_call_frame_cfa";
13662 case DW_OP_bit_piece:
13663 return "DW_OP_bit_piece";
9eae7c52
TT
13664 /* DWARF 4 extensions. */
13665 case DW_OP_implicit_value:
13666 return "DW_OP_implicit_value";
13667 case DW_OP_stack_value:
13668 return "DW_OP_stack_value";
13669 /* GNU extensions. */
ed348acc
EZ
13670 case DW_OP_GNU_push_tls_address:
13671 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13672 case DW_OP_GNU_uninit:
13673 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13674 case DW_OP_GNU_implicit_pointer:
13675 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13676 case DW_OP_GNU_entry_value:
13677 return "DW_OP_GNU_entry_value";
13678 case DW_OP_GNU_const_type:
13679 return "DW_OP_GNU_const_type";
13680 case DW_OP_GNU_regval_type:
13681 return "DW_OP_GNU_regval_type";
13682 case DW_OP_GNU_deref_type:
13683 return "DW_OP_GNU_deref_type";
13684 case DW_OP_GNU_convert:
13685 return "DW_OP_GNU_convert";
13686 case DW_OP_GNU_reinterpret:
13687 return "DW_OP_GNU_reinterpret";
c906108c 13688 default:
b1bfef65 13689 return NULL;
c906108c
SS
13690 }
13691}
13692
13693static char *
fba45db2 13694dwarf_bool_name (unsigned mybool)
c906108c
SS
13695{
13696 if (mybool)
13697 return "TRUE";
13698 else
13699 return "FALSE";
13700}
13701
13702/* Convert a DWARF type code into its string name. */
13703
13704static char *
aa1ee363 13705dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13706{
13707 switch (enc)
13708 {
b7619582
GF
13709 case DW_ATE_void:
13710 return "DW_ATE_void";
c906108c
SS
13711 case DW_ATE_address:
13712 return "DW_ATE_address";
13713 case DW_ATE_boolean:
13714 return "DW_ATE_boolean";
13715 case DW_ATE_complex_float:
13716 return "DW_ATE_complex_float";
13717 case DW_ATE_float:
13718 return "DW_ATE_float";
13719 case DW_ATE_signed:
13720 return "DW_ATE_signed";
13721 case DW_ATE_signed_char:
13722 return "DW_ATE_signed_char";
13723 case DW_ATE_unsigned:
13724 return "DW_ATE_unsigned";
13725 case DW_ATE_unsigned_char:
13726 return "DW_ATE_unsigned_char";
b7619582 13727 /* DWARF 3. */
d9fa45fe
DC
13728 case DW_ATE_imaginary_float:
13729 return "DW_ATE_imaginary_float";
b7619582
GF
13730 case DW_ATE_packed_decimal:
13731 return "DW_ATE_packed_decimal";
13732 case DW_ATE_numeric_string:
13733 return "DW_ATE_numeric_string";
13734 case DW_ATE_edited:
13735 return "DW_ATE_edited";
13736 case DW_ATE_signed_fixed:
13737 return "DW_ATE_signed_fixed";
13738 case DW_ATE_unsigned_fixed:
13739 return "DW_ATE_unsigned_fixed";
13740 case DW_ATE_decimal_float:
13741 return "DW_ATE_decimal_float";
75079b2b
TT
13742 /* DWARF 4. */
13743 case DW_ATE_UTF:
13744 return "DW_ATE_UTF";
b7619582
GF
13745 /* HP extensions. */
13746 case DW_ATE_HP_float80:
13747 return "DW_ATE_HP_float80";
13748 case DW_ATE_HP_complex_float80:
13749 return "DW_ATE_HP_complex_float80";
13750 case DW_ATE_HP_float128:
13751 return "DW_ATE_HP_float128";
13752 case DW_ATE_HP_complex_float128:
13753 return "DW_ATE_HP_complex_float128";
13754 case DW_ATE_HP_floathpintel:
13755 return "DW_ATE_HP_floathpintel";
13756 case DW_ATE_HP_imaginary_float80:
13757 return "DW_ATE_HP_imaginary_float80";
13758 case DW_ATE_HP_imaginary_float128:
13759 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13760 default:
13761 return "DW_ATE_<unknown>";
13762 }
13763}
13764
0963b4bd 13765/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13766
13767#if 0
13768static char *
aa1ee363 13769dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13770{
13771 switch (cfi_opc)
13772 {
13773 case DW_CFA_advance_loc:
13774 return "DW_CFA_advance_loc";
13775 case DW_CFA_offset:
13776 return "DW_CFA_offset";
13777 case DW_CFA_restore:
13778 return "DW_CFA_restore";
13779 case DW_CFA_nop:
13780 return "DW_CFA_nop";
13781 case DW_CFA_set_loc:
13782 return "DW_CFA_set_loc";
13783 case DW_CFA_advance_loc1:
13784 return "DW_CFA_advance_loc1";
13785 case DW_CFA_advance_loc2:
13786 return "DW_CFA_advance_loc2";
13787 case DW_CFA_advance_loc4:
13788 return "DW_CFA_advance_loc4";
13789 case DW_CFA_offset_extended:
13790 return "DW_CFA_offset_extended";
13791 case DW_CFA_restore_extended:
13792 return "DW_CFA_restore_extended";
13793 case DW_CFA_undefined:
13794 return "DW_CFA_undefined";
13795 case DW_CFA_same_value:
13796 return "DW_CFA_same_value";
13797 case DW_CFA_register:
13798 return "DW_CFA_register";
13799 case DW_CFA_remember_state:
13800 return "DW_CFA_remember_state";
13801 case DW_CFA_restore_state:
13802 return "DW_CFA_restore_state";
13803 case DW_CFA_def_cfa:
13804 return "DW_CFA_def_cfa";
13805 case DW_CFA_def_cfa_register:
13806 return "DW_CFA_def_cfa_register";
13807 case DW_CFA_def_cfa_offset:
13808 return "DW_CFA_def_cfa_offset";
b7619582 13809 /* DWARF 3. */
985cb1a3
JM
13810 case DW_CFA_def_cfa_expression:
13811 return "DW_CFA_def_cfa_expression";
13812 case DW_CFA_expression:
13813 return "DW_CFA_expression";
13814 case DW_CFA_offset_extended_sf:
13815 return "DW_CFA_offset_extended_sf";
13816 case DW_CFA_def_cfa_sf:
13817 return "DW_CFA_def_cfa_sf";
13818 case DW_CFA_def_cfa_offset_sf:
13819 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13820 case DW_CFA_val_offset:
13821 return "DW_CFA_val_offset";
13822 case DW_CFA_val_offset_sf:
13823 return "DW_CFA_val_offset_sf";
13824 case DW_CFA_val_expression:
13825 return "DW_CFA_val_expression";
13826 /* SGI/MIPS specific. */
c906108c
SS
13827 case DW_CFA_MIPS_advance_loc8:
13828 return "DW_CFA_MIPS_advance_loc8";
b7619582 13829 /* GNU extensions. */
985cb1a3
JM
13830 case DW_CFA_GNU_window_save:
13831 return "DW_CFA_GNU_window_save";
13832 case DW_CFA_GNU_args_size:
13833 return "DW_CFA_GNU_args_size";
13834 case DW_CFA_GNU_negative_offset_extended:
13835 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13836 default:
13837 return "DW_CFA_<unknown>";
13838 }
13839}
13840#endif
13841
f9aca02d 13842static void
d97bc12b 13843dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13844{
13845 unsigned int i;
13846
d97bc12b
DE
13847 print_spaces (indent, f);
13848 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13849 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13850
13851 if (die->parent != NULL)
13852 {
13853 print_spaces (indent, f);
13854 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13855 die->parent->offset);
13856 }
13857
13858 print_spaces (indent, f);
13859 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13860 dwarf_bool_name (die->child != NULL));
c906108c 13861
d97bc12b
DE
13862 print_spaces (indent, f);
13863 fprintf_unfiltered (f, " attributes:\n");
13864
c906108c
SS
13865 for (i = 0; i < die->num_attrs; ++i)
13866 {
d97bc12b
DE
13867 print_spaces (indent, f);
13868 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13869 dwarf_attr_name (die->attrs[i].name),
13870 dwarf_form_name (die->attrs[i].form));
d97bc12b 13871
c906108c
SS
13872 switch (die->attrs[i].form)
13873 {
13874 case DW_FORM_ref_addr:
13875 case DW_FORM_addr:
d97bc12b 13876 fprintf_unfiltered (f, "address: ");
5af949e3 13877 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13878 break;
13879 case DW_FORM_block2:
13880 case DW_FORM_block4:
13881 case DW_FORM_block:
13882 case DW_FORM_block1:
3e43a32a
MS
13883 fprintf_unfiltered (f, "block: size %d",
13884 DW_BLOCK (&die->attrs[i])->size);
c906108c 13885 break;
2dc7f7b3
TT
13886 case DW_FORM_exprloc:
13887 fprintf_unfiltered (f, "expression: size %u",
13888 DW_BLOCK (&die->attrs[i])->size);
13889 break;
10b3939b
DJ
13890 case DW_FORM_ref1:
13891 case DW_FORM_ref2:
13892 case DW_FORM_ref4:
d97bc12b 13893 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13894 (long) (DW_ADDR (&die->attrs[i])));
13895 break;
c906108c
SS
13896 case DW_FORM_data1:
13897 case DW_FORM_data2:
13898 case DW_FORM_data4:
ce5d95e1 13899 case DW_FORM_data8:
c906108c
SS
13900 case DW_FORM_udata:
13901 case DW_FORM_sdata:
43bbcdc2
PH
13902 fprintf_unfiltered (f, "constant: %s",
13903 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13904 break;
2dc7f7b3
TT
13905 case DW_FORM_sec_offset:
13906 fprintf_unfiltered (f, "section offset: %s",
13907 pulongest (DW_UNSND (&die->attrs[i])));
13908 break;
55f1336d 13909 case DW_FORM_ref_sig8:
348e048f
DE
13910 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13911 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b3c8eb43 13912 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
348e048f
DE
13913 else
13914 fprintf_unfiltered (f, "signatured type, offset: unknown");
13915 break;
c906108c 13916 case DW_FORM_string:
4bdf3d34 13917 case DW_FORM_strp:
8285870a 13918 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13919 DW_STRING (&die->attrs[i])
8285870a
JK
13920 ? DW_STRING (&die->attrs[i]) : "",
13921 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13922 break;
13923 case DW_FORM_flag:
13924 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13925 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13926 else
d97bc12b 13927 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13928 break;
2dc7f7b3
TT
13929 case DW_FORM_flag_present:
13930 fprintf_unfiltered (f, "flag: TRUE");
13931 break;
a8329558 13932 case DW_FORM_indirect:
0963b4bd
MS
13933 /* The reader will have reduced the indirect form to
13934 the "base form" so this form should not occur. */
3e43a32a
MS
13935 fprintf_unfiltered (f,
13936 "unexpected attribute form: DW_FORM_indirect");
a8329558 13937 break;
c906108c 13938 default:
d97bc12b 13939 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13940 die->attrs[i].form);
d97bc12b 13941 break;
c906108c 13942 }
d97bc12b 13943 fprintf_unfiltered (f, "\n");
c906108c
SS
13944 }
13945}
13946
f9aca02d 13947static void
d97bc12b 13948dump_die_for_error (struct die_info *die)
c906108c 13949{
d97bc12b
DE
13950 dump_die_shallow (gdb_stderr, 0, die);
13951}
13952
13953static void
13954dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13955{
13956 int indent = level * 4;
13957
13958 gdb_assert (die != NULL);
13959
13960 if (level >= max_level)
13961 return;
13962
13963 dump_die_shallow (f, indent, die);
13964
13965 if (die->child != NULL)
c906108c 13966 {
d97bc12b
DE
13967 print_spaces (indent, f);
13968 fprintf_unfiltered (f, " Children:");
13969 if (level + 1 < max_level)
13970 {
13971 fprintf_unfiltered (f, "\n");
13972 dump_die_1 (f, level + 1, max_level, die->child);
13973 }
13974 else
13975 {
3e43a32a
MS
13976 fprintf_unfiltered (f,
13977 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13978 }
13979 }
13980
13981 if (die->sibling != NULL && level > 0)
13982 {
13983 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13984 }
13985}
13986
d97bc12b
DE
13987/* This is called from the pdie macro in gdbinit.in.
13988 It's not static so gcc will keep a copy callable from gdb. */
13989
13990void
13991dump_die (struct die_info *die, int max_level)
13992{
13993 dump_die_1 (gdb_stdlog, 0, max_level, die);
13994}
13995
f9aca02d 13996static void
51545339 13997store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13998{
51545339 13999 void **slot;
c906108c 14000
51545339
DJ
14001 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14002
14003 *slot = die;
c906108c
SS
14004}
14005
93311388
DE
14006static int
14007is_ref_attr (struct attribute *attr)
c906108c 14008{
c906108c
SS
14009 switch (attr->form)
14010 {
14011 case DW_FORM_ref_addr:
c906108c
SS
14012 case DW_FORM_ref1:
14013 case DW_FORM_ref2:
14014 case DW_FORM_ref4:
613e1657 14015 case DW_FORM_ref8:
c906108c 14016 case DW_FORM_ref_udata:
93311388 14017 return 1;
c906108c 14018 default:
93311388 14019 return 0;
c906108c 14020 }
93311388
DE
14021}
14022
14023static unsigned int
14024dwarf2_get_ref_die_offset (struct attribute *attr)
14025{
14026 if (is_ref_attr (attr))
14027 return DW_ADDR (attr);
14028
14029 complaint (&symfile_complaints,
14030 _("unsupported die ref attribute form: '%s'"),
14031 dwarf_form_name (attr->form));
14032 return 0;
c906108c
SS
14033}
14034
43bbcdc2
PH
14035/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14036 * the value held by the attribute is not constant. */
a02abb62 14037
43bbcdc2 14038static LONGEST
a02abb62
JB
14039dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14040{
14041 if (attr->form == DW_FORM_sdata)
14042 return DW_SND (attr);
14043 else if (attr->form == DW_FORM_udata
14044 || attr->form == DW_FORM_data1
14045 || attr->form == DW_FORM_data2
14046 || attr->form == DW_FORM_data4
14047 || attr->form == DW_FORM_data8)
14048 return DW_UNSND (attr);
14049 else
14050 {
3e43a32a
MS
14051 complaint (&symfile_complaints,
14052 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14053 dwarf_form_name (attr->form));
14054 return default_value;
14055 }
14056}
14057
03dd20cc 14058/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14059 unit and add it to our queue.
14060 The result is non-zero if PER_CU was queued, otherwise the result is zero
14061 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14062
348e048f 14063static int
03dd20cc
DJ
14064maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14065 struct dwarf2_per_cu_data *per_cu)
14066{
98bfdba5
PA
14067 /* We may arrive here during partial symbol reading, if we need full
14068 DIEs to process an unusual case (e.g. template arguments). Do
14069 not queue PER_CU, just tell our caller to load its DIEs. */
14070 if (dwarf2_per_objfile->reading_partial_symbols)
14071 {
14072 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14073 return 1;
14074 return 0;
14075 }
14076
03dd20cc
DJ
14077 /* Mark the dependence relation so that we don't flush PER_CU
14078 too early. */
14079 dwarf2_add_dependence (this_cu, per_cu);
14080
14081 /* If it's already on the queue, we have nothing to do. */
14082 if (per_cu->queued)
348e048f 14083 return 0;
03dd20cc
DJ
14084
14085 /* If the compilation unit is already loaded, just mark it as
14086 used. */
14087 if (per_cu->cu != NULL)
14088 {
14089 per_cu->cu->last_used = 0;
348e048f 14090 return 0;
03dd20cc
DJ
14091 }
14092
14093 /* Add it to the queue. */
14094 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
14095
14096 return 1;
14097}
14098
14099/* Follow reference or signature attribute ATTR of SRC_DIE.
14100 On entry *REF_CU is the CU of SRC_DIE.
14101 On exit *REF_CU is the CU of the result. */
14102
14103static struct die_info *
14104follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14105 struct dwarf2_cu **ref_cu)
14106{
14107 struct die_info *die;
14108
14109 if (is_ref_attr (attr))
14110 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14111 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14112 die = follow_die_sig (src_die, attr, ref_cu);
14113 else
14114 {
14115 dump_die_for_error (src_die);
14116 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14117 (*ref_cu)->objfile->name);
14118 }
14119
14120 return die;
03dd20cc
DJ
14121}
14122
5c631832 14123/* Follow reference OFFSET.
673bfd45
DE
14124 On entry *REF_CU is the CU of the source die referencing OFFSET.
14125 On exit *REF_CU is the CU of the result.
14126 Returns NULL if OFFSET is invalid. */
f504f079 14127
f9aca02d 14128static struct die_info *
5c631832 14129follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 14130{
10b3939b 14131 struct die_info temp_die;
f2f0e013 14132 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14133
348e048f
DE
14134 gdb_assert (cu->per_cu != NULL);
14135
98bfdba5
PA
14136 target_cu = cu;
14137
8b70b953 14138 if (cu->per_cu->debug_type_section)
348e048f
DE
14139 {
14140 /* .debug_types CUs cannot reference anything outside their CU.
14141 If they need to, they have to reference a signatured type via
55f1336d 14142 DW_FORM_ref_sig8. */
348e048f 14143 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14144 return NULL;
348e048f
DE
14145 }
14146 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14147 {
14148 struct dwarf2_per_cu_data *per_cu;
9a619af0 14149
45452591 14150 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14151
14152 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
14153 if (maybe_queue_comp_unit (cu, per_cu))
14154 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 14155
10b3939b
DJ
14156 target_cu = per_cu->cu;
14157 }
98bfdba5
PA
14158 else if (cu->dies == NULL)
14159 {
14160 /* We're loading full DIEs during partial symbol reading. */
14161 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14162 load_full_comp_unit (cu->per_cu, cu->objfile);
14163 }
c906108c 14164
f2f0e013 14165 *ref_cu = target_cu;
51545339 14166 temp_die.offset = offset;
5c631832
JK
14167 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14168}
10b3939b 14169
5c631832
JK
14170/* Follow reference attribute ATTR of SRC_DIE.
14171 On entry *REF_CU is the CU of SRC_DIE.
14172 On exit *REF_CU is the CU of the result. */
14173
14174static struct die_info *
14175follow_die_ref (struct die_info *src_die, struct attribute *attr,
14176 struct dwarf2_cu **ref_cu)
14177{
14178 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14179 struct dwarf2_cu *cu = *ref_cu;
14180 struct die_info *die;
14181
14182 die = follow_die_offset (offset, ref_cu);
14183 if (!die)
14184 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14185 "at 0x%x [in module %s]"),
14186 offset, src_die->offset, cu->objfile->name);
348e048f 14187
5c631832
JK
14188 return die;
14189}
14190
d83e736b
JK
14191/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14192 Returned value is intended for DW_OP_call*. Returned
14193 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14194
14195struct dwarf2_locexpr_baton
14196dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
14197 struct dwarf2_per_cu_data *per_cu,
14198 CORE_ADDR (*get_frame_pc) (void *baton),
14199 void *baton)
5c631832 14200{
918dd910 14201 struct dwarf2_cu *cu;
5c631832
JK
14202 struct die_info *die;
14203 struct attribute *attr;
14204 struct dwarf2_locexpr_baton retval;
14205
8cf6f0b1
TT
14206 dw2_setup (per_cu->objfile);
14207
918dd910
JK
14208 if (per_cu->cu == NULL)
14209 load_cu (per_cu);
14210 cu = per_cu->cu;
14211
5c631832
JK
14212 die = follow_die_offset (offset, &cu);
14213 if (!die)
14214 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14215 offset, per_cu->cu->objfile->name);
14216
14217 attr = dwarf2_attr (die, DW_AT_location, cu);
14218 if (!attr)
14219 {
e103e986
JK
14220 /* DWARF: "If there is no such attribute, then there is no effect.".
14221 DATA is ignored if SIZE is 0. */
5c631832 14222
e103e986 14223 retval.data = NULL;
5c631832
JK
14224 retval.size = 0;
14225 }
8cf6f0b1
TT
14226 else if (attr_form_is_section_offset (attr))
14227 {
14228 struct dwarf2_loclist_baton loclist_baton;
14229 CORE_ADDR pc = (*get_frame_pc) (baton);
14230 size_t size;
14231
14232 fill_in_loclist_baton (cu, &loclist_baton, attr);
14233
14234 retval.data = dwarf2_find_location_expression (&loclist_baton,
14235 &size, pc);
14236 retval.size = size;
14237 }
5c631832
JK
14238 else
14239 {
14240 if (!attr_form_is_block (attr))
14241 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14242 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14243 offset, per_cu->cu->objfile->name);
14244
14245 retval.data = DW_BLOCK (attr)->data;
14246 retval.size = DW_BLOCK (attr)->size;
14247 }
14248 retval.per_cu = cu->per_cu;
918dd910 14249
918dd910
JK
14250 age_cached_comp_units ();
14251
5c631832 14252 return retval;
348e048f
DE
14253}
14254
8a9b8146
TT
14255/* Return the type of the DIE at DIE_OFFSET in the CU named by
14256 PER_CU. */
14257
14258struct type *
14259dwarf2_get_die_type (unsigned int die_offset,
14260 struct dwarf2_per_cu_data *per_cu)
14261{
8a9b8146 14262 dw2_setup (per_cu->objfile);
9ff3b74f 14263 return get_die_type_at_offset (die_offset, per_cu);
8a9b8146
TT
14264}
14265
348e048f
DE
14266/* Follow the signature attribute ATTR in SRC_DIE.
14267 On entry *REF_CU is the CU of SRC_DIE.
14268 On exit *REF_CU is the CU of the result. */
14269
14270static struct die_info *
14271follow_die_sig (struct die_info *src_die, struct attribute *attr,
14272 struct dwarf2_cu **ref_cu)
14273{
14274 struct objfile *objfile = (*ref_cu)->objfile;
14275 struct die_info temp_die;
14276 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14277 struct dwarf2_cu *sig_cu;
14278 struct die_info *die;
14279
14280 /* sig_type will be NULL if the signatured type is missing from
14281 the debug info. */
14282 if (sig_type == NULL)
14283 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14284 "at 0x%x [in module %s]"),
14285 src_die->offset, objfile->name);
14286
14287 /* If necessary, add it to the queue and load its DIEs. */
14288
14289 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14290 read_signatured_type (objfile, sig_type);
14291
14292 gdb_assert (sig_type->per_cu.cu != NULL);
14293
14294 sig_cu = sig_type->per_cu.cu;
14295 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14296 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14297 if (die)
14298 {
14299 *ref_cu = sig_cu;
14300 return die;
14301 }
14302
3e43a32a
MS
14303 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14304 "from DIE at 0x%x [in module %s]"),
348e048f
DE
14305 sig_type->type_offset, src_die->offset, objfile->name);
14306}
14307
14308/* Given an offset of a signatured type, return its signatured_type. */
14309
14310static struct signatured_type *
8b70b953
TT
14311lookup_signatured_type_at_offset (struct objfile *objfile,
14312 struct dwarf2_section_info *section,
14313 unsigned int offset)
348e048f 14314{
8b70b953 14315 gdb_byte *info_ptr = section->buffer + offset;
348e048f
DE
14316 unsigned int length, initial_length_size;
14317 unsigned int sig_offset;
14318 struct signatured_type find_entry, *type_sig;
14319
14320 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14321 sig_offset = (initial_length_size
14322 + 2 /*version*/
14323 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14324 + 1 /*address_size*/);
14325 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14326 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14327
14328 /* This is only used to lookup previously recorded types.
14329 If we didn't find it, it's our bug. */
14330 gdb_assert (type_sig != NULL);
b3c8eb43 14331 gdb_assert (offset == type_sig->per_cu.offset);
348e048f
DE
14332
14333 return type_sig;
14334}
14335
14336/* Read in signatured type at OFFSET and build its CU and die(s). */
14337
14338static void
14339read_signatured_type_at_offset (struct objfile *objfile,
8b70b953 14340 struct dwarf2_section_info *sect,
348e048f
DE
14341 unsigned int offset)
14342{
14343 struct signatured_type *type_sig;
14344
8b70b953 14345 dwarf2_read_section (objfile, sect);
be391dca 14346
348e048f
DE
14347 /* We have the section offset, but we need the signature to do the
14348 hash table lookup. */
8b70b953 14349 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f
DE
14350
14351 gdb_assert (type_sig->per_cu.cu == NULL);
14352
14353 read_signatured_type (objfile, type_sig);
14354
14355 gdb_assert (type_sig->per_cu.cu != NULL);
14356}
14357
14358/* Read in a signatured type and build its CU and DIEs. */
14359
14360static void
14361read_signatured_type (struct objfile *objfile,
14362 struct signatured_type *type_sig)
14363{
1fd400ff 14364 gdb_byte *types_ptr;
348e048f
DE
14365 struct die_reader_specs reader_specs;
14366 struct dwarf2_cu *cu;
14367 ULONGEST signature;
14368 struct cleanup *back_to, *free_cu_cleanup;
8b70b953 14369 struct dwarf2_section_info *section = type_sig->per_cu.debug_type_section;
348e048f 14370
8b70b953
TT
14371 dwarf2_read_section (objfile, section);
14372 types_ptr = section->buffer + type_sig->per_cu.offset;
1fd400ff 14373
348e048f
DE
14374 gdb_assert (type_sig->per_cu.cu == NULL);
14375
9816fde3
JK
14376 cu = xmalloc (sizeof (*cu));
14377 init_one_comp_unit (cu, objfile);
14378
348e048f
DE
14379 type_sig->per_cu.cu = cu;
14380 cu->per_cu = &type_sig->per_cu;
14381
14382 /* If an error occurs while loading, release our storage. */
14383 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14384
8b70b953 14385 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
348e048f
DE
14386 types_ptr, objfile->obfd);
14387 gdb_assert (signature == type_sig->signature);
14388
14389 cu->die_hash
14390 = htab_create_alloc_ex (cu->header.length / 12,
14391 die_hash,
14392 die_eq,
14393 NULL,
14394 &cu->comp_unit_obstack,
14395 hashtab_obstack_allocate,
14396 dummy_obstack_deallocate);
14397
14398 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14399 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14400
14401 init_cu_die_reader (&reader_specs, cu);
14402
14403 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14404 NULL /*parent*/);
14405
14406 /* We try not to read any attributes in this function, because not
14407 all objfiles needed for references have been loaded yet, and symbol
14408 table processing isn't initialized. But we have to set the CU language,
14409 or we won't be able to build types correctly. */
9816fde3 14410 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14411
14412 do_cleanups (back_to);
14413
14414 /* We've successfully allocated this compilation unit. Let our caller
14415 clean it up when finished with it. */
14416 discard_cleanups (free_cu_cleanup);
14417
14418 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14419 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
14420}
14421
3c6e0cb3
JK
14422/* Workaround as dwarf_expr_context_funcs.read_mem implementation before
14423 a proper runtime DWARF expressions evaluator gets implemented.
14424 Otherwise gnuv3_baseclass_offset would error by:
14425 Expected a negative vbase offset (old compiler?) */
14426
14427static void
14428decode_locdesc_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr,
14429 size_t length)
14430{
14431 struct dwarf_expr_context *ctx = baton;
14432 struct gdbarch *gdbarch = ctx->gdbarch;
14433 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
14434
14435 memset (buf, 0, length);
14436
14437 if (TYPE_LENGTH (ptr_type) == length)
14438 store_typed_address (buf, ptr_type, addr);
14439}
14440
14441static const struct dwarf_expr_context_funcs decode_locdesc_ctx_funcs =
14442{
14443 ctx_no_read_reg,
14444 decode_locdesc_read_mem,
14445 ctx_no_get_frame_base,
14446 ctx_no_get_frame_cfa,
14447 ctx_no_get_frame_pc,
14448 ctx_no_get_tls_address,
14449 ctx_no_dwarf_call,
14450 ctx_no_get_base_type
14451};
14452
c906108c
SS
14453/* Decode simple location descriptions.
14454 Given a pointer to a dwarf block that defines a location, compute
14455 the location and return the value.
14456
4cecd739
DJ
14457 NOTE drow/2003-11-18: This function is called in two situations
14458 now: for the address of static or global variables (partial symbols
14459 only) and for offsets into structures which are expected to be
14460 (more or less) constant. The partial symbol case should go away,
14461 and only the constant case should remain. That will let this
14462 function complain more accurately. A few special modes are allowed
14463 without complaint for global variables (for instance, global
14464 register values and thread-local values).
c906108c
SS
14465
14466 A location description containing no operations indicates that the
4cecd739 14467 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14468 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14469 callers will only want a very basic result and this can become a
3c6e0cb3 14470 complaint. */
c906108c
SS
14471
14472static CORE_ADDR
e7c27a73 14473decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14474{
e7c27a73 14475 struct objfile *objfile = cu->objfile;
3c6e0cb3
JK
14476 struct dwarf_expr_context *ctx;
14477 struct cleanup *old_chain;
14478 volatile struct gdb_exception ex;
c906108c 14479
3c6e0cb3
JK
14480 ctx = new_dwarf_expr_context ();
14481 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
14482 make_cleanup_value_free_to_mark (value_mark ());
f1bea926 14483
3c6e0cb3
JK
14484 ctx->gdbarch = get_objfile_arch (objfile);
14485 ctx->addr_size = cu->header.addr_size;
96408a79 14486 ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (cu->per_cu);
3c6e0cb3
JK
14487 ctx->offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14488 ctx->baton = ctx;
14489 ctx->funcs = &decode_locdesc_ctx_funcs;
c906108c 14490
3c6e0cb3
JK
14491 /* DW_AT_data_member_location expects the structure address to be pushed on
14492 the stack. Simulate the offset by address 0. */
14493 dwarf_expr_push_address (ctx, 0, 0);
c906108c 14494
3c6e0cb3
JK
14495 TRY_CATCH (ex, RETURN_MASK_ERROR)
14496 {
14497 dwarf_expr_eval (ctx, blk->data, blk->size);
14498 }
14499 if (ex.reason < 0)
14500 {
14501 if (ex.message)
14502 complaint (&symfile_complaints, "%s", ex.message);
14503 }
14504 else if (ctx->num_pieces == 0)
14505 switch (ctx->location)
14506 {
14507 /* The returned number will be bogus, just do not complain for locations
14508 in global registers - it is here only a partial symbol address. */
14509 case DWARF_VALUE_REGISTER:
d53d4ac5 14510
3c6e0cb3
JK
14511 case DWARF_VALUE_MEMORY:
14512 case DWARF_VALUE_STACK:
d53d4ac5 14513 {
3c6e0cb3 14514 CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
d53d4ac5 14515
3c6e0cb3
JK
14516 do_cleanups (old_chain);
14517 return address;
d53d4ac5 14518 }
3c6e0cb3
JK
14519 }
14520
14521 do_cleanups (old_chain);
14522 dwarf2_complex_location_expr_complaint ();
14523 return 0;
c906108c
SS
14524}
14525
14526/* memory allocation interface */
14527
c906108c 14528static struct dwarf_block *
7b5a2f43 14529dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14530{
14531 struct dwarf_block *blk;
14532
14533 blk = (struct dwarf_block *)
7b5a2f43 14534 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14535 return (blk);
14536}
14537
14538static struct abbrev_info *
f3dd6933 14539dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14540{
14541 struct abbrev_info *abbrev;
14542
f3dd6933
DJ
14543 abbrev = (struct abbrev_info *)
14544 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14545 memset (abbrev, 0, sizeof (struct abbrev_info));
14546 return (abbrev);
14547}
14548
14549static struct die_info *
b60c80d6 14550dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14551{
14552 struct die_info *die;
b60c80d6
DJ
14553 size_t size = sizeof (struct die_info);
14554
14555 if (num_attrs > 1)
14556 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14557
b60c80d6 14558 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14559 memset (die, 0, sizeof (struct die_info));
14560 return (die);
14561}
2e276125
JB
14562
14563\f
14564/* Macro support. */
14565
2e276125
JB
14566/* Return the full name of file number I in *LH's file name table.
14567 Use COMP_DIR as the name of the current directory of the
14568 compilation. The result is allocated using xmalloc; the caller is
14569 responsible for freeing it. */
14570static char *
14571file_full_name (int file, struct line_header *lh, const char *comp_dir)
14572{
6a83a1e6
EZ
14573 /* Is the file number a valid index into the line header's file name
14574 table? Remember that file numbers start with one, not zero. */
14575 if (1 <= file && file <= lh->num_file_names)
14576 {
14577 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14578
6a83a1e6
EZ
14579 if (IS_ABSOLUTE_PATH (fe->name))
14580 return xstrdup (fe->name);
14581 else
14582 {
14583 const char *dir;
14584 int dir_len;
14585 char *full_name;
14586
14587 if (fe->dir_index)
14588 dir = lh->include_dirs[fe->dir_index - 1];
14589 else
14590 dir = comp_dir;
14591
14592 if (dir)
14593 {
14594 dir_len = strlen (dir);
14595 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14596 strcpy (full_name, dir);
14597 full_name[dir_len] = '/';
14598 strcpy (full_name + dir_len + 1, fe->name);
14599 return full_name;
14600 }
14601 else
14602 return xstrdup (fe->name);
14603 }
14604 }
2e276125
JB
14605 else
14606 {
6a83a1e6
EZ
14607 /* The compiler produced a bogus file number. We can at least
14608 record the macro definitions made in the file, even if we
14609 won't be able to find the file by name. */
14610 char fake_name[80];
9a619af0 14611
6a83a1e6 14612 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14613
6e70227d 14614 complaint (&symfile_complaints,
6a83a1e6
EZ
14615 _("bad file number in macro information (%d)"),
14616 file);
2e276125 14617
6a83a1e6 14618 return xstrdup (fake_name);
2e276125
JB
14619 }
14620}
14621
14622
14623static struct macro_source_file *
14624macro_start_file (int file, int line,
14625 struct macro_source_file *current_file,
14626 const char *comp_dir,
14627 struct line_header *lh, struct objfile *objfile)
14628{
14629 /* The full name of this source file. */
14630 char *full_name = file_full_name (file, lh, comp_dir);
14631
14632 /* We don't create a macro table for this compilation unit
14633 at all until we actually get a filename. */
14634 if (! pending_macros)
4a146b47 14635 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14636 objfile->macro_cache);
2e276125
JB
14637
14638 if (! current_file)
14639 /* If we have no current file, then this must be the start_file
14640 directive for the compilation unit's main source file. */
14641 current_file = macro_set_main (pending_macros, full_name);
14642 else
14643 current_file = macro_include (current_file, line, full_name);
14644
14645 xfree (full_name);
6e70227d 14646
2e276125
JB
14647 return current_file;
14648}
14649
14650
14651/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14652 followed by a null byte. */
14653static char *
14654copy_string (const char *buf, int len)
14655{
14656 char *s = xmalloc (len + 1);
9a619af0 14657
2e276125
JB
14658 memcpy (s, buf, len);
14659 s[len] = '\0';
2e276125
JB
14660 return s;
14661}
14662
14663
14664static const char *
14665consume_improper_spaces (const char *p, const char *body)
14666{
14667 if (*p == ' ')
14668 {
4d3c2250 14669 complaint (&symfile_complaints,
3e43a32a
MS
14670 _("macro definition contains spaces "
14671 "in formal argument list:\n`%s'"),
4d3c2250 14672 body);
2e276125
JB
14673
14674 while (*p == ' ')
14675 p++;
14676 }
14677
14678 return p;
14679}
14680
14681
14682static void
14683parse_macro_definition (struct macro_source_file *file, int line,
14684 const char *body)
14685{
14686 const char *p;
14687
14688 /* The body string takes one of two forms. For object-like macro
14689 definitions, it should be:
14690
14691 <macro name> " " <definition>
14692
14693 For function-like macro definitions, it should be:
14694
14695 <macro name> "() " <definition>
14696 or
14697 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14698
14699 Spaces may appear only where explicitly indicated, and in the
14700 <definition>.
14701
14702 The Dwarf 2 spec says that an object-like macro's name is always
14703 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14704 the space when the macro's definition is the empty string.
2e276125
JB
14705
14706 The Dwarf 2 spec says that there should be no spaces between the
14707 formal arguments in a function-like macro's formal argument list,
14708 but versions of GCC around March 2002 include spaces after the
14709 commas. */
14710
14711
14712 /* Find the extent of the macro name. The macro name is terminated
14713 by either a space or null character (for an object-like macro) or
14714 an opening paren (for a function-like macro). */
14715 for (p = body; *p; p++)
14716 if (*p == ' ' || *p == '(')
14717 break;
14718
14719 if (*p == ' ' || *p == '\0')
14720 {
14721 /* It's an object-like macro. */
14722 int name_len = p - body;
14723 char *name = copy_string (body, name_len);
14724 const char *replacement;
14725
14726 if (*p == ' ')
14727 replacement = body + name_len + 1;
14728 else
14729 {
4d3c2250 14730 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14731 replacement = body + name_len;
14732 }
6e70227d 14733
2e276125
JB
14734 macro_define_object (file, line, name, replacement);
14735
14736 xfree (name);
14737 }
14738 else if (*p == '(')
14739 {
14740 /* It's a function-like macro. */
14741 char *name = copy_string (body, p - body);
14742 int argc = 0;
14743 int argv_size = 1;
14744 char **argv = xmalloc (argv_size * sizeof (*argv));
14745
14746 p++;
14747
14748 p = consume_improper_spaces (p, body);
14749
14750 /* Parse the formal argument list. */
14751 while (*p && *p != ')')
14752 {
14753 /* Find the extent of the current argument name. */
14754 const char *arg_start = p;
14755
14756 while (*p && *p != ',' && *p != ')' && *p != ' ')
14757 p++;
14758
14759 if (! *p || p == arg_start)
4d3c2250 14760 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14761 else
14762 {
14763 /* Make sure argv has room for the new argument. */
14764 if (argc >= argv_size)
14765 {
14766 argv_size *= 2;
14767 argv = xrealloc (argv, argv_size * sizeof (*argv));
14768 }
14769
14770 argv[argc++] = copy_string (arg_start, p - arg_start);
14771 }
14772
14773 p = consume_improper_spaces (p, body);
14774
14775 /* Consume the comma, if present. */
14776 if (*p == ',')
14777 {
14778 p++;
14779
14780 p = consume_improper_spaces (p, body);
14781 }
14782 }
14783
14784 if (*p == ')')
14785 {
14786 p++;
14787
14788 if (*p == ' ')
14789 /* Perfectly formed definition, no complaints. */
14790 macro_define_function (file, line, name,
6e70227d 14791 argc, (const char **) argv,
2e276125
JB
14792 p + 1);
14793 else if (*p == '\0')
14794 {
14795 /* Complain, but do define it. */
4d3c2250 14796 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14797 macro_define_function (file, line, name,
6e70227d 14798 argc, (const char **) argv,
2e276125
JB
14799 p);
14800 }
14801 else
14802 /* Just complain. */
4d3c2250 14803 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14804 }
14805 else
14806 /* Just complain. */
4d3c2250 14807 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14808
14809 xfree (name);
14810 {
14811 int i;
14812
14813 for (i = 0; i < argc; i++)
14814 xfree (argv[i]);
14815 }
14816 xfree (argv);
14817 }
14818 else
4d3c2250 14819 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14820}
14821
cf2c3c16
TT
14822/* Skip some bytes from BYTES according to the form given in FORM.
14823 Returns the new pointer. */
2e276125 14824
cf2c3c16
TT
14825static gdb_byte *
14826skip_form_bytes (bfd *abfd, gdb_byte *bytes,
14827 enum dwarf_form form,
14828 unsigned int offset_size,
14829 struct dwarf2_section_info *section)
2e276125 14830{
cf2c3c16 14831 unsigned int bytes_read;
2e276125 14832
cf2c3c16 14833 switch (form)
2e276125 14834 {
cf2c3c16
TT
14835 case DW_FORM_data1:
14836 case DW_FORM_flag:
14837 ++bytes;
14838 break;
14839
14840 case DW_FORM_data2:
14841 bytes += 2;
14842 break;
14843
14844 case DW_FORM_data4:
14845 bytes += 4;
14846 break;
14847
14848 case DW_FORM_data8:
14849 bytes += 8;
14850 break;
14851
14852 case DW_FORM_string:
14853 read_direct_string (abfd, bytes, &bytes_read);
14854 bytes += bytes_read;
14855 break;
14856
14857 case DW_FORM_sec_offset:
14858 case DW_FORM_strp:
14859 bytes += offset_size;
14860 break;
14861
14862 case DW_FORM_block:
14863 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
14864 bytes += bytes_read;
14865 break;
14866
14867 case DW_FORM_block1:
14868 bytes += 1 + read_1_byte (abfd, bytes);
14869 break;
14870 case DW_FORM_block2:
14871 bytes += 2 + read_2_bytes (abfd, bytes);
14872 break;
14873 case DW_FORM_block4:
14874 bytes += 4 + read_4_bytes (abfd, bytes);
14875 break;
14876
14877 case DW_FORM_sdata:
14878 case DW_FORM_udata:
14879 bytes = skip_leb128 (abfd, bytes);
14880 break;
14881
14882 default:
14883 {
14884 complain:
14885 complaint (&symfile_complaints,
14886 _("invalid form 0x%x in `%s'"),
14887 form,
14888 section->asection->name);
14889 return NULL;
14890 }
2e276125
JB
14891 }
14892
cf2c3c16
TT
14893 return bytes;
14894}
757a13d0 14895
cf2c3c16
TT
14896/* A helper for dwarf_decode_macros that handles skipping an unknown
14897 opcode. Returns an updated pointer to the macro data buffer; or,
14898 on error, issues a complaint and returns NULL. */
757a13d0 14899
cf2c3c16
TT
14900static gdb_byte *
14901skip_unknown_opcode (unsigned int opcode,
14902 gdb_byte **opcode_definitions,
14903 gdb_byte *mac_ptr,
14904 bfd *abfd,
14905 unsigned int offset_size,
14906 struct dwarf2_section_info *section)
14907{
14908 unsigned int bytes_read, i;
14909 unsigned long arg;
14910 gdb_byte *defn;
2e276125 14911
cf2c3c16 14912 if (opcode_definitions[opcode] == NULL)
2e276125 14913 {
cf2c3c16
TT
14914 complaint (&symfile_complaints,
14915 _("unrecognized DW_MACFINO opcode 0x%x"),
14916 opcode);
14917 return NULL;
14918 }
2e276125 14919
cf2c3c16
TT
14920 defn = opcode_definitions[opcode];
14921 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
14922 defn += bytes_read;
2e276125 14923
cf2c3c16
TT
14924 for (i = 0; i < arg; ++i)
14925 {
14926 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
14927 if (mac_ptr == NULL)
14928 {
14929 /* skip_form_bytes already issued the complaint. */
14930 return NULL;
14931 }
14932 }
757a13d0 14933
cf2c3c16
TT
14934 return mac_ptr;
14935}
757a13d0 14936
cf2c3c16
TT
14937/* A helper function which parses the header of a macro section.
14938 If the macro section is the extended (for now called "GNU") type,
14939 then this updates *OFFSET_SIZE. Returns a pointer to just after
14940 the header, or issues a complaint and returns NULL on error. */
757a13d0 14941
cf2c3c16
TT
14942static gdb_byte *
14943dwarf_parse_macro_header (gdb_byte **opcode_definitions,
14944 bfd *abfd,
14945 gdb_byte *mac_ptr,
14946 unsigned int *offset_size,
14947 int section_is_gnu)
14948{
14949 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 14950
cf2c3c16
TT
14951 if (section_is_gnu)
14952 {
14953 unsigned int version, flags;
757a13d0 14954
cf2c3c16
TT
14955 version = read_2_bytes (abfd, mac_ptr);
14956 if (version != 4)
14957 {
14958 complaint (&symfile_complaints,
14959 _("unrecognized version `%d' in .debug_macro section"),
14960 version);
14961 return NULL;
14962 }
14963 mac_ptr += 2;
757a13d0 14964
cf2c3c16
TT
14965 flags = read_1_byte (abfd, mac_ptr);
14966 ++mac_ptr;
14967 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 14968
cf2c3c16
TT
14969 if ((flags & 2) != 0)
14970 /* We don't need the line table offset. */
14971 mac_ptr += *offset_size;
757a13d0 14972
cf2c3c16
TT
14973 /* Vendor opcode descriptions. */
14974 if ((flags & 4) != 0)
14975 {
14976 unsigned int i, count;
757a13d0 14977
cf2c3c16
TT
14978 count = read_1_byte (abfd, mac_ptr);
14979 ++mac_ptr;
14980 for (i = 0; i < count; ++i)
14981 {
14982 unsigned int opcode, bytes_read;
14983 unsigned long arg;
14984
14985 opcode = read_1_byte (abfd, mac_ptr);
14986 ++mac_ptr;
14987 opcode_definitions[opcode] = mac_ptr;
14988 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14989 mac_ptr += bytes_read;
14990 mac_ptr += arg;
14991 }
757a13d0 14992 }
cf2c3c16 14993 }
757a13d0 14994
cf2c3c16
TT
14995 return mac_ptr;
14996}
757a13d0 14997
cf2c3c16
TT
14998/* A helper for dwarf_decode_macros that handles the GNU extensions,
14999 including DW_GNU_MACINFO_transparent_include. */
15000
15001static void
15002dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15003 struct macro_source_file *current_file,
15004 struct line_header *lh, char *comp_dir,
15005 struct dwarf2_section_info *section,
15006 int section_is_gnu,
15007 unsigned int offset_size,
15008 struct objfile *objfile)
15009{
15010 enum dwarf_macro_record_type macinfo_type;
15011 int at_commandline;
15012 gdb_byte *opcode_definitions[256];
757a13d0 15013
cf2c3c16
TT
15014 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15015 &offset_size, section_is_gnu);
15016 if (mac_ptr == NULL)
15017 {
15018 /* We already issued a complaint. */
15019 return;
15020 }
757a13d0
JK
15021
15022 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15023 GDB is still reading the definitions from command line. First
15024 DW_MACINFO_start_file will need to be ignored as it was already executed
15025 to create CURRENT_FILE for the main source holding also the command line
15026 definitions. On first met DW_MACINFO_start_file this flag is reset to
15027 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15028
15029 at_commandline = 1;
15030
15031 do
15032 {
15033 /* Do we at least have room for a macinfo type byte? */
15034 if (mac_ptr >= mac_end)
15035 {
cf2c3c16 15036 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
15037 break;
15038 }
15039
15040 macinfo_type = read_1_byte (abfd, mac_ptr);
15041 mac_ptr++;
15042
cf2c3c16
TT
15043 /* Note that we rely on the fact that the corresponding GNU and
15044 DWARF constants are the same. */
757a13d0
JK
15045 switch (macinfo_type)
15046 {
15047 /* A zero macinfo type indicates the end of the macro
15048 information. */
15049 case 0:
15050 break;
2e276125 15051
cf2c3c16
TT
15052 case DW_MACRO_GNU_define:
15053 case DW_MACRO_GNU_undef:
15054 case DW_MACRO_GNU_define_indirect:
15055 case DW_MACRO_GNU_undef_indirect:
2e276125 15056 {
891d2f0b 15057 unsigned int bytes_read;
2e276125
JB
15058 int line;
15059 char *body;
cf2c3c16 15060 int is_define;
2e276125 15061
cf2c3c16
TT
15062 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15063 mac_ptr += bytes_read;
15064
15065 if (macinfo_type == DW_MACRO_GNU_define
15066 || macinfo_type == DW_MACRO_GNU_undef)
15067 {
15068 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15069 mac_ptr += bytes_read;
15070 }
15071 else
15072 {
15073 LONGEST str_offset;
15074
15075 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15076 mac_ptr += offset_size;
2e276125 15077
cf2c3c16
TT
15078 body = read_indirect_string_at_offset (abfd, str_offset);
15079 }
15080
15081 is_define = (macinfo_type == DW_MACRO_GNU_define
15082 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 15083 if (! current_file)
757a13d0
JK
15084 {
15085 /* DWARF violation as no main source is present. */
15086 complaint (&symfile_complaints,
15087 _("debug info with no main source gives macro %s "
15088 "on line %d: %s"),
cf2c3c16
TT
15089 is_define ? _("definition") : _("undefinition"),
15090 line, body);
757a13d0
JK
15091 break;
15092 }
3e43a32a
MS
15093 if ((line == 0 && !at_commandline)
15094 || (line != 0 && at_commandline))
4d3c2250 15095 complaint (&symfile_complaints,
757a13d0
JK
15096 _("debug info gives %s macro %s with %s line %d: %s"),
15097 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 15098 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
15099 line == 0 ? _("zero") : _("non-zero"), line, body);
15100
cf2c3c16 15101 if (is_define)
757a13d0 15102 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
15103 else
15104 {
15105 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15106 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15107 macro_undef (current_file, line, body);
15108 }
2e276125
JB
15109 }
15110 break;
15111
cf2c3c16 15112 case DW_MACRO_GNU_start_file:
2e276125 15113 {
891d2f0b 15114 unsigned int bytes_read;
2e276125
JB
15115 int line, file;
15116
15117 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15118 mac_ptr += bytes_read;
15119 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15120 mac_ptr += bytes_read;
15121
3e43a32a
MS
15122 if ((line == 0 && !at_commandline)
15123 || (line != 0 && at_commandline))
757a13d0
JK
15124 complaint (&symfile_complaints,
15125 _("debug info gives source %d included "
15126 "from %s at %s line %d"),
15127 file, at_commandline ? _("command-line") : _("file"),
15128 line == 0 ? _("zero") : _("non-zero"), line);
15129
15130 if (at_commandline)
15131 {
cf2c3c16
TT
15132 /* This DW_MACRO_GNU_start_file was executed in the
15133 pass one. */
757a13d0
JK
15134 at_commandline = 0;
15135 }
15136 else
15137 current_file = macro_start_file (file, line,
15138 current_file, comp_dir,
cf2c3c16 15139 lh, objfile);
2e276125
JB
15140 }
15141 break;
15142
cf2c3c16 15143 case DW_MACRO_GNU_end_file:
2e276125 15144 if (! current_file)
4d3c2250 15145 complaint (&symfile_complaints,
3e43a32a
MS
15146 _("macro debug info has an unmatched "
15147 "`close_file' directive"));
2e276125
JB
15148 else
15149 {
15150 current_file = current_file->included_by;
15151 if (! current_file)
15152 {
cf2c3c16 15153 enum dwarf_macro_record_type next_type;
2e276125
JB
15154
15155 /* GCC circa March 2002 doesn't produce the zero
15156 type byte marking the end of the compilation
15157 unit. Complain if it's not there, but exit no
15158 matter what. */
15159
15160 /* Do we at least have room for a macinfo type byte? */
15161 if (mac_ptr >= mac_end)
15162 {
cf2c3c16 15163 dwarf2_macros_too_long_complaint (section);
2e276125
JB
15164 return;
15165 }
15166
15167 /* We don't increment mac_ptr here, so this is just
15168 a look-ahead. */
15169 next_type = read_1_byte (abfd, mac_ptr);
15170 if (next_type != 0)
4d3c2250 15171 complaint (&symfile_complaints,
3e43a32a
MS
15172 _("no terminating 0-type entry for "
15173 "macros in `.debug_macinfo' section"));
2e276125
JB
15174
15175 return;
15176 }
15177 }
15178 break;
15179
cf2c3c16
TT
15180 case DW_MACRO_GNU_transparent_include:
15181 {
15182 LONGEST offset;
15183
15184 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15185 mac_ptr += offset_size;
15186
15187 dwarf_decode_macro_bytes (abfd,
15188 section->buffer + offset,
15189 mac_end, current_file,
15190 lh, comp_dir,
15191 section, section_is_gnu,
15192 offset_size, objfile);
15193 }
15194 break;
15195
2e276125 15196 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
15197 if (!section_is_gnu)
15198 {
15199 unsigned int bytes_read;
15200 int constant;
2e276125 15201
cf2c3c16
TT
15202 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15203 mac_ptr += bytes_read;
15204 read_direct_string (abfd, mac_ptr, &bytes_read);
15205 mac_ptr += bytes_read;
2e276125 15206
cf2c3c16
TT
15207 /* We don't recognize any vendor extensions. */
15208 break;
15209 }
15210 /* FALLTHROUGH */
15211
15212 default:
15213 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15214 mac_ptr, abfd, offset_size,
15215 section);
15216 if (mac_ptr == NULL)
15217 return;
15218 break;
2e276125 15219 }
757a13d0 15220 } while (macinfo_type != 0);
2e276125 15221}
8e19ed76 15222
cf2c3c16
TT
15223static void
15224dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15225 char *comp_dir, bfd *abfd,
15226 struct dwarf2_cu *cu,
15227 struct dwarf2_section_info *section,
15228 int section_is_gnu)
15229{
15230 gdb_byte *mac_ptr, *mac_end;
15231 struct macro_source_file *current_file = 0;
15232 enum dwarf_macro_record_type macinfo_type;
15233 unsigned int offset_size = cu->header.offset_size;
15234 gdb_byte *opcode_definitions[256];
15235
15236 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15237 if (section->buffer == NULL)
15238 {
15239 complaint (&symfile_complaints, _("missing %s section"),
15240 section->asection->name);
15241 return;
15242 }
15243
15244 /* First pass: Find the name of the base filename.
15245 This filename is needed in order to process all macros whose definition
15246 (or undefinition) comes from the command line. These macros are defined
15247 before the first DW_MACINFO_start_file entry, and yet still need to be
15248 associated to the base file.
15249
15250 To determine the base file name, we scan the macro definitions until we
15251 reach the first DW_MACINFO_start_file entry. We then initialize
15252 CURRENT_FILE accordingly so that any macro definition found before the
15253 first DW_MACINFO_start_file can still be associated to the base file. */
15254
15255 mac_ptr = section->buffer + offset;
15256 mac_end = section->buffer + section->size;
15257
15258 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15259 &offset_size, section_is_gnu);
15260 if (mac_ptr == NULL)
15261 {
15262 /* We already issued a complaint. */
15263 return;
15264 }
15265
15266 do
15267 {
15268 /* Do we at least have room for a macinfo type byte? */
15269 if (mac_ptr >= mac_end)
15270 {
15271 /* Complaint is printed during the second pass as GDB will probably
15272 stop the first pass earlier upon finding
15273 DW_MACINFO_start_file. */
15274 break;
15275 }
15276
15277 macinfo_type = read_1_byte (abfd, mac_ptr);
15278 mac_ptr++;
15279
15280 /* Note that we rely on the fact that the corresponding GNU and
15281 DWARF constants are the same. */
15282 switch (macinfo_type)
15283 {
15284 /* A zero macinfo type indicates the end of the macro
15285 information. */
15286 case 0:
15287 break;
15288
15289 case DW_MACRO_GNU_define:
15290 case DW_MACRO_GNU_undef:
15291 /* Only skip the data by MAC_PTR. */
15292 {
15293 unsigned int bytes_read;
15294
15295 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15296 mac_ptr += bytes_read;
15297 read_direct_string (abfd, mac_ptr, &bytes_read);
15298 mac_ptr += bytes_read;
15299 }
15300 break;
15301
15302 case DW_MACRO_GNU_start_file:
15303 {
15304 unsigned int bytes_read;
15305 int line, file;
15306
15307 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15308 mac_ptr += bytes_read;
15309 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15310 mac_ptr += bytes_read;
15311
15312 current_file = macro_start_file (file, line, current_file,
15313 comp_dir, lh, cu->objfile);
15314 }
15315 break;
15316
15317 case DW_MACRO_GNU_end_file:
15318 /* No data to skip by MAC_PTR. */
15319 break;
15320
15321 case DW_MACRO_GNU_define_indirect:
15322 case DW_MACRO_GNU_undef_indirect:
15323 {
15324 unsigned int bytes_read;
15325
15326 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15327 mac_ptr += bytes_read;
15328 mac_ptr += offset_size;
15329 }
15330 break;
15331
15332 case DW_MACRO_GNU_transparent_include:
15333 /* Note that, according to the spec, a transparent include
15334 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15335 skip this opcode. */
15336 mac_ptr += offset_size;
15337 break;
15338
15339 case DW_MACINFO_vendor_ext:
15340 /* Only skip the data by MAC_PTR. */
15341 if (!section_is_gnu)
15342 {
15343 unsigned int bytes_read;
15344
15345 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15346 mac_ptr += bytes_read;
15347 read_direct_string (abfd, mac_ptr, &bytes_read);
15348 mac_ptr += bytes_read;
15349 }
15350 /* FALLTHROUGH */
15351
15352 default:
15353 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15354 mac_ptr, abfd, offset_size,
15355 section);
15356 if (mac_ptr == NULL)
15357 return;
15358 break;
15359 }
15360 } while (macinfo_type != 0 && current_file == NULL);
15361
15362 /* Second pass: Process all entries.
15363
15364 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15365 command-line macro definitions/undefinitions. This flag is unset when we
15366 reach the first DW_MACINFO_start_file entry. */
15367
15368 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15369 current_file, lh, comp_dir, section, section_is_gnu,
15370 offset_size, cu->objfile);
15371}
15372
8e19ed76 15373/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 15374 if so return true else false. */
8e19ed76
PS
15375static int
15376attr_form_is_block (struct attribute *attr)
15377{
15378 return (attr == NULL ? 0 :
15379 attr->form == DW_FORM_block1
15380 || attr->form == DW_FORM_block2
15381 || attr->form == DW_FORM_block4
2dc7f7b3
TT
15382 || attr->form == DW_FORM_block
15383 || attr->form == DW_FORM_exprloc);
8e19ed76 15384}
4c2df51b 15385
c6a0999f
JB
15386/* Return non-zero if ATTR's value is a section offset --- classes
15387 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15388 You may use DW_UNSND (attr) to retrieve such offsets.
15389
15390 Section 7.5.4, "Attribute Encodings", explains that no attribute
15391 may have a value that belongs to more than one of these classes; it
15392 would be ambiguous if we did, because we use the same forms for all
15393 of them. */
3690dd37
JB
15394static int
15395attr_form_is_section_offset (struct attribute *attr)
15396{
15397 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
15398 || attr->form == DW_FORM_data8
15399 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
15400}
15401
15402
15403/* Return non-zero if ATTR's value falls in the 'constant' class, or
15404 zero otherwise. When this function returns true, you can apply
15405 dwarf2_get_attr_constant_value to it.
15406
15407 However, note that for some attributes you must check
15408 attr_form_is_section_offset before using this test. DW_FORM_data4
15409 and DW_FORM_data8 are members of both the constant class, and of
15410 the classes that contain offsets into other debug sections
15411 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15412 that, if an attribute's can be either a constant or one of the
15413 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15414 taken as section offsets, not constants. */
15415static int
15416attr_form_is_constant (struct attribute *attr)
15417{
15418 switch (attr->form)
15419 {
15420 case DW_FORM_sdata:
15421 case DW_FORM_udata:
15422 case DW_FORM_data1:
15423 case DW_FORM_data2:
15424 case DW_FORM_data4:
15425 case DW_FORM_data8:
15426 return 1;
15427 default:
15428 return 0;
15429 }
15430}
15431
8cf6f0b1
TT
15432/* A helper function that fills in a dwarf2_loclist_baton. */
15433
15434static void
15435fill_in_loclist_baton (struct dwarf2_cu *cu,
15436 struct dwarf2_loclist_baton *baton,
15437 struct attribute *attr)
15438{
15439 dwarf2_read_section (dwarf2_per_objfile->objfile,
15440 &dwarf2_per_objfile->loc);
15441
15442 baton->per_cu = cu->per_cu;
15443 gdb_assert (baton->per_cu);
15444 /* We don't know how long the location list is, but make sure we
15445 don't run off the edge of the section. */
15446 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15447 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15448 baton->base_address = cu->base_address;
15449}
15450
4c2df51b
DJ
15451static void
15452dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 15453 struct dwarf2_cu *cu)
4c2df51b 15454{
3690dd37 15455 if (attr_form_is_section_offset (attr)
99bcc461
DJ
15456 /* ".debug_loc" may not exist at all, or the offset may be outside
15457 the section. If so, fall through to the complaint in the
15458 other branch. */
9e0ac564
TT
15459 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15460 &dwarf2_per_objfile->loc))
4c2df51b 15461 {
0d53c4c4 15462 struct dwarf2_loclist_baton *baton;
4c2df51b 15463
4a146b47 15464 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 15465 sizeof (struct dwarf2_loclist_baton));
4c2df51b 15466
8cf6f0b1 15467 fill_in_loclist_baton (cu, baton, attr);
be391dca 15468
d00adf39 15469 if (cu->base_known == 0)
0d53c4c4 15470 complaint (&symfile_complaints,
3e43a32a
MS
15471 _("Location list used without "
15472 "specifying the CU base address."));
4c2df51b 15473
768a979c 15474 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
15475 SYMBOL_LOCATION_BATON (sym) = baton;
15476 }
15477 else
15478 {
15479 struct dwarf2_locexpr_baton *baton;
15480
4a146b47 15481 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 15482 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
15483 baton->per_cu = cu->per_cu;
15484 gdb_assert (baton->per_cu);
0d53c4c4
DJ
15485
15486 if (attr_form_is_block (attr))
15487 {
15488 /* Note that we're just copying the block's data pointer
15489 here, not the actual data. We're still pointing into the
6502dd73
DJ
15490 info_buffer for SYM's objfile; right now we never release
15491 that buffer, but when we do clean up properly this may
15492 need to change. */
0d53c4c4
DJ
15493 baton->size = DW_BLOCK (attr)->size;
15494 baton->data = DW_BLOCK (attr)->data;
15495 }
15496 else
15497 {
15498 dwarf2_invalid_attrib_class_complaint ("location description",
15499 SYMBOL_NATURAL_NAME (sym));
15500 baton->size = 0;
0d53c4c4 15501 }
6e70227d 15502
768a979c 15503 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
15504 SYMBOL_LOCATION_BATON (sym) = baton;
15505 }
4c2df51b 15506}
6502dd73 15507
9aa1f1e3
TT
15508/* Return the OBJFILE associated with the compilation unit CU. If CU
15509 came from a separate debuginfo file, then the master objfile is
15510 returned. */
ae0d2f24
UW
15511
15512struct objfile *
15513dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15514{
9291a0cd 15515 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15516
15517 /* Return the master objfile, so that we can report and look up the
15518 correct file containing this variable. */
15519 if (objfile->separate_debug_objfile_backlink)
15520 objfile = objfile->separate_debug_objfile_backlink;
15521
15522 return objfile;
15523}
15524
96408a79
SA
15525/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15526 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15527 CU_HEADERP first. */
15528
15529static const struct comp_unit_head *
15530per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15531 struct dwarf2_per_cu_data *per_cu)
15532{
15533 struct objfile *objfile;
15534 struct dwarf2_per_objfile *per_objfile;
15535 gdb_byte *info_ptr;
15536
15537 if (per_cu->cu)
15538 return &per_cu->cu->header;
15539
15540 objfile = per_cu->objfile;
15541 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15542 info_ptr = per_objfile->info.buffer + per_cu->offset;
15543
15544 memset (cu_headerp, 0, sizeof (*cu_headerp));
15545 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15546
15547 return cu_headerp;
15548}
15549
ae0d2f24
UW
15550/* Return the address size given in the compilation unit header for CU. */
15551
15552CORE_ADDR
15553dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15554{
96408a79
SA
15555 struct comp_unit_head cu_header_local;
15556 const struct comp_unit_head *cu_headerp;
c471e790 15557
96408a79
SA
15558 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15559
15560 return cu_headerp->addr_size;
ae0d2f24
UW
15561}
15562
9eae7c52
TT
15563/* Return the offset size given in the compilation unit header for CU. */
15564
15565int
15566dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15567{
96408a79
SA
15568 struct comp_unit_head cu_header_local;
15569 const struct comp_unit_head *cu_headerp;
9c6c53f7 15570
96408a79
SA
15571 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15572
15573 return cu_headerp->offset_size;
15574}
15575
15576/* See its dwarf2loc.h declaration. */
15577
15578int
15579dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15580{
15581 struct comp_unit_head cu_header_local;
15582 const struct comp_unit_head *cu_headerp;
15583
15584 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15585
15586 if (cu_headerp->version == 2)
15587 return cu_headerp->addr_size;
15588 else
15589 return cu_headerp->offset_size;
181cebd4
JK
15590}
15591
9aa1f1e3
TT
15592/* Return the text offset of the CU. The returned offset comes from
15593 this CU's objfile. If this objfile came from a separate debuginfo
15594 file, then the offset may be different from the corresponding
15595 offset in the parent objfile. */
15596
15597CORE_ADDR
15598dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15599{
bb3fa9d0 15600 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
15601
15602 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15603}
15604
348e048f
DE
15605/* Locate the .debug_info compilation unit from CU's objfile which contains
15606 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
15607
15608static struct dwarf2_per_cu_data *
c764a876 15609dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
15610 struct objfile *objfile)
15611{
15612 struct dwarf2_per_cu_data *this_cu;
15613 int low, high;
15614
ae038cb0
DJ
15615 low = 0;
15616 high = dwarf2_per_objfile->n_comp_units - 1;
15617 while (high > low)
15618 {
15619 int mid = low + (high - low) / 2;
9a619af0 15620
ae038cb0
DJ
15621 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15622 high = mid;
15623 else
15624 low = mid + 1;
15625 }
15626 gdb_assert (low == high);
15627 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15628 {
10b3939b 15629 if (low == 0)
8a3fe4f8
AC
15630 error (_("Dwarf Error: could not find partial DIE containing "
15631 "offset 0x%lx [in module %s]"),
10b3939b
DJ
15632 (long) offset, bfd_get_filename (objfile->obfd));
15633
ae038cb0
DJ
15634 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15635 return dwarf2_per_objfile->all_comp_units[low-1];
15636 }
15637 else
15638 {
15639 this_cu = dwarf2_per_objfile->all_comp_units[low];
15640 if (low == dwarf2_per_objfile->n_comp_units - 1
15641 && offset >= this_cu->offset + this_cu->length)
c764a876 15642 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
15643 gdb_assert (offset < this_cu->offset + this_cu->length);
15644 return this_cu;
15645 }
15646}
15647
10b3939b
DJ
15648/* Locate the compilation unit from OBJFILE which is located at exactly
15649 OFFSET. Raises an error on failure. */
15650
ae038cb0 15651static struct dwarf2_per_cu_data *
c764a876 15652dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
15653{
15654 struct dwarf2_per_cu_data *this_cu;
9a619af0 15655
ae038cb0
DJ
15656 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15657 if (this_cu->offset != offset)
c764a876 15658 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
15659 return this_cu;
15660}
15661
9816fde3 15662/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 15663
9816fde3
JK
15664static void
15665init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 15666{
9816fde3 15667 memset (cu, 0, sizeof (*cu));
93311388
DE
15668 cu->objfile = objfile;
15669 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15670}
15671
15672/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15673
15674static void
15675prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15676{
15677 struct attribute *attr;
15678
15679 /* Set the language we're debugging. */
15680 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15681 if (attr)
15682 set_cu_language (DW_UNSND (attr), cu);
15683 else
9cded63f
TT
15684 {
15685 cu->language = language_minimal;
15686 cu->language_defn = language_def (cu->language);
15687 }
93311388
DE
15688}
15689
ae038cb0
DJ
15690/* Release one cached compilation unit, CU. We unlink it from the tree
15691 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15692 the caller is responsible for that.
15693 NOTE: DATA is a void * because this function is also used as a
15694 cleanup routine. */
ae038cb0
DJ
15695
15696static void
15697free_one_comp_unit (void *data)
15698{
15699 struct dwarf2_cu *cu = data;
15700
15701 if (cu->per_cu != NULL)
15702 cu->per_cu->cu = NULL;
15703 cu->per_cu = NULL;
15704
15705 obstack_free (&cu->comp_unit_obstack, NULL);
15706
15707 xfree (cu);
15708}
15709
72bf9492 15710/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
15711 when we're finished with it. We can't free the pointer itself, but be
15712 sure to unlink it from the cache. Also release any associated storage
15713 and perform cache maintenance.
72bf9492
DJ
15714
15715 Only used during partial symbol parsing. */
15716
15717static void
15718free_stack_comp_unit (void *data)
15719{
15720 struct dwarf2_cu *cu = data;
15721
15722 obstack_free (&cu->comp_unit_obstack, NULL);
15723 cu->partial_dies = NULL;
ae038cb0
DJ
15724
15725 if (cu->per_cu != NULL)
15726 {
15727 /* This compilation unit is on the stack in our caller, so we
15728 should not xfree it. Just unlink it. */
15729 cu->per_cu->cu = NULL;
15730 cu->per_cu = NULL;
15731
15732 /* If we had a per-cu pointer, then we may have other compilation
15733 units loaded, so age them now. */
15734 age_cached_comp_units ();
15735 }
15736}
15737
15738/* Free all cached compilation units. */
15739
15740static void
15741free_cached_comp_units (void *data)
15742{
15743 struct dwarf2_per_cu_data *per_cu, **last_chain;
15744
15745 per_cu = dwarf2_per_objfile->read_in_chain;
15746 last_chain = &dwarf2_per_objfile->read_in_chain;
15747 while (per_cu != NULL)
15748 {
15749 struct dwarf2_per_cu_data *next_cu;
15750
15751 next_cu = per_cu->cu->read_in_chain;
15752
15753 free_one_comp_unit (per_cu->cu);
15754 *last_chain = next_cu;
15755
15756 per_cu = next_cu;
15757 }
15758}
15759
15760/* Increase the age counter on each cached compilation unit, and free
15761 any that are too old. */
15762
15763static void
15764age_cached_comp_units (void)
15765{
15766 struct dwarf2_per_cu_data *per_cu, **last_chain;
15767
15768 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15769 per_cu = dwarf2_per_objfile->read_in_chain;
15770 while (per_cu != NULL)
15771 {
15772 per_cu->cu->last_used ++;
15773 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15774 dwarf2_mark (per_cu->cu);
15775 per_cu = per_cu->cu->read_in_chain;
15776 }
15777
15778 per_cu = dwarf2_per_objfile->read_in_chain;
15779 last_chain = &dwarf2_per_objfile->read_in_chain;
15780 while (per_cu != NULL)
15781 {
15782 struct dwarf2_per_cu_data *next_cu;
15783
15784 next_cu = per_cu->cu->read_in_chain;
15785
15786 if (!per_cu->cu->mark)
15787 {
15788 free_one_comp_unit (per_cu->cu);
15789 *last_chain = next_cu;
15790 }
15791 else
15792 last_chain = &per_cu->cu->read_in_chain;
15793
15794 per_cu = next_cu;
15795 }
15796}
15797
15798/* Remove a single compilation unit from the cache. */
15799
15800static void
15801free_one_cached_comp_unit (void *target_cu)
15802{
15803 struct dwarf2_per_cu_data *per_cu, **last_chain;
15804
15805 per_cu = dwarf2_per_objfile->read_in_chain;
15806 last_chain = &dwarf2_per_objfile->read_in_chain;
15807 while (per_cu != NULL)
15808 {
15809 struct dwarf2_per_cu_data *next_cu;
15810
15811 next_cu = per_cu->cu->read_in_chain;
15812
15813 if (per_cu->cu == target_cu)
15814 {
15815 free_one_comp_unit (per_cu->cu);
15816 *last_chain = next_cu;
15817 break;
15818 }
15819 else
15820 last_chain = &per_cu->cu->read_in_chain;
15821
15822 per_cu = next_cu;
15823 }
15824}
15825
fe3e1990
DJ
15826/* Release all extra memory associated with OBJFILE. */
15827
15828void
15829dwarf2_free_objfile (struct objfile *objfile)
15830{
15831 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15832
15833 if (dwarf2_per_objfile == NULL)
15834 return;
15835
15836 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15837 free_cached_comp_units (NULL);
15838
7b9f3c50
DE
15839 if (dwarf2_per_objfile->quick_file_names_table)
15840 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 15841
fe3e1990
DJ
15842 /* Everything else should be on the objfile obstack. */
15843}
15844
1c379e20
DJ
15845/* A pair of DIE offset and GDB type pointer. We store these
15846 in a hash table separate from the DIEs, and preserve them
15847 when the DIEs are flushed out of cache. */
15848
15849struct dwarf2_offset_and_type
15850{
15851 unsigned int offset;
15852 struct type *type;
15853};
15854
15855/* Hash function for a dwarf2_offset_and_type. */
15856
15857static hashval_t
15858offset_and_type_hash (const void *item)
15859{
15860 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 15861
1c379e20
DJ
15862 return ofs->offset;
15863}
15864
15865/* Equality function for a dwarf2_offset_and_type. */
15866
15867static int
15868offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15869{
15870 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15871 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 15872
1c379e20
DJ
15873 return ofs_lhs->offset == ofs_rhs->offset;
15874}
15875
15876/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
15877 table if necessary. For convenience, return TYPE.
15878
15879 The DIEs reading must have careful ordering to:
15880 * Not cause infite loops trying to read in DIEs as a prerequisite for
15881 reading current DIE.
15882 * Not trying to dereference contents of still incompletely read in types
15883 while reading in other DIEs.
15884 * Enable referencing still incompletely read in types just by a pointer to
15885 the type without accessing its fields.
15886
15887 Therefore caller should follow these rules:
15888 * Try to fetch any prerequisite types we may need to build this DIE type
15889 before building the type and calling set_die_type.
e71ec853 15890 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
15891 possible before fetching more types to complete the current type.
15892 * Make the type as complete as possible before fetching more types. */
1c379e20 15893
f792889a 15894static struct type *
1c379e20
DJ
15895set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15896{
15897 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
15898 struct objfile *objfile = cu->objfile;
15899 htab_t *type_hash_ptr;
1c379e20 15900
b4ba55a1
JB
15901 /* For Ada types, make sure that the gnat-specific data is always
15902 initialized (if not already set). There are a few types where
15903 we should not be doing so, because the type-specific area is
15904 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15905 where the type-specific area is used to store the floatformat).
15906 But this is not a problem, because the gnat-specific information
15907 is actually not needed for these types. */
15908 if (need_gnat_info (cu)
15909 && TYPE_CODE (type) != TYPE_CODE_FUNC
15910 && TYPE_CODE (type) != TYPE_CODE_FLT
15911 && !HAVE_GNAT_AUX_INFO (type))
15912 INIT_GNAT_SPECIFIC (type);
15913
8b70b953 15914 if (cu->per_cu->debug_type_section)
673bfd45
DE
15915 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15916 else
15917 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15918
15919 if (*type_hash_ptr == NULL)
f792889a 15920 {
673bfd45
DE
15921 *type_hash_ptr
15922 = htab_create_alloc_ex (127,
f792889a
DJ
15923 offset_and_type_hash,
15924 offset_and_type_eq,
15925 NULL,
673bfd45 15926 &objfile->objfile_obstack,
f792889a
DJ
15927 hashtab_obstack_allocate,
15928 dummy_obstack_deallocate);
f792889a 15929 }
1c379e20
DJ
15930
15931 ofs.offset = die->offset;
15932 ofs.type = type;
15933 slot = (struct dwarf2_offset_and_type **)
673bfd45 15934 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
15935 if (*slot)
15936 complaint (&symfile_complaints,
15937 _("A problem internal to GDB: DIE 0x%x has type already set"),
15938 die->offset);
673bfd45 15939 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 15940 **slot = ofs;
f792889a 15941 return type;
1c379e20
DJ
15942}
15943
673bfd45
DE
15944/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15945 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
15946
15947static struct type *
673bfd45
DE
15948get_die_type_at_offset (unsigned int offset,
15949 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
15950{
15951 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 15952 htab_t type_hash;
f792889a 15953
8b70b953 15954 if (per_cu->debug_type_section)
673bfd45
DE
15955 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15956 else
15957 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
15958 if (type_hash == NULL)
15959 return NULL;
1c379e20 15960
673bfd45 15961 ofs.offset = offset;
1c379e20
DJ
15962 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15963 if (slot)
15964 return slot->type;
15965 else
15966 return NULL;
15967}
15968
673bfd45
DE
15969/* Look up the type for DIE in the appropriate type_hash table,
15970 or return NULL if DIE does not have a saved type. */
15971
15972static struct type *
15973get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15974{
15975 return get_die_type_at_offset (die->offset, cu->per_cu);
15976}
15977
10b3939b
DJ
15978/* Add a dependence relationship from CU to REF_PER_CU. */
15979
15980static void
15981dwarf2_add_dependence (struct dwarf2_cu *cu,
15982 struct dwarf2_per_cu_data *ref_per_cu)
15983{
15984 void **slot;
15985
15986 if (cu->dependencies == NULL)
15987 cu->dependencies
15988 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15989 NULL, &cu->comp_unit_obstack,
15990 hashtab_obstack_allocate,
15991 dummy_obstack_deallocate);
15992
15993 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15994 if (*slot == NULL)
15995 *slot = ref_per_cu;
15996}
1c379e20 15997
f504f079
DE
15998/* Subroutine of dwarf2_mark to pass to htab_traverse.
15999 Set the mark field in every compilation unit in the
ae038cb0
DJ
16000 cache that we must keep because we are keeping CU. */
16001
10b3939b
DJ
16002static int
16003dwarf2_mark_helper (void **slot, void *data)
16004{
16005 struct dwarf2_per_cu_data *per_cu;
16006
16007 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16008
16009 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16010 reading of the chain. As such dependencies remain valid it is not much
16011 useful to track and undo them during QUIT cleanups. */
16012 if (per_cu->cu == NULL)
16013 return 1;
16014
10b3939b
DJ
16015 if (per_cu->cu->mark)
16016 return 1;
16017 per_cu->cu->mark = 1;
16018
16019 if (per_cu->cu->dependencies != NULL)
16020 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16021
16022 return 1;
16023}
16024
f504f079
DE
16025/* Set the mark field in CU and in every other compilation unit in the
16026 cache that we must keep because we are keeping CU. */
16027
ae038cb0
DJ
16028static void
16029dwarf2_mark (struct dwarf2_cu *cu)
16030{
16031 if (cu->mark)
16032 return;
16033 cu->mark = 1;
10b3939b
DJ
16034 if (cu->dependencies != NULL)
16035 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
16036}
16037
16038static void
16039dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16040{
16041 while (per_cu)
16042 {
16043 per_cu->cu->mark = 0;
16044 per_cu = per_cu->cu->read_in_chain;
16045 }
72bf9492
DJ
16046}
16047
72bf9492
DJ
16048/* Trivial hash function for partial_die_info: the hash value of a DIE
16049 is its offset in .debug_info for this objfile. */
16050
16051static hashval_t
16052partial_die_hash (const void *item)
16053{
16054 const struct partial_die_info *part_die = item;
9a619af0 16055
72bf9492
DJ
16056 return part_die->offset;
16057}
16058
16059/* Trivial comparison function for partial_die_info structures: two DIEs
16060 are equal if they have the same offset. */
16061
16062static int
16063partial_die_eq (const void *item_lhs, const void *item_rhs)
16064{
16065 const struct partial_die_info *part_die_lhs = item_lhs;
16066 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 16067
72bf9492
DJ
16068 return part_die_lhs->offset == part_die_rhs->offset;
16069}
16070
ae038cb0
DJ
16071static struct cmd_list_element *set_dwarf2_cmdlist;
16072static struct cmd_list_element *show_dwarf2_cmdlist;
16073
16074static void
16075set_dwarf2_cmd (char *args, int from_tty)
16076{
16077 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16078}
16079
16080static void
16081show_dwarf2_cmd (char *args, int from_tty)
6e70227d 16082{
ae038cb0
DJ
16083 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16084}
16085
dce234bc
PP
16086/* If section described by INFO was mmapped, munmap it now. */
16087
16088static void
16089munmap_section_buffer (struct dwarf2_section_info *info)
16090{
b315ab21 16091 if (info->map_addr != NULL)
dce234bc
PP
16092 {
16093#ifdef HAVE_MMAP
b315ab21 16094 int res;
9a619af0 16095
b315ab21
TG
16096 res = munmap (info->map_addr, info->map_len);
16097 gdb_assert (res == 0);
dce234bc
PP
16098#else
16099 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 16100 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
16101#endif
16102 }
16103}
16104
16105/* munmap debug sections for OBJFILE, if necessary. */
16106
16107static void
c1bd65d0 16108dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
16109{
16110 struct dwarf2_per_objfile *data = d;
8b70b953
TT
16111 int ix;
16112 struct dwarf2_section_info *section;
9a619af0 16113
16be1145
DE
16114 /* This is sorted according to the order they're defined in to make it easier
16115 to keep in sync. */
dce234bc
PP
16116 munmap_section_buffer (&data->info);
16117 munmap_section_buffer (&data->abbrev);
16118 munmap_section_buffer (&data->line);
16be1145 16119 munmap_section_buffer (&data->loc);
dce234bc 16120 munmap_section_buffer (&data->macinfo);
cf2c3c16 16121 munmap_section_buffer (&data->macro);
16be1145 16122 munmap_section_buffer (&data->str);
dce234bc 16123 munmap_section_buffer (&data->ranges);
dce234bc
PP
16124 munmap_section_buffer (&data->frame);
16125 munmap_section_buffer (&data->eh_frame);
9291a0cd 16126 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
16127
16128 for (ix = 0;
16129 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16130 ++ix)
16131 munmap_section_buffer (section);
16132
16133 VEC_free (dwarf2_section_info_def, data->types);
9291a0cd
TT
16134}
16135
16136\f
ae2de4f8 16137/* The "save gdb-index" command. */
9291a0cd
TT
16138
16139/* The contents of the hash table we create when building the string
16140 table. */
16141struct strtab_entry
16142{
16143 offset_type offset;
16144 const char *str;
16145};
16146
559a7a62
JK
16147/* Hash function for a strtab_entry.
16148
16149 Function is used only during write_hash_table so no index format backward
16150 compatibility is needed. */
b89be57b 16151
9291a0cd
TT
16152static hashval_t
16153hash_strtab_entry (const void *e)
16154{
16155 const struct strtab_entry *entry = e;
559a7a62 16156 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
16157}
16158
16159/* Equality function for a strtab_entry. */
b89be57b 16160
9291a0cd
TT
16161static int
16162eq_strtab_entry (const void *a, const void *b)
16163{
16164 const struct strtab_entry *ea = a;
16165 const struct strtab_entry *eb = b;
16166 return !strcmp (ea->str, eb->str);
16167}
16168
16169/* Create a strtab_entry hash table. */
b89be57b 16170
9291a0cd
TT
16171static htab_t
16172create_strtab (void)
16173{
16174 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16175 xfree, xcalloc, xfree);
16176}
16177
16178/* Add a string to the constant pool. Return the string's offset in
16179 host order. */
b89be57b 16180
9291a0cd
TT
16181static offset_type
16182add_string (htab_t table, struct obstack *cpool, const char *str)
16183{
16184 void **slot;
16185 struct strtab_entry entry;
16186 struct strtab_entry *result;
16187
16188 entry.str = str;
16189 slot = htab_find_slot (table, &entry, INSERT);
16190 if (*slot)
16191 result = *slot;
16192 else
16193 {
16194 result = XNEW (struct strtab_entry);
16195 result->offset = obstack_object_size (cpool);
16196 result->str = str;
16197 obstack_grow_str0 (cpool, str);
16198 *slot = result;
16199 }
16200 return result->offset;
16201}
16202
16203/* An entry in the symbol table. */
16204struct symtab_index_entry
16205{
16206 /* The name of the symbol. */
16207 const char *name;
16208 /* The offset of the name in the constant pool. */
16209 offset_type index_offset;
16210 /* A sorted vector of the indices of all the CUs that hold an object
16211 of this name. */
16212 VEC (offset_type) *cu_indices;
16213};
16214
16215/* The symbol table. This is a power-of-2-sized hash table. */
16216struct mapped_symtab
16217{
16218 offset_type n_elements;
16219 offset_type size;
16220 struct symtab_index_entry **data;
16221};
16222
16223/* Hash function for a symtab_index_entry. */
b89be57b 16224
9291a0cd
TT
16225static hashval_t
16226hash_symtab_entry (const void *e)
16227{
16228 const struct symtab_index_entry *entry = e;
16229 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16230 sizeof (offset_type) * VEC_length (offset_type,
16231 entry->cu_indices),
16232 0);
16233}
16234
16235/* Equality function for a symtab_index_entry. */
b89be57b 16236
9291a0cd
TT
16237static int
16238eq_symtab_entry (const void *a, const void *b)
16239{
16240 const struct symtab_index_entry *ea = a;
16241 const struct symtab_index_entry *eb = b;
16242 int len = VEC_length (offset_type, ea->cu_indices);
16243 if (len != VEC_length (offset_type, eb->cu_indices))
16244 return 0;
16245 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16246 VEC_address (offset_type, eb->cu_indices),
16247 sizeof (offset_type) * len);
16248}
16249
16250/* Destroy a symtab_index_entry. */
b89be57b 16251
9291a0cd
TT
16252static void
16253delete_symtab_entry (void *p)
16254{
16255 struct symtab_index_entry *entry = p;
16256 VEC_free (offset_type, entry->cu_indices);
16257 xfree (entry);
16258}
16259
16260/* Create a hash table holding symtab_index_entry objects. */
b89be57b 16261
9291a0cd 16262static htab_t
3876f04e 16263create_symbol_hash_table (void)
9291a0cd
TT
16264{
16265 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16266 delete_symtab_entry, xcalloc, xfree);
16267}
16268
16269/* Create a new mapped symtab object. */
b89be57b 16270
9291a0cd
TT
16271static struct mapped_symtab *
16272create_mapped_symtab (void)
16273{
16274 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16275 symtab->n_elements = 0;
16276 symtab->size = 1024;
16277 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16278 return symtab;
16279}
16280
16281/* Destroy a mapped_symtab. */
b89be57b 16282
9291a0cd
TT
16283static void
16284cleanup_mapped_symtab (void *p)
16285{
16286 struct mapped_symtab *symtab = p;
16287 /* The contents of the array are freed when the other hash table is
16288 destroyed. */
16289 xfree (symtab->data);
16290 xfree (symtab);
16291}
16292
16293/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
16294 the slot.
16295
16296 Function is used only during write_hash_table so no index format backward
16297 compatibility is needed. */
b89be57b 16298
9291a0cd
TT
16299static struct symtab_index_entry **
16300find_slot (struct mapped_symtab *symtab, const char *name)
16301{
559a7a62 16302 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
16303
16304 index = hash & (symtab->size - 1);
16305 step = ((hash * 17) & (symtab->size - 1)) | 1;
16306
16307 for (;;)
16308 {
16309 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16310 return &symtab->data[index];
16311 index = (index + step) & (symtab->size - 1);
16312 }
16313}
16314
16315/* Expand SYMTAB's hash table. */
b89be57b 16316
9291a0cd
TT
16317static void
16318hash_expand (struct mapped_symtab *symtab)
16319{
16320 offset_type old_size = symtab->size;
16321 offset_type i;
16322 struct symtab_index_entry **old_entries = symtab->data;
16323
16324 symtab->size *= 2;
16325 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16326
16327 for (i = 0; i < old_size; ++i)
16328 {
16329 if (old_entries[i])
16330 {
16331 struct symtab_index_entry **slot = find_slot (symtab,
16332 old_entries[i]->name);
16333 *slot = old_entries[i];
16334 }
16335 }
16336
16337 xfree (old_entries);
16338}
16339
16340/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16341 is the index of the CU in which the symbol appears. */
b89be57b 16342
9291a0cd
TT
16343static void
16344add_index_entry (struct mapped_symtab *symtab, const char *name,
16345 offset_type cu_index)
16346{
16347 struct symtab_index_entry **slot;
16348
16349 ++symtab->n_elements;
16350 if (4 * symtab->n_elements / 3 >= symtab->size)
16351 hash_expand (symtab);
16352
16353 slot = find_slot (symtab, name);
16354 if (!*slot)
16355 {
16356 *slot = XNEW (struct symtab_index_entry);
16357 (*slot)->name = name;
16358 (*slot)->cu_indices = NULL;
16359 }
16360 /* Don't push an index twice. Due to how we add entries we only
16361 have to check the last one. */
16362 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 16363 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
16364 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16365}
16366
16367/* Add a vector of indices to the constant pool. */
b89be57b 16368
9291a0cd 16369static offset_type
3876f04e 16370add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
16371 struct symtab_index_entry *entry)
16372{
16373 void **slot;
16374
3876f04e 16375 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
16376 if (!*slot)
16377 {
16378 offset_type len = VEC_length (offset_type, entry->cu_indices);
16379 offset_type val = MAYBE_SWAP (len);
16380 offset_type iter;
16381 int i;
16382
16383 *slot = entry;
16384 entry->index_offset = obstack_object_size (cpool);
16385
16386 obstack_grow (cpool, &val, sizeof (val));
16387 for (i = 0;
16388 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16389 ++i)
16390 {
16391 val = MAYBE_SWAP (iter);
16392 obstack_grow (cpool, &val, sizeof (val));
16393 }
16394 }
16395 else
16396 {
16397 struct symtab_index_entry *old_entry = *slot;
16398 entry->index_offset = old_entry->index_offset;
16399 entry = old_entry;
16400 }
16401 return entry->index_offset;
16402}
16403
16404/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16405 constant pool entries going into the obstack CPOOL. */
b89be57b 16406
9291a0cd
TT
16407static void
16408write_hash_table (struct mapped_symtab *symtab,
16409 struct obstack *output, struct obstack *cpool)
16410{
16411 offset_type i;
3876f04e 16412 htab_t symbol_hash_table;
9291a0cd
TT
16413 htab_t str_table;
16414
3876f04e 16415 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 16416 str_table = create_strtab ();
3876f04e 16417
9291a0cd
TT
16418 /* We add all the index vectors to the constant pool first, to
16419 ensure alignment is ok. */
16420 for (i = 0; i < symtab->size; ++i)
16421 {
16422 if (symtab->data[i])
3876f04e 16423 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
16424 }
16425
16426 /* Now write out the hash table. */
16427 for (i = 0; i < symtab->size; ++i)
16428 {
16429 offset_type str_off, vec_off;
16430
16431 if (symtab->data[i])
16432 {
16433 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16434 vec_off = symtab->data[i]->index_offset;
16435 }
16436 else
16437 {
16438 /* While 0 is a valid constant pool index, it is not valid
16439 to have 0 for both offsets. */
16440 str_off = 0;
16441 vec_off = 0;
16442 }
16443
16444 str_off = MAYBE_SWAP (str_off);
16445 vec_off = MAYBE_SWAP (vec_off);
16446
16447 obstack_grow (output, &str_off, sizeof (str_off));
16448 obstack_grow (output, &vec_off, sizeof (vec_off));
16449 }
16450
16451 htab_delete (str_table);
3876f04e 16452 htab_delete (symbol_hash_table);
9291a0cd
TT
16453}
16454
0a5429f6
DE
16455/* Struct to map psymtab to CU index in the index file. */
16456struct psymtab_cu_index_map
16457{
16458 struct partial_symtab *psymtab;
16459 unsigned int cu_index;
16460};
16461
16462static hashval_t
16463hash_psymtab_cu_index (const void *item)
16464{
16465 const struct psymtab_cu_index_map *map = item;
16466
16467 return htab_hash_pointer (map->psymtab);
16468}
16469
16470static int
16471eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16472{
16473 const struct psymtab_cu_index_map *lhs = item_lhs;
16474 const struct psymtab_cu_index_map *rhs = item_rhs;
16475
16476 return lhs->psymtab == rhs->psymtab;
16477}
16478
16479/* Helper struct for building the address table. */
16480struct addrmap_index_data
16481{
16482 struct objfile *objfile;
16483 struct obstack *addr_obstack;
16484 htab_t cu_index_htab;
16485
16486 /* Non-zero if the previous_* fields are valid.
16487 We can't write an entry until we see the next entry (since it is only then
16488 that we know the end of the entry). */
16489 int previous_valid;
16490 /* Index of the CU in the table of all CUs in the index file. */
16491 unsigned int previous_cu_index;
0963b4bd 16492 /* Start address of the CU. */
0a5429f6
DE
16493 CORE_ADDR previous_cu_start;
16494};
16495
16496/* Write an address entry to OBSTACK. */
b89be57b 16497
9291a0cd 16498static void
0a5429f6
DE
16499add_address_entry (struct objfile *objfile, struct obstack *obstack,
16500 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 16501{
0a5429f6 16502 offset_type cu_index_to_write;
9291a0cd
TT
16503 char addr[8];
16504 CORE_ADDR baseaddr;
16505
16506 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16507
0a5429f6
DE
16508 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16509 obstack_grow (obstack, addr, 8);
16510 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16511 obstack_grow (obstack, addr, 8);
16512 cu_index_to_write = MAYBE_SWAP (cu_index);
16513 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16514}
16515
16516/* Worker function for traversing an addrmap to build the address table. */
16517
16518static int
16519add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16520{
16521 struct addrmap_index_data *data = datap;
16522 struct partial_symtab *pst = obj;
16523 offset_type cu_index;
16524 void **slot;
16525
16526 if (data->previous_valid)
16527 add_address_entry (data->objfile, data->addr_obstack,
16528 data->previous_cu_start, start_addr,
16529 data->previous_cu_index);
16530
16531 data->previous_cu_start = start_addr;
16532 if (pst != NULL)
16533 {
16534 struct psymtab_cu_index_map find_map, *map;
16535 find_map.psymtab = pst;
16536 map = htab_find (data->cu_index_htab, &find_map);
16537 gdb_assert (map != NULL);
16538 data->previous_cu_index = map->cu_index;
16539 data->previous_valid = 1;
16540 }
16541 else
16542 data->previous_valid = 0;
16543
16544 return 0;
16545}
16546
16547/* Write OBJFILE's address map to OBSTACK.
16548 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16549 in the index file. */
16550
16551static void
16552write_address_map (struct objfile *objfile, struct obstack *obstack,
16553 htab_t cu_index_htab)
16554{
16555 struct addrmap_index_data addrmap_index_data;
16556
16557 /* When writing the address table, we have to cope with the fact that
16558 the addrmap iterator only provides the start of a region; we have to
16559 wait until the next invocation to get the start of the next region. */
16560
16561 addrmap_index_data.objfile = objfile;
16562 addrmap_index_data.addr_obstack = obstack;
16563 addrmap_index_data.cu_index_htab = cu_index_htab;
16564 addrmap_index_data.previous_valid = 0;
16565
16566 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16567 &addrmap_index_data);
16568
16569 /* It's highly unlikely the last entry (end address = 0xff...ff)
16570 is valid, but we should still handle it.
16571 The end address is recorded as the start of the next region, but that
16572 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16573 anyway. */
16574 if (addrmap_index_data.previous_valid)
16575 add_address_entry (objfile, obstack,
16576 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16577 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16578}
16579
16580/* Add a list of partial symbols to SYMTAB. */
b89be57b 16581
9291a0cd
TT
16582static void
16583write_psymbols (struct mapped_symtab *symtab,
987d643c 16584 htab_t psyms_seen,
9291a0cd
TT
16585 struct partial_symbol **psymp,
16586 int count,
987d643c
TT
16587 offset_type cu_index,
16588 int is_static)
9291a0cd
TT
16589{
16590 for (; count-- > 0; ++psymp)
16591 {
987d643c
TT
16592 void **slot, *lookup;
16593
9291a0cd
TT
16594 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16595 error (_("Ada is not currently supported by the index"));
987d643c
TT
16596
16597 /* We only want to add a given psymbol once. However, we also
16598 want to account for whether it is global or static. So, we
16599 may add it twice, using slightly different values. */
16600 if (is_static)
16601 {
16602 uintptr_t val = 1 | (uintptr_t) *psymp;
16603
16604 lookup = (void *) val;
16605 }
16606 else
16607 lookup = *psymp;
16608
16609 /* Only add a given psymbol once. */
16610 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16611 if (!*slot)
16612 {
16613 *slot = lookup;
16614 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
16615 }
9291a0cd
TT
16616 }
16617}
16618
16619/* Write the contents of an ("unfinished") obstack to FILE. Throw an
16620 exception if there is an error. */
b89be57b 16621
9291a0cd
TT
16622static void
16623write_obstack (FILE *file, struct obstack *obstack)
16624{
16625 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16626 file)
16627 != obstack_object_size (obstack))
16628 error (_("couldn't data write to file"));
16629}
16630
16631/* Unlink a file if the argument is not NULL. */
b89be57b 16632
9291a0cd
TT
16633static void
16634unlink_if_set (void *p)
16635{
16636 char **filename = p;
16637 if (*filename)
16638 unlink (*filename);
16639}
16640
1fd400ff
TT
16641/* A helper struct used when iterating over debug_types. */
16642struct signatured_type_index_data
16643{
16644 struct objfile *objfile;
16645 struct mapped_symtab *symtab;
16646 struct obstack *types_list;
987d643c 16647 htab_t psyms_seen;
1fd400ff
TT
16648 int cu_index;
16649};
16650
16651/* A helper function that writes a single signatured_type to an
16652 obstack. */
b89be57b 16653
1fd400ff
TT
16654static int
16655write_one_signatured_type (void **slot, void *d)
16656{
16657 struct signatured_type_index_data *info = d;
16658 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
16659 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16660 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
16661 gdb_byte val[8];
16662
16663 write_psymbols (info->symtab,
987d643c 16664 info->psyms_seen,
3e43a32a
MS
16665 info->objfile->global_psymbols.list
16666 + psymtab->globals_offset,
987d643c
TT
16667 psymtab->n_global_syms, info->cu_index,
16668 0);
1fd400ff 16669 write_psymbols (info->symtab,
987d643c 16670 info->psyms_seen,
3e43a32a
MS
16671 info->objfile->static_psymbols.list
16672 + psymtab->statics_offset,
987d643c
TT
16673 psymtab->n_static_syms, info->cu_index,
16674 1);
1fd400ff 16675
b3c8eb43 16676 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
1fd400ff
TT
16677 obstack_grow (info->types_list, val, 8);
16678 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16679 obstack_grow (info->types_list, val, 8);
16680 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16681 obstack_grow (info->types_list, val, 8);
16682
16683 ++info->cu_index;
16684
16685 return 1;
16686}
16687
9291a0cd 16688/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16689
9291a0cd
TT
16690static void
16691write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16692{
16693 struct cleanup *cleanup;
16694 char *filename, *cleanup_filename;
1fd400ff
TT
16695 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16696 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16697 int i;
16698 FILE *out_file;
16699 struct mapped_symtab *symtab;
16700 offset_type val, size_of_contents, total_len;
16701 struct stat st;
16702 char buf[8];
987d643c 16703 htab_t psyms_seen;
0a5429f6
DE
16704 htab_t cu_index_htab;
16705 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 16706
b4f2f049 16707 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 16708 return;
b4f2f049 16709
9291a0cd
TT
16710 if (dwarf2_per_objfile->using_index)
16711 error (_("Cannot use an index to create the index"));
16712
8b70b953
TT
16713 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16714 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16715
9291a0cd 16716 if (stat (objfile->name, &st) < 0)
7e17e088 16717 perror_with_name (objfile->name);
9291a0cd
TT
16718
16719 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16720 INDEX_SUFFIX, (char *) NULL);
16721 cleanup = make_cleanup (xfree, filename);
16722
16723 out_file = fopen (filename, "wb");
16724 if (!out_file)
16725 error (_("Can't open `%s' for writing"), filename);
16726
16727 cleanup_filename = filename;
16728 make_cleanup (unlink_if_set, &cleanup_filename);
16729
16730 symtab = create_mapped_symtab ();
16731 make_cleanup (cleanup_mapped_symtab, symtab);
16732
16733 obstack_init (&addr_obstack);
16734 make_cleanup_obstack_free (&addr_obstack);
16735
16736 obstack_init (&cu_list);
16737 make_cleanup_obstack_free (&cu_list);
16738
1fd400ff
TT
16739 obstack_init (&types_cu_list);
16740 make_cleanup_obstack_free (&types_cu_list);
16741
987d643c
TT
16742 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16743 NULL, xcalloc, xfree);
96408a79 16744 make_cleanup_htab_delete (psyms_seen);
987d643c 16745
0a5429f6
DE
16746 /* While we're scanning CU's create a table that maps a psymtab pointer
16747 (which is what addrmap records) to its index (which is what is recorded
16748 in the index file). This will later be needed to write the address
16749 table. */
16750 cu_index_htab = htab_create_alloc (100,
16751 hash_psymtab_cu_index,
16752 eq_psymtab_cu_index,
16753 NULL, xcalloc, xfree);
96408a79 16754 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
16755 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16756 xmalloc (sizeof (struct psymtab_cu_index_map)
16757 * dwarf2_per_objfile->n_comp_units);
16758 make_cleanup (xfree, psymtab_cu_index_map);
16759
16760 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
16761 work here. Also, the debug_types entries do not appear in
16762 all_comp_units, but only in their own hash table. */
9291a0cd
TT
16763 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16764 {
3e43a32a
MS
16765 struct dwarf2_per_cu_data *per_cu
16766 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 16767 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 16768 gdb_byte val[8];
0a5429f6
DE
16769 struct psymtab_cu_index_map *map;
16770 void **slot;
9291a0cd
TT
16771
16772 write_psymbols (symtab,
987d643c 16773 psyms_seen,
9291a0cd 16774 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
16775 psymtab->n_global_syms, i,
16776 0);
9291a0cd 16777 write_psymbols (symtab,
987d643c 16778 psyms_seen,
9291a0cd 16779 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
16780 psymtab->n_static_syms, i,
16781 1);
9291a0cd 16782
0a5429f6
DE
16783 map = &psymtab_cu_index_map[i];
16784 map->psymtab = psymtab;
16785 map->cu_index = i;
16786 slot = htab_find_slot (cu_index_htab, map, INSERT);
16787 gdb_assert (slot != NULL);
16788 gdb_assert (*slot == NULL);
16789 *slot = map;
9291a0cd 16790
e254ef6a 16791 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 16792 obstack_grow (&cu_list, val, 8);
e254ef6a 16793 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
16794 obstack_grow (&cu_list, val, 8);
16795 }
16796
0a5429f6
DE
16797 /* Dump the address map. */
16798 write_address_map (objfile, &addr_obstack, cu_index_htab);
16799
1fd400ff
TT
16800 /* Write out the .debug_type entries, if any. */
16801 if (dwarf2_per_objfile->signatured_types)
16802 {
16803 struct signatured_type_index_data sig_data;
16804
16805 sig_data.objfile = objfile;
16806 sig_data.symtab = symtab;
16807 sig_data.types_list = &types_cu_list;
987d643c 16808 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
16809 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16810 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16811 write_one_signatured_type, &sig_data);
16812 }
16813
9291a0cd
TT
16814 obstack_init (&constant_pool);
16815 make_cleanup_obstack_free (&constant_pool);
16816 obstack_init (&symtab_obstack);
16817 make_cleanup_obstack_free (&symtab_obstack);
16818 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16819
16820 obstack_init (&contents);
16821 make_cleanup_obstack_free (&contents);
1fd400ff 16822 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
16823 total_len = size_of_contents;
16824
16825 /* The version number. */
559a7a62 16826 val = MAYBE_SWAP (5);
9291a0cd
TT
16827 obstack_grow (&contents, &val, sizeof (val));
16828
16829 /* The offset of the CU list from the start of the file. */
16830 val = MAYBE_SWAP (total_len);
16831 obstack_grow (&contents, &val, sizeof (val));
16832 total_len += obstack_object_size (&cu_list);
16833
1fd400ff
TT
16834 /* The offset of the types CU list from the start of the file. */
16835 val = MAYBE_SWAP (total_len);
16836 obstack_grow (&contents, &val, sizeof (val));
16837 total_len += obstack_object_size (&types_cu_list);
16838
9291a0cd
TT
16839 /* The offset of the address table from the start of the file. */
16840 val = MAYBE_SWAP (total_len);
16841 obstack_grow (&contents, &val, sizeof (val));
16842 total_len += obstack_object_size (&addr_obstack);
16843
16844 /* The offset of the symbol table from the start of the file. */
16845 val = MAYBE_SWAP (total_len);
16846 obstack_grow (&contents, &val, sizeof (val));
16847 total_len += obstack_object_size (&symtab_obstack);
16848
16849 /* The offset of the constant pool from the start of the file. */
16850 val = MAYBE_SWAP (total_len);
16851 obstack_grow (&contents, &val, sizeof (val));
16852 total_len += obstack_object_size (&constant_pool);
16853
16854 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16855
16856 write_obstack (out_file, &contents);
16857 write_obstack (out_file, &cu_list);
1fd400ff 16858 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
16859 write_obstack (out_file, &addr_obstack);
16860 write_obstack (out_file, &symtab_obstack);
16861 write_obstack (out_file, &constant_pool);
16862
16863 fclose (out_file);
16864
16865 /* We want to keep the file, so we set cleanup_filename to NULL
16866 here. See unlink_if_set. */
16867 cleanup_filename = NULL;
16868
16869 do_cleanups (cleanup);
16870}
16871
90476074
TT
16872/* Implementation of the `save gdb-index' command.
16873
16874 Note that the file format used by this command is documented in the
16875 GDB manual. Any changes here must be documented there. */
11570e71 16876
9291a0cd
TT
16877static void
16878save_gdb_index_command (char *arg, int from_tty)
16879{
16880 struct objfile *objfile;
16881
16882 if (!arg || !*arg)
96d19272 16883 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
16884
16885 ALL_OBJFILES (objfile)
16886 {
16887 struct stat st;
16888
16889 /* If the objfile does not correspond to an actual file, skip it. */
16890 if (stat (objfile->name, &st) < 0)
16891 continue;
16892
16893 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16894 if (dwarf2_per_objfile)
16895 {
16896 volatile struct gdb_exception except;
16897
16898 TRY_CATCH (except, RETURN_MASK_ERROR)
16899 {
16900 write_psymtabs_to_index (objfile, arg);
16901 }
16902 if (except.reason < 0)
16903 exception_fprintf (gdb_stderr, except,
16904 _("Error while writing index for `%s': "),
16905 objfile->name);
16906 }
16907 }
dce234bc
PP
16908}
16909
9291a0cd
TT
16910\f
16911
9eae7c52
TT
16912int dwarf2_always_disassemble;
16913
16914static void
16915show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16916 struct cmd_list_element *c, const char *value)
16917{
3e43a32a
MS
16918 fprintf_filtered (file,
16919 _("Whether to always disassemble "
16920 "DWARF expressions is %s.\n"),
9eae7c52
TT
16921 value);
16922}
16923
900e11f9
JK
16924static void
16925show_check_physname (struct ui_file *file, int from_tty,
16926 struct cmd_list_element *c, const char *value)
16927{
16928 fprintf_filtered (file,
16929 _("Whether to check \"physname\" is %s.\n"),
16930 value);
16931}
16932
6502dd73
DJ
16933void _initialize_dwarf2_read (void);
16934
16935void
16936_initialize_dwarf2_read (void)
16937{
96d19272
JK
16938 struct cmd_list_element *c;
16939
dce234bc 16940 dwarf2_objfile_data_key
c1bd65d0 16941 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 16942
1bedd215
AC
16943 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16944Set DWARF 2 specific variables.\n\
16945Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16946 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16947 0/*allow-unknown*/, &maintenance_set_cmdlist);
16948
1bedd215
AC
16949 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16950Show DWARF 2 specific variables\n\
16951Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16952 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16953 0/*allow-unknown*/, &maintenance_show_cmdlist);
16954
16955 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
16956 &dwarf2_max_cache_age, _("\
16957Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16958Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16959A higher limit means that cached compilation units will be stored\n\
16960in memory longer, and more total memory will be used. Zero disables\n\
16961caching, which can slow down startup."),
2c5b56ce 16962 NULL,
920d2a44 16963 show_dwarf2_max_cache_age,
2c5b56ce 16964 &set_dwarf2_cmdlist,
ae038cb0 16965 &show_dwarf2_cmdlist);
d97bc12b 16966
9eae7c52
TT
16967 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16968 &dwarf2_always_disassemble, _("\
16969Set whether `info address' always disassembles DWARF expressions."), _("\
16970Show whether `info address' always disassembles DWARF expressions."), _("\
16971When enabled, DWARF expressions are always printed in an assembly-like\n\
16972syntax. When disabled, expressions will be printed in a more\n\
16973conversational style, when possible."),
16974 NULL,
16975 show_dwarf2_always_disassemble,
16976 &set_dwarf2_cmdlist,
16977 &show_dwarf2_cmdlist);
16978
d97bc12b
DE
16979 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16980Set debugging of the dwarf2 DIE reader."), _("\
16981Show debugging of the dwarf2 DIE reader."), _("\
16982When enabled (non-zero), DIEs are dumped after they are read in.\n\
16983The value is the maximum depth to print."),
16984 NULL,
16985 NULL,
16986 &setdebuglist, &showdebuglist);
9291a0cd 16987
900e11f9
JK
16988 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
16989Set cross-checking of \"physname\" code against demangler."), _("\
16990Show cross-checking of \"physname\" code against demangler."), _("\
16991When enabled, GDB's internal \"physname\" code is checked against\n\
16992the demangler."),
16993 NULL, show_check_physname,
16994 &setdebuglist, &showdebuglist);
16995
96d19272 16996 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 16997 _("\
fc1a9d6e 16998Save a gdb-index file.\n\
11570e71 16999Usage: save gdb-index DIRECTORY"),
96d19272
JK
17000 &save_cmdlist);
17001 set_cmd_completer (c, filename_completer);
6502dd73 17002}
This page took 2.621111 seconds and 4 git commands to generate.