2011-05-18 Pedro Alves <pedro@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
6aba47ca 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
7b6bb8da 4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
0fb0cc75 5 Free Software Foundation, Inc.
c906108c
SS
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 12 support.
c906108c 13
c5aa993b 14 This file is part of GDB.
c906108c 15
c5aa993b
JM
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
a9762ec7
JB
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
c906108c 20
a9762ec7
JB
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
c906108c 25
c5aa993b 26 You should have received a copy of the GNU General Public License
a9762ec7 27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
28
29#include "defs.h"
30#include "bfd.h"
c906108c
SS
31#include "symtab.h"
32#include "gdbtypes.h"
c906108c 33#include "objfiles.h"
fa8f86ff 34#include "dwarf2.h"
c906108c
SS
35#include "buildsym.h"
36#include "demangle.h"
37#include "expression.h"
d5166ae1 38#include "filenames.h" /* for DOSish file names */
2e276125 39#include "macrotab.h"
c906108c
SS
40#include "language.h"
41#include "complaints.h"
357e46e7 42#include "bcache.h"
4c2df51b
DJ
43#include "dwarf2expr.h"
44#include "dwarf2loc.h"
9219021c 45#include "cp-support.h"
72bf9492 46#include "hashtab.h"
ae038cb0
DJ
47#include "command.h"
48#include "gdbcmd.h"
edb3359d 49#include "block.h"
ff013f42 50#include "addrmap.h"
94af9270
KS
51#include "typeprint.h"
52#include "jv-lang.h"
ccefe4c4 53#include "psympriv.h"
9291a0cd
TT
54#include "exceptions.h"
55#include "gdb_stat.h"
96d19272 56#include "completer.h"
34eaf542 57#include "vec.h"
98bfdba5
PA
58#include "c-lang.h"
59#include "valprint.h"
60d5a603 60#include <ctype.h>
4c2df51b 61
c906108c
SS
62#include <fcntl.h>
63#include "gdb_string.h"
4bdf3d34 64#include "gdb_assert.h"
c906108c 65#include <sys/types.h>
233a11ab
CS
66#ifdef HAVE_ZLIB_H
67#include <zlib.h>
68#endif
dce234bc
PP
69#ifdef HAVE_MMAP
70#include <sys/mman.h>
85d9bd0e
TT
71#ifndef MAP_FAILED
72#define MAP_FAILED ((void *) -1)
73#endif
dce234bc 74#endif
d8151005 75
34eaf542
TT
76typedef struct symbol *symbolp;
77DEF_VEC_P (symbolp);
78
107d2387 79#if 0
357e46e7 80/* .debug_info header for a compilation unit
c906108c
SS
81 Because of alignment constraints, this structure has padding and cannot
82 be mapped directly onto the beginning of the .debug_info section. */
83typedef struct comp_unit_header
84 {
85 unsigned int length; /* length of the .debug_info
86 contribution */
87 unsigned short version; /* version number -- 2 for DWARF
88 version 2 */
89 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
90 unsigned char addr_size; /* byte size of an address -- 4 */
91 }
92_COMP_UNIT_HEADER;
93#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 94#endif
c906108c 95
c906108c
SS
96/* .debug_line statement program prologue
97 Because of alignment constraints, this structure has padding and cannot
98 be mapped directly onto the beginning of the .debug_info section. */
99typedef struct statement_prologue
100 {
101 unsigned int total_length; /* byte length of the statement
102 information */
103 unsigned short version; /* version number -- 2 for DWARF
104 version 2 */
105 unsigned int prologue_length; /* # bytes between prologue &
106 stmt program */
107 unsigned char minimum_instruction_length; /* byte size of
108 smallest instr */
109 unsigned char default_is_stmt; /* initial value of is_stmt
110 register */
111 char line_base;
112 unsigned char line_range;
113 unsigned char opcode_base; /* number assigned to first special
114 opcode */
115 unsigned char *standard_opcode_lengths;
116 }
117_STATEMENT_PROLOGUE;
118
d97bc12b
DE
119/* When non-zero, dump DIEs after they are read in. */
120static int dwarf2_die_debug = 0;
121
dce234bc
PP
122static int pagesize;
123
df8a16a1
DJ
124/* When set, the file that we're processing is known to have debugging
125 info for C++ namespaces. GCC 3.3.x did not produce this information,
126 but later versions do. */
127
128static int processing_has_namespace_info;
129
6502dd73
DJ
130static const struct objfile_data *dwarf2_objfile_data_key;
131
dce234bc
PP
132struct dwarf2_section_info
133{
134 asection *asection;
135 gdb_byte *buffer;
136 bfd_size_type size;
137 int was_mmapped;
be391dca
TT
138 /* True if we have tried to read this section. */
139 int readin;
dce234bc
PP
140};
141
9291a0cd
TT
142/* All offsets in the index are of this type. It must be
143 architecture-independent. */
144typedef uint32_t offset_type;
145
146DEF_VEC_I (offset_type);
147
148/* A description of the mapped index. The file format is described in
149 a comment by the code that writes the index. */
150struct mapped_index
151{
559a7a62
JK
152 /* Index data format version. */
153 int version;
154
9291a0cd
TT
155 /* The total length of the buffer. */
156 off_t total_size;
b11b1f88 157
9291a0cd
TT
158 /* A pointer to the address table data. */
159 const gdb_byte *address_table;
b11b1f88 160
9291a0cd
TT
161 /* Size of the address table data in bytes. */
162 offset_type address_table_size;
b11b1f88 163
3876f04e
DE
164 /* The symbol table, implemented as a hash table. */
165 const offset_type *symbol_table;
b11b1f88 166
9291a0cd 167 /* Size in slots, each slot is 2 offset_types. */
3876f04e 168 offset_type symbol_table_slots;
b11b1f88 169
9291a0cd
TT
170 /* A pointer to the constant pool. */
171 const char *constant_pool;
172};
173
6502dd73
DJ
174struct dwarf2_per_objfile
175{
dce234bc
PP
176 struct dwarf2_section_info info;
177 struct dwarf2_section_info abbrev;
178 struct dwarf2_section_info line;
dce234bc
PP
179 struct dwarf2_section_info loc;
180 struct dwarf2_section_info macinfo;
181 struct dwarf2_section_info str;
182 struct dwarf2_section_info ranges;
348e048f 183 struct dwarf2_section_info types;
dce234bc
PP
184 struct dwarf2_section_info frame;
185 struct dwarf2_section_info eh_frame;
9291a0cd 186 struct dwarf2_section_info gdb_index;
ae038cb0 187
be391dca
TT
188 /* Back link. */
189 struct objfile *objfile;
190
10b3939b
DJ
191 /* A list of all the compilation units. This is used to locate
192 the target compilation unit of a particular reference. */
ae038cb0
DJ
193 struct dwarf2_per_cu_data **all_comp_units;
194
195 /* The number of compilation units in ALL_COMP_UNITS. */
196 int n_comp_units;
197
1fd400ff
TT
198 /* The number of .debug_types-related CUs. */
199 int n_type_comp_units;
200
201 /* The .debug_types-related CUs. */
202 struct dwarf2_per_cu_data **type_comp_units;
203
ae038cb0
DJ
204 /* A chain of compilation units that are currently read in, so that
205 they can be freed later. */
206 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 207
348e048f
DE
208 /* A table mapping .debug_types signatures to its signatured_type entry.
209 This is NULL if the .debug_types section hasn't been read in yet. */
210 htab_t signatured_types;
211
72dca2f5
FR
212 /* A flag indicating wether this objfile has a section loaded at a
213 VMA of 0. */
214 int has_section_at_zero;
9291a0cd 215
ae2de4f8
DE
216 /* True if we are using the mapped index,
217 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
218 unsigned char using_index;
219
ae2de4f8 220 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 221 struct mapped_index *index_table;
98bfdba5 222
7b9f3c50
DE
223 /* When using index_table, this keeps track of all quick_file_names entries.
224 TUs can share line table entries with CUs or other TUs, and there can be
225 a lot more TUs than unique line tables, so we maintain a separate table
226 of all line table entries to support the sharing. */
227 htab_t quick_file_names_table;
228
98bfdba5
PA
229 /* Set during partial symbol reading, to prevent queueing of full
230 symbols. */
231 int reading_partial_symbols;
673bfd45
DE
232
233 /* Table mapping type .debug_info DIE offsets to types.
234 This is NULL if not allocated yet.
235 It (currently) makes sense to allocate debug_types_type_hash lazily.
236 To keep things simple we allocate both lazily. */
237 htab_t debug_info_type_hash;
238
239 /* Table mapping type .debug_types DIE offsets to types.
240 This is NULL if not allocated yet. */
241 htab_t debug_types_type_hash;
6502dd73
DJ
242};
243
244static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
245
246/* names of the debugging sections */
247
233a11ab
CS
248/* Note that if the debugging section has been compressed, it might
249 have a name like .zdebug_info. */
250
251#define INFO_SECTION "debug_info"
252#define ABBREV_SECTION "debug_abbrev"
253#define LINE_SECTION "debug_line"
233a11ab
CS
254#define LOC_SECTION "debug_loc"
255#define MACINFO_SECTION "debug_macinfo"
256#define STR_SECTION "debug_str"
257#define RANGES_SECTION "debug_ranges"
348e048f 258#define TYPES_SECTION "debug_types"
233a11ab
CS
259#define FRAME_SECTION "debug_frame"
260#define EH_FRAME_SECTION "eh_frame"
9291a0cd 261#define GDB_INDEX_SECTION "gdb_index"
c906108c
SS
262
263/* local data types */
264
0963b4bd 265/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
266#ifndef ABBREV_HASH_SIZE
267#define ABBREV_HASH_SIZE 121
268#endif
269
107d2387
AC
270/* The data in a compilation unit header, after target2host
271 translation, looks like this. */
c906108c 272struct comp_unit_head
a738430d 273{
c764a876 274 unsigned int length;
a738430d 275 short version;
a738430d
MK
276 unsigned char addr_size;
277 unsigned char signed_addr_p;
9cbfa09e 278 unsigned int abbrev_offset;
57349743 279
a738430d
MK
280 /* Size of file offsets; either 4 or 8. */
281 unsigned int offset_size;
57349743 282
a738430d
MK
283 /* Size of the length field; either 4 or 12. */
284 unsigned int initial_length_size;
57349743 285
a738430d
MK
286 /* Offset to the first byte of this compilation unit header in the
287 .debug_info section, for resolving relative reference dies. */
288 unsigned int offset;
57349743 289
d00adf39
DE
290 /* Offset to first die in this cu from the start of the cu.
291 This will be the first byte following the compilation unit header. */
292 unsigned int first_die_offset;
a738430d 293};
c906108c 294
3da10d80
KS
295/* Type used for delaying computation of method physnames.
296 See comments for compute_delayed_physnames. */
297struct delayed_method_info
298{
299 /* The type to which the method is attached, i.e., its parent class. */
300 struct type *type;
301
302 /* The index of the method in the type's function fieldlists. */
303 int fnfield_index;
304
305 /* The index of the method in the fieldlist. */
306 int index;
307
308 /* The name of the DIE. */
309 const char *name;
310
311 /* The DIE associated with this method. */
312 struct die_info *die;
313};
314
315typedef struct delayed_method_info delayed_method_info;
316DEF_VEC_O (delayed_method_info);
317
e7c27a73
DJ
318/* Internal state when decoding a particular compilation unit. */
319struct dwarf2_cu
320{
321 /* The objfile containing this compilation unit. */
322 struct objfile *objfile;
323
d00adf39 324 /* The header of the compilation unit. */
e7c27a73 325 struct comp_unit_head header;
e142c38c 326
d00adf39
DE
327 /* Base address of this compilation unit. */
328 CORE_ADDR base_address;
329
330 /* Non-zero if base_address has been set. */
331 int base_known;
332
e142c38c
DJ
333 struct function_range *first_fn, *last_fn, *cached_fn;
334
335 /* The language we are debugging. */
336 enum language language;
337 const struct language_defn *language_defn;
338
b0f35d58
DL
339 const char *producer;
340
e142c38c
DJ
341 /* The generic symbol table building routines have separate lists for
342 file scope symbols and all all other scopes (local scopes). So
343 we need to select the right one to pass to add_symbol_to_list().
344 We do it by keeping a pointer to the correct list in list_in_scope.
345
346 FIXME: The original dwarf code just treated the file scope as the
347 first local scope, and all other local scopes as nested local
348 scopes, and worked fine. Check to see if we really need to
349 distinguish these in buildsym.c. */
350 struct pending **list_in_scope;
351
f3dd6933
DJ
352 /* DWARF abbreviation table associated with this compilation unit. */
353 struct abbrev_info **dwarf2_abbrevs;
354
355 /* Storage for the abbrev table. */
356 struct obstack abbrev_obstack;
72bf9492
DJ
357
358 /* Hash table holding all the loaded partial DIEs. */
359 htab_t partial_dies;
360
361 /* Storage for things with the same lifetime as this read-in compilation
362 unit, including partial DIEs. */
363 struct obstack comp_unit_obstack;
364
ae038cb0
DJ
365 /* When multiple dwarf2_cu structures are living in memory, this field
366 chains them all together, so that they can be released efficiently.
367 We will probably also want a generation counter so that most-recently-used
368 compilation units are cached... */
369 struct dwarf2_per_cu_data *read_in_chain;
370
371 /* Backchain to our per_cu entry if the tree has been built. */
372 struct dwarf2_per_cu_data *per_cu;
373
374 /* How many compilation units ago was this CU last referenced? */
375 int last_used;
376
10b3939b 377 /* A hash table of die offsets for following references. */
51545339 378 htab_t die_hash;
10b3939b
DJ
379
380 /* Full DIEs if read in. */
381 struct die_info *dies;
382
383 /* A set of pointers to dwarf2_per_cu_data objects for compilation
384 units referenced by this one. Only set during full symbol processing;
385 partial symbol tables do not have dependencies. */
386 htab_t dependencies;
387
cb1df416
DJ
388 /* Header data from the line table, during full symbol processing. */
389 struct line_header *line_header;
390
3da10d80
KS
391 /* A list of methods which need to have physnames computed
392 after all type information has been read. */
393 VEC (delayed_method_info) *method_list;
394
ae038cb0
DJ
395 /* Mark used when releasing cached dies. */
396 unsigned int mark : 1;
397
398 /* This flag will be set if this compilation unit might include
399 inter-compilation-unit references. */
400 unsigned int has_form_ref_addr : 1;
401
72bf9492
DJ
402 /* This flag will be set if this compilation unit includes any
403 DW_TAG_namespace DIEs. If we know that there are explicit
404 DIEs for namespaces, we don't need to try to infer them
405 from mangled names. */
406 unsigned int has_namespace_info : 1;
8be455d7
JK
407
408 /* This CU references .debug_loc. See the symtab->locations_valid field.
409 This test is imperfect as there may exist optimized debug code not using
410 any location list and still facing inlining issues if handled as
411 unoptimized code. For a future better test see GCC PR other/32998. */
412
413 unsigned int has_loclist : 1;
e7c27a73
DJ
414};
415
10b3939b
DJ
416/* Persistent data held for a compilation unit, even when not
417 processing it. We put a pointer to this structure in the
418 read_symtab_private field of the psymtab. If we encounter
419 inter-compilation-unit references, we also maintain a sorted
420 list of all compilation units. */
421
ae038cb0
DJ
422struct dwarf2_per_cu_data
423{
348e048f 424 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 425 bytes should suffice to store the length of any compilation unit
45452591
DE
426 - if it doesn't, GDB will fall over anyway.
427 NOTE: Unlike comp_unit_head.length, this length includes
428 initial_length_size. */
c764a876 429 unsigned int offset;
348e048f 430 unsigned int length : 29;
ae038cb0
DJ
431
432 /* Flag indicating this compilation unit will be read in before
433 any of the current compilation units are processed. */
c764a876 434 unsigned int queued : 1;
ae038cb0 435
5afb4e99
DJ
436 /* This flag will be set if we need to load absolutely all DIEs
437 for this compilation unit, instead of just the ones we think
438 are interesting. It gets set if we look for a DIE in the
439 hash table and don't find it. */
440 unsigned int load_all_dies : 1;
441
348e048f
DE
442 /* Non-zero if this CU is from .debug_types.
443 Otherwise it's from .debug_info. */
444 unsigned int from_debug_types : 1;
445
17ea53c3
JK
446 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
447 of the CU cache it gets reset to NULL again. */
ae038cb0 448 struct dwarf2_cu *cu;
1c379e20 449
9291a0cd
TT
450 /* The corresponding objfile. */
451 struct objfile *objfile;
452
453 /* When using partial symbol tables, the 'psymtab' field is active.
454 Otherwise the 'quick' field is active. */
455 union
456 {
457 /* The partial symbol table associated with this compilation unit,
458 or NULL for partial units (which do not have an associated
459 symtab). */
460 struct partial_symtab *psymtab;
461
462 /* Data needed by the "quick" functions. */
463 struct dwarf2_per_cu_quick_data *quick;
464 } v;
ae038cb0
DJ
465};
466
348e048f
DE
467/* Entry in the signatured_types hash table. */
468
469struct signatured_type
470{
471 ULONGEST signature;
472
473 /* Offset in .debug_types of the TU (type_unit) for this type. */
474 unsigned int offset;
475
476 /* Offset in .debug_types of the type defined by this TU. */
477 unsigned int type_offset;
478
479 /* The CU(/TU) of this type. */
480 struct dwarf2_per_cu_data per_cu;
481};
482
0963b4bd
MS
483/* Struct used to pass misc. parameters to read_die_and_children, et
484 al. which are used for both .debug_info and .debug_types dies.
485 All parameters here are unchanging for the life of the call. This
486 struct exists to abstract away the constant parameters of die
487 reading. */
93311388
DE
488
489struct die_reader_specs
490{
491 /* The bfd of this objfile. */
492 bfd* abfd;
493
494 /* The CU of the DIE we are parsing. */
495 struct dwarf2_cu *cu;
496
497 /* Pointer to start of section buffer.
498 This is either the start of .debug_info or .debug_types. */
499 const gdb_byte *buffer;
500};
501
debd256d
JB
502/* The line number information for a compilation unit (found in the
503 .debug_line section) begins with a "statement program header",
504 which contains the following information. */
505struct line_header
506{
507 unsigned int total_length;
508 unsigned short version;
509 unsigned int header_length;
510 unsigned char minimum_instruction_length;
2dc7f7b3 511 unsigned char maximum_ops_per_instruction;
debd256d
JB
512 unsigned char default_is_stmt;
513 int line_base;
514 unsigned char line_range;
515 unsigned char opcode_base;
516
517 /* standard_opcode_lengths[i] is the number of operands for the
518 standard opcode whose value is i. This means that
519 standard_opcode_lengths[0] is unused, and the last meaningful
520 element is standard_opcode_lengths[opcode_base - 1]. */
521 unsigned char *standard_opcode_lengths;
522
523 /* The include_directories table. NOTE! These strings are not
524 allocated with xmalloc; instead, they are pointers into
525 debug_line_buffer. If you try to free them, `free' will get
526 indigestion. */
527 unsigned int num_include_dirs, include_dirs_size;
528 char **include_dirs;
529
530 /* The file_names table. NOTE! These strings are not allocated
531 with xmalloc; instead, they are pointers into debug_line_buffer.
532 Don't try to free them directly. */
533 unsigned int num_file_names, file_names_size;
534 struct file_entry
c906108c 535 {
debd256d
JB
536 char *name;
537 unsigned int dir_index;
538 unsigned int mod_time;
539 unsigned int length;
aaa75496 540 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 541 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
542 } *file_names;
543
544 /* The start and end of the statement program following this
6502dd73 545 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 546 gdb_byte *statement_program_start, *statement_program_end;
debd256d 547};
c906108c
SS
548
549/* When we construct a partial symbol table entry we only
0963b4bd 550 need this much information. */
c906108c
SS
551struct partial_die_info
552 {
72bf9492 553 /* Offset of this DIE. */
c906108c 554 unsigned int offset;
72bf9492
DJ
555
556 /* DWARF-2 tag for this DIE. */
557 ENUM_BITFIELD(dwarf_tag) tag : 16;
558
72bf9492
DJ
559 /* Assorted flags describing the data found in this DIE. */
560 unsigned int has_children : 1;
561 unsigned int is_external : 1;
562 unsigned int is_declaration : 1;
563 unsigned int has_type : 1;
564 unsigned int has_specification : 1;
565 unsigned int has_pc_info : 1;
566
567 /* Flag set if the SCOPE field of this structure has been
568 computed. */
569 unsigned int scope_set : 1;
570
fa4028e9
JB
571 /* Flag set if the DIE has a byte_size attribute. */
572 unsigned int has_byte_size : 1;
573
98bfdba5
PA
574 /* Flag set if any of the DIE's children are template arguments. */
575 unsigned int has_template_arguments : 1;
576
abc72ce4
DE
577 /* Flag set if fixup_partial_die has been called on this die. */
578 unsigned int fixup_called : 1;
579
72bf9492 580 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 581 sometimes a default name for unnamed DIEs. */
c906108c 582 char *name;
72bf9492 583
abc72ce4
DE
584 /* The linkage name, if present. */
585 const char *linkage_name;
586
72bf9492
DJ
587 /* The scope to prepend to our children. This is generally
588 allocated on the comp_unit_obstack, so will disappear
589 when this compilation unit leaves the cache. */
590 char *scope;
591
592 /* The location description associated with this DIE, if any. */
593 struct dwarf_block *locdesc;
594
595 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
596 CORE_ADDR lowpc;
597 CORE_ADDR highpc;
72bf9492 598
93311388 599 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 600 DW_AT_sibling, if any. */
abc72ce4
DE
601 /* NOTE: This member isn't strictly necessary, read_partial_die could
602 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 603 gdb_byte *sibling;
72bf9492
DJ
604
605 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
606 DW_AT_specification (or DW_AT_abstract_origin or
607 DW_AT_extension). */
608 unsigned int spec_offset;
609
610 /* Pointers to this DIE's parent, first child, and next sibling,
611 if any. */
612 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
613 };
614
0963b4bd 615/* This data structure holds the information of an abbrev. */
c906108c
SS
616struct abbrev_info
617 {
618 unsigned int number; /* number identifying abbrev */
619 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
620 unsigned short has_children; /* boolean */
621 unsigned short num_attrs; /* number of attributes */
c906108c
SS
622 struct attr_abbrev *attrs; /* an array of attribute descriptions */
623 struct abbrev_info *next; /* next in chain */
624 };
625
626struct attr_abbrev
627 {
9d25dd43
DE
628 ENUM_BITFIELD(dwarf_attribute) name : 16;
629 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
630 };
631
0963b4bd 632/* Attributes have a name and a value. */
b60c80d6
DJ
633struct attribute
634 {
9d25dd43 635 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
636 ENUM_BITFIELD(dwarf_form) form : 15;
637
638 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
639 field should be in u.str (existing only for DW_STRING) but it is kept
640 here for better struct attribute alignment. */
641 unsigned int string_is_canonical : 1;
642
b60c80d6
DJ
643 union
644 {
645 char *str;
646 struct dwarf_block *blk;
43bbcdc2
PH
647 ULONGEST unsnd;
648 LONGEST snd;
b60c80d6 649 CORE_ADDR addr;
348e048f 650 struct signatured_type *signatured_type;
b60c80d6
DJ
651 }
652 u;
653 };
654
0963b4bd 655/* This data structure holds a complete die structure. */
c906108c
SS
656struct die_info
657 {
76815b17
DE
658 /* DWARF-2 tag for this DIE. */
659 ENUM_BITFIELD(dwarf_tag) tag : 16;
660
661 /* Number of attributes */
98bfdba5
PA
662 unsigned char num_attrs;
663
664 /* True if we're presently building the full type name for the
665 type derived from this DIE. */
666 unsigned char building_fullname : 1;
76815b17
DE
667
668 /* Abbrev number */
669 unsigned int abbrev;
670
93311388 671 /* Offset in .debug_info or .debug_types section. */
76815b17 672 unsigned int offset;
78ba4af6
JB
673
674 /* The dies in a compilation unit form an n-ary tree. PARENT
675 points to this die's parent; CHILD points to the first child of
676 this node; and all the children of a given node are chained
4950bc1c 677 together via their SIBLING fields. */
639d11d3
DC
678 struct die_info *child; /* Its first child, if any. */
679 struct die_info *sibling; /* Its next sibling, if any. */
680 struct die_info *parent; /* Its parent, if any. */
c906108c 681
b60c80d6
DJ
682 /* An array of attributes, with NUM_ATTRS elements. There may be
683 zero, but it's not common and zero-sized arrays are not
684 sufficiently portable C. */
685 struct attribute attrs[1];
c906108c
SS
686 };
687
5fb290d7
DJ
688struct function_range
689{
690 const char *name;
691 CORE_ADDR lowpc, highpc;
692 int seen_line;
693 struct function_range *next;
694};
695
0963b4bd 696/* Get at parts of an attribute structure. */
c906108c
SS
697
698#define DW_STRING(attr) ((attr)->u.str)
8285870a 699#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
700#define DW_UNSND(attr) ((attr)->u.unsnd)
701#define DW_BLOCK(attr) ((attr)->u.blk)
702#define DW_SND(attr) ((attr)->u.snd)
703#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 704#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 705
0963b4bd 706/* Blocks are a bunch of untyped bytes. */
c906108c
SS
707struct dwarf_block
708 {
709 unsigned int size;
fe1b8b76 710 gdb_byte *data;
c906108c
SS
711 };
712
c906108c
SS
713#ifndef ATTR_ALLOC_CHUNK
714#define ATTR_ALLOC_CHUNK 4
715#endif
716
c906108c
SS
717/* Allocate fields for structs, unions and enums in this size. */
718#ifndef DW_FIELD_ALLOC_CHUNK
719#define DW_FIELD_ALLOC_CHUNK 4
720#endif
721
c906108c
SS
722/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
723 but this would require a corresponding change in unpack_field_as_long
724 and friends. */
725static int bits_per_byte = 8;
726
727/* The routines that read and process dies for a C struct or C++ class
728 pass lists of data member fields and lists of member function fields
729 in an instance of a field_info structure, as defined below. */
730struct field_info
c5aa993b 731 {
0963b4bd 732 /* List of data member and baseclasses fields. */
c5aa993b
JM
733 struct nextfield
734 {
735 struct nextfield *next;
736 int accessibility;
737 int virtuality;
738 struct field field;
739 }
7d0ccb61 740 *fields, *baseclasses;
c906108c 741
7d0ccb61 742 /* Number of fields (including baseclasses). */
c5aa993b 743 int nfields;
c906108c 744
c5aa993b
JM
745 /* Number of baseclasses. */
746 int nbaseclasses;
c906108c 747
c5aa993b
JM
748 /* Set if the accesibility of one of the fields is not public. */
749 int non_public_fields;
c906108c 750
c5aa993b
JM
751 /* Member function fields array, entries are allocated in the order they
752 are encountered in the object file. */
753 struct nextfnfield
754 {
755 struct nextfnfield *next;
756 struct fn_field fnfield;
757 }
758 *fnfields;
c906108c 759
c5aa993b
JM
760 /* Member function fieldlist array, contains name of possibly overloaded
761 member function, number of overloaded member functions and a pointer
762 to the head of the member function field chain. */
763 struct fnfieldlist
764 {
765 char *name;
766 int length;
767 struct nextfnfield *head;
768 }
769 *fnfieldlists;
c906108c 770
c5aa993b
JM
771 /* Number of entries in the fnfieldlists array. */
772 int nfnfields;
98751a41
JK
773
774 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
775 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
776 struct typedef_field_list
777 {
778 struct typedef_field field;
779 struct typedef_field_list *next;
780 }
781 *typedef_field_list;
782 unsigned typedef_field_list_count;
c5aa993b 783 };
c906108c 784
10b3939b
DJ
785/* One item on the queue of compilation units to read in full symbols
786 for. */
787struct dwarf2_queue_item
788{
789 struct dwarf2_per_cu_data *per_cu;
790 struct dwarf2_queue_item *next;
791};
792
793/* The current queue. */
794static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
795
ae038cb0
DJ
796/* Loaded secondary compilation units are kept in memory until they
797 have not been referenced for the processing of this many
798 compilation units. Set this to zero to disable caching. Cache
799 sizes of up to at least twenty will improve startup time for
800 typical inter-CU-reference binaries, at an obvious memory cost. */
801static int dwarf2_max_cache_age = 5;
920d2a44
AC
802static void
803show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
804 struct cmd_list_element *c, const char *value)
805{
3e43a32a
MS
806 fprintf_filtered (file, _("The upper bound on the age of cached "
807 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
808 value);
809}
810
ae038cb0 811
0963b4bd 812/* Various complaints about symbol reading that don't abort the process. */
c906108c 813
4d3c2250
KB
814static void
815dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 816{
4d3c2250 817 complaint (&symfile_complaints,
e2e0b3e5 818 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
819}
820
25e43795
DJ
821static void
822dwarf2_debug_line_missing_file_complaint (void)
823{
824 complaint (&symfile_complaints,
825 _(".debug_line section has line data without a file"));
826}
827
59205f5a
JB
828static void
829dwarf2_debug_line_missing_end_sequence_complaint (void)
830{
831 complaint (&symfile_complaints,
3e43a32a
MS
832 _(".debug_line section has line "
833 "program sequence without an end"));
59205f5a
JB
834}
835
4d3c2250
KB
836static void
837dwarf2_complex_location_expr_complaint (void)
2e276125 838{
e2e0b3e5 839 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
840}
841
4d3c2250
KB
842static void
843dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
844 int arg3)
2e276125 845{
4d3c2250 846 complaint (&symfile_complaints,
3e43a32a
MS
847 _("const value length mismatch for '%s', got %d, expected %d"),
848 arg1, arg2, arg3);
4d3c2250
KB
849}
850
851static void
852dwarf2_macros_too_long_complaint (void)
2e276125 853{
4d3c2250 854 complaint (&symfile_complaints,
e2e0b3e5 855 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
856}
857
858static void
859dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 860{
4d3c2250 861 complaint (&symfile_complaints,
3e43a32a
MS
862 _("macro debug info contains a "
863 "malformed macro definition:\n`%s'"),
4d3c2250
KB
864 arg1);
865}
866
867static void
868dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 869{
4d3c2250 870 complaint (&symfile_complaints,
3e43a32a
MS
871 _("invalid attribute class or form for '%s' in '%s'"),
872 arg1, arg2);
4d3c2250 873}
c906108c 874
c906108c
SS
875/* local function prototypes */
876
4efb68b1 877static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 878
aaa75496
JB
879static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
880 struct objfile *);
881
c67a9c90 882static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 883
72bf9492
DJ
884static void scan_partial_symbols (struct partial_die_info *,
885 CORE_ADDR *, CORE_ADDR *,
5734ee8b 886 int, struct dwarf2_cu *);
c906108c 887
72bf9492
DJ
888static void add_partial_symbol (struct partial_die_info *,
889 struct dwarf2_cu *);
63d06c5c 890
72bf9492
DJ
891static void add_partial_namespace (struct partial_die_info *pdi,
892 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 893 int need_pc, struct dwarf2_cu *cu);
63d06c5c 894
5d7cb8df
JK
895static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
896 CORE_ADDR *highpc, int need_pc,
897 struct dwarf2_cu *cu);
898
72bf9492
DJ
899static void add_partial_enumeration (struct partial_die_info *enum_pdi,
900 struct dwarf2_cu *cu);
91c24f0a 901
bc30ff58
JB
902static void add_partial_subprogram (struct partial_die_info *pdi,
903 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 904 int need_pc, struct dwarf2_cu *cu);
bc30ff58 905
fe1b8b76 906static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
907 gdb_byte *buffer, gdb_byte *info_ptr,
908 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 909
a14ed312 910static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 911
a14ed312 912static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 913
e7c27a73 914static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 915
f3dd6933 916static void dwarf2_free_abbrev_table (void *);
c906108c 917
fe1b8b76 918static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 919 struct dwarf2_cu *);
72bf9492 920
57349743 921static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 922 struct dwarf2_cu *);
c906108c 923
93311388
DE
924static struct partial_die_info *load_partial_dies (bfd *,
925 gdb_byte *, gdb_byte *,
926 int, struct dwarf2_cu *);
72bf9492 927
fe1b8b76 928static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
929 struct abbrev_info *abbrev,
930 unsigned int, bfd *,
931 gdb_byte *, gdb_byte *,
932 struct dwarf2_cu *);
c906108c 933
c764a876 934static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 935 struct dwarf2_cu *);
72bf9492
DJ
936
937static void fixup_partial_die (struct partial_die_info *,
938 struct dwarf2_cu *);
939
fe1b8b76
JB
940static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
941 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 942
fe1b8b76
JB
943static gdb_byte *read_attribute_value (struct attribute *, unsigned,
944 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 945
fe1b8b76 946static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 947
fe1b8b76 948static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 949
fe1b8b76 950static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 951
fe1b8b76 952static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 953
93311388 954static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 955
fe1b8b76 956static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 957 unsigned int *);
c906108c 958
c764a876
DE
959static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
960
961static LONGEST read_checked_initial_length_and_offset
962 (bfd *, gdb_byte *, const struct comp_unit_head *,
963 unsigned int *, unsigned int *);
613e1657 964
fe1b8b76 965static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
966 unsigned int *);
967
968static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 969
fe1b8b76 970static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 971
9b1c24c8 972static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 973
fe1b8b76
JB
974static char *read_indirect_string (bfd *, gdb_byte *,
975 const struct comp_unit_head *,
976 unsigned int *);
4bdf3d34 977
fe1b8b76 978static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 979
fe1b8b76 980static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 981
fe1b8b76 982static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 983
e142c38c 984static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 985
e142c38c
DJ
986static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
987 struct dwarf2_cu *);
c906108c 988
348e048f
DE
989static struct attribute *dwarf2_attr_no_follow (struct die_info *,
990 unsigned int,
991 struct dwarf2_cu *);
992
05cf31d1
JB
993static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
994 struct dwarf2_cu *cu);
995
e142c38c 996static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 997
e142c38c 998static struct die_info *die_specification (struct die_info *die,
f2f0e013 999 struct dwarf2_cu **);
63d06c5c 1000
debd256d
JB
1001static void free_line_header (struct line_header *lh);
1002
aaa75496
JB
1003static void add_file_name (struct line_header *, char *, unsigned int,
1004 unsigned int, unsigned int);
1005
debd256d
JB
1006static struct line_header *(dwarf_decode_line_header
1007 (unsigned int offset,
e7c27a73 1008 bfd *abfd, struct dwarf2_cu *cu));
debd256d 1009
72b9f47f 1010static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 1011 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1012
72b9f47f 1013static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1014
a14ed312 1015static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1016 struct dwarf2_cu *);
c906108c 1017
34eaf542
TT
1018static struct symbol *new_symbol_full (struct die_info *, struct type *,
1019 struct dwarf2_cu *, struct symbol *);
1020
a14ed312 1021static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1022 struct dwarf2_cu *);
c906108c 1023
98bfdba5
PA
1024static void dwarf2_const_value_attr (struct attribute *attr,
1025 struct type *type,
1026 const char *name,
1027 struct obstack *obstack,
1028 struct dwarf2_cu *cu, long *value,
1029 gdb_byte **bytes,
1030 struct dwarf2_locexpr_baton **baton);
2df3850c 1031
e7c27a73 1032static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1033
b4ba55a1
JB
1034static int need_gnat_info (struct dwarf2_cu *);
1035
3e43a32a
MS
1036static struct type *die_descriptive_type (struct die_info *,
1037 struct dwarf2_cu *);
b4ba55a1
JB
1038
1039static void set_descriptive_type (struct type *, struct die_info *,
1040 struct dwarf2_cu *);
1041
e7c27a73
DJ
1042static struct type *die_containing_type (struct die_info *,
1043 struct dwarf2_cu *);
c906108c 1044
673bfd45
DE
1045static struct type *lookup_die_type (struct die_info *, struct attribute *,
1046 struct dwarf2_cu *);
c906108c 1047
f792889a 1048static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1049
673bfd45
DE
1050static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1051
086ed43d 1052static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1053
6e70227d 1054static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1055 const char *suffix, int physname,
1056 struct dwarf2_cu *cu);
63d06c5c 1057
e7c27a73 1058static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1059
348e048f
DE
1060static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1061
e7c27a73 1062static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1063
e7c27a73 1064static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1065
ff013f42
JK
1066static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1067 struct dwarf2_cu *, struct partial_symtab *);
1068
a14ed312 1069static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1070 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1071 struct partial_symtab *);
c906108c 1072
fae299cd
DC
1073static void get_scope_pc_bounds (struct die_info *,
1074 CORE_ADDR *, CORE_ADDR *,
1075 struct dwarf2_cu *);
1076
801e3a5b
JB
1077static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1078 CORE_ADDR, struct dwarf2_cu *);
1079
a14ed312 1080static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1081 struct dwarf2_cu *);
c906108c 1082
a14ed312 1083static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1084 struct type *, struct dwarf2_cu *);
c906108c 1085
a14ed312 1086static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1087 struct die_info *, struct type *,
e7c27a73 1088 struct dwarf2_cu *);
c906108c 1089
a14ed312 1090static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1091 struct type *,
1092 struct dwarf2_cu *);
c906108c 1093
134d01f1 1094static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1095
e7c27a73 1096static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1097
e7c27a73 1098static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1099
5d7cb8df
JK
1100static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1101
27aa8d6a
SW
1102static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1103
f55ee35c
JK
1104static struct type *read_module_type (struct die_info *die,
1105 struct dwarf2_cu *cu);
1106
38d518c9 1107static const char *namespace_name (struct die_info *die,
e142c38c 1108 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1109
134d01f1 1110static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1111
e7c27a73 1112static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1113
6e70227d 1114static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1115 struct dwarf2_cu *);
1116
93311388 1117static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1118
93311388
DE
1119static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1120 gdb_byte *info_ptr,
d97bc12b
DE
1121 gdb_byte **new_info_ptr,
1122 struct die_info *parent);
1123
93311388
DE
1124static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1125 gdb_byte *info_ptr,
fe1b8b76 1126 gdb_byte **new_info_ptr,
639d11d3
DC
1127 struct die_info *parent);
1128
93311388
DE
1129static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1130 gdb_byte *info_ptr,
fe1b8b76 1131 gdb_byte **new_info_ptr,
639d11d3
DC
1132 struct die_info *parent);
1133
93311388
DE
1134static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1135 struct die_info **, gdb_byte *,
1136 int *);
1137
e7c27a73 1138static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1139
71c25dea
TT
1140static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1141 struct obstack *);
1142
e142c38c 1143static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1144
98bfdba5
PA
1145static const char *dwarf2_full_name (char *name,
1146 struct die_info *die,
1147 struct dwarf2_cu *cu);
1148
e142c38c 1149static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1150 struct dwarf2_cu **);
9219021c 1151
a14ed312 1152static char *dwarf_tag_name (unsigned int);
c906108c 1153
a14ed312 1154static char *dwarf_attr_name (unsigned int);
c906108c 1155
a14ed312 1156static char *dwarf_form_name (unsigned int);
c906108c 1157
a14ed312 1158static char *dwarf_bool_name (unsigned int);
c906108c 1159
a14ed312 1160static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1161
1162#if 0
a14ed312 1163static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1164#endif
1165
f9aca02d 1166static struct die_info *sibling_die (struct die_info *);
c906108c 1167
d97bc12b
DE
1168static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1169
1170static void dump_die_for_error (struct die_info *);
1171
1172static void dump_die_1 (struct ui_file *, int level, int max_level,
1173 struct die_info *);
c906108c 1174
d97bc12b 1175/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1176
51545339 1177static void store_in_ref_table (struct die_info *,
10b3939b 1178 struct dwarf2_cu *);
c906108c 1179
93311388
DE
1180static int is_ref_attr (struct attribute *);
1181
c764a876 1182static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1183
43bbcdc2 1184static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1185
348e048f
DE
1186static struct die_info *follow_die_ref_or_sig (struct die_info *,
1187 struct attribute *,
1188 struct dwarf2_cu **);
1189
10b3939b
DJ
1190static struct die_info *follow_die_ref (struct die_info *,
1191 struct attribute *,
f2f0e013 1192 struct dwarf2_cu **);
c906108c 1193
348e048f
DE
1194static struct die_info *follow_die_sig (struct die_info *,
1195 struct attribute *,
1196 struct dwarf2_cu **);
1197
1198static void read_signatured_type_at_offset (struct objfile *objfile,
1199 unsigned int offset);
1200
1201static void read_signatured_type (struct objfile *,
1202 struct signatured_type *type_sig);
1203
c906108c
SS
1204/* memory allocation interface */
1205
7b5a2f43 1206static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1207
f3dd6933 1208static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1209
b60c80d6 1210static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1211
e142c38c 1212static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1213
e142c38c
DJ
1214static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1215 struct dwarf2_cu *);
5fb290d7 1216
2e276125 1217static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1218 char *, bfd *, struct dwarf2_cu *);
2e276125 1219
8e19ed76
PS
1220static int attr_form_is_block (struct attribute *);
1221
3690dd37
JB
1222static int attr_form_is_section_offset (struct attribute *);
1223
1224static int attr_form_is_constant (struct attribute *);
1225
8cf6f0b1
TT
1226static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1227 struct dwarf2_loclist_baton *baton,
1228 struct attribute *attr);
1229
93e7bd98
DJ
1230static void dwarf2_symbol_mark_computed (struct attribute *attr,
1231 struct symbol *sym,
1232 struct dwarf2_cu *cu);
4c2df51b 1233
93311388
DE
1234static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1235 struct abbrev_info *abbrev,
1236 struct dwarf2_cu *cu);
4bb7a0a7 1237
72bf9492
DJ
1238static void free_stack_comp_unit (void *);
1239
72bf9492
DJ
1240static hashval_t partial_die_hash (const void *item);
1241
1242static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1243
ae038cb0 1244static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1245 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1246
1247static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1248 (unsigned int offset, struct objfile *objfile);
ae038cb0 1249
9816fde3
JK
1250static void init_one_comp_unit (struct dwarf2_cu *cu,
1251 struct objfile *objfile);
1252
1253static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1254 struct die_info *comp_unit_die);
93311388 1255
ae038cb0
DJ
1256static void free_one_comp_unit (void *);
1257
1258static void free_cached_comp_units (void *);
1259
1260static void age_cached_comp_units (void);
1261
1262static void free_one_cached_comp_unit (void *);
1263
f792889a
DJ
1264static struct type *set_die_type (struct die_info *, struct type *,
1265 struct dwarf2_cu *);
1c379e20 1266
ae038cb0
DJ
1267static void create_all_comp_units (struct objfile *);
1268
1fd400ff
TT
1269static int create_debug_types_hash_table (struct objfile *objfile);
1270
93311388
DE
1271static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1272 struct objfile *);
10b3939b
DJ
1273
1274static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1275
1276static void dwarf2_add_dependence (struct dwarf2_cu *,
1277 struct dwarf2_per_cu_data *);
1278
ae038cb0
DJ
1279static void dwarf2_mark (struct dwarf2_cu *);
1280
1281static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1282
673bfd45
DE
1283static struct type *get_die_type_at_offset (unsigned int,
1284 struct dwarf2_per_cu_data *per_cu);
1285
f792889a 1286static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1287
9291a0cd
TT
1288static void dwarf2_release_queue (void *dummy);
1289
1290static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1291 struct objfile *objfile);
1292
1293static void process_queue (struct objfile *objfile);
1294
1295static void find_file_and_directory (struct die_info *die,
1296 struct dwarf2_cu *cu,
1297 char **name, char **comp_dir);
1298
1299static char *file_full_name (int file, struct line_header *lh,
1300 const char *comp_dir);
1301
1302static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1303 gdb_byte *info_ptr,
1304 gdb_byte *buffer,
1305 unsigned int buffer_size,
1306 bfd *abfd);
1307
1308static void init_cu_die_reader (struct die_reader_specs *reader,
1309 struct dwarf2_cu *cu);
1310
673bfd45 1311static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1312
9291a0cd
TT
1313#if WORDS_BIGENDIAN
1314
1315/* Convert VALUE between big- and little-endian. */
1316static offset_type
1317byte_swap (offset_type value)
1318{
1319 offset_type result;
1320
1321 result = (value & 0xff) << 24;
1322 result |= (value & 0xff00) << 8;
1323 result |= (value & 0xff0000) >> 8;
1324 result |= (value & 0xff000000) >> 24;
1325 return result;
1326}
1327
1328#define MAYBE_SWAP(V) byte_swap (V)
1329
1330#else
1331#define MAYBE_SWAP(V) (V)
1332#endif /* WORDS_BIGENDIAN */
1333
1334/* The suffix for an index file. */
1335#define INDEX_SUFFIX ".gdb-index"
1336
3da10d80
KS
1337static const char *dwarf2_physname (char *name, struct die_info *die,
1338 struct dwarf2_cu *cu);
1339
c906108c
SS
1340/* Try to locate the sections we need for DWARF 2 debugging
1341 information and return true if we have enough to do something. */
1342
1343int
6502dd73 1344dwarf2_has_info (struct objfile *objfile)
c906108c 1345{
be391dca
TT
1346 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1347 if (!dwarf2_per_objfile)
1348 {
1349 /* Initialize per-objfile state. */
1350 struct dwarf2_per_objfile *data
1351 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1352
be391dca
TT
1353 memset (data, 0, sizeof (*data));
1354 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1355 dwarf2_per_objfile = data;
6502dd73 1356
be391dca
TT
1357 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1358 dwarf2_per_objfile->objfile = objfile;
1359 }
1360 return (dwarf2_per_objfile->info.asection != NULL
1361 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1362}
1363
233a11ab
CS
1364/* When loading sections, we can either look for ".<name>", or for
1365 * ".z<name>", which indicates a compressed section. */
1366
1367static int
dce234bc 1368section_is_p (const char *section_name, const char *name)
233a11ab 1369{
dce234bc
PP
1370 return (section_name[0] == '.'
1371 && (strcmp (section_name + 1, name) == 0
1372 || (section_name[1] == 'z'
1373 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1374}
1375
c906108c
SS
1376/* This function is mapped across the sections and remembers the
1377 offset and size of each of the debugging sections we are interested
1378 in. */
1379
1380static void
72dca2f5 1381dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1382{
dce234bc 1383 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1384 {
dce234bc
PP
1385 dwarf2_per_objfile->info.asection = sectp;
1386 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1387 }
dce234bc 1388 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1389 {
dce234bc
PP
1390 dwarf2_per_objfile->abbrev.asection = sectp;
1391 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1392 }
dce234bc 1393 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1394 {
dce234bc
PP
1395 dwarf2_per_objfile->line.asection = sectp;
1396 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1397 }
dce234bc 1398 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1399 {
dce234bc
PP
1400 dwarf2_per_objfile->loc.asection = sectp;
1401 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1402 }
dce234bc 1403 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1404 {
dce234bc
PP
1405 dwarf2_per_objfile->macinfo.asection = sectp;
1406 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1407 }
dce234bc 1408 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1409 {
dce234bc
PP
1410 dwarf2_per_objfile->str.asection = sectp;
1411 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1412 }
dce234bc 1413 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1414 {
dce234bc
PP
1415 dwarf2_per_objfile->frame.asection = sectp;
1416 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1417 }
dce234bc 1418 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1419 {
3799ccc6 1420 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1421
3799ccc6
EZ
1422 if (aflag & SEC_HAS_CONTENTS)
1423 {
dce234bc
PP
1424 dwarf2_per_objfile->eh_frame.asection = sectp;
1425 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1426 }
b6af0555 1427 }
dce234bc 1428 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1429 {
dce234bc
PP
1430 dwarf2_per_objfile->ranges.asection = sectp;
1431 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1432 }
348e048f
DE
1433 else if (section_is_p (sectp->name, TYPES_SECTION))
1434 {
1435 dwarf2_per_objfile->types.asection = sectp;
1436 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1437 }
9291a0cd
TT
1438 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1439 {
1440 dwarf2_per_objfile->gdb_index.asection = sectp;
1441 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1442 }
dce234bc 1443
72dca2f5
FR
1444 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1445 && bfd_section_vma (abfd, sectp) == 0)
1446 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1447}
1448
dce234bc
PP
1449/* Decompress a section that was compressed using zlib. Store the
1450 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1451
1452static void
dce234bc
PP
1453zlib_decompress_section (struct objfile *objfile, asection *sectp,
1454 gdb_byte **outbuf, bfd_size_type *outsize)
1455{
1456 bfd *abfd = objfile->obfd;
1457#ifndef HAVE_ZLIB_H
1458 error (_("Support for zlib-compressed DWARF data (from '%s') "
1459 "is disabled in this copy of GDB"),
1460 bfd_get_filename (abfd));
1461#else
1462 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1463 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1464 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1465 bfd_size_type uncompressed_size;
1466 gdb_byte *uncompressed_buffer;
1467 z_stream strm;
1468 int rc;
1469 int header_size = 12;
1470
1471 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1472 || bfd_bread (compressed_buffer,
1473 compressed_size, abfd) != compressed_size)
dce234bc
PP
1474 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1475 bfd_get_filename (abfd));
1476
1477 /* Read the zlib header. In this case, it should be "ZLIB" followed
1478 by the uncompressed section size, 8 bytes in big-endian order. */
1479 if (compressed_size < header_size
1480 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1481 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1482 bfd_get_filename (abfd));
1483 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1484 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1485 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1486 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1487 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1488 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1489 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1490 uncompressed_size += compressed_buffer[11];
1491
1492 /* It is possible the section consists of several compressed
1493 buffers concatenated together, so we uncompress in a loop. */
1494 strm.zalloc = NULL;
1495 strm.zfree = NULL;
1496 strm.opaque = NULL;
1497 strm.avail_in = compressed_size - header_size;
1498 strm.next_in = (Bytef*) compressed_buffer + header_size;
1499 strm.avail_out = uncompressed_size;
1500 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1501 uncompressed_size);
1502 rc = inflateInit (&strm);
1503 while (strm.avail_in > 0)
1504 {
1505 if (rc != Z_OK)
1506 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1507 bfd_get_filename (abfd), rc);
1508 strm.next_out = ((Bytef*) uncompressed_buffer
1509 + (uncompressed_size - strm.avail_out));
1510 rc = inflate (&strm, Z_FINISH);
1511 if (rc != Z_STREAM_END)
1512 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1513 bfd_get_filename (abfd), rc);
1514 rc = inflateReset (&strm);
1515 }
1516 rc = inflateEnd (&strm);
1517 if (rc != Z_OK
1518 || strm.avail_out != 0)
1519 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1520 bfd_get_filename (abfd), rc);
1521
affddf13 1522 do_cleanups (cleanup);
dce234bc
PP
1523 *outbuf = uncompressed_buffer;
1524 *outsize = uncompressed_size;
1525#endif
233a11ab
CS
1526}
1527
9e0ac564
TT
1528/* A helper function that decides whether a section is empty. */
1529
1530static int
1531dwarf2_section_empty_p (struct dwarf2_section_info *info)
1532{
1533 return info->asection == NULL || info->size == 0;
1534}
1535
dce234bc
PP
1536/* Read the contents of the section SECTP from object file specified by
1537 OBJFILE, store info about the section into INFO.
1538 If the section is compressed, uncompress it before returning. */
c906108c 1539
dce234bc
PP
1540static void
1541dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1542{
dce234bc
PP
1543 bfd *abfd = objfile->obfd;
1544 asection *sectp = info->asection;
1545 gdb_byte *buf, *retbuf;
1546 unsigned char header[4];
c906108c 1547
be391dca
TT
1548 if (info->readin)
1549 return;
dce234bc
PP
1550 info->buffer = NULL;
1551 info->was_mmapped = 0;
be391dca 1552 info->readin = 1;
188dd5d6 1553
9e0ac564 1554 if (dwarf2_section_empty_p (info))
dce234bc 1555 return;
c906108c 1556
dce234bc
PP
1557 /* Check if the file has a 4-byte header indicating compression. */
1558 if (info->size > sizeof (header)
1559 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1560 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1561 {
1562 /* Upon decompression, update the buffer and its size. */
1563 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1564 {
1565 zlib_decompress_section (objfile, sectp, &info->buffer,
1566 &info->size);
1567 return;
1568 }
1569 }
4bdf3d34 1570
dce234bc
PP
1571#ifdef HAVE_MMAP
1572 if (pagesize == 0)
1573 pagesize = getpagesize ();
2e276125 1574
dce234bc
PP
1575 /* Only try to mmap sections which are large enough: we don't want to
1576 waste space due to fragmentation. Also, only try mmap for sections
1577 without relocations. */
1578
1579 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1580 {
1581 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1582 size_t map_length = info->size + sectp->filepos - pg_offset;
1583 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1584 MAP_PRIVATE, pg_offset);
1585
1586 if (retbuf != MAP_FAILED)
1587 {
1588 info->was_mmapped = 1;
1589 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1590#if HAVE_POSIX_MADVISE
1591 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1592#endif
dce234bc
PP
1593 return;
1594 }
1595 }
1596#endif
1597
1598 /* If we get here, we are a normal, not-compressed section. */
1599 info->buffer = buf
1600 = obstack_alloc (&objfile->objfile_obstack, info->size);
1601
1602 /* When debugging .o files, we may need to apply relocations; see
1603 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1604 We never compress sections in .o files, so we only need to
1605 try this when the section is not compressed. */
ac8035ab 1606 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1607 if (retbuf != NULL)
1608 {
1609 info->buffer = retbuf;
1610 return;
1611 }
1612
1613 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1614 || bfd_bread (buf, info->size, abfd) != info->size)
1615 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1616 bfd_get_filename (abfd));
1617}
1618
9e0ac564
TT
1619/* A helper function that returns the size of a section in a safe way.
1620 If you are positive that the section has been read before using the
1621 size, then it is safe to refer to the dwarf2_section_info object's
1622 "size" field directly. In other cases, you must call this
1623 function, because for compressed sections the size field is not set
1624 correctly until the section has been read. */
1625
1626static bfd_size_type
1627dwarf2_section_size (struct objfile *objfile,
1628 struct dwarf2_section_info *info)
1629{
1630 if (!info->readin)
1631 dwarf2_read_section (objfile, info);
1632 return info->size;
1633}
1634
dce234bc 1635/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1636 SECTION_NAME. */
af34e669 1637
dce234bc
PP
1638void
1639dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1640 asection **sectp, gdb_byte **bufp,
1641 bfd_size_type *sizep)
1642{
1643 struct dwarf2_per_objfile *data
1644 = objfile_data (objfile, dwarf2_objfile_data_key);
1645 struct dwarf2_section_info *info;
a3b2a86b
TT
1646
1647 /* We may see an objfile without any DWARF, in which case we just
1648 return nothing. */
1649 if (data == NULL)
1650 {
1651 *sectp = NULL;
1652 *bufp = NULL;
1653 *sizep = 0;
1654 return;
1655 }
dce234bc
PP
1656 if (section_is_p (section_name, EH_FRAME_SECTION))
1657 info = &data->eh_frame;
1658 else if (section_is_p (section_name, FRAME_SECTION))
1659 info = &data->frame;
0d53c4c4 1660 else
f3574227 1661 gdb_assert_not_reached ("unexpected section");
dce234bc 1662
9e0ac564 1663 dwarf2_read_section (objfile, info);
dce234bc
PP
1664
1665 *sectp = info->asection;
1666 *bufp = info->buffer;
1667 *sizep = info->size;
1668}
1669
9291a0cd 1670\f
7b9f3c50
DE
1671/* DWARF quick_symbols_functions support. */
1672
1673/* TUs can share .debug_line entries, and there can be a lot more TUs than
1674 unique line tables, so we maintain a separate table of all .debug_line
1675 derived entries to support the sharing.
1676 All the quick functions need is the list of file names. We discard the
1677 line_header when we're done and don't need to record it here. */
1678struct quick_file_names
1679{
1680 /* The offset in .debug_line of the line table. We hash on this. */
1681 unsigned int offset;
1682
1683 /* The number of entries in file_names, real_names. */
1684 unsigned int num_file_names;
1685
1686 /* The file names from the line table, after being run through
1687 file_full_name. */
1688 const char **file_names;
1689
1690 /* The file names from the line table after being run through
1691 gdb_realpath. These are computed lazily. */
1692 const char **real_names;
1693};
1694
1695/* When using the index (and thus not using psymtabs), each CU has an
1696 object of this type. This is used to hold information needed by
1697 the various "quick" methods. */
1698struct dwarf2_per_cu_quick_data
1699{
1700 /* The file table. This can be NULL if there was no file table
1701 or it's currently not read in.
1702 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1703 struct quick_file_names *file_names;
1704
1705 /* The corresponding symbol table. This is NULL if symbols for this
1706 CU have not yet been read. */
1707 struct symtab *symtab;
1708
1709 /* A temporary mark bit used when iterating over all CUs in
1710 expand_symtabs_matching. */
1711 unsigned int mark : 1;
1712
1713 /* True if we've tried to read the file table and found there isn't one.
1714 There will be no point in trying to read it again next time. */
1715 unsigned int no_file_data : 1;
1716};
1717
1718/* Hash function for a quick_file_names. */
1719
1720static hashval_t
1721hash_file_name_entry (const void *e)
1722{
1723 const struct quick_file_names *file_data = e;
1724
1725 return file_data->offset;
1726}
1727
1728/* Equality function for a quick_file_names. */
1729
1730static int
1731eq_file_name_entry (const void *a, const void *b)
1732{
1733 const struct quick_file_names *ea = a;
1734 const struct quick_file_names *eb = b;
1735
1736 return ea->offset == eb->offset;
1737}
1738
1739/* Delete function for a quick_file_names. */
1740
1741static void
1742delete_file_name_entry (void *e)
1743{
1744 struct quick_file_names *file_data = e;
1745 int i;
1746
1747 for (i = 0; i < file_data->num_file_names; ++i)
1748 {
1749 xfree ((void*) file_data->file_names[i]);
1750 if (file_data->real_names)
1751 xfree ((void*) file_data->real_names[i]);
1752 }
1753
1754 /* The space for the struct itself lives on objfile_obstack,
1755 so we don't free it here. */
1756}
1757
1758/* Create a quick_file_names hash table. */
1759
1760static htab_t
1761create_quick_file_names_table (unsigned int nr_initial_entries)
1762{
1763 return htab_create_alloc (nr_initial_entries,
1764 hash_file_name_entry, eq_file_name_entry,
1765 delete_file_name_entry, xcalloc, xfree);
1766}
9291a0cd
TT
1767
1768/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1769 this CU came. */
2fdf6df6 1770
9291a0cd
TT
1771static void
1772dw2_do_instantiate_symtab (struct objfile *objfile,
1773 struct dwarf2_per_cu_data *per_cu)
1774{
1775 struct cleanup *back_to;
1776
1777 back_to = make_cleanup (dwarf2_release_queue, NULL);
1778
1779 queue_comp_unit (per_cu, objfile);
1780
1781 if (per_cu->from_debug_types)
1782 read_signatured_type_at_offset (objfile, per_cu->offset);
1783 else
1784 load_full_comp_unit (per_cu, objfile);
1785
1786 process_queue (objfile);
1787
1788 /* Age the cache, releasing compilation units that have not
1789 been used recently. */
1790 age_cached_comp_units ();
1791
1792 do_cleanups (back_to);
1793}
1794
1795/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1796 the objfile from which this CU came. Returns the resulting symbol
1797 table. */
2fdf6df6 1798
9291a0cd
TT
1799static struct symtab *
1800dw2_instantiate_symtab (struct objfile *objfile,
1801 struct dwarf2_per_cu_data *per_cu)
1802{
1803 if (!per_cu->v.quick->symtab)
1804 {
1805 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1806 increment_reading_symtab ();
1807 dw2_do_instantiate_symtab (objfile, per_cu);
1808 do_cleanups (back_to);
1809 }
1810 return per_cu->v.quick->symtab;
1811}
1812
1fd400ff 1813/* Return the CU given its index. */
2fdf6df6 1814
1fd400ff
TT
1815static struct dwarf2_per_cu_data *
1816dw2_get_cu (int index)
1817{
1818 if (index >= dwarf2_per_objfile->n_comp_units)
1819 {
1820 index -= dwarf2_per_objfile->n_comp_units;
1821 return dwarf2_per_objfile->type_comp_units[index];
1822 }
1823 return dwarf2_per_objfile->all_comp_units[index];
1824}
1825
9291a0cd
TT
1826/* A helper function that knows how to read a 64-bit value in a way
1827 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1828 otherwise. */
2fdf6df6 1829
9291a0cd
TT
1830static int
1831extract_cu_value (const char *bytes, ULONGEST *result)
1832{
1833 if (sizeof (ULONGEST) < 8)
1834 {
1835 int i;
1836
1837 /* Ignore the upper 4 bytes if they are all zero. */
1838 for (i = 0; i < 4; ++i)
1839 if (bytes[i + 4] != 0)
1840 return 0;
1841
1842 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1843 }
1844 else
1845 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1846 return 1;
1847}
1848
1849/* Read the CU list from the mapped index, and use it to create all
1850 the CU objects for this objfile. Return 0 if something went wrong,
1851 1 if everything went ok. */
2fdf6df6 1852
9291a0cd 1853static int
1fd400ff
TT
1854create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1855 offset_type cu_list_elements)
9291a0cd
TT
1856{
1857 offset_type i;
9291a0cd
TT
1858
1859 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1860 dwarf2_per_objfile->all_comp_units
1861 = obstack_alloc (&objfile->objfile_obstack,
1862 dwarf2_per_objfile->n_comp_units
1863 * sizeof (struct dwarf2_per_cu_data *));
1864
1865 for (i = 0; i < cu_list_elements; i += 2)
1866 {
1867 struct dwarf2_per_cu_data *the_cu;
1868 ULONGEST offset, length;
1869
1870 if (!extract_cu_value (cu_list, &offset)
1871 || !extract_cu_value (cu_list + 8, &length))
1872 return 0;
1873 cu_list += 2 * 8;
1874
1875 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1876 struct dwarf2_per_cu_data);
1877 the_cu->offset = offset;
1878 the_cu->length = length;
1879 the_cu->objfile = objfile;
1880 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1881 struct dwarf2_per_cu_quick_data);
1882 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1883 }
1884
1885 return 1;
1886}
1887
1fd400ff 1888/* Create the signatured type hash table from the index. */
673bfd45 1889
1fd400ff 1890static int
673bfd45
DE
1891create_signatured_type_table_from_index (struct objfile *objfile,
1892 const gdb_byte *bytes,
1893 offset_type elements)
1fd400ff
TT
1894{
1895 offset_type i;
673bfd45 1896 htab_t sig_types_hash;
1fd400ff
TT
1897
1898 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1899 dwarf2_per_objfile->type_comp_units
1900 = obstack_alloc (&objfile->objfile_obstack,
1901 dwarf2_per_objfile->n_type_comp_units
1902 * sizeof (struct dwarf2_per_cu_data *));
1903
673bfd45 1904 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1905
1906 for (i = 0; i < elements; i += 3)
1907 {
1908 struct signatured_type *type_sig;
1909 ULONGEST offset, type_offset, signature;
1910 void **slot;
1911
1912 if (!extract_cu_value (bytes, &offset)
1913 || !extract_cu_value (bytes + 8, &type_offset))
1914 return 0;
1915 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1916 bytes += 3 * 8;
1917
1918 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1919 struct signatured_type);
1920 type_sig->signature = signature;
1921 type_sig->offset = offset;
1922 type_sig->type_offset = type_offset;
1923 type_sig->per_cu.from_debug_types = 1;
1924 type_sig->per_cu.offset = offset;
1925 type_sig->per_cu.objfile = objfile;
1926 type_sig->per_cu.v.quick
1927 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1928 struct dwarf2_per_cu_quick_data);
1929
673bfd45 1930 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1931 *slot = type_sig;
1932
1933 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1934 }
1935
673bfd45 1936 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1937
1938 return 1;
1939}
1940
9291a0cd
TT
1941/* Read the address map data from the mapped index, and use it to
1942 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1943
9291a0cd
TT
1944static void
1945create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1946{
1947 const gdb_byte *iter, *end;
1948 struct obstack temp_obstack;
1949 struct addrmap *mutable_map;
1950 struct cleanup *cleanup;
1951 CORE_ADDR baseaddr;
1952
1953 obstack_init (&temp_obstack);
1954 cleanup = make_cleanup_obstack_free (&temp_obstack);
1955 mutable_map = addrmap_create_mutable (&temp_obstack);
1956
1957 iter = index->address_table;
1958 end = iter + index->address_table_size;
1959
1960 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1961
1962 while (iter < end)
1963 {
1964 ULONGEST hi, lo, cu_index;
1965 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1966 iter += 8;
1967 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1968 iter += 8;
1969 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1970 iter += 4;
1971
1972 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1973 dw2_get_cu (cu_index));
9291a0cd
TT
1974 }
1975
1976 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1977 &objfile->objfile_obstack);
1978 do_cleanups (cleanup);
1979}
1980
59d7bcaf
JK
1981/* The hash function for strings in the mapped index. This is the same as
1982 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
1983 implementation. This is necessary because the hash function is tied to the
1984 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
1985 SYMBOL_HASH_NEXT.
1986
1987 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 1988
9291a0cd 1989static hashval_t
559a7a62 1990mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
1991{
1992 const unsigned char *str = (const unsigned char *) p;
1993 hashval_t r = 0;
1994 unsigned char c;
1995
1996 while ((c = *str++) != 0)
559a7a62
JK
1997 {
1998 if (index_version >= 5)
1999 c = tolower (c);
2000 r = r * 67 + c - 113;
2001 }
9291a0cd
TT
2002
2003 return r;
2004}
2005
2006/* Find a slot in the mapped index INDEX for the object named NAME.
2007 If NAME is found, set *VEC_OUT to point to the CU vector in the
2008 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2009
9291a0cd
TT
2010static int
2011find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2012 offset_type **vec_out)
2013{
0cf03b49
JK
2014 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2015 offset_type hash;
9291a0cd 2016 offset_type slot, step;
559a7a62 2017 int (*cmp) (const char *, const char *);
9291a0cd 2018
0cf03b49
JK
2019 if (current_language->la_language == language_cplus
2020 || current_language->la_language == language_java
2021 || current_language->la_language == language_fortran)
2022 {
2023 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2024 not contain any. */
2025 const char *paren = strchr (name, '(');
2026
2027 if (paren)
2028 {
2029 char *dup;
2030
2031 dup = xmalloc (paren - name + 1);
2032 memcpy (dup, name, paren - name);
2033 dup[paren - name] = 0;
2034
2035 make_cleanup (xfree, dup);
2036 name = dup;
2037 }
2038 }
2039
559a7a62
JK
2040 /* Index version 4 did not support case insensitive searches. But the
2041 indexes for case insensitive languages are built in lowercase, therefore
2042 simulate our NAME being searched is also lowercased. */
2043 hash = mapped_index_string_hash ((index->version == 4
2044 && case_sensitivity == case_sensitive_off
2045 ? 5 : index->version),
2046 name);
2047
3876f04e
DE
2048 slot = hash & (index->symbol_table_slots - 1);
2049 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2050 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2051
2052 for (;;)
2053 {
2054 /* Convert a slot number to an offset into the table. */
2055 offset_type i = 2 * slot;
2056 const char *str;
3876f04e 2057 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2058 {
2059 do_cleanups (back_to);
2060 return 0;
2061 }
9291a0cd 2062
3876f04e 2063 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2064 if (!cmp (name, str))
9291a0cd
TT
2065 {
2066 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2067 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2068 do_cleanups (back_to);
9291a0cd
TT
2069 return 1;
2070 }
2071
3876f04e 2072 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2073 }
2074}
2075
2076/* Read the index file. If everything went ok, initialize the "quick"
2077 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2078
9291a0cd
TT
2079static int
2080dwarf2_read_index (struct objfile *objfile)
2081{
9291a0cd
TT
2082 char *addr;
2083 struct mapped_index *map;
b3b272e1 2084 offset_type *metadata;
ac0b195c
KW
2085 const gdb_byte *cu_list;
2086 const gdb_byte *types_list = NULL;
2087 offset_type version, cu_list_elements;
2088 offset_type types_list_elements = 0;
1fd400ff 2089 int i;
9291a0cd 2090
9e0ac564 2091 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2092 return 0;
82430852
JK
2093
2094 /* Older elfutils strip versions could keep the section in the main
2095 executable while splitting it for the separate debug info file. */
2096 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2097 & SEC_HAS_CONTENTS) == 0)
2098 return 0;
2099
9291a0cd
TT
2100 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2101
2102 addr = dwarf2_per_objfile->gdb_index.buffer;
2103 /* Version check. */
1fd400ff 2104 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2105 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2106 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2107 contained incomplete addrmap. So, it seems better to just ignore such
559a7a62
JK
2108 indices. Index version 4 uses a different hash function than index
2109 version 5 and later. */
831adc1f 2110 if (version < 4)
9291a0cd 2111 return 0;
594e8718
JK
2112 /* Indexes with higher version than the one supported by GDB may be no
2113 longer backward compatible. */
559a7a62 2114 if (version > 5)
594e8718 2115 return 0;
9291a0cd
TT
2116
2117 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2118 map->version = version;
b3b272e1 2119 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2120
2121 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2122
2123 i = 0;
2124 cu_list = addr + MAYBE_SWAP (metadata[i]);
2125 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2126 / 8);
1fd400ff
TT
2127 ++i;
2128
987d643c
TT
2129 types_list = addr + MAYBE_SWAP (metadata[i]);
2130 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2131 - MAYBE_SWAP (metadata[i]))
2132 / 8);
2133 ++i;
1fd400ff
TT
2134
2135 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2136 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2137 - MAYBE_SWAP (metadata[i]));
2138 ++i;
2139
3876f04e
DE
2140 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2141 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2142 - MAYBE_SWAP (metadata[i]))
2143 / (2 * sizeof (offset_type)));
1fd400ff 2144 ++i;
9291a0cd 2145
1fd400ff
TT
2146 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2147
2148 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2149 return 0;
2150
987d643c 2151 if (types_list_elements
673bfd45
DE
2152 && !create_signatured_type_table_from_index (objfile, types_list,
2153 types_list_elements))
9291a0cd
TT
2154 return 0;
2155
2156 create_addrmap_from_index (objfile, map);
2157
2158 dwarf2_per_objfile->index_table = map;
2159 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2160 dwarf2_per_objfile->quick_file_names_table =
2161 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2162
2163 return 1;
2164}
2165
2166/* A helper for the "quick" functions which sets the global
2167 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2168
9291a0cd
TT
2169static void
2170dw2_setup (struct objfile *objfile)
2171{
2172 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2173 gdb_assert (dwarf2_per_objfile);
2174}
2175
2176/* A helper for the "quick" functions which attempts to read the line
2177 table for THIS_CU. */
2fdf6df6 2178
7b9f3c50
DE
2179static struct quick_file_names *
2180dw2_get_file_names (struct objfile *objfile,
2181 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2182{
2183 bfd *abfd = objfile->obfd;
7b9f3c50 2184 struct line_header *lh;
9291a0cd
TT
2185 struct attribute *attr;
2186 struct cleanup *cleanups;
2187 struct die_info *comp_unit_die;
36374493 2188 struct dwarf2_section_info* sec;
9291a0cd
TT
2189 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2190 int has_children, i;
2191 struct dwarf2_cu cu;
2192 unsigned int bytes_read, buffer_size;
2193 struct die_reader_specs reader_specs;
2194 char *name, *comp_dir;
7b9f3c50
DE
2195 void **slot;
2196 struct quick_file_names *qfn;
2197 unsigned int line_offset;
9291a0cd 2198
7b9f3c50
DE
2199 if (this_cu->v.quick->file_names != NULL)
2200 return this_cu->v.quick->file_names;
2201 /* If we know there is no line data, no point in looking again. */
2202 if (this_cu->v.quick->no_file_data)
2203 return NULL;
9291a0cd 2204
9816fde3 2205 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2206 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2207
36374493
DE
2208 if (this_cu->from_debug_types)
2209 sec = &dwarf2_per_objfile->types;
2210 else
2211 sec = &dwarf2_per_objfile->info;
2212 dwarf2_read_section (objfile, sec);
2213 buffer_size = sec->size;
2214 buffer = sec->buffer;
9291a0cd
TT
2215 info_ptr = buffer + this_cu->offset;
2216 beg_of_comp_unit = info_ptr;
2217
2218 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2219 buffer, buffer_size,
2220 abfd);
2221
2222 /* Complete the cu_header. */
2223 cu.header.offset = beg_of_comp_unit - buffer;
2224 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2225
2226 this_cu->cu = &cu;
2227 cu.per_cu = this_cu;
2228
2229 dwarf2_read_abbrevs (abfd, &cu);
2230 make_cleanup (dwarf2_free_abbrev_table, &cu);
2231
2232 if (this_cu->from_debug_types)
2233 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2234 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2235 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2236 &has_children);
9291a0cd 2237
7b9f3c50
DE
2238 lh = NULL;
2239 slot = NULL;
2240 line_offset = 0;
9291a0cd
TT
2241 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2242 if (attr)
2243 {
7b9f3c50
DE
2244 struct quick_file_names find_entry;
2245
2246 line_offset = DW_UNSND (attr);
2247
2248 /* We may have already read in this line header (TU line header sharing).
2249 If we have we're done. */
2250 find_entry.offset = line_offset;
2251 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2252 &find_entry, INSERT);
2253 if (*slot != NULL)
2254 {
2255 do_cleanups (cleanups);
2256 this_cu->v.quick->file_names = *slot;
2257 return *slot;
2258 }
2259
9291a0cd
TT
2260 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2261 }
2262 if (lh == NULL)
2263 {
2264 do_cleanups (cleanups);
7b9f3c50
DE
2265 this_cu->v.quick->no_file_data = 1;
2266 return NULL;
9291a0cd
TT
2267 }
2268
7b9f3c50
DE
2269 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2270 qfn->offset = line_offset;
2271 gdb_assert (slot != NULL);
2272 *slot = qfn;
9291a0cd 2273
7b9f3c50 2274 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2275
7b9f3c50
DE
2276 qfn->num_file_names = lh->num_file_names;
2277 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2278 lh->num_file_names * sizeof (char *));
9291a0cd 2279 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2280 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2281 qfn->real_names = NULL;
9291a0cd 2282
7b9f3c50 2283 free_line_header (lh);
9291a0cd 2284 do_cleanups (cleanups);
7b9f3c50
DE
2285
2286 this_cu->v.quick->file_names = qfn;
2287 return qfn;
9291a0cd
TT
2288}
2289
2290/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2291 real path for a given file name from the line table. */
2fdf6df6 2292
9291a0cd 2293static const char *
7b9f3c50
DE
2294dw2_get_real_path (struct objfile *objfile,
2295 struct quick_file_names *qfn, int index)
9291a0cd 2296{
7b9f3c50
DE
2297 if (qfn->real_names == NULL)
2298 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2299 qfn->num_file_names, sizeof (char *));
9291a0cd 2300
7b9f3c50
DE
2301 if (qfn->real_names[index] == NULL)
2302 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2303
7b9f3c50 2304 return qfn->real_names[index];
9291a0cd
TT
2305}
2306
2307static struct symtab *
2308dw2_find_last_source_symtab (struct objfile *objfile)
2309{
2310 int index;
ae2de4f8 2311
9291a0cd
TT
2312 dw2_setup (objfile);
2313 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2314 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2315}
2316
7b9f3c50
DE
2317/* Traversal function for dw2_forget_cached_source_info. */
2318
2319static int
2320dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2321{
7b9f3c50 2322 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2323
7b9f3c50 2324 if (file_data->real_names)
9291a0cd 2325 {
7b9f3c50 2326 int i;
9291a0cd 2327
7b9f3c50 2328 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2329 {
7b9f3c50
DE
2330 xfree ((void*) file_data->real_names[i]);
2331 file_data->real_names[i] = NULL;
9291a0cd
TT
2332 }
2333 }
7b9f3c50
DE
2334
2335 return 1;
2336}
2337
2338static void
2339dw2_forget_cached_source_info (struct objfile *objfile)
2340{
2341 dw2_setup (objfile);
2342
2343 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2344 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2345}
2346
2347static int
2348dw2_lookup_symtab (struct objfile *objfile, const char *name,
2349 const char *full_path, const char *real_path,
2350 struct symtab **result)
2351{
2352 int i;
2353 int check_basename = lbasename (name) == name;
2354 struct dwarf2_per_cu_data *base_cu = NULL;
2355
2356 dw2_setup (objfile);
ae2de4f8 2357
1fd400ff
TT
2358 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2359 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2360 {
2361 int j;
e254ef6a 2362 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2363 struct quick_file_names *file_data;
9291a0cd 2364
e254ef6a 2365 if (per_cu->v.quick->symtab)
9291a0cd
TT
2366 continue;
2367
7b9f3c50
DE
2368 file_data = dw2_get_file_names (objfile, per_cu);
2369 if (file_data == NULL)
9291a0cd
TT
2370 continue;
2371
7b9f3c50 2372 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2373 {
7b9f3c50 2374 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2375
2376 if (FILENAME_CMP (name, this_name) == 0)
2377 {
e254ef6a 2378 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2379 return 1;
2380 }
2381
2382 if (check_basename && ! base_cu
2383 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2384 base_cu = per_cu;
9291a0cd
TT
2385
2386 if (full_path != NULL)
2387 {
7b9f3c50
DE
2388 const char *this_real_name = dw2_get_real_path (objfile,
2389 file_data, j);
9291a0cd 2390
7b9f3c50
DE
2391 if (this_real_name != NULL
2392 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2393 {
e254ef6a 2394 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2395 return 1;
2396 }
2397 }
2398
2399 if (real_path != NULL)
2400 {
7b9f3c50
DE
2401 const char *this_real_name = dw2_get_real_path (objfile,
2402 file_data, j);
9291a0cd 2403
7b9f3c50
DE
2404 if (this_real_name != NULL
2405 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2406 {
74dd2ca6
DE
2407 *result = dw2_instantiate_symtab (objfile, per_cu);
2408 return 1;
9291a0cd
TT
2409 }
2410 }
2411 }
2412 }
2413
2414 if (base_cu)
2415 {
2416 *result = dw2_instantiate_symtab (objfile, base_cu);
2417 return 1;
2418 }
2419
2420 return 0;
2421}
2422
2423static struct symtab *
2424dw2_lookup_symbol (struct objfile *objfile, int block_index,
2425 const char *name, domain_enum domain)
2426{
774b6a14 2427 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2428 instead. */
2429 return NULL;
2430}
2431
2432/* A helper function that expands all symtabs that hold an object
2433 named NAME. */
2fdf6df6 2434
9291a0cd
TT
2435static void
2436dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2437{
2438 dw2_setup (objfile);
2439
ae2de4f8 2440 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2441 if (dwarf2_per_objfile->index_table)
2442 {
2443 offset_type *vec;
2444
2445 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2446 name, &vec))
2447 {
2448 offset_type i, len = MAYBE_SWAP (*vec);
2449 for (i = 0; i < len; ++i)
2450 {
2451 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2452 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2453
e254ef6a 2454 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2455 }
2456 }
2457 }
2458}
2459
774b6a14
TT
2460static void
2461dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2462 enum block_enum block_kind, const char *name,
774b6a14 2463 domain_enum domain)
9291a0cd 2464{
774b6a14 2465 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2466}
2467
2468static void
2469dw2_print_stats (struct objfile *objfile)
2470{
2471 int i, count;
2472
2473 dw2_setup (objfile);
2474 count = 0;
1fd400ff
TT
2475 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2476 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2477 {
e254ef6a 2478 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2479
e254ef6a 2480 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2481 ++count;
2482 }
2483 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2484}
2485
2486static void
2487dw2_dump (struct objfile *objfile)
2488{
2489 /* Nothing worth printing. */
2490}
2491
2492static void
2493dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2494 struct section_offsets *delta)
2495{
2496 /* There's nothing to relocate here. */
2497}
2498
2499static void
2500dw2_expand_symtabs_for_function (struct objfile *objfile,
2501 const char *func_name)
2502{
2503 dw2_do_expand_symtabs_matching (objfile, func_name);
2504}
2505
2506static void
2507dw2_expand_all_symtabs (struct objfile *objfile)
2508{
2509 int i;
2510
2511 dw2_setup (objfile);
1fd400ff
TT
2512
2513 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2514 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2515 {
e254ef6a 2516 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2517
e254ef6a 2518 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2519 }
2520}
2521
2522static void
2523dw2_expand_symtabs_with_filename (struct objfile *objfile,
2524 const char *filename)
2525{
2526 int i;
2527
2528 dw2_setup (objfile);
d4637a04
DE
2529
2530 /* We don't need to consider type units here.
2531 This is only called for examining code, e.g. expand_line_sal.
2532 There can be an order of magnitude (or more) more type units
2533 than comp units, and we avoid them if we can. */
2534
2535 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2536 {
2537 int j;
e254ef6a 2538 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2539 struct quick_file_names *file_data;
9291a0cd 2540
e254ef6a 2541 if (per_cu->v.quick->symtab)
9291a0cd
TT
2542 continue;
2543
7b9f3c50
DE
2544 file_data = dw2_get_file_names (objfile, per_cu);
2545 if (file_data == NULL)
9291a0cd
TT
2546 continue;
2547
7b9f3c50 2548 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2549 {
7b9f3c50 2550 const char *this_name = file_data->file_names[j];
1ef75ecc 2551 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2552 {
e254ef6a 2553 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2554 break;
2555 }
2556 }
2557 }
2558}
2559
dd786858 2560static const char *
9291a0cd
TT
2561dw2_find_symbol_file (struct objfile *objfile, const char *name)
2562{
e254ef6a 2563 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2564 offset_type *vec;
7b9f3c50 2565 struct quick_file_names *file_data;
9291a0cd
TT
2566
2567 dw2_setup (objfile);
2568
ae2de4f8 2569 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2570 if (!dwarf2_per_objfile->index_table)
2571 return NULL;
2572
2573 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2574 name, &vec))
2575 return NULL;
2576
2577 /* Note that this just looks at the very first one named NAME -- but
2578 actually we are looking for a function. find_main_filename
2579 should be rewritten so that it doesn't require a custom hook. It
2580 could just use the ordinary symbol tables. */
2581 /* vec[0] is the length, which must always be >0. */
e254ef6a 2582 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2583
7b9f3c50
DE
2584 file_data = dw2_get_file_names (objfile, per_cu);
2585 if (file_data == NULL)
9291a0cd
TT
2586 return NULL;
2587
7b9f3c50 2588 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2589}
2590
2591static void
40658b94
PH
2592dw2_map_matching_symbols (const char * name, domain_enum namespace,
2593 struct objfile *objfile, int global,
2594 int (*callback) (struct block *,
2595 struct symbol *, void *),
2edb89d3
JK
2596 void *data, symbol_compare_ftype *match,
2597 symbol_compare_ftype *ordered_compare)
9291a0cd 2598{
40658b94 2599 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2600 current language is Ada for a non-Ada objfile using GNU index. As Ada
2601 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2602}
2603
2604static void
2605dw2_expand_symtabs_matching (struct objfile *objfile,
2606 int (*file_matcher) (const char *, void *),
2607 int (*name_matcher) (const char *, void *),
8903c50d 2608 enum search_domain kind,
9291a0cd
TT
2609 void *data)
2610{
2611 int i;
2612 offset_type iter;
4b5246aa 2613 struct mapped_index *index;
9291a0cd
TT
2614
2615 dw2_setup (objfile);
ae2de4f8
DE
2616
2617 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2618 if (!dwarf2_per_objfile->index_table)
2619 return;
4b5246aa 2620 index = dwarf2_per_objfile->index_table;
9291a0cd 2621
7b08b9eb
JK
2622 if (file_matcher != NULL)
2623 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2624 + dwarf2_per_objfile->n_type_comp_units); ++i)
2625 {
2626 int j;
2627 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2628 struct quick_file_names *file_data;
2629
2630 per_cu->v.quick->mark = 0;
2631 if (per_cu->v.quick->symtab)
2632 continue;
2633
2634 file_data = dw2_get_file_names (objfile, per_cu);
2635 if (file_data == NULL)
2636 continue;
2637
2638 for (j = 0; j < file_data->num_file_names; ++j)
2639 {
2640 if (file_matcher (file_data->file_names[j], data))
2641 {
2642 per_cu->v.quick->mark = 1;
2643 break;
2644 }
2645 }
2646 }
9291a0cd 2647
3876f04e 2648 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2649 {
2650 offset_type idx = 2 * iter;
2651 const char *name;
2652 offset_type *vec, vec_len, vec_idx;
2653
3876f04e 2654 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2655 continue;
2656
3876f04e 2657 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2658
2659 if (! (*name_matcher) (name, data))
2660 continue;
2661
2662 /* The name was matched, now expand corresponding CUs that were
2663 marked. */
4b5246aa 2664 vec = (offset_type *) (index->constant_pool
3876f04e 2665 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2666 vec_len = MAYBE_SWAP (vec[0]);
2667 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2668 {
e254ef6a 2669 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2670
e254ef6a 2671 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2672 if (file_matcher == NULL || per_cu->v.quick->mark)
e254ef6a 2673 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2674 }
2675 }
2676}
2677
2678static struct symtab *
2679dw2_find_pc_sect_symtab (struct objfile *objfile,
2680 struct minimal_symbol *msymbol,
2681 CORE_ADDR pc,
2682 struct obj_section *section,
2683 int warn_if_readin)
2684{
2685 struct dwarf2_per_cu_data *data;
2686
2687 dw2_setup (objfile);
2688
2689 if (!objfile->psymtabs_addrmap)
2690 return NULL;
2691
2692 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2693 if (!data)
2694 return NULL;
2695
2696 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2697 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2698 paddress (get_objfile_arch (objfile), pc));
2699
2700 return dw2_instantiate_symtab (objfile, data);
2701}
2702
9291a0cd
TT
2703static void
2704dw2_map_symbol_filenames (struct objfile *objfile,
2705 void (*fun) (const char *, const char *, void *),
2706 void *data)
2707{
2708 int i;
2709
2710 dw2_setup (objfile);
ae2de4f8 2711
1fd400ff
TT
2712 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2713 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2714 {
2715 int j;
e254ef6a 2716 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2717 struct quick_file_names *file_data;
9291a0cd 2718
e254ef6a 2719 if (per_cu->v.quick->symtab)
9291a0cd
TT
2720 continue;
2721
7b9f3c50
DE
2722 file_data = dw2_get_file_names (objfile, per_cu);
2723 if (file_data == NULL)
9291a0cd
TT
2724 continue;
2725
7b9f3c50 2726 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2727 {
7b9f3c50
DE
2728 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2729 j);
2730 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2731 }
2732 }
2733}
2734
2735static int
2736dw2_has_symbols (struct objfile *objfile)
2737{
2738 return 1;
2739}
2740
2741const struct quick_symbol_functions dwarf2_gdb_index_functions =
2742{
2743 dw2_has_symbols,
2744 dw2_find_last_source_symtab,
2745 dw2_forget_cached_source_info,
2746 dw2_lookup_symtab,
2747 dw2_lookup_symbol,
774b6a14 2748 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2749 dw2_print_stats,
2750 dw2_dump,
2751 dw2_relocate,
2752 dw2_expand_symtabs_for_function,
2753 dw2_expand_all_symtabs,
2754 dw2_expand_symtabs_with_filename,
2755 dw2_find_symbol_file,
40658b94 2756 dw2_map_matching_symbols,
9291a0cd
TT
2757 dw2_expand_symtabs_matching,
2758 dw2_find_pc_sect_symtab,
9291a0cd
TT
2759 dw2_map_symbol_filenames
2760};
2761
2762/* Initialize for reading DWARF for this objfile. Return 0 if this
2763 file will use psymtabs, or 1 if using the GNU index. */
2764
2765int
2766dwarf2_initialize_objfile (struct objfile *objfile)
2767{
2768 /* If we're about to read full symbols, don't bother with the
2769 indices. In this case we also don't care if some other debug
2770 format is making psymtabs, because they are all about to be
2771 expanded anyway. */
2772 if ((objfile->flags & OBJF_READNOW))
2773 {
2774 int i;
2775
2776 dwarf2_per_objfile->using_index = 1;
2777 create_all_comp_units (objfile);
1fd400ff 2778 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2779 dwarf2_per_objfile->quick_file_names_table =
2780 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2781
1fd400ff
TT
2782 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2783 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2784 {
e254ef6a 2785 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2786
e254ef6a
DE
2787 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2788 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2789 }
2790
2791 /* Return 1 so that gdb sees the "quick" functions. However,
2792 these functions will be no-ops because we will have expanded
2793 all symtabs. */
2794 return 1;
2795 }
2796
2797 if (dwarf2_read_index (objfile))
2798 return 1;
2799
9291a0cd
TT
2800 return 0;
2801}
2802
2803\f
2804
dce234bc
PP
2805/* Build a partial symbol table. */
2806
2807void
f29dff0a 2808dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2809{
f29dff0a 2810 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2811 {
2812 init_psymbol_list (objfile, 1024);
2813 }
2814
d146bf1e 2815 dwarf2_build_psymtabs_hard (objfile);
c906108c 2816}
c906108c 2817
45452591
DE
2818/* Return TRUE if OFFSET is within CU_HEADER. */
2819
2820static inline int
2821offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2822{
2823 unsigned int bottom = cu_header->offset;
2824 unsigned int top = (cu_header->offset
2825 + cu_header->length
2826 + cu_header->initial_length_size);
9a619af0 2827
45452591
DE
2828 return (offset >= bottom && offset < top);
2829}
2830
93311388
DE
2831/* Read in the comp unit header information from the debug_info at info_ptr.
2832 NOTE: This leaves members offset, first_die_offset to be filled in
2833 by the caller. */
107d2387 2834
fe1b8b76 2835static gdb_byte *
107d2387 2836read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2837 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2838{
2839 int signed_addr;
891d2f0b 2840 unsigned int bytes_read;
c764a876
DE
2841
2842 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2843 cu_header->initial_length_size = bytes_read;
2844 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2845 info_ptr += bytes_read;
107d2387
AC
2846 cu_header->version = read_2_bytes (abfd, info_ptr);
2847 info_ptr += 2;
613e1657 2848 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2849 &bytes_read);
613e1657 2850 info_ptr += bytes_read;
107d2387
AC
2851 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2852 info_ptr += 1;
2853 signed_addr = bfd_get_sign_extend_vma (abfd);
2854 if (signed_addr < 0)
8e65ff28 2855 internal_error (__FILE__, __LINE__,
e2e0b3e5 2856 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2857 cu_header->signed_addr_p = signed_addr;
c764a876 2858
107d2387
AC
2859 return info_ptr;
2860}
2861
fe1b8b76
JB
2862static gdb_byte *
2863partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2864 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2865 bfd *abfd)
2866{
fe1b8b76 2867 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2868
2869 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2870
2dc7f7b3 2871 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2872 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2873 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2874 bfd_get_filename (abfd));
72bf9492 2875
9e0ac564
TT
2876 if (header->abbrev_offset
2877 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2878 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
2879 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2880 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2881 (long) header->abbrev_offset,
93311388 2882 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2883 bfd_get_filename (abfd));
2884
2885 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2886 > buffer + buffer_size)
8a3fe4f8
AC
2887 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2888 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2889 (long) header->length,
93311388 2890 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2891 bfd_get_filename (abfd));
2892
2893 return info_ptr;
2894}
2895
348e048f
DE
2896/* Read in the types comp unit header information from .debug_types entry at
2897 types_ptr. The result is a pointer to one past the end of the header. */
2898
2899static gdb_byte *
2900read_type_comp_unit_head (struct comp_unit_head *cu_header,
2901 ULONGEST *signature,
2902 gdb_byte *types_ptr, bfd *abfd)
2903{
348e048f
DE
2904 gdb_byte *initial_types_ptr = types_ptr;
2905
6e70227d 2906 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2907 &dwarf2_per_objfile->types);
348e048f
DE
2908 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2909
2910 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2911
2912 *signature = read_8_bytes (abfd, types_ptr);
2913 types_ptr += 8;
2914 types_ptr += cu_header->offset_size;
2915 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2916
2917 return types_ptr;
2918}
2919
aaa75496
JB
2920/* Allocate a new partial symtab for file named NAME and mark this new
2921 partial symtab as being an include of PST. */
2922
2923static void
2924dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2925 struct objfile *objfile)
2926{
2927 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2928
2929 subpst->section_offsets = pst->section_offsets;
2930 subpst->textlow = 0;
2931 subpst->texthigh = 0;
2932
2933 subpst->dependencies = (struct partial_symtab **)
2934 obstack_alloc (&objfile->objfile_obstack,
2935 sizeof (struct partial_symtab *));
2936 subpst->dependencies[0] = pst;
2937 subpst->number_of_dependencies = 1;
2938
2939 subpst->globals_offset = 0;
2940 subpst->n_global_syms = 0;
2941 subpst->statics_offset = 0;
2942 subpst->n_static_syms = 0;
2943 subpst->symtab = NULL;
2944 subpst->read_symtab = pst->read_symtab;
2945 subpst->readin = 0;
2946
2947 /* No private part is necessary for include psymtabs. This property
2948 can be used to differentiate between such include psymtabs and
10b3939b 2949 the regular ones. */
58a9656e 2950 subpst->read_symtab_private = NULL;
aaa75496
JB
2951}
2952
2953/* Read the Line Number Program data and extract the list of files
2954 included by the source file represented by PST. Build an include
d85a05f0 2955 partial symtab for each of these included files. */
aaa75496
JB
2956
2957static void
2958dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2959 struct die_info *die,
aaa75496
JB
2960 struct partial_symtab *pst)
2961{
2962 struct objfile *objfile = cu->objfile;
2963 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2964 struct line_header *lh = NULL;
2965 struct attribute *attr;
aaa75496 2966
d85a05f0
DJ
2967 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2968 if (attr)
2969 {
2970 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2971
d85a05f0
DJ
2972 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2973 }
aaa75496
JB
2974 if (lh == NULL)
2975 return; /* No linetable, so no includes. */
2976
c6da4cef
DE
2977 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2978 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2979
2980 free_line_header (lh);
2981}
2982
348e048f
DE
2983static hashval_t
2984hash_type_signature (const void *item)
2985{
2986 const struct signatured_type *type_sig = item;
9a619af0 2987
348e048f
DE
2988 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2989 return type_sig->signature;
2990}
2991
2992static int
2993eq_type_signature (const void *item_lhs, const void *item_rhs)
2994{
2995 const struct signatured_type *lhs = item_lhs;
2996 const struct signatured_type *rhs = item_rhs;
9a619af0 2997
348e048f
DE
2998 return lhs->signature == rhs->signature;
2999}
3000
1fd400ff
TT
3001/* Allocate a hash table for signatured types. */
3002
3003static htab_t
673bfd45 3004allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3005{
3006 return htab_create_alloc_ex (41,
3007 hash_type_signature,
3008 eq_type_signature,
3009 NULL,
3010 &objfile->objfile_obstack,
3011 hashtab_obstack_allocate,
3012 dummy_obstack_deallocate);
3013}
3014
3015/* A helper function to add a signatured type CU to a list. */
3016
3017static int
3018add_signatured_type_cu_to_list (void **slot, void *datum)
3019{
3020 struct signatured_type *sigt = *slot;
3021 struct dwarf2_per_cu_data ***datap = datum;
3022
3023 **datap = &sigt->per_cu;
3024 ++*datap;
3025
3026 return 1;
3027}
3028
348e048f
DE
3029/* Create the hash table of all entries in the .debug_types section.
3030 The result is zero if there is an error (e.g. missing .debug_types section),
3031 otherwise non-zero. */
3032
3033static int
3034create_debug_types_hash_table (struct objfile *objfile)
3035{
be391dca 3036 gdb_byte *info_ptr;
348e048f 3037 htab_t types_htab;
1fd400ff 3038 struct dwarf2_per_cu_data **iter;
348e048f 3039
be391dca
TT
3040 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3041 info_ptr = dwarf2_per_objfile->types.buffer;
3042
348e048f
DE
3043 if (info_ptr == NULL)
3044 {
3045 dwarf2_per_objfile->signatured_types = NULL;
3046 return 0;
3047 }
3048
673bfd45 3049 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
3050
3051 if (dwarf2_die_debug)
3052 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3053
3e43a32a
MS
3054 while (info_ptr < dwarf2_per_objfile->types.buffer
3055 + dwarf2_per_objfile->types.size)
348e048f
DE
3056 {
3057 unsigned int offset;
3058 unsigned int offset_size;
3059 unsigned int type_offset;
3060 unsigned int length, initial_length_size;
3061 unsigned short version;
3062 ULONGEST signature;
3063 struct signatured_type *type_sig;
3064 void **slot;
3065 gdb_byte *ptr = info_ptr;
3066
3067 offset = ptr - dwarf2_per_objfile->types.buffer;
3068
3069 /* We need to read the type's signature in order to build the hash
3070 table, but we don't need to read anything else just yet. */
3071
3072 /* Sanity check to ensure entire cu is present. */
3073 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3074 if (ptr + length + initial_length_size
3075 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3076 {
3077 complaint (&symfile_complaints,
3e43a32a
MS
3078 _("debug type entry runs off end "
3079 "of `.debug_types' section, ignored"));
348e048f
DE
3080 break;
3081 }
3082
3083 offset_size = initial_length_size == 4 ? 4 : 8;
3084 ptr += initial_length_size;
3085 version = bfd_get_16 (objfile->obfd, ptr);
3086 ptr += 2;
3087 ptr += offset_size; /* abbrev offset */
3088 ptr += 1; /* address size */
3089 signature = bfd_get_64 (objfile->obfd, ptr);
3090 ptr += 8;
3091 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3092
3093 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3094 memset (type_sig, 0, sizeof (*type_sig));
3095 type_sig->signature = signature;
3096 type_sig->offset = offset;
3097 type_sig->type_offset = type_offset;
ca1f3406 3098 type_sig->per_cu.objfile = objfile;
1fd400ff 3099 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
3100
3101 slot = htab_find_slot (types_htab, type_sig, INSERT);
3102 gdb_assert (slot != NULL);
3103 *slot = type_sig;
3104
3105 if (dwarf2_die_debug)
3106 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3107 offset, phex (signature, sizeof (signature)));
3108
3109 info_ptr = info_ptr + initial_length_size + length;
3110 }
3111
3112 dwarf2_per_objfile->signatured_types = types_htab;
3113
1fd400ff
TT
3114 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3115 dwarf2_per_objfile->type_comp_units
3116 = obstack_alloc (&objfile->objfile_obstack,
3117 dwarf2_per_objfile->n_type_comp_units
3118 * sizeof (struct dwarf2_per_cu_data *));
3119 iter = &dwarf2_per_objfile->type_comp_units[0];
3120 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3121 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3122 == dwarf2_per_objfile->n_type_comp_units);
3123
348e048f
DE
3124 return 1;
3125}
3126
3127/* Lookup a signature based type.
3128 Returns NULL if SIG is not present in the table. */
3129
3130static struct signatured_type *
3131lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3132{
3133 struct signatured_type find_entry, *entry;
3134
3135 if (dwarf2_per_objfile->signatured_types == NULL)
3136 {
3137 complaint (&symfile_complaints,
55f1336d 3138 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3139 return 0;
3140 }
3141
3142 find_entry.signature = sig;
3143 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3144 return entry;
3145}
3146
d85a05f0
DJ
3147/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3148
3149static void
3150init_cu_die_reader (struct die_reader_specs *reader,
3151 struct dwarf2_cu *cu)
3152{
3153 reader->abfd = cu->objfile->obfd;
3154 reader->cu = cu;
3155 if (cu->per_cu->from_debug_types)
be391dca
TT
3156 {
3157 gdb_assert (dwarf2_per_objfile->types.readin);
3158 reader->buffer = dwarf2_per_objfile->types.buffer;
3159 }
d85a05f0 3160 else
be391dca
TT
3161 {
3162 gdb_assert (dwarf2_per_objfile->info.readin);
3163 reader->buffer = dwarf2_per_objfile->info.buffer;
3164 }
d85a05f0
DJ
3165}
3166
3167/* Find the base address of the compilation unit for range lists and
3168 location lists. It will normally be specified by DW_AT_low_pc.
3169 In DWARF-3 draft 4, the base address could be overridden by
3170 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3171 compilation units with discontinuous ranges. */
3172
3173static void
3174dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3175{
3176 struct attribute *attr;
3177
3178 cu->base_known = 0;
3179 cu->base_address = 0;
3180
3181 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3182 if (attr)
3183 {
3184 cu->base_address = DW_ADDR (attr);
3185 cu->base_known = 1;
3186 }
3187 else
3188 {
3189 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3190 if (attr)
3191 {
3192 cu->base_address = DW_ADDR (attr);
3193 cu->base_known = 1;
3194 }
3195 }
3196}
3197
348e048f
DE
3198/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3199 to combine the common parts.
93311388 3200 Process a compilation unit for a psymtab.
348e048f
DE
3201 BUFFER is a pointer to the beginning of the dwarf section buffer,
3202 either .debug_info or debug_types.
93311388
DE
3203 INFO_PTR is a pointer to the start of the CU.
3204 Returns a pointer to the next CU. */
aaa75496 3205
93311388
DE
3206static gdb_byte *
3207process_psymtab_comp_unit (struct objfile *objfile,
3208 struct dwarf2_per_cu_data *this_cu,
3209 gdb_byte *buffer, gdb_byte *info_ptr,
3210 unsigned int buffer_size)
c906108c 3211{
c906108c 3212 bfd *abfd = objfile->obfd;
93311388 3213 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3214 struct die_info *comp_unit_die;
c906108c 3215 struct partial_symtab *pst;
5734ee8b 3216 CORE_ADDR baseaddr;
93311388
DE
3217 struct cleanup *back_to_inner;
3218 struct dwarf2_cu cu;
d85a05f0
DJ
3219 int has_children, has_pc_info;
3220 struct attribute *attr;
d85a05f0
DJ
3221 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3222 struct die_reader_specs reader_specs;
3e2a0cee 3223 const char *filename;
c906108c 3224
9816fde3 3225 init_one_comp_unit (&cu, objfile);
93311388 3226 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3227
93311388
DE
3228 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3229 buffer, buffer_size,
3230 abfd);
10b3939b 3231
93311388
DE
3232 /* Complete the cu_header. */
3233 cu.header.offset = beg_of_comp_unit - buffer;
3234 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3235
93311388 3236 cu.list_in_scope = &file_symbols;
af703f96 3237
328c9494
DJ
3238 /* If this compilation unit was already read in, free the
3239 cached copy in order to read it in again. This is
3240 necessary because we skipped some symbols when we first
3241 read in the compilation unit (see load_partial_dies).
3242 This problem could be avoided, but the benefit is
3243 unclear. */
3244 if (this_cu->cu != NULL)
3245 free_one_cached_comp_unit (this_cu->cu);
3246
3247 /* Note that this is a pointer to our stack frame, being
3248 added to a global data structure. It will be cleaned up
3249 in free_stack_comp_unit when we finish with this
3250 compilation unit. */
3251 this_cu->cu = &cu;
d85a05f0
DJ
3252 cu.per_cu = this_cu;
3253
93311388
DE
3254 /* Read the abbrevs for this compilation unit into a table. */
3255 dwarf2_read_abbrevs (abfd, &cu);
3256 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3257
93311388 3258 /* Read the compilation unit die. */
348e048f
DE
3259 if (this_cu->from_debug_types)
3260 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3261 init_cu_die_reader (&reader_specs, &cu);
3262 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3263 &has_children);
93311388 3264
348e048f
DE
3265 if (this_cu->from_debug_types)
3266 {
3267 /* offset,length haven't been set yet for type units. */
3268 this_cu->offset = cu.header.offset;
3269 this_cu->length = cu.header.length + cu.header.initial_length_size;
3270 }
d85a05f0 3271 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3272 {
93311388
DE
3273 info_ptr = (beg_of_comp_unit + cu.header.length
3274 + cu.header.initial_length_size);
3275 do_cleanups (back_to_inner);
3276 return info_ptr;
3277 }
72bf9492 3278
9816fde3 3279 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3280
93311388 3281 /* Allocate a new partial symbol table structure. */
d85a05f0 3282 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3283 if (attr == NULL || !DW_STRING (attr))
3284 filename = "";
3285 else
3286 filename = DW_STRING (attr);
93311388 3287 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3288 filename,
93311388
DE
3289 /* TEXTLOW and TEXTHIGH are set below. */
3290 0,
3291 objfile->global_psymbols.next,
3292 objfile->static_psymbols.next);
72bf9492 3293
d85a05f0
DJ
3294 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3295 if (attr != NULL)
3296 pst->dirname = DW_STRING (attr);
72bf9492 3297
e38df1d0 3298 pst->read_symtab_private = this_cu;
72bf9492 3299
93311388 3300 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3301
0963b4bd 3302 /* Store the function that reads in the rest of the symbol table. */
93311388 3303 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3304
9291a0cd 3305 this_cu->v.psymtab = pst;
c906108c 3306
d85a05f0
DJ
3307 dwarf2_find_base_address (comp_unit_die, &cu);
3308
93311388
DE
3309 /* Possibly set the default values of LOWPC and HIGHPC from
3310 `DW_AT_ranges'. */
d85a05f0
DJ
3311 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3312 &best_highpc, &cu, pst);
3313 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3314 /* Store the contiguous range if it is not empty; it can be empty for
3315 CUs with no code. */
3316 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3317 best_lowpc + baseaddr,
3318 best_highpc + baseaddr - 1, pst);
93311388
DE
3319
3320 /* Check if comp unit has_children.
3321 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3322 If not, there's no more debug_info for this comp unit. */
d85a05f0 3323 if (has_children)
93311388
DE
3324 {
3325 struct partial_die_info *first_die;
3326 CORE_ADDR lowpc, highpc;
31ffec48 3327
93311388
DE
3328 lowpc = ((CORE_ADDR) -1);
3329 highpc = ((CORE_ADDR) 0);
c906108c 3330
93311388 3331 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3332
93311388 3333 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3334 ! has_pc_info, &cu);
57c22c6c 3335
93311388
DE
3336 /* If we didn't find a lowpc, set it to highpc to avoid
3337 complaints from `maint check'. */
3338 if (lowpc == ((CORE_ADDR) -1))
3339 lowpc = highpc;
10b3939b 3340
93311388
DE
3341 /* If the compilation unit didn't have an explicit address range,
3342 then use the information extracted from its child dies. */
d85a05f0 3343 if (! has_pc_info)
93311388 3344 {
d85a05f0
DJ
3345 best_lowpc = lowpc;
3346 best_highpc = highpc;
93311388
DE
3347 }
3348 }
d85a05f0
DJ
3349 pst->textlow = best_lowpc + baseaddr;
3350 pst->texthigh = best_highpc + baseaddr;
c906108c 3351
93311388
DE
3352 pst->n_global_syms = objfile->global_psymbols.next -
3353 (objfile->global_psymbols.list + pst->globals_offset);
3354 pst->n_static_syms = objfile->static_psymbols.next -
3355 (objfile->static_psymbols.list + pst->statics_offset);
3356 sort_pst_symbols (pst);
c906108c 3357
93311388
DE
3358 info_ptr = (beg_of_comp_unit + cu.header.length
3359 + cu.header.initial_length_size);
ae038cb0 3360
348e048f
DE
3361 if (this_cu->from_debug_types)
3362 {
3363 /* It's not clear we want to do anything with stmt lists here.
3364 Waiting to see what gcc ultimately does. */
3365 }
d85a05f0 3366 else
93311388
DE
3367 {
3368 /* Get the list of files included in the current compilation unit,
3369 and build a psymtab for each of them. */
d85a05f0 3370 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3371 }
ae038cb0 3372
93311388 3373 do_cleanups (back_to_inner);
ae038cb0 3374
93311388
DE
3375 return info_ptr;
3376}
ff013f42 3377
348e048f
DE
3378/* Traversal function for htab_traverse_noresize.
3379 Process one .debug_types comp-unit. */
3380
3381static int
3382process_type_comp_unit (void **slot, void *info)
3383{
3384 struct signatured_type *entry = (struct signatured_type *) *slot;
3385 struct objfile *objfile = (struct objfile *) info;
3386 struct dwarf2_per_cu_data *this_cu;
3387
3388 this_cu = &entry->per_cu;
348e048f 3389
be391dca 3390 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3391 process_psymtab_comp_unit (objfile, this_cu,
3392 dwarf2_per_objfile->types.buffer,
3393 dwarf2_per_objfile->types.buffer + entry->offset,
3394 dwarf2_per_objfile->types.size);
3395
3396 return 1;
3397}
3398
3399/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3400 Build partial symbol tables for the .debug_types comp-units. */
3401
3402static void
3403build_type_psymtabs (struct objfile *objfile)
3404{
3405 if (! create_debug_types_hash_table (objfile))
3406 return;
3407
3408 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3409 process_type_comp_unit, objfile);
3410}
3411
60606b2c
TT
3412/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3413
3414static void
3415psymtabs_addrmap_cleanup (void *o)
3416{
3417 struct objfile *objfile = o;
ec61707d 3418
60606b2c
TT
3419 objfile->psymtabs_addrmap = NULL;
3420}
3421
93311388
DE
3422/* Build the partial symbol table by doing a quick pass through the
3423 .debug_info and .debug_abbrev sections. */
72bf9492 3424
93311388 3425static void
c67a9c90 3426dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3427{
93311388 3428 gdb_byte *info_ptr;
60606b2c
TT
3429 struct cleanup *back_to, *addrmap_cleanup;
3430 struct obstack temp_obstack;
93311388 3431
98bfdba5
PA
3432 dwarf2_per_objfile->reading_partial_symbols = 1;
3433
be391dca 3434 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3435 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3436
93311388
DE
3437 /* Any cached compilation units will be linked by the per-objfile
3438 read_in_chain. Make sure to free them when we're done. */
3439 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3440
348e048f
DE
3441 build_type_psymtabs (objfile);
3442
93311388 3443 create_all_comp_units (objfile);
c906108c 3444
60606b2c
TT
3445 /* Create a temporary address map on a temporary obstack. We later
3446 copy this to the final obstack. */
3447 obstack_init (&temp_obstack);
3448 make_cleanup_obstack_free (&temp_obstack);
3449 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3450 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3451
93311388
DE
3452 /* Since the objects we're extracting from .debug_info vary in
3453 length, only the individual functions to extract them (like
3454 read_comp_unit_head and load_partial_die) can really know whether
3455 the buffer is large enough to hold another complete object.
c906108c 3456
93311388
DE
3457 At the moment, they don't actually check that. If .debug_info
3458 holds just one extra byte after the last compilation unit's dies,
3459 then read_comp_unit_head will happily read off the end of the
3460 buffer. read_partial_die is similarly casual. Those functions
3461 should be fixed.
c906108c 3462
93311388
DE
3463 For this loop condition, simply checking whether there's any data
3464 left at all should be sufficient. */
c906108c 3465
93311388
DE
3466 while (info_ptr < (dwarf2_per_objfile->info.buffer
3467 + dwarf2_per_objfile->info.size))
3468 {
3469 struct dwarf2_per_cu_data *this_cu;
dd373385 3470
3e43a32a
MS
3471 this_cu = dwarf2_find_comp_unit (info_ptr
3472 - dwarf2_per_objfile->info.buffer,
93311388 3473 objfile);
aaa75496 3474
93311388
DE
3475 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3476 dwarf2_per_objfile->info.buffer,
3477 info_ptr,
3478 dwarf2_per_objfile->info.size);
c906108c 3479 }
ff013f42
JK
3480
3481 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3482 &objfile->objfile_obstack);
60606b2c 3483 discard_cleanups (addrmap_cleanup);
ff013f42 3484
ae038cb0
DJ
3485 do_cleanups (back_to);
3486}
3487
93311388 3488/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3489
3490static void
93311388
DE
3491load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3492 struct objfile *objfile)
ae038cb0
DJ
3493{
3494 bfd *abfd = objfile->obfd;
fe1b8b76 3495 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3496 struct die_info *comp_unit_die;
ae038cb0 3497 struct dwarf2_cu *cu;
1d9ec526 3498 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3499 int has_children;
3500 struct die_reader_specs reader_specs;
98bfdba5 3501 int read_cu = 0;
ae038cb0 3502
348e048f
DE
3503 gdb_assert (! this_cu->from_debug_types);
3504
be391dca 3505 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3506 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3507 beg_of_comp_unit = info_ptr;
3508
98bfdba5
PA
3509 if (this_cu->cu == NULL)
3510 {
9816fde3
JK
3511 cu = xmalloc (sizeof (*cu));
3512 init_one_comp_unit (cu, objfile);
ae038cb0 3513
98bfdba5 3514 read_cu = 1;
ae038cb0 3515
98bfdba5
PA
3516 /* If an error occurs while loading, release our storage. */
3517 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3518
98bfdba5
PA
3519 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3520 dwarf2_per_objfile->info.buffer,
3521 dwarf2_per_objfile->info.size,
3522 abfd);
ae038cb0 3523
98bfdba5
PA
3524 /* Complete the cu_header. */
3525 cu->header.offset = this_cu->offset;
3526 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3527
3528 /* Link this compilation unit into the compilation unit tree. */
3529 this_cu->cu = cu;
3530 cu->per_cu = this_cu;
98bfdba5
PA
3531
3532 /* Link this CU into read_in_chain. */
3533 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3534 dwarf2_per_objfile->read_in_chain = this_cu;
3535 }
3536 else
3537 {
3538 cu = this_cu->cu;
3539 info_ptr += cu->header.first_die_offset;
3540 }
ae038cb0
DJ
3541
3542 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3543 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3544 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3545 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3546
3547 /* Read the compilation unit die. */
d85a05f0
DJ
3548 init_cu_die_reader (&reader_specs, cu);
3549 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3550 &has_children);
ae038cb0 3551
9816fde3 3552 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3553
ae038cb0
DJ
3554 /* Check if comp unit has_children.
3555 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3556 If not, there's no more debug_info for this comp unit. */
d85a05f0 3557 if (has_children)
93311388 3558 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3559
98bfdba5
PA
3560 do_cleanups (free_abbrevs_cleanup);
3561
3562 if (read_cu)
3563 {
3564 /* We've successfully allocated this compilation unit. Let our
3565 caller clean it up when finished with it. */
3566 discard_cleanups (free_cu_cleanup);
3567 }
ae038cb0
DJ
3568}
3569
3570/* Create a list of all compilation units in OBJFILE. We do this only
3571 if an inter-comp-unit reference is found; presumably if there is one,
3572 there will be many, and one will occur early in the .debug_info section.
3573 So there's no point in building this list incrementally. */
3574
3575static void
3576create_all_comp_units (struct objfile *objfile)
3577{
3578 int n_allocated;
3579 int n_comp_units;
3580 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3581 gdb_byte *info_ptr;
3582
3583 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3584 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3585
3586 n_comp_units = 0;
3587 n_allocated = 10;
3588 all_comp_units = xmalloc (n_allocated
3589 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3590
3e43a32a
MS
3591 while (info_ptr < dwarf2_per_objfile->info.buffer
3592 + dwarf2_per_objfile->info.size)
ae038cb0 3593 {
c764a876 3594 unsigned int length, initial_length_size;
ae038cb0 3595 struct dwarf2_per_cu_data *this_cu;
c764a876 3596 unsigned int offset;
ae038cb0 3597
dce234bc 3598 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3599
3600 /* Read just enough information to find out where the next
3601 compilation unit is. */
c764a876
DE
3602 length = read_initial_length (objfile->obfd, info_ptr,
3603 &initial_length_size);
ae038cb0
DJ
3604
3605 /* Save the compilation unit for later lookup. */
3606 this_cu = obstack_alloc (&objfile->objfile_obstack,
3607 sizeof (struct dwarf2_per_cu_data));
3608 memset (this_cu, 0, sizeof (*this_cu));
3609 this_cu->offset = offset;
c764a876 3610 this_cu->length = length + initial_length_size;
9291a0cd 3611 this_cu->objfile = objfile;
ae038cb0
DJ
3612
3613 if (n_comp_units == n_allocated)
3614 {
3615 n_allocated *= 2;
3616 all_comp_units = xrealloc (all_comp_units,
3617 n_allocated
3618 * sizeof (struct dwarf2_per_cu_data *));
3619 }
3620 all_comp_units[n_comp_units++] = this_cu;
3621
3622 info_ptr = info_ptr + this_cu->length;
3623 }
3624
3625 dwarf2_per_objfile->all_comp_units
3626 = obstack_alloc (&objfile->objfile_obstack,
3627 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3628 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3629 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3630 xfree (all_comp_units);
3631 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3632}
3633
5734ee8b
DJ
3634/* Process all loaded DIEs for compilation unit CU, starting at
3635 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3636 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3637 DW_AT_ranges). If NEED_PC is set, then this function will set
3638 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3639 and record the covered ranges in the addrmap. */
c906108c 3640
72bf9492
DJ
3641static void
3642scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3643 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3644{
72bf9492 3645 struct partial_die_info *pdi;
c906108c 3646
91c24f0a
DC
3647 /* Now, march along the PDI's, descending into ones which have
3648 interesting children but skipping the children of the other ones,
3649 until we reach the end of the compilation unit. */
c906108c 3650
72bf9492 3651 pdi = first_die;
91c24f0a 3652
72bf9492
DJ
3653 while (pdi != NULL)
3654 {
3655 fixup_partial_die (pdi, cu);
c906108c 3656
f55ee35c 3657 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3658 children, so we need to look at them. Ditto for anonymous
3659 enums. */
933c6fe4 3660
72bf9492 3661 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3662 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3663 {
72bf9492 3664 switch (pdi->tag)
c906108c
SS
3665 {
3666 case DW_TAG_subprogram:
5734ee8b 3667 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3668 break;
72929c62 3669 case DW_TAG_constant:
c906108c
SS
3670 case DW_TAG_variable:
3671 case DW_TAG_typedef:
91c24f0a 3672 case DW_TAG_union_type:
72bf9492 3673 if (!pdi->is_declaration)
63d06c5c 3674 {
72bf9492 3675 add_partial_symbol (pdi, cu);
63d06c5c
DC
3676 }
3677 break;
c906108c 3678 case DW_TAG_class_type:
680b30c7 3679 case DW_TAG_interface_type:
c906108c 3680 case DW_TAG_structure_type:
72bf9492 3681 if (!pdi->is_declaration)
c906108c 3682 {
72bf9492 3683 add_partial_symbol (pdi, cu);
c906108c
SS
3684 }
3685 break;
91c24f0a 3686 case DW_TAG_enumeration_type:
72bf9492
DJ
3687 if (!pdi->is_declaration)
3688 add_partial_enumeration (pdi, cu);
c906108c
SS
3689 break;
3690 case DW_TAG_base_type:
a02abb62 3691 case DW_TAG_subrange_type:
c906108c 3692 /* File scope base type definitions are added to the partial
c5aa993b 3693 symbol table. */
72bf9492 3694 add_partial_symbol (pdi, cu);
c906108c 3695 break;
d9fa45fe 3696 case DW_TAG_namespace:
5734ee8b 3697 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3698 break;
5d7cb8df
JK
3699 case DW_TAG_module:
3700 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3701 break;
c906108c
SS
3702 default:
3703 break;
3704 }
3705 }
3706
72bf9492
DJ
3707 /* If the die has a sibling, skip to the sibling. */
3708
3709 pdi = pdi->die_sibling;
3710 }
3711}
3712
3713/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3714
72bf9492 3715 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3716 name is concatenated with "::" and the partial DIE's name. For
3717 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3718 Enumerators are an exception; they use the scope of their parent
3719 enumeration type, i.e. the name of the enumeration type is not
3720 prepended to the enumerator.
91c24f0a 3721
72bf9492
DJ
3722 There are two complexities. One is DW_AT_specification; in this
3723 case "parent" means the parent of the target of the specification,
3724 instead of the direct parent of the DIE. The other is compilers
3725 which do not emit DW_TAG_namespace; in this case we try to guess
3726 the fully qualified name of structure types from their members'
3727 linkage names. This must be done using the DIE's children rather
3728 than the children of any DW_AT_specification target. We only need
3729 to do this for structures at the top level, i.e. if the target of
3730 any DW_AT_specification (if any; otherwise the DIE itself) does not
3731 have a parent. */
3732
3733/* Compute the scope prefix associated with PDI's parent, in
3734 compilation unit CU. The result will be allocated on CU's
3735 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3736 field. NULL is returned if no prefix is necessary. */
3737static char *
3738partial_die_parent_scope (struct partial_die_info *pdi,
3739 struct dwarf2_cu *cu)
3740{
3741 char *grandparent_scope;
3742 struct partial_die_info *parent, *real_pdi;
91c24f0a 3743
72bf9492
DJ
3744 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3745 then this means the parent of the specification DIE. */
3746
3747 real_pdi = pdi;
72bf9492 3748 while (real_pdi->has_specification)
10b3939b 3749 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3750
3751 parent = real_pdi->die_parent;
3752 if (parent == NULL)
3753 return NULL;
3754
3755 if (parent->scope_set)
3756 return parent->scope;
3757
3758 fixup_partial_die (parent, cu);
3759
10b3939b 3760 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3761
acebe513
UW
3762 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3763 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3764 Work around this problem here. */
3765 if (cu->language == language_cplus
6e70227d 3766 && parent->tag == DW_TAG_namespace
acebe513
UW
3767 && strcmp (parent->name, "::") == 0
3768 && grandparent_scope == NULL)
3769 {
3770 parent->scope = NULL;
3771 parent->scope_set = 1;
3772 return NULL;
3773 }
3774
72bf9492 3775 if (parent->tag == DW_TAG_namespace
f55ee35c 3776 || parent->tag == DW_TAG_module
72bf9492
DJ
3777 || parent->tag == DW_TAG_structure_type
3778 || parent->tag == DW_TAG_class_type
680b30c7 3779 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3780 || parent->tag == DW_TAG_union_type
3781 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3782 {
3783 if (grandparent_scope == NULL)
3784 parent->scope = parent->name;
3785 else
3e43a32a
MS
3786 parent->scope = typename_concat (&cu->comp_unit_obstack,
3787 grandparent_scope,
f55ee35c 3788 parent->name, 0, cu);
72bf9492 3789 }
ceeb3d5a 3790 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3791 /* Enumerators should not get the name of the enumeration as a prefix. */
3792 parent->scope = grandparent_scope;
3793 else
3794 {
3795 /* FIXME drow/2004-04-01: What should we be doing with
3796 function-local names? For partial symbols, we should probably be
3797 ignoring them. */
3798 complaint (&symfile_complaints,
e2e0b3e5 3799 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3800 parent->tag, pdi->offset);
3801 parent->scope = grandparent_scope;
c906108c
SS
3802 }
3803
72bf9492
DJ
3804 parent->scope_set = 1;
3805 return parent->scope;
3806}
3807
3808/* Return the fully scoped name associated with PDI, from compilation unit
3809 CU. The result will be allocated with malloc. */
3810static char *
3811partial_die_full_name (struct partial_die_info *pdi,
3812 struct dwarf2_cu *cu)
3813{
3814 char *parent_scope;
3815
98bfdba5
PA
3816 /* If this is a template instantiation, we can not work out the
3817 template arguments from partial DIEs. So, unfortunately, we have
3818 to go through the full DIEs. At least any work we do building
3819 types here will be reused if full symbols are loaded later. */
3820 if (pdi->has_template_arguments)
3821 {
3822 fixup_partial_die (pdi, cu);
3823
3824 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3825 {
3826 struct die_info *die;
3827 struct attribute attr;
3828 struct dwarf2_cu *ref_cu = cu;
3829
3830 attr.name = 0;
3831 attr.form = DW_FORM_ref_addr;
3832 attr.u.addr = pdi->offset;
3833 die = follow_die_ref (NULL, &attr, &ref_cu);
3834
3835 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3836 }
3837 }
3838
72bf9492
DJ
3839 parent_scope = partial_die_parent_scope (pdi, cu);
3840 if (parent_scope == NULL)
3841 return NULL;
3842 else
f55ee35c 3843 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3844}
3845
3846static void
72bf9492 3847add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3848{
e7c27a73 3849 struct objfile *objfile = cu->objfile;
c906108c 3850 CORE_ADDR addr = 0;
decbce07 3851 char *actual_name = NULL;
5c4e30ca 3852 const struct partial_symbol *psym = NULL;
e142c38c 3853 CORE_ADDR baseaddr;
72bf9492 3854 int built_actual_name = 0;
e142c38c
DJ
3855
3856 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3857
94af9270
KS
3858 actual_name = partial_die_full_name (pdi, cu);
3859 if (actual_name)
3860 built_actual_name = 1;
63d06c5c 3861
72bf9492
DJ
3862 if (actual_name == NULL)
3863 actual_name = pdi->name;
3864
c906108c
SS
3865 switch (pdi->tag)
3866 {
3867 case DW_TAG_subprogram:
2cfa0c8d 3868 if (pdi->is_external || cu->language == language_ada)
c906108c 3869 {
2cfa0c8d
JB
3870 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3871 of the global scope. But in Ada, we want to be able to access
3872 nested procedures globally. So all Ada subprograms are stored
3873 in the global scope. */
f47fb265 3874 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3875 mst_text, objfile); */
f47fb265
MS
3876 add_psymbol_to_list (actual_name, strlen (actual_name),
3877 built_actual_name,
3878 VAR_DOMAIN, LOC_BLOCK,
3879 &objfile->global_psymbols,
3880 0, pdi->lowpc + baseaddr,
3881 cu->language, objfile);
c906108c
SS
3882 }
3883 else
3884 {
f47fb265 3885 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3886 mst_file_text, objfile); */
f47fb265
MS
3887 add_psymbol_to_list (actual_name, strlen (actual_name),
3888 built_actual_name,
3889 VAR_DOMAIN, LOC_BLOCK,
3890 &objfile->static_psymbols,
3891 0, pdi->lowpc + baseaddr,
3892 cu->language, objfile);
c906108c
SS
3893 }
3894 break;
72929c62
JB
3895 case DW_TAG_constant:
3896 {
3897 struct psymbol_allocation_list *list;
3898
3899 if (pdi->is_external)
3900 list = &objfile->global_psymbols;
3901 else
3902 list = &objfile->static_psymbols;
f47fb265
MS
3903 add_psymbol_to_list (actual_name, strlen (actual_name),
3904 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3905 list, 0, 0, cu->language, objfile);
72929c62
JB
3906 }
3907 break;
c906108c 3908 case DW_TAG_variable:
caac4577
JG
3909 if (pdi->locdesc)
3910 addr = decode_locdesc (pdi->locdesc, cu);
3911
3912 if (pdi->locdesc
3913 && addr == 0
3914 && !dwarf2_per_objfile->has_section_at_zero)
3915 {
3916 /* A global or static variable may also have been stripped
3917 out by the linker if unused, in which case its address
3918 will be nullified; do not add such variables into partial
3919 symbol table then. */
3920 }
3921 else if (pdi->is_external)
c906108c
SS
3922 {
3923 /* Global Variable.
3924 Don't enter into the minimal symbol tables as there is
3925 a minimal symbol table entry from the ELF symbols already.
3926 Enter into partial symbol table if it has a location
3927 descriptor or a type.
3928 If the location descriptor is missing, new_symbol will create
3929 a LOC_UNRESOLVED symbol, the address of the variable will then
3930 be determined from the minimal symbol table whenever the variable
3931 is referenced.
3932 The address for the partial symbol table entry is not
3933 used by GDB, but it comes in handy for debugging partial symbol
3934 table building. */
3935
c906108c 3936 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
3937 add_psymbol_to_list (actual_name, strlen (actual_name),
3938 built_actual_name,
3939 VAR_DOMAIN, LOC_STATIC,
3940 &objfile->global_psymbols,
3941 0, addr + baseaddr,
3942 cu->language, objfile);
c906108c
SS
3943 }
3944 else
3945 {
0963b4bd 3946 /* Static Variable. Skip symbols without location descriptors. */
c906108c 3947 if (pdi->locdesc == NULL)
decbce07
MS
3948 {
3949 if (built_actual_name)
3950 xfree (actual_name);
3951 return;
3952 }
f47fb265 3953 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3954 mst_file_data, objfile); */
f47fb265
MS
3955 add_psymbol_to_list (actual_name, strlen (actual_name),
3956 built_actual_name,
3957 VAR_DOMAIN, LOC_STATIC,
3958 &objfile->static_psymbols,
3959 0, addr + baseaddr,
3960 cu->language, objfile);
c906108c
SS
3961 }
3962 break;
3963 case DW_TAG_typedef:
3964 case DW_TAG_base_type:
a02abb62 3965 case DW_TAG_subrange_type:
38d518c9 3966 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3967 built_actual_name,
176620f1 3968 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3969 &objfile->static_psymbols,
e142c38c 3970 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3971 break;
72bf9492
DJ
3972 case DW_TAG_namespace:
3973 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3974 built_actual_name,
72bf9492
DJ
3975 VAR_DOMAIN, LOC_TYPEDEF,
3976 &objfile->global_psymbols,
3977 0, (CORE_ADDR) 0, cu->language, objfile);
3978 break;
c906108c 3979 case DW_TAG_class_type:
680b30c7 3980 case DW_TAG_interface_type:
c906108c
SS
3981 case DW_TAG_structure_type:
3982 case DW_TAG_union_type:
3983 case DW_TAG_enumeration_type:
fa4028e9
JB
3984 /* Skip external references. The DWARF standard says in the section
3985 about "Structure, Union, and Class Type Entries": "An incomplete
3986 structure, union or class type is represented by a structure,
3987 union or class entry that does not have a byte size attribute
3988 and that has a DW_AT_declaration attribute." */
3989 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3990 {
3991 if (built_actual_name)
3992 xfree (actual_name);
3993 return;
3994 }
fa4028e9 3995
63d06c5c
DC
3996 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3997 static vs. global. */
38d518c9 3998 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3999 built_actual_name,
176620f1 4000 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4001 (cu->language == language_cplus
4002 || cu->language == language_java)
63d06c5c
DC
4003 ? &objfile->global_psymbols
4004 : &objfile->static_psymbols,
e142c38c 4005 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4006
c906108c
SS
4007 break;
4008 case DW_TAG_enumerator:
38d518c9 4009 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4010 built_actual_name,
176620f1 4011 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4012 (cu->language == language_cplus
4013 || cu->language == language_java)
f6fe98ef
DJ
4014 ? &objfile->global_psymbols
4015 : &objfile->static_psymbols,
e142c38c 4016 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4017 break;
4018 default:
4019 break;
4020 }
5c4e30ca 4021
72bf9492
DJ
4022 if (built_actual_name)
4023 xfree (actual_name);
c906108c
SS
4024}
4025
5c4e30ca
DC
4026/* Read a partial die corresponding to a namespace; also, add a symbol
4027 corresponding to that namespace to the symbol table. NAMESPACE is
4028 the name of the enclosing namespace. */
91c24f0a 4029
72bf9492
DJ
4030static void
4031add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4032 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4033 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4034{
72bf9492 4035 /* Add a symbol for the namespace. */
e7c27a73 4036
72bf9492 4037 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4038
4039 /* Now scan partial symbols in that namespace. */
4040
91c24f0a 4041 if (pdi->has_children)
5734ee8b 4042 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4043}
4044
5d7cb8df
JK
4045/* Read a partial die corresponding to a Fortran module. */
4046
4047static void
4048add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4049 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4050{
f55ee35c 4051 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4052
4053 if (pdi->has_children)
4054 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4055}
4056
bc30ff58
JB
4057/* Read a partial die corresponding to a subprogram and create a partial
4058 symbol for that subprogram. When the CU language allows it, this
4059 routine also defines a partial symbol for each nested subprogram
4060 that this subprogram contains.
6e70227d 4061
bc30ff58
JB
4062 DIE my also be a lexical block, in which case we simply search
4063 recursively for suprograms defined inside that lexical block.
4064 Again, this is only performed when the CU language allows this
4065 type of definitions. */
4066
4067static void
4068add_partial_subprogram (struct partial_die_info *pdi,
4069 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4070 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4071{
4072 if (pdi->tag == DW_TAG_subprogram)
4073 {
4074 if (pdi->has_pc_info)
4075 {
4076 if (pdi->lowpc < *lowpc)
4077 *lowpc = pdi->lowpc;
4078 if (pdi->highpc > *highpc)
4079 *highpc = pdi->highpc;
5734ee8b
DJ
4080 if (need_pc)
4081 {
4082 CORE_ADDR baseaddr;
4083 struct objfile *objfile = cu->objfile;
4084
4085 baseaddr = ANOFFSET (objfile->section_offsets,
4086 SECT_OFF_TEXT (objfile));
4087 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4088 pdi->lowpc + baseaddr,
4089 pdi->highpc - 1 + baseaddr,
9291a0cd 4090 cu->per_cu->v.psymtab);
5734ee8b 4091 }
bc30ff58 4092 if (!pdi->is_declaration)
e8d05480
JB
4093 /* Ignore subprogram DIEs that do not have a name, they are
4094 illegal. Do not emit a complaint at this point, we will
4095 do so when we convert this psymtab into a symtab. */
4096 if (pdi->name)
4097 add_partial_symbol (pdi, cu);
bc30ff58
JB
4098 }
4099 }
6e70227d 4100
bc30ff58
JB
4101 if (! pdi->has_children)
4102 return;
4103
4104 if (cu->language == language_ada)
4105 {
4106 pdi = pdi->die_child;
4107 while (pdi != NULL)
4108 {
4109 fixup_partial_die (pdi, cu);
4110 if (pdi->tag == DW_TAG_subprogram
4111 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4112 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4113 pdi = pdi->die_sibling;
4114 }
4115 }
4116}
4117
91c24f0a
DC
4118/* Read a partial die corresponding to an enumeration type. */
4119
72bf9492
DJ
4120static void
4121add_partial_enumeration (struct partial_die_info *enum_pdi,
4122 struct dwarf2_cu *cu)
91c24f0a 4123{
72bf9492 4124 struct partial_die_info *pdi;
91c24f0a
DC
4125
4126 if (enum_pdi->name != NULL)
72bf9492
DJ
4127 add_partial_symbol (enum_pdi, cu);
4128
4129 pdi = enum_pdi->die_child;
4130 while (pdi)
91c24f0a 4131 {
72bf9492 4132 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4133 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4134 else
72bf9492
DJ
4135 add_partial_symbol (pdi, cu);
4136 pdi = pdi->die_sibling;
91c24f0a 4137 }
91c24f0a
DC
4138}
4139
4bb7a0a7
DJ
4140/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4141 Return the corresponding abbrev, or NULL if the number is zero (indicating
4142 an empty DIE). In either case *BYTES_READ will be set to the length of
4143 the initial number. */
4144
4145static struct abbrev_info *
fe1b8b76 4146peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4147 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4148{
4149 bfd *abfd = cu->objfile->obfd;
4150 unsigned int abbrev_number;
4151 struct abbrev_info *abbrev;
4152
4153 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4154
4155 if (abbrev_number == 0)
4156 return NULL;
4157
4158 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4159 if (!abbrev)
4160 {
3e43a32a
MS
4161 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4162 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4163 }
4164
4165 return abbrev;
4166}
4167
93311388
DE
4168/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4169 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4170 DIE. Any children of the skipped DIEs will also be skipped. */
4171
fe1b8b76 4172static gdb_byte *
93311388 4173skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4174{
4175 struct abbrev_info *abbrev;
4176 unsigned int bytes_read;
4177
4178 while (1)
4179 {
4180 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4181 if (abbrev == NULL)
4182 return info_ptr + bytes_read;
4183 else
93311388 4184 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4185 }
4186}
4187
93311388
DE
4188/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4189 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4190 abbrev corresponding to that skipped uleb128 should be passed in
4191 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4192 children. */
4193
fe1b8b76 4194static gdb_byte *
93311388
DE
4195skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4196 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4197{
4198 unsigned int bytes_read;
4199 struct attribute attr;
4200 bfd *abfd = cu->objfile->obfd;
4201 unsigned int form, i;
4202
4203 for (i = 0; i < abbrev->num_attrs; i++)
4204 {
4205 /* The only abbrev we care about is DW_AT_sibling. */
4206 if (abbrev->attrs[i].name == DW_AT_sibling)
4207 {
4208 read_attribute (&attr, &abbrev->attrs[i],
4209 abfd, info_ptr, cu);
4210 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4211 complaint (&symfile_complaints,
4212 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4213 else
93311388 4214 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4215 }
4216
4217 /* If it isn't DW_AT_sibling, skip this attribute. */
4218 form = abbrev->attrs[i].form;
4219 skip_attribute:
4220 switch (form)
4221 {
4bb7a0a7 4222 case DW_FORM_ref_addr:
ae411497
TT
4223 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4224 and later it is offset sized. */
4225 if (cu->header.version == 2)
4226 info_ptr += cu->header.addr_size;
4227 else
4228 info_ptr += cu->header.offset_size;
4229 break;
4230 case DW_FORM_addr:
4bb7a0a7
DJ
4231 info_ptr += cu->header.addr_size;
4232 break;
4233 case DW_FORM_data1:
4234 case DW_FORM_ref1:
4235 case DW_FORM_flag:
4236 info_ptr += 1;
4237 break;
2dc7f7b3
TT
4238 case DW_FORM_flag_present:
4239 break;
4bb7a0a7
DJ
4240 case DW_FORM_data2:
4241 case DW_FORM_ref2:
4242 info_ptr += 2;
4243 break;
4244 case DW_FORM_data4:
4245 case DW_FORM_ref4:
4246 info_ptr += 4;
4247 break;
4248 case DW_FORM_data8:
4249 case DW_FORM_ref8:
55f1336d 4250 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4251 info_ptr += 8;
4252 break;
4253 case DW_FORM_string:
9b1c24c8 4254 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4255 info_ptr += bytes_read;
4256 break;
2dc7f7b3 4257 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4258 case DW_FORM_strp:
4259 info_ptr += cu->header.offset_size;
4260 break;
2dc7f7b3 4261 case DW_FORM_exprloc:
4bb7a0a7
DJ
4262 case DW_FORM_block:
4263 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4264 info_ptr += bytes_read;
4265 break;
4266 case DW_FORM_block1:
4267 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4268 break;
4269 case DW_FORM_block2:
4270 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4271 break;
4272 case DW_FORM_block4:
4273 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4274 break;
4275 case DW_FORM_sdata:
4276 case DW_FORM_udata:
4277 case DW_FORM_ref_udata:
4278 info_ptr = skip_leb128 (abfd, info_ptr);
4279 break;
4280 case DW_FORM_indirect:
4281 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4282 info_ptr += bytes_read;
4283 /* We need to continue parsing from here, so just go back to
4284 the top. */
4285 goto skip_attribute;
4286
4287 default:
3e43a32a
MS
4288 error (_("Dwarf Error: Cannot handle %s "
4289 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4290 dwarf_form_name (form),
4291 bfd_get_filename (abfd));
4292 }
4293 }
4294
4295 if (abbrev->has_children)
93311388 4296 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4297 else
4298 return info_ptr;
4299}
4300
93311388
DE
4301/* Locate ORIG_PDI's sibling.
4302 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4303 in BUFFER. */
91c24f0a 4304
fe1b8b76 4305static gdb_byte *
93311388
DE
4306locate_pdi_sibling (struct partial_die_info *orig_pdi,
4307 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4308 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4309{
4310 /* Do we know the sibling already? */
72bf9492 4311
91c24f0a
DC
4312 if (orig_pdi->sibling)
4313 return orig_pdi->sibling;
4314
4315 /* Are there any children to deal with? */
4316
4317 if (!orig_pdi->has_children)
4318 return info_ptr;
4319
4bb7a0a7 4320 /* Skip the children the long way. */
91c24f0a 4321
93311388 4322 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4323}
4324
c906108c
SS
4325/* Expand this partial symbol table into a full symbol table. */
4326
4327static void
fba45db2 4328dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4329{
c906108c
SS
4330 if (pst != NULL)
4331 {
4332 if (pst->readin)
4333 {
3e43a32a
MS
4334 warning (_("bug: psymtab for %s is already read in."),
4335 pst->filename);
c906108c
SS
4336 }
4337 else
4338 {
4339 if (info_verbose)
4340 {
3e43a32a
MS
4341 printf_filtered (_("Reading in symbols for %s..."),
4342 pst->filename);
c906108c
SS
4343 gdb_flush (gdb_stdout);
4344 }
4345
10b3939b
DJ
4346 /* Restore our global data. */
4347 dwarf2_per_objfile = objfile_data (pst->objfile,
4348 dwarf2_objfile_data_key);
4349
b2ab525c
KB
4350 /* If this psymtab is constructed from a debug-only objfile, the
4351 has_section_at_zero flag will not necessarily be correct. We
4352 can get the correct value for this flag by looking at the data
4353 associated with the (presumably stripped) associated objfile. */
4354 if (pst->objfile->separate_debug_objfile_backlink)
4355 {
4356 struct dwarf2_per_objfile *dpo_backlink
4357 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4358 dwarf2_objfile_data_key);
9a619af0 4359
b2ab525c
KB
4360 dwarf2_per_objfile->has_section_at_zero
4361 = dpo_backlink->has_section_at_zero;
4362 }
4363
98bfdba5
PA
4364 dwarf2_per_objfile->reading_partial_symbols = 0;
4365
c906108c
SS
4366 psymtab_to_symtab_1 (pst);
4367
4368 /* Finish up the debug error message. */
4369 if (info_verbose)
a3f17187 4370 printf_filtered (_("done.\n"));
c906108c
SS
4371 }
4372 }
4373}
4374
10b3939b
DJ
4375/* Add PER_CU to the queue. */
4376
4377static void
03dd20cc 4378queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4379{
4380 struct dwarf2_queue_item *item;
4381
4382 per_cu->queued = 1;
4383 item = xmalloc (sizeof (*item));
4384 item->per_cu = per_cu;
4385 item->next = NULL;
4386
4387 if (dwarf2_queue == NULL)
4388 dwarf2_queue = item;
4389 else
4390 dwarf2_queue_tail->next = item;
4391
4392 dwarf2_queue_tail = item;
4393}
4394
4395/* Process the queue. */
4396
4397static void
4398process_queue (struct objfile *objfile)
4399{
4400 struct dwarf2_queue_item *item, *next_item;
4401
03dd20cc
DJ
4402 /* The queue starts out with one item, but following a DIE reference
4403 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4404 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4405 {
9291a0cd
TT
4406 if (dwarf2_per_objfile->using_index
4407 ? !item->per_cu->v.quick->symtab
4408 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4409 process_full_comp_unit (item->per_cu);
4410
4411 item->per_cu->queued = 0;
4412 next_item = item->next;
4413 xfree (item);
4414 }
4415
4416 dwarf2_queue_tail = NULL;
4417}
4418
4419/* Free all allocated queue entries. This function only releases anything if
4420 an error was thrown; if the queue was processed then it would have been
4421 freed as we went along. */
4422
4423static void
4424dwarf2_release_queue (void *dummy)
4425{
4426 struct dwarf2_queue_item *item, *last;
4427
4428 item = dwarf2_queue;
4429 while (item)
4430 {
4431 /* Anything still marked queued is likely to be in an
4432 inconsistent state, so discard it. */
4433 if (item->per_cu->queued)
4434 {
4435 if (item->per_cu->cu != NULL)
4436 free_one_cached_comp_unit (item->per_cu->cu);
4437 item->per_cu->queued = 0;
4438 }
4439
4440 last = item;
4441 item = item->next;
4442 xfree (last);
4443 }
4444
4445 dwarf2_queue = dwarf2_queue_tail = NULL;
4446}
4447
4448/* Read in full symbols for PST, and anything it depends on. */
4449
c906108c 4450static void
fba45db2 4451psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4452{
10b3939b 4453 struct dwarf2_per_cu_data *per_cu;
c906108c 4454 struct cleanup *back_to;
aaa75496
JB
4455 int i;
4456
4457 for (i = 0; i < pst->number_of_dependencies; i++)
4458 if (!pst->dependencies[i]->readin)
4459 {
4460 /* Inform about additional files that need to be read in. */
4461 if (info_verbose)
4462 {
a3f17187 4463 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4464 fputs_filtered (" ", gdb_stdout);
4465 wrap_here ("");
4466 fputs_filtered ("and ", gdb_stdout);
4467 wrap_here ("");
4468 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4469 wrap_here (""); /* Flush output. */
aaa75496
JB
4470 gdb_flush (gdb_stdout);
4471 }
4472 psymtab_to_symtab_1 (pst->dependencies[i]);
4473 }
4474
e38df1d0 4475 per_cu = pst->read_symtab_private;
10b3939b
DJ
4476
4477 if (per_cu == NULL)
aaa75496
JB
4478 {
4479 /* It's an include file, no symbols to read for it.
4480 Everything is in the parent symtab. */
4481 pst->readin = 1;
4482 return;
4483 }
c906108c 4484
9291a0cd 4485 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4486}
4487
93311388 4488/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4489
93311388 4490static void
3e43a32a
MS
4491load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4492 struct objfile *objfile)
10b3939b 4493{
31ffec48 4494 bfd *abfd = objfile->obfd;
10b3939b 4495 struct dwarf2_cu *cu;
c764a876 4496 unsigned int offset;
93311388 4497 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4498 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4499 struct attribute *attr;
98bfdba5 4500 int read_cu = 0;
6502dd73 4501
348e048f
DE
4502 gdb_assert (! per_cu->from_debug_types);
4503
c906108c 4504 /* Set local variables from the partial symbol table info. */
10b3939b 4505 offset = per_cu->offset;
6502dd73 4506
be391dca 4507 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4508 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4509 beg_of_comp_unit = info_ptr;
63d06c5c 4510
98bfdba5
PA
4511 if (per_cu->cu == NULL)
4512 {
9816fde3
JK
4513 cu = xmalloc (sizeof (*cu));
4514 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4515
4516 read_cu = 1;
c906108c 4517
98bfdba5
PA
4518 /* If an error occurs while loading, release our storage. */
4519 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4520
98bfdba5
PA
4521 /* Read in the comp_unit header. */
4522 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4523
98bfdba5
PA
4524 /* Complete the cu_header. */
4525 cu->header.offset = offset;
4526 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4527
98bfdba5
PA
4528 /* Read the abbrevs for this compilation unit. */
4529 dwarf2_read_abbrevs (abfd, cu);
4530 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4531
98bfdba5
PA
4532 /* Link this compilation unit into the compilation unit tree. */
4533 per_cu->cu = cu;
4534 cu->per_cu = per_cu;
98bfdba5
PA
4535
4536 /* Link this CU into read_in_chain. */
4537 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4538 dwarf2_per_objfile->read_in_chain = per_cu;
4539 }
4540 else
4541 {
4542 cu = per_cu->cu;
4543 info_ptr += cu->header.first_die_offset;
4544 }
e142c38c 4545
93311388 4546 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4547
4548 /* We try not to read any attributes in this function, because not
4549 all objfiles needed for references have been loaded yet, and symbol
4550 table processing isn't initialized. But we have to set the CU language,
4551 or we won't be able to build types correctly. */
9816fde3 4552 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4553
a6c727b2
DJ
4554 /* Similarly, if we do not read the producer, we can not apply
4555 producer-specific interpretation. */
4556 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4557 if (attr)
4558 cu->producer = DW_STRING (attr);
4559
98bfdba5
PA
4560 if (read_cu)
4561 {
4562 do_cleanups (free_abbrevs_cleanup);
e142c38c 4563
98bfdba5
PA
4564 /* We've successfully allocated this compilation unit. Let our
4565 caller clean it up when finished with it. */
4566 discard_cleanups (free_cu_cleanup);
4567 }
10b3939b
DJ
4568}
4569
3da10d80
KS
4570/* Add a DIE to the delayed physname list. */
4571
4572static void
4573add_to_method_list (struct type *type, int fnfield_index, int index,
4574 const char *name, struct die_info *die,
4575 struct dwarf2_cu *cu)
4576{
4577 struct delayed_method_info mi;
4578 mi.type = type;
4579 mi.fnfield_index = fnfield_index;
4580 mi.index = index;
4581 mi.name = name;
4582 mi.die = die;
4583 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4584}
4585
4586/* A cleanup for freeing the delayed method list. */
4587
4588static void
4589free_delayed_list (void *ptr)
4590{
4591 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4592 if (cu->method_list != NULL)
4593 {
4594 VEC_free (delayed_method_info, cu->method_list);
4595 cu->method_list = NULL;
4596 }
4597}
4598
4599/* Compute the physnames of any methods on the CU's method list.
4600
4601 The computation of method physnames is delayed in order to avoid the
4602 (bad) condition that one of the method's formal parameters is of an as yet
4603 incomplete type. */
4604
4605static void
4606compute_delayed_physnames (struct dwarf2_cu *cu)
4607{
4608 int i;
4609 struct delayed_method_info *mi;
4610 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4611 {
4612 char *physname;
4613 struct fn_fieldlist *fn_flp
4614 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4615 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4616 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4617 }
4618}
4619
8be455d7
JK
4620/* Check for GCC >= 4.0. */
4621
4622static int
4623producer_is_gcc_ge_4_0 (struct dwarf2_cu *cu)
4624{
4625 const char *cs;
4626 int major, minor;
4627
4628 if (cu->producer == NULL)
4629 {
4630 /* For unknown compilers expect their behavior is not compliant. For GCC
4631 this case can also happen for -gdwarf-4 type units supported since
4632 gcc-4.5. */
4633
4634 return 0;
4635 }
4636
4637 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
4638
4639 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
4640 {
4641 /* For non-GCC compilers expect their behavior is not compliant. */
4642
4643 return 0;
4644 }
4645 cs = &cu->producer[strlen ("GNU ")];
4646 while (*cs && !isdigit (*cs))
4647 cs++;
4648 if (sscanf (cs, "%d.%d", &major, &minor) != 2)
4649 {
4650 /* Not recognized as GCC. */
4651
4652 return 0;
4653 }
4654
4655 return major >= 4;
4656}
4657
10b3939b
DJ
4658/* Generate full symbol information for PST and CU, whose DIEs have
4659 already been loaded into memory. */
4660
4661static void
4662process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4663{
10b3939b 4664 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4665 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4666 CORE_ADDR lowpc, highpc;
4667 struct symtab *symtab;
3da10d80 4668 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4669 CORE_ADDR baseaddr;
4670
4671 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4672
10b3939b
DJ
4673 buildsym_init ();
4674 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4675 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4676
4677 cu->list_in_scope = &file_symbols;
c906108c 4678
d85a05f0 4679 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4680
c906108c 4681 /* Do line number decoding in read_file_scope () */
10b3939b 4682 process_die (cu->dies, cu);
c906108c 4683
3da10d80
KS
4684 /* Now that we have processed all the DIEs in the CU, all the types
4685 should be complete, and it should now be safe to compute all of the
4686 physnames. */
4687 compute_delayed_physnames (cu);
4688 do_cleanups (delayed_list_cleanup);
4689
fae299cd
DC
4690 /* Some compilers don't define a DW_AT_high_pc attribute for the
4691 compilation unit. If the DW_AT_high_pc is missing, synthesize
4692 it, by scanning the DIE's below the compilation unit. */
10b3939b 4693 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4694
613e1657 4695 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4696
8be455d7 4697 if (symtab != NULL)
c906108c 4698 {
8be455d7
JK
4699 /* Set symtab language to language from DW_AT_language. If the
4700 compilation is from a C file generated by language preprocessors, do
4701 not set the language if it was already deduced by start_subfile. */
4702 if (!(cu->language == language_c && symtab->language != language_c))
4703 symtab->language = cu->language;
4704
4705 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4706 produce DW_AT_location with location lists but it can be possibly
4707 invalid without -fvar-tracking.
4708
4709 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4710 needed, it would be wrong due to missing DW_AT_producer there.
4711
4712 Still one can confuse GDB by using non-standard GCC compilation
4713 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4714 */
4715 if (cu->has_loclist && producer_is_gcc_ge_4_0 (cu))
4716 symtab->locations_valid = 1;
c906108c 4717 }
9291a0cd
TT
4718
4719 if (dwarf2_per_objfile->using_index)
4720 per_cu->v.quick->symtab = symtab;
4721 else
4722 {
4723 struct partial_symtab *pst = per_cu->v.psymtab;
4724 pst->symtab = symtab;
4725 pst->readin = 1;
4726 }
c906108c
SS
4727
4728 do_cleanups (back_to);
4729}
4730
4731/* Process a die and its children. */
4732
4733static void
e7c27a73 4734process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4735{
4736 switch (die->tag)
4737 {
4738 case DW_TAG_padding:
4739 break;
4740 case DW_TAG_compile_unit:
e7c27a73 4741 read_file_scope (die, cu);
c906108c 4742 break;
348e048f
DE
4743 case DW_TAG_type_unit:
4744 read_type_unit_scope (die, cu);
4745 break;
c906108c 4746 case DW_TAG_subprogram:
c906108c 4747 case DW_TAG_inlined_subroutine:
edb3359d 4748 read_func_scope (die, cu);
c906108c
SS
4749 break;
4750 case DW_TAG_lexical_block:
14898363
L
4751 case DW_TAG_try_block:
4752 case DW_TAG_catch_block:
e7c27a73 4753 read_lexical_block_scope (die, cu);
c906108c
SS
4754 break;
4755 case DW_TAG_class_type:
680b30c7 4756 case DW_TAG_interface_type:
c906108c
SS
4757 case DW_TAG_structure_type:
4758 case DW_TAG_union_type:
134d01f1 4759 process_structure_scope (die, cu);
c906108c
SS
4760 break;
4761 case DW_TAG_enumeration_type:
134d01f1 4762 process_enumeration_scope (die, cu);
c906108c 4763 break;
134d01f1 4764
f792889a
DJ
4765 /* These dies have a type, but processing them does not create
4766 a symbol or recurse to process the children. Therefore we can
4767 read them on-demand through read_type_die. */
c906108c 4768 case DW_TAG_subroutine_type:
72019c9c 4769 case DW_TAG_set_type:
c906108c 4770 case DW_TAG_array_type:
c906108c 4771 case DW_TAG_pointer_type:
c906108c 4772 case DW_TAG_ptr_to_member_type:
c906108c 4773 case DW_TAG_reference_type:
c906108c 4774 case DW_TAG_string_type:
c906108c 4775 break;
134d01f1 4776
c906108c 4777 case DW_TAG_base_type:
a02abb62 4778 case DW_TAG_subrange_type:
cb249c71 4779 case DW_TAG_typedef:
134d01f1
DJ
4780 /* Add a typedef symbol for the type definition, if it has a
4781 DW_AT_name. */
f792889a 4782 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4783 break;
c906108c 4784 case DW_TAG_common_block:
e7c27a73 4785 read_common_block (die, cu);
c906108c
SS
4786 break;
4787 case DW_TAG_common_inclusion:
4788 break;
d9fa45fe 4789 case DW_TAG_namespace:
63d06c5c 4790 processing_has_namespace_info = 1;
e7c27a73 4791 read_namespace (die, cu);
d9fa45fe 4792 break;
5d7cb8df 4793 case DW_TAG_module:
f55ee35c 4794 processing_has_namespace_info = 1;
5d7cb8df
JK
4795 read_module (die, cu);
4796 break;
d9fa45fe
DC
4797 case DW_TAG_imported_declaration:
4798 case DW_TAG_imported_module:
63d06c5c 4799 processing_has_namespace_info = 1;
27aa8d6a
SW
4800 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4801 || cu->language != language_fortran))
4802 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4803 dwarf_tag_name (die->tag));
4804 read_import_statement (die, cu);
d9fa45fe 4805 break;
c906108c 4806 default:
e7c27a73 4807 new_symbol (die, NULL, cu);
c906108c
SS
4808 break;
4809 }
4810}
4811
94af9270
KS
4812/* A helper function for dwarf2_compute_name which determines whether DIE
4813 needs to have the name of the scope prepended to the name listed in the
4814 die. */
4815
4816static int
4817die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4818{
1c809c68
TT
4819 struct attribute *attr;
4820
94af9270
KS
4821 switch (die->tag)
4822 {
4823 case DW_TAG_namespace:
4824 case DW_TAG_typedef:
4825 case DW_TAG_class_type:
4826 case DW_TAG_interface_type:
4827 case DW_TAG_structure_type:
4828 case DW_TAG_union_type:
4829 case DW_TAG_enumeration_type:
4830 case DW_TAG_enumerator:
4831 case DW_TAG_subprogram:
4832 case DW_TAG_member:
4833 return 1;
4834
4835 case DW_TAG_variable:
c2b0a229 4836 case DW_TAG_constant:
94af9270
KS
4837 /* We only need to prefix "globally" visible variables. These include
4838 any variable marked with DW_AT_external or any variable that
4839 lives in a namespace. [Variables in anonymous namespaces
4840 require prefixing, but they are not DW_AT_external.] */
4841
4842 if (dwarf2_attr (die, DW_AT_specification, cu))
4843 {
4844 struct dwarf2_cu *spec_cu = cu;
9a619af0 4845
94af9270
KS
4846 return die_needs_namespace (die_specification (die, &spec_cu),
4847 spec_cu);
4848 }
4849
1c809c68 4850 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4851 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4852 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4853 return 0;
4854 /* A variable in a lexical block of some kind does not need a
4855 namespace, even though in C++ such variables may be external
4856 and have a mangled name. */
4857 if (die->parent->tag == DW_TAG_lexical_block
4858 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4859 || die->parent->tag == DW_TAG_catch_block
4860 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4861 return 0;
4862 return 1;
94af9270
KS
4863
4864 default:
4865 return 0;
4866 }
4867}
4868
98bfdba5
PA
4869/* Retrieve the last character from a mem_file. */
4870
4871static void
4872do_ui_file_peek_last (void *object, const char *buffer, long length)
4873{
4874 char *last_char_p = (char *) object;
4875
4876 if (length > 0)
4877 *last_char_p = buffer[length - 1];
4878}
4879
94af9270
KS
4880/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4881 compute the physname for the object, which include a method's
4882 formal parameters (C++/Java) and return type (Java).
4883
af6b7be1
JB
4884 For Ada, return the DIE's linkage name rather than the fully qualified
4885 name. PHYSNAME is ignored..
4886
94af9270
KS
4887 The result is allocated on the objfile_obstack and canonicalized. */
4888
4889static const char *
4890dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4891 int physname)
4892{
4893 if (name == NULL)
4894 name = dwarf2_name (die, cu);
4895
f55ee35c
JK
4896 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4897 compute it by typename_concat inside GDB. */
4898 if (cu->language == language_ada
4899 || (cu->language == language_fortran && physname))
4900 {
4901 /* For Ada unit, we prefer the linkage name over the name, as
4902 the former contains the exported name, which the user expects
4903 to be able to reference. Ideally, we want the user to be able
4904 to reference this entity using either natural or linkage name,
4905 but we haven't started looking at this enhancement yet. */
4906 struct attribute *attr;
4907
4908 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4909 if (attr == NULL)
4910 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4911 if (attr && DW_STRING (attr))
4912 return DW_STRING (attr);
4913 }
4914
94af9270
KS
4915 /* These are the only languages we know how to qualify names in. */
4916 if (name != NULL
f55ee35c
JK
4917 && (cu->language == language_cplus || cu->language == language_java
4918 || cu->language == language_fortran))
94af9270
KS
4919 {
4920 if (die_needs_namespace (die, cu))
4921 {
4922 long length;
4923 char *prefix;
4924 struct ui_file *buf;
4925
4926 prefix = determine_prefix (die, cu);
4927 buf = mem_fileopen ();
4928 if (*prefix != '\0')
4929 {
f55ee35c
JK
4930 char *prefixed_name = typename_concat (NULL, prefix, name,
4931 physname, cu);
9a619af0 4932
94af9270
KS
4933 fputs_unfiltered (prefixed_name, buf);
4934 xfree (prefixed_name);
4935 }
4936 else
62d5b8da 4937 fputs_unfiltered (name, buf);
94af9270 4938
98bfdba5
PA
4939 /* Template parameters may be specified in the DIE's DW_AT_name, or
4940 as children with DW_TAG_template_type_param or
4941 DW_TAG_value_type_param. If the latter, add them to the name
4942 here. If the name already has template parameters, then
4943 skip this step; some versions of GCC emit both, and
4944 it is more efficient to use the pre-computed name.
4945
4946 Something to keep in mind about this process: it is very
4947 unlikely, or in some cases downright impossible, to produce
4948 something that will match the mangled name of a function.
4949 If the definition of the function has the same debug info,
4950 we should be able to match up with it anyway. But fallbacks
4951 using the minimal symbol, for instance to find a method
4952 implemented in a stripped copy of libstdc++, will not work.
4953 If we do not have debug info for the definition, we will have to
4954 match them up some other way.
4955
4956 When we do name matching there is a related problem with function
4957 templates; two instantiated function templates are allowed to
4958 differ only by their return types, which we do not add here. */
4959
4960 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4961 {
4962 struct attribute *attr;
4963 struct die_info *child;
4964 int first = 1;
4965
4966 die->building_fullname = 1;
4967
4968 for (child = die->child; child != NULL; child = child->sibling)
4969 {
4970 struct type *type;
4971 long value;
4972 gdb_byte *bytes;
4973 struct dwarf2_locexpr_baton *baton;
4974 struct value *v;
4975
4976 if (child->tag != DW_TAG_template_type_param
4977 && child->tag != DW_TAG_template_value_param)
4978 continue;
4979
4980 if (first)
4981 {
4982 fputs_unfiltered ("<", buf);
4983 first = 0;
4984 }
4985 else
4986 fputs_unfiltered (", ", buf);
4987
4988 attr = dwarf2_attr (child, DW_AT_type, cu);
4989 if (attr == NULL)
4990 {
4991 complaint (&symfile_complaints,
4992 _("template parameter missing DW_AT_type"));
4993 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4994 continue;
4995 }
4996 type = die_type (child, cu);
4997
4998 if (child->tag == DW_TAG_template_type_param)
4999 {
5000 c_print_type (type, "", buf, -1, 0);
5001 continue;
5002 }
5003
5004 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5005 if (attr == NULL)
5006 {
5007 complaint (&symfile_complaints,
3e43a32a
MS
5008 _("template parameter missing "
5009 "DW_AT_const_value"));
98bfdba5
PA
5010 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5011 continue;
5012 }
5013
5014 dwarf2_const_value_attr (attr, type, name,
5015 &cu->comp_unit_obstack, cu,
5016 &value, &bytes, &baton);
5017
5018 if (TYPE_NOSIGN (type))
5019 /* GDB prints characters as NUMBER 'CHAR'. If that's
5020 changed, this can use value_print instead. */
5021 c_printchar (value, type, buf);
5022 else
5023 {
5024 struct value_print_options opts;
5025
5026 if (baton != NULL)
5027 v = dwarf2_evaluate_loc_desc (type, NULL,
5028 baton->data,
5029 baton->size,
5030 baton->per_cu);
5031 else if (bytes != NULL)
5032 {
5033 v = allocate_value (type);
5034 memcpy (value_contents_writeable (v), bytes,
5035 TYPE_LENGTH (type));
5036 }
5037 else
5038 v = value_from_longest (type, value);
5039
3e43a32a
MS
5040 /* Specify decimal so that we do not depend on
5041 the radix. */
98bfdba5
PA
5042 get_formatted_print_options (&opts, 'd');
5043 opts.raw = 1;
5044 value_print (v, buf, &opts);
5045 release_value (v);
5046 value_free (v);
5047 }
5048 }
5049
5050 die->building_fullname = 0;
5051
5052 if (!first)
5053 {
5054 /* Close the argument list, with a space if necessary
5055 (nested templates). */
5056 char last_char = '\0';
5057 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5058 if (last_char == '>')
5059 fputs_unfiltered (" >", buf);
5060 else
5061 fputs_unfiltered (">", buf);
5062 }
5063 }
5064
94af9270
KS
5065 /* For Java and C++ methods, append formal parameter type
5066 information, if PHYSNAME. */
6e70227d 5067
94af9270
KS
5068 if (physname && die->tag == DW_TAG_subprogram
5069 && (cu->language == language_cplus
5070 || cu->language == language_java))
5071 {
5072 struct type *type = read_type_die (die, cu);
5073
3167638f 5074 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5075
5076 if (cu->language == language_java)
5077 {
5078 /* For java, we must append the return type to method
0963b4bd 5079 names. */
94af9270
KS
5080 if (die->tag == DW_TAG_subprogram)
5081 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5082 0, 0);
5083 }
5084 else if (cu->language == language_cplus)
5085 {
60430eff
DJ
5086 /* Assume that an artificial first parameter is
5087 "this", but do not crash if it is not. RealView
5088 marks unnamed (and thus unused) parameters as
5089 artificial; there is no way to differentiate
5090 the two cases. */
94af9270
KS
5091 if (TYPE_NFIELDS (type) > 0
5092 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5093 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5094 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5095 0))))
94af9270
KS
5096 fputs_unfiltered (" const", buf);
5097 }
5098 }
5099
5100 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5101 &length);
5102 ui_file_delete (buf);
5103
5104 if (cu->language == language_cplus)
5105 {
5106 char *cname
5107 = dwarf2_canonicalize_name (name, cu,
5108 &cu->objfile->objfile_obstack);
9a619af0 5109
94af9270
KS
5110 if (cname != NULL)
5111 name = cname;
5112 }
5113 }
5114 }
5115
5116 return name;
5117}
5118
0114d602
DJ
5119/* Return the fully qualified name of DIE, based on its DW_AT_name.
5120 If scope qualifiers are appropriate they will be added. The result
5121 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5122 not have a name. NAME may either be from a previous call to
5123 dwarf2_name or NULL.
5124
0963b4bd 5125 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5126
5127static const char *
94af9270 5128dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5129{
94af9270
KS
5130 return dwarf2_compute_name (name, die, cu, 0);
5131}
0114d602 5132
94af9270
KS
5133/* Construct a physname for the given DIE in CU. NAME may either be
5134 from a previous call to dwarf2_name or NULL. The result will be
5135 allocated on the objfile_objstack or NULL if the DIE does not have a
5136 name.
0114d602 5137
94af9270 5138 The output string will be canonicalized (if C++/Java). */
0114d602 5139
94af9270
KS
5140static const char *
5141dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5142{
5143 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
5144}
5145
27aa8d6a
SW
5146/* Read the import statement specified by the given die and record it. */
5147
5148static void
5149read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5150{
5151 struct attribute *import_attr;
5152 struct die_info *imported_die;
de4affc9 5153 struct dwarf2_cu *imported_cu;
27aa8d6a 5154 const char *imported_name;
794684b6 5155 const char *imported_name_prefix;
13387711
SW
5156 const char *canonical_name;
5157 const char *import_alias;
5158 const char *imported_declaration = NULL;
794684b6 5159 const char *import_prefix;
13387711
SW
5160
5161 char *temp;
27aa8d6a
SW
5162
5163 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5164 if (import_attr == NULL)
5165 {
5166 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5167 dwarf_tag_name (die->tag));
5168 return;
5169 }
5170
de4affc9
CC
5171 imported_cu = cu;
5172 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5173 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5174 if (imported_name == NULL)
5175 {
5176 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5177
5178 The import in the following code:
5179 namespace A
5180 {
5181 typedef int B;
5182 }
5183
5184 int main ()
5185 {
5186 using A::B;
5187 B b;
5188 return b;
5189 }
5190
5191 ...
5192 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5193 <52> DW_AT_decl_file : 1
5194 <53> DW_AT_decl_line : 6
5195 <54> DW_AT_import : <0x75>
5196 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5197 <59> DW_AT_name : B
5198 <5b> DW_AT_decl_file : 1
5199 <5c> DW_AT_decl_line : 2
5200 <5d> DW_AT_type : <0x6e>
5201 ...
5202 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5203 <76> DW_AT_byte_size : 4
5204 <77> DW_AT_encoding : 5 (signed)
5205
5206 imports the wrong die ( 0x75 instead of 0x58 ).
5207 This case will be ignored until the gcc bug is fixed. */
5208 return;
5209 }
5210
82856980
SW
5211 /* Figure out the local name after import. */
5212 import_alias = dwarf2_name (die, cu);
27aa8d6a 5213
794684b6
SW
5214 /* Figure out where the statement is being imported to. */
5215 import_prefix = determine_prefix (die, cu);
5216
5217 /* Figure out what the scope of the imported die is and prepend it
5218 to the name of the imported die. */
de4affc9 5219 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5220
f55ee35c
JK
5221 if (imported_die->tag != DW_TAG_namespace
5222 && imported_die->tag != DW_TAG_module)
794684b6 5223 {
13387711
SW
5224 imported_declaration = imported_name;
5225 canonical_name = imported_name_prefix;
794684b6 5226 }
13387711 5227 else if (strlen (imported_name_prefix) > 0)
794684b6 5228 {
13387711
SW
5229 temp = alloca (strlen (imported_name_prefix)
5230 + 2 + strlen (imported_name) + 1);
5231 strcpy (temp, imported_name_prefix);
5232 strcat (temp, "::");
5233 strcat (temp, imported_name);
5234 canonical_name = temp;
794684b6 5235 }
13387711
SW
5236 else
5237 canonical_name = imported_name;
794684b6 5238
c0cc3a76
SW
5239 cp_add_using_directive (import_prefix,
5240 canonical_name,
5241 import_alias,
13387711 5242 imported_declaration,
c0cc3a76 5243 &cu->objfile->objfile_obstack);
27aa8d6a
SW
5244}
5245
5fb290d7 5246static void
e142c38c 5247initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5248{
e142c38c 5249 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5250}
5251
ae2de4f8
DE
5252/* Cleanup function for read_file_scope. */
5253
cb1df416
DJ
5254static void
5255free_cu_line_header (void *arg)
5256{
5257 struct dwarf2_cu *cu = arg;
5258
5259 free_line_header (cu->line_header);
5260 cu->line_header = NULL;
5261}
5262
9291a0cd
TT
5263static void
5264find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5265 char **name, char **comp_dir)
5266{
5267 struct attribute *attr;
5268
5269 *name = NULL;
5270 *comp_dir = NULL;
5271
5272 /* Find the filename. Do not use dwarf2_name here, since the filename
5273 is not a source language identifier. */
5274 attr = dwarf2_attr (die, DW_AT_name, cu);
5275 if (attr)
5276 {
5277 *name = DW_STRING (attr);
5278 }
5279
5280 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5281 if (attr)
5282 *comp_dir = DW_STRING (attr);
5283 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5284 {
5285 *comp_dir = ldirname (*name);
5286 if (*comp_dir != NULL)
5287 make_cleanup (xfree, *comp_dir);
5288 }
5289 if (*comp_dir != NULL)
5290 {
5291 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5292 directory, get rid of it. */
5293 char *cp = strchr (*comp_dir, ':');
5294
5295 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5296 *comp_dir = cp + 1;
5297 }
5298
5299 if (*name == NULL)
5300 *name = "<unknown>";
5301}
5302
ae2de4f8
DE
5303/* Process DW_TAG_compile_unit. */
5304
c906108c 5305static void
e7c27a73 5306read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5307{
e7c27a73 5308 struct objfile *objfile = cu->objfile;
debd256d 5309 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5310 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5311 CORE_ADDR highpc = ((CORE_ADDR) 0);
5312 struct attribute *attr;
e1024ff1 5313 char *name = NULL;
c906108c
SS
5314 char *comp_dir = NULL;
5315 struct die_info *child_die;
5316 bfd *abfd = objfile->obfd;
debd256d 5317 struct line_header *line_header = 0;
e142c38c 5318 CORE_ADDR baseaddr;
6e70227d 5319
e142c38c 5320 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5321
fae299cd 5322 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5323
5324 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5325 from finish_block. */
2acceee2 5326 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5327 lowpc = highpc;
5328 lowpc += baseaddr;
5329 highpc += baseaddr;
5330
9291a0cd 5331 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5332
e142c38c 5333 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5334 if (attr)
5335 {
e142c38c 5336 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5337 }
5338
b0f35d58 5339 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5340 if (attr)
b0f35d58 5341 cu->producer = DW_STRING (attr);
303b6f5d 5342
f4b8a18d
KW
5343 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5344 standardised yet. As a workaround for the language detection we fall
5345 back to the DW_AT_producer string. */
5346 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5347 cu->language = language_opencl;
5348
0963b4bd 5349 /* We assume that we're processing GCC output. */
c906108c 5350 processing_gcc_compilation = 2;
c906108c 5351
df8a16a1
DJ
5352 processing_has_namespace_info = 0;
5353
c906108c
SS
5354 start_symtab (name, comp_dir, lowpc);
5355 record_debugformat ("DWARF 2");
303b6f5d 5356 record_producer (cu->producer);
c906108c 5357
e142c38c 5358 initialize_cu_func_list (cu);
c906108c 5359
cb1df416
DJ
5360 /* Decode line number information if present. We do this before
5361 processing child DIEs, so that the line header table is available
5362 for DW_AT_decl_file. */
e142c38c 5363 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5364 if (attr)
5365 {
debd256d 5366 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5367 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5368 if (line_header)
5369 {
cb1df416
DJ
5370 cu->line_header = line_header;
5371 make_cleanup (free_cu_line_header, cu);
aaa75496 5372 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5373 }
5fb290d7 5374 }
debd256d 5375
cb1df416
DJ
5376 /* Process all dies in compilation unit. */
5377 if (die->child != NULL)
5378 {
5379 child_die = die->child;
5380 while (child_die && child_die->tag)
5381 {
5382 process_die (child_die, cu);
5383 child_die = sibling_die (child_die);
5384 }
5385 }
5386
2e276125
JB
5387 /* Decode macro information, if present. Dwarf 2 macro information
5388 refers to information in the line number info statement program
5389 header, so we can only read it if we've read the header
5390 successfully. */
e142c38c 5391 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5392 if (attr && line_header)
2e276125
JB
5393 {
5394 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5395
2e276125 5396 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5397 comp_dir, abfd, cu);
2e276125 5398 }
debd256d 5399 do_cleanups (back_to);
5fb290d7
DJ
5400}
5401
ae2de4f8
DE
5402/* Process DW_TAG_type_unit.
5403 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5404 actual type being defined by this TU. In this case the first top
5405 level sibling is there to provide context only. */
5406
5407static void
5408read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5409{
5410 struct objfile *objfile = cu->objfile;
5411 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5412 CORE_ADDR lowpc;
5413 struct attribute *attr;
5414 char *name = NULL;
5415 char *comp_dir = NULL;
5416 struct die_info *child_die;
5417 bfd *abfd = objfile->obfd;
348e048f
DE
5418
5419 /* start_symtab needs a low pc, but we don't really have one.
5420 Do what read_file_scope would do in the absence of such info. */
5421 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5422
5423 /* Find the filename. Do not use dwarf2_name here, since the filename
5424 is not a source language identifier. */
5425 attr = dwarf2_attr (die, DW_AT_name, cu);
5426 if (attr)
5427 name = DW_STRING (attr);
5428
5429 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5430 if (attr)
5431 comp_dir = DW_STRING (attr);
5432 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5433 {
5434 comp_dir = ldirname (name);
5435 if (comp_dir != NULL)
5436 make_cleanup (xfree, comp_dir);
5437 }
5438
5439 if (name == NULL)
5440 name = "<unknown>";
5441
5442 attr = dwarf2_attr (die, DW_AT_language, cu);
5443 if (attr)
5444 set_cu_language (DW_UNSND (attr), cu);
5445
5446 /* This isn't technically needed today. It is done for symmetry
5447 with read_file_scope. */
5448 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5449 if (attr)
348e048f
DE
5450 cu->producer = DW_STRING (attr);
5451
0963b4bd 5452 /* We assume that we're processing GCC output. */
348e048f
DE
5453 processing_gcc_compilation = 2;
5454
5455 processing_has_namespace_info = 0;
5456
5457 start_symtab (name, comp_dir, lowpc);
5458 record_debugformat ("DWARF 2");
5459 record_producer (cu->producer);
5460
5461 /* Process the dies in the type unit. */
5462 if (die->child == NULL)
5463 {
5464 dump_die_for_error (die);
5465 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5466 bfd_get_filename (abfd));
5467 }
5468
5469 child_die = die->child;
5470
5471 while (child_die && child_die->tag)
5472 {
5473 process_die (child_die, cu);
5474
5475 child_die = sibling_die (child_die);
5476 }
5477
5478 do_cleanups (back_to);
5479}
5480
5fb290d7 5481static void
e142c38c
DJ
5482add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5483 struct dwarf2_cu *cu)
5fb290d7
DJ
5484{
5485 struct function_range *thisfn;
5486
5487 thisfn = (struct function_range *)
7b5a2f43 5488 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5489 thisfn->name = name;
5490 thisfn->lowpc = lowpc;
5491 thisfn->highpc = highpc;
5492 thisfn->seen_line = 0;
5493 thisfn->next = NULL;
5494
e142c38c
DJ
5495 if (cu->last_fn == NULL)
5496 cu->first_fn = thisfn;
5fb290d7 5497 else
e142c38c 5498 cu->last_fn->next = thisfn;
5fb290d7 5499
e142c38c 5500 cu->last_fn = thisfn;
c906108c
SS
5501}
5502
d389af10
JK
5503/* qsort helper for inherit_abstract_dies. */
5504
5505static int
5506unsigned_int_compar (const void *ap, const void *bp)
5507{
5508 unsigned int a = *(unsigned int *) ap;
5509 unsigned int b = *(unsigned int *) bp;
5510
5511 return (a > b) - (b > a);
5512}
5513
5514/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5515 Inherit only the children of the DW_AT_abstract_origin DIE not being
5516 already referenced by DW_AT_abstract_origin from the children of the
5517 current DIE. */
d389af10
JK
5518
5519static void
5520inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5521{
5522 struct die_info *child_die;
5523 unsigned die_children_count;
5524 /* CU offsets which were referenced by children of the current DIE. */
5525 unsigned *offsets;
5526 unsigned *offsets_end, *offsetp;
5527 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5528 struct die_info *origin_die;
5529 /* Iterator of the ORIGIN_DIE children. */
5530 struct die_info *origin_child_die;
5531 struct cleanup *cleanups;
5532 struct attribute *attr;
cd02d79d
PA
5533 struct dwarf2_cu *origin_cu;
5534 struct pending **origin_previous_list_in_scope;
d389af10
JK
5535
5536 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5537 if (!attr)
5538 return;
5539
cd02d79d
PA
5540 /* Note that following die references may follow to a die in a
5541 different cu. */
5542
5543 origin_cu = cu;
5544 origin_die = follow_die_ref (die, attr, &origin_cu);
5545
5546 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5547 symbols in. */
5548 origin_previous_list_in_scope = origin_cu->list_in_scope;
5549 origin_cu->list_in_scope = cu->list_in_scope;
5550
edb3359d
DJ
5551 if (die->tag != origin_die->tag
5552 && !(die->tag == DW_TAG_inlined_subroutine
5553 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5554 complaint (&symfile_complaints,
5555 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5556 die->offset, origin_die->offset);
5557
5558 child_die = die->child;
5559 die_children_count = 0;
5560 while (child_die && child_die->tag)
5561 {
5562 child_die = sibling_die (child_die);
5563 die_children_count++;
5564 }
5565 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5566 cleanups = make_cleanup (xfree, offsets);
5567
5568 offsets_end = offsets;
5569 child_die = die->child;
5570 while (child_die && child_die->tag)
5571 {
c38f313d
DJ
5572 /* For each CHILD_DIE, find the corresponding child of
5573 ORIGIN_DIE. If there is more than one layer of
5574 DW_AT_abstract_origin, follow them all; there shouldn't be,
5575 but GCC versions at least through 4.4 generate this (GCC PR
5576 40573). */
5577 struct die_info *child_origin_die = child_die;
cd02d79d 5578 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5579
c38f313d
DJ
5580 while (1)
5581 {
cd02d79d
PA
5582 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5583 child_origin_cu);
c38f313d
DJ
5584 if (attr == NULL)
5585 break;
cd02d79d
PA
5586 child_origin_die = follow_die_ref (child_origin_die, attr,
5587 &child_origin_cu);
c38f313d
DJ
5588 }
5589
d389af10
JK
5590 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5591 counterpart may exist. */
c38f313d 5592 if (child_origin_die != child_die)
d389af10 5593 {
edb3359d
DJ
5594 if (child_die->tag != child_origin_die->tag
5595 && !(child_die->tag == DW_TAG_inlined_subroutine
5596 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5597 complaint (&symfile_complaints,
5598 _("Child DIE 0x%x and its abstract origin 0x%x have "
5599 "different tags"), child_die->offset,
5600 child_origin_die->offset);
c38f313d
DJ
5601 if (child_origin_die->parent != origin_die)
5602 complaint (&symfile_complaints,
5603 _("Child DIE 0x%x and its abstract origin 0x%x have "
5604 "different parents"), child_die->offset,
5605 child_origin_die->offset);
5606 else
5607 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5608 }
5609 child_die = sibling_die (child_die);
5610 }
5611 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5612 unsigned_int_compar);
5613 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5614 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5615 complaint (&symfile_complaints,
5616 _("Multiple children of DIE 0x%x refer "
5617 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5618 die->offset, *offsetp);
5619
5620 offsetp = offsets;
5621 origin_child_die = origin_die->child;
5622 while (origin_child_die && origin_child_die->tag)
5623 {
5624 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5625 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5626 offsetp++;
5627 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5628 {
5629 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5630 process_die (origin_child_die, origin_cu);
d389af10
JK
5631 }
5632 origin_child_die = sibling_die (origin_child_die);
5633 }
cd02d79d 5634 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5635
5636 do_cleanups (cleanups);
5637}
5638
c906108c 5639static void
e7c27a73 5640read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5641{
e7c27a73 5642 struct objfile *objfile = cu->objfile;
52f0bd74 5643 struct context_stack *new;
c906108c
SS
5644 CORE_ADDR lowpc;
5645 CORE_ADDR highpc;
5646 struct die_info *child_die;
edb3359d 5647 struct attribute *attr, *call_line, *call_file;
c906108c 5648 char *name;
e142c38c 5649 CORE_ADDR baseaddr;
801e3a5b 5650 struct block *block;
edb3359d 5651 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5652 VEC (symbolp) *template_args = NULL;
5653 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5654
5655 if (inlined_func)
5656 {
5657 /* If we do not have call site information, we can't show the
5658 caller of this inlined function. That's too confusing, so
5659 only use the scope for local variables. */
5660 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5661 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5662 if (call_line == NULL || call_file == NULL)
5663 {
5664 read_lexical_block_scope (die, cu);
5665 return;
5666 }
5667 }
c906108c 5668
e142c38c
DJ
5669 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5670
94af9270 5671 name = dwarf2_name (die, cu);
c906108c 5672
e8d05480
JB
5673 /* Ignore functions with missing or empty names. These are actually
5674 illegal according to the DWARF standard. */
5675 if (name == NULL)
5676 {
5677 complaint (&symfile_complaints,
5678 _("missing name for subprogram DIE at %d"), die->offset);
5679 return;
5680 }
5681
5682 /* Ignore functions with missing or invalid low and high pc attributes. */
5683 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5684 {
ae4d0c03
PM
5685 attr = dwarf2_attr (die, DW_AT_external, cu);
5686 if (!attr || !DW_UNSND (attr))
5687 complaint (&symfile_complaints,
3e43a32a
MS
5688 _("cannot get low and high bounds "
5689 "for subprogram DIE at %d"),
ae4d0c03 5690 die->offset);
e8d05480
JB
5691 return;
5692 }
c906108c
SS
5693
5694 lowpc += baseaddr;
5695 highpc += baseaddr;
5696
5fb290d7 5697 /* Record the function range for dwarf_decode_lines. */
e142c38c 5698 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5699
34eaf542
TT
5700 /* If we have any template arguments, then we must allocate a
5701 different sort of symbol. */
5702 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5703 {
5704 if (child_die->tag == DW_TAG_template_type_param
5705 || child_die->tag == DW_TAG_template_value_param)
5706 {
5707 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5708 struct template_symbol);
5709 templ_func->base.is_cplus_template_function = 1;
5710 break;
5711 }
5712 }
5713
c906108c 5714 new = push_context (0, lowpc);
34eaf542
TT
5715 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5716 (struct symbol *) templ_func);
4c2df51b 5717
4cecd739
DJ
5718 /* If there is a location expression for DW_AT_frame_base, record
5719 it. */
e142c38c 5720 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5721 if (attr)
c034e007
AC
5722 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5723 expression is being recorded directly in the function's symbol
5724 and not in a separate frame-base object. I guess this hack is
5725 to avoid adding some sort of frame-base adjunct/annex to the
5726 function's symbol :-(. The problem with doing this is that it
5727 results in a function symbol with a location expression that
5728 has nothing to do with the location of the function, ouch! The
5729 relationship should be: a function's symbol has-a frame base; a
5730 frame-base has-a location expression. */
e7c27a73 5731 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5732
e142c38c 5733 cu->list_in_scope = &local_symbols;
c906108c 5734
639d11d3 5735 if (die->child != NULL)
c906108c 5736 {
639d11d3 5737 child_die = die->child;
c906108c
SS
5738 while (child_die && child_die->tag)
5739 {
34eaf542
TT
5740 if (child_die->tag == DW_TAG_template_type_param
5741 || child_die->tag == DW_TAG_template_value_param)
5742 {
5743 struct symbol *arg = new_symbol (child_die, NULL, cu);
5744
f1078f66
DJ
5745 if (arg != NULL)
5746 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5747 }
5748 else
5749 process_die (child_die, cu);
c906108c
SS
5750 child_die = sibling_die (child_die);
5751 }
5752 }
5753
d389af10
JK
5754 inherit_abstract_dies (die, cu);
5755
4a811a97
UW
5756 /* If we have a DW_AT_specification, we might need to import using
5757 directives from the context of the specification DIE. See the
5758 comment in determine_prefix. */
5759 if (cu->language == language_cplus
5760 && dwarf2_attr (die, DW_AT_specification, cu))
5761 {
5762 struct dwarf2_cu *spec_cu = cu;
5763 struct die_info *spec_die = die_specification (die, &spec_cu);
5764
5765 while (spec_die)
5766 {
5767 child_die = spec_die->child;
5768 while (child_die && child_die->tag)
5769 {
5770 if (child_die->tag == DW_TAG_imported_module)
5771 process_die (child_die, spec_cu);
5772 child_die = sibling_die (child_die);
5773 }
5774
5775 /* In some cases, GCC generates specification DIEs that
5776 themselves contain DW_AT_specification attributes. */
5777 spec_die = die_specification (spec_die, &spec_cu);
5778 }
5779 }
5780
c906108c
SS
5781 new = pop_context ();
5782 /* Make a block for the local symbols within. */
801e3a5b
JB
5783 block = finish_block (new->name, &local_symbols, new->old_blocks,
5784 lowpc, highpc, objfile);
5785
df8a16a1 5786 /* For C++, set the block's scope. */
f55ee35c 5787 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5788 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5789 determine_prefix (die, cu),
df8a16a1
DJ
5790 processing_has_namespace_info);
5791
801e3a5b
JB
5792 /* If we have address ranges, record them. */
5793 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5794
34eaf542
TT
5795 /* Attach template arguments to function. */
5796 if (! VEC_empty (symbolp, template_args))
5797 {
5798 gdb_assert (templ_func != NULL);
5799
5800 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5801 templ_func->template_arguments
5802 = obstack_alloc (&objfile->objfile_obstack,
5803 (templ_func->n_template_arguments
5804 * sizeof (struct symbol *)));
5805 memcpy (templ_func->template_arguments,
5806 VEC_address (symbolp, template_args),
5807 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5808 VEC_free (symbolp, template_args);
5809 }
5810
208d8187
JB
5811 /* In C++, we can have functions nested inside functions (e.g., when
5812 a function declares a class that has methods). This means that
5813 when we finish processing a function scope, we may need to go
5814 back to building a containing block's symbol lists. */
5815 local_symbols = new->locals;
5816 param_symbols = new->params;
27aa8d6a 5817 using_directives = new->using_directives;
208d8187 5818
921e78cf
JB
5819 /* If we've finished processing a top-level function, subsequent
5820 symbols go in the file symbol list. */
5821 if (outermost_context_p ())
e142c38c 5822 cu->list_in_scope = &file_symbols;
c906108c
SS
5823}
5824
5825/* Process all the DIES contained within a lexical block scope. Start
5826 a new scope, process the dies, and then close the scope. */
5827
5828static void
e7c27a73 5829read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5830{
e7c27a73 5831 struct objfile *objfile = cu->objfile;
52f0bd74 5832 struct context_stack *new;
c906108c
SS
5833 CORE_ADDR lowpc, highpc;
5834 struct die_info *child_die;
e142c38c
DJ
5835 CORE_ADDR baseaddr;
5836
5837 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5838
5839 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5840 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5841 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5842 be nasty. Might be easier to properly extend generic blocks to
af34e669 5843 describe ranges. */
d85a05f0 5844 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5845 return;
5846 lowpc += baseaddr;
5847 highpc += baseaddr;
5848
5849 push_context (0, lowpc);
639d11d3 5850 if (die->child != NULL)
c906108c 5851 {
639d11d3 5852 child_die = die->child;
c906108c
SS
5853 while (child_die && child_die->tag)
5854 {
e7c27a73 5855 process_die (child_die, cu);
c906108c
SS
5856 child_die = sibling_die (child_die);
5857 }
5858 }
5859 new = pop_context ();
5860
8540c487 5861 if (local_symbols != NULL || using_directives != NULL)
c906108c 5862 {
801e3a5b
JB
5863 struct block *block
5864 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5865 highpc, objfile);
5866
5867 /* Note that recording ranges after traversing children, as we
5868 do here, means that recording a parent's ranges entails
5869 walking across all its children's ranges as they appear in
5870 the address map, which is quadratic behavior.
5871
5872 It would be nicer to record the parent's ranges before
5873 traversing its children, simply overriding whatever you find
5874 there. But since we don't even decide whether to create a
5875 block until after we've traversed its children, that's hard
5876 to do. */
5877 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5878 }
5879 local_symbols = new->locals;
27aa8d6a 5880 using_directives = new->using_directives;
c906108c
SS
5881}
5882
43039443 5883/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5884 Return 1 if the attributes are present and valid, otherwise, return 0.
5885 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5886
5887static int
5888dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5889 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5890 struct partial_symtab *ranges_pst)
43039443
JK
5891{
5892 struct objfile *objfile = cu->objfile;
5893 struct comp_unit_head *cu_header = &cu->header;
5894 bfd *obfd = objfile->obfd;
5895 unsigned int addr_size = cu_header->addr_size;
5896 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5897 /* Base address selection entry. */
5898 CORE_ADDR base;
5899 int found_base;
5900 unsigned int dummy;
5901 gdb_byte *buffer;
5902 CORE_ADDR marker;
5903 int low_set;
5904 CORE_ADDR low = 0;
5905 CORE_ADDR high = 0;
ff013f42 5906 CORE_ADDR baseaddr;
43039443 5907
d00adf39
DE
5908 found_base = cu->base_known;
5909 base = cu->base_address;
43039443 5910
be391dca 5911 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5912 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5913 {
5914 complaint (&symfile_complaints,
5915 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5916 offset);
5917 return 0;
5918 }
dce234bc 5919 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5920
5921 /* Read in the largest possible address. */
5922 marker = read_address (obfd, buffer, cu, &dummy);
5923 if ((marker & mask) == mask)
5924 {
5925 /* If we found the largest possible address, then
5926 read the base address. */
5927 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5928 buffer += 2 * addr_size;
5929 offset += 2 * addr_size;
5930 found_base = 1;
5931 }
5932
5933 low_set = 0;
5934
e7030f15 5935 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5936
43039443
JK
5937 while (1)
5938 {
5939 CORE_ADDR range_beginning, range_end;
5940
5941 range_beginning = read_address (obfd, buffer, cu, &dummy);
5942 buffer += addr_size;
5943 range_end = read_address (obfd, buffer, cu, &dummy);
5944 buffer += addr_size;
5945 offset += 2 * addr_size;
5946
5947 /* An end of list marker is a pair of zero addresses. */
5948 if (range_beginning == 0 && range_end == 0)
5949 /* Found the end of list entry. */
5950 break;
5951
5952 /* Each base address selection entry is a pair of 2 values.
5953 The first is the largest possible address, the second is
5954 the base address. Check for a base address here. */
5955 if ((range_beginning & mask) == mask)
5956 {
5957 /* If we found the largest possible address, then
5958 read the base address. */
5959 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5960 found_base = 1;
5961 continue;
5962 }
5963
5964 if (!found_base)
5965 {
5966 /* We have no valid base address for the ranges
5967 data. */
5968 complaint (&symfile_complaints,
5969 _("Invalid .debug_ranges data (no base address)"));
5970 return 0;
5971 }
5972
9277c30c
UW
5973 if (range_beginning > range_end)
5974 {
5975 /* Inverted range entries are invalid. */
5976 complaint (&symfile_complaints,
5977 _("Invalid .debug_ranges data (inverted range)"));
5978 return 0;
5979 }
5980
5981 /* Empty range entries have no effect. */
5982 if (range_beginning == range_end)
5983 continue;
5984
43039443
JK
5985 range_beginning += base;
5986 range_end += base;
5987
9277c30c 5988 if (ranges_pst != NULL)
ff013f42 5989 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
5990 range_beginning + baseaddr,
5991 range_end - 1 + baseaddr,
ff013f42
JK
5992 ranges_pst);
5993
43039443
JK
5994 /* FIXME: This is recording everything as a low-high
5995 segment of consecutive addresses. We should have a
5996 data structure for discontiguous block ranges
5997 instead. */
5998 if (! low_set)
5999 {
6000 low = range_beginning;
6001 high = range_end;
6002 low_set = 1;
6003 }
6004 else
6005 {
6006 if (range_beginning < low)
6007 low = range_beginning;
6008 if (range_end > high)
6009 high = range_end;
6010 }
6011 }
6012
6013 if (! low_set)
6014 /* If the first entry is an end-of-list marker, the range
6015 describes an empty scope, i.e. no instructions. */
6016 return 0;
6017
6018 if (low_return)
6019 *low_return = low;
6020 if (high_return)
6021 *high_return = high;
6022 return 1;
6023}
6024
af34e669
DJ
6025/* Get low and high pc attributes from a die. Return 1 if the attributes
6026 are present and valid, otherwise, return 0. Return -1 if the range is
6027 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6028static int
af34e669 6029dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6030 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6031 struct partial_symtab *pst)
c906108c
SS
6032{
6033 struct attribute *attr;
af34e669
DJ
6034 CORE_ADDR low = 0;
6035 CORE_ADDR high = 0;
6036 int ret = 0;
c906108c 6037
e142c38c 6038 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6039 if (attr)
af34e669
DJ
6040 {
6041 high = DW_ADDR (attr);
e142c38c 6042 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6043 if (attr)
6044 low = DW_ADDR (attr);
6045 else
6046 /* Found high w/o low attribute. */
6047 return 0;
6048
6049 /* Found consecutive range of addresses. */
6050 ret = 1;
6051 }
c906108c 6052 else
af34e669 6053 {
e142c38c 6054 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6055 if (attr != NULL)
6056 {
af34e669 6057 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6058 .debug_ranges section. */
d85a05f0 6059 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6060 return 0;
43039443 6061 /* Found discontinuous range of addresses. */
af34e669
DJ
6062 ret = -1;
6063 }
6064 }
c906108c 6065
9373cf26
JK
6066 /* read_partial_die has also the strict LOW < HIGH requirement. */
6067 if (high <= low)
c906108c
SS
6068 return 0;
6069
6070 /* When using the GNU linker, .gnu.linkonce. sections are used to
6071 eliminate duplicate copies of functions and vtables and such.
6072 The linker will arbitrarily choose one and discard the others.
6073 The AT_*_pc values for such functions refer to local labels in
6074 these sections. If the section from that file was discarded, the
6075 labels are not in the output, so the relocs get a value of 0.
6076 If this is a discarded function, mark the pc bounds as invalid,
6077 so that GDB will ignore it. */
72dca2f5 6078 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6079 return 0;
6080
6081 *lowpc = low;
6082 *highpc = high;
af34e669 6083 return ret;
c906108c
SS
6084}
6085
b084d499
JB
6086/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6087 its low and high PC addresses. Do nothing if these addresses could not
6088 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6089 and HIGHPC to the high address if greater than HIGHPC. */
6090
6091static void
6092dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6093 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6094 struct dwarf2_cu *cu)
6095{
6096 CORE_ADDR low, high;
6097 struct die_info *child = die->child;
6098
d85a05f0 6099 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6100 {
6101 *lowpc = min (*lowpc, low);
6102 *highpc = max (*highpc, high);
6103 }
6104
6105 /* If the language does not allow nested subprograms (either inside
6106 subprograms or lexical blocks), we're done. */
6107 if (cu->language != language_ada)
6108 return;
6e70227d 6109
b084d499
JB
6110 /* Check all the children of the given DIE. If it contains nested
6111 subprograms, then check their pc bounds. Likewise, we need to
6112 check lexical blocks as well, as they may also contain subprogram
6113 definitions. */
6114 while (child && child->tag)
6115 {
6116 if (child->tag == DW_TAG_subprogram
6117 || child->tag == DW_TAG_lexical_block)
6118 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6119 child = sibling_die (child);
6120 }
6121}
6122
fae299cd
DC
6123/* Get the low and high pc's represented by the scope DIE, and store
6124 them in *LOWPC and *HIGHPC. If the correct values can't be
6125 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6126
6127static void
6128get_scope_pc_bounds (struct die_info *die,
6129 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6130 struct dwarf2_cu *cu)
6131{
6132 CORE_ADDR best_low = (CORE_ADDR) -1;
6133 CORE_ADDR best_high = (CORE_ADDR) 0;
6134 CORE_ADDR current_low, current_high;
6135
d85a05f0 6136 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6137 {
6138 best_low = current_low;
6139 best_high = current_high;
6140 }
6141 else
6142 {
6143 struct die_info *child = die->child;
6144
6145 while (child && child->tag)
6146 {
6147 switch (child->tag) {
6148 case DW_TAG_subprogram:
b084d499 6149 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6150 break;
6151 case DW_TAG_namespace:
f55ee35c 6152 case DW_TAG_module:
fae299cd
DC
6153 /* FIXME: carlton/2004-01-16: Should we do this for
6154 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6155 that current GCC's always emit the DIEs corresponding
6156 to definitions of methods of classes as children of a
6157 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6158 the DIEs giving the declarations, which could be
6159 anywhere). But I don't see any reason why the
6160 standards says that they have to be there. */
6161 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6162
6163 if (current_low != ((CORE_ADDR) -1))
6164 {
6165 best_low = min (best_low, current_low);
6166 best_high = max (best_high, current_high);
6167 }
6168 break;
6169 default:
0963b4bd 6170 /* Ignore. */
fae299cd
DC
6171 break;
6172 }
6173
6174 child = sibling_die (child);
6175 }
6176 }
6177
6178 *lowpc = best_low;
6179 *highpc = best_high;
6180}
6181
801e3a5b
JB
6182/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6183 in DIE. */
6184static void
6185dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6186 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6187{
6188 struct attribute *attr;
6189
6190 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6191 if (attr)
6192 {
6193 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6194
801e3a5b
JB
6195 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6196 if (attr)
6197 {
6198 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6199
801e3a5b
JB
6200 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6201 }
6202 }
6203
6204 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6205 if (attr)
6206 {
6207 bfd *obfd = cu->objfile->obfd;
6208
6209 /* The value of the DW_AT_ranges attribute is the offset of the
6210 address range list in the .debug_ranges section. */
6211 unsigned long offset = DW_UNSND (attr);
dce234bc 6212 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6213
6214 /* For some target architectures, but not others, the
6215 read_address function sign-extends the addresses it returns.
6216 To recognize base address selection entries, we need a
6217 mask. */
6218 unsigned int addr_size = cu->header.addr_size;
6219 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6220
6221 /* The base address, to which the next pair is relative. Note
6222 that this 'base' is a DWARF concept: most entries in a range
6223 list are relative, to reduce the number of relocs against the
6224 debugging information. This is separate from this function's
6225 'baseaddr' argument, which GDB uses to relocate debugging
6226 information from a shared library based on the address at
6227 which the library was loaded. */
d00adf39
DE
6228 CORE_ADDR base = cu->base_address;
6229 int base_known = cu->base_known;
801e3a5b 6230
be391dca 6231 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6232 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6233 {
6234 complaint (&symfile_complaints,
6235 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6236 offset);
6237 return;
6238 }
6239
6240 for (;;)
6241 {
6242 unsigned int bytes_read;
6243 CORE_ADDR start, end;
6244
6245 start = read_address (obfd, buffer, cu, &bytes_read);
6246 buffer += bytes_read;
6247 end = read_address (obfd, buffer, cu, &bytes_read);
6248 buffer += bytes_read;
6249
6250 /* Did we find the end of the range list? */
6251 if (start == 0 && end == 0)
6252 break;
6253
6254 /* Did we find a base address selection entry? */
6255 else if ((start & base_select_mask) == base_select_mask)
6256 {
6257 base = end;
6258 base_known = 1;
6259 }
6260
6261 /* We found an ordinary address range. */
6262 else
6263 {
6264 if (!base_known)
6265 {
6266 complaint (&symfile_complaints,
3e43a32a
MS
6267 _("Invalid .debug_ranges data "
6268 "(no base address)"));
801e3a5b
JB
6269 return;
6270 }
6271
9277c30c
UW
6272 if (start > end)
6273 {
6274 /* Inverted range entries are invalid. */
6275 complaint (&symfile_complaints,
6276 _("Invalid .debug_ranges data "
6277 "(inverted range)"));
6278 return;
6279 }
6280
6281 /* Empty range entries have no effect. */
6282 if (start == end)
6283 continue;
6284
6e70227d
DE
6285 record_block_range (block,
6286 baseaddr + base + start,
801e3a5b
JB
6287 baseaddr + base + end - 1);
6288 }
6289 }
6290 }
6291}
6292
60d5a603
JK
6293/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6294 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6295 during 4.6.0 experimental. */
6296
6297static int
6298producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6299{
6300 const char *cs;
6301 int major, minor, release;
6302
6303 if (cu->producer == NULL)
6304 {
6305 /* For unknown compilers expect their behavior is DWARF version
6306 compliant.
6307
6308 GCC started to support .debug_types sections by -gdwarf-4 since
6309 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6310 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6311 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6312 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6313
6314 return 0;
6315 }
6316
6317 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6318
6319 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6320 {
6321 /* For non-GCC compilers expect their behavior is DWARF version
6322 compliant. */
6323
6324 return 0;
6325 }
6326 cs = &cu->producer[strlen ("GNU ")];
6327 while (*cs && !isdigit (*cs))
6328 cs++;
6329 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6330 {
6331 /* Not recognized as GCC. */
6332
6333 return 0;
6334 }
6335
6336 return major < 4 || (major == 4 && minor < 6);
6337}
6338
6339/* Return the default accessibility type if it is not overriden by
6340 DW_AT_accessibility. */
6341
6342static enum dwarf_access_attribute
6343dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6344{
6345 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6346 {
6347 /* The default DWARF 2 accessibility for members is public, the default
6348 accessibility for inheritance is private. */
6349
6350 if (die->tag != DW_TAG_inheritance)
6351 return DW_ACCESS_public;
6352 else
6353 return DW_ACCESS_private;
6354 }
6355 else
6356 {
6357 /* DWARF 3+ defines the default accessibility a different way. The same
6358 rules apply now for DW_TAG_inheritance as for the members and it only
6359 depends on the container kind. */
6360
6361 if (die->parent->tag == DW_TAG_class_type)
6362 return DW_ACCESS_private;
6363 else
6364 return DW_ACCESS_public;
6365 }
6366}
6367
74ac6d43
TT
6368/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6369 offset. If the attribute was not found return 0, otherwise return
6370 1. If it was found but could not properly be handled, set *OFFSET
6371 to 0. */
6372
6373static int
6374handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6375 LONGEST *offset)
6376{
6377 struct attribute *attr;
6378
6379 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6380 if (attr != NULL)
6381 {
6382 *offset = 0;
6383
6384 /* Note that we do not check for a section offset first here.
6385 This is because DW_AT_data_member_location is new in DWARF 4,
6386 so if we see it, we can assume that a constant form is really
6387 a constant and not a section offset. */
6388 if (attr_form_is_constant (attr))
6389 *offset = dwarf2_get_attr_constant_value (attr, 0);
6390 else if (attr_form_is_section_offset (attr))
6391 dwarf2_complex_location_expr_complaint ();
6392 else if (attr_form_is_block (attr))
6393 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6394 else
6395 dwarf2_complex_location_expr_complaint ();
6396
6397 return 1;
6398 }
6399
6400 return 0;
6401}
6402
c906108c
SS
6403/* Add an aggregate field to the field list. */
6404
6405static void
107d2387 6406dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6407 struct dwarf2_cu *cu)
6e70227d 6408{
e7c27a73 6409 struct objfile *objfile = cu->objfile;
5e2b427d 6410 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6411 struct nextfield *new_field;
6412 struct attribute *attr;
6413 struct field *fp;
6414 char *fieldname = "";
6415
6416 /* Allocate a new field list entry and link it in. */
6417 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6418 make_cleanup (xfree, new_field);
c906108c 6419 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6420
6421 if (die->tag == DW_TAG_inheritance)
6422 {
6423 new_field->next = fip->baseclasses;
6424 fip->baseclasses = new_field;
6425 }
6426 else
6427 {
6428 new_field->next = fip->fields;
6429 fip->fields = new_field;
6430 }
c906108c
SS
6431 fip->nfields++;
6432
e142c38c 6433 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6434 if (attr)
6435 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
6436 else
6437 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
6438 if (new_field->accessibility != DW_ACCESS_public)
6439 fip->non_public_fields = 1;
60d5a603 6440
e142c38c 6441 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6442 if (attr)
6443 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
6444 else
6445 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
6446
6447 fp = &new_field->field;
a9a9bd0f 6448
e142c38c 6449 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6450 {
74ac6d43
TT
6451 LONGEST offset;
6452
a9a9bd0f 6453 /* Data member other than a C++ static data member. */
6e70227d 6454
c906108c 6455 /* Get type of field. */
e7c27a73 6456 fp->type = die_type (die, cu);
c906108c 6457
d6a843b5 6458 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6459
c906108c 6460 /* Get bit size of field (zero if none). */
e142c38c 6461 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6462 if (attr)
6463 {
6464 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6465 }
6466 else
6467 {
6468 FIELD_BITSIZE (*fp) = 0;
6469 }
6470
6471 /* Get bit offset of field. */
74ac6d43
TT
6472 if (handle_data_member_location (die, cu, &offset))
6473 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 6474 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6475 if (attr)
6476 {
5e2b427d 6477 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6478 {
6479 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6480 additional bit offset from the MSB of the containing
6481 anonymous object to the MSB of the field. We don't
6482 have to do anything special since we don't need to
6483 know the size of the anonymous object. */
c906108c
SS
6484 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6485 }
6486 else
6487 {
6488 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6489 MSB of the anonymous object, subtract off the number of
6490 bits from the MSB of the field to the MSB of the
6491 object, and then subtract off the number of bits of
6492 the field itself. The result is the bit offset of
6493 the LSB of the field. */
c906108c
SS
6494 int anonymous_size;
6495 int bit_offset = DW_UNSND (attr);
6496
e142c38c 6497 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6498 if (attr)
6499 {
6500 /* The size of the anonymous object containing
6501 the bit field is explicit, so use the
6502 indicated size (in bytes). */
6503 anonymous_size = DW_UNSND (attr);
6504 }
6505 else
6506 {
6507 /* The size of the anonymous object containing
6508 the bit field must be inferred from the type
6509 attribute of the data member containing the
6510 bit field. */
6511 anonymous_size = TYPE_LENGTH (fp->type);
6512 }
6513 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6514 - bit_offset - FIELD_BITSIZE (*fp);
6515 }
6516 }
6517
6518 /* Get name of field. */
39cbfefa
DJ
6519 fieldname = dwarf2_name (die, cu);
6520 if (fieldname == NULL)
6521 fieldname = "";
d8151005
DJ
6522
6523 /* The name is already allocated along with this objfile, so we don't
6524 need to duplicate it for the type. */
6525 fp->name = fieldname;
c906108c
SS
6526
6527 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6528 pointer or virtual base class pointer) to private. */
e142c38c 6529 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6530 {
d48cc9dd 6531 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6532 new_field->accessibility = DW_ACCESS_private;
6533 fip->non_public_fields = 1;
6534 }
6535 }
a9a9bd0f 6536 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6537 {
a9a9bd0f
DC
6538 /* C++ static member. */
6539
6540 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6541 is a declaration, but all versions of G++ as of this writing
6542 (so through at least 3.2.1) incorrectly generate
6543 DW_TAG_variable tags. */
6e70227d 6544
c906108c 6545 char *physname;
c906108c 6546
a9a9bd0f 6547 /* Get name of field. */
39cbfefa
DJ
6548 fieldname = dwarf2_name (die, cu);
6549 if (fieldname == NULL)
c906108c
SS
6550 return;
6551
254e6b9e 6552 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6553 if (attr
6554 /* Only create a symbol if this is an external value.
6555 new_symbol checks this and puts the value in the global symbol
6556 table, which we want. If it is not external, new_symbol
6557 will try to put the value in cu->list_in_scope which is wrong. */
6558 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6559 {
6560 /* A static const member, not much different than an enum as far as
6561 we're concerned, except that we can support more types. */
6562 new_symbol (die, NULL, cu);
6563 }
6564
2df3850c 6565 /* Get physical name. */
94af9270 6566 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6567
d8151005
DJ
6568 /* The name is already allocated along with this objfile, so we don't
6569 need to duplicate it for the type. */
6570 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6571 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6572 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6573 }
6574 else if (die->tag == DW_TAG_inheritance)
6575 {
74ac6d43 6576 LONGEST offset;
d4b96c9a 6577
74ac6d43
TT
6578 /* C++ base class field. */
6579 if (handle_data_member_location (die, cu, &offset))
6580 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 6581 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6582 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6583 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6584 fip->nbaseclasses++;
6585 }
6586}
6587
98751a41
JK
6588/* Add a typedef defined in the scope of the FIP's class. */
6589
6590static void
6591dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6592 struct dwarf2_cu *cu)
6e70227d 6593{
98751a41 6594 struct objfile *objfile = cu->objfile;
98751a41
JK
6595 struct typedef_field_list *new_field;
6596 struct attribute *attr;
6597 struct typedef_field *fp;
6598 char *fieldname = "";
6599
6600 /* Allocate a new field list entry and link it in. */
6601 new_field = xzalloc (sizeof (*new_field));
6602 make_cleanup (xfree, new_field);
6603
6604 gdb_assert (die->tag == DW_TAG_typedef);
6605
6606 fp = &new_field->field;
6607
6608 /* Get name of field. */
6609 fp->name = dwarf2_name (die, cu);
6610 if (fp->name == NULL)
6611 return;
6612
6613 fp->type = read_type_die (die, cu);
6614
6615 new_field->next = fip->typedef_field_list;
6616 fip->typedef_field_list = new_field;
6617 fip->typedef_field_list_count++;
6618}
6619
c906108c
SS
6620/* Create the vector of fields, and attach it to the type. */
6621
6622static void
fba45db2 6623dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6624 struct dwarf2_cu *cu)
c906108c
SS
6625{
6626 int nfields = fip->nfields;
6627
6628 /* Record the field count, allocate space for the array of fields,
6629 and create blank accessibility bitfields if necessary. */
6630 TYPE_NFIELDS (type) = nfields;
6631 TYPE_FIELDS (type) = (struct field *)
6632 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6633 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6634
b4ba55a1 6635 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6636 {
6637 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6638
6639 TYPE_FIELD_PRIVATE_BITS (type) =
6640 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6641 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6642
6643 TYPE_FIELD_PROTECTED_BITS (type) =
6644 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6645 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6646
774b6a14
TT
6647 TYPE_FIELD_IGNORE_BITS (type) =
6648 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6649 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6650 }
6651
6652 /* If the type has baseclasses, allocate and clear a bit vector for
6653 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6654 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6655 {
6656 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6657 unsigned char *pointer;
c906108c
SS
6658
6659 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6660 pointer = TYPE_ALLOC (type, num_bytes);
6661 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6662 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6663 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6664 }
6665
3e43a32a
MS
6666 /* Copy the saved-up fields into the field vector. Start from the head of
6667 the list, adding to the tail of the field array, so that they end up in
6668 the same order in the array in which they were added to the list. */
c906108c
SS
6669 while (nfields-- > 0)
6670 {
7d0ccb61
DJ
6671 struct nextfield *fieldp;
6672
6673 if (fip->fields)
6674 {
6675 fieldp = fip->fields;
6676 fip->fields = fieldp->next;
6677 }
6678 else
6679 {
6680 fieldp = fip->baseclasses;
6681 fip->baseclasses = fieldp->next;
6682 }
6683
6684 TYPE_FIELD (type, nfields) = fieldp->field;
6685 switch (fieldp->accessibility)
c906108c 6686 {
c5aa993b 6687 case DW_ACCESS_private:
b4ba55a1
JB
6688 if (cu->language != language_ada)
6689 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6690 break;
c906108c 6691
c5aa993b 6692 case DW_ACCESS_protected:
b4ba55a1
JB
6693 if (cu->language != language_ada)
6694 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6695 break;
c906108c 6696
c5aa993b
JM
6697 case DW_ACCESS_public:
6698 break;
c906108c 6699
c5aa993b
JM
6700 default:
6701 /* Unknown accessibility. Complain and treat it as public. */
6702 {
e2e0b3e5 6703 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6704 fieldp->accessibility);
c5aa993b
JM
6705 }
6706 break;
c906108c
SS
6707 }
6708 if (nfields < fip->nbaseclasses)
6709 {
7d0ccb61 6710 switch (fieldp->virtuality)
c906108c 6711 {
c5aa993b
JM
6712 case DW_VIRTUALITY_virtual:
6713 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 6714 if (cu->language == language_ada)
a73c6dcd 6715 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
6716 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6717 break;
c906108c
SS
6718 }
6719 }
c906108c
SS
6720 }
6721}
6722
c906108c
SS
6723/* Add a member function to the proper fieldlist. */
6724
6725static void
107d2387 6726dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6727 struct type *type, struct dwarf2_cu *cu)
c906108c 6728{
e7c27a73 6729 struct objfile *objfile = cu->objfile;
c906108c
SS
6730 struct attribute *attr;
6731 struct fnfieldlist *flp;
6732 int i;
6733 struct fn_field *fnp;
6734 char *fieldname;
c906108c 6735 struct nextfnfield *new_fnfield;
f792889a 6736 struct type *this_type;
60d5a603 6737 enum dwarf_access_attribute accessibility;
c906108c 6738
b4ba55a1 6739 if (cu->language == language_ada)
a73c6dcd 6740 error (_("unexpected member function in Ada type"));
b4ba55a1 6741
2df3850c 6742 /* Get name of member function. */
39cbfefa
DJ
6743 fieldname = dwarf2_name (die, cu);
6744 if (fieldname == NULL)
2df3850c 6745 return;
c906108c 6746
c906108c
SS
6747 /* Look up member function name in fieldlist. */
6748 for (i = 0; i < fip->nfnfields; i++)
6749 {
27bfe10e 6750 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6751 break;
6752 }
6753
6754 /* Create new list element if necessary. */
6755 if (i < fip->nfnfields)
6756 flp = &fip->fnfieldlists[i];
6757 else
6758 {
6759 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6760 {
6761 fip->fnfieldlists = (struct fnfieldlist *)
6762 xrealloc (fip->fnfieldlists,
6763 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6764 * sizeof (struct fnfieldlist));
c906108c 6765 if (fip->nfnfields == 0)
c13c43fd 6766 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6767 }
6768 flp = &fip->fnfieldlists[fip->nfnfields];
6769 flp->name = fieldname;
6770 flp->length = 0;
6771 flp->head = NULL;
3da10d80 6772 i = fip->nfnfields++;
c906108c
SS
6773 }
6774
6775 /* Create a new member function field and chain it to the field list
0963b4bd 6776 entry. */
c906108c 6777 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6778 make_cleanup (xfree, new_fnfield);
c906108c
SS
6779 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6780 new_fnfield->next = flp->head;
6781 flp->head = new_fnfield;
6782 flp->length++;
6783
6784 /* Fill in the member function field info. */
6785 fnp = &new_fnfield->fnfield;
3da10d80
KS
6786
6787 /* Delay processing of the physname until later. */
6788 if (cu->language == language_cplus || cu->language == language_java)
6789 {
6790 add_to_method_list (type, i, flp->length - 1, fieldname,
6791 die, cu);
6792 }
6793 else
6794 {
6795 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6796 fnp->physname = physname ? physname : "";
6797 }
6798
c906108c 6799 fnp->type = alloc_type (objfile);
f792889a
DJ
6800 this_type = read_type_die (die, cu);
6801 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6802 {
f792889a 6803 int nparams = TYPE_NFIELDS (this_type);
c906108c 6804
f792889a 6805 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6806 of the method itself (TYPE_CODE_METHOD). */
6807 smash_to_method_type (fnp->type, type,
f792889a
DJ
6808 TYPE_TARGET_TYPE (this_type),
6809 TYPE_FIELDS (this_type),
6810 TYPE_NFIELDS (this_type),
6811 TYPE_VARARGS (this_type));
c906108c
SS
6812
6813 /* Handle static member functions.
c5aa993b 6814 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
6815 member functions. G++ helps GDB by marking the first
6816 parameter for non-static member functions (which is the this
6817 pointer) as artificial. We obtain this information from
6818 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6819 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6820 fnp->voffset = VOFFSET_STATIC;
6821 }
6822 else
e2e0b3e5 6823 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6824 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6825
6826 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6827 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6828 fnp->fcontext = die_containing_type (die, cu);
c906108c 6829
3e43a32a
MS
6830 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6831 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
6832
6833 /* Get accessibility. */
e142c38c 6834 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 6835 if (attr)
60d5a603
JK
6836 accessibility = DW_UNSND (attr);
6837 else
6838 accessibility = dwarf2_default_access_attribute (die, cu);
6839 switch (accessibility)
c906108c 6840 {
60d5a603
JK
6841 case DW_ACCESS_private:
6842 fnp->is_private = 1;
6843 break;
6844 case DW_ACCESS_protected:
6845 fnp->is_protected = 1;
6846 break;
c906108c
SS
6847 }
6848
b02dede2 6849 /* Check for artificial methods. */
e142c38c 6850 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6851 if (attr && DW_UNSND (attr) != 0)
6852 fnp->is_artificial = 1;
6853
0d564a31 6854 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6855 function. For older versions of GCC, this is an offset in the
6856 appropriate virtual table, as specified by DW_AT_containing_type.
6857 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6858 to the object address. */
6859
e142c38c 6860 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6861 if (attr)
8e19ed76 6862 {
aec5aa8b 6863 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6864 {
aec5aa8b
TT
6865 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6866 {
6867 /* Old-style GCC. */
6868 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6869 }
6870 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6871 || (DW_BLOCK (attr)->size > 1
6872 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6873 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6874 {
6875 struct dwarf_block blk;
6876 int offset;
6877
6878 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6879 ? 1 : 2);
6880 blk.size = DW_BLOCK (attr)->size - offset;
6881 blk.data = DW_BLOCK (attr)->data + offset;
6882 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6883 if ((fnp->voffset % cu->header.addr_size) != 0)
6884 dwarf2_complex_location_expr_complaint ();
6885 else
6886 fnp->voffset /= cu->header.addr_size;
6887 fnp->voffset += 2;
6888 }
6889 else
6890 dwarf2_complex_location_expr_complaint ();
6891
6892 if (!fnp->fcontext)
6893 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6894 }
3690dd37 6895 else if (attr_form_is_section_offset (attr))
8e19ed76 6896 {
4d3c2250 6897 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6898 }
6899 else
6900 {
4d3c2250
KB
6901 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6902 fieldname);
8e19ed76 6903 }
0d564a31 6904 }
d48cc9dd
DJ
6905 else
6906 {
6907 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6908 if (attr && DW_UNSND (attr))
6909 {
6910 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6911 complaint (&symfile_complaints,
3e43a32a
MS
6912 _("Member function \"%s\" (offset %d) is virtual "
6913 "but the vtable offset is not specified"),
d48cc9dd 6914 fieldname, die->offset);
9655fd1a 6915 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6916 TYPE_CPLUS_DYNAMIC (type) = 1;
6917 }
6918 }
c906108c
SS
6919}
6920
6921/* Create the vector of member function fields, and attach it to the type. */
6922
6923static void
fba45db2 6924dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6925 struct dwarf2_cu *cu)
c906108c
SS
6926{
6927 struct fnfieldlist *flp;
6928 int total_length = 0;
6929 int i;
6930
b4ba55a1 6931 if (cu->language == language_ada)
a73c6dcd 6932 error (_("unexpected member functions in Ada type"));
b4ba55a1 6933
c906108c
SS
6934 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6935 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6936 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6937
6938 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6939 {
6940 struct nextfnfield *nfp = flp->head;
6941 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6942 int k;
6943
6944 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6945 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6946 fn_flp->fn_fields = (struct fn_field *)
6947 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6948 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6949 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6950
6951 total_length += flp->length;
6952 }
6953
6954 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6955 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6956}
6957
1168df01
JB
6958/* Returns non-zero if NAME is the name of a vtable member in CU's
6959 language, zero otherwise. */
6960static int
6961is_vtable_name (const char *name, struct dwarf2_cu *cu)
6962{
6963 static const char vptr[] = "_vptr";
987504bb 6964 static const char vtable[] = "vtable";
1168df01 6965
987504bb
JJ
6966 /* Look for the C++ and Java forms of the vtable. */
6967 if ((cu->language == language_java
6968 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6969 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6970 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6971 return 1;
6972
6973 return 0;
6974}
6975
c0dd20ea 6976/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6977 functions, with the ABI-specified layout. If TYPE describes
6978 such a structure, smash it into a member function type.
61049d3b
DJ
6979
6980 GCC shouldn't do this; it should just output pointer to member DIEs.
6981 This is GCC PR debug/28767. */
c0dd20ea 6982
0b92b5bb
TT
6983static void
6984quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6985{
0b92b5bb 6986 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6987
6988 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6989 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6990 return;
c0dd20ea
DJ
6991
6992 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6993 if (TYPE_FIELD_NAME (type, 0) == NULL
6994 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6995 || TYPE_FIELD_NAME (type, 1) == NULL
6996 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6997 return;
c0dd20ea
DJ
6998
6999 /* Find the type of the method. */
0b92b5bb 7000 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7001 if (pfn_type == NULL
7002 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7003 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7004 return;
c0dd20ea
DJ
7005
7006 /* Look for the "this" argument. */
7007 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7008 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7009 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7010 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7011 return;
c0dd20ea
DJ
7012
7013 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7014 new_type = alloc_type (objfile);
7015 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7016 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7017 TYPE_VARARGS (pfn_type));
0b92b5bb 7018 smash_to_methodptr_type (type, new_type);
c0dd20ea 7019}
1168df01 7020
c906108c 7021/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7022 (definition) to create a type for the structure or union. Fill in
7023 the type's name and general properties; the members will not be
7024 processed until process_structure_type.
c906108c 7025
c767944b
DJ
7026 NOTE: we need to call these functions regardless of whether or not the
7027 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7028 structure or union. This gets the type entered into our set of
7029 user defined types.
7030
7031 However, if the structure is incomplete (an opaque struct/union)
7032 then suppress creating a symbol table entry for it since gdb only
7033 wants to find the one with the complete definition. Note that if
7034 it is complete, we just call new_symbol, which does it's own
7035 checking about whether the struct/union is anonymous or not (and
7036 suppresses creating a symbol table entry itself). */
7037
f792889a 7038static struct type *
134d01f1 7039read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7040{
e7c27a73 7041 struct objfile *objfile = cu->objfile;
c906108c
SS
7042 struct type *type;
7043 struct attribute *attr;
39cbfefa 7044 char *name;
c906108c 7045
348e048f
DE
7046 /* If the definition of this type lives in .debug_types, read that type.
7047 Don't follow DW_AT_specification though, that will take us back up
7048 the chain and we want to go down. */
7049 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7050 if (attr)
7051 {
7052 struct dwarf2_cu *type_cu = cu;
7053 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7054
348e048f
DE
7055 /* We could just recurse on read_structure_type, but we need to call
7056 get_die_type to ensure only one type for this DIE is created.
7057 This is important, for example, because for c++ classes we need
7058 TYPE_NAME set which is only done by new_symbol. Blech. */
7059 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7060
7061 /* TYPE_CU may not be the same as CU.
7062 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7063 return set_die_type (die, type, cu);
7064 }
7065
c0dd20ea 7066 type = alloc_type (objfile);
c906108c 7067 INIT_CPLUS_SPECIFIC (type);
93311388 7068
39cbfefa
DJ
7069 name = dwarf2_name (die, cu);
7070 if (name != NULL)
c906108c 7071 {
987504bb
JJ
7072 if (cu->language == language_cplus
7073 || cu->language == language_java)
63d06c5c 7074 {
3da10d80
KS
7075 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7076
7077 /* dwarf2_full_name might have already finished building the DIE's
7078 type. If so, there is no need to continue. */
7079 if (get_die_type (die, cu) != NULL)
7080 return get_die_type (die, cu);
7081
7082 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7083 if (die->tag == DW_TAG_structure_type
7084 || die->tag == DW_TAG_class_type)
7085 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7086 }
7087 else
7088 {
d8151005
DJ
7089 /* The name is already allocated along with this objfile, so
7090 we don't need to duplicate it for the type. */
94af9270
KS
7091 TYPE_TAG_NAME (type) = (char *) name;
7092 if (die->tag == DW_TAG_class_type)
7093 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7094 }
c906108c
SS
7095 }
7096
7097 if (die->tag == DW_TAG_structure_type)
7098 {
7099 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7100 }
7101 else if (die->tag == DW_TAG_union_type)
7102 {
7103 TYPE_CODE (type) = TYPE_CODE_UNION;
7104 }
7105 else
7106 {
c906108c
SS
7107 TYPE_CODE (type) = TYPE_CODE_CLASS;
7108 }
7109
0cc2414c
TT
7110 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7111 TYPE_DECLARED_CLASS (type) = 1;
7112
e142c38c 7113 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7114 if (attr)
7115 {
7116 TYPE_LENGTH (type) = DW_UNSND (attr);
7117 }
7118 else
7119 {
7120 TYPE_LENGTH (type) = 0;
7121 }
7122
876cecd0 7123 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7124 if (die_is_declaration (die, cu))
876cecd0 7125 TYPE_STUB (type) = 1;
a6c727b2
DJ
7126 else if (attr == NULL && die->child == NULL
7127 && producer_is_realview (cu->producer))
7128 /* RealView does not output the required DW_AT_declaration
7129 on incomplete types. */
7130 TYPE_STUB (type) = 1;
dc718098 7131
c906108c
SS
7132 /* We need to add the type field to the die immediately so we don't
7133 infinitely recurse when dealing with pointers to the structure
0963b4bd 7134 type within the structure itself. */
1c379e20 7135 set_die_type (die, type, cu);
c906108c 7136
7e314c57
JK
7137 /* set_die_type should be already done. */
7138 set_descriptive_type (type, die, cu);
7139
c767944b
DJ
7140 return type;
7141}
7142
7143/* Finish creating a structure or union type, including filling in
7144 its members and creating a symbol for it. */
7145
7146static void
7147process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7148{
7149 struct objfile *objfile = cu->objfile;
7150 struct die_info *child_die = die->child;
7151 struct type *type;
7152
7153 type = get_die_type (die, cu);
7154 if (type == NULL)
7155 type = read_structure_type (die, cu);
7156
e142c38c 7157 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7158 {
7159 struct field_info fi;
7160 struct die_info *child_die;
34eaf542 7161 VEC (symbolp) *template_args = NULL;
c767944b 7162 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7163
7164 memset (&fi, 0, sizeof (struct field_info));
7165
639d11d3 7166 child_die = die->child;
c906108c
SS
7167
7168 while (child_die && child_die->tag)
7169 {
a9a9bd0f
DC
7170 if (child_die->tag == DW_TAG_member
7171 || child_die->tag == DW_TAG_variable)
c906108c 7172 {
a9a9bd0f
DC
7173 /* NOTE: carlton/2002-11-05: A C++ static data member
7174 should be a DW_TAG_member that is a declaration, but
7175 all versions of G++ as of this writing (so through at
7176 least 3.2.1) incorrectly generate DW_TAG_variable
7177 tags for them instead. */
e7c27a73 7178 dwarf2_add_field (&fi, child_die, cu);
c906108c 7179 }
8713b1b1 7180 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7181 {
0963b4bd 7182 /* C++ member function. */
e7c27a73 7183 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7184 }
7185 else if (child_die->tag == DW_TAG_inheritance)
7186 {
7187 /* C++ base class field. */
e7c27a73 7188 dwarf2_add_field (&fi, child_die, cu);
c906108c 7189 }
98751a41
JK
7190 else if (child_die->tag == DW_TAG_typedef)
7191 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7192 else if (child_die->tag == DW_TAG_template_type_param
7193 || child_die->tag == DW_TAG_template_value_param)
7194 {
7195 struct symbol *arg = new_symbol (child_die, NULL, cu);
7196
f1078f66
DJ
7197 if (arg != NULL)
7198 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7199 }
7200
c906108c
SS
7201 child_die = sibling_die (child_die);
7202 }
7203
34eaf542
TT
7204 /* Attach template arguments to type. */
7205 if (! VEC_empty (symbolp, template_args))
7206 {
7207 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7208 TYPE_N_TEMPLATE_ARGUMENTS (type)
7209 = VEC_length (symbolp, template_args);
7210 TYPE_TEMPLATE_ARGUMENTS (type)
7211 = obstack_alloc (&objfile->objfile_obstack,
7212 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7213 * sizeof (struct symbol *)));
7214 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7215 VEC_address (symbolp, template_args),
7216 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7217 * sizeof (struct symbol *)));
7218 VEC_free (symbolp, template_args);
7219 }
7220
c906108c
SS
7221 /* Attach fields and member functions to the type. */
7222 if (fi.nfields)
e7c27a73 7223 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7224 if (fi.nfnfields)
7225 {
e7c27a73 7226 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7227
c5aa993b 7228 /* Get the type which refers to the base class (possibly this
c906108c 7229 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7230 class from the DW_AT_containing_type attribute. This use of
7231 DW_AT_containing_type is a GNU extension. */
c906108c 7232
e142c38c 7233 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7234 {
e7c27a73 7235 struct type *t = die_containing_type (die, cu);
c906108c
SS
7236
7237 TYPE_VPTR_BASETYPE (type) = t;
7238 if (type == t)
7239 {
c906108c
SS
7240 int i;
7241
7242 /* Our own class provides vtbl ptr. */
7243 for (i = TYPE_NFIELDS (t) - 1;
7244 i >= TYPE_N_BASECLASSES (t);
7245 --i)
7246 {
7247 char *fieldname = TYPE_FIELD_NAME (t, i);
7248
1168df01 7249 if (is_vtable_name (fieldname, cu))
c906108c
SS
7250 {
7251 TYPE_VPTR_FIELDNO (type) = i;
7252 break;
7253 }
7254 }
7255
7256 /* Complain if virtual function table field not found. */
7257 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7258 complaint (&symfile_complaints,
3e43a32a
MS
7259 _("virtual function table pointer "
7260 "not found when defining class '%s'"),
4d3c2250
KB
7261 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7262 "");
c906108c
SS
7263 }
7264 else
7265 {
7266 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7267 }
7268 }
f6235d4c
EZ
7269 else if (cu->producer
7270 && strncmp (cu->producer,
7271 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7272 {
7273 /* The IBM XLC compiler does not provide direct indication
7274 of the containing type, but the vtable pointer is
7275 always named __vfp. */
7276
7277 int i;
7278
7279 for (i = TYPE_NFIELDS (type) - 1;
7280 i >= TYPE_N_BASECLASSES (type);
7281 --i)
7282 {
7283 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7284 {
7285 TYPE_VPTR_FIELDNO (type) = i;
7286 TYPE_VPTR_BASETYPE (type) = type;
7287 break;
7288 }
7289 }
7290 }
c906108c 7291 }
98751a41
JK
7292
7293 /* Copy fi.typedef_field_list linked list elements content into the
7294 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7295 if (fi.typedef_field_list)
7296 {
7297 int i = fi.typedef_field_list_count;
7298
a0d7a4ff 7299 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7300 TYPE_TYPEDEF_FIELD_ARRAY (type)
7301 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7302 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7303
7304 /* Reverse the list order to keep the debug info elements order. */
7305 while (--i >= 0)
7306 {
7307 struct typedef_field *dest, *src;
6e70227d 7308
98751a41
JK
7309 dest = &TYPE_TYPEDEF_FIELD (type, i);
7310 src = &fi.typedef_field_list->field;
7311 fi.typedef_field_list = fi.typedef_field_list->next;
7312 *dest = *src;
7313 }
7314 }
c767944b
DJ
7315
7316 do_cleanups (back_to);
c906108c 7317 }
63d06c5c 7318
0b92b5bb
TT
7319 quirk_gcc_member_function_pointer (type, cu->objfile);
7320
90aeadfc
DC
7321 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7322 snapshots) has been known to create a die giving a declaration
7323 for a class that has, as a child, a die giving a definition for a
7324 nested class. So we have to process our children even if the
7325 current die is a declaration. Normally, of course, a declaration
7326 won't have any children at all. */
134d01f1 7327
90aeadfc
DC
7328 while (child_die != NULL && child_die->tag)
7329 {
7330 if (child_die->tag == DW_TAG_member
7331 || child_die->tag == DW_TAG_variable
34eaf542
TT
7332 || child_die->tag == DW_TAG_inheritance
7333 || child_die->tag == DW_TAG_template_value_param
7334 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7335 {
90aeadfc 7336 /* Do nothing. */
134d01f1 7337 }
90aeadfc
DC
7338 else
7339 process_die (child_die, cu);
134d01f1 7340
90aeadfc 7341 child_die = sibling_die (child_die);
134d01f1
DJ
7342 }
7343
fa4028e9
JB
7344 /* Do not consider external references. According to the DWARF standard,
7345 these DIEs are identified by the fact that they have no byte_size
7346 attribute, and a declaration attribute. */
7347 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7348 || !die_is_declaration (die, cu))
c767944b 7349 new_symbol (die, type, cu);
134d01f1
DJ
7350}
7351
7352/* Given a DW_AT_enumeration_type die, set its type. We do not
7353 complete the type's fields yet, or create any symbols. */
c906108c 7354
f792889a 7355static struct type *
134d01f1 7356read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7357{
e7c27a73 7358 struct objfile *objfile = cu->objfile;
c906108c 7359 struct type *type;
c906108c 7360 struct attribute *attr;
0114d602 7361 const char *name;
134d01f1 7362
348e048f
DE
7363 /* If the definition of this type lives in .debug_types, read that type.
7364 Don't follow DW_AT_specification though, that will take us back up
7365 the chain and we want to go down. */
7366 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7367 if (attr)
7368 {
7369 struct dwarf2_cu *type_cu = cu;
7370 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7371
348e048f 7372 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7373
7374 /* TYPE_CU may not be the same as CU.
7375 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7376 return set_die_type (die, type, cu);
7377 }
7378
c906108c
SS
7379 type = alloc_type (objfile);
7380
7381 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7382 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7383 if (name != NULL)
0114d602 7384 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7385
e142c38c 7386 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7387 if (attr)
7388 {
7389 TYPE_LENGTH (type) = DW_UNSND (attr);
7390 }
7391 else
7392 {
7393 TYPE_LENGTH (type) = 0;
7394 }
7395
137033e9
JB
7396 /* The enumeration DIE can be incomplete. In Ada, any type can be
7397 declared as private in the package spec, and then defined only
7398 inside the package body. Such types are known as Taft Amendment
7399 Types. When another package uses such a type, an incomplete DIE
7400 may be generated by the compiler. */
02eb380e 7401 if (die_is_declaration (die, cu))
876cecd0 7402 TYPE_STUB (type) = 1;
02eb380e 7403
f792889a 7404 return set_die_type (die, type, cu);
134d01f1
DJ
7405}
7406
7407/* Given a pointer to a die which begins an enumeration, process all
7408 the dies that define the members of the enumeration, and create the
7409 symbol for the enumeration type.
7410
7411 NOTE: We reverse the order of the element list. */
7412
7413static void
7414process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7415{
f792889a 7416 struct type *this_type;
134d01f1 7417
f792889a
DJ
7418 this_type = get_die_type (die, cu);
7419 if (this_type == NULL)
7420 this_type = read_enumeration_type (die, cu);
9dc481d3 7421
639d11d3 7422 if (die->child != NULL)
c906108c 7423 {
9dc481d3
DE
7424 struct die_info *child_die;
7425 struct symbol *sym;
7426 struct field *fields = NULL;
7427 int num_fields = 0;
7428 int unsigned_enum = 1;
7429 char *name;
7430
639d11d3 7431 child_die = die->child;
c906108c
SS
7432 while (child_die && child_die->tag)
7433 {
7434 if (child_die->tag != DW_TAG_enumerator)
7435 {
e7c27a73 7436 process_die (child_die, cu);
c906108c
SS
7437 }
7438 else
7439 {
39cbfefa
DJ
7440 name = dwarf2_name (child_die, cu);
7441 if (name)
c906108c 7442 {
f792889a 7443 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7444 if (SYMBOL_VALUE (sym) < 0)
7445 unsigned_enum = 0;
7446
7447 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7448 {
7449 fields = (struct field *)
7450 xrealloc (fields,
7451 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7452 * sizeof (struct field));
c906108c
SS
7453 }
7454
3567439c 7455 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7456 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7457 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7458 FIELD_BITSIZE (fields[num_fields]) = 0;
7459
7460 num_fields++;
7461 }
7462 }
7463
7464 child_die = sibling_die (child_die);
7465 }
7466
7467 if (num_fields)
7468 {
f792889a
DJ
7469 TYPE_NFIELDS (this_type) = num_fields;
7470 TYPE_FIELDS (this_type) = (struct field *)
7471 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7472 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7473 sizeof (struct field) * num_fields);
b8c9b27d 7474 xfree (fields);
c906108c
SS
7475 }
7476 if (unsigned_enum)
876cecd0 7477 TYPE_UNSIGNED (this_type) = 1;
c906108c 7478 }
134d01f1 7479
f792889a 7480 new_symbol (die, this_type, cu);
c906108c
SS
7481}
7482
7483/* Extract all information from a DW_TAG_array_type DIE and put it in
7484 the DIE's type field. For now, this only handles one dimensional
7485 arrays. */
7486
f792889a 7487static struct type *
e7c27a73 7488read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7489{
e7c27a73 7490 struct objfile *objfile = cu->objfile;
c906108c 7491 struct die_info *child_die;
7e314c57 7492 struct type *type;
c906108c
SS
7493 struct type *element_type, *range_type, *index_type;
7494 struct type **range_types = NULL;
7495 struct attribute *attr;
7496 int ndim = 0;
7497 struct cleanup *back_to;
39cbfefa 7498 char *name;
c906108c 7499
e7c27a73 7500 element_type = die_type (die, cu);
c906108c 7501
7e314c57
JK
7502 /* The die_type call above may have already set the type for this DIE. */
7503 type = get_die_type (die, cu);
7504 if (type)
7505 return type;
7506
c906108c
SS
7507 /* Irix 6.2 native cc creates array types without children for
7508 arrays with unspecified length. */
639d11d3 7509 if (die->child == NULL)
c906108c 7510 {
46bf5051 7511 index_type = objfile_type (objfile)->builtin_int;
c906108c 7512 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7513 type = create_array_type (NULL, element_type, range_type);
7514 return set_die_type (die, type, cu);
c906108c
SS
7515 }
7516
7517 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7518 child_die = die->child;
c906108c
SS
7519 while (child_die && child_die->tag)
7520 {
7521 if (child_die->tag == DW_TAG_subrange_type)
7522 {
f792889a 7523 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7524
f792889a 7525 if (child_type != NULL)
a02abb62 7526 {
0963b4bd
MS
7527 /* The range type was succesfully read. Save it for the
7528 array type creation. */
a02abb62
JB
7529 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7530 {
7531 range_types = (struct type **)
7532 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7533 * sizeof (struct type *));
7534 if (ndim == 0)
7535 make_cleanup (free_current_contents, &range_types);
7536 }
f792889a 7537 range_types[ndim++] = child_type;
a02abb62 7538 }
c906108c
SS
7539 }
7540 child_die = sibling_die (child_die);
7541 }
7542
7543 /* Dwarf2 dimensions are output from left to right, create the
7544 necessary array types in backwards order. */
7ca2d3a3 7545
c906108c 7546 type = element_type;
7ca2d3a3
DL
7547
7548 if (read_array_order (die, cu) == DW_ORD_col_major)
7549 {
7550 int i = 0;
9a619af0 7551
7ca2d3a3
DL
7552 while (i < ndim)
7553 type = create_array_type (NULL, type, range_types[i++]);
7554 }
7555 else
7556 {
7557 while (ndim-- > 0)
7558 type = create_array_type (NULL, type, range_types[ndim]);
7559 }
c906108c 7560
f5f8a009
EZ
7561 /* Understand Dwarf2 support for vector types (like they occur on
7562 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7563 array type. This is not part of the Dwarf2/3 standard yet, but a
7564 custom vendor extension. The main difference between a regular
7565 array and the vector variant is that vectors are passed by value
7566 to functions. */
e142c38c 7567 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7568 if (attr)
ea37ba09 7569 make_vector_type (type);
f5f8a009 7570
dbc98a8b
KW
7571 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7572 implementation may choose to implement triple vectors using this
7573 attribute. */
7574 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7575 if (attr)
7576 {
7577 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7578 TYPE_LENGTH (type) = DW_UNSND (attr);
7579 else
3e43a32a
MS
7580 complaint (&symfile_complaints,
7581 _("DW_AT_byte_size for array type smaller "
7582 "than the total size of elements"));
dbc98a8b
KW
7583 }
7584
39cbfefa
DJ
7585 name = dwarf2_name (die, cu);
7586 if (name)
7587 TYPE_NAME (type) = name;
6e70227d 7588
0963b4bd 7589 /* Install the type in the die. */
7e314c57
JK
7590 set_die_type (die, type, cu);
7591
7592 /* set_die_type should be already done. */
b4ba55a1
JB
7593 set_descriptive_type (type, die, cu);
7594
c906108c
SS
7595 do_cleanups (back_to);
7596
7e314c57 7597 return type;
c906108c
SS
7598}
7599
7ca2d3a3 7600static enum dwarf_array_dim_ordering
6e70227d 7601read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7602{
7603 struct attribute *attr;
7604
7605 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7606
7607 if (attr) return DW_SND (attr);
7608
0963b4bd
MS
7609 /* GNU F77 is a special case, as at 08/2004 array type info is the
7610 opposite order to the dwarf2 specification, but data is still
7611 laid out as per normal fortran.
7ca2d3a3 7612
0963b4bd
MS
7613 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7614 version checking. */
7ca2d3a3 7615
905e0470
PM
7616 if (cu->language == language_fortran
7617 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7618 {
7619 return DW_ORD_row_major;
7620 }
7621
6e70227d 7622 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7623 {
7624 case array_column_major:
7625 return DW_ORD_col_major;
7626 case array_row_major:
7627 default:
7628 return DW_ORD_row_major;
7629 };
7630}
7631
72019c9c 7632/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 7633 the DIE's type field. */
72019c9c 7634
f792889a 7635static struct type *
72019c9c
GM
7636read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7637{
7e314c57
JK
7638 struct type *domain_type, *set_type;
7639 struct attribute *attr;
f792889a 7640
7e314c57
JK
7641 domain_type = die_type (die, cu);
7642
7643 /* The die_type call above may have already set the type for this DIE. */
7644 set_type = get_die_type (die, cu);
7645 if (set_type)
7646 return set_type;
7647
7648 set_type = create_set_type (NULL, domain_type);
7649
7650 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7651 if (attr)
7652 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7653
f792889a 7654 return set_die_type (die, set_type, cu);
72019c9c 7655}
7ca2d3a3 7656
c906108c
SS
7657/* First cut: install each common block member as a global variable. */
7658
7659static void
e7c27a73 7660read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7661{
7662 struct die_info *child_die;
7663 struct attribute *attr;
7664 struct symbol *sym;
7665 CORE_ADDR base = (CORE_ADDR) 0;
7666
e142c38c 7667 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7668 if (attr)
7669 {
0963b4bd 7670 /* Support the .debug_loc offsets. */
8e19ed76
PS
7671 if (attr_form_is_block (attr))
7672 {
e7c27a73 7673 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7674 }
3690dd37 7675 else if (attr_form_is_section_offset (attr))
8e19ed76 7676 {
4d3c2250 7677 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7678 }
7679 else
7680 {
4d3c2250
KB
7681 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7682 "common block member");
8e19ed76 7683 }
c906108c 7684 }
639d11d3 7685 if (die->child != NULL)
c906108c 7686 {
639d11d3 7687 child_die = die->child;
c906108c
SS
7688 while (child_die && child_die->tag)
7689 {
74ac6d43
TT
7690 LONGEST offset;
7691
e7c27a73 7692 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
7693 if (sym != NULL
7694 && handle_data_member_location (child_die, cu, &offset))
c906108c 7695 {
74ac6d43 7696 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
7697 add_symbol_to_list (sym, &global_symbols);
7698 }
7699 child_die = sibling_die (child_die);
7700 }
7701 }
7702}
7703
0114d602 7704/* Create a type for a C++ namespace. */
d9fa45fe 7705
0114d602
DJ
7706static struct type *
7707read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7708{
e7c27a73 7709 struct objfile *objfile = cu->objfile;
0114d602 7710 const char *previous_prefix, *name;
9219021c 7711 int is_anonymous;
0114d602
DJ
7712 struct type *type;
7713
7714 /* For extensions, reuse the type of the original namespace. */
7715 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7716 {
7717 struct die_info *ext_die;
7718 struct dwarf2_cu *ext_cu = cu;
9a619af0 7719
0114d602
DJ
7720 ext_die = dwarf2_extension (die, &ext_cu);
7721 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7722
7723 /* EXT_CU may not be the same as CU.
7724 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7725 return set_die_type (die, type, cu);
7726 }
9219021c 7727
e142c38c 7728 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7729
7730 /* Now build the name of the current namespace. */
7731
0114d602
DJ
7732 previous_prefix = determine_prefix (die, cu);
7733 if (previous_prefix[0] != '\0')
7734 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7735 previous_prefix, name, 0, cu);
0114d602
DJ
7736
7737 /* Create the type. */
7738 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7739 objfile);
7740 TYPE_NAME (type) = (char *) name;
7741 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7742
60531b24 7743 return set_die_type (die, type, cu);
0114d602
DJ
7744}
7745
7746/* Read a C++ namespace. */
7747
7748static void
7749read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7750{
7751 struct objfile *objfile = cu->objfile;
0114d602 7752 int is_anonymous;
9219021c 7753
5c4e30ca
DC
7754 /* Add a symbol associated to this if we haven't seen the namespace
7755 before. Also, add a using directive if it's an anonymous
7756 namespace. */
9219021c 7757
f2f0e013 7758 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7759 {
7760 struct type *type;
7761
0114d602 7762 type = read_type_die (die, cu);
e7c27a73 7763 new_symbol (die, type, cu);
5c4e30ca 7764
e8e80198 7765 namespace_name (die, &is_anonymous, cu);
5c4e30ca 7766 if (is_anonymous)
0114d602
DJ
7767 {
7768 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7769
c0cc3a76 7770 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7771 NULL, &objfile->objfile_obstack);
0114d602 7772 }
5c4e30ca 7773 }
9219021c 7774
639d11d3 7775 if (die->child != NULL)
d9fa45fe 7776 {
639d11d3 7777 struct die_info *child_die = die->child;
6e70227d 7778
d9fa45fe
DC
7779 while (child_die && child_die->tag)
7780 {
e7c27a73 7781 process_die (child_die, cu);
d9fa45fe
DC
7782 child_die = sibling_die (child_die);
7783 }
7784 }
38d518c9
EZ
7785}
7786
f55ee35c
JK
7787/* Read a Fortran module as type. This DIE can be only a declaration used for
7788 imported module. Still we need that type as local Fortran "use ... only"
7789 declaration imports depend on the created type in determine_prefix. */
7790
7791static struct type *
7792read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7793{
7794 struct objfile *objfile = cu->objfile;
7795 char *module_name;
7796 struct type *type;
7797
7798 module_name = dwarf2_name (die, cu);
7799 if (!module_name)
3e43a32a
MS
7800 complaint (&symfile_complaints,
7801 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
7802 die->offset);
7803 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7804
7805 /* determine_prefix uses TYPE_TAG_NAME. */
7806 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7807
7808 return set_die_type (die, type, cu);
7809}
7810
5d7cb8df
JK
7811/* Read a Fortran module. */
7812
7813static void
7814read_module (struct die_info *die, struct dwarf2_cu *cu)
7815{
7816 struct die_info *child_die = die->child;
7817
5d7cb8df
JK
7818 while (child_die && child_die->tag)
7819 {
7820 process_die (child_die, cu);
7821 child_die = sibling_die (child_die);
7822 }
7823}
7824
38d518c9
EZ
7825/* Return the name of the namespace represented by DIE. Set
7826 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7827 namespace. */
7828
7829static const char *
e142c38c 7830namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7831{
7832 struct die_info *current_die;
7833 const char *name = NULL;
7834
7835 /* Loop through the extensions until we find a name. */
7836
7837 for (current_die = die;
7838 current_die != NULL;
f2f0e013 7839 current_die = dwarf2_extension (die, &cu))
38d518c9 7840 {
e142c38c 7841 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7842 if (name != NULL)
7843 break;
7844 }
7845
7846 /* Is it an anonymous namespace? */
7847
7848 *is_anonymous = (name == NULL);
7849 if (*is_anonymous)
7850 name = "(anonymous namespace)";
7851
7852 return name;
d9fa45fe
DC
7853}
7854
c906108c
SS
7855/* Extract all information from a DW_TAG_pointer_type DIE and add to
7856 the user defined type vector. */
7857
f792889a 7858static struct type *
e7c27a73 7859read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7860{
5e2b427d 7861 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7862 struct comp_unit_head *cu_header = &cu->header;
c906108c 7863 struct type *type;
8b2dbe47
KB
7864 struct attribute *attr_byte_size;
7865 struct attribute *attr_address_class;
7866 int byte_size, addr_class;
7e314c57
JK
7867 struct type *target_type;
7868
7869 target_type = die_type (die, cu);
c906108c 7870
7e314c57
JK
7871 /* The die_type call above may have already set the type for this DIE. */
7872 type = get_die_type (die, cu);
7873 if (type)
7874 return type;
7875
7876 type = lookup_pointer_type (target_type);
8b2dbe47 7877
e142c38c 7878 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7879 if (attr_byte_size)
7880 byte_size = DW_UNSND (attr_byte_size);
c906108c 7881 else
8b2dbe47
KB
7882 byte_size = cu_header->addr_size;
7883
e142c38c 7884 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7885 if (attr_address_class)
7886 addr_class = DW_UNSND (attr_address_class);
7887 else
7888 addr_class = DW_ADDR_none;
7889
7890 /* If the pointer size or address class is different than the
7891 default, create a type variant marked as such and set the
7892 length accordingly. */
7893 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7894 {
5e2b427d 7895 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7896 {
7897 int type_flags;
7898
849957d9 7899 type_flags = gdbarch_address_class_type_flags
5e2b427d 7900 (gdbarch, byte_size, addr_class);
876cecd0
TT
7901 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7902 == 0);
8b2dbe47
KB
7903 type = make_type_with_address_space (type, type_flags);
7904 }
7905 else if (TYPE_LENGTH (type) != byte_size)
7906 {
3e43a32a
MS
7907 complaint (&symfile_complaints,
7908 _("invalid pointer size %d"), byte_size);
8b2dbe47 7909 }
6e70227d 7910 else
9a619af0
MS
7911 {
7912 /* Should we also complain about unhandled address classes? */
7913 }
c906108c 7914 }
8b2dbe47
KB
7915
7916 TYPE_LENGTH (type) = byte_size;
f792889a 7917 return set_die_type (die, type, cu);
c906108c
SS
7918}
7919
7920/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7921 the user defined type vector. */
7922
f792889a 7923static struct type *
e7c27a73 7924read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7925{
7926 struct type *type;
7927 struct type *to_type;
7928 struct type *domain;
7929
e7c27a73
DJ
7930 to_type = die_type (die, cu);
7931 domain = die_containing_type (die, cu);
0d5de010 7932
7e314c57
JK
7933 /* The calls above may have already set the type for this DIE. */
7934 type = get_die_type (die, cu);
7935 if (type)
7936 return type;
7937
0d5de010
DJ
7938 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7939 type = lookup_methodptr_type (to_type);
7940 else
7941 type = lookup_memberptr_type (to_type, domain);
c906108c 7942
f792889a 7943 return set_die_type (die, type, cu);
c906108c
SS
7944}
7945
7946/* Extract all information from a DW_TAG_reference_type DIE and add to
7947 the user defined type vector. */
7948
f792889a 7949static struct type *
e7c27a73 7950read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7951{
e7c27a73 7952 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7953 struct type *type, *target_type;
c906108c
SS
7954 struct attribute *attr;
7955
7e314c57
JK
7956 target_type = die_type (die, cu);
7957
7958 /* The die_type call above may have already set the type for this DIE. */
7959 type = get_die_type (die, cu);
7960 if (type)
7961 return type;
7962
7963 type = lookup_reference_type (target_type);
e142c38c 7964 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7965 if (attr)
7966 {
7967 TYPE_LENGTH (type) = DW_UNSND (attr);
7968 }
7969 else
7970 {
107d2387 7971 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7972 }
f792889a 7973 return set_die_type (die, type, cu);
c906108c
SS
7974}
7975
f792889a 7976static struct type *
e7c27a73 7977read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7978{
f792889a 7979 struct type *base_type, *cv_type;
c906108c 7980
e7c27a73 7981 base_type = die_type (die, cu);
7e314c57
JK
7982
7983 /* The die_type call above may have already set the type for this DIE. */
7984 cv_type = get_die_type (die, cu);
7985 if (cv_type)
7986 return cv_type;
7987
2f608a3a
KW
7988 /* In case the const qualifier is applied to an array type, the element type
7989 is so qualified, not the array type (section 6.7.3 of C99). */
7990 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7991 {
7992 struct type *el_type, *inner_array;
7993
7994 base_type = copy_type (base_type);
7995 inner_array = base_type;
7996
7997 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7998 {
7999 TYPE_TARGET_TYPE (inner_array) =
8000 copy_type (TYPE_TARGET_TYPE (inner_array));
8001 inner_array = TYPE_TARGET_TYPE (inner_array);
8002 }
8003
8004 el_type = TYPE_TARGET_TYPE (inner_array);
8005 TYPE_TARGET_TYPE (inner_array) =
8006 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8007
8008 return set_die_type (die, base_type, cu);
8009 }
8010
f792889a
DJ
8011 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8012 return set_die_type (die, cv_type, cu);
c906108c
SS
8013}
8014
f792889a 8015static struct type *
e7c27a73 8016read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8017{
f792889a 8018 struct type *base_type, *cv_type;
c906108c 8019
e7c27a73 8020 base_type = die_type (die, cu);
7e314c57
JK
8021
8022 /* The die_type call above may have already set the type for this DIE. */
8023 cv_type = get_die_type (die, cu);
8024 if (cv_type)
8025 return cv_type;
8026
f792889a
DJ
8027 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8028 return set_die_type (die, cv_type, cu);
c906108c
SS
8029}
8030
8031/* Extract all information from a DW_TAG_string_type DIE and add to
8032 the user defined type vector. It isn't really a user defined type,
8033 but it behaves like one, with other DIE's using an AT_user_def_type
8034 attribute to reference it. */
8035
f792889a 8036static struct type *
e7c27a73 8037read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8038{
e7c27a73 8039 struct objfile *objfile = cu->objfile;
3b7538c0 8040 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8041 struct type *type, *range_type, *index_type, *char_type;
8042 struct attribute *attr;
8043 unsigned int length;
8044
e142c38c 8045 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8046 if (attr)
8047 {
8048 length = DW_UNSND (attr);
8049 }
8050 else
8051 {
0963b4bd 8052 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8053 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8054 if (attr)
8055 {
8056 length = DW_UNSND (attr);
8057 }
8058 else
8059 {
8060 length = 1;
8061 }
c906108c 8062 }
6ccb9162 8063
46bf5051 8064 index_type = objfile_type (objfile)->builtin_int;
c906108c 8065 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8066 char_type = language_string_char_type (cu->language_defn, gdbarch);
8067 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8068
f792889a 8069 return set_die_type (die, type, cu);
c906108c
SS
8070}
8071
8072/* Handle DIES due to C code like:
8073
8074 struct foo
c5aa993b
JM
8075 {
8076 int (*funcp)(int a, long l);
8077 int b;
8078 };
c906108c 8079
0963b4bd 8080 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8081
f792889a 8082static struct type *
e7c27a73 8083read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8084{
0963b4bd
MS
8085 struct type *type; /* Type that this function returns. */
8086 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8087 struct attribute *attr;
8088
e7c27a73 8089 type = die_type (die, cu);
7e314c57
JK
8090
8091 /* The die_type call above may have already set the type for this DIE. */
8092 ftype = get_die_type (die, cu);
8093 if (ftype)
8094 return ftype;
8095
0c8b41f1 8096 ftype = lookup_function_type (type);
c906108c 8097
5b8101ae 8098 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8099 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8100 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8101 || cu->language == language_cplus
5b8101ae
PM
8102 || cu->language == language_java
8103 || cu->language == language_pascal)
876cecd0 8104 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8105 else if (producer_is_realview (cu->producer))
8106 /* RealView does not emit DW_AT_prototyped. We can not
8107 distinguish prototyped and unprototyped functions; default to
8108 prototyped, since that is more common in modern code (and
8109 RealView warns about unprototyped functions). */
8110 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8111
c055b101
CV
8112 /* Store the calling convention in the type if it's available in
8113 the subroutine die. Otherwise set the calling convention to
8114 the default value DW_CC_normal. */
8115 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8116 if (attr)
8117 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8118 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8119 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8120 else
8121 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8122
8123 /* We need to add the subroutine type to the die immediately so
8124 we don't infinitely recurse when dealing with parameters
0963b4bd 8125 declared as the same subroutine type. */
76c10ea2 8126 set_die_type (die, ftype, cu);
6e70227d 8127
639d11d3 8128 if (die->child != NULL)
c906108c 8129 {
8072405b 8130 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 8131 struct die_info *child_die;
8072405b 8132 int nparams, iparams;
c906108c
SS
8133
8134 /* Count the number of parameters.
8135 FIXME: GDB currently ignores vararg functions, but knows about
8136 vararg member functions. */
8072405b 8137 nparams = 0;
639d11d3 8138 child_die = die->child;
c906108c
SS
8139 while (child_die && child_die->tag)
8140 {
8141 if (child_die->tag == DW_TAG_formal_parameter)
8142 nparams++;
8143 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8144 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8145 child_die = sibling_die (child_die);
8146 }
8147
8148 /* Allocate storage for parameters and fill them in. */
8149 TYPE_NFIELDS (ftype) = nparams;
8150 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8151 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8152
8072405b
JK
8153 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8154 even if we error out during the parameters reading below. */
8155 for (iparams = 0; iparams < nparams; iparams++)
8156 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8157
8158 iparams = 0;
639d11d3 8159 child_die = die->child;
c906108c
SS
8160 while (child_die && child_die->tag)
8161 {
8162 if (child_die->tag == DW_TAG_formal_parameter)
8163 {
3ce3b1ba
PA
8164 struct type *arg_type;
8165
8166 /* DWARF version 2 has no clean way to discern C++
8167 static and non-static member functions. G++ helps
8168 GDB by marking the first parameter for non-static
8169 member functions (which is the this pointer) as
8170 artificial. We pass this information to
8171 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8172
8173 DWARF version 3 added DW_AT_object_pointer, which GCC
8174 4.5 does not yet generate. */
e142c38c 8175 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8176 if (attr)
8177 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8178 else
418835cc
KS
8179 {
8180 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8181
8182 /* GCC/43521: In java, the formal parameter
8183 "this" is sometimes not marked with DW_AT_artificial. */
8184 if (cu->language == language_java)
8185 {
8186 const char *name = dwarf2_name (child_die, cu);
9a619af0 8187
418835cc
KS
8188 if (name && !strcmp (name, "this"))
8189 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8190 }
8191 }
3ce3b1ba
PA
8192 arg_type = die_type (child_die, cu);
8193
8194 /* RealView does not mark THIS as const, which the testsuite
8195 expects. GCC marks THIS as const in method definitions,
8196 but not in the class specifications (GCC PR 43053). */
8197 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8198 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8199 {
8200 int is_this = 0;
8201 struct dwarf2_cu *arg_cu = cu;
8202 const char *name = dwarf2_name (child_die, cu);
8203
8204 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8205 if (attr)
8206 {
8207 /* If the compiler emits this, use it. */
8208 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8209 is_this = 1;
8210 }
8211 else if (name && strcmp (name, "this") == 0)
8212 /* Function definitions will have the argument names. */
8213 is_this = 1;
8214 else if (name == NULL && iparams == 0)
8215 /* Declarations may not have the names, so like
8216 elsewhere in GDB, assume an artificial first
8217 argument is "this". */
8218 is_this = 1;
8219
8220 if (is_this)
8221 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8222 arg_type, 0);
8223 }
8224
8225 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8226 iparams++;
8227 }
8228 child_die = sibling_die (child_die);
8229 }
8230 }
8231
76c10ea2 8232 return ftype;
c906108c
SS
8233}
8234
f792889a 8235static struct type *
e7c27a73 8236read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8237{
e7c27a73 8238 struct objfile *objfile = cu->objfile;
0114d602 8239 const char *name = NULL;
f792889a 8240 struct type *this_type;
c906108c 8241
94af9270 8242 name = dwarf2_full_name (NULL, die, cu);
f792889a 8243 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8244 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8245 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8246 set_die_type (die, this_type, cu);
8247 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8248 return this_type;
c906108c
SS
8249}
8250
8251/* Find a representation of a given base type and install
8252 it in the TYPE field of the die. */
8253
f792889a 8254static struct type *
e7c27a73 8255read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8256{
e7c27a73 8257 struct objfile *objfile = cu->objfile;
c906108c
SS
8258 struct type *type;
8259 struct attribute *attr;
8260 int encoding = 0, size = 0;
39cbfefa 8261 char *name;
6ccb9162
UW
8262 enum type_code code = TYPE_CODE_INT;
8263 int type_flags = 0;
8264 struct type *target_type = NULL;
c906108c 8265
e142c38c 8266 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8267 if (attr)
8268 {
8269 encoding = DW_UNSND (attr);
8270 }
e142c38c 8271 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8272 if (attr)
8273 {
8274 size = DW_UNSND (attr);
8275 }
39cbfefa 8276 name = dwarf2_name (die, cu);
6ccb9162 8277 if (!name)
c906108c 8278 {
6ccb9162
UW
8279 complaint (&symfile_complaints,
8280 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8281 }
6ccb9162
UW
8282
8283 switch (encoding)
c906108c 8284 {
6ccb9162
UW
8285 case DW_ATE_address:
8286 /* Turn DW_ATE_address into a void * pointer. */
8287 code = TYPE_CODE_PTR;
8288 type_flags |= TYPE_FLAG_UNSIGNED;
8289 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8290 break;
8291 case DW_ATE_boolean:
8292 code = TYPE_CODE_BOOL;
8293 type_flags |= TYPE_FLAG_UNSIGNED;
8294 break;
8295 case DW_ATE_complex_float:
8296 code = TYPE_CODE_COMPLEX;
8297 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8298 break;
8299 case DW_ATE_decimal_float:
8300 code = TYPE_CODE_DECFLOAT;
8301 break;
8302 case DW_ATE_float:
8303 code = TYPE_CODE_FLT;
8304 break;
8305 case DW_ATE_signed:
8306 break;
8307 case DW_ATE_unsigned:
8308 type_flags |= TYPE_FLAG_UNSIGNED;
8309 break;
8310 case DW_ATE_signed_char:
6e70227d 8311 if (cu->language == language_ada || cu->language == language_m2
868a0084 8312 || cu->language == language_pascal)
6ccb9162
UW
8313 code = TYPE_CODE_CHAR;
8314 break;
8315 case DW_ATE_unsigned_char:
868a0084
PM
8316 if (cu->language == language_ada || cu->language == language_m2
8317 || cu->language == language_pascal)
6ccb9162
UW
8318 code = TYPE_CODE_CHAR;
8319 type_flags |= TYPE_FLAG_UNSIGNED;
8320 break;
75079b2b
TT
8321 case DW_ATE_UTF:
8322 /* We just treat this as an integer and then recognize the
8323 type by name elsewhere. */
8324 break;
8325
6ccb9162
UW
8326 default:
8327 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8328 dwarf_type_encoding_name (encoding));
8329 break;
c906108c 8330 }
6ccb9162 8331
0114d602
DJ
8332 type = init_type (code, size, type_flags, NULL, objfile);
8333 TYPE_NAME (type) = name;
6ccb9162
UW
8334 TYPE_TARGET_TYPE (type) = target_type;
8335
0114d602 8336 if (name && strcmp (name, "char") == 0)
876cecd0 8337 TYPE_NOSIGN (type) = 1;
0114d602 8338
f792889a 8339 return set_die_type (die, type, cu);
c906108c
SS
8340}
8341
a02abb62
JB
8342/* Read the given DW_AT_subrange DIE. */
8343
f792889a 8344static struct type *
a02abb62
JB
8345read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8346{
8347 struct type *base_type;
8348 struct type *range_type;
8349 struct attribute *attr;
43bbcdc2
PH
8350 LONGEST low = 0;
8351 LONGEST high = -1;
39cbfefa 8352 char *name;
43bbcdc2 8353 LONGEST negative_mask;
e77813c8 8354
a02abb62 8355 base_type = die_type (die, cu);
953ac07e
JK
8356 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8357 check_typedef (base_type);
a02abb62 8358
7e314c57
JK
8359 /* The die_type call above may have already set the type for this DIE. */
8360 range_type = get_die_type (die, cu);
8361 if (range_type)
8362 return range_type;
8363
e142c38c 8364 if (cu->language == language_fortran)
6e70227d 8365 {
a02abb62
JB
8366 /* FORTRAN implies a lower bound of 1, if not given. */
8367 low = 1;
8368 }
8369
dd5e6932
DJ
8370 /* FIXME: For variable sized arrays either of these could be
8371 a variable rather than a constant value. We'll allow it,
8372 but we don't know how to handle it. */
e142c38c 8373 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8374 if (attr)
8375 low = dwarf2_get_attr_constant_value (attr, 0);
8376
e142c38c 8377 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8378 if (attr)
6e70227d 8379 {
e77813c8 8380 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8381 {
8382 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8383 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8384 FIXME: GDB does not yet know how to handle dynamic
8385 arrays properly, treat them as arrays with unspecified
8386 length for now.
8387
8388 FIXME: jimb/2003-09-22: GDB does not really know
8389 how to handle arrays of unspecified length
8390 either; we just represent them as zero-length
8391 arrays. Choose an appropriate upper bound given
8392 the lower bound we've computed above. */
8393 high = low - 1;
8394 }
8395 else
8396 high = dwarf2_get_attr_constant_value (attr, 1);
8397 }
e77813c8
PM
8398 else
8399 {
8400 attr = dwarf2_attr (die, DW_AT_count, cu);
8401 if (attr)
8402 {
8403 int count = dwarf2_get_attr_constant_value (attr, 1);
8404 high = low + count - 1;
8405 }
c2ff108b
JK
8406 else
8407 {
8408 /* Unspecified array length. */
8409 high = low - 1;
8410 }
e77813c8
PM
8411 }
8412
8413 /* Dwarf-2 specifications explicitly allows to create subrange types
8414 without specifying a base type.
8415 In that case, the base type must be set to the type of
8416 the lower bound, upper bound or count, in that order, if any of these
8417 three attributes references an object that has a type.
8418 If no base type is found, the Dwarf-2 specifications say that
8419 a signed integer type of size equal to the size of an address should
8420 be used.
8421 For the following C code: `extern char gdb_int [];'
8422 GCC produces an empty range DIE.
8423 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 8424 high bound or count are not yet handled by this code. */
e77813c8
PM
8425 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8426 {
8427 struct objfile *objfile = cu->objfile;
8428 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8429 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8430 struct type *int_type = objfile_type (objfile)->builtin_int;
8431
8432 /* Test "int", "long int", and "long long int" objfile types,
8433 and select the first one having a size above or equal to the
8434 architecture address size. */
8435 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8436 base_type = int_type;
8437 else
8438 {
8439 int_type = objfile_type (objfile)->builtin_long;
8440 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8441 base_type = int_type;
8442 else
8443 {
8444 int_type = objfile_type (objfile)->builtin_long_long;
8445 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8446 base_type = int_type;
8447 }
8448 }
8449 }
a02abb62 8450
6e70227d 8451 negative_mask =
43bbcdc2
PH
8452 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8453 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8454 low |= negative_mask;
8455 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8456 high |= negative_mask;
8457
a02abb62
JB
8458 range_type = create_range_type (NULL, base_type, low, high);
8459
bbb0eef6
JK
8460 /* Mark arrays with dynamic length at least as an array of unspecified
8461 length. GDB could check the boundary but before it gets implemented at
8462 least allow accessing the array elements. */
8463 if (attr && attr->form == DW_FORM_block1)
8464 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8465
c2ff108b
JK
8466 /* Ada expects an empty array on no boundary attributes. */
8467 if (attr == NULL && cu->language != language_ada)
8468 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8469
39cbfefa
DJ
8470 name = dwarf2_name (die, cu);
8471 if (name)
8472 TYPE_NAME (range_type) = name;
6e70227d 8473
e142c38c 8474 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8475 if (attr)
8476 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8477
7e314c57
JK
8478 set_die_type (die, range_type, cu);
8479
8480 /* set_die_type should be already done. */
b4ba55a1
JB
8481 set_descriptive_type (range_type, die, cu);
8482
7e314c57 8483 return range_type;
a02abb62 8484}
6e70227d 8485
f792889a 8486static struct type *
81a17f79
JB
8487read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8488{
8489 struct type *type;
81a17f79 8490
81a17f79
JB
8491 /* For now, we only support the C meaning of an unspecified type: void. */
8492
0114d602
DJ
8493 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8494 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8495
f792889a 8496 return set_die_type (die, type, cu);
81a17f79 8497}
a02abb62 8498
51545339
DJ
8499/* Trivial hash function for die_info: the hash value of a DIE
8500 is its offset in .debug_info for this objfile. */
8501
8502static hashval_t
8503die_hash (const void *item)
8504{
8505 const struct die_info *die = item;
9a619af0 8506
51545339
DJ
8507 return die->offset;
8508}
8509
8510/* Trivial comparison function for die_info structures: two DIEs
8511 are equal if they have the same offset. */
8512
8513static int
8514die_eq (const void *item_lhs, const void *item_rhs)
8515{
8516 const struct die_info *die_lhs = item_lhs;
8517 const struct die_info *die_rhs = item_rhs;
9a619af0 8518
51545339
DJ
8519 return die_lhs->offset == die_rhs->offset;
8520}
8521
c906108c
SS
8522/* Read a whole compilation unit into a linked list of dies. */
8523
f9aca02d 8524static struct die_info *
93311388 8525read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8526{
93311388 8527 struct die_reader_specs reader_specs;
98bfdba5 8528 int read_abbrevs = 0;
1d9ec526 8529 struct cleanup *back_to = NULL;
98bfdba5
PA
8530 struct die_info *die;
8531
8532 if (cu->dwarf2_abbrevs == NULL)
8533 {
8534 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8535 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8536 read_abbrevs = 1;
8537 }
93311388 8538
348e048f 8539 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8540 cu->die_hash
8541 = htab_create_alloc_ex (cu->header.length / 12,
8542 die_hash,
8543 die_eq,
8544 NULL,
8545 &cu->comp_unit_obstack,
8546 hashtab_obstack_allocate,
8547 dummy_obstack_deallocate);
8548
93311388
DE
8549 init_cu_die_reader (&reader_specs, cu);
8550
98bfdba5
PA
8551 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8552
8553 if (read_abbrevs)
8554 do_cleanups (back_to);
8555
8556 return die;
639d11d3
DC
8557}
8558
d97bc12b
DE
8559/* Main entry point for reading a DIE and all children.
8560 Read the DIE and dump it if requested. */
8561
8562static struct die_info *
93311388
DE
8563read_die_and_children (const struct die_reader_specs *reader,
8564 gdb_byte *info_ptr,
d97bc12b
DE
8565 gdb_byte **new_info_ptr,
8566 struct die_info *parent)
8567{
93311388 8568 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8569 new_info_ptr, parent);
8570
8571 if (dwarf2_die_debug)
8572 {
348e048f
DE
8573 fprintf_unfiltered (gdb_stdlog,
8574 "\nRead die from %s of %s:\n",
8575 reader->buffer == dwarf2_per_objfile->info.buffer
8576 ? ".debug_info"
8577 : reader->buffer == dwarf2_per_objfile->types.buffer
8578 ? ".debug_types"
8579 : "unknown section",
8580 reader->abfd->filename);
d97bc12b
DE
8581 dump_die (result, dwarf2_die_debug);
8582 }
8583
8584 return result;
8585}
8586
639d11d3
DC
8587/* Read a single die and all its descendents. Set the die's sibling
8588 field to NULL; set other fields in the die correctly, and set all
8589 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8590 location of the info_ptr after reading all of those dies. PARENT
8591 is the parent of the die in question. */
8592
8593static struct die_info *
93311388
DE
8594read_die_and_children_1 (const struct die_reader_specs *reader,
8595 gdb_byte *info_ptr,
d97bc12b
DE
8596 gdb_byte **new_info_ptr,
8597 struct die_info *parent)
639d11d3
DC
8598{
8599 struct die_info *die;
fe1b8b76 8600 gdb_byte *cur_ptr;
639d11d3
DC
8601 int has_children;
8602
93311388 8603 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8604 if (die == NULL)
8605 {
8606 *new_info_ptr = cur_ptr;
8607 return NULL;
8608 }
93311388 8609 store_in_ref_table (die, reader->cu);
639d11d3
DC
8610
8611 if (has_children)
348e048f 8612 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8613 else
8614 {
8615 die->child = NULL;
8616 *new_info_ptr = cur_ptr;
8617 }
8618
8619 die->sibling = NULL;
8620 die->parent = parent;
8621 return die;
8622}
8623
8624/* Read a die, all of its descendents, and all of its siblings; set
8625 all of the fields of all of the dies correctly. Arguments are as
8626 in read_die_and_children. */
8627
8628static struct die_info *
93311388
DE
8629read_die_and_siblings (const struct die_reader_specs *reader,
8630 gdb_byte *info_ptr,
fe1b8b76 8631 gdb_byte **new_info_ptr,
639d11d3
DC
8632 struct die_info *parent)
8633{
8634 struct die_info *first_die, *last_sibling;
fe1b8b76 8635 gdb_byte *cur_ptr;
639d11d3 8636
c906108c 8637 cur_ptr = info_ptr;
639d11d3
DC
8638 first_die = last_sibling = NULL;
8639
8640 while (1)
c906108c 8641 {
639d11d3 8642 struct die_info *die
93311388 8643 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8644
1d325ec1 8645 if (die == NULL)
c906108c 8646 {
639d11d3
DC
8647 *new_info_ptr = cur_ptr;
8648 return first_die;
c906108c 8649 }
1d325ec1
DJ
8650
8651 if (!first_die)
8652 first_die = die;
c906108c 8653 else
1d325ec1
DJ
8654 last_sibling->sibling = die;
8655
8656 last_sibling = die;
c906108c 8657 }
c906108c
SS
8658}
8659
93311388
DE
8660/* Read the die from the .debug_info section buffer. Set DIEP to
8661 point to a newly allocated die with its information, except for its
8662 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8663 whether the die has children or not. */
8664
8665static gdb_byte *
8666read_full_die (const struct die_reader_specs *reader,
8667 struct die_info **diep, gdb_byte *info_ptr,
8668 int *has_children)
8669{
8670 unsigned int abbrev_number, bytes_read, i, offset;
8671 struct abbrev_info *abbrev;
8672 struct die_info *die;
8673 struct dwarf2_cu *cu = reader->cu;
8674 bfd *abfd = reader->abfd;
8675
8676 offset = info_ptr - reader->buffer;
8677 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8678 info_ptr += bytes_read;
8679 if (!abbrev_number)
8680 {
8681 *diep = NULL;
8682 *has_children = 0;
8683 return info_ptr;
8684 }
8685
8686 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8687 if (!abbrev)
348e048f
DE
8688 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8689 abbrev_number,
8690 bfd_get_filename (abfd));
8691
93311388
DE
8692 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8693 die->offset = offset;
8694 die->tag = abbrev->tag;
8695 die->abbrev = abbrev_number;
8696
8697 die->num_attrs = abbrev->num_attrs;
8698
8699 for (i = 0; i < abbrev->num_attrs; ++i)
8700 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8701 abfd, info_ptr, cu);
8702
8703 *diep = die;
8704 *has_children = abbrev->has_children;
8705 return info_ptr;
8706}
8707
c906108c
SS
8708/* In DWARF version 2, the description of the debugging information is
8709 stored in a separate .debug_abbrev section. Before we read any
8710 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8711 in a hash table. This function also sets flags in CU describing
8712 the data found in the abbrev table. */
c906108c
SS
8713
8714static void
e7c27a73 8715dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8716{
e7c27a73 8717 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8718 gdb_byte *abbrev_ptr;
c906108c
SS
8719 struct abbrev_info *cur_abbrev;
8720 unsigned int abbrev_number, bytes_read, abbrev_name;
8721 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8722 struct attr_abbrev *cur_attrs;
8723 unsigned int allocated_attrs;
c906108c 8724
0963b4bd 8725 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
8726 obstack_init (&cu->abbrev_obstack);
8727 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8728 (ABBREV_HASH_SIZE
8729 * sizeof (struct abbrev_info *)));
8730 memset (cu->dwarf2_abbrevs, 0,
8731 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8732
be391dca
TT
8733 dwarf2_read_section (dwarf2_per_objfile->objfile,
8734 &dwarf2_per_objfile->abbrev);
dce234bc 8735 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8736 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8737 abbrev_ptr += bytes_read;
8738
f3dd6933
DJ
8739 allocated_attrs = ATTR_ALLOC_CHUNK;
8740 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8741
0963b4bd 8742 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
8743 while (abbrev_number)
8744 {
f3dd6933 8745 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8746
8747 /* read in abbrev header */
8748 cur_abbrev->number = abbrev_number;
8749 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8750 abbrev_ptr += bytes_read;
8751 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8752 abbrev_ptr += 1;
8753
72bf9492
DJ
8754 if (cur_abbrev->tag == DW_TAG_namespace)
8755 cu->has_namespace_info = 1;
8756
c906108c
SS
8757 /* now read in declarations */
8758 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8759 abbrev_ptr += bytes_read;
8760 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8761 abbrev_ptr += bytes_read;
8762 while (abbrev_name)
8763 {
f3dd6933 8764 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8765 {
f3dd6933
DJ
8766 allocated_attrs += ATTR_ALLOC_CHUNK;
8767 cur_attrs
8768 = xrealloc (cur_attrs, (allocated_attrs
8769 * sizeof (struct attr_abbrev)));
c906108c 8770 }
ae038cb0
DJ
8771
8772 /* Record whether this compilation unit might have
8773 inter-compilation-unit references. If we don't know what form
8774 this attribute will have, then it might potentially be a
8775 DW_FORM_ref_addr, so we conservatively expect inter-CU
8776 references. */
8777
8778 if (abbrev_form == DW_FORM_ref_addr
8779 || abbrev_form == DW_FORM_indirect)
8780 cu->has_form_ref_addr = 1;
8781
f3dd6933
DJ
8782 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8783 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8784 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8785 abbrev_ptr += bytes_read;
8786 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8787 abbrev_ptr += bytes_read;
8788 }
8789
f3dd6933
DJ
8790 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8791 (cur_abbrev->num_attrs
8792 * sizeof (struct attr_abbrev)));
8793 memcpy (cur_abbrev->attrs, cur_attrs,
8794 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8795
c906108c 8796 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8797 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8798 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8799
8800 /* Get next abbreviation.
8801 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8802 always properly terminated with an abbrev number of 0.
8803 Exit loop if we encounter an abbreviation which we have
8804 already read (which means we are about to read the abbreviations
8805 for the next compile unit) or if the end of the abbreviation
8806 table is reached. */
dce234bc
PP
8807 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8808 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8809 break;
8810 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8811 abbrev_ptr += bytes_read;
e7c27a73 8812 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8813 break;
8814 }
f3dd6933
DJ
8815
8816 xfree (cur_attrs);
c906108c
SS
8817}
8818
f3dd6933 8819/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8820
c906108c 8821static void
f3dd6933 8822dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8823{
f3dd6933 8824 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8825
f3dd6933
DJ
8826 obstack_free (&cu->abbrev_obstack, NULL);
8827 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8828}
8829
8830/* Lookup an abbrev_info structure in the abbrev hash table. */
8831
8832static struct abbrev_info *
e7c27a73 8833dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8834{
8835 unsigned int hash_number;
8836 struct abbrev_info *abbrev;
8837
8838 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8839 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8840
8841 while (abbrev)
8842 {
8843 if (abbrev->number == number)
8844 return abbrev;
8845 else
8846 abbrev = abbrev->next;
8847 }
8848 return NULL;
8849}
8850
72bf9492
DJ
8851/* Returns nonzero if TAG represents a type that we might generate a partial
8852 symbol for. */
8853
8854static int
8855is_type_tag_for_partial (int tag)
8856{
8857 switch (tag)
8858 {
8859#if 0
8860 /* Some types that would be reasonable to generate partial symbols for,
8861 that we don't at present. */
8862 case DW_TAG_array_type:
8863 case DW_TAG_file_type:
8864 case DW_TAG_ptr_to_member_type:
8865 case DW_TAG_set_type:
8866 case DW_TAG_string_type:
8867 case DW_TAG_subroutine_type:
8868#endif
8869 case DW_TAG_base_type:
8870 case DW_TAG_class_type:
680b30c7 8871 case DW_TAG_interface_type:
72bf9492
DJ
8872 case DW_TAG_enumeration_type:
8873 case DW_TAG_structure_type:
8874 case DW_TAG_subrange_type:
8875 case DW_TAG_typedef:
8876 case DW_TAG_union_type:
8877 return 1;
8878 default:
8879 return 0;
8880 }
8881}
8882
8883/* Load all DIEs that are interesting for partial symbols into memory. */
8884
8885static struct partial_die_info *
93311388
DE
8886load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8887 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8888{
8889 struct partial_die_info *part_die;
8890 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8891 struct abbrev_info *abbrev;
8892 unsigned int bytes_read;
5afb4e99 8893 unsigned int load_all = 0;
72bf9492
DJ
8894
8895 int nesting_level = 1;
8896
8897 parent_die = NULL;
8898 last_die = NULL;
8899
5afb4e99
DJ
8900 if (cu->per_cu && cu->per_cu->load_all_dies)
8901 load_all = 1;
8902
72bf9492
DJ
8903 cu->partial_dies
8904 = htab_create_alloc_ex (cu->header.length / 12,
8905 partial_die_hash,
8906 partial_die_eq,
8907 NULL,
8908 &cu->comp_unit_obstack,
8909 hashtab_obstack_allocate,
8910 dummy_obstack_deallocate);
8911
8912 part_die = obstack_alloc (&cu->comp_unit_obstack,
8913 sizeof (struct partial_die_info));
8914
8915 while (1)
8916 {
8917 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8918
8919 /* A NULL abbrev means the end of a series of children. */
8920 if (abbrev == NULL)
8921 {
8922 if (--nesting_level == 0)
8923 {
8924 /* PART_DIE was probably the last thing allocated on the
8925 comp_unit_obstack, so we could call obstack_free
8926 here. We don't do that because the waste is small,
8927 and will be cleaned up when we're done with this
8928 compilation unit. This way, we're also more robust
8929 against other users of the comp_unit_obstack. */
8930 return first_die;
8931 }
8932 info_ptr += bytes_read;
8933 last_die = parent_die;
8934 parent_die = parent_die->die_parent;
8935 continue;
8936 }
8937
98bfdba5
PA
8938 /* Check for template arguments. We never save these; if
8939 they're seen, we just mark the parent, and go on our way. */
8940 if (parent_die != NULL
8941 && cu->language == language_cplus
8942 && (abbrev->tag == DW_TAG_template_type_param
8943 || abbrev->tag == DW_TAG_template_value_param))
8944 {
8945 parent_die->has_template_arguments = 1;
8946
8947 if (!load_all)
8948 {
8949 /* We don't need a partial DIE for the template argument. */
8950 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8951 cu);
8952 continue;
8953 }
8954 }
8955
8956 /* We only recurse into subprograms looking for template arguments.
8957 Skip their other children. */
8958 if (!load_all
8959 && cu->language == language_cplus
8960 && parent_die != NULL
8961 && parent_die->tag == DW_TAG_subprogram)
8962 {
8963 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8964 continue;
8965 }
8966
5afb4e99
DJ
8967 /* Check whether this DIE is interesting enough to save. Normally
8968 we would not be interested in members here, but there may be
8969 later variables referencing them via DW_AT_specification (for
8970 static members). */
8971 if (!load_all
8972 && !is_type_tag_for_partial (abbrev->tag)
72929c62 8973 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
8974 && abbrev->tag != DW_TAG_enumerator
8975 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8976 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8977 && abbrev->tag != DW_TAG_variable
5afb4e99 8978 && abbrev->tag != DW_TAG_namespace
f55ee35c 8979 && abbrev->tag != DW_TAG_module
5afb4e99 8980 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8981 {
8982 /* Otherwise we skip to the next sibling, if any. */
93311388 8983 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8984 continue;
8985 }
8986
93311388
DE
8987 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8988 buffer, info_ptr, cu);
72bf9492
DJ
8989
8990 /* This two-pass algorithm for processing partial symbols has a
8991 high cost in cache pressure. Thus, handle some simple cases
8992 here which cover the majority of C partial symbols. DIEs
8993 which neither have specification tags in them, nor could have
8994 specification tags elsewhere pointing at them, can simply be
8995 processed and discarded.
8996
8997 This segment is also optional; scan_partial_symbols and
8998 add_partial_symbol will handle these DIEs if we chain
8999 them in normally. When compilers which do not emit large
9000 quantities of duplicate debug information are more common,
9001 this code can probably be removed. */
9002
9003 /* Any complete simple types at the top level (pretty much all
9004 of them, for a language without namespaces), can be processed
9005 directly. */
9006 if (parent_die == NULL
9007 && part_die->has_specification == 0
9008 && part_die->is_declaration == 0
d8228535 9009 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9010 || part_die->tag == DW_TAG_base_type
9011 || part_die->tag == DW_TAG_subrange_type))
9012 {
9013 if (building_psymtab && part_die->name != NULL)
04a679b8 9014 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
9015 VAR_DOMAIN, LOC_TYPEDEF,
9016 &cu->objfile->static_psymbols,
9017 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 9018 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9019 continue;
9020 }
9021
d8228535
JK
9022 /* The exception for DW_TAG_typedef with has_children above is
9023 a workaround of GCC PR debug/47510. In the case of this complaint
9024 type_name_no_tag_or_error will error on such types later.
9025
9026 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9027 it could not find the child DIEs referenced later, this is checked
9028 above. In correct DWARF DW_TAG_typedef should have no children. */
9029
9030 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9031 complaint (&symfile_complaints,
9032 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9033 "- DIE at 0x%x [in module %s]"),
9034 part_die->offset, cu->objfile->name);
9035
72bf9492
DJ
9036 /* If we're at the second level, and we're an enumerator, and
9037 our parent has no specification (meaning possibly lives in a
9038 namespace elsewhere), then we can add the partial symbol now
9039 instead of queueing it. */
9040 if (part_die->tag == DW_TAG_enumerator
9041 && parent_die != NULL
9042 && parent_die->die_parent == NULL
9043 && parent_die->tag == DW_TAG_enumeration_type
9044 && parent_die->has_specification == 0)
9045 {
9046 if (part_die->name == NULL)
3e43a32a
MS
9047 complaint (&symfile_complaints,
9048 _("malformed enumerator DIE ignored"));
72bf9492 9049 else if (building_psymtab)
04a679b8 9050 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9051 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9052 (cu->language == language_cplus
9053 || cu->language == language_java)
72bf9492
DJ
9054 ? &cu->objfile->global_psymbols
9055 : &cu->objfile->static_psymbols,
9056 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9057
93311388 9058 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9059 continue;
9060 }
9061
9062 /* We'll save this DIE so link it in. */
9063 part_die->die_parent = parent_die;
9064 part_die->die_sibling = NULL;
9065 part_die->die_child = NULL;
9066
9067 if (last_die && last_die == parent_die)
9068 last_die->die_child = part_die;
9069 else if (last_die)
9070 last_die->die_sibling = part_die;
9071
9072 last_die = part_die;
9073
9074 if (first_die == NULL)
9075 first_die = part_die;
9076
9077 /* Maybe add the DIE to the hash table. Not all DIEs that we
9078 find interesting need to be in the hash table, because we
9079 also have the parent/sibling/child chains; only those that we
9080 might refer to by offset later during partial symbol reading.
9081
9082 For now this means things that might have be the target of a
9083 DW_AT_specification, DW_AT_abstract_origin, or
9084 DW_AT_extension. DW_AT_extension will refer only to
9085 namespaces; DW_AT_abstract_origin refers to functions (and
9086 many things under the function DIE, but we do not recurse
9087 into function DIEs during partial symbol reading) and
9088 possibly variables as well; DW_AT_specification refers to
9089 declarations. Declarations ought to have the DW_AT_declaration
9090 flag. It happens that GCC forgets to put it in sometimes, but
9091 only for functions, not for types.
9092
9093 Adding more things than necessary to the hash table is harmless
9094 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9095 wasted time in find_partial_die, when we reread the compilation
9096 unit with load_all_dies set. */
72bf9492 9097
5afb4e99 9098 if (load_all
72929c62 9099 || abbrev->tag == DW_TAG_constant
5afb4e99 9100 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9101 || abbrev->tag == DW_TAG_variable
9102 || abbrev->tag == DW_TAG_namespace
9103 || part_die->is_declaration)
9104 {
9105 void **slot;
9106
9107 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9108 part_die->offset, INSERT);
9109 *slot = part_die;
9110 }
9111
9112 part_die = obstack_alloc (&cu->comp_unit_obstack,
9113 sizeof (struct partial_die_info));
9114
9115 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9116 we have no reason to follow the children of structures; for other
98bfdba5
PA
9117 languages we have to, so that we can get at method physnames
9118 to infer fully qualified class names, for DW_AT_specification,
9119 and for C++ template arguments. For C++, we also look one level
9120 inside functions to find template arguments (if the name of the
9121 function does not already contain the template arguments).
bc30ff58
JB
9122
9123 For Ada, we need to scan the children of subprograms and lexical
9124 blocks as well because Ada allows the definition of nested
9125 entities that could be interesting for the debugger, such as
9126 nested subprograms for instance. */
72bf9492 9127 if (last_die->has_children
5afb4e99
DJ
9128 && (load_all
9129 || last_die->tag == DW_TAG_namespace
f55ee35c 9130 || last_die->tag == DW_TAG_module
72bf9492 9131 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9132 || (cu->language == language_cplus
9133 && last_die->tag == DW_TAG_subprogram
9134 && (last_die->name == NULL
9135 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9136 || (cu->language != language_c
9137 && (last_die->tag == DW_TAG_class_type
680b30c7 9138 || last_die->tag == DW_TAG_interface_type
72bf9492 9139 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9140 || last_die->tag == DW_TAG_union_type))
9141 || (cu->language == language_ada
9142 && (last_die->tag == DW_TAG_subprogram
9143 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9144 {
9145 nesting_level++;
9146 parent_die = last_die;
9147 continue;
9148 }
9149
9150 /* Otherwise we skip to the next sibling, if any. */
93311388 9151 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9152
9153 /* Back to the top, do it again. */
9154 }
9155}
9156
c906108c
SS
9157/* Read a minimal amount of information into the minimal die structure. */
9158
fe1b8b76 9159static gdb_byte *
72bf9492
DJ
9160read_partial_die (struct partial_die_info *part_die,
9161 struct abbrev_info *abbrev,
9162 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9163 gdb_byte *buffer, gdb_byte *info_ptr,
9164 struct dwarf2_cu *cu)
c906108c 9165{
fa238c03 9166 unsigned int i;
c906108c 9167 struct attribute attr;
c5aa993b 9168 int has_low_pc_attr = 0;
c906108c
SS
9169 int has_high_pc_attr = 0;
9170
72bf9492 9171 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9172
93311388 9173 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9174
9175 info_ptr += abbrev_len;
9176
9177 if (abbrev == NULL)
9178 return info_ptr;
9179
c906108c
SS
9180 part_die->tag = abbrev->tag;
9181 part_die->has_children = abbrev->has_children;
c906108c
SS
9182
9183 for (i = 0; i < abbrev->num_attrs; ++i)
9184 {
e7c27a73 9185 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9186
9187 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9188 partial symbol table. */
c906108c
SS
9189 switch (attr.name)
9190 {
9191 case DW_AT_name:
71c25dea
TT
9192 switch (part_die->tag)
9193 {
9194 case DW_TAG_compile_unit:
348e048f 9195 case DW_TAG_type_unit:
71c25dea
TT
9196 /* Compilation units have a DW_AT_name that is a filename, not
9197 a source language identifier. */
9198 case DW_TAG_enumeration_type:
9199 case DW_TAG_enumerator:
9200 /* These tags always have simple identifiers already; no need
9201 to canonicalize them. */
9202 part_die->name = DW_STRING (&attr);
9203 break;
9204 default:
9205 part_die->name
9206 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9207 &cu->objfile->objfile_obstack);
71c25dea
TT
9208 break;
9209 }
c906108c 9210 break;
31ef98ae 9211 case DW_AT_linkage_name:
c906108c 9212 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9213 /* Note that both forms of linkage name might appear. We
9214 assume they will be the same, and we only store the last
9215 one we see. */
94af9270
KS
9216 if (cu->language == language_ada)
9217 part_die->name = DW_STRING (&attr);
abc72ce4 9218 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9219 break;
9220 case DW_AT_low_pc:
9221 has_low_pc_attr = 1;
9222 part_die->lowpc = DW_ADDR (&attr);
9223 break;
9224 case DW_AT_high_pc:
9225 has_high_pc_attr = 1;
9226 part_die->highpc = DW_ADDR (&attr);
9227 break;
9228 case DW_AT_location:
0963b4bd 9229 /* Support the .debug_loc offsets. */
8e19ed76
PS
9230 if (attr_form_is_block (&attr))
9231 {
9232 part_die->locdesc = DW_BLOCK (&attr);
9233 }
3690dd37 9234 else if (attr_form_is_section_offset (&attr))
8e19ed76 9235 {
4d3c2250 9236 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9237 }
9238 else
9239 {
4d3c2250
KB
9240 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9241 "partial symbol information");
8e19ed76 9242 }
c906108c 9243 break;
c906108c
SS
9244 case DW_AT_external:
9245 part_die->is_external = DW_UNSND (&attr);
9246 break;
9247 case DW_AT_declaration:
9248 part_die->is_declaration = DW_UNSND (&attr);
9249 break;
9250 case DW_AT_type:
9251 part_die->has_type = 1;
9252 break;
9253 case DW_AT_abstract_origin:
9254 case DW_AT_specification:
72bf9492
DJ
9255 case DW_AT_extension:
9256 part_die->has_specification = 1;
c764a876 9257 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9258 break;
9259 case DW_AT_sibling:
9260 /* Ignore absolute siblings, they might point outside of
9261 the current compile unit. */
9262 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9263 complaint (&symfile_complaints,
9264 _("ignoring absolute DW_AT_sibling"));
c906108c 9265 else
93311388 9266 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9267 break;
fa4028e9
JB
9268 case DW_AT_byte_size:
9269 part_die->has_byte_size = 1;
9270 break;
68511cec
CES
9271 case DW_AT_calling_convention:
9272 /* DWARF doesn't provide a way to identify a program's source-level
9273 entry point. DW_AT_calling_convention attributes are only meant
9274 to describe functions' calling conventions.
9275
9276 However, because it's a necessary piece of information in
9277 Fortran, and because DW_CC_program is the only piece of debugging
9278 information whose definition refers to a 'main program' at all,
9279 several compilers have begun marking Fortran main programs with
9280 DW_CC_program --- even when those functions use the standard
9281 calling conventions.
9282
9283 So until DWARF specifies a way to provide this information and
9284 compilers pick up the new representation, we'll support this
9285 practice. */
9286 if (DW_UNSND (&attr) == DW_CC_program
9287 && cu->language == language_fortran)
01f8c46d
JK
9288 {
9289 set_main_name (part_die->name);
9290
9291 /* As this DIE has a static linkage the name would be difficult
9292 to look up later. */
9293 language_of_main = language_fortran;
9294 }
68511cec 9295 break;
c906108c
SS
9296 default:
9297 break;
9298 }
9299 }
9300
9373cf26
JK
9301 if (has_low_pc_attr && has_high_pc_attr)
9302 {
9303 /* When using the GNU linker, .gnu.linkonce. sections are used to
9304 eliminate duplicate copies of functions and vtables and such.
9305 The linker will arbitrarily choose one and discard the others.
9306 The AT_*_pc values for such functions refer to local labels in
9307 these sections. If the section from that file was discarded, the
9308 labels are not in the output, so the relocs get a value of 0.
9309 If this is a discarded function, mark the pc bounds as invalid,
9310 so that GDB will ignore it. */
9311 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9312 {
9313 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9314
9315 complaint (&symfile_complaints,
9316 _("DW_AT_low_pc %s is zero "
9317 "for DIE at 0x%x [in module %s]"),
9318 paddress (gdbarch, part_die->lowpc),
9319 part_die->offset, cu->objfile->name);
9320 }
9321 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9322 else if (part_die->lowpc >= part_die->highpc)
9323 {
9324 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9325
9326 complaint (&symfile_complaints,
9327 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9328 "for DIE at 0x%x [in module %s]"),
9329 paddress (gdbarch, part_die->lowpc),
9330 paddress (gdbarch, part_die->highpc),
9331 part_die->offset, cu->objfile->name);
9332 }
9333 else
9334 part_die->has_pc_info = 1;
9335 }
85cbf3d3 9336
c906108c
SS
9337 return info_ptr;
9338}
9339
72bf9492
DJ
9340/* Find a cached partial DIE at OFFSET in CU. */
9341
9342static struct partial_die_info *
c764a876 9343find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9344{
9345 struct partial_die_info *lookup_die = NULL;
9346 struct partial_die_info part_die;
9347
9348 part_die.offset = offset;
9349 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9350
72bf9492
DJ
9351 return lookup_die;
9352}
9353
348e048f
DE
9354/* Find a partial DIE at OFFSET, which may or may not be in CU,
9355 except in the case of .debug_types DIEs which do not reference
9356 outside their CU (they do however referencing other types via
55f1336d 9357 DW_FORM_ref_sig8). */
72bf9492
DJ
9358
9359static struct partial_die_info *
c764a876 9360find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9361{
5afb4e99
DJ
9362 struct dwarf2_per_cu_data *per_cu = NULL;
9363 struct partial_die_info *pd = NULL;
72bf9492 9364
348e048f
DE
9365 if (cu->per_cu->from_debug_types)
9366 {
9367 pd = find_partial_die_in_comp_unit (offset, cu);
9368 if (pd != NULL)
9369 return pd;
9370 goto not_found;
9371 }
9372
45452591 9373 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9374 {
9375 pd = find_partial_die_in_comp_unit (offset, cu);
9376 if (pd != NULL)
9377 return pd;
9378 }
72bf9492 9379
ae038cb0
DJ
9380 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9381
98bfdba5
PA
9382 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9383 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9384
9385 per_cu->cu->last_used = 0;
5afb4e99
DJ
9386 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9387
9388 if (pd == NULL && per_cu->load_all_dies == 0)
9389 {
9390 struct cleanup *back_to;
9391 struct partial_die_info comp_unit_die;
9392 struct abbrev_info *abbrev;
9393 unsigned int bytes_read;
9394 char *info_ptr;
9395
9396 per_cu->load_all_dies = 1;
9397
9398 /* Re-read the DIEs. */
9399 back_to = make_cleanup (null_cleanup, 0);
9400 if (per_cu->cu->dwarf2_abbrevs == NULL)
9401 {
9402 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9403 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9404 }
dce234bc 9405 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9406 + per_cu->cu->header.offset
9407 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9408 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9409 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9410 per_cu->cu->objfile->obfd,
9411 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9412 per_cu->cu);
9413 if (comp_unit_die.has_children)
93311388
DE
9414 load_partial_dies (per_cu->cu->objfile->obfd,
9415 dwarf2_per_objfile->info.buffer, info_ptr,
9416 0, per_cu->cu);
5afb4e99
DJ
9417 do_cleanups (back_to);
9418
9419 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9420 }
9421
348e048f
DE
9422 not_found:
9423
5afb4e99
DJ
9424 if (pd == NULL)
9425 internal_error (__FILE__, __LINE__,
3e43a32a
MS
9426 _("could not find partial DIE 0x%x "
9427 "in cache [from module %s]\n"),
5afb4e99
DJ
9428 offset, bfd_get_filename (cu->objfile->obfd));
9429 return pd;
72bf9492
DJ
9430}
9431
abc72ce4
DE
9432/* See if we can figure out if the class lives in a namespace. We do
9433 this by looking for a member function; its demangled name will
9434 contain namespace info, if there is any. */
9435
9436static void
9437guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9438 struct dwarf2_cu *cu)
9439{
9440 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9441 what template types look like, because the demangler
9442 frequently doesn't give the same name as the debug info. We
9443 could fix this by only using the demangled name to get the
9444 prefix (but see comment in read_structure_type). */
9445
9446 struct partial_die_info *real_pdi;
9447 struct partial_die_info *child_pdi;
9448
9449 /* If this DIE (this DIE's specification, if any) has a parent, then
9450 we should not do this. We'll prepend the parent's fully qualified
9451 name when we create the partial symbol. */
9452
9453 real_pdi = struct_pdi;
9454 while (real_pdi->has_specification)
9455 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9456
9457 if (real_pdi->die_parent != NULL)
9458 return;
9459
9460 for (child_pdi = struct_pdi->die_child;
9461 child_pdi != NULL;
9462 child_pdi = child_pdi->die_sibling)
9463 {
9464 if (child_pdi->tag == DW_TAG_subprogram
9465 && child_pdi->linkage_name != NULL)
9466 {
9467 char *actual_class_name
9468 = language_class_name_from_physname (cu->language_defn,
9469 child_pdi->linkage_name);
9470 if (actual_class_name != NULL)
9471 {
9472 struct_pdi->name
9473 = obsavestring (actual_class_name,
9474 strlen (actual_class_name),
9475 &cu->objfile->objfile_obstack);
9476 xfree (actual_class_name);
9477 }
9478 break;
9479 }
9480 }
9481}
9482
72bf9492
DJ
9483/* Adjust PART_DIE before generating a symbol for it. This function
9484 may set the is_external flag or change the DIE's name. */
9485
9486static void
9487fixup_partial_die (struct partial_die_info *part_die,
9488 struct dwarf2_cu *cu)
9489{
abc72ce4
DE
9490 /* Once we've fixed up a die, there's no point in doing so again.
9491 This also avoids a memory leak if we were to call
9492 guess_partial_die_structure_name multiple times. */
9493 if (part_die->fixup_called)
9494 return;
9495
72bf9492
DJ
9496 /* If we found a reference attribute and the DIE has no name, try
9497 to find a name in the referred to DIE. */
9498
9499 if (part_die->name == NULL && part_die->has_specification)
9500 {
9501 struct partial_die_info *spec_die;
72bf9492 9502
10b3939b 9503 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9504
10b3939b 9505 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9506
9507 if (spec_die->name)
9508 {
9509 part_die->name = spec_die->name;
9510
9511 /* Copy DW_AT_external attribute if it is set. */
9512 if (spec_die->is_external)
9513 part_die->is_external = spec_die->is_external;
9514 }
9515 }
9516
9517 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9518
9519 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9520 part_die->name = "(anonymous namespace)";
9521
abc72ce4
DE
9522 /* If there is no parent die to provide a namespace, and there are
9523 children, see if we can determine the namespace from their linkage
9524 name.
9525 NOTE: We need to do this even if cu->has_namespace_info != 0.
9526 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9527 if (cu->language == language_cplus
9528 && dwarf2_per_objfile->types.asection != NULL
9529 && part_die->die_parent == NULL
9530 && part_die->has_children
9531 && (part_die->tag == DW_TAG_class_type
9532 || part_die->tag == DW_TAG_structure_type
9533 || part_die->tag == DW_TAG_union_type))
9534 guess_partial_die_structure_name (part_die, cu);
9535
53832f31
TT
9536 /* GCC might emit a nameless struct or union that has a linkage
9537 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
9538 if (part_die->name == NULL
9539 && (part_die->tag == DW_TAG_structure_type
9540 || part_die->tag == DW_TAG_union_type
9541 || part_die->tag == DW_TAG_class_type)
9542 && part_die->linkage_name != NULL)
9543 {
9544 char *demangled;
9545
9546 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9547 if (demangled)
9548 {
9549 part_die->name = obsavestring (demangled, strlen (demangled),
9550 &cu->objfile->objfile_obstack);
9551 xfree (demangled);
9552 }
9553 }
9554
abc72ce4 9555 part_die->fixup_called = 1;
72bf9492
DJ
9556}
9557
a8329558 9558/* Read an attribute value described by an attribute form. */
c906108c 9559
fe1b8b76 9560static gdb_byte *
a8329558 9561read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9562 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9563 struct dwarf2_cu *cu)
c906108c 9564{
e7c27a73 9565 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9566 unsigned int bytes_read;
9567 struct dwarf_block *blk;
9568
a8329558
KW
9569 attr->form = form;
9570 switch (form)
c906108c 9571 {
c906108c 9572 case DW_FORM_ref_addr:
ae411497
TT
9573 if (cu->header.version == 2)
9574 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9575 else
3e43a32a
MS
9576 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9577 &cu->header, &bytes_read);
ae411497
TT
9578 info_ptr += bytes_read;
9579 break;
9580 case DW_FORM_addr:
e7c27a73 9581 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9582 info_ptr += bytes_read;
c906108c
SS
9583 break;
9584 case DW_FORM_block2:
7b5a2f43 9585 blk = dwarf_alloc_block (cu);
c906108c
SS
9586 blk->size = read_2_bytes (abfd, info_ptr);
9587 info_ptr += 2;
9588 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9589 info_ptr += blk->size;
9590 DW_BLOCK (attr) = blk;
9591 break;
9592 case DW_FORM_block4:
7b5a2f43 9593 blk = dwarf_alloc_block (cu);
c906108c
SS
9594 blk->size = read_4_bytes (abfd, info_ptr);
9595 info_ptr += 4;
9596 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9597 info_ptr += blk->size;
9598 DW_BLOCK (attr) = blk;
9599 break;
9600 case DW_FORM_data2:
9601 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9602 info_ptr += 2;
9603 break;
9604 case DW_FORM_data4:
9605 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9606 info_ptr += 4;
9607 break;
9608 case DW_FORM_data8:
9609 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9610 info_ptr += 8;
9611 break;
2dc7f7b3
TT
9612 case DW_FORM_sec_offset:
9613 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9614 info_ptr += bytes_read;
9615 break;
c906108c 9616 case DW_FORM_string:
9b1c24c8 9617 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9618 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9619 info_ptr += bytes_read;
9620 break;
4bdf3d34
JJ
9621 case DW_FORM_strp:
9622 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9623 &bytes_read);
8285870a 9624 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9625 info_ptr += bytes_read;
9626 break;
2dc7f7b3 9627 case DW_FORM_exprloc:
c906108c 9628 case DW_FORM_block:
7b5a2f43 9629 blk = dwarf_alloc_block (cu);
c906108c
SS
9630 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9631 info_ptr += bytes_read;
9632 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9633 info_ptr += blk->size;
9634 DW_BLOCK (attr) = blk;
9635 break;
9636 case DW_FORM_block1:
7b5a2f43 9637 blk = dwarf_alloc_block (cu);
c906108c
SS
9638 blk->size = read_1_byte (abfd, info_ptr);
9639 info_ptr += 1;
9640 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9641 info_ptr += blk->size;
9642 DW_BLOCK (attr) = blk;
9643 break;
9644 case DW_FORM_data1:
9645 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9646 info_ptr += 1;
9647 break;
9648 case DW_FORM_flag:
9649 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9650 info_ptr += 1;
9651 break;
2dc7f7b3
TT
9652 case DW_FORM_flag_present:
9653 DW_UNSND (attr) = 1;
9654 break;
c906108c
SS
9655 case DW_FORM_sdata:
9656 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9657 info_ptr += bytes_read;
9658 break;
9659 case DW_FORM_udata:
9660 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9661 info_ptr += bytes_read;
9662 break;
9663 case DW_FORM_ref1:
10b3939b 9664 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9665 info_ptr += 1;
9666 break;
9667 case DW_FORM_ref2:
10b3939b 9668 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9669 info_ptr += 2;
9670 break;
9671 case DW_FORM_ref4:
10b3939b 9672 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9673 info_ptr += 4;
9674 break;
613e1657 9675 case DW_FORM_ref8:
10b3939b 9676 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9677 info_ptr += 8;
9678 break;
55f1336d 9679 case DW_FORM_ref_sig8:
348e048f
DE
9680 /* Convert the signature to something we can record in DW_UNSND
9681 for later lookup.
9682 NOTE: This is NULL if the type wasn't found. */
9683 DW_SIGNATURED_TYPE (attr) =
9684 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9685 info_ptr += 8;
9686 break;
c906108c 9687 case DW_FORM_ref_udata:
10b3939b
DJ
9688 DW_ADDR (attr) = (cu->header.offset
9689 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9690 info_ptr += bytes_read;
9691 break;
c906108c 9692 case DW_FORM_indirect:
a8329558
KW
9693 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9694 info_ptr += bytes_read;
e7c27a73 9695 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9696 break;
c906108c 9697 default:
8a3fe4f8 9698 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9699 dwarf_form_name (form),
9700 bfd_get_filename (abfd));
c906108c 9701 }
28e94949
JB
9702
9703 /* We have seen instances where the compiler tried to emit a byte
9704 size attribute of -1 which ended up being encoded as an unsigned
9705 0xffffffff. Although 0xffffffff is technically a valid size value,
9706 an object of this size seems pretty unlikely so we can relatively
9707 safely treat these cases as if the size attribute was invalid and
9708 treat them as zero by default. */
9709 if (attr->name == DW_AT_byte_size
9710 && form == DW_FORM_data4
9711 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9712 {
9713 complaint
9714 (&symfile_complaints,
43bbcdc2
PH
9715 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9716 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9717 DW_UNSND (attr) = 0;
9718 }
28e94949 9719
c906108c
SS
9720 return info_ptr;
9721}
9722
a8329558
KW
9723/* Read an attribute described by an abbreviated attribute. */
9724
fe1b8b76 9725static gdb_byte *
a8329558 9726read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9727 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9728{
9729 attr->name = abbrev->name;
e7c27a73 9730 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9731}
9732
0963b4bd 9733/* Read dwarf information from a buffer. */
c906108c
SS
9734
9735static unsigned int
fe1b8b76 9736read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9737{
fe1b8b76 9738 return bfd_get_8 (abfd, buf);
c906108c
SS
9739}
9740
9741static int
fe1b8b76 9742read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9743{
fe1b8b76 9744 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9745}
9746
9747static unsigned int
fe1b8b76 9748read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9749{
fe1b8b76 9750 return bfd_get_16 (abfd, buf);
c906108c
SS
9751}
9752
9753static int
fe1b8b76 9754read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9755{
fe1b8b76 9756 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9757}
9758
9759static unsigned int
fe1b8b76 9760read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9761{
fe1b8b76 9762 return bfd_get_32 (abfd, buf);
c906108c
SS
9763}
9764
9765static int
fe1b8b76 9766read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9767{
fe1b8b76 9768 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9769}
9770
93311388 9771static ULONGEST
fe1b8b76 9772read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9773{
fe1b8b76 9774 return bfd_get_64 (abfd, buf);
c906108c
SS
9775}
9776
9777static CORE_ADDR
fe1b8b76 9778read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9779 unsigned int *bytes_read)
c906108c 9780{
e7c27a73 9781 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9782 CORE_ADDR retval = 0;
9783
107d2387 9784 if (cu_header->signed_addr_p)
c906108c 9785 {
107d2387
AC
9786 switch (cu_header->addr_size)
9787 {
9788 case 2:
fe1b8b76 9789 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9790 break;
9791 case 4:
fe1b8b76 9792 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9793 break;
9794 case 8:
fe1b8b76 9795 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9796 break;
9797 default:
8e65ff28 9798 internal_error (__FILE__, __LINE__,
e2e0b3e5 9799 _("read_address: bad switch, signed [in module %s]"),
659b0389 9800 bfd_get_filename (abfd));
107d2387
AC
9801 }
9802 }
9803 else
9804 {
9805 switch (cu_header->addr_size)
9806 {
9807 case 2:
fe1b8b76 9808 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9809 break;
9810 case 4:
fe1b8b76 9811 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9812 break;
9813 case 8:
fe1b8b76 9814 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9815 break;
9816 default:
8e65ff28 9817 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
9818 _("read_address: bad switch, "
9819 "unsigned [in module %s]"),
659b0389 9820 bfd_get_filename (abfd));
107d2387 9821 }
c906108c 9822 }
64367e0a 9823
107d2387
AC
9824 *bytes_read = cu_header->addr_size;
9825 return retval;
c906108c
SS
9826}
9827
f7ef9339 9828/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9829 specification allows the initial length to take up either 4 bytes
9830 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9831 bytes describe the length and all offsets will be 8 bytes in length
9832 instead of 4.
9833
f7ef9339
KB
9834 An older, non-standard 64-bit format is also handled by this
9835 function. The older format in question stores the initial length
9836 as an 8-byte quantity without an escape value. Lengths greater
9837 than 2^32 aren't very common which means that the initial 4 bytes
9838 is almost always zero. Since a length value of zero doesn't make
9839 sense for the 32-bit format, this initial zero can be considered to
9840 be an escape value which indicates the presence of the older 64-bit
9841 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9842 greater than 4GB. If it becomes necessary to handle lengths
9843 somewhat larger than 4GB, we could allow other small values (such
9844 as the non-sensical values of 1, 2, and 3) to also be used as
9845 escape values indicating the presence of the old format.
f7ef9339 9846
917c78fc
MK
9847 The value returned via bytes_read should be used to increment the
9848 relevant pointer after calling read_initial_length().
c764a876 9849
613e1657
KB
9850 [ Note: read_initial_length() and read_offset() are based on the
9851 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9852 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9853 from:
9854
f7ef9339 9855 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9856
613e1657
KB
9857 This document is only a draft and is subject to change. (So beware.)
9858
f7ef9339 9859 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9860 determined empirically by examining 64-bit ELF files produced by
9861 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9862
9863 - Kevin, July 16, 2002
613e1657
KB
9864 ] */
9865
9866static LONGEST
c764a876 9867read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9868{
fe1b8b76 9869 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9870
dd373385 9871 if (length == 0xffffffff)
613e1657 9872 {
fe1b8b76 9873 length = bfd_get_64 (abfd, buf + 4);
613e1657 9874 *bytes_read = 12;
613e1657 9875 }
dd373385 9876 else if (length == 0)
f7ef9339 9877 {
dd373385 9878 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9879 length = bfd_get_64 (abfd, buf);
f7ef9339 9880 *bytes_read = 8;
f7ef9339 9881 }
613e1657
KB
9882 else
9883 {
9884 *bytes_read = 4;
613e1657
KB
9885 }
9886
c764a876
DE
9887 return length;
9888}
dd373385 9889
c764a876
DE
9890/* Cover function for read_initial_length.
9891 Returns the length of the object at BUF, and stores the size of the
9892 initial length in *BYTES_READ and stores the size that offsets will be in
9893 *OFFSET_SIZE.
9894 If the initial length size is not equivalent to that specified in
9895 CU_HEADER then issue a complaint.
9896 This is useful when reading non-comp-unit headers. */
dd373385 9897
c764a876
DE
9898static LONGEST
9899read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9900 const struct comp_unit_head *cu_header,
9901 unsigned int *bytes_read,
9902 unsigned int *offset_size)
9903{
9904 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9905
9906 gdb_assert (cu_header->initial_length_size == 4
9907 || cu_header->initial_length_size == 8
9908 || cu_header->initial_length_size == 12);
9909
9910 if (cu_header->initial_length_size != *bytes_read)
9911 complaint (&symfile_complaints,
9912 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9913
c764a876 9914 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9915 return length;
613e1657
KB
9916}
9917
9918/* Read an offset from the data stream. The size of the offset is
917c78fc 9919 given by cu_header->offset_size. */
613e1657
KB
9920
9921static LONGEST
fe1b8b76 9922read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9923 unsigned int *bytes_read)
c764a876
DE
9924{
9925 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9926
c764a876
DE
9927 *bytes_read = cu_header->offset_size;
9928 return offset;
9929}
9930
9931/* Read an offset from the data stream. */
9932
9933static LONGEST
9934read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9935{
9936 LONGEST retval = 0;
9937
c764a876 9938 switch (offset_size)
613e1657
KB
9939 {
9940 case 4:
fe1b8b76 9941 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9942 break;
9943 case 8:
fe1b8b76 9944 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9945 break;
9946 default:
8e65ff28 9947 internal_error (__FILE__, __LINE__,
c764a876 9948 _("read_offset_1: bad switch [in module %s]"),
659b0389 9949 bfd_get_filename (abfd));
613e1657
KB
9950 }
9951
917c78fc 9952 return retval;
613e1657
KB
9953}
9954
fe1b8b76
JB
9955static gdb_byte *
9956read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9957{
9958 /* If the size of a host char is 8 bits, we can return a pointer
9959 to the buffer, otherwise we have to copy the data to a buffer
9960 allocated on the temporary obstack. */
4bdf3d34 9961 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9962 return buf;
c906108c
SS
9963}
9964
9965static char *
9b1c24c8 9966read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9967{
9968 /* If the size of a host char is 8 bits, we can return a pointer
9969 to the string, otherwise we have to copy the string to a buffer
9970 allocated on the temporary obstack. */
4bdf3d34 9971 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9972 if (*buf == '\0')
9973 {
9974 *bytes_read_ptr = 1;
9975 return NULL;
9976 }
fe1b8b76
JB
9977 *bytes_read_ptr = strlen ((char *) buf) + 1;
9978 return (char *) buf;
4bdf3d34
JJ
9979}
9980
9981static char *
fe1b8b76 9982read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9983 const struct comp_unit_head *cu_header,
9984 unsigned int *bytes_read_ptr)
9985{
c764a876 9986 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9987
be391dca 9988 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9989 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9990 {
8a3fe4f8 9991 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9992 bfd_get_filename (abfd));
4bdf3d34 9993 return NULL;
c906108c 9994 }
dce234bc 9995 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9996 {
3e43a32a
MS
9997 error (_("DW_FORM_strp pointing outside of "
9998 ".debug_str section [in module %s]"),
9999 bfd_get_filename (abfd));
c906108c
SS
10000 return NULL;
10001 }
4bdf3d34 10002 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10003 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10004 return NULL;
dce234bc 10005 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10006}
10007
ce5d95e1 10008static unsigned long
fe1b8b76 10009read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10010{
ce5d95e1
JB
10011 unsigned long result;
10012 unsigned int num_read;
c906108c
SS
10013 int i, shift;
10014 unsigned char byte;
10015
10016 result = 0;
10017 shift = 0;
10018 num_read = 0;
10019 i = 0;
10020 while (1)
10021 {
fe1b8b76 10022 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10023 buf++;
10024 num_read++;
ce5d95e1 10025 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10026 if ((byte & 128) == 0)
10027 {
10028 break;
10029 }
10030 shift += 7;
10031 }
10032 *bytes_read_ptr = num_read;
10033 return result;
10034}
10035
ce5d95e1 10036static long
fe1b8b76 10037read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10038{
ce5d95e1 10039 long result;
77e0b926 10040 int i, shift, num_read;
c906108c
SS
10041 unsigned char byte;
10042
10043 result = 0;
10044 shift = 0;
c906108c
SS
10045 num_read = 0;
10046 i = 0;
10047 while (1)
10048 {
fe1b8b76 10049 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10050 buf++;
10051 num_read++;
ce5d95e1 10052 result |= ((long)(byte & 127) << shift);
c906108c
SS
10053 shift += 7;
10054 if ((byte & 128) == 0)
10055 {
10056 break;
10057 }
10058 }
77e0b926
DJ
10059 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10060 result |= -(((long)1) << shift);
c906108c
SS
10061 *bytes_read_ptr = num_read;
10062 return result;
10063}
10064
4bb7a0a7
DJ
10065/* Return a pointer to just past the end of an LEB128 number in BUF. */
10066
fe1b8b76
JB
10067static gdb_byte *
10068skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10069{
10070 int byte;
10071
10072 while (1)
10073 {
fe1b8b76 10074 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10075 buf++;
10076 if ((byte & 128) == 0)
10077 return buf;
10078 }
10079}
10080
c906108c 10081static void
e142c38c 10082set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10083{
10084 switch (lang)
10085 {
10086 case DW_LANG_C89:
76bee0cc 10087 case DW_LANG_C99:
c906108c 10088 case DW_LANG_C:
e142c38c 10089 cu->language = language_c;
c906108c
SS
10090 break;
10091 case DW_LANG_C_plus_plus:
e142c38c 10092 cu->language = language_cplus;
c906108c 10093 break;
6aecb9c2
JB
10094 case DW_LANG_D:
10095 cu->language = language_d;
10096 break;
c906108c
SS
10097 case DW_LANG_Fortran77:
10098 case DW_LANG_Fortran90:
b21b22e0 10099 case DW_LANG_Fortran95:
e142c38c 10100 cu->language = language_fortran;
c906108c
SS
10101 break;
10102 case DW_LANG_Mips_Assembler:
e142c38c 10103 cu->language = language_asm;
c906108c 10104 break;
bebd888e 10105 case DW_LANG_Java:
e142c38c 10106 cu->language = language_java;
bebd888e 10107 break;
c906108c 10108 case DW_LANG_Ada83:
8aaf0b47 10109 case DW_LANG_Ada95:
bc5f45f8
JB
10110 cu->language = language_ada;
10111 break;
72019c9c
GM
10112 case DW_LANG_Modula2:
10113 cu->language = language_m2;
10114 break;
fe8e67fd
PM
10115 case DW_LANG_Pascal83:
10116 cu->language = language_pascal;
10117 break;
22566fbd
DJ
10118 case DW_LANG_ObjC:
10119 cu->language = language_objc;
10120 break;
c906108c
SS
10121 case DW_LANG_Cobol74:
10122 case DW_LANG_Cobol85:
c906108c 10123 default:
e142c38c 10124 cu->language = language_minimal;
c906108c
SS
10125 break;
10126 }
e142c38c 10127 cu->language_defn = language_def (cu->language);
c906108c
SS
10128}
10129
10130/* Return the named attribute or NULL if not there. */
10131
10132static struct attribute *
e142c38c 10133dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10134{
10135 unsigned int i;
10136 struct attribute *spec = NULL;
10137
10138 for (i = 0; i < die->num_attrs; ++i)
10139 {
10140 if (die->attrs[i].name == name)
10b3939b 10141 return &die->attrs[i];
c906108c
SS
10142 if (die->attrs[i].name == DW_AT_specification
10143 || die->attrs[i].name == DW_AT_abstract_origin)
10144 spec = &die->attrs[i];
10145 }
c906108c 10146
10b3939b 10147 if (spec)
f2f0e013
DJ
10148 {
10149 die = follow_die_ref (die, spec, &cu);
10150 return dwarf2_attr (die, name, cu);
10151 }
c5aa993b 10152
c906108c
SS
10153 return NULL;
10154}
10155
348e048f
DE
10156/* Return the named attribute or NULL if not there,
10157 but do not follow DW_AT_specification, etc.
10158 This is for use in contexts where we're reading .debug_types dies.
10159 Following DW_AT_specification, DW_AT_abstract_origin will take us
10160 back up the chain, and we want to go down. */
10161
10162static struct attribute *
10163dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10164 struct dwarf2_cu *cu)
10165{
10166 unsigned int i;
10167
10168 for (i = 0; i < die->num_attrs; ++i)
10169 if (die->attrs[i].name == name)
10170 return &die->attrs[i];
10171
10172 return NULL;
10173}
10174
05cf31d1
JB
10175/* Return non-zero iff the attribute NAME is defined for the given DIE,
10176 and holds a non-zero value. This function should only be used for
2dc7f7b3 10177 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10178
10179static int
10180dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10181{
10182 struct attribute *attr = dwarf2_attr (die, name, cu);
10183
10184 return (attr && DW_UNSND (attr));
10185}
10186
3ca72b44 10187static int
e142c38c 10188die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10189{
05cf31d1
JB
10190 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10191 which value is non-zero. However, we have to be careful with
10192 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10193 (via dwarf2_flag_true_p) follows this attribute. So we may
10194 end up accidently finding a declaration attribute that belongs
10195 to a different DIE referenced by the specification attribute,
10196 even though the given DIE does not have a declaration attribute. */
10197 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10198 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10199}
10200
63d06c5c 10201/* Return the die giving the specification for DIE, if there is
f2f0e013 10202 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10203 containing the return value on output. If there is no
10204 specification, but there is an abstract origin, that is
10205 returned. */
63d06c5c
DC
10206
10207static struct die_info *
f2f0e013 10208die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10209{
f2f0e013
DJ
10210 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10211 *spec_cu);
63d06c5c 10212
edb3359d
DJ
10213 if (spec_attr == NULL)
10214 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10215
63d06c5c
DC
10216 if (spec_attr == NULL)
10217 return NULL;
10218 else
f2f0e013 10219 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10220}
c906108c 10221
debd256d 10222/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10223 refers to.
10224 NOTE: This is also used as a "cleanup" function. */
10225
debd256d
JB
10226static void
10227free_line_header (struct line_header *lh)
10228{
10229 if (lh->standard_opcode_lengths)
a8bc7b56 10230 xfree (lh->standard_opcode_lengths);
debd256d
JB
10231
10232 /* Remember that all the lh->file_names[i].name pointers are
10233 pointers into debug_line_buffer, and don't need to be freed. */
10234 if (lh->file_names)
a8bc7b56 10235 xfree (lh->file_names);
debd256d
JB
10236
10237 /* Similarly for the include directory names. */
10238 if (lh->include_dirs)
a8bc7b56 10239 xfree (lh->include_dirs);
debd256d 10240
a8bc7b56 10241 xfree (lh);
debd256d
JB
10242}
10243
debd256d 10244/* Add an entry to LH's include directory table. */
ae2de4f8 10245
debd256d
JB
10246static void
10247add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10248{
debd256d
JB
10249 /* Grow the array if necessary. */
10250 if (lh->include_dirs_size == 0)
c5aa993b 10251 {
debd256d
JB
10252 lh->include_dirs_size = 1; /* for testing */
10253 lh->include_dirs = xmalloc (lh->include_dirs_size
10254 * sizeof (*lh->include_dirs));
10255 }
10256 else if (lh->num_include_dirs >= lh->include_dirs_size)
10257 {
10258 lh->include_dirs_size *= 2;
10259 lh->include_dirs = xrealloc (lh->include_dirs,
10260 (lh->include_dirs_size
10261 * sizeof (*lh->include_dirs)));
c5aa993b 10262 }
c906108c 10263
debd256d
JB
10264 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10265}
6e70227d 10266
debd256d 10267/* Add an entry to LH's file name table. */
ae2de4f8 10268
debd256d
JB
10269static void
10270add_file_name (struct line_header *lh,
10271 char *name,
10272 unsigned int dir_index,
10273 unsigned int mod_time,
10274 unsigned int length)
10275{
10276 struct file_entry *fe;
10277
10278 /* Grow the array if necessary. */
10279 if (lh->file_names_size == 0)
10280 {
10281 lh->file_names_size = 1; /* for testing */
10282 lh->file_names = xmalloc (lh->file_names_size
10283 * sizeof (*lh->file_names));
10284 }
10285 else if (lh->num_file_names >= lh->file_names_size)
10286 {
10287 lh->file_names_size *= 2;
10288 lh->file_names = xrealloc (lh->file_names,
10289 (lh->file_names_size
10290 * sizeof (*lh->file_names)));
10291 }
10292
10293 fe = &lh->file_names[lh->num_file_names++];
10294 fe->name = name;
10295 fe->dir_index = dir_index;
10296 fe->mod_time = mod_time;
10297 fe->length = length;
aaa75496 10298 fe->included_p = 0;
cb1df416 10299 fe->symtab = NULL;
debd256d 10300}
6e70227d 10301
debd256d 10302/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10303 .debug_line, according to the endianness of ABFD. Return a pointer
10304 to a struct line_header, allocated using xmalloc.
debd256d
JB
10305
10306 NOTE: the strings in the include directory and file name tables of
10307 the returned object point into debug_line_buffer, and must not be
10308 freed. */
ae2de4f8 10309
debd256d
JB
10310static struct line_header *
10311dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10312 struct dwarf2_cu *cu)
debd256d
JB
10313{
10314 struct cleanup *back_to;
10315 struct line_header *lh;
fe1b8b76 10316 gdb_byte *line_ptr;
c764a876 10317 unsigned int bytes_read, offset_size;
debd256d
JB
10318 int i;
10319 char *cur_dir, *cur_file;
10320
be391dca 10321 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10322 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10323 {
e2e0b3e5 10324 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10325 return 0;
10326 }
10327
a738430d
MK
10328 /* Make sure that at least there's room for the total_length field.
10329 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10330 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10331 {
4d3c2250 10332 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10333 return 0;
10334 }
10335
10336 lh = xmalloc (sizeof (*lh));
10337 memset (lh, 0, sizeof (*lh));
10338 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10339 (void *) lh);
10340
dce234bc 10341 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10342
a738430d 10343 /* Read in the header. */
6e70227d 10344 lh->total_length =
c764a876
DE
10345 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10346 &bytes_read, &offset_size);
debd256d 10347 line_ptr += bytes_read;
dce234bc
PP
10348 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10349 + dwarf2_per_objfile->line.size))
debd256d 10350 {
4d3c2250 10351 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10352 return 0;
10353 }
10354 lh->statement_program_end = line_ptr + lh->total_length;
10355 lh->version = read_2_bytes (abfd, line_ptr);
10356 line_ptr += 2;
c764a876
DE
10357 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10358 line_ptr += offset_size;
debd256d
JB
10359 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10360 line_ptr += 1;
2dc7f7b3
TT
10361 if (lh->version >= 4)
10362 {
10363 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10364 line_ptr += 1;
10365 }
10366 else
10367 lh->maximum_ops_per_instruction = 1;
10368
10369 if (lh->maximum_ops_per_instruction == 0)
10370 {
10371 lh->maximum_ops_per_instruction = 1;
10372 complaint (&symfile_complaints,
3e43a32a
MS
10373 _("invalid maximum_ops_per_instruction "
10374 "in `.debug_line' section"));
2dc7f7b3
TT
10375 }
10376
debd256d
JB
10377 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10378 line_ptr += 1;
10379 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10380 line_ptr += 1;
10381 lh->line_range = read_1_byte (abfd, line_ptr);
10382 line_ptr += 1;
10383 lh->opcode_base = read_1_byte (abfd, line_ptr);
10384 line_ptr += 1;
10385 lh->standard_opcode_lengths
fe1b8b76 10386 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10387
10388 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10389 for (i = 1; i < lh->opcode_base; ++i)
10390 {
10391 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10392 line_ptr += 1;
10393 }
10394
a738430d 10395 /* Read directory table. */
9b1c24c8 10396 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10397 {
10398 line_ptr += bytes_read;
10399 add_include_dir (lh, cur_dir);
10400 }
10401 line_ptr += bytes_read;
10402
a738430d 10403 /* Read file name table. */
9b1c24c8 10404 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10405 {
10406 unsigned int dir_index, mod_time, length;
10407
10408 line_ptr += bytes_read;
10409 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10410 line_ptr += bytes_read;
10411 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10412 line_ptr += bytes_read;
10413 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10414 line_ptr += bytes_read;
10415
10416 add_file_name (lh, cur_file, dir_index, mod_time, length);
10417 }
10418 line_ptr += bytes_read;
6e70227d 10419 lh->statement_program_start = line_ptr;
debd256d 10420
dce234bc
PP
10421 if (line_ptr > (dwarf2_per_objfile->line.buffer
10422 + dwarf2_per_objfile->line.size))
4d3c2250 10423 complaint (&symfile_complaints,
3e43a32a
MS
10424 _("line number info header doesn't "
10425 "fit in `.debug_line' section"));
debd256d
JB
10426
10427 discard_cleanups (back_to);
10428 return lh;
10429}
c906108c 10430
5fb290d7
DJ
10431/* This function exists to work around a bug in certain compilers
10432 (particularly GCC 2.95), in which the first line number marker of a
10433 function does not show up until after the prologue, right before
10434 the second line number marker. This function shifts ADDRESS down
10435 to the beginning of the function if necessary, and is called on
10436 addresses passed to record_line. */
10437
10438static CORE_ADDR
e142c38c 10439check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10440{
10441 struct function_range *fn;
10442
10443 /* Find the function_range containing address. */
e142c38c 10444 if (!cu->first_fn)
5fb290d7
DJ
10445 return address;
10446
e142c38c
DJ
10447 if (!cu->cached_fn)
10448 cu->cached_fn = cu->first_fn;
5fb290d7 10449
e142c38c 10450 fn = cu->cached_fn;
5fb290d7
DJ
10451 while (fn)
10452 if (fn->lowpc <= address && fn->highpc > address)
10453 goto found;
10454 else
10455 fn = fn->next;
10456
e142c38c
DJ
10457 fn = cu->first_fn;
10458 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10459 if (fn->lowpc <= address && fn->highpc > address)
10460 goto found;
10461 else
10462 fn = fn->next;
10463
10464 return address;
10465
10466 found:
10467 if (fn->seen_line)
10468 return address;
10469 if (address != fn->lowpc)
4d3c2250 10470 complaint (&symfile_complaints,
e2e0b3e5 10471 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10472 (unsigned long) address, fn->name);
5fb290d7
DJ
10473 fn->seen_line = 1;
10474 return fn->lowpc;
10475}
10476
c6da4cef
DE
10477/* Subroutine of dwarf_decode_lines to simplify it.
10478 Return the file name of the psymtab for included file FILE_INDEX
10479 in line header LH of PST.
10480 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10481 If space for the result is malloc'd, it will be freed by a cleanup.
10482 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10483
10484static char *
10485psymtab_include_file_name (const struct line_header *lh, int file_index,
10486 const struct partial_symtab *pst,
10487 const char *comp_dir)
10488{
10489 const struct file_entry fe = lh->file_names [file_index];
10490 char *include_name = fe.name;
10491 char *include_name_to_compare = include_name;
10492 char *dir_name = NULL;
72b9f47f
TT
10493 const char *pst_filename;
10494 char *copied_name = NULL;
c6da4cef
DE
10495 int file_is_pst;
10496
10497 if (fe.dir_index)
10498 dir_name = lh->include_dirs[fe.dir_index - 1];
10499
10500 if (!IS_ABSOLUTE_PATH (include_name)
10501 && (dir_name != NULL || comp_dir != NULL))
10502 {
10503 /* Avoid creating a duplicate psymtab for PST.
10504 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10505 Before we do the comparison, however, we need to account
10506 for DIR_NAME and COMP_DIR.
10507 First prepend dir_name (if non-NULL). If we still don't
10508 have an absolute path prepend comp_dir (if non-NULL).
10509 However, the directory we record in the include-file's
10510 psymtab does not contain COMP_DIR (to match the
10511 corresponding symtab(s)).
10512
10513 Example:
10514
10515 bash$ cd /tmp
10516 bash$ gcc -g ./hello.c
10517 include_name = "hello.c"
10518 dir_name = "."
10519 DW_AT_comp_dir = comp_dir = "/tmp"
10520 DW_AT_name = "./hello.c" */
10521
10522 if (dir_name != NULL)
10523 {
10524 include_name = concat (dir_name, SLASH_STRING,
10525 include_name, (char *)NULL);
10526 include_name_to_compare = include_name;
10527 make_cleanup (xfree, include_name);
10528 }
10529 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10530 {
10531 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10532 include_name, (char *)NULL);
10533 }
10534 }
10535
10536 pst_filename = pst->filename;
10537 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10538 {
72b9f47f
TT
10539 copied_name = concat (pst->dirname, SLASH_STRING,
10540 pst_filename, (char *)NULL);
10541 pst_filename = copied_name;
c6da4cef
DE
10542 }
10543
1e3fad37 10544 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10545
10546 if (include_name_to_compare != include_name)
10547 xfree (include_name_to_compare);
72b9f47f
TT
10548 if (copied_name != NULL)
10549 xfree (copied_name);
c6da4cef
DE
10550
10551 if (file_is_pst)
10552 return NULL;
10553 return include_name;
10554}
10555
c91513d8
PP
10556/* Ignore this record_line request. */
10557
10558static void
10559noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10560{
10561 return;
10562}
10563
aaa75496
JB
10564/* Decode the Line Number Program (LNP) for the given line_header
10565 structure and CU. The actual information extracted and the type
10566 of structures created from the LNP depends on the value of PST.
10567
10568 1. If PST is NULL, then this procedure uses the data from the program
10569 to create all necessary symbol tables, and their linetables.
6e70227d 10570
aaa75496
JB
10571 2. If PST is not NULL, this procedure reads the program to determine
10572 the list of files included by the unit represented by PST, and
c6da4cef
DE
10573 builds all the associated partial symbol tables.
10574
10575 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10576 It is used for relative paths in the line table.
10577 NOTE: When processing partial symtabs (pst != NULL),
10578 comp_dir == pst->dirname.
10579
10580 NOTE: It is important that psymtabs have the same file name (via strcmp)
10581 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10582 symtab we don't use it in the name of the psymtabs we create.
10583 E.g. expand_line_sal requires this when finding psymtabs to expand.
10584 A good testcase for this is mb-inline.exp. */
debd256d 10585
c906108c 10586static void
72b9f47f 10587dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10588 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10589{
a8c50c1f 10590 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10591 gdb_byte *line_end;
a8c50c1f 10592 unsigned int bytes_read, extended_len;
c906108c 10593 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10594 CORE_ADDR baseaddr;
10595 struct objfile *objfile = cu->objfile;
fbf65064 10596 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10597 const int decode_for_pst_p = (pst != NULL);
cb1df416 10598 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
10599 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10600 = record_line;
e142c38c
DJ
10601
10602 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10603
debd256d
JB
10604 line_ptr = lh->statement_program_start;
10605 line_end = lh->statement_program_end;
c906108c
SS
10606
10607 /* Read the statement sequences until there's nothing left. */
10608 while (line_ptr < line_end)
10609 {
10610 /* state machine registers */
10611 CORE_ADDR address = 0;
10612 unsigned int file = 1;
10613 unsigned int line = 1;
10614 unsigned int column = 0;
debd256d 10615 int is_stmt = lh->default_is_stmt;
c906108c
SS
10616 int basic_block = 0;
10617 int end_sequence = 0;
fbf65064 10618 CORE_ADDR addr;
2dc7f7b3 10619 unsigned char op_index = 0;
c906108c 10620
aaa75496 10621 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10622 {
aaa75496 10623 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10624 /* lh->include_dirs and lh->file_names are 0-based, but the
10625 directory and file name numbers in the statement program
10626 are 1-based. */
10627 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10628 char *dir = NULL;
a738430d 10629
debd256d
JB
10630 if (fe->dir_index)
10631 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10632
10633 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10634 }
10635
a738430d 10636 /* Decode the table. */
c5aa993b 10637 while (!end_sequence)
c906108c
SS
10638 {
10639 op_code = read_1_byte (abfd, line_ptr);
10640 line_ptr += 1;
59205f5a
JB
10641 if (line_ptr > line_end)
10642 {
10643 dwarf2_debug_line_missing_end_sequence_complaint ();
10644 break;
10645 }
9aa1fe7e 10646
debd256d 10647 if (op_code >= lh->opcode_base)
6e70227d 10648 {
a738430d 10649 /* Special operand. */
debd256d 10650 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10651 address += (((op_index + (adj_opcode / lh->line_range))
10652 / lh->maximum_ops_per_instruction)
10653 * lh->minimum_instruction_length);
10654 op_index = ((op_index + (adj_opcode / lh->line_range))
10655 % lh->maximum_ops_per_instruction);
debd256d 10656 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10657 if (lh->num_file_names < file || file == 0)
25e43795 10658 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10659 /* For now we ignore lines not starting on an
10660 instruction boundary. */
10661 else if (op_index == 0)
25e43795
DJ
10662 {
10663 lh->file_names[file - 1].included_p = 1;
ca5f395d 10664 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10665 {
10666 if (last_subfile != current_subfile)
10667 {
10668 addr = gdbarch_addr_bits_remove (gdbarch, address);
10669 if (last_subfile)
c91513d8 10670 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10671 last_subfile = current_subfile;
10672 }
25e43795 10673 /* Append row to matrix using current values. */
fbf65064
UW
10674 addr = check_cu_functions (address, cu);
10675 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10676 (*p_record_line) (current_subfile, line, addr);
366da635 10677 }
25e43795 10678 }
ca5f395d 10679 basic_block = 0;
9aa1fe7e
GK
10680 }
10681 else switch (op_code)
c906108c
SS
10682 {
10683 case DW_LNS_extended_op:
3e43a32a
MS
10684 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10685 &bytes_read);
473b7be6 10686 line_ptr += bytes_read;
a8c50c1f 10687 extended_end = line_ptr + extended_len;
c906108c
SS
10688 extended_op = read_1_byte (abfd, line_ptr);
10689 line_ptr += 1;
10690 switch (extended_op)
10691 {
10692 case DW_LNE_end_sequence:
c91513d8 10693 p_record_line = record_line;
c906108c 10694 end_sequence = 1;
c906108c
SS
10695 break;
10696 case DW_LNE_set_address:
e7c27a73 10697 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
10698
10699 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10700 {
10701 /* This line table is for a function which has been
10702 GCd by the linker. Ignore it. PR gdb/12528 */
10703
10704 long line_offset
10705 = line_ptr - dwarf2_per_objfile->line.buffer;
10706
10707 complaint (&symfile_complaints,
10708 _(".debug_line address at offset 0x%lx is 0 "
10709 "[in module %s]"),
10710 line_offset, cu->objfile->name);
10711 p_record_line = noop_record_line;
10712 }
10713
2dc7f7b3 10714 op_index = 0;
107d2387
AC
10715 line_ptr += bytes_read;
10716 address += baseaddr;
c906108c
SS
10717 break;
10718 case DW_LNE_define_file:
debd256d
JB
10719 {
10720 char *cur_file;
10721 unsigned int dir_index, mod_time, length;
6e70227d 10722
3e43a32a
MS
10723 cur_file = read_direct_string (abfd, line_ptr,
10724 &bytes_read);
debd256d
JB
10725 line_ptr += bytes_read;
10726 dir_index =
10727 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10728 line_ptr += bytes_read;
10729 mod_time =
10730 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10731 line_ptr += bytes_read;
10732 length =
10733 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10734 line_ptr += bytes_read;
10735 add_file_name (lh, cur_file, dir_index, mod_time, length);
10736 }
c906108c 10737 break;
d0c6ba3d
CC
10738 case DW_LNE_set_discriminator:
10739 /* The discriminator is not interesting to the debugger;
10740 just ignore it. */
10741 line_ptr = extended_end;
10742 break;
c906108c 10743 default:
4d3c2250 10744 complaint (&symfile_complaints,
e2e0b3e5 10745 _("mangled .debug_line section"));
debd256d 10746 return;
c906108c 10747 }
a8c50c1f
DJ
10748 /* Make sure that we parsed the extended op correctly. If e.g.
10749 we expected a different address size than the producer used,
10750 we may have read the wrong number of bytes. */
10751 if (line_ptr != extended_end)
10752 {
10753 complaint (&symfile_complaints,
10754 _("mangled .debug_line section"));
10755 return;
10756 }
c906108c
SS
10757 break;
10758 case DW_LNS_copy:
59205f5a 10759 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10760 dwarf2_debug_line_missing_file_complaint ();
10761 else
366da635 10762 {
25e43795 10763 lh->file_names[file - 1].included_p = 1;
ca5f395d 10764 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10765 {
10766 if (last_subfile != current_subfile)
10767 {
10768 addr = gdbarch_addr_bits_remove (gdbarch, address);
10769 if (last_subfile)
c91513d8 10770 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10771 last_subfile = current_subfile;
10772 }
10773 addr = check_cu_functions (address, cu);
10774 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10775 (*p_record_line) (current_subfile, line, addr);
fbf65064 10776 }
366da635 10777 }
c906108c
SS
10778 basic_block = 0;
10779 break;
10780 case DW_LNS_advance_pc:
2dc7f7b3
TT
10781 {
10782 CORE_ADDR adjust
10783 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10784
10785 address += (((op_index + adjust)
10786 / lh->maximum_ops_per_instruction)
10787 * lh->minimum_instruction_length);
10788 op_index = ((op_index + adjust)
10789 % lh->maximum_ops_per_instruction);
10790 line_ptr += bytes_read;
10791 }
c906108c
SS
10792 break;
10793 case DW_LNS_advance_line:
10794 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10795 line_ptr += bytes_read;
10796 break;
10797 case DW_LNS_set_file:
debd256d 10798 {
a738430d
MK
10799 /* The arrays lh->include_dirs and lh->file_names are
10800 0-based, but the directory and file name numbers in
10801 the statement program are 1-based. */
debd256d 10802 struct file_entry *fe;
4f1520fb 10803 char *dir = NULL;
a738430d 10804
debd256d
JB
10805 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10806 line_ptr += bytes_read;
59205f5a 10807 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10808 dwarf2_debug_line_missing_file_complaint ();
10809 else
10810 {
10811 fe = &lh->file_names[file - 1];
10812 if (fe->dir_index)
10813 dir = lh->include_dirs[fe->dir_index - 1];
10814 if (!decode_for_pst_p)
10815 {
10816 last_subfile = current_subfile;
10817 dwarf2_start_subfile (fe->name, dir, comp_dir);
10818 }
10819 }
debd256d 10820 }
c906108c
SS
10821 break;
10822 case DW_LNS_set_column:
10823 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10824 line_ptr += bytes_read;
10825 break;
10826 case DW_LNS_negate_stmt:
10827 is_stmt = (!is_stmt);
10828 break;
10829 case DW_LNS_set_basic_block:
10830 basic_block = 1;
10831 break;
c2c6d25f
JM
10832 /* Add to the address register of the state machine the
10833 address increment value corresponding to special opcode
a738430d
MK
10834 255. I.e., this value is scaled by the minimum
10835 instruction length since special opcode 255 would have
b021a221 10836 scaled the increment. */
c906108c 10837 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10838 {
10839 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10840
10841 address += (((op_index + adjust)
10842 / lh->maximum_ops_per_instruction)
10843 * lh->minimum_instruction_length);
10844 op_index = ((op_index + adjust)
10845 % lh->maximum_ops_per_instruction);
10846 }
c906108c
SS
10847 break;
10848 case DW_LNS_fixed_advance_pc:
10849 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10850 op_index = 0;
c906108c
SS
10851 line_ptr += 2;
10852 break;
9aa1fe7e 10853 default:
a738430d
MK
10854 {
10855 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10856 int i;
a738430d 10857
debd256d 10858 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10859 {
10860 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10861 line_ptr += bytes_read;
10862 }
10863 }
c906108c
SS
10864 }
10865 }
59205f5a
JB
10866 if (lh->num_file_names < file || file == 0)
10867 dwarf2_debug_line_missing_file_complaint ();
10868 else
10869 {
10870 lh->file_names[file - 1].included_p = 1;
10871 if (!decode_for_pst_p)
fbf65064
UW
10872 {
10873 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 10874 (*p_record_line) (current_subfile, 0, addr);
fbf65064 10875 }
59205f5a 10876 }
c906108c 10877 }
aaa75496
JB
10878
10879 if (decode_for_pst_p)
10880 {
10881 int file_index;
10882
10883 /* Now that we're done scanning the Line Header Program, we can
10884 create the psymtab of each included file. */
10885 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10886 if (lh->file_names[file_index].included_p == 1)
10887 {
c6da4cef
DE
10888 char *include_name =
10889 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10890 if (include_name != NULL)
aaa75496
JB
10891 dwarf2_create_include_psymtab (include_name, pst, objfile);
10892 }
10893 }
cb1df416
DJ
10894 else
10895 {
10896 /* Make sure a symtab is created for every file, even files
10897 which contain only variables (i.e. no code with associated
10898 line numbers). */
10899
10900 int i;
10901 struct file_entry *fe;
10902
10903 for (i = 0; i < lh->num_file_names; i++)
10904 {
10905 char *dir = NULL;
9a619af0 10906
cb1df416
DJ
10907 fe = &lh->file_names[i];
10908 if (fe->dir_index)
10909 dir = lh->include_dirs[fe->dir_index - 1];
10910 dwarf2_start_subfile (fe->name, dir, comp_dir);
10911
10912 /* Skip the main file; we don't need it, and it must be
10913 allocated last, so that it will show up before the
10914 non-primary symtabs in the objfile's symtab list. */
10915 if (current_subfile == first_subfile)
10916 continue;
10917
10918 if (current_subfile->symtab == NULL)
10919 current_subfile->symtab = allocate_symtab (current_subfile->name,
10920 cu->objfile);
10921 fe->symtab = current_subfile->symtab;
10922 }
10923 }
c906108c
SS
10924}
10925
10926/* Start a subfile for DWARF. FILENAME is the name of the file and
10927 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10928 or NULL if not known. COMP_DIR is the compilation directory for the
10929 linetable's compilation unit or NULL if not known.
c906108c
SS
10930 This routine tries to keep line numbers from identical absolute and
10931 relative file names in a common subfile.
10932
10933 Using the `list' example from the GDB testsuite, which resides in
10934 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10935 of /srcdir/list0.c yields the following debugging information for list0.c:
10936
c5aa993b
JM
10937 DW_AT_name: /srcdir/list0.c
10938 DW_AT_comp_dir: /compdir
357e46e7 10939 files.files[0].name: list0.h
c5aa993b 10940 files.files[0].dir: /srcdir
357e46e7 10941 files.files[1].name: list0.c
c5aa993b 10942 files.files[1].dir: /srcdir
c906108c
SS
10943
10944 The line number information for list0.c has to end up in a single
4f1520fb
FR
10945 subfile, so that `break /srcdir/list0.c:1' works as expected.
10946 start_subfile will ensure that this happens provided that we pass the
10947 concatenation of files.files[1].dir and files.files[1].name as the
10948 subfile's name. */
c906108c
SS
10949
10950static void
3e43a32a
MS
10951dwarf2_start_subfile (char *filename, const char *dirname,
10952 const char *comp_dir)
c906108c 10953{
4f1520fb
FR
10954 char *fullname;
10955
10956 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10957 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10958 second argument to start_subfile. To be consistent, we do the
10959 same here. In order not to lose the line information directory,
10960 we concatenate it to the filename when it makes sense.
10961 Note that the Dwarf3 standard says (speaking of filenames in line
10962 information): ``The directory index is ignored for file names
10963 that represent full path names''. Thus ignoring dirname in the
10964 `else' branch below isn't an issue. */
c906108c 10965
d5166ae1 10966 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10967 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10968 else
10969 fullname = filename;
c906108c 10970
4f1520fb
FR
10971 start_subfile (fullname, comp_dir);
10972
10973 if (fullname != filename)
10974 xfree (fullname);
c906108c
SS
10975}
10976
4c2df51b
DJ
10977static void
10978var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10979 struct dwarf2_cu *cu)
4c2df51b 10980{
e7c27a73
DJ
10981 struct objfile *objfile = cu->objfile;
10982 struct comp_unit_head *cu_header = &cu->header;
10983
4c2df51b
DJ
10984 /* NOTE drow/2003-01-30: There used to be a comment and some special
10985 code here to turn a symbol with DW_AT_external and a
10986 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10987 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10988 with some versions of binutils) where shared libraries could have
10989 relocations against symbols in their debug information - the
10990 minimal symbol would have the right address, but the debug info
10991 would not. It's no longer necessary, because we will explicitly
10992 apply relocations when we read in the debug information now. */
10993
10994 /* A DW_AT_location attribute with no contents indicates that a
10995 variable has been optimized away. */
10996 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10997 {
10998 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10999 return;
11000 }
11001
11002 /* Handle one degenerate form of location expression specially, to
11003 preserve GDB's previous behavior when section offsets are
11004 specified. If this is just a DW_OP_addr then mark this symbol
11005 as LOC_STATIC. */
11006
11007 if (attr_form_is_block (attr)
11008 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11009 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11010 {
891d2f0b 11011 unsigned int dummy;
4c2df51b
DJ
11012
11013 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11014 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11015 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11016 fixup_symbol_section (sym, objfile);
11017 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11018 SYMBOL_SECTION (sym));
4c2df51b
DJ
11019 return;
11020 }
11021
11022 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11023 expression evaluator, and use LOC_COMPUTED only when necessary
11024 (i.e. when the value of a register or memory location is
11025 referenced, or a thread-local block, etc.). Then again, it might
11026 not be worthwhile. I'm assuming that it isn't unless performance
11027 or memory numbers show me otherwise. */
11028
e7c27a73 11029 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11030 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11031
11032 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11033 cu->has_loclist = 1;
4c2df51b
DJ
11034}
11035
c906108c
SS
11036/* Given a pointer to a DWARF information entry, figure out if we need
11037 to make a symbol table entry for it, and if so, create a new entry
11038 and return a pointer to it.
11039 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11040 used the passed type.
11041 If SPACE is not NULL, use it to hold the new symbol. If it is
11042 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11043
11044static struct symbol *
34eaf542
TT
11045new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11046 struct symbol *space)
c906108c 11047{
e7c27a73 11048 struct objfile *objfile = cu->objfile;
c906108c
SS
11049 struct symbol *sym = NULL;
11050 char *name;
11051 struct attribute *attr = NULL;
11052 struct attribute *attr2 = NULL;
e142c38c 11053 CORE_ADDR baseaddr;
e37fd15a
SW
11054 struct pending **list_to_add = NULL;
11055
edb3359d 11056 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11057
11058 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11059
94af9270 11060 name = dwarf2_name (die, cu);
c906108c
SS
11061 if (name)
11062 {
94af9270 11063 const char *linkagename;
34eaf542 11064 int suppress_add = 0;
94af9270 11065
34eaf542
TT
11066 if (space)
11067 sym = space;
11068 else
11069 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11070 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11071
11072 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11073 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11074 linkagename = dwarf2_physname (name, die, cu);
11075 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11076
f55ee35c
JK
11077 /* Fortran does not have mangling standard and the mangling does differ
11078 between gfortran, iFort etc. */
11079 if (cu->language == language_fortran
b250c185 11080 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11081 symbol_set_demangled_name (&(sym->ginfo),
11082 (char *) dwarf2_full_name (name, die, cu),
11083 NULL);
f55ee35c 11084
c906108c 11085 /* Default assumptions.
c5aa993b 11086 Use the passed type or decode it from the die. */
176620f1 11087 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11088 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11089 if (type != NULL)
11090 SYMBOL_TYPE (sym) = type;
11091 else
e7c27a73 11092 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11093 attr = dwarf2_attr (die,
11094 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11095 cu);
c906108c
SS
11096 if (attr)
11097 {
11098 SYMBOL_LINE (sym) = DW_UNSND (attr);
11099 }
cb1df416 11100
edb3359d
DJ
11101 attr = dwarf2_attr (die,
11102 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11103 cu);
cb1df416
DJ
11104 if (attr)
11105 {
11106 int file_index = DW_UNSND (attr);
9a619af0 11107
cb1df416
DJ
11108 if (cu->line_header == NULL
11109 || file_index > cu->line_header->num_file_names)
11110 complaint (&symfile_complaints,
11111 _("file index out of range"));
1c3d648d 11112 else if (file_index > 0)
cb1df416
DJ
11113 {
11114 struct file_entry *fe;
9a619af0 11115
cb1df416
DJ
11116 fe = &cu->line_header->file_names[file_index - 1];
11117 SYMBOL_SYMTAB (sym) = fe->symtab;
11118 }
11119 }
11120
c906108c
SS
11121 switch (die->tag)
11122 {
11123 case DW_TAG_label:
e142c38c 11124 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11125 if (attr)
11126 {
11127 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11128 }
0f5238ed
TT
11129 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11130 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11131 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11132 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11133 break;
11134 case DW_TAG_subprogram:
11135 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11136 finish_block. */
11137 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11138 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11139 if ((attr2 && (DW_UNSND (attr2) != 0))
11140 || cu->language == language_ada)
c906108c 11141 {
2cfa0c8d
JB
11142 /* Subprograms marked external are stored as a global symbol.
11143 Ada subprograms, whether marked external or not, are always
11144 stored as a global symbol, because we want to be able to
11145 access them globally. For instance, we want to be able
11146 to break on a nested subprogram without having to
11147 specify the context. */
e37fd15a 11148 list_to_add = &global_symbols;
c906108c
SS
11149 }
11150 else
11151 {
e37fd15a 11152 list_to_add = cu->list_in_scope;
c906108c
SS
11153 }
11154 break;
edb3359d
DJ
11155 case DW_TAG_inlined_subroutine:
11156 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11157 finish_block. */
11158 SYMBOL_CLASS (sym) = LOC_BLOCK;
11159 SYMBOL_INLINED (sym) = 1;
11160 /* Do not add the symbol to any lists. It will be found via
11161 BLOCK_FUNCTION from the blockvector. */
11162 break;
34eaf542
TT
11163 case DW_TAG_template_value_param:
11164 suppress_add = 1;
11165 /* Fall through. */
72929c62 11166 case DW_TAG_constant:
c906108c 11167 case DW_TAG_variable:
254e6b9e 11168 case DW_TAG_member:
0963b4bd
MS
11169 /* Compilation with minimal debug info may result in
11170 variables with missing type entries. Change the
11171 misleading `void' type to something sensible. */
c906108c 11172 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11173 SYMBOL_TYPE (sym)
46bf5051 11174 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11175
e142c38c 11176 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11177 /* In the case of DW_TAG_member, we should only be called for
11178 static const members. */
11179 if (die->tag == DW_TAG_member)
11180 {
3863f96c
DE
11181 /* dwarf2_add_field uses die_is_declaration,
11182 so we do the same. */
254e6b9e
DE
11183 gdb_assert (die_is_declaration (die, cu));
11184 gdb_assert (attr);
11185 }
c906108c
SS
11186 if (attr)
11187 {
e7c27a73 11188 dwarf2_const_value (attr, sym, cu);
e142c38c 11189 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11190 if (!suppress_add)
34eaf542
TT
11191 {
11192 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11193 list_to_add = &global_symbols;
34eaf542 11194 else
e37fd15a 11195 list_to_add = cu->list_in_scope;
34eaf542 11196 }
c906108c
SS
11197 break;
11198 }
e142c38c 11199 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11200 if (attr)
11201 {
e7c27a73 11202 var_decode_location (attr, sym, cu);
e142c38c 11203 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11204 if (SYMBOL_CLASS (sym) == LOC_STATIC
11205 && SYMBOL_VALUE_ADDRESS (sym) == 0
11206 && !dwarf2_per_objfile->has_section_at_zero)
11207 {
11208 /* When a static variable is eliminated by the linker,
11209 the corresponding debug information is not stripped
11210 out, but the variable address is set to null;
11211 do not add such variables into symbol table. */
11212 }
11213 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11214 {
f55ee35c
JK
11215 /* Workaround gfortran PR debug/40040 - it uses
11216 DW_AT_location for variables in -fPIC libraries which may
11217 get overriden by other libraries/executable and get
11218 a different address. Resolve it by the minimal symbol
11219 which may come from inferior's executable using copy
11220 relocation. Make this workaround only for gfortran as for
11221 other compilers GDB cannot guess the minimal symbol
11222 Fortran mangling kind. */
11223 if (cu->language == language_fortran && die->parent
11224 && die->parent->tag == DW_TAG_module
11225 && cu->producer
11226 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11227 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11228
1c809c68
TT
11229 /* A variable with DW_AT_external is never static,
11230 but it may be block-scoped. */
11231 list_to_add = (cu->list_in_scope == &file_symbols
11232 ? &global_symbols : cu->list_in_scope);
1c809c68 11233 }
c906108c 11234 else
e37fd15a 11235 list_to_add = cu->list_in_scope;
c906108c
SS
11236 }
11237 else
11238 {
11239 /* We do not know the address of this symbol.
c5aa993b
JM
11240 If it is an external symbol and we have type information
11241 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11242 The address of the variable will then be determined from
11243 the minimal symbol table whenever the variable is
11244 referenced. */
e142c38c 11245 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11246 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11247 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11248 {
0fe7935b
DJ
11249 /* A variable with DW_AT_external is never static, but it
11250 may be block-scoped. */
11251 list_to_add = (cu->list_in_scope == &file_symbols
11252 ? &global_symbols : cu->list_in_scope);
11253
c906108c 11254 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11255 }
442ddf59
JK
11256 else if (!die_is_declaration (die, cu))
11257 {
11258 /* Use the default LOC_OPTIMIZED_OUT class. */
11259 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11260 if (!suppress_add)
11261 list_to_add = cu->list_in_scope;
442ddf59 11262 }
c906108c
SS
11263 }
11264 break;
11265 case DW_TAG_formal_parameter:
edb3359d
DJ
11266 /* If we are inside a function, mark this as an argument. If
11267 not, we might be looking at an argument to an inlined function
11268 when we do not have enough information to show inlined frames;
11269 pretend it's a local variable in that case so that the user can
11270 still see it. */
11271 if (context_stack_depth > 0
11272 && context_stack[context_stack_depth - 1].name != NULL)
11273 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11274 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11275 if (attr)
11276 {
e7c27a73 11277 var_decode_location (attr, sym, cu);
c906108c 11278 }
e142c38c 11279 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11280 if (attr)
11281 {
e7c27a73 11282 dwarf2_const_value (attr, sym, cu);
c906108c 11283 }
f346a30d
PM
11284 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11285 if (attr && DW_UNSND (attr))
11286 {
11287 struct type *ref_type;
11288
11289 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11290 SYMBOL_TYPE (sym) = ref_type;
11291 }
11292
e37fd15a 11293 list_to_add = cu->list_in_scope;
c906108c
SS
11294 break;
11295 case DW_TAG_unspecified_parameters:
11296 /* From varargs functions; gdb doesn't seem to have any
11297 interest in this information, so just ignore it for now.
11298 (FIXME?) */
11299 break;
34eaf542
TT
11300 case DW_TAG_template_type_param:
11301 suppress_add = 1;
11302 /* Fall through. */
c906108c 11303 case DW_TAG_class_type:
680b30c7 11304 case DW_TAG_interface_type:
c906108c
SS
11305 case DW_TAG_structure_type:
11306 case DW_TAG_union_type:
72019c9c 11307 case DW_TAG_set_type:
c906108c
SS
11308 case DW_TAG_enumeration_type:
11309 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11310 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11311
63d06c5c 11312 {
987504bb 11313 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11314 really ever be static objects: otherwise, if you try
11315 to, say, break of a class's method and you're in a file
11316 which doesn't mention that class, it won't work unless
11317 the check for all static symbols in lookup_symbol_aux
11318 saves you. See the OtherFileClass tests in
11319 gdb.c++/namespace.exp. */
11320
e37fd15a 11321 if (!suppress_add)
34eaf542 11322 {
34eaf542
TT
11323 list_to_add = (cu->list_in_scope == &file_symbols
11324 && (cu->language == language_cplus
11325 || cu->language == language_java)
11326 ? &global_symbols : cu->list_in_scope);
63d06c5c 11327
64382290
TT
11328 /* The semantics of C++ state that "struct foo {
11329 ... }" also defines a typedef for "foo". A Java
11330 class declaration also defines a typedef for the
11331 class. */
11332 if (cu->language == language_cplus
11333 || cu->language == language_java
11334 || cu->language == language_ada)
11335 {
11336 /* The symbol's name is already allocated along
11337 with this objfile, so we don't need to
11338 duplicate it for the type. */
11339 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11340 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11341 }
63d06c5c
DC
11342 }
11343 }
c906108c
SS
11344 break;
11345 case DW_TAG_typedef:
63d06c5c
DC
11346 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11347 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11348 list_to_add = cu->list_in_scope;
63d06c5c 11349 break;
c906108c 11350 case DW_TAG_base_type:
a02abb62 11351 case DW_TAG_subrange_type:
c906108c 11352 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11353 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11354 list_to_add = cu->list_in_scope;
c906108c
SS
11355 break;
11356 case DW_TAG_enumerator:
e142c38c 11357 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11358 if (attr)
11359 {
e7c27a73 11360 dwarf2_const_value (attr, sym, cu);
c906108c 11361 }
63d06c5c
DC
11362 {
11363 /* NOTE: carlton/2003-11-10: See comment above in the
11364 DW_TAG_class_type, etc. block. */
11365
e142c38c 11366 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11367 && (cu->language == language_cplus
11368 || cu->language == language_java)
e142c38c 11369 ? &global_symbols : cu->list_in_scope);
63d06c5c 11370 }
c906108c 11371 break;
5c4e30ca
DC
11372 case DW_TAG_namespace:
11373 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11374 list_to_add = &global_symbols;
5c4e30ca 11375 break;
c906108c
SS
11376 default:
11377 /* Not a tag we recognize. Hopefully we aren't processing
11378 trash data, but since we must specifically ignore things
11379 we don't recognize, there is nothing else we should do at
0963b4bd 11380 this point. */
e2e0b3e5 11381 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11382 dwarf_tag_name (die->tag));
c906108c
SS
11383 break;
11384 }
df8a16a1 11385
e37fd15a
SW
11386 if (suppress_add)
11387 {
11388 sym->hash_next = objfile->template_symbols;
11389 objfile->template_symbols = sym;
11390 list_to_add = NULL;
11391 }
11392
11393 if (list_to_add != NULL)
11394 add_symbol_to_list (sym, list_to_add);
11395
df8a16a1
DJ
11396 /* For the benefit of old versions of GCC, check for anonymous
11397 namespaces based on the demangled name. */
11398 if (!processing_has_namespace_info
94af9270 11399 && cu->language == language_cplus)
df8a16a1 11400 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
11401 }
11402 return (sym);
11403}
11404
34eaf542
TT
11405/* A wrapper for new_symbol_full that always allocates a new symbol. */
11406
11407static struct symbol *
11408new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11409{
11410 return new_symbol_full (die, type, cu, NULL);
11411}
11412
98bfdba5
PA
11413/* Given an attr with a DW_FORM_dataN value in host byte order,
11414 zero-extend it as appropriate for the symbol's type. The DWARF
11415 standard (v4) is not entirely clear about the meaning of using
11416 DW_FORM_dataN for a constant with a signed type, where the type is
11417 wider than the data. The conclusion of a discussion on the DWARF
11418 list was that this is unspecified. We choose to always zero-extend
11419 because that is the interpretation long in use by GCC. */
c906108c 11420
98bfdba5
PA
11421static gdb_byte *
11422dwarf2_const_value_data (struct attribute *attr, struct type *type,
11423 const char *name, struct obstack *obstack,
11424 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11425{
e7c27a73 11426 struct objfile *objfile = cu->objfile;
e17a4113
UW
11427 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11428 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11429 LONGEST l = DW_UNSND (attr);
11430
11431 if (bits < sizeof (*value) * 8)
11432 {
11433 l &= ((LONGEST) 1 << bits) - 1;
11434 *value = l;
11435 }
11436 else if (bits == sizeof (*value) * 8)
11437 *value = l;
11438 else
11439 {
11440 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11441 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11442 return bytes;
11443 }
11444
11445 return NULL;
11446}
11447
11448/* Read a constant value from an attribute. Either set *VALUE, or if
11449 the value does not fit in *VALUE, set *BYTES - either already
11450 allocated on the objfile obstack, or newly allocated on OBSTACK,
11451 or, set *BATON, if we translated the constant to a location
11452 expression. */
11453
11454static void
11455dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11456 const char *name, struct obstack *obstack,
11457 struct dwarf2_cu *cu,
11458 long *value, gdb_byte **bytes,
11459 struct dwarf2_locexpr_baton **baton)
11460{
11461 struct objfile *objfile = cu->objfile;
11462 struct comp_unit_head *cu_header = &cu->header;
c906108c 11463 struct dwarf_block *blk;
98bfdba5
PA
11464 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11465 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11466
11467 *value = 0;
11468 *bytes = NULL;
11469 *baton = NULL;
c906108c
SS
11470
11471 switch (attr->form)
11472 {
11473 case DW_FORM_addr:
ac56253d 11474 {
ac56253d
TT
11475 gdb_byte *data;
11476
98bfdba5
PA
11477 if (TYPE_LENGTH (type) != cu_header->addr_size)
11478 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11479 cu_header->addr_size,
98bfdba5 11480 TYPE_LENGTH (type));
ac56253d
TT
11481 /* Symbols of this form are reasonably rare, so we just
11482 piggyback on the existing location code rather than writing
11483 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11484 *baton = obstack_alloc (&objfile->objfile_obstack,
11485 sizeof (struct dwarf2_locexpr_baton));
11486 (*baton)->per_cu = cu->per_cu;
11487 gdb_assert ((*baton)->per_cu);
ac56253d 11488
98bfdba5
PA
11489 (*baton)->size = 2 + cu_header->addr_size;
11490 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11491 (*baton)->data = data;
ac56253d
TT
11492
11493 data[0] = DW_OP_addr;
11494 store_unsigned_integer (&data[1], cu_header->addr_size,
11495 byte_order, DW_ADDR (attr));
11496 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11497 }
c906108c 11498 break;
4ac36638 11499 case DW_FORM_string:
93b5768b 11500 case DW_FORM_strp:
98bfdba5
PA
11501 /* DW_STRING is already allocated on the objfile obstack, point
11502 directly to it. */
11503 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11504 break;
c906108c
SS
11505 case DW_FORM_block1:
11506 case DW_FORM_block2:
11507 case DW_FORM_block4:
11508 case DW_FORM_block:
2dc7f7b3 11509 case DW_FORM_exprloc:
c906108c 11510 blk = DW_BLOCK (attr);
98bfdba5
PA
11511 if (TYPE_LENGTH (type) != blk->size)
11512 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11513 TYPE_LENGTH (type));
11514 *bytes = blk->data;
c906108c 11515 break;
2df3850c
JM
11516
11517 /* The DW_AT_const_value attributes are supposed to carry the
11518 symbol's value "represented as it would be on the target
11519 architecture." By the time we get here, it's already been
11520 converted to host endianness, so we just need to sign- or
11521 zero-extend it as appropriate. */
11522 case DW_FORM_data1:
3e43a32a
MS
11523 *bytes = dwarf2_const_value_data (attr, type, name,
11524 obstack, cu, value, 8);
2df3850c 11525 break;
c906108c 11526 case DW_FORM_data2:
3e43a32a
MS
11527 *bytes = dwarf2_const_value_data (attr, type, name,
11528 obstack, cu, value, 16);
2df3850c 11529 break;
c906108c 11530 case DW_FORM_data4:
3e43a32a
MS
11531 *bytes = dwarf2_const_value_data (attr, type, name,
11532 obstack, cu, value, 32);
2df3850c 11533 break;
c906108c 11534 case DW_FORM_data8:
3e43a32a
MS
11535 *bytes = dwarf2_const_value_data (attr, type, name,
11536 obstack, cu, value, 64);
2df3850c
JM
11537 break;
11538
c906108c 11539 case DW_FORM_sdata:
98bfdba5 11540 *value = DW_SND (attr);
2df3850c
JM
11541 break;
11542
c906108c 11543 case DW_FORM_udata:
98bfdba5 11544 *value = DW_UNSND (attr);
c906108c 11545 break;
2df3850c 11546
c906108c 11547 default:
4d3c2250 11548 complaint (&symfile_complaints,
e2e0b3e5 11549 _("unsupported const value attribute form: '%s'"),
4d3c2250 11550 dwarf_form_name (attr->form));
98bfdba5 11551 *value = 0;
c906108c
SS
11552 break;
11553 }
11554}
11555
2df3850c 11556
98bfdba5
PA
11557/* Copy constant value from an attribute to a symbol. */
11558
2df3850c 11559static void
98bfdba5
PA
11560dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11561 struct dwarf2_cu *cu)
2df3850c 11562{
98bfdba5
PA
11563 struct objfile *objfile = cu->objfile;
11564 struct comp_unit_head *cu_header = &cu->header;
11565 long value;
11566 gdb_byte *bytes;
11567 struct dwarf2_locexpr_baton *baton;
2df3850c 11568
98bfdba5
PA
11569 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11570 SYMBOL_PRINT_NAME (sym),
11571 &objfile->objfile_obstack, cu,
11572 &value, &bytes, &baton);
2df3850c 11573
98bfdba5
PA
11574 if (baton != NULL)
11575 {
11576 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11577 SYMBOL_LOCATION_BATON (sym) = baton;
11578 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11579 }
11580 else if (bytes != NULL)
11581 {
11582 SYMBOL_VALUE_BYTES (sym) = bytes;
11583 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11584 }
11585 else
11586 {
11587 SYMBOL_VALUE (sym) = value;
11588 SYMBOL_CLASS (sym) = LOC_CONST;
11589 }
2df3850c
JM
11590}
11591
c906108c
SS
11592/* Return the type of the die in question using its DW_AT_type attribute. */
11593
11594static struct type *
e7c27a73 11595die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11596{
c906108c 11597 struct attribute *type_attr;
c906108c 11598
e142c38c 11599 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11600 if (!type_attr)
11601 {
11602 /* A missing DW_AT_type represents a void type. */
46bf5051 11603 return objfile_type (cu->objfile)->builtin_void;
c906108c 11604 }
348e048f 11605
673bfd45 11606 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11607}
11608
b4ba55a1
JB
11609/* True iff CU's producer generates GNAT Ada auxiliary information
11610 that allows to find parallel types through that information instead
11611 of having to do expensive parallel lookups by type name. */
11612
11613static int
11614need_gnat_info (struct dwarf2_cu *cu)
11615{
11616 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11617 of GNAT produces this auxiliary information, without any indication
11618 that it is produced. Part of enhancing the FSF version of GNAT
11619 to produce that information will be to put in place an indicator
11620 that we can use in order to determine whether the descriptive type
11621 info is available or not. One suggestion that has been made is
11622 to use a new attribute, attached to the CU die. For now, assume
11623 that the descriptive type info is not available. */
11624 return 0;
11625}
11626
b4ba55a1
JB
11627/* Return the auxiliary type of the die in question using its
11628 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11629 attribute is not present. */
11630
11631static struct type *
11632die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11633{
b4ba55a1 11634 struct attribute *type_attr;
b4ba55a1
JB
11635
11636 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11637 if (!type_attr)
11638 return NULL;
11639
673bfd45 11640 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11641}
11642
11643/* If DIE has a descriptive_type attribute, then set the TYPE's
11644 descriptive type accordingly. */
11645
11646static void
11647set_descriptive_type (struct type *type, struct die_info *die,
11648 struct dwarf2_cu *cu)
11649{
11650 struct type *descriptive_type = die_descriptive_type (die, cu);
11651
11652 if (descriptive_type)
11653 {
11654 ALLOCATE_GNAT_AUX_TYPE (type);
11655 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11656 }
11657}
11658
c906108c
SS
11659/* Return the containing type of the die in question using its
11660 DW_AT_containing_type attribute. */
11661
11662static struct type *
e7c27a73 11663die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11664{
c906108c 11665 struct attribute *type_attr;
c906108c 11666
e142c38c 11667 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11668 if (!type_attr)
11669 error (_("Dwarf Error: Problem turning containing type into gdb type "
11670 "[in module %s]"), cu->objfile->name);
11671
673bfd45 11672 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11673}
11674
673bfd45
DE
11675/* Look up the type of DIE in CU using its type attribute ATTR.
11676 If there is no type substitute an error marker. */
11677
c906108c 11678static struct type *
673bfd45
DE
11679lookup_die_type (struct die_info *die, struct attribute *attr,
11680 struct dwarf2_cu *cu)
c906108c 11681{
f792889a
DJ
11682 struct type *this_type;
11683
673bfd45
DE
11684 /* First see if we have it cached. */
11685
11686 if (is_ref_attr (attr))
11687 {
11688 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11689
11690 this_type = get_die_type_at_offset (offset, cu->per_cu);
11691 }
55f1336d 11692 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
11693 {
11694 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11695 struct dwarf2_cu *sig_cu;
11696 unsigned int offset;
11697
11698 /* sig_type will be NULL if the signatured type is missing from
11699 the debug info. */
11700 if (sig_type == NULL)
11701 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11702 "at 0x%x [in module %s]"),
11703 die->offset, cu->objfile->name);
11704
11705 gdb_assert (sig_type->per_cu.from_debug_types);
11706 offset = sig_type->offset + sig_type->type_offset;
11707 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11708 }
11709 else
11710 {
11711 dump_die_for_error (die);
11712 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11713 dwarf_attr_name (attr->name), cu->objfile->name);
11714 }
11715
11716 /* If not cached we need to read it in. */
11717
11718 if (this_type == NULL)
11719 {
11720 struct die_info *type_die;
11721 struct dwarf2_cu *type_cu = cu;
11722
11723 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11724 /* If the type is cached, we should have found it above. */
11725 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11726 this_type = read_type_die_1 (type_die, type_cu);
11727 }
11728
11729 /* If we still don't have a type use an error marker. */
11730
11731 if (this_type == NULL)
c906108c 11732 {
b00fdb78
TT
11733 char *message, *saved;
11734
11735 /* read_type_die already issued a complaint. */
11736 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11737 cu->objfile->name,
11738 cu->header.offset,
11739 die->offset);
11740 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11741 message, strlen (message));
11742 xfree (message);
11743
11744 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11745 }
673bfd45 11746
f792889a 11747 return this_type;
c906108c
SS
11748}
11749
673bfd45
DE
11750/* Return the type in DIE, CU.
11751 Returns NULL for invalid types.
11752
11753 This first does a lookup in the appropriate type_hash table,
11754 and only reads the die in if necessary.
11755
11756 NOTE: This can be called when reading in partial or full symbols. */
11757
f792889a 11758static struct type *
e7c27a73 11759read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11760{
f792889a
DJ
11761 struct type *this_type;
11762
11763 this_type = get_die_type (die, cu);
11764 if (this_type)
11765 return this_type;
11766
673bfd45
DE
11767 return read_type_die_1 (die, cu);
11768}
11769
11770/* Read the type in DIE, CU.
11771 Returns NULL for invalid types. */
11772
11773static struct type *
11774read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11775{
11776 struct type *this_type = NULL;
11777
c906108c
SS
11778 switch (die->tag)
11779 {
11780 case DW_TAG_class_type:
680b30c7 11781 case DW_TAG_interface_type:
c906108c
SS
11782 case DW_TAG_structure_type:
11783 case DW_TAG_union_type:
f792889a 11784 this_type = read_structure_type (die, cu);
c906108c
SS
11785 break;
11786 case DW_TAG_enumeration_type:
f792889a 11787 this_type = read_enumeration_type (die, cu);
c906108c
SS
11788 break;
11789 case DW_TAG_subprogram:
11790 case DW_TAG_subroutine_type:
edb3359d 11791 case DW_TAG_inlined_subroutine:
f792889a 11792 this_type = read_subroutine_type (die, cu);
c906108c
SS
11793 break;
11794 case DW_TAG_array_type:
f792889a 11795 this_type = read_array_type (die, cu);
c906108c 11796 break;
72019c9c 11797 case DW_TAG_set_type:
f792889a 11798 this_type = read_set_type (die, cu);
72019c9c 11799 break;
c906108c 11800 case DW_TAG_pointer_type:
f792889a 11801 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11802 break;
11803 case DW_TAG_ptr_to_member_type:
f792889a 11804 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11805 break;
11806 case DW_TAG_reference_type:
f792889a 11807 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11808 break;
11809 case DW_TAG_const_type:
f792889a 11810 this_type = read_tag_const_type (die, cu);
c906108c
SS
11811 break;
11812 case DW_TAG_volatile_type:
f792889a 11813 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11814 break;
11815 case DW_TAG_string_type:
f792889a 11816 this_type = read_tag_string_type (die, cu);
c906108c
SS
11817 break;
11818 case DW_TAG_typedef:
f792889a 11819 this_type = read_typedef (die, cu);
c906108c 11820 break;
a02abb62 11821 case DW_TAG_subrange_type:
f792889a 11822 this_type = read_subrange_type (die, cu);
a02abb62 11823 break;
c906108c 11824 case DW_TAG_base_type:
f792889a 11825 this_type = read_base_type (die, cu);
c906108c 11826 break;
81a17f79 11827 case DW_TAG_unspecified_type:
f792889a 11828 this_type = read_unspecified_type (die, cu);
81a17f79 11829 break;
0114d602
DJ
11830 case DW_TAG_namespace:
11831 this_type = read_namespace_type (die, cu);
11832 break;
f55ee35c
JK
11833 case DW_TAG_module:
11834 this_type = read_module_type (die, cu);
11835 break;
c906108c 11836 default:
3e43a32a
MS
11837 complaint (&symfile_complaints,
11838 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11839 dwarf_tag_name (die->tag));
c906108c
SS
11840 break;
11841 }
63d06c5c 11842
f792889a 11843 return this_type;
63d06c5c
DC
11844}
11845
abc72ce4
DE
11846/* See if we can figure out if the class lives in a namespace. We do
11847 this by looking for a member function; its demangled name will
11848 contain namespace info, if there is any.
11849 Return the computed name or NULL.
11850 Space for the result is allocated on the objfile's obstack.
11851 This is the full-die version of guess_partial_die_structure_name.
11852 In this case we know DIE has no useful parent. */
11853
11854static char *
11855guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11856{
11857 struct die_info *spec_die;
11858 struct dwarf2_cu *spec_cu;
11859 struct die_info *child;
11860
11861 spec_cu = cu;
11862 spec_die = die_specification (die, &spec_cu);
11863 if (spec_die != NULL)
11864 {
11865 die = spec_die;
11866 cu = spec_cu;
11867 }
11868
11869 for (child = die->child;
11870 child != NULL;
11871 child = child->sibling)
11872 {
11873 if (child->tag == DW_TAG_subprogram)
11874 {
11875 struct attribute *attr;
11876
11877 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11878 if (attr == NULL)
11879 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11880 if (attr != NULL)
11881 {
11882 char *actual_name
11883 = language_class_name_from_physname (cu->language_defn,
11884 DW_STRING (attr));
11885 char *name = NULL;
11886
11887 if (actual_name != NULL)
11888 {
11889 char *die_name = dwarf2_name (die, cu);
11890
11891 if (die_name != NULL
11892 && strcmp (die_name, actual_name) != 0)
11893 {
11894 /* Strip off the class name from the full name.
11895 We want the prefix. */
11896 int die_name_len = strlen (die_name);
11897 int actual_name_len = strlen (actual_name);
11898
11899 /* Test for '::' as a sanity check. */
11900 if (actual_name_len > die_name_len + 2
3e43a32a
MS
11901 && actual_name[actual_name_len
11902 - die_name_len - 1] == ':')
abc72ce4
DE
11903 name =
11904 obsavestring (actual_name,
11905 actual_name_len - die_name_len - 2,
11906 &cu->objfile->objfile_obstack);
11907 }
11908 }
11909 xfree (actual_name);
11910 return name;
11911 }
11912 }
11913 }
11914
11915 return NULL;
11916}
11917
fdde2d81 11918/* Return the name of the namespace/class that DIE is defined within,
0114d602 11919 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11920
0114d602
DJ
11921 For example, if we're within the method foo() in the following
11922 code:
11923
11924 namespace N {
11925 class C {
11926 void foo () {
11927 }
11928 };
11929 }
11930
11931 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11932
11933static char *
e142c38c 11934determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11935{
0114d602
DJ
11936 struct die_info *parent, *spec_die;
11937 struct dwarf2_cu *spec_cu;
11938 struct type *parent_type;
63d06c5c 11939
f55ee35c
JK
11940 if (cu->language != language_cplus && cu->language != language_java
11941 && cu->language != language_fortran)
0114d602
DJ
11942 return "";
11943
11944 /* We have to be careful in the presence of DW_AT_specification.
11945 For example, with GCC 3.4, given the code
11946
11947 namespace N {
11948 void foo() {
11949 // Definition of N::foo.
11950 }
11951 }
11952
11953 then we'll have a tree of DIEs like this:
11954
11955 1: DW_TAG_compile_unit
11956 2: DW_TAG_namespace // N
11957 3: DW_TAG_subprogram // declaration of N::foo
11958 4: DW_TAG_subprogram // definition of N::foo
11959 DW_AT_specification // refers to die #3
11960
11961 Thus, when processing die #4, we have to pretend that we're in
11962 the context of its DW_AT_specification, namely the contex of die
11963 #3. */
11964 spec_cu = cu;
11965 spec_die = die_specification (die, &spec_cu);
11966 if (spec_die == NULL)
11967 parent = die->parent;
11968 else
63d06c5c 11969 {
0114d602
DJ
11970 parent = spec_die->parent;
11971 cu = spec_cu;
63d06c5c 11972 }
0114d602
DJ
11973
11974 if (parent == NULL)
11975 return "";
98bfdba5
PA
11976 else if (parent->building_fullname)
11977 {
11978 const char *name;
11979 const char *parent_name;
11980
11981 /* It has been seen on RealView 2.2 built binaries,
11982 DW_TAG_template_type_param types actually _defined_ as
11983 children of the parent class:
11984
11985 enum E {};
11986 template class <class Enum> Class{};
11987 Class<enum E> class_e;
11988
11989 1: DW_TAG_class_type (Class)
11990 2: DW_TAG_enumeration_type (E)
11991 3: DW_TAG_enumerator (enum1:0)
11992 3: DW_TAG_enumerator (enum2:1)
11993 ...
11994 2: DW_TAG_template_type_param
11995 DW_AT_type DW_FORM_ref_udata (E)
11996
11997 Besides being broken debug info, it can put GDB into an
11998 infinite loop. Consider:
11999
12000 When we're building the full name for Class<E>, we'll start
12001 at Class, and go look over its template type parameters,
12002 finding E. We'll then try to build the full name of E, and
12003 reach here. We're now trying to build the full name of E,
12004 and look over the parent DIE for containing scope. In the
12005 broken case, if we followed the parent DIE of E, we'd again
12006 find Class, and once again go look at its template type
12007 arguments, etc., etc. Simply don't consider such parent die
12008 as source-level parent of this die (it can't be, the language
12009 doesn't allow it), and break the loop here. */
12010 name = dwarf2_name (die, cu);
12011 parent_name = dwarf2_name (parent, cu);
12012 complaint (&symfile_complaints,
12013 _("template param type '%s' defined within parent '%s'"),
12014 name ? name : "<unknown>",
12015 parent_name ? parent_name : "<unknown>");
12016 return "";
12017 }
63d06c5c 12018 else
0114d602
DJ
12019 switch (parent->tag)
12020 {
63d06c5c 12021 case DW_TAG_namespace:
0114d602 12022 parent_type = read_type_die (parent, cu);
acebe513
UW
12023 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12024 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12025 Work around this problem here. */
12026 if (cu->language == language_cplus
12027 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12028 return "";
0114d602
DJ
12029 /* We give a name to even anonymous namespaces. */
12030 return TYPE_TAG_NAME (parent_type);
63d06c5c 12031 case DW_TAG_class_type:
680b30c7 12032 case DW_TAG_interface_type:
63d06c5c 12033 case DW_TAG_structure_type:
0114d602 12034 case DW_TAG_union_type:
f55ee35c 12035 case DW_TAG_module:
0114d602
DJ
12036 parent_type = read_type_die (parent, cu);
12037 if (TYPE_TAG_NAME (parent_type) != NULL)
12038 return TYPE_TAG_NAME (parent_type);
12039 else
12040 /* An anonymous structure is only allowed non-static data
12041 members; no typedefs, no member functions, et cetera.
12042 So it does not need a prefix. */
12043 return "";
abc72ce4
DE
12044 case DW_TAG_compile_unit:
12045 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12046 if (cu->language == language_cplus
12047 && dwarf2_per_objfile->types.asection != NULL
12048 && die->child != NULL
12049 && (die->tag == DW_TAG_class_type
12050 || die->tag == DW_TAG_structure_type
12051 || die->tag == DW_TAG_union_type))
12052 {
12053 char *name = guess_full_die_structure_name (die, cu);
12054 if (name != NULL)
12055 return name;
12056 }
12057 return "";
63d06c5c 12058 default:
8176b9b8 12059 return determine_prefix (parent, cu);
63d06c5c 12060 }
63d06c5c
DC
12061}
12062
3e43a32a
MS
12063/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12064 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12065 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12066 an obconcat, otherwise allocate storage for the result. The CU argument is
12067 used to determine the language and hence, the appropriate separator. */
987504bb 12068
f55ee35c 12069#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12070
12071static char *
f55ee35c
JK
12072typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12073 int physname, struct dwarf2_cu *cu)
63d06c5c 12074{
f55ee35c 12075 const char *lead = "";
5c315b68 12076 const char *sep;
63d06c5c 12077
3e43a32a
MS
12078 if (suffix == NULL || suffix[0] == '\0'
12079 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12080 sep = "";
12081 else if (cu->language == language_java)
12082 sep = ".";
f55ee35c
JK
12083 else if (cu->language == language_fortran && physname)
12084 {
12085 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12086 DW_AT_MIPS_linkage_name is preferred and used instead. */
12087
12088 lead = "__";
12089 sep = "_MOD_";
12090 }
987504bb
JJ
12091 else
12092 sep = "::";
63d06c5c 12093
6dd47d34
DE
12094 if (prefix == NULL)
12095 prefix = "";
12096 if (suffix == NULL)
12097 suffix = "";
12098
987504bb
JJ
12099 if (obs == NULL)
12100 {
3e43a32a
MS
12101 char *retval
12102 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12103
f55ee35c
JK
12104 strcpy (retval, lead);
12105 strcat (retval, prefix);
6dd47d34
DE
12106 strcat (retval, sep);
12107 strcat (retval, suffix);
63d06c5c
DC
12108 return retval;
12109 }
987504bb
JJ
12110 else
12111 {
12112 /* We have an obstack. */
f55ee35c 12113 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12114 }
63d06c5c
DC
12115}
12116
c906108c
SS
12117/* Return sibling of die, NULL if no sibling. */
12118
f9aca02d 12119static struct die_info *
fba45db2 12120sibling_die (struct die_info *die)
c906108c 12121{
639d11d3 12122 return die->sibling;
c906108c
SS
12123}
12124
71c25dea
TT
12125/* Get name of a die, return NULL if not found. */
12126
12127static char *
12128dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12129 struct obstack *obstack)
12130{
12131 if (name && cu->language == language_cplus)
12132 {
12133 char *canon_name = cp_canonicalize_string (name);
12134
12135 if (canon_name != NULL)
12136 {
12137 if (strcmp (canon_name, name) != 0)
12138 name = obsavestring (canon_name, strlen (canon_name),
12139 obstack);
12140 xfree (canon_name);
12141 }
12142 }
12143
12144 return name;
c906108c
SS
12145}
12146
9219021c
DC
12147/* Get name of a die, return NULL if not found. */
12148
12149static char *
e142c38c 12150dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12151{
12152 struct attribute *attr;
12153
e142c38c 12154 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12155 if ((!attr || !DW_STRING (attr))
12156 && die->tag != DW_TAG_class_type
12157 && die->tag != DW_TAG_interface_type
12158 && die->tag != DW_TAG_structure_type
12159 && die->tag != DW_TAG_union_type)
71c25dea
TT
12160 return NULL;
12161
12162 switch (die->tag)
12163 {
12164 case DW_TAG_compile_unit:
12165 /* Compilation units have a DW_AT_name that is a filename, not
12166 a source language identifier. */
12167 case DW_TAG_enumeration_type:
12168 case DW_TAG_enumerator:
12169 /* These tags always have simple identifiers already; no need
12170 to canonicalize them. */
12171 return DW_STRING (attr);
907af001 12172
418835cc
KS
12173 case DW_TAG_subprogram:
12174 /* Java constructors will all be named "<init>", so return
12175 the class name when we see this special case. */
12176 if (cu->language == language_java
12177 && DW_STRING (attr) != NULL
12178 && strcmp (DW_STRING (attr), "<init>") == 0)
12179 {
12180 struct dwarf2_cu *spec_cu = cu;
12181 struct die_info *spec_die;
12182
12183 /* GCJ will output '<init>' for Java constructor names.
12184 For this special case, return the name of the parent class. */
12185
12186 /* GCJ may output suprogram DIEs with AT_specification set.
12187 If so, use the name of the specified DIE. */
12188 spec_die = die_specification (die, &spec_cu);
12189 if (spec_die != NULL)
12190 return dwarf2_name (spec_die, spec_cu);
12191
12192 do
12193 {
12194 die = die->parent;
12195 if (die->tag == DW_TAG_class_type)
12196 return dwarf2_name (die, cu);
12197 }
12198 while (die->tag != DW_TAG_compile_unit);
12199 }
907af001
UW
12200 break;
12201
12202 case DW_TAG_class_type:
12203 case DW_TAG_interface_type:
12204 case DW_TAG_structure_type:
12205 case DW_TAG_union_type:
12206 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12207 structures or unions. These were of the form "._%d" in GCC 4.1,
12208 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12209 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12210 if (attr && DW_STRING (attr)
12211 && (strncmp (DW_STRING (attr), "._", 2) == 0
12212 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12213 return NULL;
53832f31
TT
12214
12215 /* GCC might emit a nameless typedef that has a linkage name. See
12216 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12217 if (!attr || DW_STRING (attr) == NULL)
12218 {
df5c6c50 12219 char *demangled = NULL;
53832f31
TT
12220
12221 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12222 if (attr == NULL)
12223 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12224
12225 if (attr == NULL || DW_STRING (attr) == NULL)
12226 return NULL;
12227
df5c6c50
JK
12228 /* Avoid demangling DW_STRING (attr) the second time on a second
12229 call for the same DIE. */
12230 if (!DW_STRING_IS_CANONICAL (attr))
12231 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12232
12233 if (demangled)
12234 {
12235 /* FIXME: we already did this for the partial symbol... */
12236 DW_STRING (attr)
12237 = obsavestring (demangled, strlen (demangled),
12238 &cu->objfile->objfile_obstack);
12239 DW_STRING_IS_CANONICAL (attr) = 1;
12240 xfree (demangled);
12241 }
12242 }
907af001
UW
12243 break;
12244
71c25dea 12245 default:
907af001
UW
12246 break;
12247 }
12248
12249 if (!DW_STRING_IS_CANONICAL (attr))
12250 {
12251 DW_STRING (attr)
12252 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12253 &cu->objfile->objfile_obstack);
12254 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12255 }
907af001 12256 return DW_STRING (attr);
9219021c
DC
12257}
12258
12259/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12260 is none. *EXT_CU is the CU containing DIE on input, and the CU
12261 containing the return value on output. */
9219021c
DC
12262
12263static struct die_info *
f2f0e013 12264dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12265{
12266 struct attribute *attr;
9219021c 12267
f2f0e013 12268 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12269 if (attr == NULL)
12270 return NULL;
12271
f2f0e013 12272 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12273}
12274
c906108c
SS
12275/* Convert a DIE tag into its string name. */
12276
12277static char *
aa1ee363 12278dwarf_tag_name (unsigned tag)
c906108c
SS
12279{
12280 switch (tag)
12281 {
12282 case DW_TAG_padding:
12283 return "DW_TAG_padding";
12284 case DW_TAG_array_type:
12285 return "DW_TAG_array_type";
12286 case DW_TAG_class_type:
12287 return "DW_TAG_class_type";
12288 case DW_TAG_entry_point:
12289 return "DW_TAG_entry_point";
12290 case DW_TAG_enumeration_type:
12291 return "DW_TAG_enumeration_type";
12292 case DW_TAG_formal_parameter:
12293 return "DW_TAG_formal_parameter";
12294 case DW_TAG_imported_declaration:
12295 return "DW_TAG_imported_declaration";
12296 case DW_TAG_label:
12297 return "DW_TAG_label";
12298 case DW_TAG_lexical_block:
12299 return "DW_TAG_lexical_block";
12300 case DW_TAG_member:
12301 return "DW_TAG_member";
12302 case DW_TAG_pointer_type:
12303 return "DW_TAG_pointer_type";
12304 case DW_TAG_reference_type:
12305 return "DW_TAG_reference_type";
12306 case DW_TAG_compile_unit:
12307 return "DW_TAG_compile_unit";
12308 case DW_TAG_string_type:
12309 return "DW_TAG_string_type";
12310 case DW_TAG_structure_type:
12311 return "DW_TAG_structure_type";
12312 case DW_TAG_subroutine_type:
12313 return "DW_TAG_subroutine_type";
12314 case DW_TAG_typedef:
12315 return "DW_TAG_typedef";
12316 case DW_TAG_union_type:
12317 return "DW_TAG_union_type";
12318 case DW_TAG_unspecified_parameters:
12319 return "DW_TAG_unspecified_parameters";
12320 case DW_TAG_variant:
12321 return "DW_TAG_variant";
12322 case DW_TAG_common_block:
12323 return "DW_TAG_common_block";
12324 case DW_TAG_common_inclusion:
12325 return "DW_TAG_common_inclusion";
12326 case DW_TAG_inheritance:
12327 return "DW_TAG_inheritance";
12328 case DW_TAG_inlined_subroutine:
12329 return "DW_TAG_inlined_subroutine";
12330 case DW_TAG_module:
12331 return "DW_TAG_module";
12332 case DW_TAG_ptr_to_member_type:
12333 return "DW_TAG_ptr_to_member_type";
12334 case DW_TAG_set_type:
12335 return "DW_TAG_set_type";
12336 case DW_TAG_subrange_type:
12337 return "DW_TAG_subrange_type";
12338 case DW_TAG_with_stmt:
12339 return "DW_TAG_with_stmt";
12340 case DW_TAG_access_declaration:
12341 return "DW_TAG_access_declaration";
12342 case DW_TAG_base_type:
12343 return "DW_TAG_base_type";
12344 case DW_TAG_catch_block:
12345 return "DW_TAG_catch_block";
12346 case DW_TAG_const_type:
12347 return "DW_TAG_const_type";
12348 case DW_TAG_constant:
12349 return "DW_TAG_constant";
12350 case DW_TAG_enumerator:
12351 return "DW_TAG_enumerator";
12352 case DW_TAG_file_type:
12353 return "DW_TAG_file_type";
12354 case DW_TAG_friend:
12355 return "DW_TAG_friend";
12356 case DW_TAG_namelist:
12357 return "DW_TAG_namelist";
12358 case DW_TAG_namelist_item:
12359 return "DW_TAG_namelist_item";
12360 case DW_TAG_packed_type:
12361 return "DW_TAG_packed_type";
12362 case DW_TAG_subprogram:
12363 return "DW_TAG_subprogram";
12364 case DW_TAG_template_type_param:
12365 return "DW_TAG_template_type_param";
12366 case DW_TAG_template_value_param:
12367 return "DW_TAG_template_value_param";
12368 case DW_TAG_thrown_type:
12369 return "DW_TAG_thrown_type";
12370 case DW_TAG_try_block:
12371 return "DW_TAG_try_block";
12372 case DW_TAG_variant_part:
12373 return "DW_TAG_variant_part";
12374 case DW_TAG_variable:
12375 return "DW_TAG_variable";
12376 case DW_TAG_volatile_type:
12377 return "DW_TAG_volatile_type";
d9fa45fe
DC
12378 case DW_TAG_dwarf_procedure:
12379 return "DW_TAG_dwarf_procedure";
12380 case DW_TAG_restrict_type:
12381 return "DW_TAG_restrict_type";
12382 case DW_TAG_interface_type:
12383 return "DW_TAG_interface_type";
12384 case DW_TAG_namespace:
12385 return "DW_TAG_namespace";
12386 case DW_TAG_imported_module:
12387 return "DW_TAG_imported_module";
12388 case DW_TAG_unspecified_type:
12389 return "DW_TAG_unspecified_type";
12390 case DW_TAG_partial_unit:
12391 return "DW_TAG_partial_unit";
12392 case DW_TAG_imported_unit:
12393 return "DW_TAG_imported_unit";
b7619582
GF
12394 case DW_TAG_condition:
12395 return "DW_TAG_condition";
12396 case DW_TAG_shared_type:
12397 return "DW_TAG_shared_type";
348e048f
DE
12398 case DW_TAG_type_unit:
12399 return "DW_TAG_type_unit";
c906108c
SS
12400 case DW_TAG_MIPS_loop:
12401 return "DW_TAG_MIPS_loop";
b7619582
GF
12402 case DW_TAG_HP_array_descriptor:
12403 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12404 case DW_TAG_format_label:
12405 return "DW_TAG_format_label";
12406 case DW_TAG_function_template:
12407 return "DW_TAG_function_template";
12408 case DW_TAG_class_template:
12409 return "DW_TAG_class_template";
b7619582
GF
12410 case DW_TAG_GNU_BINCL:
12411 return "DW_TAG_GNU_BINCL";
12412 case DW_TAG_GNU_EINCL:
12413 return "DW_TAG_GNU_EINCL";
12414 case DW_TAG_upc_shared_type:
12415 return "DW_TAG_upc_shared_type";
12416 case DW_TAG_upc_strict_type:
12417 return "DW_TAG_upc_strict_type";
12418 case DW_TAG_upc_relaxed_type:
12419 return "DW_TAG_upc_relaxed_type";
12420 case DW_TAG_PGI_kanji_type:
12421 return "DW_TAG_PGI_kanji_type";
12422 case DW_TAG_PGI_interface_block:
12423 return "DW_TAG_PGI_interface_block";
c906108c
SS
12424 default:
12425 return "DW_TAG_<unknown>";
12426 }
12427}
12428
12429/* Convert a DWARF attribute code into its string name. */
12430
12431static char *
aa1ee363 12432dwarf_attr_name (unsigned attr)
c906108c
SS
12433{
12434 switch (attr)
12435 {
12436 case DW_AT_sibling:
12437 return "DW_AT_sibling";
12438 case DW_AT_location:
12439 return "DW_AT_location";
12440 case DW_AT_name:
12441 return "DW_AT_name";
12442 case DW_AT_ordering:
12443 return "DW_AT_ordering";
12444 case DW_AT_subscr_data:
12445 return "DW_AT_subscr_data";
12446 case DW_AT_byte_size:
12447 return "DW_AT_byte_size";
12448 case DW_AT_bit_offset:
12449 return "DW_AT_bit_offset";
12450 case DW_AT_bit_size:
12451 return "DW_AT_bit_size";
12452 case DW_AT_element_list:
12453 return "DW_AT_element_list";
12454 case DW_AT_stmt_list:
12455 return "DW_AT_stmt_list";
12456 case DW_AT_low_pc:
12457 return "DW_AT_low_pc";
12458 case DW_AT_high_pc:
12459 return "DW_AT_high_pc";
12460 case DW_AT_language:
12461 return "DW_AT_language";
12462 case DW_AT_member:
12463 return "DW_AT_member";
12464 case DW_AT_discr:
12465 return "DW_AT_discr";
12466 case DW_AT_discr_value:
12467 return "DW_AT_discr_value";
12468 case DW_AT_visibility:
12469 return "DW_AT_visibility";
12470 case DW_AT_import:
12471 return "DW_AT_import";
12472 case DW_AT_string_length:
12473 return "DW_AT_string_length";
12474 case DW_AT_common_reference:
12475 return "DW_AT_common_reference";
12476 case DW_AT_comp_dir:
12477 return "DW_AT_comp_dir";
12478 case DW_AT_const_value:
12479 return "DW_AT_const_value";
12480 case DW_AT_containing_type:
12481 return "DW_AT_containing_type";
12482 case DW_AT_default_value:
12483 return "DW_AT_default_value";
12484 case DW_AT_inline:
12485 return "DW_AT_inline";
12486 case DW_AT_is_optional:
12487 return "DW_AT_is_optional";
12488 case DW_AT_lower_bound:
12489 return "DW_AT_lower_bound";
12490 case DW_AT_producer:
12491 return "DW_AT_producer";
12492 case DW_AT_prototyped:
12493 return "DW_AT_prototyped";
12494 case DW_AT_return_addr:
12495 return "DW_AT_return_addr";
12496 case DW_AT_start_scope:
12497 return "DW_AT_start_scope";
09fa0d7c
JK
12498 case DW_AT_bit_stride:
12499 return "DW_AT_bit_stride";
c906108c
SS
12500 case DW_AT_upper_bound:
12501 return "DW_AT_upper_bound";
12502 case DW_AT_abstract_origin:
12503 return "DW_AT_abstract_origin";
12504 case DW_AT_accessibility:
12505 return "DW_AT_accessibility";
12506 case DW_AT_address_class:
12507 return "DW_AT_address_class";
12508 case DW_AT_artificial:
12509 return "DW_AT_artificial";
12510 case DW_AT_base_types:
12511 return "DW_AT_base_types";
12512 case DW_AT_calling_convention:
12513 return "DW_AT_calling_convention";
12514 case DW_AT_count:
12515 return "DW_AT_count";
12516 case DW_AT_data_member_location:
12517 return "DW_AT_data_member_location";
12518 case DW_AT_decl_column:
12519 return "DW_AT_decl_column";
12520 case DW_AT_decl_file:
12521 return "DW_AT_decl_file";
12522 case DW_AT_decl_line:
12523 return "DW_AT_decl_line";
12524 case DW_AT_declaration:
12525 return "DW_AT_declaration";
12526 case DW_AT_discr_list:
12527 return "DW_AT_discr_list";
12528 case DW_AT_encoding:
12529 return "DW_AT_encoding";
12530 case DW_AT_external:
12531 return "DW_AT_external";
12532 case DW_AT_frame_base:
12533 return "DW_AT_frame_base";
12534 case DW_AT_friend:
12535 return "DW_AT_friend";
12536 case DW_AT_identifier_case:
12537 return "DW_AT_identifier_case";
12538 case DW_AT_macro_info:
12539 return "DW_AT_macro_info";
12540 case DW_AT_namelist_items:
12541 return "DW_AT_namelist_items";
12542 case DW_AT_priority:
12543 return "DW_AT_priority";
12544 case DW_AT_segment:
12545 return "DW_AT_segment";
12546 case DW_AT_specification:
12547 return "DW_AT_specification";
12548 case DW_AT_static_link:
12549 return "DW_AT_static_link";
12550 case DW_AT_type:
12551 return "DW_AT_type";
12552 case DW_AT_use_location:
12553 return "DW_AT_use_location";
12554 case DW_AT_variable_parameter:
12555 return "DW_AT_variable_parameter";
12556 case DW_AT_virtuality:
12557 return "DW_AT_virtuality";
12558 case DW_AT_vtable_elem_location:
12559 return "DW_AT_vtable_elem_location";
b7619582 12560 /* DWARF 3 values. */
d9fa45fe
DC
12561 case DW_AT_allocated:
12562 return "DW_AT_allocated";
12563 case DW_AT_associated:
12564 return "DW_AT_associated";
12565 case DW_AT_data_location:
12566 return "DW_AT_data_location";
09fa0d7c
JK
12567 case DW_AT_byte_stride:
12568 return "DW_AT_byte_stride";
d9fa45fe
DC
12569 case DW_AT_entry_pc:
12570 return "DW_AT_entry_pc";
12571 case DW_AT_use_UTF8:
12572 return "DW_AT_use_UTF8";
12573 case DW_AT_extension:
12574 return "DW_AT_extension";
12575 case DW_AT_ranges:
12576 return "DW_AT_ranges";
12577 case DW_AT_trampoline:
12578 return "DW_AT_trampoline";
12579 case DW_AT_call_column:
12580 return "DW_AT_call_column";
12581 case DW_AT_call_file:
12582 return "DW_AT_call_file";
12583 case DW_AT_call_line:
12584 return "DW_AT_call_line";
b7619582
GF
12585 case DW_AT_description:
12586 return "DW_AT_description";
12587 case DW_AT_binary_scale:
12588 return "DW_AT_binary_scale";
12589 case DW_AT_decimal_scale:
12590 return "DW_AT_decimal_scale";
12591 case DW_AT_small:
12592 return "DW_AT_small";
12593 case DW_AT_decimal_sign:
12594 return "DW_AT_decimal_sign";
12595 case DW_AT_digit_count:
12596 return "DW_AT_digit_count";
12597 case DW_AT_picture_string:
12598 return "DW_AT_picture_string";
12599 case DW_AT_mutable:
12600 return "DW_AT_mutable";
12601 case DW_AT_threads_scaled:
12602 return "DW_AT_threads_scaled";
12603 case DW_AT_explicit:
12604 return "DW_AT_explicit";
12605 case DW_AT_object_pointer:
12606 return "DW_AT_object_pointer";
12607 case DW_AT_endianity:
12608 return "DW_AT_endianity";
12609 case DW_AT_elemental:
12610 return "DW_AT_elemental";
12611 case DW_AT_pure:
12612 return "DW_AT_pure";
12613 case DW_AT_recursive:
12614 return "DW_AT_recursive";
348e048f
DE
12615 /* DWARF 4 values. */
12616 case DW_AT_signature:
12617 return "DW_AT_signature";
31ef98ae
TT
12618 case DW_AT_linkage_name:
12619 return "DW_AT_linkage_name";
b7619582 12620 /* SGI/MIPS extensions. */
c764a876 12621#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12622 case DW_AT_MIPS_fde:
12623 return "DW_AT_MIPS_fde";
c764a876 12624#endif
c906108c
SS
12625 case DW_AT_MIPS_loop_begin:
12626 return "DW_AT_MIPS_loop_begin";
12627 case DW_AT_MIPS_tail_loop_begin:
12628 return "DW_AT_MIPS_tail_loop_begin";
12629 case DW_AT_MIPS_epilog_begin:
12630 return "DW_AT_MIPS_epilog_begin";
12631 case DW_AT_MIPS_loop_unroll_factor:
12632 return "DW_AT_MIPS_loop_unroll_factor";
12633 case DW_AT_MIPS_software_pipeline_depth:
12634 return "DW_AT_MIPS_software_pipeline_depth";
12635 case DW_AT_MIPS_linkage_name:
12636 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12637 case DW_AT_MIPS_stride:
12638 return "DW_AT_MIPS_stride";
12639 case DW_AT_MIPS_abstract_name:
12640 return "DW_AT_MIPS_abstract_name";
12641 case DW_AT_MIPS_clone_origin:
12642 return "DW_AT_MIPS_clone_origin";
12643 case DW_AT_MIPS_has_inlines:
12644 return "DW_AT_MIPS_has_inlines";
b7619582 12645 /* HP extensions. */
c764a876 12646#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12647 case DW_AT_HP_block_index:
12648 return "DW_AT_HP_block_index";
c764a876 12649#endif
b7619582
GF
12650 case DW_AT_HP_unmodifiable:
12651 return "DW_AT_HP_unmodifiable";
12652 case DW_AT_HP_actuals_stmt_list:
12653 return "DW_AT_HP_actuals_stmt_list";
12654 case DW_AT_HP_proc_per_section:
12655 return "DW_AT_HP_proc_per_section";
12656 case DW_AT_HP_raw_data_ptr:
12657 return "DW_AT_HP_raw_data_ptr";
12658 case DW_AT_HP_pass_by_reference:
12659 return "DW_AT_HP_pass_by_reference";
12660 case DW_AT_HP_opt_level:
12661 return "DW_AT_HP_opt_level";
12662 case DW_AT_HP_prof_version_id:
12663 return "DW_AT_HP_prof_version_id";
12664 case DW_AT_HP_opt_flags:
12665 return "DW_AT_HP_opt_flags";
12666 case DW_AT_HP_cold_region_low_pc:
12667 return "DW_AT_HP_cold_region_low_pc";
12668 case DW_AT_HP_cold_region_high_pc:
12669 return "DW_AT_HP_cold_region_high_pc";
12670 case DW_AT_HP_all_variables_modifiable:
12671 return "DW_AT_HP_all_variables_modifiable";
12672 case DW_AT_HP_linkage_name:
12673 return "DW_AT_HP_linkage_name";
12674 case DW_AT_HP_prof_flags:
12675 return "DW_AT_HP_prof_flags";
12676 /* GNU extensions. */
c906108c
SS
12677 case DW_AT_sf_names:
12678 return "DW_AT_sf_names";
12679 case DW_AT_src_info:
12680 return "DW_AT_src_info";
12681 case DW_AT_mac_info:
12682 return "DW_AT_mac_info";
12683 case DW_AT_src_coords:
12684 return "DW_AT_src_coords";
12685 case DW_AT_body_begin:
12686 return "DW_AT_body_begin";
12687 case DW_AT_body_end:
12688 return "DW_AT_body_end";
f5f8a009
EZ
12689 case DW_AT_GNU_vector:
12690 return "DW_AT_GNU_vector";
2de00c64
DE
12691 case DW_AT_GNU_odr_signature:
12692 return "DW_AT_GNU_odr_signature";
b7619582
GF
12693 /* VMS extensions. */
12694 case DW_AT_VMS_rtnbeg_pd_address:
12695 return "DW_AT_VMS_rtnbeg_pd_address";
12696 /* UPC extension. */
12697 case DW_AT_upc_threads_scaled:
12698 return "DW_AT_upc_threads_scaled";
12699 /* PGI (STMicroelectronics) extensions. */
12700 case DW_AT_PGI_lbase:
12701 return "DW_AT_PGI_lbase";
12702 case DW_AT_PGI_soffset:
12703 return "DW_AT_PGI_soffset";
12704 case DW_AT_PGI_lstride:
12705 return "DW_AT_PGI_lstride";
c906108c
SS
12706 default:
12707 return "DW_AT_<unknown>";
12708 }
12709}
12710
12711/* Convert a DWARF value form code into its string name. */
12712
12713static char *
aa1ee363 12714dwarf_form_name (unsigned form)
c906108c
SS
12715{
12716 switch (form)
12717 {
12718 case DW_FORM_addr:
12719 return "DW_FORM_addr";
12720 case DW_FORM_block2:
12721 return "DW_FORM_block2";
12722 case DW_FORM_block4:
12723 return "DW_FORM_block4";
12724 case DW_FORM_data2:
12725 return "DW_FORM_data2";
12726 case DW_FORM_data4:
12727 return "DW_FORM_data4";
12728 case DW_FORM_data8:
12729 return "DW_FORM_data8";
12730 case DW_FORM_string:
12731 return "DW_FORM_string";
12732 case DW_FORM_block:
12733 return "DW_FORM_block";
12734 case DW_FORM_block1:
12735 return "DW_FORM_block1";
12736 case DW_FORM_data1:
12737 return "DW_FORM_data1";
12738 case DW_FORM_flag:
12739 return "DW_FORM_flag";
12740 case DW_FORM_sdata:
12741 return "DW_FORM_sdata";
12742 case DW_FORM_strp:
12743 return "DW_FORM_strp";
12744 case DW_FORM_udata:
12745 return "DW_FORM_udata";
12746 case DW_FORM_ref_addr:
12747 return "DW_FORM_ref_addr";
12748 case DW_FORM_ref1:
12749 return "DW_FORM_ref1";
12750 case DW_FORM_ref2:
12751 return "DW_FORM_ref2";
12752 case DW_FORM_ref4:
12753 return "DW_FORM_ref4";
12754 case DW_FORM_ref8:
12755 return "DW_FORM_ref8";
12756 case DW_FORM_ref_udata:
12757 return "DW_FORM_ref_udata";
12758 case DW_FORM_indirect:
12759 return "DW_FORM_indirect";
348e048f
DE
12760 case DW_FORM_sec_offset:
12761 return "DW_FORM_sec_offset";
12762 case DW_FORM_exprloc:
12763 return "DW_FORM_exprloc";
12764 case DW_FORM_flag_present:
12765 return "DW_FORM_flag_present";
55f1336d
TT
12766 case DW_FORM_ref_sig8:
12767 return "DW_FORM_ref_sig8";
c906108c
SS
12768 default:
12769 return "DW_FORM_<unknown>";
12770 }
12771}
12772
12773/* Convert a DWARF stack opcode into its string name. */
12774
9eae7c52 12775const char *
b1bfef65 12776dwarf_stack_op_name (unsigned op)
c906108c
SS
12777{
12778 switch (op)
12779 {
12780 case DW_OP_addr:
12781 return "DW_OP_addr";
12782 case DW_OP_deref:
12783 return "DW_OP_deref";
12784 case DW_OP_const1u:
12785 return "DW_OP_const1u";
12786 case DW_OP_const1s:
12787 return "DW_OP_const1s";
12788 case DW_OP_const2u:
12789 return "DW_OP_const2u";
12790 case DW_OP_const2s:
12791 return "DW_OP_const2s";
12792 case DW_OP_const4u:
12793 return "DW_OP_const4u";
12794 case DW_OP_const4s:
12795 return "DW_OP_const4s";
12796 case DW_OP_const8u:
12797 return "DW_OP_const8u";
12798 case DW_OP_const8s:
12799 return "DW_OP_const8s";
12800 case DW_OP_constu:
12801 return "DW_OP_constu";
12802 case DW_OP_consts:
12803 return "DW_OP_consts";
12804 case DW_OP_dup:
12805 return "DW_OP_dup";
12806 case DW_OP_drop:
12807 return "DW_OP_drop";
12808 case DW_OP_over:
12809 return "DW_OP_over";
12810 case DW_OP_pick:
12811 return "DW_OP_pick";
12812 case DW_OP_swap:
12813 return "DW_OP_swap";
12814 case DW_OP_rot:
12815 return "DW_OP_rot";
12816 case DW_OP_xderef:
12817 return "DW_OP_xderef";
12818 case DW_OP_abs:
12819 return "DW_OP_abs";
12820 case DW_OP_and:
12821 return "DW_OP_and";
12822 case DW_OP_div:
12823 return "DW_OP_div";
12824 case DW_OP_minus:
12825 return "DW_OP_minus";
12826 case DW_OP_mod:
12827 return "DW_OP_mod";
12828 case DW_OP_mul:
12829 return "DW_OP_mul";
12830 case DW_OP_neg:
12831 return "DW_OP_neg";
12832 case DW_OP_not:
12833 return "DW_OP_not";
12834 case DW_OP_or:
12835 return "DW_OP_or";
12836 case DW_OP_plus:
12837 return "DW_OP_plus";
12838 case DW_OP_plus_uconst:
12839 return "DW_OP_plus_uconst";
12840 case DW_OP_shl:
12841 return "DW_OP_shl";
12842 case DW_OP_shr:
12843 return "DW_OP_shr";
12844 case DW_OP_shra:
12845 return "DW_OP_shra";
12846 case DW_OP_xor:
12847 return "DW_OP_xor";
12848 case DW_OP_bra:
12849 return "DW_OP_bra";
12850 case DW_OP_eq:
12851 return "DW_OP_eq";
12852 case DW_OP_ge:
12853 return "DW_OP_ge";
12854 case DW_OP_gt:
12855 return "DW_OP_gt";
12856 case DW_OP_le:
12857 return "DW_OP_le";
12858 case DW_OP_lt:
12859 return "DW_OP_lt";
12860 case DW_OP_ne:
12861 return "DW_OP_ne";
12862 case DW_OP_skip:
12863 return "DW_OP_skip";
12864 case DW_OP_lit0:
12865 return "DW_OP_lit0";
12866 case DW_OP_lit1:
12867 return "DW_OP_lit1";
12868 case DW_OP_lit2:
12869 return "DW_OP_lit2";
12870 case DW_OP_lit3:
12871 return "DW_OP_lit3";
12872 case DW_OP_lit4:
12873 return "DW_OP_lit4";
12874 case DW_OP_lit5:
12875 return "DW_OP_lit5";
12876 case DW_OP_lit6:
12877 return "DW_OP_lit6";
12878 case DW_OP_lit7:
12879 return "DW_OP_lit7";
12880 case DW_OP_lit8:
12881 return "DW_OP_lit8";
12882 case DW_OP_lit9:
12883 return "DW_OP_lit9";
12884 case DW_OP_lit10:
12885 return "DW_OP_lit10";
12886 case DW_OP_lit11:
12887 return "DW_OP_lit11";
12888 case DW_OP_lit12:
12889 return "DW_OP_lit12";
12890 case DW_OP_lit13:
12891 return "DW_OP_lit13";
12892 case DW_OP_lit14:
12893 return "DW_OP_lit14";
12894 case DW_OP_lit15:
12895 return "DW_OP_lit15";
12896 case DW_OP_lit16:
12897 return "DW_OP_lit16";
12898 case DW_OP_lit17:
12899 return "DW_OP_lit17";
12900 case DW_OP_lit18:
12901 return "DW_OP_lit18";
12902 case DW_OP_lit19:
12903 return "DW_OP_lit19";
12904 case DW_OP_lit20:
12905 return "DW_OP_lit20";
12906 case DW_OP_lit21:
12907 return "DW_OP_lit21";
12908 case DW_OP_lit22:
12909 return "DW_OP_lit22";
12910 case DW_OP_lit23:
12911 return "DW_OP_lit23";
12912 case DW_OP_lit24:
12913 return "DW_OP_lit24";
12914 case DW_OP_lit25:
12915 return "DW_OP_lit25";
12916 case DW_OP_lit26:
12917 return "DW_OP_lit26";
12918 case DW_OP_lit27:
12919 return "DW_OP_lit27";
12920 case DW_OP_lit28:
12921 return "DW_OP_lit28";
12922 case DW_OP_lit29:
12923 return "DW_OP_lit29";
12924 case DW_OP_lit30:
12925 return "DW_OP_lit30";
12926 case DW_OP_lit31:
12927 return "DW_OP_lit31";
12928 case DW_OP_reg0:
12929 return "DW_OP_reg0";
12930 case DW_OP_reg1:
12931 return "DW_OP_reg1";
12932 case DW_OP_reg2:
12933 return "DW_OP_reg2";
12934 case DW_OP_reg3:
12935 return "DW_OP_reg3";
12936 case DW_OP_reg4:
12937 return "DW_OP_reg4";
12938 case DW_OP_reg5:
12939 return "DW_OP_reg5";
12940 case DW_OP_reg6:
12941 return "DW_OP_reg6";
12942 case DW_OP_reg7:
12943 return "DW_OP_reg7";
12944 case DW_OP_reg8:
12945 return "DW_OP_reg8";
12946 case DW_OP_reg9:
12947 return "DW_OP_reg9";
12948 case DW_OP_reg10:
12949 return "DW_OP_reg10";
12950 case DW_OP_reg11:
12951 return "DW_OP_reg11";
12952 case DW_OP_reg12:
12953 return "DW_OP_reg12";
12954 case DW_OP_reg13:
12955 return "DW_OP_reg13";
12956 case DW_OP_reg14:
12957 return "DW_OP_reg14";
12958 case DW_OP_reg15:
12959 return "DW_OP_reg15";
12960 case DW_OP_reg16:
12961 return "DW_OP_reg16";
12962 case DW_OP_reg17:
12963 return "DW_OP_reg17";
12964 case DW_OP_reg18:
12965 return "DW_OP_reg18";
12966 case DW_OP_reg19:
12967 return "DW_OP_reg19";
12968 case DW_OP_reg20:
12969 return "DW_OP_reg20";
12970 case DW_OP_reg21:
12971 return "DW_OP_reg21";
12972 case DW_OP_reg22:
12973 return "DW_OP_reg22";
12974 case DW_OP_reg23:
12975 return "DW_OP_reg23";
12976 case DW_OP_reg24:
12977 return "DW_OP_reg24";
12978 case DW_OP_reg25:
12979 return "DW_OP_reg25";
12980 case DW_OP_reg26:
12981 return "DW_OP_reg26";
12982 case DW_OP_reg27:
12983 return "DW_OP_reg27";
12984 case DW_OP_reg28:
12985 return "DW_OP_reg28";
12986 case DW_OP_reg29:
12987 return "DW_OP_reg29";
12988 case DW_OP_reg30:
12989 return "DW_OP_reg30";
12990 case DW_OP_reg31:
12991 return "DW_OP_reg31";
12992 case DW_OP_breg0:
12993 return "DW_OP_breg0";
12994 case DW_OP_breg1:
12995 return "DW_OP_breg1";
12996 case DW_OP_breg2:
12997 return "DW_OP_breg2";
12998 case DW_OP_breg3:
12999 return "DW_OP_breg3";
13000 case DW_OP_breg4:
13001 return "DW_OP_breg4";
13002 case DW_OP_breg5:
13003 return "DW_OP_breg5";
13004 case DW_OP_breg6:
13005 return "DW_OP_breg6";
13006 case DW_OP_breg7:
13007 return "DW_OP_breg7";
13008 case DW_OP_breg8:
13009 return "DW_OP_breg8";
13010 case DW_OP_breg9:
13011 return "DW_OP_breg9";
13012 case DW_OP_breg10:
13013 return "DW_OP_breg10";
13014 case DW_OP_breg11:
13015 return "DW_OP_breg11";
13016 case DW_OP_breg12:
13017 return "DW_OP_breg12";
13018 case DW_OP_breg13:
13019 return "DW_OP_breg13";
13020 case DW_OP_breg14:
13021 return "DW_OP_breg14";
13022 case DW_OP_breg15:
13023 return "DW_OP_breg15";
13024 case DW_OP_breg16:
13025 return "DW_OP_breg16";
13026 case DW_OP_breg17:
13027 return "DW_OP_breg17";
13028 case DW_OP_breg18:
13029 return "DW_OP_breg18";
13030 case DW_OP_breg19:
13031 return "DW_OP_breg19";
13032 case DW_OP_breg20:
13033 return "DW_OP_breg20";
13034 case DW_OP_breg21:
13035 return "DW_OP_breg21";
13036 case DW_OP_breg22:
13037 return "DW_OP_breg22";
13038 case DW_OP_breg23:
13039 return "DW_OP_breg23";
13040 case DW_OP_breg24:
13041 return "DW_OP_breg24";
13042 case DW_OP_breg25:
13043 return "DW_OP_breg25";
13044 case DW_OP_breg26:
13045 return "DW_OP_breg26";
13046 case DW_OP_breg27:
13047 return "DW_OP_breg27";
13048 case DW_OP_breg28:
13049 return "DW_OP_breg28";
13050 case DW_OP_breg29:
13051 return "DW_OP_breg29";
13052 case DW_OP_breg30:
13053 return "DW_OP_breg30";
13054 case DW_OP_breg31:
13055 return "DW_OP_breg31";
13056 case DW_OP_regx:
13057 return "DW_OP_regx";
13058 case DW_OP_fbreg:
13059 return "DW_OP_fbreg";
13060 case DW_OP_bregx:
13061 return "DW_OP_bregx";
13062 case DW_OP_piece:
13063 return "DW_OP_piece";
13064 case DW_OP_deref_size:
13065 return "DW_OP_deref_size";
13066 case DW_OP_xderef_size:
13067 return "DW_OP_xderef_size";
13068 case DW_OP_nop:
13069 return "DW_OP_nop";
b7619582 13070 /* DWARF 3 extensions. */
ed348acc
EZ
13071 case DW_OP_push_object_address:
13072 return "DW_OP_push_object_address";
13073 case DW_OP_call2:
13074 return "DW_OP_call2";
13075 case DW_OP_call4:
13076 return "DW_OP_call4";
13077 case DW_OP_call_ref:
13078 return "DW_OP_call_ref";
b7619582
GF
13079 case DW_OP_form_tls_address:
13080 return "DW_OP_form_tls_address";
13081 case DW_OP_call_frame_cfa:
13082 return "DW_OP_call_frame_cfa";
13083 case DW_OP_bit_piece:
13084 return "DW_OP_bit_piece";
9eae7c52
TT
13085 /* DWARF 4 extensions. */
13086 case DW_OP_implicit_value:
13087 return "DW_OP_implicit_value";
13088 case DW_OP_stack_value:
13089 return "DW_OP_stack_value";
13090 /* GNU extensions. */
ed348acc
EZ
13091 case DW_OP_GNU_push_tls_address:
13092 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13093 case DW_OP_GNU_uninit:
13094 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13095 case DW_OP_GNU_implicit_pointer:
13096 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13097 case DW_OP_GNU_entry_value:
13098 return "DW_OP_GNU_entry_value";
13099 case DW_OP_GNU_const_type:
13100 return "DW_OP_GNU_const_type";
13101 case DW_OP_GNU_regval_type:
13102 return "DW_OP_GNU_regval_type";
13103 case DW_OP_GNU_deref_type:
13104 return "DW_OP_GNU_deref_type";
13105 case DW_OP_GNU_convert:
13106 return "DW_OP_GNU_convert";
13107 case DW_OP_GNU_reinterpret:
13108 return "DW_OP_GNU_reinterpret";
c906108c 13109 default:
b1bfef65 13110 return NULL;
c906108c
SS
13111 }
13112}
13113
13114static char *
fba45db2 13115dwarf_bool_name (unsigned mybool)
c906108c
SS
13116{
13117 if (mybool)
13118 return "TRUE";
13119 else
13120 return "FALSE";
13121}
13122
13123/* Convert a DWARF type code into its string name. */
13124
13125static char *
aa1ee363 13126dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13127{
13128 switch (enc)
13129 {
b7619582
GF
13130 case DW_ATE_void:
13131 return "DW_ATE_void";
c906108c
SS
13132 case DW_ATE_address:
13133 return "DW_ATE_address";
13134 case DW_ATE_boolean:
13135 return "DW_ATE_boolean";
13136 case DW_ATE_complex_float:
13137 return "DW_ATE_complex_float";
13138 case DW_ATE_float:
13139 return "DW_ATE_float";
13140 case DW_ATE_signed:
13141 return "DW_ATE_signed";
13142 case DW_ATE_signed_char:
13143 return "DW_ATE_signed_char";
13144 case DW_ATE_unsigned:
13145 return "DW_ATE_unsigned";
13146 case DW_ATE_unsigned_char:
13147 return "DW_ATE_unsigned_char";
b7619582 13148 /* DWARF 3. */
d9fa45fe
DC
13149 case DW_ATE_imaginary_float:
13150 return "DW_ATE_imaginary_float";
b7619582
GF
13151 case DW_ATE_packed_decimal:
13152 return "DW_ATE_packed_decimal";
13153 case DW_ATE_numeric_string:
13154 return "DW_ATE_numeric_string";
13155 case DW_ATE_edited:
13156 return "DW_ATE_edited";
13157 case DW_ATE_signed_fixed:
13158 return "DW_ATE_signed_fixed";
13159 case DW_ATE_unsigned_fixed:
13160 return "DW_ATE_unsigned_fixed";
13161 case DW_ATE_decimal_float:
13162 return "DW_ATE_decimal_float";
75079b2b
TT
13163 /* DWARF 4. */
13164 case DW_ATE_UTF:
13165 return "DW_ATE_UTF";
b7619582
GF
13166 /* HP extensions. */
13167 case DW_ATE_HP_float80:
13168 return "DW_ATE_HP_float80";
13169 case DW_ATE_HP_complex_float80:
13170 return "DW_ATE_HP_complex_float80";
13171 case DW_ATE_HP_float128:
13172 return "DW_ATE_HP_float128";
13173 case DW_ATE_HP_complex_float128:
13174 return "DW_ATE_HP_complex_float128";
13175 case DW_ATE_HP_floathpintel:
13176 return "DW_ATE_HP_floathpintel";
13177 case DW_ATE_HP_imaginary_float80:
13178 return "DW_ATE_HP_imaginary_float80";
13179 case DW_ATE_HP_imaginary_float128:
13180 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13181 default:
13182 return "DW_ATE_<unknown>";
13183 }
13184}
13185
0963b4bd 13186/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13187
13188#if 0
13189static char *
aa1ee363 13190dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13191{
13192 switch (cfi_opc)
13193 {
13194 case DW_CFA_advance_loc:
13195 return "DW_CFA_advance_loc";
13196 case DW_CFA_offset:
13197 return "DW_CFA_offset";
13198 case DW_CFA_restore:
13199 return "DW_CFA_restore";
13200 case DW_CFA_nop:
13201 return "DW_CFA_nop";
13202 case DW_CFA_set_loc:
13203 return "DW_CFA_set_loc";
13204 case DW_CFA_advance_loc1:
13205 return "DW_CFA_advance_loc1";
13206 case DW_CFA_advance_loc2:
13207 return "DW_CFA_advance_loc2";
13208 case DW_CFA_advance_loc4:
13209 return "DW_CFA_advance_loc4";
13210 case DW_CFA_offset_extended:
13211 return "DW_CFA_offset_extended";
13212 case DW_CFA_restore_extended:
13213 return "DW_CFA_restore_extended";
13214 case DW_CFA_undefined:
13215 return "DW_CFA_undefined";
13216 case DW_CFA_same_value:
13217 return "DW_CFA_same_value";
13218 case DW_CFA_register:
13219 return "DW_CFA_register";
13220 case DW_CFA_remember_state:
13221 return "DW_CFA_remember_state";
13222 case DW_CFA_restore_state:
13223 return "DW_CFA_restore_state";
13224 case DW_CFA_def_cfa:
13225 return "DW_CFA_def_cfa";
13226 case DW_CFA_def_cfa_register:
13227 return "DW_CFA_def_cfa_register";
13228 case DW_CFA_def_cfa_offset:
13229 return "DW_CFA_def_cfa_offset";
b7619582 13230 /* DWARF 3. */
985cb1a3
JM
13231 case DW_CFA_def_cfa_expression:
13232 return "DW_CFA_def_cfa_expression";
13233 case DW_CFA_expression:
13234 return "DW_CFA_expression";
13235 case DW_CFA_offset_extended_sf:
13236 return "DW_CFA_offset_extended_sf";
13237 case DW_CFA_def_cfa_sf:
13238 return "DW_CFA_def_cfa_sf";
13239 case DW_CFA_def_cfa_offset_sf:
13240 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13241 case DW_CFA_val_offset:
13242 return "DW_CFA_val_offset";
13243 case DW_CFA_val_offset_sf:
13244 return "DW_CFA_val_offset_sf";
13245 case DW_CFA_val_expression:
13246 return "DW_CFA_val_expression";
13247 /* SGI/MIPS specific. */
c906108c
SS
13248 case DW_CFA_MIPS_advance_loc8:
13249 return "DW_CFA_MIPS_advance_loc8";
b7619582 13250 /* GNU extensions. */
985cb1a3
JM
13251 case DW_CFA_GNU_window_save:
13252 return "DW_CFA_GNU_window_save";
13253 case DW_CFA_GNU_args_size:
13254 return "DW_CFA_GNU_args_size";
13255 case DW_CFA_GNU_negative_offset_extended:
13256 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13257 default:
13258 return "DW_CFA_<unknown>";
13259 }
13260}
13261#endif
13262
f9aca02d 13263static void
d97bc12b 13264dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13265{
13266 unsigned int i;
13267
d97bc12b
DE
13268 print_spaces (indent, f);
13269 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13270 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13271
13272 if (die->parent != NULL)
13273 {
13274 print_spaces (indent, f);
13275 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13276 die->parent->offset);
13277 }
13278
13279 print_spaces (indent, f);
13280 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13281 dwarf_bool_name (die->child != NULL));
c906108c 13282
d97bc12b
DE
13283 print_spaces (indent, f);
13284 fprintf_unfiltered (f, " attributes:\n");
13285
c906108c
SS
13286 for (i = 0; i < die->num_attrs; ++i)
13287 {
d97bc12b
DE
13288 print_spaces (indent, f);
13289 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13290 dwarf_attr_name (die->attrs[i].name),
13291 dwarf_form_name (die->attrs[i].form));
d97bc12b 13292
c906108c
SS
13293 switch (die->attrs[i].form)
13294 {
13295 case DW_FORM_ref_addr:
13296 case DW_FORM_addr:
d97bc12b 13297 fprintf_unfiltered (f, "address: ");
5af949e3 13298 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13299 break;
13300 case DW_FORM_block2:
13301 case DW_FORM_block4:
13302 case DW_FORM_block:
13303 case DW_FORM_block1:
3e43a32a
MS
13304 fprintf_unfiltered (f, "block: size %d",
13305 DW_BLOCK (&die->attrs[i])->size);
c906108c 13306 break;
2dc7f7b3
TT
13307 case DW_FORM_exprloc:
13308 fprintf_unfiltered (f, "expression: size %u",
13309 DW_BLOCK (&die->attrs[i])->size);
13310 break;
10b3939b
DJ
13311 case DW_FORM_ref1:
13312 case DW_FORM_ref2:
13313 case DW_FORM_ref4:
d97bc12b 13314 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13315 (long) (DW_ADDR (&die->attrs[i])));
13316 break;
c906108c
SS
13317 case DW_FORM_data1:
13318 case DW_FORM_data2:
13319 case DW_FORM_data4:
ce5d95e1 13320 case DW_FORM_data8:
c906108c
SS
13321 case DW_FORM_udata:
13322 case DW_FORM_sdata:
43bbcdc2
PH
13323 fprintf_unfiltered (f, "constant: %s",
13324 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13325 break;
2dc7f7b3
TT
13326 case DW_FORM_sec_offset:
13327 fprintf_unfiltered (f, "section offset: %s",
13328 pulongest (DW_UNSND (&die->attrs[i])));
13329 break;
55f1336d 13330 case DW_FORM_ref_sig8:
348e048f
DE
13331 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13332 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13333 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
13334 else
13335 fprintf_unfiltered (f, "signatured type, offset: unknown");
13336 break;
c906108c 13337 case DW_FORM_string:
4bdf3d34 13338 case DW_FORM_strp:
8285870a 13339 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13340 DW_STRING (&die->attrs[i])
8285870a
JK
13341 ? DW_STRING (&die->attrs[i]) : "",
13342 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13343 break;
13344 case DW_FORM_flag:
13345 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13346 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13347 else
d97bc12b 13348 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13349 break;
2dc7f7b3
TT
13350 case DW_FORM_flag_present:
13351 fprintf_unfiltered (f, "flag: TRUE");
13352 break;
a8329558 13353 case DW_FORM_indirect:
0963b4bd
MS
13354 /* The reader will have reduced the indirect form to
13355 the "base form" so this form should not occur. */
3e43a32a
MS
13356 fprintf_unfiltered (f,
13357 "unexpected attribute form: DW_FORM_indirect");
a8329558 13358 break;
c906108c 13359 default:
d97bc12b 13360 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13361 die->attrs[i].form);
d97bc12b 13362 break;
c906108c 13363 }
d97bc12b 13364 fprintf_unfiltered (f, "\n");
c906108c
SS
13365 }
13366}
13367
f9aca02d 13368static void
d97bc12b 13369dump_die_for_error (struct die_info *die)
c906108c 13370{
d97bc12b
DE
13371 dump_die_shallow (gdb_stderr, 0, die);
13372}
13373
13374static void
13375dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13376{
13377 int indent = level * 4;
13378
13379 gdb_assert (die != NULL);
13380
13381 if (level >= max_level)
13382 return;
13383
13384 dump_die_shallow (f, indent, die);
13385
13386 if (die->child != NULL)
c906108c 13387 {
d97bc12b
DE
13388 print_spaces (indent, f);
13389 fprintf_unfiltered (f, " Children:");
13390 if (level + 1 < max_level)
13391 {
13392 fprintf_unfiltered (f, "\n");
13393 dump_die_1 (f, level + 1, max_level, die->child);
13394 }
13395 else
13396 {
3e43a32a
MS
13397 fprintf_unfiltered (f,
13398 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13399 }
13400 }
13401
13402 if (die->sibling != NULL && level > 0)
13403 {
13404 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13405 }
13406}
13407
d97bc12b
DE
13408/* This is called from the pdie macro in gdbinit.in.
13409 It's not static so gcc will keep a copy callable from gdb. */
13410
13411void
13412dump_die (struct die_info *die, int max_level)
13413{
13414 dump_die_1 (gdb_stdlog, 0, max_level, die);
13415}
13416
f9aca02d 13417static void
51545339 13418store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13419{
51545339 13420 void **slot;
c906108c 13421
51545339
DJ
13422 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13423
13424 *slot = die;
c906108c
SS
13425}
13426
93311388
DE
13427static int
13428is_ref_attr (struct attribute *attr)
c906108c 13429{
c906108c
SS
13430 switch (attr->form)
13431 {
13432 case DW_FORM_ref_addr:
c906108c
SS
13433 case DW_FORM_ref1:
13434 case DW_FORM_ref2:
13435 case DW_FORM_ref4:
613e1657 13436 case DW_FORM_ref8:
c906108c 13437 case DW_FORM_ref_udata:
93311388 13438 return 1;
c906108c 13439 default:
93311388 13440 return 0;
c906108c 13441 }
93311388
DE
13442}
13443
13444static unsigned int
13445dwarf2_get_ref_die_offset (struct attribute *attr)
13446{
13447 if (is_ref_attr (attr))
13448 return DW_ADDR (attr);
13449
13450 complaint (&symfile_complaints,
13451 _("unsupported die ref attribute form: '%s'"),
13452 dwarf_form_name (attr->form));
13453 return 0;
c906108c
SS
13454}
13455
43bbcdc2
PH
13456/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13457 * the value held by the attribute is not constant. */
a02abb62 13458
43bbcdc2 13459static LONGEST
a02abb62
JB
13460dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13461{
13462 if (attr->form == DW_FORM_sdata)
13463 return DW_SND (attr);
13464 else if (attr->form == DW_FORM_udata
13465 || attr->form == DW_FORM_data1
13466 || attr->form == DW_FORM_data2
13467 || attr->form == DW_FORM_data4
13468 || attr->form == DW_FORM_data8)
13469 return DW_UNSND (attr);
13470 else
13471 {
3e43a32a
MS
13472 complaint (&symfile_complaints,
13473 _("Attribute value is not a constant (%s)"),
a02abb62
JB
13474 dwarf_form_name (attr->form));
13475 return default_value;
13476 }
13477}
13478
03dd20cc 13479/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
13480 unit and add it to our queue.
13481 The result is non-zero if PER_CU was queued, otherwise the result is zero
13482 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 13483
348e048f 13484static int
03dd20cc
DJ
13485maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13486 struct dwarf2_per_cu_data *per_cu)
13487{
98bfdba5
PA
13488 /* We may arrive here during partial symbol reading, if we need full
13489 DIEs to process an unusual case (e.g. template arguments). Do
13490 not queue PER_CU, just tell our caller to load its DIEs. */
13491 if (dwarf2_per_objfile->reading_partial_symbols)
13492 {
13493 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13494 return 1;
13495 return 0;
13496 }
13497
03dd20cc
DJ
13498 /* Mark the dependence relation so that we don't flush PER_CU
13499 too early. */
13500 dwarf2_add_dependence (this_cu, per_cu);
13501
13502 /* If it's already on the queue, we have nothing to do. */
13503 if (per_cu->queued)
348e048f 13504 return 0;
03dd20cc
DJ
13505
13506 /* If the compilation unit is already loaded, just mark it as
13507 used. */
13508 if (per_cu->cu != NULL)
13509 {
13510 per_cu->cu->last_used = 0;
348e048f 13511 return 0;
03dd20cc
DJ
13512 }
13513
13514 /* Add it to the queue. */
13515 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
13516
13517 return 1;
13518}
13519
13520/* Follow reference or signature attribute ATTR of SRC_DIE.
13521 On entry *REF_CU is the CU of SRC_DIE.
13522 On exit *REF_CU is the CU of the result. */
13523
13524static struct die_info *
13525follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13526 struct dwarf2_cu **ref_cu)
13527{
13528 struct die_info *die;
13529
13530 if (is_ref_attr (attr))
13531 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 13532 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
13533 die = follow_die_sig (src_die, attr, ref_cu);
13534 else
13535 {
13536 dump_die_for_error (src_die);
13537 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13538 (*ref_cu)->objfile->name);
13539 }
13540
13541 return die;
03dd20cc
DJ
13542}
13543
5c631832 13544/* Follow reference OFFSET.
673bfd45
DE
13545 On entry *REF_CU is the CU of the source die referencing OFFSET.
13546 On exit *REF_CU is the CU of the result.
13547 Returns NULL if OFFSET is invalid. */
f504f079 13548
f9aca02d 13549static struct die_info *
5c631832 13550follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13551{
10b3939b 13552 struct die_info temp_die;
f2f0e013 13553 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13554
348e048f
DE
13555 gdb_assert (cu->per_cu != NULL);
13556
98bfdba5
PA
13557 target_cu = cu;
13558
348e048f
DE
13559 if (cu->per_cu->from_debug_types)
13560 {
13561 /* .debug_types CUs cannot reference anything outside their CU.
13562 If they need to, they have to reference a signatured type via
55f1336d 13563 DW_FORM_ref_sig8. */
348e048f 13564 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13565 return NULL;
348e048f
DE
13566 }
13567 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13568 {
13569 struct dwarf2_per_cu_data *per_cu;
9a619af0 13570
45452591 13571 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13572
13573 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13574 if (maybe_queue_comp_unit (cu, per_cu))
13575 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13576
10b3939b
DJ
13577 target_cu = per_cu->cu;
13578 }
98bfdba5
PA
13579 else if (cu->dies == NULL)
13580 {
13581 /* We're loading full DIEs during partial symbol reading. */
13582 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13583 load_full_comp_unit (cu->per_cu, cu->objfile);
13584 }
c906108c 13585
f2f0e013 13586 *ref_cu = target_cu;
51545339 13587 temp_die.offset = offset;
5c631832
JK
13588 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13589}
10b3939b 13590
5c631832
JK
13591/* Follow reference attribute ATTR of SRC_DIE.
13592 On entry *REF_CU is the CU of SRC_DIE.
13593 On exit *REF_CU is the CU of the result. */
13594
13595static struct die_info *
13596follow_die_ref (struct die_info *src_die, struct attribute *attr,
13597 struct dwarf2_cu **ref_cu)
13598{
13599 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13600 struct dwarf2_cu *cu = *ref_cu;
13601 struct die_info *die;
13602
13603 die = follow_die_offset (offset, ref_cu);
13604 if (!die)
13605 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13606 "at 0x%x [in module %s]"),
13607 offset, src_die->offset, cu->objfile->name);
348e048f 13608
5c631832
JK
13609 return die;
13610}
13611
13612/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13613 value is intended for DW_OP_call*. */
13614
13615struct dwarf2_locexpr_baton
13616dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
13617 struct dwarf2_per_cu_data *per_cu,
13618 CORE_ADDR (*get_frame_pc) (void *baton),
13619 void *baton)
5c631832
JK
13620{
13621 struct dwarf2_cu *cu = per_cu->cu;
13622 struct die_info *die;
13623 struct attribute *attr;
13624 struct dwarf2_locexpr_baton retval;
13625
8cf6f0b1
TT
13626 dw2_setup (per_cu->objfile);
13627
5c631832
JK
13628 die = follow_die_offset (offset, &cu);
13629 if (!die)
13630 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13631 offset, per_cu->cu->objfile->name);
13632
13633 attr = dwarf2_attr (die, DW_AT_location, cu);
13634 if (!attr)
13635 {
13636 /* DWARF: "If there is no such attribute, then there is no effect.". */
13637
13638 retval.data = NULL;
13639 retval.size = 0;
13640 }
8cf6f0b1
TT
13641 else if (attr_form_is_section_offset (attr))
13642 {
13643 struct dwarf2_loclist_baton loclist_baton;
13644 CORE_ADDR pc = (*get_frame_pc) (baton);
13645 size_t size;
13646
13647 fill_in_loclist_baton (cu, &loclist_baton, attr);
13648
13649 retval.data = dwarf2_find_location_expression (&loclist_baton,
13650 &size, pc);
13651 retval.size = size;
13652 }
5c631832
JK
13653 else
13654 {
13655 if (!attr_form_is_block (attr))
13656 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13657 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13658 offset, per_cu->cu->objfile->name);
13659
13660 retval.data = DW_BLOCK (attr)->data;
13661 retval.size = DW_BLOCK (attr)->size;
13662 }
13663 retval.per_cu = cu->per_cu;
13664 return retval;
348e048f
DE
13665}
13666
8a9b8146
TT
13667/* Return the type of the DIE at DIE_OFFSET in the CU named by
13668 PER_CU. */
13669
13670struct type *
13671dwarf2_get_die_type (unsigned int die_offset,
13672 struct dwarf2_per_cu_data *per_cu)
13673{
8a9b8146 13674 dw2_setup (per_cu->objfile);
9ff3b74f 13675 return get_die_type_at_offset (die_offset, per_cu);
8a9b8146
TT
13676}
13677
348e048f
DE
13678/* Follow the signature attribute ATTR in SRC_DIE.
13679 On entry *REF_CU is the CU of SRC_DIE.
13680 On exit *REF_CU is the CU of the result. */
13681
13682static struct die_info *
13683follow_die_sig (struct die_info *src_die, struct attribute *attr,
13684 struct dwarf2_cu **ref_cu)
13685{
13686 struct objfile *objfile = (*ref_cu)->objfile;
13687 struct die_info temp_die;
13688 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13689 struct dwarf2_cu *sig_cu;
13690 struct die_info *die;
13691
13692 /* sig_type will be NULL if the signatured type is missing from
13693 the debug info. */
13694 if (sig_type == NULL)
13695 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13696 "at 0x%x [in module %s]"),
13697 src_die->offset, objfile->name);
13698
13699 /* If necessary, add it to the queue and load its DIEs. */
13700
13701 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13702 read_signatured_type (objfile, sig_type);
13703
13704 gdb_assert (sig_type->per_cu.cu != NULL);
13705
13706 sig_cu = sig_type->per_cu.cu;
13707 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13708 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13709 if (die)
13710 {
13711 *ref_cu = sig_cu;
13712 return die;
13713 }
13714
3e43a32a
MS
13715 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13716 "from DIE at 0x%x [in module %s]"),
348e048f
DE
13717 sig_type->type_offset, src_die->offset, objfile->name);
13718}
13719
13720/* Given an offset of a signatured type, return its signatured_type. */
13721
13722static struct signatured_type *
13723lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13724{
13725 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13726 unsigned int length, initial_length_size;
13727 unsigned int sig_offset;
13728 struct signatured_type find_entry, *type_sig;
13729
13730 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13731 sig_offset = (initial_length_size
13732 + 2 /*version*/
13733 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13734 + 1 /*address_size*/);
13735 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13736 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13737
13738 /* This is only used to lookup previously recorded types.
13739 If we didn't find it, it's our bug. */
13740 gdb_assert (type_sig != NULL);
13741 gdb_assert (offset == type_sig->offset);
13742
13743 return type_sig;
13744}
13745
13746/* Read in signatured type at OFFSET and build its CU and die(s). */
13747
13748static void
13749read_signatured_type_at_offset (struct objfile *objfile,
13750 unsigned int offset)
13751{
13752 struct signatured_type *type_sig;
13753
be391dca
TT
13754 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13755
348e048f
DE
13756 /* We have the section offset, but we need the signature to do the
13757 hash table lookup. */
13758 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13759
13760 gdb_assert (type_sig->per_cu.cu == NULL);
13761
13762 read_signatured_type (objfile, type_sig);
13763
13764 gdb_assert (type_sig->per_cu.cu != NULL);
13765}
13766
13767/* Read in a signatured type and build its CU and DIEs. */
13768
13769static void
13770read_signatured_type (struct objfile *objfile,
13771 struct signatured_type *type_sig)
13772{
1fd400ff 13773 gdb_byte *types_ptr;
348e048f
DE
13774 struct die_reader_specs reader_specs;
13775 struct dwarf2_cu *cu;
13776 ULONGEST signature;
13777 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13778
1fd400ff
TT
13779 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13780 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13781
348e048f
DE
13782 gdb_assert (type_sig->per_cu.cu == NULL);
13783
9816fde3
JK
13784 cu = xmalloc (sizeof (*cu));
13785 init_one_comp_unit (cu, objfile);
13786
348e048f
DE
13787 type_sig->per_cu.cu = cu;
13788 cu->per_cu = &type_sig->per_cu;
13789
13790 /* If an error occurs while loading, release our storage. */
13791 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13792
13793 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13794 types_ptr, objfile->obfd);
13795 gdb_assert (signature == type_sig->signature);
13796
13797 cu->die_hash
13798 = htab_create_alloc_ex (cu->header.length / 12,
13799 die_hash,
13800 die_eq,
13801 NULL,
13802 &cu->comp_unit_obstack,
13803 hashtab_obstack_allocate,
13804 dummy_obstack_deallocate);
13805
13806 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13807 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13808
13809 init_cu_die_reader (&reader_specs, cu);
13810
13811 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13812 NULL /*parent*/);
13813
13814 /* We try not to read any attributes in this function, because not
13815 all objfiles needed for references have been loaded yet, and symbol
13816 table processing isn't initialized. But we have to set the CU language,
13817 or we won't be able to build types correctly. */
9816fde3 13818 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
13819
13820 do_cleanups (back_to);
13821
13822 /* We've successfully allocated this compilation unit. Let our caller
13823 clean it up when finished with it. */
13824 discard_cleanups (free_cu_cleanup);
13825
13826 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13827 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13828}
13829
c906108c
SS
13830/* Decode simple location descriptions.
13831 Given a pointer to a dwarf block that defines a location, compute
13832 the location and return the value.
13833
4cecd739
DJ
13834 NOTE drow/2003-11-18: This function is called in two situations
13835 now: for the address of static or global variables (partial symbols
13836 only) and for offsets into structures which are expected to be
13837 (more or less) constant. The partial symbol case should go away,
13838 and only the constant case should remain. That will let this
13839 function complain more accurately. A few special modes are allowed
13840 without complaint for global variables (for instance, global
13841 register values and thread-local values).
c906108c
SS
13842
13843 A location description containing no operations indicates that the
4cecd739 13844 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13845 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13846 callers will only want a very basic result and this can become a
13847 complaint.
c906108c 13848
d53d4ac5 13849 Note that stack[0] is unused except as a default error return. */
c906108c
SS
13850
13851static CORE_ADDR
e7c27a73 13852decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13853{
e7c27a73 13854 struct objfile *objfile = cu->objfile;
c906108c
SS
13855 int i;
13856 int size = blk->size;
fe1b8b76 13857 gdb_byte *data = blk->data;
c906108c
SS
13858 CORE_ADDR stack[64];
13859 int stacki;
13860 unsigned int bytes_read, unsnd;
fe1b8b76 13861 gdb_byte op;
c906108c
SS
13862
13863 i = 0;
13864 stacki = 0;
13865 stack[stacki] = 0;
d53d4ac5 13866 stack[++stacki] = 0;
c906108c
SS
13867
13868 while (i < size)
13869 {
c906108c
SS
13870 op = data[i++];
13871 switch (op)
13872 {
f1bea926
JM
13873 case DW_OP_lit0:
13874 case DW_OP_lit1:
13875 case DW_OP_lit2:
13876 case DW_OP_lit3:
13877 case DW_OP_lit4:
13878 case DW_OP_lit5:
13879 case DW_OP_lit6:
13880 case DW_OP_lit7:
13881 case DW_OP_lit8:
13882 case DW_OP_lit9:
13883 case DW_OP_lit10:
13884 case DW_OP_lit11:
13885 case DW_OP_lit12:
13886 case DW_OP_lit13:
13887 case DW_OP_lit14:
13888 case DW_OP_lit15:
13889 case DW_OP_lit16:
13890 case DW_OP_lit17:
13891 case DW_OP_lit18:
13892 case DW_OP_lit19:
13893 case DW_OP_lit20:
13894 case DW_OP_lit21:
13895 case DW_OP_lit22:
13896 case DW_OP_lit23:
13897 case DW_OP_lit24:
13898 case DW_OP_lit25:
13899 case DW_OP_lit26:
13900 case DW_OP_lit27:
13901 case DW_OP_lit28:
13902 case DW_OP_lit29:
13903 case DW_OP_lit30:
13904 case DW_OP_lit31:
13905 stack[++stacki] = op - DW_OP_lit0;
13906 break;
13907
c906108c
SS
13908 case DW_OP_reg0:
13909 case DW_OP_reg1:
13910 case DW_OP_reg2:
13911 case DW_OP_reg3:
13912 case DW_OP_reg4:
13913 case DW_OP_reg5:
13914 case DW_OP_reg6:
13915 case DW_OP_reg7:
13916 case DW_OP_reg8:
13917 case DW_OP_reg9:
13918 case DW_OP_reg10:
13919 case DW_OP_reg11:
13920 case DW_OP_reg12:
13921 case DW_OP_reg13:
13922 case DW_OP_reg14:
13923 case DW_OP_reg15:
13924 case DW_OP_reg16:
13925 case DW_OP_reg17:
13926 case DW_OP_reg18:
13927 case DW_OP_reg19:
13928 case DW_OP_reg20:
13929 case DW_OP_reg21:
13930 case DW_OP_reg22:
13931 case DW_OP_reg23:
13932 case DW_OP_reg24:
13933 case DW_OP_reg25:
13934 case DW_OP_reg26:
13935 case DW_OP_reg27:
13936 case DW_OP_reg28:
13937 case DW_OP_reg29:
13938 case DW_OP_reg30:
13939 case DW_OP_reg31:
c906108c 13940 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13941 if (i < size)
13942 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13943 break;
13944
13945 case DW_OP_regx:
c906108c
SS
13946 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13947 i += bytes_read;
c906108c 13948 stack[++stacki] = unsnd;
4cecd739
DJ
13949 if (i < size)
13950 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13951 break;
13952
13953 case DW_OP_addr:
107d2387 13954 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13955 cu, &bytes_read);
107d2387 13956 i += bytes_read;
c906108c
SS
13957 break;
13958
13959 case DW_OP_const1u:
13960 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13961 i += 1;
13962 break;
13963
13964 case DW_OP_const1s:
13965 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13966 i += 1;
13967 break;
13968
13969 case DW_OP_const2u:
13970 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13971 i += 2;
13972 break;
13973
13974 case DW_OP_const2s:
13975 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13976 i += 2;
13977 break;
13978
13979 case DW_OP_const4u:
13980 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13981 i += 4;
13982 break;
13983
13984 case DW_OP_const4s:
13985 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13986 i += 4;
13987 break;
13988
13989 case DW_OP_constu:
13990 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13991 &bytes_read);
c906108c
SS
13992 i += bytes_read;
13993 break;
13994
13995 case DW_OP_consts:
13996 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13997 i += bytes_read;
13998 break;
13999
f1bea926
JM
14000 case DW_OP_dup:
14001 stack[stacki + 1] = stack[stacki];
14002 stacki++;
14003 break;
14004
c906108c
SS
14005 case DW_OP_plus:
14006 stack[stacki - 1] += stack[stacki];
14007 stacki--;
14008 break;
14009
14010 case DW_OP_plus_uconst:
3e43a32a
MS
14011 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14012 &bytes_read);
c906108c
SS
14013 i += bytes_read;
14014 break;
14015
14016 case DW_OP_minus:
f1bea926 14017 stack[stacki - 1] -= stack[stacki];
c906108c
SS
14018 stacki--;
14019 break;
14020
7a292a7a 14021 case DW_OP_deref:
7a292a7a 14022 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
14023 this using GDB's address_class enum. This is valid for partial
14024 global symbols, although the variable's address will be bogus
14025 in the psymtab. */
7a292a7a 14026 if (i < size)
4d3c2250 14027 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
14028 break;
14029
9d774e44 14030 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
14031 /* The top of the stack has the offset from the beginning
14032 of the thread control block at which the variable is located. */
14033 /* Nothing should follow this operator, so the top of stack would
14034 be returned. */
4cecd739
DJ
14035 /* This is valid for partial global symbols, but the variable's
14036 address will be bogus in the psymtab. */
9d774e44 14037 if (i < size)
4d3c2250 14038 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
14039 break;
14040
42be36b3
CT
14041 case DW_OP_GNU_uninit:
14042 break;
14043
c906108c 14044 default:
b1bfef65
TT
14045 {
14046 const char *name = dwarf_stack_op_name (op);
14047
14048 if (name)
14049 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14050 name);
14051 else
14052 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14053 op);
14054 }
14055
c906108c
SS
14056 return (stack[stacki]);
14057 }
d53d4ac5
TT
14058
14059 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14060 outside of the allocated space. Also enforce minimum>0. */
14061 if (stacki >= ARRAY_SIZE (stack) - 1)
14062 {
14063 complaint (&symfile_complaints,
14064 _("location description stack overflow"));
14065 return 0;
14066 }
14067
14068 if (stacki <= 0)
14069 {
14070 complaint (&symfile_complaints,
14071 _("location description stack underflow"));
14072 return 0;
14073 }
c906108c
SS
14074 }
14075 return (stack[stacki]);
14076}
14077
14078/* memory allocation interface */
14079
c906108c 14080static struct dwarf_block *
7b5a2f43 14081dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14082{
14083 struct dwarf_block *blk;
14084
14085 blk = (struct dwarf_block *)
7b5a2f43 14086 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14087 return (blk);
14088}
14089
14090static struct abbrev_info *
f3dd6933 14091dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14092{
14093 struct abbrev_info *abbrev;
14094
f3dd6933
DJ
14095 abbrev = (struct abbrev_info *)
14096 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14097 memset (abbrev, 0, sizeof (struct abbrev_info));
14098 return (abbrev);
14099}
14100
14101static struct die_info *
b60c80d6 14102dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14103{
14104 struct die_info *die;
b60c80d6
DJ
14105 size_t size = sizeof (struct die_info);
14106
14107 if (num_attrs > 1)
14108 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14109
b60c80d6 14110 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14111 memset (die, 0, sizeof (struct die_info));
14112 return (die);
14113}
2e276125
JB
14114
14115\f
14116/* Macro support. */
14117
2e276125
JB
14118/* Return the full name of file number I in *LH's file name table.
14119 Use COMP_DIR as the name of the current directory of the
14120 compilation. The result is allocated using xmalloc; the caller is
14121 responsible for freeing it. */
14122static char *
14123file_full_name (int file, struct line_header *lh, const char *comp_dir)
14124{
6a83a1e6
EZ
14125 /* Is the file number a valid index into the line header's file name
14126 table? Remember that file numbers start with one, not zero. */
14127 if (1 <= file && file <= lh->num_file_names)
14128 {
14129 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14130
6a83a1e6
EZ
14131 if (IS_ABSOLUTE_PATH (fe->name))
14132 return xstrdup (fe->name);
14133 else
14134 {
14135 const char *dir;
14136 int dir_len;
14137 char *full_name;
14138
14139 if (fe->dir_index)
14140 dir = lh->include_dirs[fe->dir_index - 1];
14141 else
14142 dir = comp_dir;
14143
14144 if (dir)
14145 {
14146 dir_len = strlen (dir);
14147 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14148 strcpy (full_name, dir);
14149 full_name[dir_len] = '/';
14150 strcpy (full_name + dir_len + 1, fe->name);
14151 return full_name;
14152 }
14153 else
14154 return xstrdup (fe->name);
14155 }
14156 }
2e276125
JB
14157 else
14158 {
6a83a1e6
EZ
14159 /* The compiler produced a bogus file number. We can at least
14160 record the macro definitions made in the file, even if we
14161 won't be able to find the file by name. */
14162 char fake_name[80];
9a619af0 14163
6a83a1e6 14164 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14165
6e70227d 14166 complaint (&symfile_complaints,
6a83a1e6
EZ
14167 _("bad file number in macro information (%d)"),
14168 file);
2e276125 14169
6a83a1e6 14170 return xstrdup (fake_name);
2e276125
JB
14171 }
14172}
14173
14174
14175static struct macro_source_file *
14176macro_start_file (int file, int line,
14177 struct macro_source_file *current_file,
14178 const char *comp_dir,
14179 struct line_header *lh, struct objfile *objfile)
14180{
14181 /* The full name of this source file. */
14182 char *full_name = file_full_name (file, lh, comp_dir);
14183
14184 /* We don't create a macro table for this compilation unit
14185 at all until we actually get a filename. */
14186 if (! pending_macros)
4a146b47 14187 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14188 objfile->macro_cache);
2e276125
JB
14189
14190 if (! current_file)
14191 /* If we have no current file, then this must be the start_file
14192 directive for the compilation unit's main source file. */
14193 current_file = macro_set_main (pending_macros, full_name);
14194 else
14195 current_file = macro_include (current_file, line, full_name);
14196
14197 xfree (full_name);
6e70227d 14198
2e276125
JB
14199 return current_file;
14200}
14201
14202
14203/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14204 followed by a null byte. */
14205static char *
14206copy_string (const char *buf, int len)
14207{
14208 char *s = xmalloc (len + 1);
9a619af0 14209
2e276125
JB
14210 memcpy (s, buf, len);
14211 s[len] = '\0';
2e276125
JB
14212 return s;
14213}
14214
14215
14216static const char *
14217consume_improper_spaces (const char *p, const char *body)
14218{
14219 if (*p == ' ')
14220 {
4d3c2250 14221 complaint (&symfile_complaints,
3e43a32a
MS
14222 _("macro definition contains spaces "
14223 "in formal argument list:\n`%s'"),
4d3c2250 14224 body);
2e276125
JB
14225
14226 while (*p == ' ')
14227 p++;
14228 }
14229
14230 return p;
14231}
14232
14233
14234static void
14235parse_macro_definition (struct macro_source_file *file, int line,
14236 const char *body)
14237{
14238 const char *p;
14239
14240 /* The body string takes one of two forms. For object-like macro
14241 definitions, it should be:
14242
14243 <macro name> " " <definition>
14244
14245 For function-like macro definitions, it should be:
14246
14247 <macro name> "() " <definition>
14248 or
14249 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14250
14251 Spaces may appear only where explicitly indicated, and in the
14252 <definition>.
14253
14254 The Dwarf 2 spec says that an object-like macro's name is always
14255 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14256 the space when the macro's definition is the empty string.
2e276125
JB
14257
14258 The Dwarf 2 spec says that there should be no spaces between the
14259 formal arguments in a function-like macro's formal argument list,
14260 but versions of GCC around March 2002 include spaces after the
14261 commas. */
14262
14263
14264 /* Find the extent of the macro name. The macro name is terminated
14265 by either a space or null character (for an object-like macro) or
14266 an opening paren (for a function-like macro). */
14267 for (p = body; *p; p++)
14268 if (*p == ' ' || *p == '(')
14269 break;
14270
14271 if (*p == ' ' || *p == '\0')
14272 {
14273 /* It's an object-like macro. */
14274 int name_len = p - body;
14275 char *name = copy_string (body, name_len);
14276 const char *replacement;
14277
14278 if (*p == ' ')
14279 replacement = body + name_len + 1;
14280 else
14281 {
4d3c2250 14282 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14283 replacement = body + name_len;
14284 }
6e70227d 14285
2e276125
JB
14286 macro_define_object (file, line, name, replacement);
14287
14288 xfree (name);
14289 }
14290 else if (*p == '(')
14291 {
14292 /* It's a function-like macro. */
14293 char *name = copy_string (body, p - body);
14294 int argc = 0;
14295 int argv_size = 1;
14296 char **argv = xmalloc (argv_size * sizeof (*argv));
14297
14298 p++;
14299
14300 p = consume_improper_spaces (p, body);
14301
14302 /* Parse the formal argument list. */
14303 while (*p && *p != ')')
14304 {
14305 /* Find the extent of the current argument name. */
14306 const char *arg_start = p;
14307
14308 while (*p && *p != ',' && *p != ')' && *p != ' ')
14309 p++;
14310
14311 if (! *p || p == arg_start)
4d3c2250 14312 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14313 else
14314 {
14315 /* Make sure argv has room for the new argument. */
14316 if (argc >= argv_size)
14317 {
14318 argv_size *= 2;
14319 argv = xrealloc (argv, argv_size * sizeof (*argv));
14320 }
14321
14322 argv[argc++] = copy_string (arg_start, p - arg_start);
14323 }
14324
14325 p = consume_improper_spaces (p, body);
14326
14327 /* Consume the comma, if present. */
14328 if (*p == ',')
14329 {
14330 p++;
14331
14332 p = consume_improper_spaces (p, body);
14333 }
14334 }
14335
14336 if (*p == ')')
14337 {
14338 p++;
14339
14340 if (*p == ' ')
14341 /* Perfectly formed definition, no complaints. */
14342 macro_define_function (file, line, name,
6e70227d 14343 argc, (const char **) argv,
2e276125
JB
14344 p + 1);
14345 else if (*p == '\0')
14346 {
14347 /* Complain, but do define it. */
4d3c2250 14348 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14349 macro_define_function (file, line, name,
6e70227d 14350 argc, (const char **) argv,
2e276125
JB
14351 p);
14352 }
14353 else
14354 /* Just complain. */
4d3c2250 14355 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14356 }
14357 else
14358 /* Just complain. */
4d3c2250 14359 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14360
14361 xfree (name);
14362 {
14363 int i;
14364
14365 for (i = 0; i < argc; i++)
14366 xfree (argv[i]);
14367 }
14368 xfree (argv);
14369 }
14370 else
4d3c2250 14371 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14372}
14373
14374
14375static void
14376dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14377 char *comp_dir, bfd *abfd,
e7c27a73 14378 struct dwarf2_cu *cu)
2e276125 14379{
fe1b8b76 14380 gdb_byte *mac_ptr, *mac_end;
2e276125 14381 struct macro_source_file *current_file = 0;
757a13d0
JK
14382 enum dwarf_macinfo_record_type macinfo_type;
14383 int at_commandline;
2e276125 14384
be391dca
TT
14385 dwarf2_read_section (dwarf2_per_objfile->objfile,
14386 &dwarf2_per_objfile->macinfo);
dce234bc 14387 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 14388 {
e2e0b3e5 14389 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
14390 return;
14391 }
14392
757a13d0
JK
14393 /* First pass: Find the name of the base filename.
14394 This filename is needed in order to process all macros whose definition
14395 (or undefinition) comes from the command line. These macros are defined
14396 before the first DW_MACINFO_start_file entry, and yet still need to be
14397 associated to the base file.
14398
14399 To determine the base file name, we scan the macro definitions until we
14400 reach the first DW_MACINFO_start_file entry. We then initialize
14401 CURRENT_FILE accordingly so that any macro definition found before the
14402 first DW_MACINFO_start_file can still be associated to the base file. */
14403
dce234bc
PP
14404 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14405 mac_end = dwarf2_per_objfile->macinfo.buffer
14406 + dwarf2_per_objfile->macinfo.size;
2e276125 14407
757a13d0 14408 do
2e276125 14409 {
2e276125
JB
14410 /* Do we at least have room for a macinfo type byte? */
14411 if (mac_ptr >= mac_end)
14412 {
757a13d0 14413 /* Complaint is printed during the second pass as GDB will probably
3e43a32a
MS
14414 stop the first pass earlier upon finding
14415 DW_MACINFO_start_file. */
757a13d0 14416 break;
2e276125
JB
14417 }
14418
14419 macinfo_type = read_1_byte (abfd, mac_ptr);
14420 mac_ptr++;
14421
14422 switch (macinfo_type)
14423 {
14424 /* A zero macinfo type indicates the end of the macro
14425 information. */
14426 case 0:
757a13d0
JK
14427 break;
14428
14429 case DW_MACINFO_define:
14430 case DW_MACINFO_undef:
14431 /* Only skip the data by MAC_PTR. */
14432 {
14433 unsigned int bytes_read;
14434
14435 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14436 mac_ptr += bytes_read;
9b1c24c8 14437 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14438 mac_ptr += bytes_read;
14439 }
14440 break;
14441
14442 case DW_MACINFO_start_file:
14443 {
14444 unsigned int bytes_read;
14445 int line, file;
14446
14447 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14448 mac_ptr += bytes_read;
14449 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14450 mac_ptr += bytes_read;
14451
3e43a32a
MS
14452 current_file = macro_start_file (file, line, current_file,
14453 comp_dir, lh, cu->objfile);
757a13d0
JK
14454 }
14455 break;
14456
14457 case DW_MACINFO_end_file:
14458 /* No data to skip by MAC_PTR. */
14459 break;
14460
14461 case DW_MACINFO_vendor_ext:
14462 /* Only skip the data by MAC_PTR. */
14463 {
14464 unsigned int bytes_read;
14465
14466 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14467 mac_ptr += bytes_read;
9b1c24c8 14468 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14469 mac_ptr += bytes_read;
14470 }
14471 break;
14472
14473 default:
14474 break;
14475 }
14476 } while (macinfo_type != 0 && current_file == NULL);
14477
14478 /* Second pass: Process all entries.
14479
14480 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14481 command-line macro definitions/undefinitions. This flag is unset when we
14482 reach the first DW_MACINFO_start_file entry. */
14483
dce234bc 14484 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
14485
14486 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14487 GDB is still reading the definitions from command line. First
14488 DW_MACINFO_start_file will need to be ignored as it was already executed
14489 to create CURRENT_FILE for the main source holding also the command line
14490 definitions. On first met DW_MACINFO_start_file this flag is reset to
14491 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14492
14493 at_commandline = 1;
14494
14495 do
14496 {
14497 /* Do we at least have room for a macinfo type byte? */
14498 if (mac_ptr >= mac_end)
14499 {
14500 dwarf2_macros_too_long_complaint ();
14501 break;
14502 }
14503
14504 macinfo_type = read_1_byte (abfd, mac_ptr);
14505 mac_ptr++;
14506
14507 switch (macinfo_type)
14508 {
14509 /* A zero macinfo type indicates the end of the macro
14510 information. */
14511 case 0:
14512 break;
2e276125
JB
14513
14514 case DW_MACINFO_define:
14515 case DW_MACINFO_undef:
14516 {
891d2f0b 14517 unsigned int bytes_read;
2e276125
JB
14518 int line;
14519 char *body;
14520
14521 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14522 mac_ptr += bytes_read;
9b1c24c8 14523 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14524 mac_ptr += bytes_read;
14525
14526 if (! current_file)
757a13d0
JK
14527 {
14528 /* DWARF violation as no main source is present. */
14529 complaint (&symfile_complaints,
14530 _("debug info with no main source gives macro %s "
14531 "on line %d: %s"),
6e70227d
DE
14532 macinfo_type == DW_MACINFO_define ?
14533 _("definition") :
905e0470
PM
14534 macinfo_type == DW_MACINFO_undef ?
14535 _("undefinition") :
14536 _("something-or-other"), line, body);
757a13d0
JK
14537 break;
14538 }
3e43a32a
MS
14539 if ((line == 0 && !at_commandline)
14540 || (line != 0 && at_commandline))
4d3c2250 14541 complaint (&symfile_complaints,
757a13d0
JK
14542 _("debug info gives %s macro %s with %s line %d: %s"),
14543 at_commandline ? _("command-line") : _("in-file"),
905e0470 14544 macinfo_type == DW_MACINFO_define ?
6e70227d 14545 _("definition") :
905e0470
PM
14546 macinfo_type == DW_MACINFO_undef ?
14547 _("undefinition") :
14548 _("something-or-other"),
757a13d0
JK
14549 line == 0 ? _("zero") : _("non-zero"), line, body);
14550
14551 if (macinfo_type == DW_MACINFO_define)
14552 parse_macro_definition (current_file, line, body);
14553 else if (macinfo_type == DW_MACINFO_undef)
14554 macro_undef (current_file, line, body);
2e276125
JB
14555 }
14556 break;
14557
14558 case DW_MACINFO_start_file:
14559 {
891d2f0b 14560 unsigned int bytes_read;
2e276125
JB
14561 int line, file;
14562
14563 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14564 mac_ptr += bytes_read;
14565 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14566 mac_ptr += bytes_read;
14567
3e43a32a
MS
14568 if ((line == 0 && !at_commandline)
14569 || (line != 0 && at_commandline))
757a13d0
JK
14570 complaint (&symfile_complaints,
14571 _("debug info gives source %d included "
14572 "from %s at %s line %d"),
14573 file, at_commandline ? _("command-line") : _("file"),
14574 line == 0 ? _("zero") : _("non-zero"), line);
14575
14576 if (at_commandline)
14577 {
14578 /* This DW_MACINFO_start_file was executed in the pass one. */
14579 at_commandline = 0;
14580 }
14581 else
14582 current_file = macro_start_file (file, line,
14583 current_file, comp_dir,
14584 lh, cu->objfile);
2e276125
JB
14585 }
14586 break;
14587
14588 case DW_MACINFO_end_file:
14589 if (! current_file)
4d3c2250 14590 complaint (&symfile_complaints,
3e43a32a
MS
14591 _("macro debug info has an unmatched "
14592 "`close_file' directive"));
2e276125
JB
14593 else
14594 {
14595 current_file = current_file->included_by;
14596 if (! current_file)
14597 {
14598 enum dwarf_macinfo_record_type next_type;
14599
14600 /* GCC circa March 2002 doesn't produce the zero
14601 type byte marking the end of the compilation
14602 unit. Complain if it's not there, but exit no
14603 matter what. */
14604
14605 /* Do we at least have room for a macinfo type byte? */
14606 if (mac_ptr >= mac_end)
14607 {
4d3c2250 14608 dwarf2_macros_too_long_complaint ();
2e276125
JB
14609 return;
14610 }
14611
14612 /* We don't increment mac_ptr here, so this is just
14613 a look-ahead. */
14614 next_type = read_1_byte (abfd, mac_ptr);
14615 if (next_type != 0)
4d3c2250 14616 complaint (&symfile_complaints,
3e43a32a
MS
14617 _("no terminating 0-type entry for "
14618 "macros in `.debug_macinfo' section"));
2e276125
JB
14619
14620 return;
14621 }
14622 }
14623 break;
14624
14625 case DW_MACINFO_vendor_ext:
14626 {
891d2f0b 14627 unsigned int bytes_read;
2e276125 14628 int constant;
2e276125
JB
14629
14630 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14631 mac_ptr += bytes_read;
e8e80198 14632 read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14633 mac_ptr += bytes_read;
14634
14635 /* We don't recognize any vendor extensions. */
14636 }
14637 break;
14638 }
757a13d0 14639 } while (macinfo_type != 0);
2e276125 14640}
8e19ed76
PS
14641
14642/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 14643 if so return true else false. */
8e19ed76
PS
14644static int
14645attr_form_is_block (struct attribute *attr)
14646{
14647 return (attr == NULL ? 0 :
14648 attr->form == DW_FORM_block1
14649 || attr->form == DW_FORM_block2
14650 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14651 || attr->form == DW_FORM_block
14652 || attr->form == DW_FORM_exprloc);
8e19ed76 14653}
4c2df51b 14654
c6a0999f
JB
14655/* Return non-zero if ATTR's value is a section offset --- classes
14656 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14657 You may use DW_UNSND (attr) to retrieve such offsets.
14658
14659 Section 7.5.4, "Attribute Encodings", explains that no attribute
14660 may have a value that belongs to more than one of these classes; it
14661 would be ambiguous if we did, because we use the same forms for all
14662 of them. */
3690dd37
JB
14663static int
14664attr_form_is_section_offset (struct attribute *attr)
14665{
14666 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14667 || attr->form == DW_FORM_data8
14668 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14669}
14670
14671
14672/* Return non-zero if ATTR's value falls in the 'constant' class, or
14673 zero otherwise. When this function returns true, you can apply
14674 dwarf2_get_attr_constant_value to it.
14675
14676 However, note that for some attributes you must check
14677 attr_form_is_section_offset before using this test. DW_FORM_data4
14678 and DW_FORM_data8 are members of both the constant class, and of
14679 the classes that contain offsets into other debug sections
14680 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14681 that, if an attribute's can be either a constant or one of the
14682 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14683 taken as section offsets, not constants. */
14684static int
14685attr_form_is_constant (struct attribute *attr)
14686{
14687 switch (attr->form)
14688 {
14689 case DW_FORM_sdata:
14690 case DW_FORM_udata:
14691 case DW_FORM_data1:
14692 case DW_FORM_data2:
14693 case DW_FORM_data4:
14694 case DW_FORM_data8:
14695 return 1;
14696 default:
14697 return 0;
14698 }
14699}
14700
8cf6f0b1
TT
14701/* A helper function that fills in a dwarf2_loclist_baton. */
14702
14703static void
14704fill_in_loclist_baton (struct dwarf2_cu *cu,
14705 struct dwarf2_loclist_baton *baton,
14706 struct attribute *attr)
14707{
14708 dwarf2_read_section (dwarf2_per_objfile->objfile,
14709 &dwarf2_per_objfile->loc);
14710
14711 baton->per_cu = cu->per_cu;
14712 gdb_assert (baton->per_cu);
14713 /* We don't know how long the location list is, but make sure we
14714 don't run off the edge of the section. */
14715 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14716 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14717 baton->base_address = cu->base_address;
14718}
14719
4c2df51b
DJ
14720static void
14721dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14722 struct dwarf2_cu *cu)
4c2df51b 14723{
3690dd37 14724 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14725 /* ".debug_loc" may not exist at all, or the offset may be outside
14726 the section. If so, fall through to the complaint in the
14727 other branch. */
9e0ac564
TT
14728 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14729 &dwarf2_per_objfile->loc))
4c2df51b 14730 {
0d53c4c4 14731 struct dwarf2_loclist_baton *baton;
4c2df51b 14732
4a146b47 14733 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14734 sizeof (struct dwarf2_loclist_baton));
4c2df51b 14735
8cf6f0b1 14736 fill_in_loclist_baton (cu, baton, attr);
be391dca 14737
d00adf39 14738 if (cu->base_known == 0)
0d53c4c4 14739 complaint (&symfile_complaints,
3e43a32a
MS
14740 _("Location list used without "
14741 "specifying the CU base address."));
4c2df51b 14742
768a979c 14743 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14744 SYMBOL_LOCATION_BATON (sym) = baton;
14745 }
14746 else
14747 {
14748 struct dwarf2_locexpr_baton *baton;
14749
4a146b47 14750 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14751 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14752 baton->per_cu = cu->per_cu;
14753 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14754
14755 if (attr_form_is_block (attr))
14756 {
14757 /* Note that we're just copying the block's data pointer
14758 here, not the actual data. We're still pointing into the
6502dd73
DJ
14759 info_buffer for SYM's objfile; right now we never release
14760 that buffer, but when we do clean up properly this may
14761 need to change. */
0d53c4c4
DJ
14762 baton->size = DW_BLOCK (attr)->size;
14763 baton->data = DW_BLOCK (attr)->data;
14764 }
14765 else
14766 {
14767 dwarf2_invalid_attrib_class_complaint ("location description",
14768 SYMBOL_NATURAL_NAME (sym));
14769 baton->size = 0;
14770 baton->data = NULL;
14771 }
6e70227d 14772
768a979c 14773 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14774 SYMBOL_LOCATION_BATON (sym) = baton;
14775 }
4c2df51b 14776}
6502dd73 14777
9aa1f1e3
TT
14778/* Return the OBJFILE associated with the compilation unit CU. If CU
14779 came from a separate debuginfo file, then the master objfile is
14780 returned. */
ae0d2f24
UW
14781
14782struct objfile *
14783dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14784{
9291a0cd 14785 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14786
14787 /* Return the master objfile, so that we can report and look up the
14788 correct file containing this variable. */
14789 if (objfile->separate_debug_objfile_backlink)
14790 objfile = objfile->separate_debug_objfile_backlink;
14791
14792 return objfile;
14793}
14794
14795/* Return the address size given in the compilation unit header for CU. */
14796
14797CORE_ADDR
14798dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14799{
14800 if (per_cu->cu)
14801 return per_cu->cu->header.addr_size;
14802 else
14803 {
14804 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14805 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14806 struct dwarf2_per_objfile *per_objfile
14807 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14808 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14809 struct comp_unit_head cu_header;
9a619af0 14810
ae0d2f24
UW
14811 memset (&cu_header, 0, sizeof cu_header);
14812 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14813 return cu_header.addr_size;
14814 }
14815}
14816
9eae7c52
TT
14817/* Return the offset size given in the compilation unit header for CU. */
14818
14819int
14820dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14821{
14822 if (per_cu->cu)
14823 return per_cu->cu->header.offset_size;
14824 else
14825 {
14826 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14827 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14828 struct dwarf2_per_objfile *per_objfile
14829 = objfile_data (objfile, dwarf2_objfile_data_key);
14830 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14831 struct comp_unit_head cu_header;
14832
14833 memset (&cu_header, 0, sizeof cu_header);
14834 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14835 return cu_header.offset_size;
14836 }
14837}
14838
9aa1f1e3
TT
14839/* Return the text offset of the CU. The returned offset comes from
14840 this CU's objfile. If this objfile came from a separate debuginfo
14841 file, then the offset may be different from the corresponding
14842 offset in the parent objfile. */
14843
14844CORE_ADDR
14845dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14846{
bb3fa9d0 14847 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14848
14849 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14850}
14851
348e048f
DE
14852/* Locate the .debug_info compilation unit from CU's objfile which contains
14853 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14854
14855static struct dwarf2_per_cu_data *
c764a876 14856dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14857 struct objfile *objfile)
14858{
14859 struct dwarf2_per_cu_data *this_cu;
14860 int low, high;
14861
ae038cb0
DJ
14862 low = 0;
14863 high = dwarf2_per_objfile->n_comp_units - 1;
14864 while (high > low)
14865 {
14866 int mid = low + (high - low) / 2;
9a619af0 14867
ae038cb0
DJ
14868 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14869 high = mid;
14870 else
14871 low = mid + 1;
14872 }
14873 gdb_assert (low == high);
14874 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14875 {
10b3939b 14876 if (low == 0)
8a3fe4f8
AC
14877 error (_("Dwarf Error: could not find partial DIE containing "
14878 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14879 (long) offset, bfd_get_filename (objfile->obfd));
14880
ae038cb0
DJ
14881 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14882 return dwarf2_per_objfile->all_comp_units[low-1];
14883 }
14884 else
14885 {
14886 this_cu = dwarf2_per_objfile->all_comp_units[low];
14887 if (low == dwarf2_per_objfile->n_comp_units - 1
14888 && offset >= this_cu->offset + this_cu->length)
c764a876 14889 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14890 gdb_assert (offset < this_cu->offset + this_cu->length);
14891 return this_cu;
14892 }
14893}
14894
10b3939b
DJ
14895/* Locate the compilation unit from OBJFILE which is located at exactly
14896 OFFSET. Raises an error on failure. */
14897
ae038cb0 14898static struct dwarf2_per_cu_data *
c764a876 14899dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14900{
14901 struct dwarf2_per_cu_data *this_cu;
9a619af0 14902
ae038cb0
DJ
14903 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14904 if (this_cu->offset != offset)
c764a876 14905 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14906 return this_cu;
14907}
14908
9816fde3 14909/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 14910
9816fde3
JK
14911static void
14912init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 14913{
9816fde3 14914 memset (cu, 0, sizeof (*cu));
93311388
DE
14915 cu->objfile = objfile;
14916 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
14917}
14918
14919/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14920
14921static void
14922prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14923{
14924 struct attribute *attr;
14925
14926 /* Set the language we're debugging. */
14927 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14928 if (attr)
14929 set_cu_language (DW_UNSND (attr), cu);
14930 else
9cded63f
TT
14931 {
14932 cu->language = language_minimal;
14933 cu->language_defn = language_def (cu->language);
14934 }
93311388
DE
14935}
14936
ae038cb0
DJ
14937/* Release one cached compilation unit, CU. We unlink it from the tree
14938 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14939 the caller is responsible for that.
14940 NOTE: DATA is a void * because this function is also used as a
14941 cleanup routine. */
ae038cb0
DJ
14942
14943static void
14944free_one_comp_unit (void *data)
14945{
14946 struct dwarf2_cu *cu = data;
14947
14948 if (cu->per_cu != NULL)
14949 cu->per_cu->cu = NULL;
14950 cu->per_cu = NULL;
14951
14952 obstack_free (&cu->comp_unit_obstack, NULL);
14953
14954 xfree (cu);
14955}
14956
72bf9492 14957/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14958 when we're finished with it. We can't free the pointer itself, but be
14959 sure to unlink it from the cache. Also release any associated storage
14960 and perform cache maintenance.
72bf9492
DJ
14961
14962 Only used during partial symbol parsing. */
14963
14964static void
14965free_stack_comp_unit (void *data)
14966{
14967 struct dwarf2_cu *cu = data;
14968
14969 obstack_free (&cu->comp_unit_obstack, NULL);
14970 cu->partial_dies = NULL;
ae038cb0
DJ
14971
14972 if (cu->per_cu != NULL)
14973 {
14974 /* This compilation unit is on the stack in our caller, so we
14975 should not xfree it. Just unlink it. */
14976 cu->per_cu->cu = NULL;
14977 cu->per_cu = NULL;
14978
14979 /* If we had a per-cu pointer, then we may have other compilation
14980 units loaded, so age them now. */
14981 age_cached_comp_units ();
14982 }
14983}
14984
14985/* Free all cached compilation units. */
14986
14987static void
14988free_cached_comp_units (void *data)
14989{
14990 struct dwarf2_per_cu_data *per_cu, **last_chain;
14991
14992 per_cu = dwarf2_per_objfile->read_in_chain;
14993 last_chain = &dwarf2_per_objfile->read_in_chain;
14994 while (per_cu != NULL)
14995 {
14996 struct dwarf2_per_cu_data *next_cu;
14997
14998 next_cu = per_cu->cu->read_in_chain;
14999
15000 free_one_comp_unit (per_cu->cu);
15001 *last_chain = next_cu;
15002
15003 per_cu = next_cu;
15004 }
15005}
15006
15007/* Increase the age counter on each cached compilation unit, and free
15008 any that are too old. */
15009
15010static void
15011age_cached_comp_units (void)
15012{
15013 struct dwarf2_per_cu_data *per_cu, **last_chain;
15014
15015 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15016 per_cu = dwarf2_per_objfile->read_in_chain;
15017 while (per_cu != NULL)
15018 {
15019 per_cu->cu->last_used ++;
15020 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15021 dwarf2_mark (per_cu->cu);
15022 per_cu = per_cu->cu->read_in_chain;
15023 }
15024
15025 per_cu = dwarf2_per_objfile->read_in_chain;
15026 last_chain = &dwarf2_per_objfile->read_in_chain;
15027 while (per_cu != NULL)
15028 {
15029 struct dwarf2_per_cu_data *next_cu;
15030
15031 next_cu = per_cu->cu->read_in_chain;
15032
15033 if (!per_cu->cu->mark)
15034 {
15035 free_one_comp_unit (per_cu->cu);
15036 *last_chain = next_cu;
15037 }
15038 else
15039 last_chain = &per_cu->cu->read_in_chain;
15040
15041 per_cu = next_cu;
15042 }
15043}
15044
15045/* Remove a single compilation unit from the cache. */
15046
15047static void
15048free_one_cached_comp_unit (void *target_cu)
15049{
15050 struct dwarf2_per_cu_data *per_cu, **last_chain;
15051
15052 per_cu = dwarf2_per_objfile->read_in_chain;
15053 last_chain = &dwarf2_per_objfile->read_in_chain;
15054 while (per_cu != NULL)
15055 {
15056 struct dwarf2_per_cu_data *next_cu;
15057
15058 next_cu = per_cu->cu->read_in_chain;
15059
15060 if (per_cu->cu == target_cu)
15061 {
15062 free_one_comp_unit (per_cu->cu);
15063 *last_chain = next_cu;
15064 break;
15065 }
15066 else
15067 last_chain = &per_cu->cu->read_in_chain;
15068
15069 per_cu = next_cu;
15070 }
15071}
15072
fe3e1990
DJ
15073/* Release all extra memory associated with OBJFILE. */
15074
15075void
15076dwarf2_free_objfile (struct objfile *objfile)
15077{
15078 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15079
15080 if (dwarf2_per_objfile == NULL)
15081 return;
15082
15083 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15084 free_cached_comp_units (NULL);
15085
7b9f3c50
DE
15086 if (dwarf2_per_objfile->quick_file_names_table)
15087 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 15088
fe3e1990
DJ
15089 /* Everything else should be on the objfile obstack. */
15090}
15091
1c379e20
DJ
15092/* A pair of DIE offset and GDB type pointer. We store these
15093 in a hash table separate from the DIEs, and preserve them
15094 when the DIEs are flushed out of cache. */
15095
15096struct dwarf2_offset_and_type
15097{
15098 unsigned int offset;
15099 struct type *type;
15100};
15101
15102/* Hash function for a dwarf2_offset_and_type. */
15103
15104static hashval_t
15105offset_and_type_hash (const void *item)
15106{
15107 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 15108
1c379e20
DJ
15109 return ofs->offset;
15110}
15111
15112/* Equality function for a dwarf2_offset_and_type. */
15113
15114static int
15115offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15116{
15117 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15118 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 15119
1c379e20
DJ
15120 return ofs_lhs->offset == ofs_rhs->offset;
15121}
15122
15123/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
15124 table if necessary. For convenience, return TYPE.
15125
15126 The DIEs reading must have careful ordering to:
15127 * Not cause infite loops trying to read in DIEs as a prerequisite for
15128 reading current DIE.
15129 * Not trying to dereference contents of still incompletely read in types
15130 while reading in other DIEs.
15131 * Enable referencing still incompletely read in types just by a pointer to
15132 the type without accessing its fields.
15133
15134 Therefore caller should follow these rules:
15135 * Try to fetch any prerequisite types we may need to build this DIE type
15136 before building the type and calling set_die_type.
e71ec853 15137 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
15138 possible before fetching more types to complete the current type.
15139 * Make the type as complete as possible before fetching more types. */
1c379e20 15140
f792889a 15141static struct type *
1c379e20
DJ
15142set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15143{
15144 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
15145 struct objfile *objfile = cu->objfile;
15146 htab_t *type_hash_ptr;
1c379e20 15147
b4ba55a1
JB
15148 /* For Ada types, make sure that the gnat-specific data is always
15149 initialized (if not already set). There are a few types where
15150 we should not be doing so, because the type-specific area is
15151 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15152 where the type-specific area is used to store the floatformat).
15153 But this is not a problem, because the gnat-specific information
15154 is actually not needed for these types. */
15155 if (need_gnat_info (cu)
15156 && TYPE_CODE (type) != TYPE_CODE_FUNC
15157 && TYPE_CODE (type) != TYPE_CODE_FLT
15158 && !HAVE_GNAT_AUX_INFO (type))
15159 INIT_GNAT_SPECIFIC (type);
15160
673bfd45
DE
15161 if (cu->per_cu->from_debug_types)
15162 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15163 else
15164 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15165
15166 if (*type_hash_ptr == NULL)
f792889a 15167 {
673bfd45
DE
15168 *type_hash_ptr
15169 = htab_create_alloc_ex (127,
f792889a
DJ
15170 offset_and_type_hash,
15171 offset_and_type_eq,
15172 NULL,
673bfd45 15173 &objfile->objfile_obstack,
f792889a
DJ
15174 hashtab_obstack_allocate,
15175 dummy_obstack_deallocate);
f792889a 15176 }
1c379e20
DJ
15177
15178 ofs.offset = die->offset;
15179 ofs.type = type;
15180 slot = (struct dwarf2_offset_and_type **)
673bfd45 15181 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
15182 if (*slot)
15183 complaint (&symfile_complaints,
15184 _("A problem internal to GDB: DIE 0x%x has type already set"),
15185 die->offset);
673bfd45 15186 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 15187 **slot = ofs;
f792889a 15188 return type;
1c379e20
DJ
15189}
15190
673bfd45
DE
15191/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15192 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
15193
15194static struct type *
673bfd45
DE
15195get_die_type_at_offset (unsigned int offset,
15196 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
15197{
15198 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 15199 htab_t type_hash;
f792889a 15200
673bfd45
DE
15201 if (per_cu->from_debug_types)
15202 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15203 else
15204 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
15205 if (type_hash == NULL)
15206 return NULL;
1c379e20 15207
673bfd45 15208 ofs.offset = offset;
1c379e20
DJ
15209 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15210 if (slot)
15211 return slot->type;
15212 else
15213 return NULL;
15214}
15215
673bfd45
DE
15216/* Look up the type for DIE in the appropriate type_hash table,
15217 or return NULL if DIE does not have a saved type. */
15218
15219static struct type *
15220get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15221{
15222 return get_die_type_at_offset (die->offset, cu->per_cu);
15223}
15224
10b3939b
DJ
15225/* Add a dependence relationship from CU to REF_PER_CU. */
15226
15227static void
15228dwarf2_add_dependence (struct dwarf2_cu *cu,
15229 struct dwarf2_per_cu_data *ref_per_cu)
15230{
15231 void **slot;
15232
15233 if (cu->dependencies == NULL)
15234 cu->dependencies
15235 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15236 NULL, &cu->comp_unit_obstack,
15237 hashtab_obstack_allocate,
15238 dummy_obstack_deallocate);
15239
15240 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15241 if (*slot == NULL)
15242 *slot = ref_per_cu;
15243}
1c379e20 15244
f504f079
DE
15245/* Subroutine of dwarf2_mark to pass to htab_traverse.
15246 Set the mark field in every compilation unit in the
ae038cb0
DJ
15247 cache that we must keep because we are keeping CU. */
15248
10b3939b
DJ
15249static int
15250dwarf2_mark_helper (void **slot, void *data)
15251{
15252 struct dwarf2_per_cu_data *per_cu;
15253
15254 per_cu = (struct dwarf2_per_cu_data *) *slot;
15255 if (per_cu->cu->mark)
15256 return 1;
15257 per_cu->cu->mark = 1;
15258
15259 if (per_cu->cu->dependencies != NULL)
15260 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15261
15262 return 1;
15263}
15264
f504f079
DE
15265/* Set the mark field in CU and in every other compilation unit in the
15266 cache that we must keep because we are keeping CU. */
15267
ae038cb0
DJ
15268static void
15269dwarf2_mark (struct dwarf2_cu *cu)
15270{
15271 if (cu->mark)
15272 return;
15273 cu->mark = 1;
10b3939b
DJ
15274 if (cu->dependencies != NULL)
15275 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
15276}
15277
15278static void
15279dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15280{
15281 while (per_cu)
15282 {
15283 per_cu->cu->mark = 0;
15284 per_cu = per_cu->cu->read_in_chain;
15285 }
72bf9492
DJ
15286}
15287
72bf9492
DJ
15288/* Trivial hash function for partial_die_info: the hash value of a DIE
15289 is its offset in .debug_info for this objfile. */
15290
15291static hashval_t
15292partial_die_hash (const void *item)
15293{
15294 const struct partial_die_info *part_die = item;
9a619af0 15295
72bf9492
DJ
15296 return part_die->offset;
15297}
15298
15299/* Trivial comparison function for partial_die_info structures: two DIEs
15300 are equal if they have the same offset. */
15301
15302static int
15303partial_die_eq (const void *item_lhs, const void *item_rhs)
15304{
15305 const struct partial_die_info *part_die_lhs = item_lhs;
15306 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 15307
72bf9492
DJ
15308 return part_die_lhs->offset == part_die_rhs->offset;
15309}
15310
ae038cb0
DJ
15311static struct cmd_list_element *set_dwarf2_cmdlist;
15312static struct cmd_list_element *show_dwarf2_cmdlist;
15313
15314static void
15315set_dwarf2_cmd (char *args, int from_tty)
15316{
15317 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15318}
15319
15320static void
15321show_dwarf2_cmd (char *args, int from_tty)
6e70227d 15322{
ae038cb0
DJ
15323 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15324}
15325
dce234bc
PP
15326/* If section described by INFO was mmapped, munmap it now. */
15327
15328static void
15329munmap_section_buffer (struct dwarf2_section_info *info)
15330{
15331 if (info->was_mmapped)
15332 {
15333#ifdef HAVE_MMAP
15334 intptr_t begin = (intptr_t) info->buffer;
15335 intptr_t map_begin = begin & ~(pagesize - 1);
15336 size_t map_length = info->size + begin - map_begin;
9a619af0 15337
dce234bc
PP
15338 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15339#else
15340 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 15341 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
15342#endif
15343 }
15344}
15345
15346/* munmap debug sections for OBJFILE, if necessary. */
15347
15348static void
c1bd65d0 15349dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
15350{
15351 struct dwarf2_per_objfile *data = d;
9a619af0 15352
16be1145
DE
15353 /* This is sorted according to the order they're defined in to make it easier
15354 to keep in sync. */
dce234bc
PP
15355 munmap_section_buffer (&data->info);
15356 munmap_section_buffer (&data->abbrev);
15357 munmap_section_buffer (&data->line);
16be1145 15358 munmap_section_buffer (&data->loc);
dce234bc 15359 munmap_section_buffer (&data->macinfo);
16be1145 15360 munmap_section_buffer (&data->str);
dce234bc 15361 munmap_section_buffer (&data->ranges);
16be1145 15362 munmap_section_buffer (&data->types);
dce234bc
PP
15363 munmap_section_buffer (&data->frame);
15364 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
15365 munmap_section_buffer (&data->gdb_index);
15366}
15367
15368\f
ae2de4f8 15369/* The "save gdb-index" command. */
9291a0cd
TT
15370
15371/* The contents of the hash table we create when building the string
15372 table. */
15373struct strtab_entry
15374{
15375 offset_type offset;
15376 const char *str;
15377};
15378
559a7a62
JK
15379/* Hash function for a strtab_entry.
15380
15381 Function is used only during write_hash_table so no index format backward
15382 compatibility is needed. */
b89be57b 15383
9291a0cd
TT
15384static hashval_t
15385hash_strtab_entry (const void *e)
15386{
15387 const struct strtab_entry *entry = e;
559a7a62 15388 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
15389}
15390
15391/* Equality function for a strtab_entry. */
b89be57b 15392
9291a0cd
TT
15393static int
15394eq_strtab_entry (const void *a, const void *b)
15395{
15396 const struct strtab_entry *ea = a;
15397 const struct strtab_entry *eb = b;
15398 return !strcmp (ea->str, eb->str);
15399}
15400
15401/* Create a strtab_entry hash table. */
b89be57b 15402
9291a0cd
TT
15403static htab_t
15404create_strtab (void)
15405{
15406 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15407 xfree, xcalloc, xfree);
15408}
15409
15410/* Add a string to the constant pool. Return the string's offset in
15411 host order. */
b89be57b 15412
9291a0cd
TT
15413static offset_type
15414add_string (htab_t table, struct obstack *cpool, const char *str)
15415{
15416 void **slot;
15417 struct strtab_entry entry;
15418 struct strtab_entry *result;
15419
15420 entry.str = str;
15421 slot = htab_find_slot (table, &entry, INSERT);
15422 if (*slot)
15423 result = *slot;
15424 else
15425 {
15426 result = XNEW (struct strtab_entry);
15427 result->offset = obstack_object_size (cpool);
15428 result->str = str;
15429 obstack_grow_str0 (cpool, str);
15430 *slot = result;
15431 }
15432 return result->offset;
15433}
15434
15435/* An entry in the symbol table. */
15436struct symtab_index_entry
15437{
15438 /* The name of the symbol. */
15439 const char *name;
15440 /* The offset of the name in the constant pool. */
15441 offset_type index_offset;
15442 /* A sorted vector of the indices of all the CUs that hold an object
15443 of this name. */
15444 VEC (offset_type) *cu_indices;
15445};
15446
15447/* The symbol table. This is a power-of-2-sized hash table. */
15448struct mapped_symtab
15449{
15450 offset_type n_elements;
15451 offset_type size;
15452 struct symtab_index_entry **data;
15453};
15454
15455/* Hash function for a symtab_index_entry. */
b89be57b 15456
9291a0cd
TT
15457static hashval_t
15458hash_symtab_entry (const void *e)
15459{
15460 const struct symtab_index_entry *entry = e;
15461 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15462 sizeof (offset_type) * VEC_length (offset_type,
15463 entry->cu_indices),
15464 0);
15465}
15466
15467/* Equality function for a symtab_index_entry. */
b89be57b 15468
9291a0cd
TT
15469static int
15470eq_symtab_entry (const void *a, const void *b)
15471{
15472 const struct symtab_index_entry *ea = a;
15473 const struct symtab_index_entry *eb = b;
15474 int len = VEC_length (offset_type, ea->cu_indices);
15475 if (len != VEC_length (offset_type, eb->cu_indices))
15476 return 0;
15477 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15478 VEC_address (offset_type, eb->cu_indices),
15479 sizeof (offset_type) * len);
15480}
15481
15482/* Destroy a symtab_index_entry. */
b89be57b 15483
9291a0cd
TT
15484static void
15485delete_symtab_entry (void *p)
15486{
15487 struct symtab_index_entry *entry = p;
15488 VEC_free (offset_type, entry->cu_indices);
15489 xfree (entry);
15490}
15491
15492/* Create a hash table holding symtab_index_entry objects. */
b89be57b 15493
9291a0cd 15494static htab_t
3876f04e 15495create_symbol_hash_table (void)
9291a0cd
TT
15496{
15497 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15498 delete_symtab_entry, xcalloc, xfree);
15499}
15500
15501/* Create a new mapped symtab object. */
b89be57b 15502
9291a0cd
TT
15503static struct mapped_symtab *
15504create_mapped_symtab (void)
15505{
15506 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15507 symtab->n_elements = 0;
15508 symtab->size = 1024;
15509 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15510 return symtab;
15511}
15512
15513/* Destroy a mapped_symtab. */
b89be57b 15514
9291a0cd
TT
15515static void
15516cleanup_mapped_symtab (void *p)
15517{
15518 struct mapped_symtab *symtab = p;
15519 /* The contents of the array are freed when the other hash table is
15520 destroyed. */
15521 xfree (symtab->data);
15522 xfree (symtab);
15523}
15524
15525/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
15526 the slot.
15527
15528 Function is used only during write_hash_table so no index format backward
15529 compatibility is needed. */
b89be57b 15530
9291a0cd
TT
15531static struct symtab_index_entry **
15532find_slot (struct mapped_symtab *symtab, const char *name)
15533{
559a7a62 15534 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
15535
15536 index = hash & (symtab->size - 1);
15537 step = ((hash * 17) & (symtab->size - 1)) | 1;
15538
15539 for (;;)
15540 {
15541 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15542 return &symtab->data[index];
15543 index = (index + step) & (symtab->size - 1);
15544 }
15545}
15546
15547/* Expand SYMTAB's hash table. */
b89be57b 15548
9291a0cd
TT
15549static void
15550hash_expand (struct mapped_symtab *symtab)
15551{
15552 offset_type old_size = symtab->size;
15553 offset_type i;
15554 struct symtab_index_entry **old_entries = symtab->data;
15555
15556 symtab->size *= 2;
15557 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15558
15559 for (i = 0; i < old_size; ++i)
15560 {
15561 if (old_entries[i])
15562 {
15563 struct symtab_index_entry **slot = find_slot (symtab,
15564 old_entries[i]->name);
15565 *slot = old_entries[i];
15566 }
15567 }
15568
15569 xfree (old_entries);
15570}
15571
15572/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15573 is the index of the CU in which the symbol appears. */
b89be57b 15574
9291a0cd
TT
15575static void
15576add_index_entry (struct mapped_symtab *symtab, const char *name,
15577 offset_type cu_index)
15578{
15579 struct symtab_index_entry **slot;
15580
15581 ++symtab->n_elements;
15582 if (4 * symtab->n_elements / 3 >= symtab->size)
15583 hash_expand (symtab);
15584
15585 slot = find_slot (symtab, name);
15586 if (!*slot)
15587 {
15588 *slot = XNEW (struct symtab_index_entry);
15589 (*slot)->name = name;
15590 (*slot)->cu_indices = NULL;
15591 }
15592 /* Don't push an index twice. Due to how we add entries we only
15593 have to check the last one. */
15594 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 15595 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
15596 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15597}
15598
15599/* Add a vector of indices to the constant pool. */
b89be57b 15600
9291a0cd 15601static offset_type
3876f04e 15602add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15603 struct symtab_index_entry *entry)
15604{
15605 void **slot;
15606
3876f04e 15607 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15608 if (!*slot)
15609 {
15610 offset_type len = VEC_length (offset_type, entry->cu_indices);
15611 offset_type val = MAYBE_SWAP (len);
15612 offset_type iter;
15613 int i;
15614
15615 *slot = entry;
15616 entry->index_offset = obstack_object_size (cpool);
15617
15618 obstack_grow (cpool, &val, sizeof (val));
15619 for (i = 0;
15620 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15621 ++i)
15622 {
15623 val = MAYBE_SWAP (iter);
15624 obstack_grow (cpool, &val, sizeof (val));
15625 }
15626 }
15627 else
15628 {
15629 struct symtab_index_entry *old_entry = *slot;
15630 entry->index_offset = old_entry->index_offset;
15631 entry = old_entry;
15632 }
15633 return entry->index_offset;
15634}
15635
15636/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15637 constant pool entries going into the obstack CPOOL. */
b89be57b 15638
9291a0cd
TT
15639static void
15640write_hash_table (struct mapped_symtab *symtab,
15641 struct obstack *output, struct obstack *cpool)
15642{
15643 offset_type i;
3876f04e 15644 htab_t symbol_hash_table;
9291a0cd
TT
15645 htab_t str_table;
15646
3876f04e 15647 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15648 str_table = create_strtab ();
3876f04e 15649
9291a0cd
TT
15650 /* We add all the index vectors to the constant pool first, to
15651 ensure alignment is ok. */
15652 for (i = 0; i < symtab->size; ++i)
15653 {
15654 if (symtab->data[i])
3876f04e 15655 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15656 }
15657
15658 /* Now write out the hash table. */
15659 for (i = 0; i < symtab->size; ++i)
15660 {
15661 offset_type str_off, vec_off;
15662
15663 if (symtab->data[i])
15664 {
15665 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15666 vec_off = symtab->data[i]->index_offset;
15667 }
15668 else
15669 {
15670 /* While 0 is a valid constant pool index, it is not valid
15671 to have 0 for both offsets. */
15672 str_off = 0;
15673 vec_off = 0;
15674 }
15675
15676 str_off = MAYBE_SWAP (str_off);
15677 vec_off = MAYBE_SWAP (vec_off);
15678
15679 obstack_grow (output, &str_off, sizeof (str_off));
15680 obstack_grow (output, &vec_off, sizeof (vec_off));
15681 }
15682
15683 htab_delete (str_table);
3876f04e 15684 htab_delete (symbol_hash_table);
9291a0cd
TT
15685}
15686
0a5429f6
DE
15687/* Struct to map psymtab to CU index in the index file. */
15688struct psymtab_cu_index_map
15689{
15690 struct partial_symtab *psymtab;
15691 unsigned int cu_index;
15692};
15693
15694static hashval_t
15695hash_psymtab_cu_index (const void *item)
15696{
15697 const struct psymtab_cu_index_map *map = item;
15698
15699 return htab_hash_pointer (map->psymtab);
15700}
15701
15702static int
15703eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15704{
15705 const struct psymtab_cu_index_map *lhs = item_lhs;
15706 const struct psymtab_cu_index_map *rhs = item_rhs;
15707
15708 return lhs->psymtab == rhs->psymtab;
15709}
15710
15711/* Helper struct for building the address table. */
15712struct addrmap_index_data
15713{
15714 struct objfile *objfile;
15715 struct obstack *addr_obstack;
15716 htab_t cu_index_htab;
15717
15718 /* Non-zero if the previous_* fields are valid.
15719 We can't write an entry until we see the next entry (since it is only then
15720 that we know the end of the entry). */
15721 int previous_valid;
15722 /* Index of the CU in the table of all CUs in the index file. */
15723 unsigned int previous_cu_index;
0963b4bd 15724 /* Start address of the CU. */
0a5429f6
DE
15725 CORE_ADDR previous_cu_start;
15726};
15727
15728/* Write an address entry to OBSTACK. */
b89be57b 15729
9291a0cd 15730static void
0a5429f6
DE
15731add_address_entry (struct objfile *objfile, struct obstack *obstack,
15732 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 15733{
0a5429f6 15734 offset_type cu_index_to_write;
9291a0cd
TT
15735 char addr[8];
15736 CORE_ADDR baseaddr;
15737
15738 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15739
0a5429f6
DE
15740 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15741 obstack_grow (obstack, addr, 8);
15742 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15743 obstack_grow (obstack, addr, 8);
15744 cu_index_to_write = MAYBE_SWAP (cu_index);
15745 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15746}
15747
15748/* Worker function for traversing an addrmap to build the address table. */
15749
15750static int
15751add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15752{
15753 struct addrmap_index_data *data = datap;
15754 struct partial_symtab *pst = obj;
15755 offset_type cu_index;
15756 void **slot;
15757
15758 if (data->previous_valid)
15759 add_address_entry (data->objfile, data->addr_obstack,
15760 data->previous_cu_start, start_addr,
15761 data->previous_cu_index);
15762
15763 data->previous_cu_start = start_addr;
15764 if (pst != NULL)
15765 {
15766 struct psymtab_cu_index_map find_map, *map;
15767 find_map.psymtab = pst;
15768 map = htab_find (data->cu_index_htab, &find_map);
15769 gdb_assert (map != NULL);
15770 data->previous_cu_index = map->cu_index;
15771 data->previous_valid = 1;
15772 }
15773 else
15774 data->previous_valid = 0;
15775
15776 return 0;
15777}
15778
15779/* Write OBJFILE's address map to OBSTACK.
15780 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15781 in the index file. */
15782
15783static void
15784write_address_map (struct objfile *objfile, struct obstack *obstack,
15785 htab_t cu_index_htab)
15786{
15787 struct addrmap_index_data addrmap_index_data;
15788
15789 /* When writing the address table, we have to cope with the fact that
15790 the addrmap iterator only provides the start of a region; we have to
15791 wait until the next invocation to get the start of the next region. */
15792
15793 addrmap_index_data.objfile = objfile;
15794 addrmap_index_data.addr_obstack = obstack;
15795 addrmap_index_data.cu_index_htab = cu_index_htab;
15796 addrmap_index_data.previous_valid = 0;
15797
15798 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15799 &addrmap_index_data);
15800
15801 /* It's highly unlikely the last entry (end address = 0xff...ff)
15802 is valid, but we should still handle it.
15803 The end address is recorded as the start of the next region, but that
15804 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15805 anyway. */
15806 if (addrmap_index_data.previous_valid)
15807 add_address_entry (objfile, obstack,
15808 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15809 addrmap_index_data.previous_cu_index);
9291a0cd
TT
15810}
15811
15812/* Add a list of partial symbols to SYMTAB. */
b89be57b 15813
9291a0cd
TT
15814static void
15815write_psymbols (struct mapped_symtab *symtab,
987d643c 15816 htab_t psyms_seen,
9291a0cd
TT
15817 struct partial_symbol **psymp,
15818 int count,
987d643c
TT
15819 offset_type cu_index,
15820 int is_static)
9291a0cd
TT
15821{
15822 for (; count-- > 0; ++psymp)
15823 {
987d643c
TT
15824 void **slot, *lookup;
15825
9291a0cd
TT
15826 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15827 error (_("Ada is not currently supported by the index"));
987d643c
TT
15828
15829 /* We only want to add a given psymbol once. However, we also
15830 want to account for whether it is global or static. So, we
15831 may add it twice, using slightly different values. */
15832 if (is_static)
15833 {
15834 uintptr_t val = 1 | (uintptr_t) *psymp;
15835
15836 lookup = (void *) val;
15837 }
15838 else
15839 lookup = *psymp;
15840
15841 /* Only add a given psymbol once. */
15842 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15843 if (!*slot)
15844 {
15845 *slot = lookup;
15846 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15847 }
9291a0cd
TT
15848 }
15849}
15850
15851/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15852 exception if there is an error. */
b89be57b 15853
9291a0cd
TT
15854static void
15855write_obstack (FILE *file, struct obstack *obstack)
15856{
15857 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15858 file)
15859 != obstack_object_size (obstack))
15860 error (_("couldn't data write to file"));
15861}
15862
15863/* Unlink a file if the argument is not NULL. */
b89be57b 15864
9291a0cd
TT
15865static void
15866unlink_if_set (void *p)
15867{
15868 char **filename = p;
15869 if (*filename)
15870 unlink (*filename);
15871}
15872
1fd400ff
TT
15873/* A helper struct used when iterating over debug_types. */
15874struct signatured_type_index_data
15875{
15876 struct objfile *objfile;
15877 struct mapped_symtab *symtab;
15878 struct obstack *types_list;
987d643c 15879 htab_t psyms_seen;
1fd400ff
TT
15880 int cu_index;
15881};
15882
15883/* A helper function that writes a single signatured_type to an
15884 obstack. */
b89be57b 15885
1fd400ff
TT
15886static int
15887write_one_signatured_type (void **slot, void *d)
15888{
15889 struct signatured_type_index_data *info = d;
15890 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15891 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15892 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15893 gdb_byte val[8];
15894
15895 write_psymbols (info->symtab,
987d643c 15896 info->psyms_seen,
3e43a32a
MS
15897 info->objfile->global_psymbols.list
15898 + psymtab->globals_offset,
987d643c
TT
15899 psymtab->n_global_syms, info->cu_index,
15900 0);
1fd400ff 15901 write_psymbols (info->symtab,
987d643c 15902 info->psyms_seen,
3e43a32a
MS
15903 info->objfile->static_psymbols.list
15904 + psymtab->statics_offset,
987d643c
TT
15905 psymtab->n_static_syms, info->cu_index,
15906 1);
1fd400ff
TT
15907
15908 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15909 obstack_grow (info->types_list, val, 8);
15910 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15911 obstack_grow (info->types_list, val, 8);
15912 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15913 obstack_grow (info->types_list, val, 8);
15914
15915 ++info->cu_index;
15916
15917 return 1;
15918}
15919
987d643c
TT
15920/* A cleanup function for an htab_t. */
15921
15922static void
15923cleanup_htab (void *arg)
15924{
15925 htab_delete (arg);
15926}
15927
9291a0cd 15928/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 15929
9291a0cd
TT
15930static void
15931write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15932{
15933 struct cleanup *cleanup;
15934 char *filename, *cleanup_filename;
1fd400ff
TT
15935 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15936 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15937 int i;
15938 FILE *out_file;
15939 struct mapped_symtab *symtab;
15940 offset_type val, size_of_contents, total_len;
15941 struct stat st;
15942 char buf[8];
987d643c 15943 htab_t psyms_seen;
0a5429f6
DE
15944 htab_t cu_index_htab;
15945 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 15946
b4f2f049 15947 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 15948 return;
b4f2f049 15949
9291a0cd
TT
15950 if (dwarf2_per_objfile->using_index)
15951 error (_("Cannot use an index to create the index"));
15952
15953 if (stat (objfile->name, &st) < 0)
7e17e088 15954 perror_with_name (objfile->name);
9291a0cd
TT
15955
15956 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15957 INDEX_SUFFIX, (char *) NULL);
15958 cleanup = make_cleanup (xfree, filename);
15959
15960 out_file = fopen (filename, "wb");
15961 if (!out_file)
15962 error (_("Can't open `%s' for writing"), filename);
15963
15964 cleanup_filename = filename;
15965 make_cleanup (unlink_if_set, &cleanup_filename);
15966
15967 symtab = create_mapped_symtab ();
15968 make_cleanup (cleanup_mapped_symtab, symtab);
15969
15970 obstack_init (&addr_obstack);
15971 make_cleanup_obstack_free (&addr_obstack);
15972
15973 obstack_init (&cu_list);
15974 make_cleanup_obstack_free (&cu_list);
15975
1fd400ff
TT
15976 obstack_init (&types_cu_list);
15977 make_cleanup_obstack_free (&types_cu_list);
15978
987d643c
TT
15979 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15980 NULL, xcalloc, xfree);
15981 make_cleanup (cleanup_htab, psyms_seen);
15982
0a5429f6
DE
15983 /* While we're scanning CU's create a table that maps a psymtab pointer
15984 (which is what addrmap records) to its index (which is what is recorded
15985 in the index file). This will later be needed to write the address
15986 table. */
15987 cu_index_htab = htab_create_alloc (100,
15988 hash_psymtab_cu_index,
15989 eq_psymtab_cu_index,
15990 NULL, xcalloc, xfree);
15991 make_cleanup (cleanup_htab, cu_index_htab);
15992 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15993 xmalloc (sizeof (struct psymtab_cu_index_map)
15994 * dwarf2_per_objfile->n_comp_units);
15995 make_cleanup (xfree, psymtab_cu_index_map);
15996
15997 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
15998 work here. Also, the debug_types entries do not appear in
15999 all_comp_units, but only in their own hash table. */
9291a0cd
TT
16000 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16001 {
3e43a32a
MS
16002 struct dwarf2_per_cu_data *per_cu
16003 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 16004 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 16005 gdb_byte val[8];
0a5429f6
DE
16006 struct psymtab_cu_index_map *map;
16007 void **slot;
9291a0cd
TT
16008
16009 write_psymbols (symtab,
987d643c 16010 psyms_seen,
9291a0cd 16011 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
16012 psymtab->n_global_syms, i,
16013 0);
9291a0cd 16014 write_psymbols (symtab,
987d643c 16015 psyms_seen,
9291a0cd 16016 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
16017 psymtab->n_static_syms, i,
16018 1);
9291a0cd 16019
0a5429f6
DE
16020 map = &psymtab_cu_index_map[i];
16021 map->psymtab = psymtab;
16022 map->cu_index = i;
16023 slot = htab_find_slot (cu_index_htab, map, INSERT);
16024 gdb_assert (slot != NULL);
16025 gdb_assert (*slot == NULL);
16026 *slot = map;
9291a0cd 16027
e254ef6a 16028 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 16029 obstack_grow (&cu_list, val, 8);
e254ef6a 16030 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
16031 obstack_grow (&cu_list, val, 8);
16032 }
16033
0a5429f6
DE
16034 /* Dump the address map. */
16035 write_address_map (objfile, &addr_obstack, cu_index_htab);
16036
1fd400ff
TT
16037 /* Write out the .debug_type entries, if any. */
16038 if (dwarf2_per_objfile->signatured_types)
16039 {
16040 struct signatured_type_index_data sig_data;
16041
16042 sig_data.objfile = objfile;
16043 sig_data.symtab = symtab;
16044 sig_data.types_list = &types_cu_list;
987d643c 16045 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
16046 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16047 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16048 write_one_signatured_type, &sig_data);
16049 }
16050
9291a0cd
TT
16051 obstack_init (&constant_pool);
16052 make_cleanup_obstack_free (&constant_pool);
16053 obstack_init (&symtab_obstack);
16054 make_cleanup_obstack_free (&symtab_obstack);
16055 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16056
16057 obstack_init (&contents);
16058 make_cleanup_obstack_free (&contents);
1fd400ff 16059 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
16060 total_len = size_of_contents;
16061
16062 /* The version number. */
559a7a62 16063 val = MAYBE_SWAP (5);
9291a0cd
TT
16064 obstack_grow (&contents, &val, sizeof (val));
16065
16066 /* The offset of the CU list from the start of the file. */
16067 val = MAYBE_SWAP (total_len);
16068 obstack_grow (&contents, &val, sizeof (val));
16069 total_len += obstack_object_size (&cu_list);
16070
1fd400ff
TT
16071 /* The offset of the types CU list from the start of the file. */
16072 val = MAYBE_SWAP (total_len);
16073 obstack_grow (&contents, &val, sizeof (val));
16074 total_len += obstack_object_size (&types_cu_list);
16075
9291a0cd
TT
16076 /* The offset of the address table from the start of the file. */
16077 val = MAYBE_SWAP (total_len);
16078 obstack_grow (&contents, &val, sizeof (val));
16079 total_len += obstack_object_size (&addr_obstack);
16080
16081 /* The offset of the symbol table from the start of the file. */
16082 val = MAYBE_SWAP (total_len);
16083 obstack_grow (&contents, &val, sizeof (val));
16084 total_len += obstack_object_size (&symtab_obstack);
16085
16086 /* The offset of the constant pool from the start of the file. */
16087 val = MAYBE_SWAP (total_len);
16088 obstack_grow (&contents, &val, sizeof (val));
16089 total_len += obstack_object_size (&constant_pool);
16090
16091 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16092
16093 write_obstack (out_file, &contents);
16094 write_obstack (out_file, &cu_list);
1fd400ff 16095 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
16096 write_obstack (out_file, &addr_obstack);
16097 write_obstack (out_file, &symtab_obstack);
16098 write_obstack (out_file, &constant_pool);
16099
16100 fclose (out_file);
16101
16102 /* We want to keep the file, so we set cleanup_filename to NULL
16103 here. See unlink_if_set. */
16104 cleanup_filename = NULL;
16105
16106 do_cleanups (cleanup);
16107}
16108
90476074
TT
16109/* Implementation of the `save gdb-index' command.
16110
16111 Note that the file format used by this command is documented in the
16112 GDB manual. Any changes here must be documented there. */
11570e71 16113
9291a0cd
TT
16114static void
16115save_gdb_index_command (char *arg, int from_tty)
16116{
16117 struct objfile *objfile;
16118
16119 if (!arg || !*arg)
96d19272 16120 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
16121
16122 ALL_OBJFILES (objfile)
16123 {
16124 struct stat st;
16125
16126 /* If the objfile does not correspond to an actual file, skip it. */
16127 if (stat (objfile->name, &st) < 0)
16128 continue;
16129
16130 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16131 if (dwarf2_per_objfile)
16132 {
16133 volatile struct gdb_exception except;
16134
16135 TRY_CATCH (except, RETURN_MASK_ERROR)
16136 {
16137 write_psymtabs_to_index (objfile, arg);
16138 }
16139 if (except.reason < 0)
16140 exception_fprintf (gdb_stderr, except,
16141 _("Error while writing index for `%s': "),
16142 objfile->name);
16143 }
16144 }
dce234bc
PP
16145}
16146
9291a0cd
TT
16147\f
16148
9eae7c52
TT
16149int dwarf2_always_disassemble;
16150
16151static void
16152show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16153 struct cmd_list_element *c, const char *value)
16154{
3e43a32a
MS
16155 fprintf_filtered (file,
16156 _("Whether to always disassemble "
16157 "DWARF expressions is %s.\n"),
9eae7c52
TT
16158 value);
16159}
16160
6502dd73
DJ
16161void _initialize_dwarf2_read (void);
16162
16163void
16164_initialize_dwarf2_read (void)
16165{
96d19272
JK
16166 struct cmd_list_element *c;
16167
dce234bc 16168 dwarf2_objfile_data_key
c1bd65d0 16169 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 16170
1bedd215
AC
16171 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16172Set DWARF 2 specific variables.\n\
16173Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16174 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16175 0/*allow-unknown*/, &maintenance_set_cmdlist);
16176
1bedd215
AC
16177 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16178Show DWARF 2 specific variables\n\
16179Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16180 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16181 0/*allow-unknown*/, &maintenance_show_cmdlist);
16182
16183 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
16184 &dwarf2_max_cache_age, _("\
16185Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16186Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16187A higher limit means that cached compilation units will be stored\n\
16188in memory longer, and more total memory will be used. Zero disables\n\
16189caching, which can slow down startup."),
2c5b56ce 16190 NULL,
920d2a44 16191 show_dwarf2_max_cache_age,
2c5b56ce 16192 &set_dwarf2_cmdlist,
ae038cb0 16193 &show_dwarf2_cmdlist);
d97bc12b 16194
9eae7c52
TT
16195 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16196 &dwarf2_always_disassemble, _("\
16197Set whether `info address' always disassembles DWARF expressions."), _("\
16198Show whether `info address' always disassembles DWARF expressions."), _("\
16199When enabled, DWARF expressions are always printed in an assembly-like\n\
16200syntax. When disabled, expressions will be printed in a more\n\
16201conversational style, when possible."),
16202 NULL,
16203 show_dwarf2_always_disassemble,
16204 &set_dwarf2_cmdlist,
16205 &show_dwarf2_cmdlist);
16206
d97bc12b
DE
16207 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16208Set debugging of the dwarf2 DIE reader."), _("\
16209Show debugging of the dwarf2 DIE reader."), _("\
16210When enabled (non-zero), DIEs are dumped after they are read in.\n\
16211The value is the maximum depth to print."),
16212 NULL,
16213 NULL,
16214 &setdebuglist, &showdebuglist);
9291a0cd 16215
96d19272 16216 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 16217 _("\
fc1a9d6e 16218Save a gdb-index file.\n\
11570e71 16219Usage: save gdb-index DIRECTORY"),
96d19272
JK
16220 &save_cmdlist);
16221 set_cmd_completer (c, filename_completer);
6502dd73 16222}
This page took 2.181407 seconds and 4 git commands to generate.