gdb/
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
6aba47ca 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
7b6bb8da 4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
0fb0cc75 5 Free Software Foundation, Inc.
c906108c
SS
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 12 support.
c906108c 13
c5aa993b 14 This file is part of GDB.
c906108c 15
c5aa993b
JM
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
a9762ec7
JB
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
c906108c 20
a9762ec7
JB
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
c906108c 25
c5aa993b 26 You should have received a copy of the GNU General Public License
a9762ec7 27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
28
29#include "defs.h"
30#include "bfd.h"
c906108c
SS
31#include "symtab.h"
32#include "gdbtypes.h"
c906108c 33#include "objfiles.h"
fa8f86ff 34#include "dwarf2.h"
c906108c
SS
35#include "buildsym.h"
36#include "demangle.h"
37#include "expression.h"
d5166ae1 38#include "filenames.h" /* for DOSish file names */
2e276125 39#include "macrotab.h"
c906108c
SS
40#include "language.h"
41#include "complaints.h"
357e46e7 42#include "bcache.h"
4c2df51b
DJ
43#include "dwarf2expr.h"
44#include "dwarf2loc.h"
9219021c 45#include "cp-support.h"
72bf9492 46#include "hashtab.h"
ae038cb0
DJ
47#include "command.h"
48#include "gdbcmd.h"
edb3359d 49#include "block.h"
ff013f42 50#include "addrmap.h"
94af9270
KS
51#include "typeprint.h"
52#include "jv-lang.h"
ccefe4c4 53#include "psympriv.h"
9291a0cd
TT
54#include "exceptions.h"
55#include "gdb_stat.h"
96d19272 56#include "completer.h"
34eaf542 57#include "vec.h"
98bfdba5
PA
58#include "c-lang.h"
59#include "valprint.h"
60d5a603 60#include <ctype.h>
4c2df51b 61
c906108c
SS
62#include <fcntl.h>
63#include "gdb_string.h"
4bdf3d34 64#include "gdb_assert.h"
c906108c 65#include <sys/types.h>
233a11ab
CS
66#ifdef HAVE_ZLIB_H
67#include <zlib.h>
68#endif
dce234bc
PP
69#ifdef HAVE_MMAP
70#include <sys/mman.h>
85d9bd0e
TT
71#ifndef MAP_FAILED
72#define MAP_FAILED ((void *) -1)
73#endif
dce234bc 74#endif
d8151005 75
34eaf542
TT
76typedef struct symbol *symbolp;
77DEF_VEC_P (symbolp);
78
107d2387 79#if 0
357e46e7 80/* .debug_info header for a compilation unit
c906108c
SS
81 Because of alignment constraints, this structure has padding and cannot
82 be mapped directly onto the beginning of the .debug_info section. */
83typedef struct comp_unit_header
84 {
85 unsigned int length; /* length of the .debug_info
86 contribution */
87 unsigned short version; /* version number -- 2 for DWARF
88 version 2 */
89 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
90 unsigned char addr_size; /* byte size of an address -- 4 */
91 }
92_COMP_UNIT_HEADER;
93#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 94#endif
c906108c 95
c906108c
SS
96/* .debug_line statement program prologue
97 Because of alignment constraints, this structure has padding and cannot
98 be mapped directly onto the beginning of the .debug_info section. */
99typedef struct statement_prologue
100 {
101 unsigned int total_length; /* byte length of the statement
102 information */
103 unsigned short version; /* version number -- 2 for DWARF
104 version 2 */
105 unsigned int prologue_length; /* # bytes between prologue &
106 stmt program */
107 unsigned char minimum_instruction_length; /* byte size of
108 smallest instr */
109 unsigned char default_is_stmt; /* initial value of is_stmt
110 register */
111 char line_base;
112 unsigned char line_range;
113 unsigned char opcode_base; /* number assigned to first special
114 opcode */
115 unsigned char *standard_opcode_lengths;
116 }
117_STATEMENT_PROLOGUE;
118
d97bc12b
DE
119/* When non-zero, dump DIEs after they are read in. */
120static int dwarf2_die_debug = 0;
121
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
c906108c
SS
6368/* Add an aggregate field to the field list. */
6369
6370static void
107d2387 6371dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6372 struct dwarf2_cu *cu)
6e70227d 6373{
e7c27a73 6374 struct objfile *objfile = cu->objfile;
5e2b427d 6375 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6376 struct nextfield *new_field;
6377 struct attribute *attr;
6378 struct field *fp;
6379 char *fieldname = "";
6380
6381 /* Allocate a new field list entry and link it in. */
6382 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6383 make_cleanup (xfree, new_field);
c906108c 6384 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6385
6386 if (die->tag == DW_TAG_inheritance)
6387 {
6388 new_field->next = fip->baseclasses;
6389 fip->baseclasses = new_field;
6390 }
6391 else
6392 {
6393 new_field->next = fip->fields;
6394 fip->fields = new_field;
6395 }
c906108c
SS
6396 fip->nfields++;
6397
e142c38c 6398 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6399 if (attr)
6400 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
6401 else
6402 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
6403 if (new_field->accessibility != DW_ACCESS_public)
6404 fip->non_public_fields = 1;
60d5a603 6405
e142c38c 6406 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6407 if (attr)
6408 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
6409 else
6410 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
6411
6412 fp = &new_field->field;
a9a9bd0f 6413
e142c38c 6414 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6415 {
a9a9bd0f 6416 /* Data member other than a C++ static data member. */
6e70227d 6417
c906108c 6418 /* Get type of field. */
e7c27a73 6419 fp->type = die_type (die, cu);
c906108c 6420
d6a843b5 6421 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6422
c906108c 6423 /* Get bit size of field (zero if none). */
e142c38c 6424 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6425 if (attr)
6426 {
6427 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6428 }
6429 else
6430 {
6431 FIELD_BITSIZE (*fp) = 0;
6432 }
6433
6434 /* Get bit offset of field. */
e142c38c 6435 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6436 if (attr)
6437 {
d4b96c9a 6438 int byte_offset = 0;
c6a0999f 6439
3690dd37 6440 if (attr_form_is_section_offset (attr))
d4b96c9a 6441 dwarf2_complex_location_expr_complaint ();
3690dd37 6442 else if (attr_form_is_constant (attr))
c6a0999f 6443 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6444 else if (attr_form_is_block (attr))
c6a0999f 6445 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6446 else
6447 dwarf2_complex_location_expr_complaint ();
c6a0999f 6448
d6a843b5 6449 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6450 }
e142c38c 6451 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6452 if (attr)
6453 {
5e2b427d 6454 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6455 {
6456 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6457 additional bit offset from the MSB of the containing
6458 anonymous object to the MSB of the field. We don't
6459 have to do anything special since we don't need to
6460 know the size of the anonymous object. */
c906108c
SS
6461 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6462 }
6463 else
6464 {
6465 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6466 MSB of the anonymous object, subtract off the number of
6467 bits from the MSB of the field to the MSB of the
6468 object, and then subtract off the number of bits of
6469 the field itself. The result is the bit offset of
6470 the LSB of the field. */
c906108c
SS
6471 int anonymous_size;
6472 int bit_offset = DW_UNSND (attr);
6473
e142c38c 6474 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6475 if (attr)
6476 {
6477 /* The size of the anonymous object containing
6478 the bit field is explicit, so use the
6479 indicated size (in bytes). */
6480 anonymous_size = DW_UNSND (attr);
6481 }
6482 else
6483 {
6484 /* The size of the anonymous object containing
6485 the bit field must be inferred from the type
6486 attribute of the data member containing the
6487 bit field. */
6488 anonymous_size = TYPE_LENGTH (fp->type);
6489 }
6490 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6491 - bit_offset - FIELD_BITSIZE (*fp);
6492 }
6493 }
6494
6495 /* Get name of field. */
39cbfefa
DJ
6496 fieldname = dwarf2_name (die, cu);
6497 if (fieldname == NULL)
6498 fieldname = "";
d8151005
DJ
6499
6500 /* The name is already allocated along with this objfile, so we don't
6501 need to duplicate it for the type. */
6502 fp->name = fieldname;
c906108c
SS
6503
6504 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6505 pointer or virtual base class pointer) to private. */
e142c38c 6506 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6507 {
d48cc9dd 6508 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6509 new_field->accessibility = DW_ACCESS_private;
6510 fip->non_public_fields = 1;
6511 }
6512 }
a9a9bd0f 6513 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6514 {
a9a9bd0f
DC
6515 /* C++ static member. */
6516
6517 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6518 is a declaration, but all versions of G++ as of this writing
6519 (so through at least 3.2.1) incorrectly generate
6520 DW_TAG_variable tags. */
6e70227d 6521
c906108c 6522 char *physname;
c906108c 6523
a9a9bd0f 6524 /* Get name of field. */
39cbfefa
DJ
6525 fieldname = dwarf2_name (die, cu);
6526 if (fieldname == NULL)
c906108c
SS
6527 return;
6528
254e6b9e 6529 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6530 if (attr
6531 /* Only create a symbol if this is an external value.
6532 new_symbol checks this and puts the value in the global symbol
6533 table, which we want. If it is not external, new_symbol
6534 will try to put the value in cu->list_in_scope which is wrong. */
6535 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6536 {
6537 /* A static const member, not much different than an enum as far as
6538 we're concerned, except that we can support more types. */
6539 new_symbol (die, NULL, cu);
6540 }
6541
2df3850c 6542 /* Get physical name. */
94af9270 6543 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6544
d8151005
DJ
6545 /* The name is already allocated along with this objfile, so we don't
6546 need to duplicate it for the type. */
6547 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6548 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6549 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6550 }
6551 else if (die->tag == DW_TAG_inheritance)
6552 {
6553 /* C++ base class field. */
e142c38c 6554 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6555 if (attr)
d4b96c9a
JK
6556 {
6557 int byte_offset = 0;
6558
6559 if (attr_form_is_section_offset (attr))
6560 dwarf2_complex_location_expr_complaint ();
6561 else if (attr_form_is_constant (attr))
6562 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6563 else if (attr_form_is_block (attr))
6564 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6565 else
6566 dwarf2_complex_location_expr_complaint ();
6567
6568 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6569 }
c906108c 6570 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6571 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6572 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6573 fip->nbaseclasses++;
6574 }
6575}
6576
98751a41
JK
6577/* Add a typedef defined in the scope of the FIP's class. */
6578
6579static void
6580dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6581 struct dwarf2_cu *cu)
6e70227d 6582{
98751a41 6583 struct objfile *objfile = cu->objfile;
98751a41
JK
6584 struct typedef_field_list *new_field;
6585 struct attribute *attr;
6586 struct typedef_field *fp;
6587 char *fieldname = "";
6588
6589 /* Allocate a new field list entry and link it in. */
6590 new_field = xzalloc (sizeof (*new_field));
6591 make_cleanup (xfree, new_field);
6592
6593 gdb_assert (die->tag == DW_TAG_typedef);
6594
6595 fp = &new_field->field;
6596
6597 /* Get name of field. */
6598 fp->name = dwarf2_name (die, cu);
6599 if (fp->name == NULL)
6600 return;
6601
6602 fp->type = read_type_die (die, cu);
6603
6604 new_field->next = fip->typedef_field_list;
6605 fip->typedef_field_list = new_field;
6606 fip->typedef_field_list_count++;
6607}
6608
c906108c
SS
6609/* Create the vector of fields, and attach it to the type. */
6610
6611static void
fba45db2 6612dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6613 struct dwarf2_cu *cu)
c906108c
SS
6614{
6615 int nfields = fip->nfields;
6616
6617 /* Record the field count, allocate space for the array of fields,
6618 and create blank accessibility bitfields if necessary. */
6619 TYPE_NFIELDS (type) = nfields;
6620 TYPE_FIELDS (type) = (struct field *)
6621 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6622 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6623
b4ba55a1 6624 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6625 {
6626 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6627
6628 TYPE_FIELD_PRIVATE_BITS (type) =
6629 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6630 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6631
6632 TYPE_FIELD_PROTECTED_BITS (type) =
6633 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6634 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6635
774b6a14
TT
6636 TYPE_FIELD_IGNORE_BITS (type) =
6637 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6638 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6639 }
6640
6641 /* If the type has baseclasses, allocate and clear a bit vector for
6642 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6643 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6644 {
6645 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6646 unsigned char *pointer;
c906108c
SS
6647
6648 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6649 pointer = TYPE_ALLOC (type, num_bytes);
6650 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6651 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6652 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6653 }
6654
3e43a32a
MS
6655 /* Copy the saved-up fields into the field vector. Start from the head of
6656 the list, adding to the tail of the field array, so that they end up in
6657 the same order in the array in which they were added to the list. */
c906108c
SS
6658 while (nfields-- > 0)
6659 {
7d0ccb61
DJ
6660 struct nextfield *fieldp;
6661
6662 if (fip->fields)
6663 {
6664 fieldp = fip->fields;
6665 fip->fields = fieldp->next;
6666 }
6667 else
6668 {
6669 fieldp = fip->baseclasses;
6670 fip->baseclasses = fieldp->next;
6671 }
6672
6673 TYPE_FIELD (type, nfields) = fieldp->field;
6674 switch (fieldp->accessibility)
c906108c 6675 {
c5aa993b 6676 case DW_ACCESS_private:
b4ba55a1
JB
6677 if (cu->language != language_ada)
6678 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6679 break;
c906108c 6680
c5aa993b 6681 case DW_ACCESS_protected:
b4ba55a1
JB
6682 if (cu->language != language_ada)
6683 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6684 break;
c906108c 6685
c5aa993b
JM
6686 case DW_ACCESS_public:
6687 break;
c906108c 6688
c5aa993b
JM
6689 default:
6690 /* Unknown accessibility. Complain and treat it as public. */
6691 {
e2e0b3e5 6692 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6693 fieldp->accessibility);
c5aa993b
JM
6694 }
6695 break;
c906108c
SS
6696 }
6697 if (nfields < fip->nbaseclasses)
6698 {
7d0ccb61 6699 switch (fieldp->virtuality)
c906108c 6700 {
c5aa993b
JM
6701 case DW_VIRTUALITY_virtual:
6702 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 6703 if (cu->language == language_ada)
a73c6dcd 6704 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
6705 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6706 break;
c906108c
SS
6707 }
6708 }
c906108c
SS
6709 }
6710}
6711
c906108c
SS
6712/* Add a member function to the proper fieldlist. */
6713
6714static void
107d2387 6715dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6716 struct type *type, struct dwarf2_cu *cu)
c906108c 6717{
e7c27a73 6718 struct objfile *objfile = cu->objfile;
c906108c
SS
6719 struct attribute *attr;
6720 struct fnfieldlist *flp;
6721 int i;
6722 struct fn_field *fnp;
6723 char *fieldname;
c906108c 6724 struct nextfnfield *new_fnfield;
f792889a 6725 struct type *this_type;
60d5a603 6726 enum dwarf_access_attribute accessibility;
c906108c 6727
b4ba55a1 6728 if (cu->language == language_ada)
a73c6dcd 6729 error (_("unexpected member function in Ada type"));
b4ba55a1 6730
2df3850c 6731 /* Get name of member function. */
39cbfefa
DJ
6732 fieldname = dwarf2_name (die, cu);
6733 if (fieldname == NULL)
2df3850c 6734 return;
c906108c 6735
c906108c
SS
6736 /* Look up member function name in fieldlist. */
6737 for (i = 0; i < fip->nfnfields; i++)
6738 {
27bfe10e 6739 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6740 break;
6741 }
6742
6743 /* Create new list element if necessary. */
6744 if (i < fip->nfnfields)
6745 flp = &fip->fnfieldlists[i];
6746 else
6747 {
6748 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6749 {
6750 fip->fnfieldlists = (struct fnfieldlist *)
6751 xrealloc (fip->fnfieldlists,
6752 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6753 * sizeof (struct fnfieldlist));
c906108c 6754 if (fip->nfnfields == 0)
c13c43fd 6755 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6756 }
6757 flp = &fip->fnfieldlists[fip->nfnfields];
6758 flp->name = fieldname;
6759 flp->length = 0;
6760 flp->head = NULL;
3da10d80 6761 i = fip->nfnfields++;
c906108c
SS
6762 }
6763
6764 /* Create a new member function field and chain it to the field list
0963b4bd 6765 entry. */
c906108c 6766 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6767 make_cleanup (xfree, new_fnfield);
c906108c
SS
6768 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6769 new_fnfield->next = flp->head;
6770 flp->head = new_fnfield;
6771 flp->length++;
6772
6773 /* Fill in the member function field info. */
6774 fnp = &new_fnfield->fnfield;
3da10d80
KS
6775
6776 /* Delay processing of the physname until later. */
6777 if (cu->language == language_cplus || cu->language == language_java)
6778 {
6779 add_to_method_list (type, i, flp->length - 1, fieldname,
6780 die, cu);
6781 }
6782 else
6783 {
6784 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6785 fnp->physname = physname ? physname : "";
6786 }
6787
c906108c 6788 fnp->type = alloc_type (objfile);
f792889a
DJ
6789 this_type = read_type_die (die, cu);
6790 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6791 {
f792889a 6792 int nparams = TYPE_NFIELDS (this_type);
c906108c 6793
f792889a 6794 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6795 of the method itself (TYPE_CODE_METHOD). */
6796 smash_to_method_type (fnp->type, type,
f792889a
DJ
6797 TYPE_TARGET_TYPE (this_type),
6798 TYPE_FIELDS (this_type),
6799 TYPE_NFIELDS (this_type),
6800 TYPE_VARARGS (this_type));
c906108c
SS
6801
6802 /* Handle static member functions.
c5aa993b 6803 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
6804 member functions. G++ helps GDB by marking the first
6805 parameter for non-static member functions (which is the this
6806 pointer) as artificial. We obtain this information from
6807 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6808 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6809 fnp->voffset = VOFFSET_STATIC;
6810 }
6811 else
e2e0b3e5 6812 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6813 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6814
6815 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6816 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6817 fnp->fcontext = die_containing_type (die, cu);
c906108c 6818
3e43a32a
MS
6819 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6820 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
6821
6822 /* Get accessibility. */
e142c38c 6823 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 6824 if (attr)
60d5a603
JK
6825 accessibility = DW_UNSND (attr);
6826 else
6827 accessibility = dwarf2_default_access_attribute (die, cu);
6828 switch (accessibility)
c906108c 6829 {
60d5a603
JK
6830 case DW_ACCESS_private:
6831 fnp->is_private = 1;
6832 break;
6833 case DW_ACCESS_protected:
6834 fnp->is_protected = 1;
6835 break;
c906108c
SS
6836 }
6837
b02dede2 6838 /* Check for artificial methods. */
e142c38c 6839 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6840 if (attr && DW_UNSND (attr) != 0)
6841 fnp->is_artificial = 1;
6842
0d564a31 6843 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6844 function. For older versions of GCC, this is an offset in the
6845 appropriate virtual table, as specified by DW_AT_containing_type.
6846 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6847 to the object address. */
6848
e142c38c 6849 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6850 if (attr)
8e19ed76 6851 {
aec5aa8b 6852 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6853 {
aec5aa8b
TT
6854 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6855 {
6856 /* Old-style GCC. */
6857 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6858 }
6859 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6860 || (DW_BLOCK (attr)->size > 1
6861 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6862 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6863 {
6864 struct dwarf_block blk;
6865 int offset;
6866
6867 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6868 ? 1 : 2);
6869 blk.size = DW_BLOCK (attr)->size - offset;
6870 blk.data = DW_BLOCK (attr)->data + offset;
6871 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6872 if ((fnp->voffset % cu->header.addr_size) != 0)
6873 dwarf2_complex_location_expr_complaint ();
6874 else
6875 fnp->voffset /= cu->header.addr_size;
6876 fnp->voffset += 2;
6877 }
6878 else
6879 dwarf2_complex_location_expr_complaint ();
6880
6881 if (!fnp->fcontext)
6882 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6883 }
3690dd37 6884 else if (attr_form_is_section_offset (attr))
8e19ed76 6885 {
4d3c2250 6886 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6887 }
6888 else
6889 {
4d3c2250
KB
6890 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6891 fieldname);
8e19ed76 6892 }
0d564a31 6893 }
d48cc9dd
DJ
6894 else
6895 {
6896 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6897 if (attr && DW_UNSND (attr))
6898 {
6899 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6900 complaint (&symfile_complaints,
3e43a32a
MS
6901 _("Member function \"%s\" (offset %d) is virtual "
6902 "but the vtable offset is not specified"),
d48cc9dd 6903 fieldname, die->offset);
9655fd1a 6904 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6905 TYPE_CPLUS_DYNAMIC (type) = 1;
6906 }
6907 }
c906108c
SS
6908}
6909
6910/* Create the vector of member function fields, and attach it to the type. */
6911
6912static void
fba45db2 6913dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6914 struct dwarf2_cu *cu)
c906108c
SS
6915{
6916 struct fnfieldlist *flp;
6917 int total_length = 0;
6918 int i;
6919
b4ba55a1 6920 if (cu->language == language_ada)
a73c6dcd 6921 error (_("unexpected member functions in Ada type"));
b4ba55a1 6922
c906108c
SS
6923 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6924 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6925 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6926
6927 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6928 {
6929 struct nextfnfield *nfp = flp->head;
6930 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6931 int k;
6932
6933 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6934 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6935 fn_flp->fn_fields = (struct fn_field *)
6936 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6937 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6938 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6939
6940 total_length += flp->length;
6941 }
6942
6943 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6944 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6945}
6946
1168df01
JB
6947/* Returns non-zero if NAME is the name of a vtable member in CU's
6948 language, zero otherwise. */
6949static int
6950is_vtable_name (const char *name, struct dwarf2_cu *cu)
6951{
6952 static const char vptr[] = "_vptr";
987504bb 6953 static const char vtable[] = "vtable";
1168df01 6954
987504bb
JJ
6955 /* Look for the C++ and Java forms of the vtable. */
6956 if ((cu->language == language_java
6957 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6958 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6959 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6960 return 1;
6961
6962 return 0;
6963}
6964
c0dd20ea 6965/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6966 functions, with the ABI-specified layout. If TYPE describes
6967 such a structure, smash it into a member function type.
61049d3b
DJ
6968
6969 GCC shouldn't do this; it should just output pointer to member DIEs.
6970 This is GCC PR debug/28767. */
c0dd20ea 6971
0b92b5bb
TT
6972static void
6973quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6974{
0b92b5bb 6975 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6976
6977 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6978 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6979 return;
c0dd20ea
DJ
6980
6981 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6982 if (TYPE_FIELD_NAME (type, 0) == NULL
6983 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6984 || TYPE_FIELD_NAME (type, 1) == NULL
6985 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6986 return;
c0dd20ea
DJ
6987
6988 /* Find the type of the method. */
0b92b5bb 6989 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6990 if (pfn_type == NULL
6991 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6992 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6993 return;
c0dd20ea
DJ
6994
6995 /* Look for the "this" argument. */
6996 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6997 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6998 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6999 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7000 return;
c0dd20ea
DJ
7001
7002 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7003 new_type = alloc_type (objfile);
7004 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7005 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7006 TYPE_VARARGS (pfn_type));
0b92b5bb 7007 smash_to_methodptr_type (type, new_type);
c0dd20ea 7008}
1168df01 7009
c906108c 7010/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7011 (definition) to create a type for the structure or union. Fill in
7012 the type's name and general properties; the members will not be
7013 processed until process_structure_type.
c906108c 7014
c767944b
DJ
7015 NOTE: we need to call these functions regardless of whether or not the
7016 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7017 structure or union. This gets the type entered into our set of
7018 user defined types.
7019
7020 However, if the structure is incomplete (an opaque struct/union)
7021 then suppress creating a symbol table entry for it since gdb only
7022 wants to find the one with the complete definition. Note that if
7023 it is complete, we just call new_symbol, which does it's own
7024 checking about whether the struct/union is anonymous or not (and
7025 suppresses creating a symbol table entry itself). */
7026
f792889a 7027static struct type *
134d01f1 7028read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7029{
e7c27a73 7030 struct objfile *objfile = cu->objfile;
c906108c
SS
7031 struct type *type;
7032 struct attribute *attr;
39cbfefa 7033 char *name;
c906108c 7034
348e048f
DE
7035 /* If the definition of this type lives in .debug_types, read that type.
7036 Don't follow DW_AT_specification though, that will take us back up
7037 the chain and we want to go down. */
7038 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7039 if (attr)
7040 {
7041 struct dwarf2_cu *type_cu = cu;
7042 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7043
348e048f
DE
7044 /* We could just recurse on read_structure_type, but we need to call
7045 get_die_type to ensure only one type for this DIE is created.
7046 This is important, for example, because for c++ classes we need
7047 TYPE_NAME set which is only done by new_symbol. Blech. */
7048 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7049
7050 /* TYPE_CU may not be the same as CU.
7051 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7052 return set_die_type (die, type, cu);
7053 }
7054
c0dd20ea 7055 type = alloc_type (objfile);
c906108c 7056 INIT_CPLUS_SPECIFIC (type);
93311388 7057
39cbfefa
DJ
7058 name = dwarf2_name (die, cu);
7059 if (name != NULL)
c906108c 7060 {
987504bb
JJ
7061 if (cu->language == language_cplus
7062 || cu->language == language_java)
63d06c5c 7063 {
3da10d80
KS
7064 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7065
7066 /* dwarf2_full_name might have already finished building the DIE's
7067 type. If so, there is no need to continue. */
7068 if (get_die_type (die, cu) != NULL)
7069 return get_die_type (die, cu);
7070
7071 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7072 if (die->tag == DW_TAG_structure_type
7073 || die->tag == DW_TAG_class_type)
7074 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7075 }
7076 else
7077 {
d8151005
DJ
7078 /* The name is already allocated along with this objfile, so
7079 we don't need to duplicate it for the type. */
94af9270
KS
7080 TYPE_TAG_NAME (type) = (char *) name;
7081 if (die->tag == DW_TAG_class_type)
7082 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7083 }
c906108c
SS
7084 }
7085
7086 if (die->tag == DW_TAG_structure_type)
7087 {
7088 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7089 }
7090 else if (die->tag == DW_TAG_union_type)
7091 {
7092 TYPE_CODE (type) = TYPE_CODE_UNION;
7093 }
7094 else
7095 {
c906108c
SS
7096 TYPE_CODE (type) = TYPE_CODE_CLASS;
7097 }
7098
0cc2414c
TT
7099 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7100 TYPE_DECLARED_CLASS (type) = 1;
7101
e142c38c 7102 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7103 if (attr)
7104 {
7105 TYPE_LENGTH (type) = DW_UNSND (attr);
7106 }
7107 else
7108 {
7109 TYPE_LENGTH (type) = 0;
7110 }
7111
876cecd0 7112 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7113 if (die_is_declaration (die, cu))
876cecd0 7114 TYPE_STUB (type) = 1;
a6c727b2
DJ
7115 else if (attr == NULL && die->child == NULL
7116 && producer_is_realview (cu->producer))
7117 /* RealView does not output the required DW_AT_declaration
7118 on incomplete types. */
7119 TYPE_STUB (type) = 1;
dc718098 7120
c906108c
SS
7121 /* We need to add the type field to the die immediately so we don't
7122 infinitely recurse when dealing with pointers to the structure
0963b4bd 7123 type within the structure itself. */
1c379e20 7124 set_die_type (die, type, cu);
c906108c 7125
7e314c57
JK
7126 /* set_die_type should be already done. */
7127 set_descriptive_type (type, die, cu);
7128
c767944b
DJ
7129 return type;
7130}
7131
7132/* Finish creating a structure or union type, including filling in
7133 its members and creating a symbol for it. */
7134
7135static void
7136process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7137{
7138 struct objfile *objfile = cu->objfile;
7139 struct die_info *child_die = die->child;
7140 struct type *type;
7141
7142 type = get_die_type (die, cu);
7143 if (type == NULL)
7144 type = read_structure_type (die, cu);
7145
e142c38c 7146 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7147 {
7148 struct field_info fi;
7149 struct die_info *child_die;
34eaf542 7150 VEC (symbolp) *template_args = NULL;
c767944b 7151 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7152
7153 memset (&fi, 0, sizeof (struct field_info));
7154
639d11d3 7155 child_die = die->child;
c906108c
SS
7156
7157 while (child_die && child_die->tag)
7158 {
a9a9bd0f
DC
7159 if (child_die->tag == DW_TAG_member
7160 || child_die->tag == DW_TAG_variable)
c906108c 7161 {
a9a9bd0f
DC
7162 /* NOTE: carlton/2002-11-05: A C++ static data member
7163 should be a DW_TAG_member that is a declaration, but
7164 all versions of G++ as of this writing (so through at
7165 least 3.2.1) incorrectly generate DW_TAG_variable
7166 tags for them instead. */
e7c27a73 7167 dwarf2_add_field (&fi, child_die, cu);
c906108c 7168 }
8713b1b1 7169 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7170 {
0963b4bd 7171 /* C++ member function. */
e7c27a73 7172 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7173 }
7174 else if (child_die->tag == DW_TAG_inheritance)
7175 {
7176 /* C++ base class field. */
e7c27a73 7177 dwarf2_add_field (&fi, child_die, cu);
c906108c 7178 }
98751a41
JK
7179 else if (child_die->tag == DW_TAG_typedef)
7180 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7181 else if (child_die->tag == DW_TAG_template_type_param
7182 || child_die->tag == DW_TAG_template_value_param)
7183 {
7184 struct symbol *arg = new_symbol (child_die, NULL, cu);
7185
f1078f66
DJ
7186 if (arg != NULL)
7187 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7188 }
7189
c906108c
SS
7190 child_die = sibling_die (child_die);
7191 }
7192
34eaf542
TT
7193 /* Attach template arguments to type. */
7194 if (! VEC_empty (symbolp, template_args))
7195 {
7196 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7197 TYPE_N_TEMPLATE_ARGUMENTS (type)
7198 = VEC_length (symbolp, template_args);
7199 TYPE_TEMPLATE_ARGUMENTS (type)
7200 = obstack_alloc (&objfile->objfile_obstack,
7201 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7202 * sizeof (struct symbol *)));
7203 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7204 VEC_address (symbolp, template_args),
7205 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7206 * sizeof (struct symbol *)));
7207 VEC_free (symbolp, template_args);
7208 }
7209
c906108c
SS
7210 /* Attach fields and member functions to the type. */
7211 if (fi.nfields)
e7c27a73 7212 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7213 if (fi.nfnfields)
7214 {
e7c27a73 7215 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7216
c5aa993b 7217 /* Get the type which refers to the base class (possibly this
c906108c 7218 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7219 class from the DW_AT_containing_type attribute. This use of
7220 DW_AT_containing_type is a GNU extension. */
c906108c 7221
e142c38c 7222 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7223 {
e7c27a73 7224 struct type *t = die_containing_type (die, cu);
c906108c
SS
7225
7226 TYPE_VPTR_BASETYPE (type) = t;
7227 if (type == t)
7228 {
c906108c
SS
7229 int i;
7230
7231 /* Our own class provides vtbl ptr. */
7232 for (i = TYPE_NFIELDS (t) - 1;
7233 i >= TYPE_N_BASECLASSES (t);
7234 --i)
7235 {
7236 char *fieldname = TYPE_FIELD_NAME (t, i);
7237
1168df01 7238 if (is_vtable_name (fieldname, cu))
c906108c
SS
7239 {
7240 TYPE_VPTR_FIELDNO (type) = i;
7241 break;
7242 }
7243 }
7244
7245 /* Complain if virtual function table field not found. */
7246 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7247 complaint (&symfile_complaints,
3e43a32a
MS
7248 _("virtual function table pointer "
7249 "not found when defining class '%s'"),
4d3c2250
KB
7250 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7251 "");
c906108c
SS
7252 }
7253 else
7254 {
7255 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7256 }
7257 }
f6235d4c
EZ
7258 else if (cu->producer
7259 && strncmp (cu->producer,
7260 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7261 {
7262 /* The IBM XLC compiler does not provide direct indication
7263 of the containing type, but the vtable pointer is
7264 always named __vfp. */
7265
7266 int i;
7267
7268 for (i = TYPE_NFIELDS (type) - 1;
7269 i >= TYPE_N_BASECLASSES (type);
7270 --i)
7271 {
7272 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7273 {
7274 TYPE_VPTR_FIELDNO (type) = i;
7275 TYPE_VPTR_BASETYPE (type) = type;
7276 break;
7277 }
7278 }
7279 }
c906108c 7280 }
98751a41
JK
7281
7282 /* Copy fi.typedef_field_list linked list elements content into the
7283 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7284 if (fi.typedef_field_list)
7285 {
7286 int i = fi.typedef_field_list_count;
7287
a0d7a4ff 7288 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7289 TYPE_TYPEDEF_FIELD_ARRAY (type)
7290 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7291 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7292
7293 /* Reverse the list order to keep the debug info elements order. */
7294 while (--i >= 0)
7295 {
7296 struct typedef_field *dest, *src;
6e70227d 7297
98751a41
JK
7298 dest = &TYPE_TYPEDEF_FIELD (type, i);
7299 src = &fi.typedef_field_list->field;
7300 fi.typedef_field_list = fi.typedef_field_list->next;
7301 *dest = *src;
7302 }
7303 }
c767944b
DJ
7304
7305 do_cleanups (back_to);
c906108c 7306 }
63d06c5c 7307
0b92b5bb
TT
7308 quirk_gcc_member_function_pointer (type, cu->objfile);
7309
90aeadfc
DC
7310 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7311 snapshots) has been known to create a die giving a declaration
7312 for a class that has, as a child, a die giving a definition for a
7313 nested class. So we have to process our children even if the
7314 current die is a declaration. Normally, of course, a declaration
7315 won't have any children at all. */
134d01f1 7316
90aeadfc
DC
7317 while (child_die != NULL && child_die->tag)
7318 {
7319 if (child_die->tag == DW_TAG_member
7320 || child_die->tag == DW_TAG_variable
34eaf542
TT
7321 || child_die->tag == DW_TAG_inheritance
7322 || child_die->tag == DW_TAG_template_value_param
7323 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7324 {
90aeadfc 7325 /* Do nothing. */
134d01f1 7326 }
90aeadfc
DC
7327 else
7328 process_die (child_die, cu);
134d01f1 7329
90aeadfc 7330 child_die = sibling_die (child_die);
134d01f1
DJ
7331 }
7332
fa4028e9
JB
7333 /* Do not consider external references. According to the DWARF standard,
7334 these DIEs are identified by the fact that they have no byte_size
7335 attribute, and a declaration attribute. */
7336 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7337 || !die_is_declaration (die, cu))
c767944b 7338 new_symbol (die, type, cu);
134d01f1
DJ
7339}
7340
7341/* Given a DW_AT_enumeration_type die, set its type. We do not
7342 complete the type's fields yet, or create any symbols. */
c906108c 7343
f792889a 7344static struct type *
134d01f1 7345read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7346{
e7c27a73 7347 struct objfile *objfile = cu->objfile;
c906108c 7348 struct type *type;
c906108c 7349 struct attribute *attr;
0114d602 7350 const char *name;
134d01f1 7351
348e048f
DE
7352 /* If the definition of this type lives in .debug_types, read that type.
7353 Don't follow DW_AT_specification though, that will take us back up
7354 the chain and we want to go down. */
7355 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7356 if (attr)
7357 {
7358 struct dwarf2_cu *type_cu = cu;
7359 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7360
348e048f 7361 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7362
7363 /* TYPE_CU may not be the same as CU.
7364 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7365 return set_die_type (die, type, cu);
7366 }
7367
c906108c
SS
7368 type = alloc_type (objfile);
7369
7370 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7371 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7372 if (name != NULL)
0114d602 7373 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7374
e142c38c 7375 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7376 if (attr)
7377 {
7378 TYPE_LENGTH (type) = DW_UNSND (attr);
7379 }
7380 else
7381 {
7382 TYPE_LENGTH (type) = 0;
7383 }
7384
137033e9
JB
7385 /* The enumeration DIE can be incomplete. In Ada, any type can be
7386 declared as private in the package spec, and then defined only
7387 inside the package body. Such types are known as Taft Amendment
7388 Types. When another package uses such a type, an incomplete DIE
7389 may be generated by the compiler. */
02eb380e 7390 if (die_is_declaration (die, cu))
876cecd0 7391 TYPE_STUB (type) = 1;
02eb380e 7392
f792889a 7393 return set_die_type (die, type, cu);
134d01f1
DJ
7394}
7395
7396/* Given a pointer to a die which begins an enumeration, process all
7397 the dies that define the members of the enumeration, and create the
7398 symbol for the enumeration type.
7399
7400 NOTE: We reverse the order of the element list. */
7401
7402static void
7403process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7404{
f792889a 7405 struct type *this_type;
134d01f1 7406
f792889a
DJ
7407 this_type = get_die_type (die, cu);
7408 if (this_type == NULL)
7409 this_type = read_enumeration_type (die, cu);
9dc481d3 7410
639d11d3 7411 if (die->child != NULL)
c906108c 7412 {
9dc481d3
DE
7413 struct die_info *child_die;
7414 struct symbol *sym;
7415 struct field *fields = NULL;
7416 int num_fields = 0;
7417 int unsigned_enum = 1;
7418 char *name;
7419
639d11d3 7420 child_die = die->child;
c906108c
SS
7421 while (child_die && child_die->tag)
7422 {
7423 if (child_die->tag != DW_TAG_enumerator)
7424 {
e7c27a73 7425 process_die (child_die, cu);
c906108c
SS
7426 }
7427 else
7428 {
39cbfefa
DJ
7429 name = dwarf2_name (child_die, cu);
7430 if (name)
c906108c 7431 {
f792889a 7432 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7433 if (SYMBOL_VALUE (sym) < 0)
7434 unsigned_enum = 0;
7435
7436 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7437 {
7438 fields = (struct field *)
7439 xrealloc (fields,
7440 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7441 * sizeof (struct field));
c906108c
SS
7442 }
7443
3567439c 7444 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7445 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7446 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7447 FIELD_BITSIZE (fields[num_fields]) = 0;
7448
7449 num_fields++;
7450 }
7451 }
7452
7453 child_die = sibling_die (child_die);
7454 }
7455
7456 if (num_fields)
7457 {
f792889a
DJ
7458 TYPE_NFIELDS (this_type) = num_fields;
7459 TYPE_FIELDS (this_type) = (struct field *)
7460 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7461 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7462 sizeof (struct field) * num_fields);
b8c9b27d 7463 xfree (fields);
c906108c
SS
7464 }
7465 if (unsigned_enum)
876cecd0 7466 TYPE_UNSIGNED (this_type) = 1;
c906108c 7467 }
134d01f1 7468
f792889a 7469 new_symbol (die, this_type, cu);
c906108c
SS
7470}
7471
7472/* Extract all information from a DW_TAG_array_type DIE and put it in
7473 the DIE's type field. For now, this only handles one dimensional
7474 arrays. */
7475
f792889a 7476static struct type *
e7c27a73 7477read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7478{
e7c27a73 7479 struct objfile *objfile = cu->objfile;
c906108c 7480 struct die_info *child_die;
7e314c57 7481 struct type *type;
c906108c
SS
7482 struct type *element_type, *range_type, *index_type;
7483 struct type **range_types = NULL;
7484 struct attribute *attr;
7485 int ndim = 0;
7486 struct cleanup *back_to;
39cbfefa 7487 char *name;
c906108c 7488
e7c27a73 7489 element_type = die_type (die, cu);
c906108c 7490
7e314c57
JK
7491 /* The die_type call above may have already set the type for this DIE. */
7492 type = get_die_type (die, cu);
7493 if (type)
7494 return type;
7495
c906108c
SS
7496 /* Irix 6.2 native cc creates array types without children for
7497 arrays with unspecified length. */
639d11d3 7498 if (die->child == NULL)
c906108c 7499 {
46bf5051 7500 index_type = objfile_type (objfile)->builtin_int;
c906108c 7501 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7502 type = create_array_type (NULL, element_type, range_type);
7503 return set_die_type (die, type, cu);
c906108c
SS
7504 }
7505
7506 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7507 child_die = die->child;
c906108c
SS
7508 while (child_die && child_die->tag)
7509 {
7510 if (child_die->tag == DW_TAG_subrange_type)
7511 {
f792889a 7512 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7513
f792889a 7514 if (child_type != NULL)
a02abb62 7515 {
0963b4bd
MS
7516 /* The range type was succesfully read. Save it for the
7517 array type creation. */
a02abb62
JB
7518 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7519 {
7520 range_types = (struct type **)
7521 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7522 * sizeof (struct type *));
7523 if (ndim == 0)
7524 make_cleanup (free_current_contents, &range_types);
7525 }
f792889a 7526 range_types[ndim++] = child_type;
a02abb62 7527 }
c906108c
SS
7528 }
7529 child_die = sibling_die (child_die);
7530 }
7531
7532 /* Dwarf2 dimensions are output from left to right, create the
7533 necessary array types in backwards order. */
7ca2d3a3 7534
c906108c 7535 type = element_type;
7ca2d3a3
DL
7536
7537 if (read_array_order (die, cu) == DW_ORD_col_major)
7538 {
7539 int i = 0;
9a619af0 7540
7ca2d3a3
DL
7541 while (i < ndim)
7542 type = create_array_type (NULL, type, range_types[i++]);
7543 }
7544 else
7545 {
7546 while (ndim-- > 0)
7547 type = create_array_type (NULL, type, range_types[ndim]);
7548 }
c906108c 7549
f5f8a009
EZ
7550 /* Understand Dwarf2 support for vector types (like they occur on
7551 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7552 array type. This is not part of the Dwarf2/3 standard yet, but a
7553 custom vendor extension. The main difference between a regular
7554 array and the vector variant is that vectors are passed by value
7555 to functions. */
e142c38c 7556 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7557 if (attr)
ea37ba09 7558 make_vector_type (type);
f5f8a009 7559
dbc98a8b
KW
7560 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7561 implementation may choose to implement triple vectors using this
7562 attribute. */
7563 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7564 if (attr)
7565 {
7566 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7567 TYPE_LENGTH (type) = DW_UNSND (attr);
7568 else
3e43a32a
MS
7569 complaint (&symfile_complaints,
7570 _("DW_AT_byte_size for array type smaller "
7571 "than the total size of elements"));
dbc98a8b
KW
7572 }
7573
39cbfefa
DJ
7574 name = dwarf2_name (die, cu);
7575 if (name)
7576 TYPE_NAME (type) = name;
6e70227d 7577
0963b4bd 7578 /* Install the type in the die. */
7e314c57
JK
7579 set_die_type (die, type, cu);
7580
7581 /* set_die_type should be already done. */
b4ba55a1
JB
7582 set_descriptive_type (type, die, cu);
7583
c906108c
SS
7584 do_cleanups (back_to);
7585
7e314c57 7586 return type;
c906108c
SS
7587}
7588
7ca2d3a3 7589static enum dwarf_array_dim_ordering
6e70227d 7590read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7591{
7592 struct attribute *attr;
7593
7594 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7595
7596 if (attr) return DW_SND (attr);
7597
0963b4bd
MS
7598 /* GNU F77 is a special case, as at 08/2004 array type info is the
7599 opposite order to the dwarf2 specification, but data is still
7600 laid out as per normal fortran.
7ca2d3a3 7601
0963b4bd
MS
7602 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7603 version checking. */
7ca2d3a3 7604
905e0470
PM
7605 if (cu->language == language_fortran
7606 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7607 {
7608 return DW_ORD_row_major;
7609 }
7610
6e70227d 7611 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7612 {
7613 case array_column_major:
7614 return DW_ORD_col_major;
7615 case array_row_major:
7616 default:
7617 return DW_ORD_row_major;
7618 };
7619}
7620
72019c9c 7621/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 7622 the DIE's type field. */
72019c9c 7623
f792889a 7624static struct type *
72019c9c
GM
7625read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7626{
7e314c57
JK
7627 struct type *domain_type, *set_type;
7628 struct attribute *attr;
f792889a 7629
7e314c57
JK
7630 domain_type = die_type (die, cu);
7631
7632 /* The die_type call above may have already set the type for this DIE. */
7633 set_type = get_die_type (die, cu);
7634 if (set_type)
7635 return set_type;
7636
7637 set_type = create_set_type (NULL, domain_type);
7638
7639 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7640 if (attr)
7641 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7642
f792889a 7643 return set_die_type (die, set_type, cu);
72019c9c 7644}
7ca2d3a3 7645
c906108c
SS
7646/* First cut: install each common block member as a global variable. */
7647
7648static void
e7c27a73 7649read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7650{
7651 struct die_info *child_die;
7652 struct attribute *attr;
7653 struct symbol *sym;
7654 CORE_ADDR base = (CORE_ADDR) 0;
7655
e142c38c 7656 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7657 if (attr)
7658 {
0963b4bd 7659 /* Support the .debug_loc offsets. */
8e19ed76
PS
7660 if (attr_form_is_block (attr))
7661 {
e7c27a73 7662 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7663 }
3690dd37 7664 else if (attr_form_is_section_offset (attr))
8e19ed76 7665 {
4d3c2250 7666 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7667 }
7668 else
7669 {
4d3c2250
KB
7670 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7671 "common block member");
8e19ed76 7672 }
c906108c 7673 }
639d11d3 7674 if (die->child != NULL)
c906108c 7675 {
639d11d3 7676 child_die = die->child;
c906108c
SS
7677 while (child_die && child_die->tag)
7678 {
e7c27a73 7679 sym = new_symbol (child_die, NULL, cu);
e142c38c 7680 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
f1078f66 7681 if (sym != NULL && attr != NULL)
c906108c 7682 {
d4b96c9a
JK
7683 CORE_ADDR byte_offset = 0;
7684
7685 if (attr_form_is_section_offset (attr))
7686 dwarf2_complex_location_expr_complaint ();
7687 else if (attr_form_is_constant (attr))
7688 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7689 else if (attr_form_is_block (attr))
7690 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7691 else
7692 dwarf2_complex_location_expr_complaint ();
7693
7694 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7695 add_symbol_to_list (sym, &global_symbols);
7696 }
7697 child_die = sibling_die (child_die);
7698 }
7699 }
7700}
7701
0114d602 7702/* Create a type for a C++ namespace. */
d9fa45fe 7703
0114d602
DJ
7704static struct type *
7705read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7706{
e7c27a73 7707 struct objfile *objfile = cu->objfile;
0114d602 7708 const char *previous_prefix, *name;
9219021c 7709 int is_anonymous;
0114d602
DJ
7710 struct type *type;
7711
7712 /* For extensions, reuse the type of the original namespace. */
7713 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7714 {
7715 struct die_info *ext_die;
7716 struct dwarf2_cu *ext_cu = cu;
9a619af0 7717
0114d602
DJ
7718 ext_die = dwarf2_extension (die, &ext_cu);
7719 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7720
7721 /* EXT_CU may not be the same as CU.
7722 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7723 return set_die_type (die, type, cu);
7724 }
9219021c 7725
e142c38c 7726 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7727
7728 /* Now build the name of the current namespace. */
7729
0114d602
DJ
7730 previous_prefix = determine_prefix (die, cu);
7731 if (previous_prefix[0] != '\0')
7732 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7733 previous_prefix, name, 0, cu);
0114d602
DJ
7734
7735 /* Create the type. */
7736 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7737 objfile);
7738 TYPE_NAME (type) = (char *) name;
7739 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7740
60531b24 7741 return set_die_type (die, type, cu);
0114d602
DJ
7742}
7743
7744/* Read a C++ namespace. */
7745
7746static void
7747read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7748{
7749 struct objfile *objfile = cu->objfile;
0114d602 7750 int is_anonymous;
9219021c 7751
5c4e30ca
DC
7752 /* Add a symbol associated to this if we haven't seen the namespace
7753 before. Also, add a using directive if it's an anonymous
7754 namespace. */
9219021c 7755
f2f0e013 7756 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7757 {
7758 struct type *type;
7759
0114d602 7760 type = read_type_die (die, cu);
e7c27a73 7761 new_symbol (die, type, cu);
5c4e30ca 7762
e8e80198 7763 namespace_name (die, &is_anonymous, cu);
5c4e30ca 7764 if (is_anonymous)
0114d602
DJ
7765 {
7766 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7767
c0cc3a76 7768 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7769 NULL, &objfile->objfile_obstack);
0114d602 7770 }
5c4e30ca 7771 }
9219021c 7772
639d11d3 7773 if (die->child != NULL)
d9fa45fe 7774 {
639d11d3 7775 struct die_info *child_die = die->child;
6e70227d 7776
d9fa45fe
DC
7777 while (child_die && child_die->tag)
7778 {
e7c27a73 7779 process_die (child_die, cu);
d9fa45fe
DC
7780 child_die = sibling_die (child_die);
7781 }
7782 }
38d518c9
EZ
7783}
7784
f55ee35c
JK
7785/* Read a Fortran module as type. This DIE can be only a declaration used for
7786 imported module. Still we need that type as local Fortran "use ... only"
7787 declaration imports depend on the created type in determine_prefix. */
7788
7789static struct type *
7790read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7791{
7792 struct objfile *objfile = cu->objfile;
7793 char *module_name;
7794 struct type *type;
7795
7796 module_name = dwarf2_name (die, cu);
7797 if (!module_name)
3e43a32a
MS
7798 complaint (&symfile_complaints,
7799 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
7800 die->offset);
7801 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7802
7803 /* determine_prefix uses TYPE_TAG_NAME. */
7804 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7805
7806 return set_die_type (die, type, cu);
7807}
7808
5d7cb8df
JK
7809/* Read a Fortran module. */
7810
7811static void
7812read_module (struct die_info *die, struct dwarf2_cu *cu)
7813{
7814 struct die_info *child_die = die->child;
7815
5d7cb8df
JK
7816 while (child_die && child_die->tag)
7817 {
7818 process_die (child_die, cu);
7819 child_die = sibling_die (child_die);
7820 }
7821}
7822
38d518c9
EZ
7823/* Return the name of the namespace represented by DIE. Set
7824 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7825 namespace. */
7826
7827static const char *
e142c38c 7828namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7829{
7830 struct die_info *current_die;
7831 const char *name = NULL;
7832
7833 /* Loop through the extensions until we find a name. */
7834
7835 for (current_die = die;
7836 current_die != NULL;
f2f0e013 7837 current_die = dwarf2_extension (die, &cu))
38d518c9 7838 {
e142c38c 7839 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7840 if (name != NULL)
7841 break;
7842 }
7843
7844 /* Is it an anonymous namespace? */
7845
7846 *is_anonymous = (name == NULL);
7847 if (*is_anonymous)
7848 name = "(anonymous namespace)";
7849
7850 return name;
d9fa45fe
DC
7851}
7852
c906108c
SS
7853/* Extract all information from a DW_TAG_pointer_type DIE and add to
7854 the user defined type vector. */
7855
f792889a 7856static struct type *
e7c27a73 7857read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7858{
5e2b427d 7859 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7860 struct comp_unit_head *cu_header = &cu->header;
c906108c 7861 struct type *type;
8b2dbe47
KB
7862 struct attribute *attr_byte_size;
7863 struct attribute *attr_address_class;
7864 int byte_size, addr_class;
7e314c57
JK
7865 struct type *target_type;
7866
7867 target_type = die_type (die, cu);
c906108c 7868
7e314c57
JK
7869 /* The die_type call above may have already set the type for this DIE. */
7870 type = get_die_type (die, cu);
7871 if (type)
7872 return type;
7873
7874 type = lookup_pointer_type (target_type);
8b2dbe47 7875
e142c38c 7876 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7877 if (attr_byte_size)
7878 byte_size = DW_UNSND (attr_byte_size);
c906108c 7879 else
8b2dbe47
KB
7880 byte_size = cu_header->addr_size;
7881
e142c38c 7882 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7883 if (attr_address_class)
7884 addr_class = DW_UNSND (attr_address_class);
7885 else
7886 addr_class = DW_ADDR_none;
7887
7888 /* If the pointer size or address class is different than the
7889 default, create a type variant marked as such and set the
7890 length accordingly. */
7891 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7892 {
5e2b427d 7893 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7894 {
7895 int type_flags;
7896
849957d9 7897 type_flags = gdbarch_address_class_type_flags
5e2b427d 7898 (gdbarch, byte_size, addr_class);
876cecd0
TT
7899 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7900 == 0);
8b2dbe47
KB
7901 type = make_type_with_address_space (type, type_flags);
7902 }
7903 else if (TYPE_LENGTH (type) != byte_size)
7904 {
3e43a32a
MS
7905 complaint (&symfile_complaints,
7906 _("invalid pointer size %d"), byte_size);
8b2dbe47 7907 }
6e70227d 7908 else
9a619af0
MS
7909 {
7910 /* Should we also complain about unhandled address classes? */
7911 }
c906108c 7912 }
8b2dbe47
KB
7913
7914 TYPE_LENGTH (type) = byte_size;
f792889a 7915 return set_die_type (die, type, cu);
c906108c
SS
7916}
7917
7918/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7919 the user defined type vector. */
7920
f792889a 7921static struct type *
e7c27a73 7922read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7923{
7924 struct type *type;
7925 struct type *to_type;
7926 struct type *domain;
7927
e7c27a73
DJ
7928 to_type = die_type (die, cu);
7929 domain = die_containing_type (die, cu);
0d5de010 7930
7e314c57
JK
7931 /* The calls above may have already set the type for this DIE. */
7932 type = get_die_type (die, cu);
7933 if (type)
7934 return type;
7935
0d5de010
DJ
7936 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7937 type = lookup_methodptr_type (to_type);
7938 else
7939 type = lookup_memberptr_type (to_type, domain);
c906108c 7940
f792889a 7941 return set_die_type (die, type, cu);
c906108c
SS
7942}
7943
7944/* Extract all information from a DW_TAG_reference_type DIE and add to
7945 the user defined type vector. */
7946
f792889a 7947static struct type *
e7c27a73 7948read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7949{
e7c27a73 7950 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7951 struct type *type, *target_type;
c906108c
SS
7952 struct attribute *attr;
7953
7e314c57
JK
7954 target_type = die_type (die, cu);
7955
7956 /* The die_type call above may have already set the type for this DIE. */
7957 type = get_die_type (die, cu);
7958 if (type)
7959 return type;
7960
7961 type = lookup_reference_type (target_type);
e142c38c 7962 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7963 if (attr)
7964 {
7965 TYPE_LENGTH (type) = DW_UNSND (attr);
7966 }
7967 else
7968 {
107d2387 7969 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7970 }
f792889a 7971 return set_die_type (die, type, cu);
c906108c
SS
7972}
7973
f792889a 7974static struct type *
e7c27a73 7975read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7976{
f792889a 7977 struct type *base_type, *cv_type;
c906108c 7978
e7c27a73 7979 base_type = die_type (die, cu);
7e314c57
JK
7980
7981 /* The die_type call above may have already set the type for this DIE. */
7982 cv_type = get_die_type (die, cu);
7983 if (cv_type)
7984 return cv_type;
7985
2f608a3a
KW
7986 /* In case the const qualifier is applied to an array type, the element type
7987 is so qualified, not the array type (section 6.7.3 of C99). */
7988 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7989 {
7990 struct type *el_type, *inner_array;
7991
7992 base_type = copy_type (base_type);
7993 inner_array = base_type;
7994
7995 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7996 {
7997 TYPE_TARGET_TYPE (inner_array) =
7998 copy_type (TYPE_TARGET_TYPE (inner_array));
7999 inner_array = TYPE_TARGET_TYPE (inner_array);
8000 }
8001
8002 el_type = TYPE_TARGET_TYPE (inner_array);
8003 TYPE_TARGET_TYPE (inner_array) =
8004 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8005
8006 return set_die_type (die, base_type, cu);
8007 }
8008
f792889a
DJ
8009 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8010 return set_die_type (die, cv_type, cu);
c906108c
SS
8011}
8012
f792889a 8013static struct type *
e7c27a73 8014read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8015{
f792889a 8016 struct type *base_type, *cv_type;
c906108c 8017
e7c27a73 8018 base_type = die_type (die, cu);
7e314c57
JK
8019
8020 /* The die_type call above may have already set the type for this DIE. */
8021 cv_type = get_die_type (die, cu);
8022 if (cv_type)
8023 return cv_type;
8024
f792889a
DJ
8025 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8026 return set_die_type (die, cv_type, cu);
c906108c
SS
8027}
8028
8029/* Extract all information from a DW_TAG_string_type DIE and add to
8030 the user defined type vector. It isn't really a user defined type,
8031 but it behaves like one, with other DIE's using an AT_user_def_type
8032 attribute to reference it. */
8033
f792889a 8034static struct type *
e7c27a73 8035read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8036{
e7c27a73 8037 struct objfile *objfile = cu->objfile;
3b7538c0 8038 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8039 struct type *type, *range_type, *index_type, *char_type;
8040 struct attribute *attr;
8041 unsigned int length;
8042
e142c38c 8043 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8044 if (attr)
8045 {
8046 length = DW_UNSND (attr);
8047 }
8048 else
8049 {
0963b4bd 8050 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8051 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8052 if (attr)
8053 {
8054 length = DW_UNSND (attr);
8055 }
8056 else
8057 {
8058 length = 1;
8059 }
c906108c 8060 }
6ccb9162 8061
46bf5051 8062 index_type = objfile_type (objfile)->builtin_int;
c906108c 8063 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8064 char_type = language_string_char_type (cu->language_defn, gdbarch);
8065 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8066
f792889a 8067 return set_die_type (die, type, cu);
c906108c
SS
8068}
8069
8070/* Handle DIES due to C code like:
8071
8072 struct foo
c5aa993b
JM
8073 {
8074 int (*funcp)(int a, long l);
8075 int b;
8076 };
c906108c 8077
0963b4bd 8078 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8079
f792889a 8080static struct type *
e7c27a73 8081read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8082{
0963b4bd
MS
8083 struct type *type; /* Type that this function returns. */
8084 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8085 struct attribute *attr;
8086
e7c27a73 8087 type = die_type (die, cu);
7e314c57
JK
8088
8089 /* The die_type call above may have already set the type for this DIE. */
8090 ftype = get_die_type (die, cu);
8091 if (ftype)
8092 return ftype;
8093
0c8b41f1 8094 ftype = lookup_function_type (type);
c906108c 8095
5b8101ae 8096 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8097 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8098 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8099 || cu->language == language_cplus
5b8101ae
PM
8100 || cu->language == language_java
8101 || cu->language == language_pascal)
876cecd0 8102 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8103 else if (producer_is_realview (cu->producer))
8104 /* RealView does not emit DW_AT_prototyped. We can not
8105 distinguish prototyped and unprototyped functions; default to
8106 prototyped, since that is more common in modern code (and
8107 RealView warns about unprototyped functions). */
8108 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8109
c055b101
CV
8110 /* Store the calling convention in the type if it's available in
8111 the subroutine die. Otherwise set the calling convention to
8112 the default value DW_CC_normal. */
8113 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8114 if (attr)
8115 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8116 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8117 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8118 else
8119 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8120
8121 /* We need to add the subroutine type to the die immediately so
8122 we don't infinitely recurse when dealing with parameters
0963b4bd 8123 declared as the same subroutine type. */
76c10ea2 8124 set_die_type (die, ftype, cu);
6e70227d 8125
639d11d3 8126 if (die->child != NULL)
c906108c 8127 {
8072405b 8128 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 8129 struct die_info *child_die;
8072405b 8130 int nparams, iparams;
c906108c
SS
8131
8132 /* Count the number of parameters.
8133 FIXME: GDB currently ignores vararg functions, but knows about
8134 vararg member functions. */
8072405b 8135 nparams = 0;
639d11d3 8136 child_die = die->child;
c906108c
SS
8137 while (child_die && child_die->tag)
8138 {
8139 if (child_die->tag == DW_TAG_formal_parameter)
8140 nparams++;
8141 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8142 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8143 child_die = sibling_die (child_die);
8144 }
8145
8146 /* Allocate storage for parameters and fill them in. */
8147 TYPE_NFIELDS (ftype) = nparams;
8148 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8149 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8150
8072405b
JK
8151 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8152 even if we error out during the parameters reading below. */
8153 for (iparams = 0; iparams < nparams; iparams++)
8154 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8155
8156 iparams = 0;
639d11d3 8157 child_die = die->child;
c906108c
SS
8158 while (child_die && child_die->tag)
8159 {
8160 if (child_die->tag == DW_TAG_formal_parameter)
8161 {
3ce3b1ba
PA
8162 struct type *arg_type;
8163
8164 /* DWARF version 2 has no clean way to discern C++
8165 static and non-static member functions. G++ helps
8166 GDB by marking the first parameter for non-static
8167 member functions (which is the this pointer) as
8168 artificial. We pass this information to
8169 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8170
8171 DWARF version 3 added DW_AT_object_pointer, which GCC
8172 4.5 does not yet generate. */
e142c38c 8173 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8174 if (attr)
8175 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8176 else
418835cc
KS
8177 {
8178 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8179
8180 /* GCC/43521: In java, the formal parameter
8181 "this" is sometimes not marked with DW_AT_artificial. */
8182 if (cu->language == language_java)
8183 {
8184 const char *name = dwarf2_name (child_die, cu);
9a619af0 8185
418835cc
KS
8186 if (name && !strcmp (name, "this"))
8187 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8188 }
8189 }
3ce3b1ba
PA
8190 arg_type = die_type (child_die, cu);
8191
8192 /* RealView does not mark THIS as const, which the testsuite
8193 expects. GCC marks THIS as const in method definitions,
8194 but not in the class specifications (GCC PR 43053). */
8195 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8196 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8197 {
8198 int is_this = 0;
8199 struct dwarf2_cu *arg_cu = cu;
8200 const char *name = dwarf2_name (child_die, cu);
8201
8202 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8203 if (attr)
8204 {
8205 /* If the compiler emits this, use it. */
8206 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8207 is_this = 1;
8208 }
8209 else if (name && strcmp (name, "this") == 0)
8210 /* Function definitions will have the argument names. */
8211 is_this = 1;
8212 else if (name == NULL && iparams == 0)
8213 /* Declarations may not have the names, so like
8214 elsewhere in GDB, assume an artificial first
8215 argument is "this". */
8216 is_this = 1;
8217
8218 if (is_this)
8219 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8220 arg_type, 0);
8221 }
8222
8223 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8224 iparams++;
8225 }
8226 child_die = sibling_die (child_die);
8227 }
8228 }
8229
76c10ea2 8230 return ftype;
c906108c
SS
8231}
8232
f792889a 8233static struct type *
e7c27a73 8234read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8235{
e7c27a73 8236 struct objfile *objfile = cu->objfile;
0114d602 8237 const char *name = NULL;
f792889a 8238 struct type *this_type;
c906108c 8239
94af9270 8240 name = dwarf2_full_name (NULL, die, cu);
f792889a 8241 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8242 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8243 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8244 set_die_type (die, this_type, cu);
8245 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8246 return this_type;
c906108c
SS
8247}
8248
8249/* Find a representation of a given base type and install
8250 it in the TYPE field of the die. */
8251
f792889a 8252static struct type *
e7c27a73 8253read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8254{
e7c27a73 8255 struct objfile *objfile = cu->objfile;
c906108c
SS
8256 struct type *type;
8257 struct attribute *attr;
8258 int encoding = 0, size = 0;
39cbfefa 8259 char *name;
6ccb9162
UW
8260 enum type_code code = TYPE_CODE_INT;
8261 int type_flags = 0;
8262 struct type *target_type = NULL;
c906108c 8263
e142c38c 8264 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8265 if (attr)
8266 {
8267 encoding = DW_UNSND (attr);
8268 }
e142c38c 8269 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8270 if (attr)
8271 {
8272 size = DW_UNSND (attr);
8273 }
39cbfefa 8274 name = dwarf2_name (die, cu);
6ccb9162 8275 if (!name)
c906108c 8276 {
6ccb9162
UW
8277 complaint (&symfile_complaints,
8278 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8279 }
6ccb9162
UW
8280
8281 switch (encoding)
c906108c 8282 {
6ccb9162
UW
8283 case DW_ATE_address:
8284 /* Turn DW_ATE_address into a void * pointer. */
8285 code = TYPE_CODE_PTR;
8286 type_flags |= TYPE_FLAG_UNSIGNED;
8287 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8288 break;
8289 case DW_ATE_boolean:
8290 code = TYPE_CODE_BOOL;
8291 type_flags |= TYPE_FLAG_UNSIGNED;
8292 break;
8293 case DW_ATE_complex_float:
8294 code = TYPE_CODE_COMPLEX;
8295 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8296 break;
8297 case DW_ATE_decimal_float:
8298 code = TYPE_CODE_DECFLOAT;
8299 break;
8300 case DW_ATE_float:
8301 code = TYPE_CODE_FLT;
8302 break;
8303 case DW_ATE_signed:
8304 break;
8305 case DW_ATE_unsigned:
8306 type_flags |= TYPE_FLAG_UNSIGNED;
8307 break;
8308 case DW_ATE_signed_char:
6e70227d 8309 if (cu->language == language_ada || cu->language == language_m2
868a0084 8310 || cu->language == language_pascal)
6ccb9162
UW
8311 code = TYPE_CODE_CHAR;
8312 break;
8313 case DW_ATE_unsigned_char:
868a0084
PM
8314 if (cu->language == language_ada || cu->language == language_m2
8315 || cu->language == language_pascal)
6ccb9162
UW
8316 code = TYPE_CODE_CHAR;
8317 type_flags |= TYPE_FLAG_UNSIGNED;
8318 break;
75079b2b
TT
8319 case DW_ATE_UTF:
8320 /* We just treat this as an integer and then recognize the
8321 type by name elsewhere. */
8322 break;
8323
6ccb9162
UW
8324 default:
8325 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8326 dwarf_type_encoding_name (encoding));
8327 break;
c906108c 8328 }
6ccb9162 8329
0114d602
DJ
8330 type = init_type (code, size, type_flags, NULL, objfile);
8331 TYPE_NAME (type) = name;
6ccb9162
UW
8332 TYPE_TARGET_TYPE (type) = target_type;
8333
0114d602 8334 if (name && strcmp (name, "char") == 0)
876cecd0 8335 TYPE_NOSIGN (type) = 1;
0114d602 8336
f792889a 8337 return set_die_type (die, type, cu);
c906108c
SS
8338}
8339
a02abb62
JB
8340/* Read the given DW_AT_subrange DIE. */
8341
f792889a 8342static struct type *
a02abb62
JB
8343read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8344{
8345 struct type *base_type;
8346 struct type *range_type;
8347 struct attribute *attr;
43bbcdc2
PH
8348 LONGEST low = 0;
8349 LONGEST high = -1;
39cbfefa 8350 char *name;
43bbcdc2 8351 LONGEST negative_mask;
e77813c8 8352
a02abb62 8353 base_type = die_type (die, cu);
953ac07e
JK
8354 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8355 check_typedef (base_type);
a02abb62 8356
7e314c57
JK
8357 /* The die_type call above may have already set the type for this DIE. */
8358 range_type = get_die_type (die, cu);
8359 if (range_type)
8360 return range_type;
8361
e142c38c 8362 if (cu->language == language_fortran)
6e70227d 8363 {
a02abb62
JB
8364 /* FORTRAN implies a lower bound of 1, if not given. */
8365 low = 1;
8366 }
8367
dd5e6932
DJ
8368 /* FIXME: For variable sized arrays either of these could be
8369 a variable rather than a constant value. We'll allow it,
8370 but we don't know how to handle it. */
e142c38c 8371 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8372 if (attr)
8373 low = dwarf2_get_attr_constant_value (attr, 0);
8374
e142c38c 8375 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8376 if (attr)
6e70227d 8377 {
e77813c8 8378 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8379 {
8380 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8381 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8382 FIXME: GDB does not yet know how to handle dynamic
8383 arrays properly, treat them as arrays with unspecified
8384 length for now.
8385
8386 FIXME: jimb/2003-09-22: GDB does not really know
8387 how to handle arrays of unspecified length
8388 either; we just represent them as zero-length
8389 arrays. Choose an appropriate upper bound given
8390 the lower bound we've computed above. */
8391 high = low - 1;
8392 }
8393 else
8394 high = dwarf2_get_attr_constant_value (attr, 1);
8395 }
e77813c8
PM
8396 else
8397 {
8398 attr = dwarf2_attr (die, DW_AT_count, cu);
8399 if (attr)
8400 {
8401 int count = dwarf2_get_attr_constant_value (attr, 1);
8402 high = low + count - 1;
8403 }
c2ff108b
JK
8404 else
8405 {
8406 /* Unspecified array length. */
8407 high = low - 1;
8408 }
e77813c8
PM
8409 }
8410
8411 /* Dwarf-2 specifications explicitly allows to create subrange types
8412 without specifying a base type.
8413 In that case, the base type must be set to the type of
8414 the lower bound, upper bound or count, in that order, if any of these
8415 three attributes references an object that has a type.
8416 If no base type is found, the Dwarf-2 specifications say that
8417 a signed integer type of size equal to the size of an address should
8418 be used.
8419 For the following C code: `extern char gdb_int [];'
8420 GCC produces an empty range DIE.
8421 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 8422 high bound or count are not yet handled by this code. */
e77813c8
PM
8423 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8424 {
8425 struct objfile *objfile = cu->objfile;
8426 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8427 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8428 struct type *int_type = objfile_type (objfile)->builtin_int;
8429
8430 /* Test "int", "long int", and "long long int" objfile types,
8431 and select the first one having a size above or equal to the
8432 architecture address size. */
8433 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8434 base_type = int_type;
8435 else
8436 {
8437 int_type = objfile_type (objfile)->builtin_long;
8438 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8439 base_type = int_type;
8440 else
8441 {
8442 int_type = objfile_type (objfile)->builtin_long_long;
8443 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8444 base_type = int_type;
8445 }
8446 }
8447 }
a02abb62 8448
6e70227d 8449 negative_mask =
43bbcdc2
PH
8450 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8451 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8452 low |= negative_mask;
8453 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8454 high |= negative_mask;
8455
a02abb62
JB
8456 range_type = create_range_type (NULL, base_type, low, high);
8457
bbb0eef6
JK
8458 /* Mark arrays with dynamic length at least as an array of unspecified
8459 length. GDB could check the boundary but before it gets implemented at
8460 least allow accessing the array elements. */
8461 if (attr && attr->form == DW_FORM_block1)
8462 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8463
c2ff108b
JK
8464 /* Ada expects an empty array on no boundary attributes. */
8465 if (attr == NULL && cu->language != language_ada)
8466 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8467
39cbfefa
DJ
8468 name = dwarf2_name (die, cu);
8469 if (name)
8470 TYPE_NAME (range_type) = name;
6e70227d 8471
e142c38c 8472 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8473 if (attr)
8474 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8475
7e314c57
JK
8476 set_die_type (die, range_type, cu);
8477
8478 /* set_die_type should be already done. */
b4ba55a1
JB
8479 set_descriptive_type (range_type, die, cu);
8480
7e314c57 8481 return range_type;
a02abb62 8482}
6e70227d 8483
f792889a 8484static struct type *
81a17f79
JB
8485read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8486{
8487 struct type *type;
81a17f79 8488
81a17f79
JB
8489 /* For now, we only support the C meaning of an unspecified type: void. */
8490
0114d602
DJ
8491 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8492 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8493
f792889a 8494 return set_die_type (die, type, cu);
81a17f79 8495}
a02abb62 8496
51545339
DJ
8497/* Trivial hash function for die_info: the hash value of a DIE
8498 is its offset in .debug_info for this objfile. */
8499
8500static hashval_t
8501die_hash (const void *item)
8502{
8503 const struct die_info *die = item;
9a619af0 8504
51545339
DJ
8505 return die->offset;
8506}
8507
8508/* Trivial comparison function for die_info structures: two DIEs
8509 are equal if they have the same offset. */
8510
8511static int
8512die_eq (const void *item_lhs, const void *item_rhs)
8513{
8514 const struct die_info *die_lhs = item_lhs;
8515 const struct die_info *die_rhs = item_rhs;
9a619af0 8516
51545339
DJ
8517 return die_lhs->offset == die_rhs->offset;
8518}
8519
c906108c
SS
8520/* Read a whole compilation unit into a linked list of dies. */
8521
f9aca02d 8522static struct die_info *
93311388 8523read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8524{
93311388 8525 struct die_reader_specs reader_specs;
98bfdba5 8526 int read_abbrevs = 0;
1d9ec526 8527 struct cleanup *back_to = NULL;
98bfdba5
PA
8528 struct die_info *die;
8529
8530 if (cu->dwarf2_abbrevs == NULL)
8531 {
8532 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8533 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8534 read_abbrevs = 1;
8535 }
93311388 8536
348e048f 8537 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8538 cu->die_hash
8539 = htab_create_alloc_ex (cu->header.length / 12,
8540 die_hash,
8541 die_eq,
8542 NULL,
8543 &cu->comp_unit_obstack,
8544 hashtab_obstack_allocate,
8545 dummy_obstack_deallocate);
8546
93311388
DE
8547 init_cu_die_reader (&reader_specs, cu);
8548
98bfdba5
PA
8549 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8550
8551 if (read_abbrevs)
8552 do_cleanups (back_to);
8553
8554 return die;
639d11d3
DC
8555}
8556
d97bc12b
DE
8557/* Main entry point for reading a DIE and all children.
8558 Read the DIE and dump it if requested. */
8559
8560static struct die_info *
93311388
DE
8561read_die_and_children (const struct die_reader_specs *reader,
8562 gdb_byte *info_ptr,
d97bc12b
DE
8563 gdb_byte **new_info_ptr,
8564 struct die_info *parent)
8565{
93311388 8566 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8567 new_info_ptr, parent);
8568
8569 if (dwarf2_die_debug)
8570 {
348e048f
DE
8571 fprintf_unfiltered (gdb_stdlog,
8572 "\nRead die from %s of %s:\n",
8573 reader->buffer == dwarf2_per_objfile->info.buffer
8574 ? ".debug_info"
8575 : reader->buffer == dwarf2_per_objfile->types.buffer
8576 ? ".debug_types"
8577 : "unknown section",
8578 reader->abfd->filename);
d97bc12b
DE
8579 dump_die (result, dwarf2_die_debug);
8580 }
8581
8582 return result;
8583}
8584
639d11d3
DC
8585/* Read a single die and all its descendents. Set the die's sibling
8586 field to NULL; set other fields in the die correctly, and set all
8587 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8588 location of the info_ptr after reading all of those dies. PARENT
8589 is the parent of the die in question. */
8590
8591static struct die_info *
93311388
DE
8592read_die_and_children_1 (const struct die_reader_specs *reader,
8593 gdb_byte *info_ptr,
d97bc12b
DE
8594 gdb_byte **new_info_ptr,
8595 struct die_info *parent)
639d11d3
DC
8596{
8597 struct die_info *die;
fe1b8b76 8598 gdb_byte *cur_ptr;
639d11d3
DC
8599 int has_children;
8600
93311388 8601 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8602 if (die == NULL)
8603 {
8604 *new_info_ptr = cur_ptr;
8605 return NULL;
8606 }
93311388 8607 store_in_ref_table (die, reader->cu);
639d11d3
DC
8608
8609 if (has_children)
348e048f 8610 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8611 else
8612 {
8613 die->child = NULL;
8614 *new_info_ptr = cur_ptr;
8615 }
8616
8617 die->sibling = NULL;
8618 die->parent = parent;
8619 return die;
8620}
8621
8622/* Read a die, all of its descendents, and all of its siblings; set
8623 all of the fields of all of the dies correctly. Arguments are as
8624 in read_die_and_children. */
8625
8626static struct die_info *
93311388
DE
8627read_die_and_siblings (const struct die_reader_specs *reader,
8628 gdb_byte *info_ptr,
fe1b8b76 8629 gdb_byte **new_info_ptr,
639d11d3
DC
8630 struct die_info *parent)
8631{
8632 struct die_info *first_die, *last_sibling;
fe1b8b76 8633 gdb_byte *cur_ptr;
639d11d3 8634
c906108c 8635 cur_ptr = info_ptr;
639d11d3
DC
8636 first_die = last_sibling = NULL;
8637
8638 while (1)
c906108c 8639 {
639d11d3 8640 struct die_info *die
93311388 8641 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8642
1d325ec1 8643 if (die == NULL)
c906108c 8644 {
639d11d3
DC
8645 *new_info_ptr = cur_ptr;
8646 return first_die;
c906108c 8647 }
1d325ec1
DJ
8648
8649 if (!first_die)
8650 first_die = die;
c906108c 8651 else
1d325ec1
DJ
8652 last_sibling->sibling = die;
8653
8654 last_sibling = die;
c906108c 8655 }
c906108c
SS
8656}
8657
93311388
DE
8658/* Read the die from the .debug_info section buffer. Set DIEP to
8659 point to a newly allocated die with its information, except for its
8660 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8661 whether the die has children or not. */
8662
8663static gdb_byte *
8664read_full_die (const struct die_reader_specs *reader,
8665 struct die_info **diep, gdb_byte *info_ptr,
8666 int *has_children)
8667{
8668 unsigned int abbrev_number, bytes_read, i, offset;
8669 struct abbrev_info *abbrev;
8670 struct die_info *die;
8671 struct dwarf2_cu *cu = reader->cu;
8672 bfd *abfd = reader->abfd;
8673
8674 offset = info_ptr - reader->buffer;
8675 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8676 info_ptr += bytes_read;
8677 if (!abbrev_number)
8678 {
8679 *diep = NULL;
8680 *has_children = 0;
8681 return info_ptr;
8682 }
8683
8684 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8685 if (!abbrev)
348e048f
DE
8686 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8687 abbrev_number,
8688 bfd_get_filename (abfd));
8689
93311388
DE
8690 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8691 die->offset = offset;
8692 die->tag = abbrev->tag;
8693 die->abbrev = abbrev_number;
8694
8695 die->num_attrs = abbrev->num_attrs;
8696
8697 for (i = 0; i < abbrev->num_attrs; ++i)
8698 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8699 abfd, info_ptr, cu);
8700
8701 *diep = die;
8702 *has_children = abbrev->has_children;
8703 return info_ptr;
8704}
8705
c906108c
SS
8706/* In DWARF version 2, the description of the debugging information is
8707 stored in a separate .debug_abbrev section. Before we read any
8708 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8709 in a hash table. This function also sets flags in CU describing
8710 the data found in the abbrev table. */
c906108c
SS
8711
8712static void
e7c27a73 8713dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8714{
e7c27a73 8715 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8716 gdb_byte *abbrev_ptr;
c906108c
SS
8717 struct abbrev_info *cur_abbrev;
8718 unsigned int abbrev_number, bytes_read, abbrev_name;
8719 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8720 struct attr_abbrev *cur_attrs;
8721 unsigned int allocated_attrs;
c906108c 8722
0963b4bd 8723 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
8724 obstack_init (&cu->abbrev_obstack);
8725 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8726 (ABBREV_HASH_SIZE
8727 * sizeof (struct abbrev_info *)));
8728 memset (cu->dwarf2_abbrevs, 0,
8729 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8730
be391dca
TT
8731 dwarf2_read_section (dwarf2_per_objfile->objfile,
8732 &dwarf2_per_objfile->abbrev);
dce234bc 8733 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8734 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8735 abbrev_ptr += bytes_read;
8736
f3dd6933
DJ
8737 allocated_attrs = ATTR_ALLOC_CHUNK;
8738 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8739
0963b4bd 8740 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
8741 while (abbrev_number)
8742 {
f3dd6933 8743 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8744
8745 /* read in abbrev header */
8746 cur_abbrev->number = abbrev_number;
8747 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8748 abbrev_ptr += bytes_read;
8749 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8750 abbrev_ptr += 1;
8751
72bf9492
DJ
8752 if (cur_abbrev->tag == DW_TAG_namespace)
8753 cu->has_namespace_info = 1;
8754
c906108c
SS
8755 /* now read in declarations */
8756 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8757 abbrev_ptr += bytes_read;
8758 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8759 abbrev_ptr += bytes_read;
8760 while (abbrev_name)
8761 {
f3dd6933 8762 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8763 {
f3dd6933
DJ
8764 allocated_attrs += ATTR_ALLOC_CHUNK;
8765 cur_attrs
8766 = xrealloc (cur_attrs, (allocated_attrs
8767 * sizeof (struct attr_abbrev)));
c906108c 8768 }
ae038cb0
DJ
8769
8770 /* Record whether this compilation unit might have
8771 inter-compilation-unit references. If we don't know what form
8772 this attribute will have, then it might potentially be a
8773 DW_FORM_ref_addr, so we conservatively expect inter-CU
8774 references. */
8775
8776 if (abbrev_form == DW_FORM_ref_addr
8777 || abbrev_form == DW_FORM_indirect)
8778 cu->has_form_ref_addr = 1;
8779
f3dd6933
DJ
8780 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8781 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8782 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8783 abbrev_ptr += bytes_read;
8784 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8785 abbrev_ptr += bytes_read;
8786 }
8787
f3dd6933
DJ
8788 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8789 (cur_abbrev->num_attrs
8790 * sizeof (struct attr_abbrev)));
8791 memcpy (cur_abbrev->attrs, cur_attrs,
8792 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8793
c906108c 8794 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8795 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8796 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8797
8798 /* Get next abbreviation.
8799 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8800 always properly terminated with an abbrev number of 0.
8801 Exit loop if we encounter an abbreviation which we have
8802 already read (which means we are about to read the abbreviations
8803 for the next compile unit) or if the end of the abbreviation
8804 table is reached. */
dce234bc
PP
8805 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8806 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8807 break;
8808 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8809 abbrev_ptr += bytes_read;
e7c27a73 8810 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8811 break;
8812 }
f3dd6933
DJ
8813
8814 xfree (cur_attrs);
c906108c
SS
8815}
8816
f3dd6933 8817/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8818
c906108c 8819static void
f3dd6933 8820dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8821{
f3dd6933 8822 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8823
f3dd6933
DJ
8824 obstack_free (&cu->abbrev_obstack, NULL);
8825 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8826}
8827
8828/* Lookup an abbrev_info structure in the abbrev hash table. */
8829
8830static struct abbrev_info *
e7c27a73 8831dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8832{
8833 unsigned int hash_number;
8834 struct abbrev_info *abbrev;
8835
8836 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8837 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8838
8839 while (abbrev)
8840 {
8841 if (abbrev->number == number)
8842 return abbrev;
8843 else
8844 abbrev = abbrev->next;
8845 }
8846 return NULL;
8847}
8848
72bf9492
DJ
8849/* Returns nonzero if TAG represents a type that we might generate a partial
8850 symbol for. */
8851
8852static int
8853is_type_tag_for_partial (int tag)
8854{
8855 switch (tag)
8856 {
8857#if 0
8858 /* Some types that would be reasonable to generate partial symbols for,
8859 that we don't at present. */
8860 case DW_TAG_array_type:
8861 case DW_TAG_file_type:
8862 case DW_TAG_ptr_to_member_type:
8863 case DW_TAG_set_type:
8864 case DW_TAG_string_type:
8865 case DW_TAG_subroutine_type:
8866#endif
8867 case DW_TAG_base_type:
8868 case DW_TAG_class_type:
680b30c7 8869 case DW_TAG_interface_type:
72bf9492
DJ
8870 case DW_TAG_enumeration_type:
8871 case DW_TAG_structure_type:
8872 case DW_TAG_subrange_type:
8873 case DW_TAG_typedef:
8874 case DW_TAG_union_type:
8875 return 1;
8876 default:
8877 return 0;
8878 }
8879}
8880
8881/* Load all DIEs that are interesting for partial symbols into memory. */
8882
8883static struct partial_die_info *
93311388
DE
8884load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8885 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8886{
8887 struct partial_die_info *part_die;
8888 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8889 struct abbrev_info *abbrev;
8890 unsigned int bytes_read;
5afb4e99 8891 unsigned int load_all = 0;
72bf9492
DJ
8892
8893 int nesting_level = 1;
8894
8895 parent_die = NULL;
8896 last_die = NULL;
8897
5afb4e99
DJ
8898 if (cu->per_cu && cu->per_cu->load_all_dies)
8899 load_all = 1;
8900
72bf9492
DJ
8901 cu->partial_dies
8902 = htab_create_alloc_ex (cu->header.length / 12,
8903 partial_die_hash,
8904 partial_die_eq,
8905 NULL,
8906 &cu->comp_unit_obstack,
8907 hashtab_obstack_allocate,
8908 dummy_obstack_deallocate);
8909
8910 part_die = obstack_alloc (&cu->comp_unit_obstack,
8911 sizeof (struct partial_die_info));
8912
8913 while (1)
8914 {
8915 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8916
8917 /* A NULL abbrev means the end of a series of children. */
8918 if (abbrev == NULL)
8919 {
8920 if (--nesting_level == 0)
8921 {
8922 /* PART_DIE was probably the last thing allocated on the
8923 comp_unit_obstack, so we could call obstack_free
8924 here. We don't do that because the waste is small,
8925 and will be cleaned up when we're done with this
8926 compilation unit. This way, we're also more robust
8927 against other users of the comp_unit_obstack. */
8928 return first_die;
8929 }
8930 info_ptr += bytes_read;
8931 last_die = parent_die;
8932 parent_die = parent_die->die_parent;
8933 continue;
8934 }
8935
98bfdba5
PA
8936 /* Check for template arguments. We never save these; if
8937 they're seen, we just mark the parent, and go on our way. */
8938 if (parent_die != NULL
8939 && cu->language == language_cplus
8940 && (abbrev->tag == DW_TAG_template_type_param
8941 || abbrev->tag == DW_TAG_template_value_param))
8942 {
8943 parent_die->has_template_arguments = 1;
8944
8945 if (!load_all)
8946 {
8947 /* We don't need a partial DIE for the template argument. */
8948 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8949 cu);
8950 continue;
8951 }
8952 }
8953
8954 /* We only recurse into subprograms looking for template arguments.
8955 Skip their other children. */
8956 if (!load_all
8957 && cu->language == language_cplus
8958 && parent_die != NULL
8959 && parent_die->tag == DW_TAG_subprogram)
8960 {
8961 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8962 continue;
8963 }
8964
5afb4e99
DJ
8965 /* Check whether this DIE is interesting enough to save. Normally
8966 we would not be interested in members here, but there may be
8967 later variables referencing them via DW_AT_specification (for
8968 static members). */
8969 if (!load_all
8970 && !is_type_tag_for_partial (abbrev->tag)
72929c62 8971 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
8972 && abbrev->tag != DW_TAG_enumerator
8973 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8974 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8975 && abbrev->tag != DW_TAG_variable
5afb4e99 8976 && abbrev->tag != DW_TAG_namespace
f55ee35c 8977 && abbrev->tag != DW_TAG_module
5afb4e99 8978 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8979 {
8980 /* Otherwise we skip to the next sibling, if any. */
93311388 8981 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8982 continue;
8983 }
8984
93311388
DE
8985 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8986 buffer, info_ptr, cu);
72bf9492
DJ
8987
8988 /* This two-pass algorithm for processing partial symbols has a
8989 high cost in cache pressure. Thus, handle some simple cases
8990 here which cover the majority of C partial symbols. DIEs
8991 which neither have specification tags in them, nor could have
8992 specification tags elsewhere pointing at them, can simply be
8993 processed and discarded.
8994
8995 This segment is also optional; scan_partial_symbols and
8996 add_partial_symbol will handle these DIEs if we chain
8997 them in normally. When compilers which do not emit large
8998 quantities of duplicate debug information are more common,
8999 this code can probably be removed. */
9000
9001 /* Any complete simple types at the top level (pretty much all
9002 of them, for a language without namespaces), can be processed
9003 directly. */
9004 if (parent_die == NULL
9005 && part_die->has_specification == 0
9006 && part_die->is_declaration == 0
d8228535 9007 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9008 || part_die->tag == DW_TAG_base_type
9009 || part_die->tag == DW_TAG_subrange_type))
9010 {
9011 if (building_psymtab && part_die->name != NULL)
04a679b8 9012 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
9013 VAR_DOMAIN, LOC_TYPEDEF,
9014 &cu->objfile->static_psymbols,
9015 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 9016 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9017 continue;
9018 }
9019
d8228535
JK
9020 /* The exception for DW_TAG_typedef with has_children above is
9021 a workaround of GCC PR debug/47510. In the case of this complaint
9022 type_name_no_tag_or_error will error on such types later.
9023
9024 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9025 it could not find the child DIEs referenced later, this is checked
9026 above. In correct DWARF DW_TAG_typedef should have no children. */
9027
9028 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9029 complaint (&symfile_complaints,
9030 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9031 "- DIE at 0x%x [in module %s]"),
9032 part_die->offset, cu->objfile->name);
9033
72bf9492
DJ
9034 /* If we're at the second level, and we're an enumerator, and
9035 our parent has no specification (meaning possibly lives in a
9036 namespace elsewhere), then we can add the partial symbol now
9037 instead of queueing it. */
9038 if (part_die->tag == DW_TAG_enumerator
9039 && parent_die != NULL
9040 && parent_die->die_parent == NULL
9041 && parent_die->tag == DW_TAG_enumeration_type
9042 && parent_die->has_specification == 0)
9043 {
9044 if (part_die->name == NULL)
3e43a32a
MS
9045 complaint (&symfile_complaints,
9046 _("malformed enumerator DIE ignored"));
72bf9492 9047 else if (building_psymtab)
04a679b8 9048 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9049 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9050 (cu->language == language_cplus
9051 || cu->language == language_java)
72bf9492
DJ
9052 ? &cu->objfile->global_psymbols
9053 : &cu->objfile->static_psymbols,
9054 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9055
93311388 9056 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9057 continue;
9058 }
9059
9060 /* We'll save this DIE so link it in. */
9061 part_die->die_parent = parent_die;
9062 part_die->die_sibling = NULL;
9063 part_die->die_child = NULL;
9064
9065 if (last_die && last_die == parent_die)
9066 last_die->die_child = part_die;
9067 else if (last_die)
9068 last_die->die_sibling = part_die;
9069
9070 last_die = part_die;
9071
9072 if (first_die == NULL)
9073 first_die = part_die;
9074
9075 /* Maybe add the DIE to the hash table. Not all DIEs that we
9076 find interesting need to be in the hash table, because we
9077 also have the parent/sibling/child chains; only those that we
9078 might refer to by offset later during partial symbol reading.
9079
9080 For now this means things that might have be the target of a
9081 DW_AT_specification, DW_AT_abstract_origin, or
9082 DW_AT_extension. DW_AT_extension will refer only to
9083 namespaces; DW_AT_abstract_origin refers to functions (and
9084 many things under the function DIE, but we do not recurse
9085 into function DIEs during partial symbol reading) and
9086 possibly variables as well; DW_AT_specification refers to
9087 declarations. Declarations ought to have the DW_AT_declaration
9088 flag. It happens that GCC forgets to put it in sometimes, but
9089 only for functions, not for types.
9090
9091 Adding more things than necessary to the hash table is harmless
9092 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9093 wasted time in find_partial_die, when we reread the compilation
9094 unit with load_all_dies set. */
72bf9492 9095
5afb4e99 9096 if (load_all
72929c62 9097 || abbrev->tag == DW_TAG_constant
5afb4e99 9098 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9099 || abbrev->tag == DW_TAG_variable
9100 || abbrev->tag == DW_TAG_namespace
9101 || part_die->is_declaration)
9102 {
9103 void **slot;
9104
9105 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9106 part_die->offset, INSERT);
9107 *slot = part_die;
9108 }
9109
9110 part_die = obstack_alloc (&cu->comp_unit_obstack,
9111 sizeof (struct partial_die_info));
9112
9113 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9114 we have no reason to follow the children of structures; for other
98bfdba5
PA
9115 languages we have to, so that we can get at method physnames
9116 to infer fully qualified class names, for DW_AT_specification,
9117 and for C++ template arguments. For C++, we also look one level
9118 inside functions to find template arguments (if the name of the
9119 function does not already contain the template arguments).
bc30ff58
JB
9120
9121 For Ada, we need to scan the children of subprograms and lexical
9122 blocks as well because Ada allows the definition of nested
9123 entities that could be interesting for the debugger, such as
9124 nested subprograms for instance. */
72bf9492 9125 if (last_die->has_children
5afb4e99
DJ
9126 && (load_all
9127 || last_die->tag == DW_TAG_namespace
f55ee35c 9128 || last_die->tag == DW_TAG_module
72bf9492 9129 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9130 || (cu->language == language_cplus
9131 && last_die->tag == DW_TAG_subprogram
9132 && (last_die->name == NULL
9133 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9134 || (cu->language != language_c
9135 && (last_die->tag == DW_TAG_class_type
680b30c7 9136 || last_die->tag == DW_TAG_interface_type
72bf9492 9137 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9138 || last_die->tag == DW_TAG_union_type))
9139 || (cu->language == language_ada
9140 && (last_die->tag == DW_TAG_subprogram
9141 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9142 {
9143 nesting_level++;
9144 parent_die = last_die;
9145 continue;
9146 }
9147
9148 /* Otherwise we skip to the next sibling, if any. */
93311388 9149 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9150
9151 /* Back to the top, do it again. */
9152 }
9153}
9154
c906108c
SS
9155/* Read a minimal amount of information into the minimal die structure. */
9156
fe1b8b76 9157static gdb_byte *
72bf9492
DJ
9158read_partial_die (struct partial_die_info *part_die,
9159 struct abbrev_info *abbrev,
9160 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9161 gdb_byte *buffer, gdb_byte *info_ptr,
9162 struct dwarf2_cu *cu)
c906108c 9163{
fa238c03 9164 unsigned int i;
c906108c 9165 struct attribute attr;
c5aa993b 9166 int has_low_pc_attr = 0;
c906108c
SS
9167 int has_high_pc_attr = 0;
9168
72bf9492 9169 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9170
93311388 9171 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9172
9173 info_ptr += abbrev_len;
9174
9175 if (abbrev == NULL)
9176 return info_ptr;
9177
c906108c
SS
9178 part_die->tag = abbrev->tag;
9179 part_die->has_children = abbrev->has_children;
c906108c
SS
9180
9181 for (i = 0; i < abbrev->num_attrs; ++i)
9182 {
e7c27a73 9183 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9184
9185 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9186 partial symbol table. */
c906108c
SS
9187 switch (attr.name)
9188 {
9189 case DW_AT_name:
71c25dea
TT
9190 switch (part_die->tag)
9191 {
9192 case DW_TAG_compile_unit:
348e048f 9193 case DW_TAG_type_unit:
71c25dea
TT
9194 /* Compilation units have a DW_AT_name that is a filename, not
9195 a source language identifier. */
9196 case DW_TAG_enumeration_type:
9197 case DW_TAG_enumerator:
9198 /* These tags always have simple identifiers already; no need
9199 to canonicalize them. */
9200 part_die->name = DW_STRING (&attr);
9201 break;
9202 default:
9203 part_die->name
9204 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9205 &cu->objfile->objfile_obstack);
71c25dea
TT
9206 break;
9207 }
c906108c 9208 break;
31ef98ae 9209 case DW_AT_linkage_name:
c906108c 9210 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9211 /* Note that both forms of linkage name might appear. We
9212 assume they will be the same, and we only store the last
9213 one we see. */
94af9270
KS
9214 if (cu->language == language_ada)
9215 part_die->name = DW_STRING (&attr);
abc72ce4 9216 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9217 break;
9218 case DW_AT_low_pc:
9219 has_low_pc_attr = 1;
9220 part_die->lowpc = DW_ADDR (&attr);
9221 break;
9222 case DW_AT_high_pc:
9223 has_high_pc_attr = 1;
9224 part_die->highpc = DW_ADDR (&attr);
9225 break;
9226 case DW_AT_location:
0963b4bd 9227 /* Support the .debug_loc offsets. */
8e19ed76
PS
9228 if (attr_form_is_block (&attr))
9229 {
9230 part_die->locdesc = DW_BLOCK (&attr);
9231 }
3690dd37 9232 else if (attr_form_is_section_offset (&attr))
8e19ed76 9233 {
4d3c2250 9234 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9235 }
9236 else
9237 {
4d3c2250
KB
9238 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9239 "partial symbol information");
8e19ed76 9240 }
c906108c 9241 break;
c906108c
SS
9242 case DW_AT_external:
9243 part_die->is_external = DW_UNSND (&attr);
9244 break;
9245 case DW_AT_declaration:
9246 part_die->is_declaration = DW_UNSND (&attr);
9247 break;
9248 case DW_AT_type:
9249 part_die->has_type = 1;
9250 break;
9251 case DW_AT_abstract_origin:
9252 case DW_AT_specification:
72bf9492
DJ
9253 case DW_AT_extension:
9254 part_die->has_specification = 1;
c764a876 9255 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9256 break;
9257 case DW_AT_sibling:
9258 /* Ignore absolute siblings, they might point outside of
9259 the current compile unit. */
9260 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9261 complaint (&symfile_complaints,
9262 _("ignoring absolute DW_AT_sibling"));
c906108c 9263 else
93311388 9264 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9265 break;
fa4028e9
JB
9266 case DW_AT_byte_size:
9267 part_die->has_byte_size = 1;
9268 break;
68511cec
CES
9269 case DW_AT_calling_convention:
9270 /* DWARF doesn't provide a way to identify a program's source-level
9271 entry point. DW_AT_calling_convention attributes are only meant
9272 to describe functions' calling conventions.
9273
9274 However, because it's a necessary piece of information in
9275 Fortran, and because DW_CC_program is the only piece of debugging
9276 information whose definition refers to a 'main program' at all,
9277 several compilers have begun marking Fortran main programs with
9278 DW_CC_program --- even when those functions use the standard
9279 calling conventions.
9280
9281 So until DWARF specifies a way to provide this information and
9282 compilers pick up the new representation, we'll support this
9283 practice. */
9284 if (DW_UNSND (&attr) == DW_CC_program
9285 && cu->language == language_fortran)
01f8c46d
JK
9286 {
9287 set_main_name (part_die->name);
9288
9289 /* As this DIE has a static linkage the name would be difficult
9290 to look up later. */
9291 language_of_main = language_fortran;
9292 }
68511cec 9293 break;
c906108c
SS
9294 default:
9295 break;
9296 }
9297 }
9298
9373cf26
JK
9299 if (has_low_pc_attr && has_high_pc_attr)
9300 {
9301 /* When using the GNU linker, .gnu.linkonce. sections are used to
9302 eliminate duplicate copies of functions and vtables and such.
9303 The linker will arbitrarily choose one and discard the others.
9304 The AT_*_pc values for such functions refer to local labels in
9305 these sections. If the section from that file was discarded, the
9306 labels are not in the output, so the relocs get a value of 0.
9307 If this is a discarded function, mark the pc bounds as invalid,
9308 so that GDB will ignore it. */
9309 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9310 {
9311 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9312
9313 complaint (&symfile_complaints,
9314 _("DW_AT_low_pc %s is zero "
9315 "for DIE at 0x%x [in module %s]"),
9316 paddress (gdbarch, part_die->lowpc),
9317 part_die->offset, cu->objfile->name);
9318 }
9319 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9320 else if (part_die->lowpc >= part_die->highpc)
9321 {
9322 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9323
9324 complaint (&symfile_complaints,
9325 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9326 "for DIE at 0x%x [in module %s]"),
9327 paddress (gdbarch, part_die->lowpc),
9328 paddress (gdbarch, part_die->highpc),
9329 part_die->offset, cu->objfile->name);
9330 }
9331 else
9332 part_die->has_pc_info = 1;
9333 }
85cbf3d3 9334
c906108c
SS
9335 return info_ptr;
9336}
9337
72bf9492
DJ
9338/* Find a cached partial DIE at OFFSET in CU. */
9339
9340static struct partial_die_info *
c764a876 9341find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9342{
9343 struct partial_die_info *lookup_die = NULL;
9344 struct partial_die_info part_die;
9345
9346 part_die.offset = offset;
9347 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9348
72bf9492
DJ
9349 return lookup_die;
9350}
9351
348e048f
DE
9352/* Find a partial DIE at OFFSET, which may or may not be in CU,
9353 except in the case of .debug_types DIEs which do not reference
9354 outside their CU (they do however referencing other types via
55f1336d 9355 DW_FORM_ref_sig8). */
72bf9492
DJ
9356
9357static struct partial_die_info *
c764a876 9358find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9359{
5afb4e99
DJ
9360 struct dwarf2_per_cu_data *per_cu = NULL;
9361 struct partial_die_info *pd = NULL;
72bf9492 9362
348e048f
DE
9363 if (cu->per_cu->from_debug_types)
9364 {
9365 pd = find_partial_die_in_comp_unit (offset, cu);
9366 if (pd != NULL)
9367 return pd;
9368 goto not_found;
9369 }
9370
45452591 9371 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9372 {
9373 pd = find_partial_die_in_comp_unit (offset, cu);
9374 if (pd != NULL)
9375 return pd;
9376 }
72bf9492 9377
ae038cb0
DJ
9378 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9379
98bfdba5
PA
9380 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9381 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9382
9383 per_cu->cu->last_used = 0;
5afb4e99
DJ
9384 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9385
9386 if (pd == NULL && per_cu->load_all_dies == 0)
9387 {
9388 struct cleanup *back_to;
9389 struct partial_die_info comp_unit_die;
9390 struct abbrev_info *abbrev;
9391 unsigned int bytes_read;
9392 char *info_ptr;
9393
9394 per_cu->load_all_dies = 1;
9395
9396 /* Re-read the DIEs. */
9397 back_to = make_cleanup (null_cleanup, 0);
9398 if (per_cu->cu->dwarf2_abbrevs == NULL)
9399 {
9400 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9401 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9402 }
dce234bc 9403 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9404 + per_cu->cu->header.offset
9405 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9406 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9407 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9408 per_cu->cu->objfile->obfd,
9409 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9410 per_cu->cu);
9411 if (comp_unit_die.has_children)
93311388
DE
9412 load_partial_dies (per_cu->cu->objfile->obfd,
9413 dwarf2_per_objfile->info.buffer, info_ptr,
9414 0, per_cu->cu);
5afb4e99
DJ
9415 do_cleanups (back_to);
9416
9417 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9418 }
9419
348e048f
DE
9420 not_found:
9421
5afb4e99
DJ
9422 if (pd == NULL)
9423 internal_error (__FILE__, __LINE__,
3e43a32a
MS
9424 _("could not find partial DIE 0x%x "
9425 "in cache [from module %s]\n"),
5afb4e99
DJ
9426 offset, bfd_get_filename (cu->objfile->obfd));
9427 return pd;
72bf9492
DJ
9428}
9429
abc72ce4
DE
9430/* See if we can figure out if the class lives in a namespace. We do
9431 this by looking for a member function; its demangled name will
9432 contain namespace info, if there is any. */
9433
9434static void
9435guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9436 struct dwarf2_cu *cu)
9437{
9438 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9439 what template types look like, because the demangler
9440 frequently doesn't give the same name as the debug info. We
9441 could fix this by only using the demangled name to get the
9442 prefix (but see comment in read_structure_type). */
9443
9444 struct partial_die_info *real_pdi;
9445 struct partial_die_info *child_pdi;
9446
9447 /* If this DIE (this DIE's specification, if any) has a parent, then
9448 we should not do this. We'll prepend the parent's fully qualified
9449 name when we create the partial symbol. */
9450
9451 real_pdi = struct_pdi;
9452 while (real_pdi->has_specification)
9453 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9454
9455 if (real_pdi->die_parent != NULL)
9456 return;
9457
9458 for (child_pdi = struct_pdi->die_child;
9459 child_pdi != NULL;
9460 child_pdi = child_pdi->die_sibling)
9461 {
9462 if (child_pdi->tag == DW_TAG_subprogram
9463 && child_pdi->linkage_name != NULL)
9464 {
9465 char *actual_class_name
9466 = language_class_name_from_physname (cu->language_defn,
9467 child_pdi->linkage_name);
9468 if (actual_class_name != NULL)
9469 {
9470 struct_pdi->name
9471 = obsavestring (actual_class_name,
9472 strlen (actual_class_name),
9473 &cu->objfile->objfile_obstack);
9474 xfree (actual_class_name);
9475 }
9476 break;
9477 }
9478 }
9479}
9480
72bf9492
DJ
9481/* Adjust PART_DIE before generating a symbol for it. This function
9482 may set the is_external flag or change the DIE's name. */
9483
9484static void
9485fixup_partial_die (struct partial_die_info *part_die,
9486 struct dwarf2_cu *cu)
9487{
abc72ce4
DE
9488 /* Once we've fixed up a die, there's no point in doing so again.
9489 This also avoids a memory leak if we were to call
9490 guess_partial_die_structure_name multiple times. */
9491 if (part_die->fixup_called)
9492 return;
9493
72bf9492
DJ
9494 /* If we found a reference attribute and the DIE has no name, try
9495 to find a name in the referred to DIE. */
9496
9497 if (part_die->name == NULL && part_die->has_specification)
9498 {
9499 struct partial_die_info *spec_die;
72bf9492 9500
10b3939b 9501 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9502
10b3939b 9503 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9504
9505 if (spec_die->name)
9506 {
9507 part_die->name = spec_die->name;
9508
9509 /* Copy DW_AT_external attribute if it is set. */
9510 if (spec_die->is_external)
9511 part_die->is_external = spec_die->is_external;
9512 }
9513 }
9514
9515 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9516
9517 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9518 part_die->name = "(anonymous namespace)";
9519
abc72ce4
DE
9520 /* If there is no parent die to provide a namespace, and there are
9521 children, see if we can determine the namespace from their linkage
9522 name.
9523 NOTE: We need to do this even if cu->has_namespace_info != 0.
9524 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9525 if (cu->language == language_cplus
9526 && dwarf2_per_objfile->types.asection != NULL
9527 && part_die->die_parent == NULL
9528 && part_die->has_children
9529 && (part_die->tag == DW_TAG_class_type
9530 || part_die->tag == DW_TAG_structure_type
9531 || part_die->tag == DW_TAG_union_type))
9532 guess_partial_die_structure_name (part_die, cu);
9533
53832f31
TT
9534 /* GCC might emit a nameless struct or union that has a linkage
9535 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
9536 if (part_die->name == NULL
9537 && (part_die->tag == DW_TAG_structure_type
9538 || part_die->tag == DW_TAG_union_type
9539 || part_die->tag == DW_TAG_class_type)
9540 && part_die->linkage_name != NULL)
9541 {
9542 char *demangled;
9543
9544 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9545 if (demangled)
9546 {
9547 part_die->name = obsavestring (demangled, strlen (demangled),
9548 &cu->objfile->objfile_obstack);
9549 xfree (demangled);
9550 }
9551 }
9552
abc72ce4 9553 part_die->fixup_called = 1;
72bf9492
DJ
9554}
9555
a8329558 9556/* Read an attribute value described by an attribute form. */
c906108c 9557
fe1b8b76 9558static gdb_byte *
a8329558 9559read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9560 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9561 struct dwarf2_cu *cu)
c906108c 9562{
e7c27a73 9563 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9564 unsigned int bytes_read;
9565 struct dwarf_block *blk;
9566
a8329558
KW
9567 attr->form = form;
9568 switch (form)
c906108c 9569 {
c906108c 9570 case DW_FORM_ref_addr:
ae411497
TT
9571 if (cu->header.version == 2)
9572 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9573 else
3e43a32a
MS
9574 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9575 &cu->header, &bytes_read);
ae411497
TT
9576 info_ptr += bytes_read;
9577 break;
9578 case DW_FORM_addr:
e7c27a73 9579 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9580 info_ptr += bytes_read;
c906108c
SS
9581 break;
9582 case DW_FORM_block2:
7b5a2f43 9583 blk = dwarf_alloc_block (cu);
c906108c
SS
9584 blk->size = read_2_bytes (abfd, info_ptr);
9585 info_ptr += 2;
9586 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9587 info_ptr += blk->size;
9588 DW_BLOCK (attr) = blk;
9589 break;
9590 case DW_FORM_block4:
7b5a2f43 9591 blk = dwarf_alloc_block (cu);
c906108c
SS
9592 blk->size = read_4_bytes (abfd, info_ptr);
9593 info_ptr += 4;
9594 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9595 info_ptr += blk->size;
9596 DW_BLOCK (attr) = blk;
9597 break;
9598 case DW_FORM_data2:
9599 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9600 info_ptr += 2;
9601 break;
9602 case DW_FORM_data4:
9603 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9604 info_ptr += 4;
9605 break;
9606 case DW_FORM_data8:
9607 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9608 info_ptr += 8;
9609 break;
2dc7f7b3
TT
9610 case DW_FORM_sec_offset:
9611 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9612 info_ptr += bytes_read;
9613 break;
c906108c 9614 case DW_FORM_string:
9b1c24c8 9615 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9616 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9617 info_ptr += bytes_read;
9618 break;
4bdf3d34
JJ
9619 case DW_FORM_strp:
9620 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9621 &bytes_read);
8285870a 9622 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9623 info_ptr += bytes_read;
9624 break;
2dc7f7b3 9625 case DW_FORM_exprloc:
c906108c 9626 case DW_FORM_block:
7b5a2f43 9627 blk = dwarf_alloc_block (cu);
c906108c
SS
9628 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9629 info_ptr += bytes_read;
9630 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9631 info_ptr += blk->size;
9632 DW_BLOCK (attr) = blk;
9633 break;
9634 case DW_FORM_block1:
7b5a2f43 9635 blk = dwarf_alloc_block (cu);
c906108c
SS
9636 blk->size = read_1_byte (abfd, info_ptr);
9637 info_ptr += 1;
9638 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9639 info_ptr += blk->size;
9640 DW_BLOCK (attr) = blk;
9641 break;
9642 case DW_FORM_data1:
9643 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9644 info_ptr += 1;
9645 break;
9646 case DW_FORM_flag:
9647 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9648 info_ptr += 1;
9649 break;
2dc7f7b3
TT
9650 case DW_FORM_flag_present:
9651 DW_UNSND (attr) = 1;
9652 break;
c906108c
SS
9653 case DW_FORM_sdata:
9654 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9655 info_ptr += bytes_read;
9656 break;
9657 case DW_FORM_udata:
9658 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9659 info_ptr += bytes_read;
9660 break;
9661 case DW_FORM_ref1:
10b3939b 9662 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9663 info_ptr += 1;
9664 break;
9665 case DW_FORM_ref2:
10b3939b 9666 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9667 info_ptr += 2;
9668 break;
9669 case DW_FORM_ref4:
10b3939b 9670 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9671 info_ptr += 4;
9672 break;
613e1657 9673 case DW_FORM_ref8:
10b3939b 9674 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9675 info_ptr += 8;
9676 break;
55f1336d 9677 case DW_FORM_ref_sig8:
348e048f
DE
9678 /* Convert the signature to something we can record in DW_UNSND
9679 for later lookup.
9680 NOTE: This is NULL if the type wasn't found. */
9681 DW_SIGNATURED_TYPE (attr) =
9682 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9683 info_ptr += 8;
9684 break;
c906108c 9685 case DW_FORM_ref_udata:
10b3939b
DJ
9686 DW_ADDR (attr) = (cu->header.offset
9687 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9688 info_ptr += bytes_read;
9689 break;
c906108c 9690 case DW_FORM_indirect:
a8329558
KW
9691 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9692 info_ptr += bytes_read;
e7c27a73 9693 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9694 break;
c906108c 9695 default:
8a3fe4f8 9696 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9697 dwarf_form_name (form),
9698 bfd_get_filename (abfd));
c906108c 9699 }
28e94949
JB
9700
9701 /* We have seen instances where the compiler tried to emit a byte
9702 size attribute of -1 which ended up being encoded as an unsigned
9703 0xffffffff. Although 0xffffffff is technically a valid size value,
9704 an object of this size seems pretty unlikely so we can relatively
9705 safely treat these cases as if the size attribute was invalid and
9706 treat them as zero by default. */
9707 if (attr->name == DW_AT_byte_size
9708 && form == DW_FORM_data4
9709 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9710 {
9711 complaint
9712 (&symfile_complaints,
43bbcdc2
PH
9713 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9714 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9715 DW_UNSND (attr) = 0;
9716 }
28e94949 9717
c906108c
SS
9718 return info_ptr;
9719}
9720
a8329558
KW
9721/* Read an attribute described by an abbreviated attribute. */
9722
fe1b8b76 9723static gdb_byte *
a8329558 9724read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9725 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9726{
9727 attr->name = abbrev->name;
e7c27a73 9728 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9729}
9730
0963b4bd 9731/* Read dwarf information from a buffer. */
c906108c
SS
9732
9733static unsigned int
fe1b8b76 9734read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9735{
fe1b8b76 9736 return bfd_get_8 (abfd, buf);
c906108c
SS
9737}
9738
9739static int
fe1b8b76 9740read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9741{
fe1b8b76 9742 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9743}
9744
9745static unsigned int
fe1b8b76 9746read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9747{
fe1b8b76 9748 return bfd_get_16 (abfd, buf);
c906108c
SS
9749}
9750
9751static int
fe1b8b76 9752read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9753{
fe1b8b76 9754 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9755}
9756
9757static unsigned int
fe1b8b76 9758read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9759{
fe1b8b76 9760 return bfd_get_32 (abfd, buf);
c906108c
SS
9761}
9762
9763static int
fe1b8b76 9764read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9765{
fe1b8b76 9766 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9767}
9768
93311388 9769static ULONGEST
fe1b8b76 9770read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9771{
fe1b8b76 9772 return bfd_get_64 (abfd, buf);
c906108c
SS
9773}
9774
9775static CORE_ADDR
fe1b8b76 9776read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9777 unsigned int *bytes_read)
c906108c 9778{
e7c27a73 9779 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9780 CORE_ADDR retval = 0;
9781
107d2387 9782 if (cu_header->signed_addr_p)
c906108c 9783 {
107d2387
AC
9784 switch (cu_header->addr_size)
9785 {
9786 case 2:
fe1b8b76 9787 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9788 break;
9789 case 4:
fe1b8b76 9790 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9791 break;
9792 case 8:
fe1b8b76 9793 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9794 break;
9795 default:
8e65ff28 9796 internal_error (__FILE__, __LINE__,
e2e0b3e5 9797 _("read_address: bad switch, signed [in module %s]"),
659b0389 9798 bfd_get_filename (abfd));
107d2387
AC
9799 }
9800 }
9801 else
9802 {
9803 switch (cu_header->addr_size)
9804 {
9805 case 2:
fe1b8b76 9806 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9807 break;
9808 case 4:
fe1b8b76 9809 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9810 break;
9811 case 8:
fe1b8b76 9812 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9813 break;
9814 default:
8e65ff28 9815 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
9816 _("read_address: bad switch, "
9817 "unsigned [in module %s]"),
659b0389 9818 bfd_get_filename (abfd));
107d2387 9819 }
c906108c 9820 }
64367e0a 9821
107d2387
AC
9822 *bytes_read = cu_header->addr_size;
9823 return retval;
c906108c
SS
9824}
9825
f7ef9339 9826/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9827 specification allows the initial length to take up either 4 bytes
9828 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9829 bytes describe the length and all offsets will be 8 bytes in length
9830 instead of 4.
9831
f7ef9339
KB
9832 An older, non-standard 64-bit format is also handled by this
9833 function. The older format in question stores the initial length
9834 as an 8-byte quantity without an escape value. Lengths greater
9835 than 2^32 aren't very common which means that the initial 4 bytes
9836 is almost always zero. Since a length value of zero doesn't make
9837 sense for the 32-bit format, this initial zero can be considered to
9838 be an escape value which indicates the presence of the older 64-bit
9839 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9840 greater than 4GB. If it becomes necessary to handle lengths
9841 somewhat larger than 4GB, we could allow other small values (such
9842 as the non-sensical values of 1, 2, and 3) to also be used as
9843 escape values indicating the presence of the old format.
f7ef9339 9844
917c78fc
MK
9845 The value returned via bytes_read should be used to increment the
9846 relevant pointer after calling read_initial_length().
c764a876 9847
613e1657
KB
9848 [ Note: read_initial_length() and read_offset() are based on the
9849 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9850 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9851 from:
9852
f7ef9339 9853 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9854
613e1657
KB
9855 This document is only a draft and is subject to change. (So beware.)
9856
f7ef9339 9857 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9858 determined empirically by examining 64-bit ELF files produced by
9859 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9860
9861 - Kevin, July 16, 2002
613e1657
KB
9862 ] */
9863
9864static LONGEST
c764a876 9865read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9866{
fe1b8b76 9867 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9868
dd373385 9869 if (length == 0xffffffff)
613e1657 9870 {
fe1b8b76 9871 length = bfd_get_64 (abfd, buf + 4);
613e1657 9872 *bytes_read = 12;
613e1657 9873 }
dd373385 9874 else if (length == 0)
f7ef9339 9875 {
dd373385 9876 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9877 length = bfd_get_64 (abfd, buf);
f7ef9339 9878 *bytes_read = 8;
f7ef9339 9879 }
613e1657
KB
9880 else
9881 {
9882 *bytes_read = 4;
613e1657
KB
9883 }
9884
c764a876
DE
9885 return length;
9886}
dd373385 9887
c764a876
DE
9888/* Cover function for read_initial_length.
9889 Returns the length of the object at BUF, and stores the size of the
9890 initial length in *BYTES_READ and stores the size that offsets will be in
9891 *OFFSET_SIZE.
9892 If the initial length size is not equivalent to that specified in
9893 CU_HEADER then issue a complaint.
9894 This is useful when reading non-comp-unit headers. */
dd373385 9895
c764a876
DE
9896static LONGEST
9897read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9898 const struct comp_unit_head *cu_header,
9899 unsigned int *bytes_read,
9900 unsigned int *offset_size)
9901{
9902 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9903
9904 gdb_assert (cu_header->initial_length_size == 4
9905 || cu_header->initial_length_size == 8
9906 || cu_header->initial_length_size == 12);
9907
9908 if (cu_header->initial_length_size != *bytes_read)
9909 complaint (&symfile_complaints,
9910 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9911
c764a876 9912 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9913 return length;
613e1657
KB
9914}
9915
9916/* Read an offset from the data stream. The size of the offset is
917c78fc 9917 given by cu_header->offset_size. */
613e1657
KB
9918
9919static LONGEST
fe1b8b76 9920read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9921 unsigned int *bytes_read)
c764a876
DE
9922{
9923 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9924
c764a876
DE
9925 *bytes_read = cu_header->offset_size;
9926 return offset;
9927}
9928
9929/* Read an offset from the data stream. */
9930
9931static LONGEST
9932read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9933{
9934 LONGEST retval = 0;
9935
c764a876 9936 switch (offset_size)
613e1657
KB
9937 {
9938 case 4:
fe1b8b76 9939 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9940 break;
9941 case 8:
fe1b8b76 9942 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9943 break;
9944 default:
8e65ff28 9945 internal_error (__FILE__, __LINE__,
c764a876 9946 _("read_offset_1: bad switch [in module %s]"),
659b0389 9947 bfd_get_filename (abfd));
613e1657
KB
9948 }
9949
917c78fc 9950 return retval;
613e1657
KB
9951}
9952
fe1b8b76
JB
9953static gdb_byte *
9954read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9955{
9956 /* If the size of a host char is 8 bits, we can return a pointer
9957 to the buffer, otherwise we have to copy the data to a buffer
9958 allocated on the temporary obstack. */
4bdf3d34 9959 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9960 return buf;
c906108c
SS
9961}
9962
9963static char *
9b1c24c8 9964read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9965{
9966 /* If the size of a host char is 8 bits, we can return a pointer
9967 to the string, otherwise we have to copy the string to a buffer
9968 allocated on the temporary obstack. */
4bdf3d34 9969 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9970 if (*buf == '\0')
9971 {
9972 *bytes_read_ptr = 1;
9973 return NULL;
9974 }
fe1b8b76
JB
9975 *bytes_read_ptr = strlen ((char *) buf) + 1;
9976 return (char *) buf;
4bdf3d34
JJ
9977}
9978
9979static char *
fe1b8b76 9980read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9981 const struct comp_unit_head *cu_header,
9982 unsigned int *bytes_read_ptr)
9983{
c764a876 9984 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9985
be391dca 9986 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9987 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9988 {
8a3fe4f8 9989 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9990 bfd_get_filename (abfd));
4bdf3d34 9991 return NULL;
c906108c 9992 }
dce234bc 9993 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9994 {
3e43a32a
MS
9995 error (_("DW_FORM_strp pointing outside of "
9996 ".debug_str section [in module %s]"),
9997 bfd_get_filename (abfd));
c906108c
SS
9998 return NULL;
9999 }
4bdf3d34 10000 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10001 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10002 return NULL;
dce234bc 10003 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10004}
10005
ce5d95e1 10006static unsigned long
fe1b8b76 10007read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10008{
ce5d95e1
JB
10009 unsigned long result;
10010 unsigned int num_read;
c906108c
SS
10011 int i, shift;
10012 unsigned char byte;
10013
10014 result = 0;
10015 shift = 0;
10016 num_read = 0;
10017 i = 0;
10018 while (1)
10019 {
fe1b8b76 10020 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10021 buf++;
10022 num_read++;
ce5d95e1 10023 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10024 if ((byte & 128) == 0)
10025 {
10026 break;
10027 }
10028 shift += 7;
10029 }
10030 *bytes_read_ptr = num_read;
10031 return result;
10032}
10033
ce5d95e1 10034static long
fe1b8b76 10035read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10036{
ce5d95e1 10037 long result;
77e0b926 10038 int i, shift, num_read;
c906108c
SS
10039 unsigned char byte;
10040
10041 result = 0;
10042 shift = 0;
c906108c
SS
10043 num_read = 0;
10044 i = 0;
10045 while (1)
10046 {
fe1b8b76 10047 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10048 buf++;
10049 num_read++;
ce5d95e1 10050 result |= ((long)(byte & 127) << shift);
c906108c
SS
10051 shift += 7;
10052 if ((byte & 128) == 0)
10053 {
10054 break;
10055 }
10056 }
77e0b926
DJ
10057 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10058 result |= -(((long)1) << shift);
c906108c
SS
10059 *bytes_read_ptr = num_read;
10060 return result;
10061}
10062
4bb7a0a7
DJ
10063/* Return a pointer to just past the end of an LEB128 number in BUF. */
10064
fe1b8b76
JB
10065static gdb_byte *
10066skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10067{
10068 int byte;
10069
10070 while (1)
10071 {
fe1b8b76 10072 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10073 buf++;
10074 if ((byte & 128) == 0)
10075 return buf;
10076 }
10077}
10078
c906108c 10079static void
e142c38c 10080set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10081{
10082 switch (lang)
10083 {
10084 case DW_LANG_C89:
76bee0cc 10085 case DW_LANG_C99:
c906108c 10086 case DW_LANG_C:
e142c38c 10087 cu->language = language_c;
c906108c
SS
10088 break;
10089 case DW_LANG_C_plus_plus:
e142c38c 10090 cu->language = language_cplus;
c906108c 10091 break;
6aecb9c2
JB
10092 case DW_LANG_D:
10093 cu->language = language_d;
10094 break;
c906108c
SS
10095 case DW_LANG_Fortran77:
10096 case DW_LANG_Fortran90:
b21b22e0 10097 case DW_LANG_Fortran95:
e142c38c 10098 cu->language = language_fortran;
c906108c
SS
10099 break;
10100 case DW_LANG_Mips_Assembler:
e142c38c 10101 cu->language = language_asm;
c906108c 10102 break;
bebd888e 10103 case DW_LANG_Java:
e142c38c 10104 cu->language = language_java;
bebd888e 10105 break;
c906108c 10106 case DW_LANG_Ada83:
8aaf0b47 10107 case DW_LANG_Ada95:
bc5f45f8
JB
10108 cu->language = language_ada;
10109 break;
72019c9c
GM
10110 case DW_LANG_Modula2:
10111 cu->language = language_m2;
10112 break;
fe8e67fd
PM
10113 case DW_LANG_Pascal83:
10114 cu->language = language_pascal;
10115 break;
22566fbd
DJ
10116 case DW_LANG_ObjC:
10117 cu->language = language_objc;
10118 break;
c906108c
SS
10119 case DW_LANG_Cobol74:
10120 case DW_LANG_Cobol85:
c906108c 10121 default:
e142c38c 10122 cu->language = language_minimal;
c906108c
SS
10123 break;
10124 }
e142c38c 10125 cu->language_defn = language_def (cu->language);
c906108c
SS
10126}
10127
10128/* Return the named attribute or NULL if not there. */
10129
10130static struct attribute *
e142c38c 10131dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10132{
10133 unsigned int i;
10134 struct attribute *spec = NULL;
10135
10136 for (i = 0; i < die->num_attrs; ++i)
10137 {
10138 if (die->attrs[i].name == name)
10b3939b 10139 return &die->attrs[i];
c906108c
SS
10140 if (die->attrs[i].name == DW_AT_specification
10141 || die->attrs[i].name == DW_AT_abstract_origin)
10142 spec = &die->attrs[i];
10143 }
c906108c 10144
10b3939b 10145 if (spec)
f2f0e013
DJ
10146 {
10147 die = follow_die_ref (die, spec, &cu);
10148 return dwarf2_attr (die, name, cu);
10149 }
c5aa993b 10150
c906108c
SS
10151 return NULL;
10152}
10153
348e048f
DE
10154/* Return the named attribute or NULL if not there,
10155 but do not follow DW_AT_specification, etc.
10156 This is for use in contexts where we're reading .debug_types dies.
10157 Following DW_AT_specification, DW_AT_abstract_origin will take us
10158 back up the chain, and we want to go down. */
10159
10160static struct attribute *
10161dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10162 struct dwarf2_cu *cu)
10163{
10164 unsigned int i;
10165
10166 for (i = 0; i < die->num_attrs; ++i)
10167 if (die->attrs[i].name == name)
10168 return &die->attrs[i];
10169
10170 return NULL;
10171}
10172
05cf31d1
JB
10173/* Return non-zero iff the attribute NAME is defined for the given DIE,
10174 and holds a non-zero value. This function should only be used for
2dc7f7b3 10175 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10176
10177static int
10178dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10179{
10180 struct attribute *attr = dwarf2_attr (die, name, cu);
10181
10182 return (attr && DW_UNSND (attr));
10183}
10184
3ca72b44 10185static int
e142c38c 10186die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10187{
05cf31d1
JB
10188 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10189 which value is non-zero. However, we have to be careful with
10190 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10191 (via dwarf2_flag_true_p) follows this attribute. So we may
10192 end up accidently finding a declaration attribute that belongs
10193 to a different DIE referenced by the specification attribute,
10194 even though the given DIE does not have a declaration attribute. */
10195 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10196 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10197}
10198
63d06c5c 10199/* Return the die giving the specification for DIE, if there is
f2f0e013 10200 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10201 containing the return value on output. If there is no
10202 specification, but there is an abstract origin, that is
10203 returned. */
63d06c5c
DC
10204
10205static struct die_info *
f2f0e013 10206die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10207{
f2f0e013
DJ
10208 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10209 *spec_cu);
63d06c5c 10210
edb3359d
DJ
10211 if (spec_attr == NULL)
10212 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10213
63d06c5c
DC
10214 if (spec_attr == NULL)
10215 return NULL;
10216 else
f2f0e013 10217 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10218}
c906108c 10219
debd256d 10220/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10221 refers to.
10222 NOTE: This is also used as a "cleanup" function. */
10223
debd256d
JB
10224static void
10225free_line_header (struct line_header *lh)
10226{
10227 if (lh->standard_opcode_lengths)
a8bc7b56 10228 xfree (lh->standard_opcode_lengths);
debd256d
JB
10229
10230 /* Remember that all the lh->file_names[i].name pointers are
10231 pointers into debug_line_buffer, and don't need to be freed. */
10232 if (lh->file_names)
a8bc7b56 10233 xfree (lh->file_names);
debd256d
JB
10234
10235 /* Similarly for the include directory names. */
10236 if (lh->include_dirs)
a8bc7b56 10237 xfree (lh->include_dirs);
debd256d 10238
a8bc7b56 10239 xfree (lh);
debd256d
JB
10240}
10241
debd256d 10242/* Add an entry to LH's include directory table. */
ae2de4f8 10243
debd256d
JB
10244static void
10245add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10246{
debd256d
JB
10247 /* Grow the array if necessary. */
10248 if (lh->include_dirs_size == 0)
c5aa993b 10249 {
debd256d
JB
10250 lh->include_dirs_size = 1; /* for testing */
10251 lh->include_dirs = xmalloc (lh->include_dirs_size
10252 * sizeof (*lh->include_dirs));
10253 }
10254 else if (lh->num_include_dirs >= lh->include_dirs_size)
10255 {
10256 lh->include_dirs_size *= 2;
10257 lh->include_dirs = xrealloc (lh->include_dirs,
10258 (lh->include_dirs_size
10259 * sizeof (*lh->include_dirs)));
c5aa993b 10260 }
c906108c 10261
debd256d
JB
10262 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10263}
6e70227d 10264
debd256d 10265/* Add an entry to LH's file name table. */
ae2de4f8 10266
debd256d
JB
10267static void
10268add_file_name (struct line_header *lh,
10269 char *name,
10270 unsigned int dir_index,
10271 unsigned int mod_time,
10272 unsigned int length)
10273{
10274 struct file_entry *fe;
10275
10276 /* Grow the array if necessary. */
10277 if (lh->file_names_size == 0)
10278 {
10279 lh->file_names_size = 1; /* for testing */
10280 lh->file_names = xmalloc (lh->file_names_size
10281 * sizeof (*lh->file_names));
10282 }
10283 else if (lh->num_file_names >= lh->file_names_size)
10284 {
10285 lh->file_names_size *= 2;
10286 lh->file_names = xrealloc (lh->file_names,
10287 (lh->file_names_size
10288 * sizeof (*lh->file_names)));
10289 }
10290
10291 fe = &lh->file_names[lh->num_file_names++];
10292 fe->name = name;
10293 fe->dir_index = dir_index;
10294 fe->mod_time = mod_time;
10295 fe->length = length;
aaa75496 10296 fe->included_p = 0;
cb1df416 10297 fe->symtab = NULL;
debd256d 10298}
6e70227d 10299
debd256d 10300/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10301 .debug_line, according to the endianness of ABFD. Return a pointer
10302 to a struct line_header, allocated using xmalloc.
debd256d
JB
10303
10304 NOTE: the strings in the include directory and file name tables of
10305 the returned object point into debug_line_buffer, and must not be
10306 freed. */
ae2de4f8 10307
debd256d
JB
10308static struct line_header *
10309dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10310 struct dwarf2_cu *cu)
debd256d
JB
10311{
10312 struct cleanup *back_to;
10313 struct line_header *lh;
fe1b8b76 10314 gdb_byte *line_ptr;
c764a876 10315 unsigned int bytes_read, offset_size;
debd256d
JB
10316 int i;
10317 char *cur_dir, *cur_file;
10318
be391dca 10319 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10320 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10321 {
e2e0b3e5 10322 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10323 return 0;
10324 }
10325
a738430d
MK
10326 /* Make sure that at least there's room for the total_length field.
10327 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10328 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10329 {
4d3c2250 10330 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10331 return 0;
10332 }
10333
10334 lh = xmalloc (sizeof (*lh));
10335 memset (lh, 0, sizeof (*lh));
10336 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10337 (void *) lh);
10338
dce234bc 10339 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10340
a738430d 10341 /* Read in the header. */
6e70227d 10342 lh->total_length =
c764a876
DE
10343 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10344 &bytes_read, &offset_size);
debd256d 10345 line_ptr += bytes_read;
dce234bc
PP
10346 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10347 + dwarf2_per_objfile->line.size))
debd256d 10348 {
4d3c2250 10349 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10350 return 0;
10351 }
10352 lh->statement_program_end = line_ptr + lh->total_length;
10353 lh->version = read_2_bytes (abfd, line_ptr);
10354 line_ptr += 2;
c764a876
DE
10355 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10356 line_ptr += offset_size;
debd256d
JB
10357 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10358 line_ptr += 1;
2dc7f7b3
TT
10359 if (lh->version >= 4)
10360 {
10361 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10362 line_ptr += 1;
10363 }
10364 else
10365 lh->maximum_ops_per_instruction = 1;
10366
10367 if (lh->maximum_ops_per_instruction == 0)
10368 {
10369 lh->maximum_ops_per_instruction = 1;
10370 complaint (&symfile_complaints,
3e43a32a
MS
10371 _("invalid maximum_ops_per_instruction "
10372 "in `.debug_line' section"));
2dc7f7b3
TT
10373 }
10374
debd256d
JB
10375 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10376 line_ptr += 1;
10377 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10378 line_ptr += 1;
10379 lh->line_range = read_1_byte (abfd, line_ptr);
10380 line_ptr += 1;
10381 lh->opcode_base = read_1_byte (abfd, line_ptr);
10382 line_ptr += 1;
10383 lh->standard_opcode_lengths
fe1b8b76 10384 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10385
10386 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10387 for (i = 1; i < lh->opcode_base; ++i)
10388 {
10389 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10390 line_ptr += 1;
10391 }
10392
a738430d 10393 /* Read directory table. */
9b1c24c8 10394 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10395 {
10396 line_ptr += bytes_read;
10397 add_include_dir (lh, cur_dir);
10398 }
10399 line_ptr += bytes_read;
10400
a738430d 10401 /* Read file name table. */
9b1c24c8 10402 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10403 {
10404 unsigned int dir_index, mod_time, length;
10405
10406 line_ptr += bytes_read;
10407 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10408 line_ptr += bytes_read;
10409 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10410 line_ptr += bytes_read;
10411 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10412 line_ptr += bytes_read;
10413
10414 add_file_name (lh, cur_file, dir_index, mod_time, length);
10415 }
10416 line_ptr += bytes_read;
6e70227d 10417 lh->statement_program_start = line_ptr;
debd256d 10418
dce234bc
PP
10419 if (line_ptr > (dwarf2_per_objfile->line.buffer
10420 + dwarf2_per_objfile->line.size))
4d3c2250 10421 complaint (&symfile_complaints,
3e43a32a
MS
10422 _("line number info header doesn't "
10423 "fit in `.debug_line' section"));
debd256d
JB
10424
10425 discard_cleanups (back_to);
10426 return lh;
10427}
c906108c 10428
5fb290d7
DJ
10429/* This function exists to work around a bug in certain compilers
10430 (particularly GCC 2.95), in which the first line number marker of a
10431 function does not show up until after the prologue, right before
10432 the second line number marker. This function shifts ADDRESS down
10433 to the beginning of the function if necessary, and is called on
10434 addresses passed to record_line. */
10435
10436static CORE_ADDR
e142c38c 10437check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10438{
10439 struct function_range *fn;
10440
10441 /* Find the function_range containing address. */
e142c38c 10442 if (!cu->first_fn)
5fb290d7
DJ
10443 return address;
10444
e142c38c
DJ
10445 if (!cu->cached_fn)
10446 cu->cached_fn = cu->first_fn;
5fb290d7 10447
e142c38c 10448 fn = cu->cached_fn;
5fb290d7
DJ
10449 while (fn)
10450 if (fn->lowpc <= address && fn->highpc > address)
10451 goto found;
10452 else
10453 fn = fn->next;
10454
e142c38c
DJ
10455 fn = cu->first_fn;
10456 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10457 if (fn->lowpc <= address && fn->highpc > address)
10458 goto found;
10459 else
10460 fn = fn->next;
10461
10462 return address;
10463
10464 found:
10465 if (fn->seen_line)
10466 return address;
10467 if (address != fn->lowpc)
4d3c2250 10468 complaint (&symfile_complaints,
e2e0b3e5 10469 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10470 (unsigned long) address, fn->name);
5fb290d7
DJ
10471 fn->seen_line = 1;
10472 return fn->lowpc;
10473}
10474
c6da4cef
DE
10475/* Subroutine of dwarf_decode_lines to simplify it.
10476 Return the file name of the psymtab for included file FILE_INDEX
10477 in line header LH of PST.
10478 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10479 If space for the result is malloc'd, it will be freed by a cleanup.
10480 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10481
10482static char *
10483psymtab_include_file_name (const struct line_header *lh, int file_index,
10484 const struct partial_symtab *pst,
10485 const char *comp_dir)
10486{
10487 const struct file_entry fe = lh->file_names [file_index];
10488 char *include_name = fe.name;
10489 char *include_name_to_compare = include_name;
10490 char *dir_name = NULL;
72b9f47f
TT
10491 const char *pst_filename;
10492 char *copied_name = NULL;
c6da4cef
DE
10493 int file_is_pst;
10494
10495 if (fe.dir_index)
10496 dir_name = lh->include_dirs[fe.dir_index - 1];
10497
10498 if (!IS_ABSOLUTE_PATH (include_name)
10499 && (dir_name != NULL || comp_dir != NULL))
10500 {
10501 /* Avoid creating a duplicate psymtab for PST.
10502 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10503 Before we do the comparison, however, we need to account
10504 for DIR_NAME and COMP_DIR.
10505 First prepend dir_name (if non-NULL). If we still don't
10506 have an absolute path prepend comp_dir (if non-NULL).
10507 However, the directory we record in the include-file's
10508 psymtab does not contain COMP_DIR (to match the
10509 corresponding symtab(s)).
10510
10511 Example:
10512
10513 bash$ cd /tmp
10514 bash$ gcc -g ./hello.c
10515 include_name = "hello.c"
10516 dir_name = "."
10517 DW_AT_comp_dir = comp_dir = "/tmp"
10518 DW_AT_name = "./hello.c" */
10519
10520 if (dir_name != NULL)
10521 {
10522 include_name = concat (dir_name, SLASH_STRING,
10523 include_name, (char *)NULL);
10524 include_name_to_compare = include_name;
10525 make_cleanup (xfree, include_name);
10526 }
10527 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10528 {
10529 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10530 include_name, (char *)NULL);
10531 }
10532 }
10533
10534 pst_filename = pst->filename;
10535 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10536 {
72b9f47f
TT
10537 copied_name = concat (pst->dirname, SLASH_STRING,
10538 pst_filename, (char *)NULL);
10539 pst_filename = copied_name;
c6da4cef
DE
10540 }
10541
1e3fad37 10542 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10543
10544 if (include_name_to_compare != include_name)
10545 xfree (include_name_to_compare);
72b9f47f
TT
10546 if (copied_name != NULL)
10547 xfree (copied_name);
c6da4cef
DE
10548
10549 if (file_is_pst)
10550 return NULL;
10551 return include_name;
10552}
10553
c91513d8
PP
10554/* Ignore this record_line request. */
10555
10556static void
10557noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10558{
10559 return;
10560}
10561
aaa75496
JB
10562/* Decode the Line Number Program (LNP) for the given line_header
10563 structure and CU. The actual information extracted and the type
10564 of structures created from the LNP depends on the value of PST.
10565
10566 1. If PST is NULL, then this procedure uses the data from the program
10567 to create all necessary symbol tables, and their linetables.
6e70227d 10568
aaa75496
JB
10569 2. If PST is not NULL, this procedure reads the program to determine
10570 the list of files included by the unit represented by PST, and
c6da4cef
DE
10571 builds all the associated partial symbol tables.
10572
10573 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10574 It is used for relative paths in the line table.
10575 NOTE: When processing partial symtabs (pst != NULL),
10576 comp_dir == pst->dirname.
10577
10578 NOTE: It is important that psymtabs have the same file name (via strcmp)
10579 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10580 symtab we don't use it in the name of the psymtabs we create.
10581 E.g. expand_line_sal requires this when finding psymtabs to expand.
10582 A good testcase for this is mb-inline.exp. */
debd256d 10583
c906108c 10584static void
72b9f47f 10585dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10586 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10587{
a8c50c1f 10588 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10589 gdb_byte *line_end;
a8c50c1f 10590 unsigned int bytes_read, extended_len;
c906108c 10591 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10592 CORE_ADDR baseaddr;
10593 struct objfile *objfile = cu->objfile;
fbf65064 10594 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10595 const int decode_for_pst_p = (pst != NULL);
cb1df416 10596 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
10597 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10598 = record_line;
e142c38c
DJ
10599
10600 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10601
debd256d
JB
10602 line_ptr = lh->statement_program_start;
10603 line_end = lh->statement_program_end;
c906108c
SS
10604
10605 /* Read the statement sequences until there's nothing left. */
10606 while (line_ptr < line_end)
10607 {
10608 /* state machine registers */
10609 CORE_ADDR address = 0;
10610 unsigned int file = 1;
10611 unsigned int line = 1;
10612 unsigned int column = 0;
debd256d 10613 int is_stmt = lh->default_is_stmt;
c906108c
SS
10614 int basic_block = 0;
10615 int end_sequence = 0;
fbf65064 10616 CORE_ADDR addr;
2dc7f7b3 10617 unsigned char op_index = 0;
c906108c 10618
aaa75496 10619 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10620 {
aaa75496 10621 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10622 /* lh->include_dirs and lh->file_names are 0-based, but the
10623 directory and file name numbers in the statement program
10624 are 1-based. */
10625 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10626 char *dir = NULL;
a738430d 10627
debd256d
JB
10628 if (fe->dir_index)
10629 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10630
10631 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10632 }
10633
a738430d 10634 /* Decode the table. */
c5aa993b 10635 while (!end_sequence)
c906108c
SS
10636 {
10637 op_code = read_1_byte (abfd, line_ptr);
10638 line_ptr += 1;
59205f5a
JB
10639 if (line_ptr > line_end)
10640 {
10641 dwarf2_debug_line_missing_end_sequence_complaint ();
10642 break;
10643 }
9aa1fe7e 10644
debd256d 10645 if (op_code >= lh->opcode_base)
6e70227d 10646 {
a738430d 10647 /* Special operand. */
debd256d 10648 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10649 address += (((op_index + (adj_opcode / lh->line_range))
10650 / lh->maximum_ops_per_instruction)
10651 * lh->minimum_instruction_length);
10652 op_index = ((op_index + (adj_opcode / lh->line_range))
10653 % lh->maximum_ops_per_instruction);
debd256d 10654 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10655 if (lh->num_file_names < file || file == 0)
25e43795 10656 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10657 /* For now we ignore lines not starting on an
10658 instruction boundary. */
10659 else if (op_index == 0)
25e43795
DJ
10660 {
10661 lh->file_names[file - 1].included_p = 1;
ca5f395d 10662 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10663 {
10664 if (last_subfile != current_subfile)
10665 {
10666 addr = gdbarch_addr_bits_remove (gdbarch, address);
10667 if (last_subfile)
c91513d8 10668 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10669 last_subfile = current_subfile;
10670 }
25e43795 10671 /* Append row to matrix using current values. */
fbf65064
UW
10672 addr = check_cu_functions (address, cu);
10673 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10674 (*p_record_line) (current_subfile, line, addr);
366da635 10675 }
25e43795 10676 }
ca5f395d 10677 basic_block = 0;
9aa1fe7e
GK
10678 }
10679 else switch (op_code)
c906108c
SS
10680 {
10681 case DW_LNS_extended_op:
3e43a32a
MS
10682 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10683 &bytes_read);
473b7be6 10684 line_ptr += bytes_read;
a8c50c1f 10685 extended_end = line_ptr + extended_len;
c906108c
SS
10686 extended_op = read_1_byte (abfd, line_ptr);
10687 line_ptr += 1;
10688 switch (extended_op)
10689 {
10690 case DW_LNE_end_sequence:
c91513d8 10691 p_record_line = record_line;
c906108c 10692 end_sequence = 1;
c906108c
SS
10693 break;
10694 case DW_LNE_set_address:
e7c27a73 10695 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
10696
10697 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10698 {
10699 /* This line table is for a function which has been
10700 GCd by the linker. Ignore it. PR gdb/12528 */
10701
10702 long line_offset
10703 = line_ptr - dwarf2_per_objfile->line.buffer;
10704
10705 complaint (&symfile_complaints,
10706 _(".debug_line address at offset 0x%lx is 0 "
10707 "[in module %s]"),
10708 line_offset, cu->objfile->name);
10709 p_record_line = noop_record_line;
10710 }
10711
2dc7f7b3 10712 op_index = 0;
107d2387
AC
10713 line_ptr += bytes_read;
10714 address += baseaddr;
c906108c
SS
10715 break;
10716 case DW_LNE_define_file:
debd256d
JB
10717 {
10718 char *cur_file;
10719 unsigned int dir_index, mod_time, length;
6e70227d 10720
3e43a32a
MS
10721 cur_file = read_direct_string (abfd, line_ptr,
10722 &bytes_read);
debd256d
JB
10723 line_ptr += bytes_read;
10724 dir_index =
10725 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10726 line_ptr += bytes_read;
10727 mod_time =
10728 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10729 line_ptr += bytes_read;
10730 length =
10731 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10732 line_ptr += bytes_read;
10733 add_file_name (lh, cur_file, dir_index, mod_time, length);
10734 }
c906108c 10735 break;
d0c6ba3d
CC
10736 case DW_LNE_set_discriminator:
10737 /* The discriminator is not interesting to the debugger;
10738 just ignore it. */
10739 line_ptr = extended_end;
10740 break;
c906108c 10741 default:
4d3c2250 10742 complaint (&symfile_complaints,
e2e0b3e5 10743 _("mangled .debug_line section"));
debd256d 10744 return;
c906108c 10745 }
a8c50c1f
DJ
10746 /* Make sure that we parsed the extended op correctly. If e.g.
10747 we expected a different address size than the producer used,
10748 we may have read the wrong number of bytes. */
10749 if (line_ptr != extended_end)
10750 {
10751 complaint (&symfile_complaints,
10752 _("mangled .debug_line section"));
10753 return;
10754 }
c906108c
SS
10755 break;
10756 case DW_LNS_copy:
59205f5a 10757 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10758 dwarf2_debug_line_missing_file_complaint ();
10759 else
366da635 10760 {
25e43795 10761 lh->file_names[file - 1].included_p = 1;
ca5f395d 10762 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10763 {
10764 if (last_subfile != current_subfile)
10765 {
10766 addr = gdbarch_addr_bits_remove (gdbarch, address);
10767 if (last_subfile)
c91513d8 10768 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10769 last_subfile = current_subfile;
10770 }
10771 addr = check_cu_functions (address, cu);
10772 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10773 (*p_record_line) (current_subfile, line, addr);
fbf65064 10774 }
366da635 10775 }
c906108c
SS
10776 basic_block = 0;
10777 break;
10778 case DW_LNS_advance_pc:
2dc7f7b3
TT
10779 {
10780 CORE_ADDR adjust
10781 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10782
10783 address += (((op_index + adjust)
10784 / lh->maximum_ops_per_instruction)
10785 * lh->minimum_instruction_length);
10786 op_index = ((op_index + adjust)
10787 % lh->maximum_ops_per_instruction);
10788 line_ptr += bytes_read;
10789 }
c906108c
SS
10790 break;
10791 case DW_LNS_advance_line:
10792 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10793 line_ptr += bytes_read;
10794 break;
10795 case DW_LNS_set_file:
debd256d 10796 {
a738430d
MK
10797 /* The arrays lh->include_dirs and lh->file_names are
10798 0-based, but the directory and file name numbers in
10799 the statement program are 1-based. */
debd256d 10800 struct file_entry *fe;
4f1520fb 10801 char *dir = NULL;
a738430d 10802
debd256d
JB
10803 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10804 line_ptr += bytes_read;
59205f5a 10805 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10806 dwarf2_debug_line_missing_file_complaint ();
10807 else
10808 {
10809 fe = &lh->file_names[file - 1];
10810 if (fe->dir_index)
10811 dir = lh->include_dirs[fe->dir_index - 1];
10812 if (!decode_for_pst_p)
10813 {
10814 last_subfile = current_subfile;
10815 dwarf2_start_subfile (fe->name, dir, comp_dir);
10816 }
10817 }
debd256d 10818 }
c906108c
SS
10819 break;
10820 case DW_LNS_set_column:
10821 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10822 line_ptr += bytes_read;
10823 break;
10824 case DW_LNS_negate_stmt:
10825 is_stmt = (!is_stmt);
10826 break;
10827 case DW_LNS_set_basic_block:
10828 basic_block = 1;
10829 break;
c2c6d25f
JM
10830 /* Add to the address register of the state machine the
10831 address increment value corresponding to special opcode
a738430d
MK
10832 255. I.e., this value is scaled by the minimum
10833 instruction length since special opcode 255 would have
b021a221 10834 scaled the increment. */
c906108c 10835 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10836 {
10837 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10838
10839 address += (((op_index + adjust)
10840 / lh->maximum_ops_per_instruction)
10841 * lh->minimum_instruction_length);
10842 op_index = ((op_index + adjust)
10843 % lh->maximum_ops_per_instruction);
10844 }
c906108c
SS
10845 break;
10846 case DW_LNS_fixed_advance_pc:
10847 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10848 op_index = 0;
c906108c
SS
10849 line_ptr += 2;
10850 break;
9aa1fe7e 10851 default:
a738430d
MK
10852 {
10853 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10854 int i;
a738430d 10855
debd256d 10856 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10857 {
10858 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10859 line_ptr += bytes_read;
10860 }
10861 }
c906108c
SS
10862 }
10863 }
59205f5a
JB
10864 if (lh->num_file_names < file || file == 0)
10865 dwarf2_debug_line_missing_file_complaint ();
10866 else
10867 {
10868 lh->file_names[file - 1].included_p = 1;
10869 if (!decode_for_pst_p)
fbf65064
UW
10870 {
10871 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 10872 (*p_record_line) (current_subfile, 0, addr);
fbf65064 10873 }
59205f5a 10874 }
c906108c 10875 }
aaa75496
JB
10876
10877 if (decode_for_pst_p)
10878 {
10879 int file_index;
10880
10881 /* Now that we're done scanning the Line Header Program, we can
10882 create the psymtab of each included file. */
10883 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10884 if (lh->file_names[file_index].included_p == 1)
10885 {
c6da4cef
DE
10886 char *include_name =
10887 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10888 if (include_name != NULL)
aaa75496
JB
10889 dwarf2_create_include_psymtab (include_name, pst, objfile);
10890 }
10891 }
cb1df416
DJ
10892 else
10893 {
10894 /* Make sure a symtab is created for every file, even files
10895 which contain only variables (i.e. no code with associated
10896 line numbers). */
10897
10898 int i;
10899 struct file_entry *fe;
10900
10901 for (i = 0; i < lh->num_file_names; i++)
10902 {
10903 char *dir = NULL;
9a619af0 10904
cb1df416
DJ
10905 fe = &lh->file_names[i];
10906 if (fe->dir_index)
10907 dir = lh->include_dirs[fe->dir_index - 1];
10908 dwarf2_start_subfile (fe->name, dir, comp_dir);
10909
10910 /* Skip the main file; we don't need it, and it must be
10911 allocated last, so that it will show up before the
10912 non-primary symtabs in the objfile's symtab list. */
10913 if (current_subfile == first_subfile)
10914 continue;
10915
10916 if (current_subfile->symtab == NULL)
10917 current_subfile->symtab = allocate_symtab (current_subfile->name,
10918 cu->objfile);
10919 fe->symtab = current_subfile->symtab;
10920 }
10921 }
c906108c
SS
10922}
10923
10924/* Start a subfile for DWARF. FILENAME is the name of the file and
10925 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10926 or NULL if not known. COMP_DIR is the compilation directory for the
10927 linetable's compilation unit or NULL if not known.
c906108c
SS
10928 This routine tries to keep line numbers from identical absolute and
10929 relative file names in a common subfile.
10930
10931 Using the `list' example from the GDB testsuite, which resides in
10932 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10933 of /srcdir/list0.c yields the following debugging information for list0.c:
10934
c5aa993b
JM
10935 DW_AT_name: /srcdir/list0.c
10936 DW_AT_comp_dir: /compdir
357e46e7 10937 files.files[0].name: list0.h
c5aa993b 10938 files.files[0].dir: /srcdir
357e46e7 10939 files.files[1].name: list0.c
c5aa993b 10940 files.files[1].dir: /srcdir
c906108c
SS
10941
10942 The line number information for list0.c has to end up in a single
4f1520fb
FR
10943 subfile, so that `break /srcdir/list0.c:1' works as expected.
10944 start_subfile will ensure that this happens provided that we pass the
10945 concatenation of files.files[1].dir and files.files[1].name as the
10946 subfile's name. */
c906108c
SS
10947
10948static void
3e43a32a
MS
10949dwarf2_start_subfile (char *filename, const char *dirname,
10950 const char *comp_dir)
c906108c 10951{
4f1520fb
FR
10952 char *fullname;
10953
10954 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10955 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10956 second argument to start_subfile. To be consistent, we do the
10957 same here. In order not to lose the line information directory,
10958 we concatenate it to the filename when it makes sense.
10959 Note that the Dwarf3 standard says (speaking of filenames in line
10960 information): ``The directory index is ignored for file names
10961 that represent full path names''. Thus ignoring dirname in the
10962 `else' branch below isn't an issue. */
c906108c 10963
d5166ae1 10964 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10965 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10966 else
10967 fullname = filename;
c906108c 10968
4f1520fb
FR
10969 start_subfile (fullname, comp_dir);
10970
10971 if (fullname != filename)
10972 xfree (fullname);
c906108c
SS
10973}
10974
4c2df51b
DJ
10975static void
10976var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10977 struct dwarf2_cu *cu)
4c2df51b 10978{
e7c27a73
DJ
10979 struct objfile *objfile = cu->objfile;
10980 struct comp_unit_head *cu_header = &cu->header;
10981
4c2df51b
DJ
10982 /* NOTE drow/2003-01-30: There used to be a comment and some special
10983 code here to turn a symbol with DW_AT_external and a
10984 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10985 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10986 with some versions of binutils) where shared libraries could have
10987 relocations against symbols in their debug information - the
10988 minimal symbol would have the right address, but the debug info
10989 would not. It's no longer necessary, because we will explicitly
10990 apply relocations when we read in the debug information now. */
10991
10992 /* A DW_AT_location attribute with no contents indicates that a
10993 variable has been optimized away. */
10994 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10995 {
10996 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10997 return;
10998 }
10999
11000 /* Handle one degenerate form of location expression specially, to
11001 preserve GDB's previous behavior when section offsets are
11002 specified. If this is just a DW_OP_addr then mark this symbol
11003 as LOC_STATIC. */
11004
11005 if (attr_form_is_block (attr)
11006 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11007 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11008 {
891d2f0b 11009 unsigned int dummy;
4c2df51b
DJ
11010
11011 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11012 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11013 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11014 fixup_symbol_section (sym, objfile);
11015 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11016 SYMBOL_SECTION (sym));
4c2df51b
DJ
11017 return;
11018 }
11019
11020 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11021 expression evaluator, and use LOC_COMPUTED only when necessary
11022 (i.e. when the value of a register or memory location is
11023 referenced, or a thread-local block, etc.). Then again, it might
11024 not be worthwhile. I'm assuming that it isn't unless performance
11025 or memory numbers show me otherwise. */
11026
e7c27a73 11027 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11028 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11029
11030 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11031 cu->has_loclist = 1;
4c2df51b
DJ
11032}
11033
c906108c
SS
11034/* Given a pointer to a DWARF information entry, figure out if we need
11035 to make a symbol table entry for it, and if so, create a new entry
11036 and return a pointer to it.
11037 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11038 used the passed type.
11039 If SPACE is not NULL, use it to hold the new symbol. If it is
11040 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11041
11042static struct symbol *
34eaf542
TT
11043new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11044 struct symbol *space)
c906108c 11045{
e7c27a73 11046 struct objfile *objfile = cu->objfile;
c906108c
SS
11047 struct symbol *sym = NULL;
11048 char *name;
11049 struct attribute *attr = NULL;
11050 struct attribute *attr2 = NULL;
e142c38c 11051 CORE_ADDR baseaddr;
e37fd15a
SW
11052 struct pending **list_to_add = NULL;
11053
edb3359d 11054 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11055
11056 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11057
94af9270 11058 name = dwarf2_name (die, cu);
c906108c
SS
11059 if (name)
11060 {
94af9270 11061 const char *linkagename;
34eaf542 11062 int suppress_add = 0;
94af9270 11063
34eaf542
TT
11064 if (space)
11065 sym = space;
11066 else
11067 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11068 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11069
11070 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11071 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11072 linkagename = dwarf2_physname (name, die, cu);
11073 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11074
f55ee35c
JK
11075 /* Fortran does not have mangling standard and the mangling does differ
11076 between gfortran, iFort etc. */
11077 if (cu->language == language_fortran
b250c185 11078 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11079 symbol_set_demangled_name (&(sym->ginfo),
11080 (char *) dwarf2_full_name (name, die, cu),
11081 NULL);
f55ee35c 11082
c906108c 11083 /* Default assumptions.
c5aa993b 11084 Use the passed type or decode it from the die. */
176620f1 11085 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11086 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11087 if (type != NULL)
11088 SYMBOL_TYPE (sym) = type;
11089 else
e7c27a73 11090 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11091 attr = dwarf2_attr (die,
11092 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11093 cu);
c906108c
SS
11094 if (attr)
11095 {
11096 SYMBOL_LINE (sym) = DW_UNSND (attr);
11097 }
cb1df416 11098
edb3359d
DJ
11099 attr = dwarf2_attr (die,
11100 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11101 cu);
cb1df416
DJ
11102 if (attr)
11103 {
11104 int file_index = DW_UNSND (attr);
9a619af0 11105
cb1df416
DJ
11106 if (cu->line_header == NULL
11107 || file_index > cu->line_header->num_file_names)
11108 complaint (&symfile_complaints,
11109 _("file index out of range"));
1c3d648d 11110 else if (file_index > 0)
cb1df416
DJ
11111 {
11112 struct file_entry *fe;
9a619af0 11113
cb1df416
DJ
11114 fe = &cu->line_header->file_names[file_index - 1];
11115 SYMBOL_SYMTAB (sym) = fe->symtab;
11116 }
11117 }
11118
c906108c
SS
11119 switch (die->tag)
11120 {
11121 case DW_TAG_label:
e142c38c 11122 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11123 if (attr)
11124 {
11125 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11126 }
0f5238ed
TT
11127 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11128 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11129 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11130 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11131 break;
11132 case DW_TAG_subprogram:
11133 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11134 finish_block. */
11135 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11136 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11137 if ((attr2 && (DW_UNSND (attr2) != 0))
11138 || cu->language == language_ada)
c906108c 11139 {
2cfa0c8d
JB
11140 /* Subprograms marked external are stored as a global symbol.
11141 Ada subprograms, whether marked external or not, are always
11142 stored as a global symbol, because we want to be able to
11143 access them globally. For instance, we want to be able
11144 to break on a nested subprogram without having to
11145 specify the context. */
e37fd15a 11146 list_to_add = &global_symbols;
c906108c
SS
11147 }
11148 else
11149 {
e37fd15a 11150 list_to_add = cu->list_in_scope;
c906108c
SS
11151 }
11152 break;
edb3359d
DJ
11153 case DW_TAG_inlined_subroutine:
11154 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11155 finish_block. */
11156 SYMBOL_CLASS (sym) = LOC_BLOCK;
11157 SYMBOL_INLINED (sym) = 1;
11158 /* Do not add the symbol to any lists. It will be found via
11159 BLOCK_FUNCTION from the blockvector. */
11160 break;
34eaf542
TT
11161 case DW_TAG_template_value_param:
11162 suppress_add = 1;
11163 /* Fall through. */
72929c62 11164 case DW_TAG_constant:
c906108c 11165 case DW_TAG_variable:
254e6b9e 11166 case DW_TAG_member:
0963b4bd
MS
11167 /* Compilation with minimal debug info may result in
11168 variables with missing type entries. Change the
11169 misleading `void' type to something sensible. */
c906108c 11170 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11171 SYMBOL_TYPE (sym)
46bf5051 11172 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11173
e142c38c 11174 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11175 /* In the case of DW_TAG_member, we should only be called for
11176 static const members. */
11177 if (die->tag == DW_TAG_member)
11178 {
3863f96c
DE
11179 /* dwarf2_add_field uses die_is_declaration,
11180 so we do the same. */
254e6b9e
DE
11181 gdb_assert (die_is_declaration (die, cu));
11182 gdb_assert (attr);
11183 }
c906108c
SS
11184 if (attr)
11185 {
e7c27a73 11186 dwarf2_const_value (attr, sym, cu);
e142c38c 11187 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11188 if (!suppress_add)
34eaf542
TT
11189 {
11190 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11191 list_to_add = &global_symbols;
34eaf542 11192 else
e37fd15a 11193 list_to_add = cu->list_in_scope;
34eaf542 11194 }
c906108c
SS
11195 break;
11196 }
e142c38c 11197 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11198 if (attr)
11199 {
e7c27a73 11200 var_decode_location (attr, sym, cu);
e142c38c 11201 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11202 if (SYMBOL_CLASS (sym) == LOC_STATIC
11203 && SYMBOL_VALUE_ADDRESS (sym) == 0
11204 && !dwarf2_per_objfile->has_section_at_zero)
11205 {
11206 /* When a static variable is eliminated by the linker,
11207 the corresponding debug information is not stripped
11208 out, but the variable address is set to null;
11209 do not add such variables into symbol table. */
11210 }
11211 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11212 {
f55ee35c
JK
11213 /* Workaround gfortran PR debug/40040 - it uses
11214 DW_AT_location for variables in -fPIC libraries which may
11215 get overriden by other libraries/executable and get
11216 a different address. Resolve it by the minimal symbol
11217 which may come from inferior's executable using copy
11218 relocation. Make this workaround only for gfortran as for
11219 other compilers GDB cannot guess the minimal symbol
11220 Fortran mangling kind. */
11221 if (cu->language == language_fortran && die->parent
11222 && die->parent->tag == DW_TAG_module
11223 && cu->producer
11224 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11225 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11226
1c809c68
TT
11227 /* A variable with DW_AT_external is never static,
11228 but it may be block-scoped. */
11229 list_to_add = (cu->list_in_scope == &file_symbols
11230 ? &global_symbols : cu->list_in_scope);
1c809c68 11231 }
c906108c 11232 else
e37fd15a 11233 list_to_add = cu->list_in_scope;
c906108c
SS
11234 }
11235 else
11236 {
11237 /* We do not know the address of this symbol.
c5aa993b
JM
11238 If it is an external symbol and we have type information
11239 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11240 The address of the variable will then be determined from
11241 the minimal symbol table whenever the variable is
11242 referenced. */
e142c38c 11243 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11244 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11245 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11246 {
0fe7935b
DJ
11247 /* A variable with DW_AT_external is never static, but it
11248 may be block-scoped. */
11249 list_to_add = (cu->list_in_scope == &file_symbols
11250 ? &global_symbols : cu->list_in_scope);
11251
c906108c 11252 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11253 }
442ddf59
JK
11254 else if (!die_is_declaration (die, cu))
11255 {
11256 /* Use the default LOC_OPTIMIZED_OUT class. */
11257 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11258 if (!suppress_add)
11259 list_to_add = cu->list_in_scope;
442ddf59 11260 }
c906108c
SS
11261 }
11262 break;
11263 case DW_TAG_formal_parameter:
edb3359d
DJ
11264 /* If we are inside a function, mark this as an argument. If
11265 not, we might be looking at an argument to an inlined function
11266 when we do not have enough information to show inlined frames;
11267 pretend it's a local variable in that case so that the user can
11268 still see it. */
11269 if (context_stack_depth > 0
11270 && context_stack[context_stack_depth - 1].name != NULL)
11271 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11272 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11273 if (attr)
11274 {
e7c27a73 11275 var_decode_location (attr, sym, cu);
c906108c 11276 }
e142c38c 11277 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11278 if (attr)
11279 {
e7c27a73 11280 dwarf2_const_value (attr, sym, cu);
c906108c 11281 }
f346a30d
PM
11282 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11283 if (attr && DW_UNSND (attr))
11284 {
11285 struct type *ref_type;
11286
11287 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11288 SYMBOL_TYPE (sym) = ref_type;
11289 }
11290
e37fd15a 11291 list_to_add = cu->list_in_scope;
c906108c
SS
11292 break;
11293 case DW_TAG_unspecified_parameters:
11294 /* From varargs functions; gdb doesn't seem to have any
11295 interest in this information, so just ignore it for now.
11296 (FIXME?) */
11297 break;
34eaf542
TT
11298 case DW_TAG_template_type_param:
11299 suppress_add = 1;
11300 /* Fall through. */
c906108c 11301 case DW_TAG_class_type:
680b30c7 11302 case DW_TAG_interface_type:
c906108c
SS
11303 case DW_TAG_structure_type:
11304 case DW_TAG_union_type:
72019c9c 11305 case DW_TAG_set_type:
c906108c
SS
11306 case DW_TAG_enumeration_type:
11307 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11308 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11309
63d06c5c 11310 {
987504bb 11311 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11312 really ever be static objects: otherwise, if you try
11313 to, say, break of a class's method and you're in a file
11314 which doesn't mention that class, it won't work unless
11315 the check for all static symbols in lookup_symbol_aux
11316 saves you. See the OtherFileClass tests in
11317 gdb.c++/namespace.exp. */
11318
e37fd15a 11319 if (!suppress_add)
34eaf542 11320 {
34eaf542
TT
11321 list_to_add = (cu->list_in_scope == &file_symbols
11322 && (cu->language == language_cplus
11323 || cu->language == language_java)
11324 ? &global_symbols : cu->list_in_scope);
63d06c5c 11325
64382290
TT
11326 /* The semantics of C++ state that "struct foo {
11327 ... }" also defines a typedef for "foo". A Java
11328 class declaration also defines a typedef for the
11329 class. */
11330 if (cu->language == language_cplus
11331 || cu->language == language_java
11332 || cu->language == language_ada)
11333 {
11334 /* The symbol's name is already allocated along
11335 with this objfile, so we don't need to
11336 duplicate it for the type. */
11337 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11338 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11339 }
63d06c5c
DC
11340 }
11341 }
c906108c
SS
11342 break;
11343 case DW_TAG_typedef:
63d06c5c
DC
11344 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11345 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11346 list_to_add = cu->list_in_scope;
63d06c5c 11347 break;
c906108c 11348 case DW_TAG_base_type:
a02abb62 11349 case DW_TAG_subrange_type:
c906108c 11350 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11351 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11352 list_to_add = cu->list_in_scope;
c906108c
SS
11353 break;
11354 case DW_TAG_enumerator:
e142c38c 11355 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11356 if (attr)
11357 {
e7c27a73 11358 dwarf2_const_value (attr, sym, cu);
c906108c 11359 }
63d06c5c
DC
11360 {
11361 /* NOTE: carlton/2003-11-10: See comment above in the
11362 DW_TAG_class_type, etc. block. */
11363
e142c38c 11364 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11365 && (cu->language == language_cplus
11366 || cu->language == language_java)
e142c38c 11367 ? &global_symbols : cu->list_in_scope);
63d06c5c 11368 }
c906108c 11369 break;
5c4e30ca
DC
11370 case DW_TAG_namespace:
11371 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11372 list_to_add = &global_symbols;
5c4e30ca 11373 break;
c906108c
SS
11374 default:
11375 /* Not a tag we recognize. Hopefully we aren't processing
11376 trash data, but since we must specifically ignore things
11377 we don't recognize, there is nothing else we should do at
0963b4bd 11378 this point. */
e2e0b3e5 11379 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11380 dwarf_tag_name (die->tag));
c906108c
SS
11381 break;
11382 }
df8a16a1 11383
e37fd15a
SW
11384 if (suppress_add)
11385 {
11386 sym->hash_next = objfile->template_symbols;
11387 objfile->template_symbols = sym;
11388 list_to_add = NULL;
11389 }
11390
11391 if (list_to_add != NULL)
11392 add_symbol_to_list (sym, list_to_add);
11393
df8a16a1
DJ
11394 /* For the benefit of old versions of GCC, check for anonymous
11395 namespaces based on the demangled name. */
11396 if (!processing_has_namespace_info
94af9270 11397 && cu->language == language_cplus)
df8a16a1 11398 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
11399 }
11400 return (sym);
11401}
11402
34eaf542
TT
11403/* A wrapper for new_symbol_full that always allocates a new symbol. */
11404
11405static struct symbol *
11406new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11407{
11408 return new_symbol_full (die, type, cu, NULL);
11409}
11410
98bfdba5
PA
11411/* Given an attr with a DW_FORM_dataN value in host byte order,
11412 zero-extend it as appropriate for the symbol's type. The DWARF
11413 standard (v4) is not entirely clear about the meaning of using
11414 DW_FORM_dataN for a constant with a signed type, where the type is
11415 wider than the data. The conclusion of a discussion on the DWARF
11416 list was that this is unspecified. We choose to always zero-extend
11417 because that is the interpretation long in use by GCC. */
c906108c 11418
98bfdba5
PA
11419static gdb_byte *
11420dwarf2_const_value_data (struct attribute *attr, struct type *type,
11421 const char *name, struct obstack *obstack,
11422 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11423{
e7c27a73 11424 struct objfile *objfile = cu->objfile;
e17a4113
UW
11425 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11426 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11427 LONGEST l = DW_UNSND (attr);
11428
11429 if (bits < sizeof (*value) * 8)
11430 {
11431 l &= ((LONGEST) 1 << bits) - 1;
11432 *value = l;
11433 }
11434 else if (bits == sizeof (*value) * 8)
11435 *value = l;
11436 else
11437 {
11438 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11439 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11440 return bytes;
11441 }
11442
11443 return NULL;
11444}
11445
11446/* Read a constant value from an attribute. Either set *VALUE, or if
11447 the value does not fit in *VALUE, set *BYTES - either already
11448 allocated on the objfile obstack, or newly allocated on OBSTACK,
11449 or, set *BATON, if we translated the constant to a location
11450 expression. */
11451
11452static void
11453dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11454 const char *name, struct obstack *obstack,
11455 struct dwarf2_cu *cu,
11456 long *value, gdb_byte **bytes,
11457 struct dwarf2_locexpr_baton **baton)
11458{
11459 struct objfile *objfile = cu->objfile;
11460 struct comp_unit_head *cu_header = &cu->header;
c906108c 11461 struct dwarf_block *blk;
98bfdba5
PA
11462 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11463 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11464
11465 *value = 0;
11466 *bytes = NULL;
11467 *baton = NULL;
c906108c
SS
11468
11469 switch (attr->form)
11470 {
11471 case DW_FORM_addr:
ac56253d 11472 {
ac56253d
TT
11473 gdb_byte *data;
11474
98bfdba5
PA
11475 if (TYPE_LENGTH (type) != cu_header->addr_size)
11476 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11477 cu_header->addr_size,
98bfdba5 11478 TYPE_LENGTH (type));
ac56253d
TT
11479 /* Symbols of this form are reasonably rare, so we just
11480 piggyback on the existing location code rather than writing
11481 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11482 *baton = obstack_alloc (&objfile->objfile_obstack,
11483 sizeof (struct dwarf2_locexpr_baton));
11484 (*baton)->per_cu = cu->per_cu;
11485 gdb_assert ((*baton)->per_cu);
ac56253d 11486
98bfdba5
PA
11487 (*baton)->size = 2 + cu_header->addr_size;
11488 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11489 (*baton)->data = data;
ac56253d
TT
11490
11491 data[0] = DW_OP_addr;
11492 store_unsigned_integer (&data[1], cu_header->addr_size,
11493 byte_order, DW_ADDR (attr));
11494 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11495 }
c906108c 11496 break;
4ac36638 11497 case DW_FORM_string:
93b5768b 11498 case DW_FORM_strp:
98bfdba5
PA
11499 /* DW_STRING is already allocated on the objfile obstack, point
11500 directly to it. */
11501 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11502 break;
c906108c
SS
11503 case DW_FORM_block1:
11504 case DW_FORM_block2:
11505 case DW_FORM_block4:
11506 case DW_FORM_block:
2dc7f7b3 11507 case DW_FORM_exprloc:
c906108c 11508 blk = DW_BLOCK (attr);
98bfdba5
PA
11509 if (TYPE_LENGTH (type) != blk->size)
11510 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11511 TYPE_LENGTH (type));
11512 *bytes = blk->data;
c906108c 11513 break;
2df3850c
JM
11514
11515 /* The DW_AT_const_value attributes are supposed to carry the
11516 symbol's value "represented as it would be on the target
11517 architecture." By the time we get here, it's already been
11518 converted to host endianness, so we just need to sign- or
11519 zero-extend it as appropriate. */
11520 case DW_FORM_data1:
3e43a32a
MS
11521 *bytes = dwarf2_const_value_data (attr, type, name,
11522 obstack, cu, value, 8);
2df3850c 11523 break;
c906108c 11524 case DW_FORM_data2:
3e43a32a
MS
11525 *bytes = dwarf2_const_value_data (attr, type, name,
11526 obstack, cu, value, 16);
2df3850c 11527 break;
c906108c 11528 case DW_FORM_data4:
3e43a32a
MS
11529 *bytes = dwarf2_const_value_data (attr, type, name,
11530 obstack, cu, value, 32);
2df3850c 11531 break;
c906108c 11532 case DW_FORM_data8:
3e43a32a
MS
11533 *bytes = dwarf2_const_value_data (attr, type, name,
11534 obstack, cu, value, 64);
2df3850c
JM
11535 break;
11536
c906108c 11537 case DW_FORM_sdata:
98bfdba5 11538 *value = DW_SND (attr);
2df3850c
JM
11539 break;
11540
c906108c 11541 case DW_FORM_udata:
98bfdba5 11542 *value = DW_UNSND (attr);
c906108c 11543 break;
2df3850c 11544
c906108c 11545 default:
4d3c2250 11546 complaint (&symfile_complaints,
e2e0b3e5 11547 _("unsupported const value attribute form: '%s'"),
4d3c2250 11548 dwarf_form_name (attr->form));
98bfdba5 11549 *value = 0;
c906108c
SS
11550 break;
11551 }
11552}
11553
2df3850c 11554
98bfdba5
PA
11555/* Copy constant value from an attribute to a symbol. */
11556
2df3850c 11557static void
98bfdba5
PA
11558dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11559 struct dwarf2_cu *cu)
2df3850c 11560{
98bfdba5
PA
11561 struct objfile *objfile = cu->objfile;
11562 struct comp_unit_head *cu_header = &cu->header;
11563 long value;
11564 gdb_byte *bytes;
11565 struct dwarf2_locexpr_baton *baton;
2df3850c 11566
98bfdba5
PA
11567 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11568 SYMBOL_PRINT_NAME (sym),
11569 &objfile->objfile_obstack, cu,
11570 &value, &bytes, &baton);
2df3850c 11571
98bfdba5
PA
11572 if (baton != NULL)
11573 {
11574 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11575 SYMBOL_LOCATION_BATON (sym) = baton;
11576 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11577 }
11578 else if (bytes != NULL)
11579 {
11580 SYMBOL_VALUE_BYTES (sym) = bytes;
11581 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11582 }
11583 else
11584 {
11585 SYMBOL_VALUE (sym) = value;
11586 SYMBOL_CLASS (sym) = LOC_CONST;
11587 }
2df3850c
JM
11588}
11589
c906108c
SS
11590/* Return the type of the die in question using its DW_AT_type attribute. */
11591
11592static struct type *
e7c27a73 11593die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11594{
c906108c 11595 struct attribute *type_attr;
c906108c 11596
e142c38c 11597 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11598 if (!type_attr)
11599 {
11600 /* A missing DW_AT_type represents a void type. */
46bf5051 11601 return objfile_type (cu->objfile)->builtin_void;
c906108c 11602 }
348e048f 11603
673bfd45 11604 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11605}
11606
b4ba55a1
JB
11607/* True iff CU's producer generates GNAT Ada auxiliary information
11608 that allows to find parallel types through that information instead
11609 of having to do expensive parallel lookups by type name. */
11610
11611static int
11612need_gnat_info (struct dwarf2_cu *cu)
11613{
11614 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11615 of GNAT produces this auxiliary information, without any indication
11616 that it is produced. Part of enhancing the FSF version of GNAT
11617 to produce that information will be to put in place an indicator
11618 that we can use in order to determine whether the descriptive type
11619 info is available or not. One suggestion that has been made is
11620 to use a new attribute, attached to the CU die. For now, assume
11621 that the descriptive type info is not available. */
11622 return 0;
11623}
11624
b4ba55a1
JB
11625/* Return the auxiliary type of the die in question using its
11626 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11627 attribute is not present. */
11628
11629static struct type *
11630die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11631{
b4ba55a1 11632 struct attribute *type_attr;
b4ba55a1
JB
11633
11634 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11635 if (!type_attr)
11636 return NULL;
11637
673bfd45 11638 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11639}
11640
11641/* If DIE has a descriptive_type attribute, then set the TYPE's
11642 descriptive type accordingly. */
11643
11644static void
11645set_descriptive_type (struct type *type, struct die_info *die,
11646 struct dwarf2_cu *cu)
11647{
11648 struct type *descriptive_type = die_descriptive_type (die, cu);
11649
11650 if (descriptive_type)
11651 {
11652 ALLOCATE_GNAT_AUX_TYPE (type);
11653 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11654 }
11655}
11656
c906108c
SS
11657/* Return the containing type of the die in question using its
11658 DW_AT_containing_type attribute. */
11659
11660static struct type *
e7c27a73 11661die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11662{
c906108c 11663 struct attribute *type_attr;
c906108c 11664
e142c38c 11665 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11666 if (!type_attr)
11667 error (_("Dwarf Error: Problem turning containing type into gdb type "
11668 "[in module %s]"), cu->objfile->name);
11669
673bfd45 11670 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11671}
11672
673bfd45
DE
11673/* Look up the type of DIE in CU using its type attribute ATTR.
11674 If there is no type substitute an error marker. */
11675
c906108c 11676static struct type *
673bfd45
DE
11677lookup_die_type (struct die_info *die, struct attribute *attr,
11678 struct dwarf2_cu *cu)
c906108c 11679{
f792889a
DJ
11680 struct type *this_type;
11681
673bfd45
DE
11682 /* First see if we have it cached. */
11683
11684 if (is_ref_attr (attr))
11685 {
11686 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11687
11688 this_type = get_die_type_at_offset (offset, cu->per_cu);
11689 }
55f1336d 11690 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
11691 {
11692 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11693 struct dwarf2_cu *sig_cu;
11694 unsigned int offset;
11695
11696 /* sig_type will be NULL if the signatured type is missing from
11697 the debug info. */
11698 if (sig_type == NULL)
11699 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11700 "at 0x%x [in module %s]"),
11701 die->offset, cu->objfile->name);
11702
11703 gdb_assert (sig_type->per_cu.from_debug_types);
11704 offset = sig_type->offset + sig_type->type_offset;
11705 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11706 }
11707 else
11708 {
11709 dump_die_for_error (die);
11710 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11711 dwarf_attr_name (attr->name), cu->objfile->name);
11712 }
11713
11714 /* If not cached we need to read it in. */
11715
11716 if (this_type == NULL)
11717 {
11718 struct die_info *type_die;
11719 struct dwarf2_cu *type_cu = cu;
11720
11721 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11722 /* If the type is cached, we should have found it above. */
11723 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11724 this_type = read_type_die_1 (type_die, type_cu);
11725 }
11726
11727 /* If we still don't have a type use an error marker. */
11728
11729 if (this_type == NULL)
c906108c 11730 {
b00fdb78
TT
11731 char *message, *saved;
11732
11733 /* read_type_die already issued a complaint. */
11734 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11735 cu->objfile->name,
11736 cu->header.offset,
11737 die->offset);
11738 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11739 message, strlen (message));
11740 xfree (message);
11741
11742 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11743 }
673bfd45 11744
f792889a 11745 return this_type;
c906108c
SS
11746}
11747
673bfd45
DE
11748/* Return the type in DIE, CU.
11749 Returns NULL for invalid types.
11750
11751 This first does a lookup in the appropriate type_hash table,
11752 and only reads the die in if necessary.
11753
11754 NOTE: This can be called when reading in partial or full symbols. */
11755
f792889a 11756static struct type *
e7c27a73 11757read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11758{
f792889a
DJ
11759 struct type *this_type;
11760
11761 this_type = get_die_type (die, cu);
11762 if (this_type)
11763 return this_type;
11764
673bfd45
DE
11765 return read_type_die_1 (die, cu);
11766}
11767
11768/* Read the type in DIE, CU.
11769 Returns NULL for invalid types. */
11770
11771static struct type *
11772read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11773{
11774 struct type *this_type = NULL;
11775
c906108c
SS
11776 switch (die->tag)
11777 {
11778 case DW_TAG_class_type:
680b30c7 11779 case DW_TAG_interface_type:
c906108c
SS
11780 case DW_TAG_structure_type:
11781 case DW_TAG_union_type:
f792889a 11782 this_type = read_structure_type (die, cu);
c906108c
SS
11783 break;
11784 case DW_TAG_enumeration_type:
f792889a 11785 this_type = read_enumeration_type (die, cu);
c906108c
SS
11786 break;
11787 case DW_TAG_subprogram:
11788 case DW_TAG_subroutine_type:
edb3359d 11789 case DW_TAG_inlined_subroutine:
f792889a 11790 this_type = read_subroutine_type (die, cu);
c906108c
SS
11791 break;
11792 case DW_TAG_array_type:
f792889a 11793 this_type = read_array_type (die, cu);
c906108c 11794 break;
72019c9c 11795 case DW_TAG_set_type:
f792889a 11796 this_type = read_set_type (die, cu);
72019c9c 11797 break;
c906108c 11798 case DW_TAG_pointer_type:
f792889a 11799 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11800 break;
11801 case DW_TAG_ptr_to_member_type:
f792889a 11802 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11803 break;
11804 case DW_TAG_reference_type:
f792889a 11805 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11806 break;
11807 case DW_TAG_const_type:
f792889a 11808 this_type = read_tag_const_type (die, cu);
c906108c
SS
11809 break;
11810 case DW_TAG_volatile_type:
f792889a 11811 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11812 break;
11813 case DW_TAG_string_type:
f792889a 11814 this_type = read_tag_string_type (die, cu);
c906108c
SS
11815 break;
11816 case DW_TAG_typedef:
f792889a 11817 this_type = read_typedef (die, cu);
c906108c 11818 break;
a02abb62 11819 case DW_TAG_subrange_type:
f792889a 11820 this_type = read_subrange_type (die, cu);
a02abb62 11821 break;
c906108c 11822 case DW_TAG_base_type:
f792889a 11823 this_type = read_base_type (die, cu);
c906108c 11824 break;
81a17f79 11825 case DW_TAG_unspecified_type:
f792889a 11826 this_type = read_unspecified_type (die, cu);
81a17f79 11827 break;
0114d602
DJ
11828 case DW_TAG_namespace:
11829 this_type = read_namespace_type (die, cu);
11830 break;
f55ee35c
JK
11831 case DW_TAG_module:
11832 this_type = read_module_type (die, cu);
11833 break;
c906108c 11834 default:
3e43a32a
MS
11835 complaint (&symfile_complaints,
11836 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11837 dwarf_tag_name (die->tag));
c906108c
SS
11838 break;
11839 }
63d06c5c 11840
f792889a 11841 return this_type;
63d06c5c
DC
11842}
11843
abc72ce4
DE
11844/* See if we can figure out if the class lives in a namespace. We do
11845 this by looking for a member function; its demangled name will
11846 contain namespace info, if there is any.
11847 Return the computed name or NULL.
11848 Space for the result is allocated on the objfile's obstack.
11849 This is the full-die version of guess_partial_die_structure_name.
11850 In this case we know DIE has no useful parent. */
11851
11852static char *
11853guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11854{
11855 struct die_info *spec_die;
11856 struct dwarf2_cu *spec_cu;
11857 struct die_info *child;
11858
11859 spec_cu = cu;
11860 spec_die = die_specification (die, &spec_cu);
11861 if (spec_die != NULL)
11862 {
11863 die = spec_die;
11864 cu = spec_cu;
11865 }
11866
11867 for (child = die->child;
11868 child != NULL;
11869 child = child->sibling)
11870 {
11871 if (child->tag == DW_TAG_subprogram)
11872 {
11873 struct attribute *attr;
11874
11875 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11876 if (attr == NULL)
11877 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11878 if (attr != NULL)
11879 {
11880 char *actual_name
11881 = language_class_name_from_physname (cu->language_defn,
11882 DW_STRING (attr));
11883 char *name = NULL;
11884
11885 if (actual_name != NULL)
11886 {
11887 char *die_name = dwarf2_name (die, cu);
11888
11889 if (die_name != NULL
11890 && strcmp (die_name, actual_name) != 0)
11891 {
11892 /* Strip off the class name from the full name.
11893 We want the prefix. */
11894 int die_name_len = strlen (die_name);
11895 int actual_name_len = strlen (actual_name);
11896
11897 /* Test for '::' as a sanity check. */
11898 if (actual_name_len > die_name_len + 2
3e43a32a
MS
11899 && actual_name[actual_name_len
11900 - die_name_len - 1] == ':')
abc72ce4
DE
11901 name =
11902 obsavestring (actual_name,
11903 actual_name_len - die_name_len - 2,
11904 &cu->objfile->objfile_obstack);
11905 }
11906 }
11907 xfree (actual_name);
11908 return name;
11909 }
11910 }
11911 }
11912
11913 return NULL;
11914}
11915
fdde2d81 11916/* Return the name of the namespace/class that DIE is defined within,
0114d602 11917 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11918
0114d602
DJ
11919 For example, if we're within the method foo() in the following
11920 code:
11921
11922 namespace N {
11923 class C {
11924 void foo () {
11925 }
11926 };
11927 }
11928
11929 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11930
11931static char *
e142c38c 11932determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11933{
0114d602
DJ
11934 struct die_info *parent, *spec_die;
11935 struct dwarf2_cu *spec_cu;
11936 struct type *parent_type;
63d06c5c 11937
f55ee35c
JK
11938 if (cu->language != language_cplus && cu->language != language_java
11939 && cu->language != language_fortran)
0114d602
DJ
11940 return "";
11941
11942 /* We have to be careful in the presence of DW_AT_specification.
11943 For example, with GCC 3.4, given the code
11944
11945 namespace N {
11946 void foo() {
11947 // Definition of N::foo.
11948 }
11949 }
11950
11951 then we'll have a tree of DIEs like this:
11952
11953 1: DW_TAG_compile_unit
11954 2: DW_TAG_namespace // N
11955 3: DW_TAG_subprogram // declaration of N::foo
11956 4: DW_TAG_subprogram // definition of N::foo
11957 DW_AT_specification // refers to die #3
11958
11959 Thus, when processing die #4, we have to pretend that we're in
11960 the context of its DW_AT_specification, namely the contex of die
11961 #3. */
11962 spec_cu = cu;
11963 spec_die = die_specification (die, &spec_cu);
11964 if (spec_die == NULL)
11965 parent = die->parent;
11966 else
63d06c5c 11967 {
0114d602
DJ
11968 parent = spec_die->parent;
11969 cu = spec_cu;
63d06c5c 11970 }
0114d602
DJ
11971
11972 if (parent == NULL)
11973 return "";
98bfdba5
PA
11974 else if (parent->building_fullname)
11975 {
11976 const char *name;
11977 const char *parent_name;
11978
11979 /* It has been seen on RealView 2.2 built binaries,
11980 DW_TAG_template_type_param types actually _defined_ as
11981 children of the parent class:
11982
11983 enum E {};
11984 template class <class Enum> Class{};
11985 Class<enum E> class_e;
11986
11987 1: DW_TAG_class_type (Class)
11988 2: DW_TAG_enumeration_type (E)
11989 3: DW_TAG_enumerator (enum1:0)
11990 3: DW_TAG_enumerator (enum2:1)
11991 ...
11992 2: DW_TAG_template_type_param
11993 DW_AT_type DW_FORM_ref_udata (E)
11994
11995 Besides being broken debug info, it can put GDB into an
11996 infinite loop. Consider:
11997
11998 When we're building the full name for Class<E>, we'll start
11999 at Class, and go look over its template type parameters,
12000 finding E. We'll then try to build the full name of E, and
12001 reach here. We're now trying to build the full name of E,
12002 and look over the parent DIE for containing scope. In the
12003 broken case, if we followed the parent DIE of E, we'd again
12004 find Class, and once again go look at its template type
12005 arguments, etc., etc. Simply don't consider such parent die
12006 as source-level parent of this die (it can't be, the language
12007 doesn't allow it), and break the loop here. */
12008 name = dwarf2_name (die, cu);
12009 parent_name = dwarf2_name (parent, cu);
12010 complaint (&symfile_complaints,
12011 _("template param type '%s' defined within parent '%s'"),
12012 name ? name : "<unknown>",
12013 parent_name ? parent_name : "<unknown>");
12014 return "";
12015 }
63d06c5c 12016 else
0114d602
DJ
12017 switch (parent->tag)
12018 {
63d06c5c 12019 case DW_TAG_namespace:
0114d602 12020 parent_type = read_type_die (parent, cu);
acebe513
UW
12021 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12022 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12023 Work around this problem here. */
12024 if (cu->language == language_cplus
12025 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12026 return "";
0114d602
DJ
12027 /* We give a name to even anonymous namespaces. */
12028 return TYPE_TAG_NAME (parent_type);
63d06c5c 12029 case DW_TAG_class_type:
680b30c7 12030 case DW_TAG_interface_type:
63d06c5c 12031 case DW_TAG_structure_type:
0114d602 12032 case DW_TAG_union_type:
f55ee35c 12033 case DW_TAG_module:
0114d602
DJ
12034 parent_type = read_type_die (parent, cu);
12035 if (TYPE_TAG_NAME (parent_type) != NULL)
12036 return TYPE_TAG_NAME (parent_type);
12037 else
12038 /* An anonymous structure is only allowed non-static data
12039 members; no typedefs, no member functions, et cetera.
12040 So it does not need a prefix. */
12041 return "";
abc72ce4
DE
12042 case DW_TAG_compile_unit:
12043 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12044 if (cu->language == language_cplus
12045 && dwarf2_per_objfile->types.asection != NULL
12046 && die->child != NULL
12047 && (die->tag == DW_TAG_class_type
12048 || die->tag == DW_TAG_structure_type
12049 || die->tag == DW_TAG_union_type))
12050 {
12051 char *name = guess_full_die_structure_name (die, cu);
12052 if (name != NULL)
12053 return name;
12054 }
12055 return "";
63d06c5c 12056 default:
8176b9b8 12057 return determine_prefix (parent, cu);
63d06c5c 12058 }
63d06c5c
DC
12059}
12060
3e43a32a
MS
12061/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12062 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12063 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12064 an obconcat, otherwise allocate storage for the result. The CU argument is
12065 used to determine the language and hence, the appropriate separator. */
987504bb 12066
f55ee35c 12067#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12068
12069static char *
f55ee35c
JK
12070typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12071 int physname, struct dwarf2_cu *cu)
63d06c5c 12072{
f55ee35c 12073 const char *lead = "";
5c315b68 12074 const char *sep;
63d06c5c 12075
3e43a32a
MS
12076 if (suffix == NULL || suffix[0] == '\0'
12077 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12078 sep = "";
12079 else if (cu->language == language_java)
12080 sep = ".";
f55ee35c
JK
12081 else if (cu->language == language_fortran && physname)
12082 {
12083 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12084 DW_AT_MIPS_linkage_name is preferred and used instead. */
12085
12086 lead = "__";
12087 sep = "_MOD_";
12088 }
987504bb
JJ
12089 else
12090 sep = "::";
63d06c5c 12091
6dd47d34
DE
12092 if (prefix == NULL)
12093 prefix = "";
12094 if (suffix == NULL)
12095 suffix = "";
12096
987504bb
JJ
12097 if (obs == NULL)
12098 {
3e43a32a
MS
12099 char *retval
12100 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12101
f55ee35c
JK
12102 strcpy (retval, lead);
12103 strcat (retval, prefix);
6dd47d34
DE
12104 strcat (retval, sep);
12105 strcat (retval, suffix);
63d06c5c
DC
12106 return retval;
12107 }
987504bb
JJ
12108 else
12109 {
12110 /* We have an obstack. */
f55ee35c 12111 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12112 }
63d06c5c
DC
12113}
12114
c906108c
SS
12115/* Return sibling of die, NULL if no sibling. */
12116
f9aca02d 12117static struct die_info *
fba45db2 12118sibling_die (struct die_info *die)
c906108c 12119{
639d11d3 12120 return die->sibling;
c906108c
SS
12121}
12122
71c25dea
TT
12123/* Get name of a die, return NULL if not found. */
12124
12125static char *
12126dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12127 struct obstack *obstack)
12128{
12129 if (name && cu->language == language_cplus)
12130 {
12131 char *canon_name = cp_canonicalize_string (name);
12132
12133 if (canon_name != NULL)
12134 {
12135 if (strcmp (canon_name, name) != 0)
12136 name = obsavestring (canon_name, strlen (canon_name),
12137 obstack);
12138 xfree (canon_name);
12139 }
12140 }
12141
12142 return name;
c906108c
SS
12143}
12144
9219021c
DC
12145/* Get name of a die, return NULL if not found. */
12146
12147static char *
e142c38c 12148dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12149{
12150 struct attribute *attr;
12151
e142c38c 12152 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12153 if ((!attr || !DW_STRING (attr))
12154 && die->tag != DW_TAG_class_type
12155 && die->tag != DW_TAG_interface_type
12156 && die->tag != DW_TAG_structure_type
12157 && die->tag != DW_TAG_union_type)
71c25dea
TT
12158 return NULL;
12159
12160 switch (die->tag)
12161 {
12162 case DW_TAG_compile_unit:
12163 /* Compilation units have a DW_AT_name that is a filename, not
12164 a source language identifier. */
12165 case DW_TAG_enumeration_type:
12166 case DW_TAG_enumerator:
12167 /* These tags always have simple identifiers already; no need
12168 to canonicalize them. */
12169 return DW_STRING (attr);
907af001 12170
418835cc
KS
12171 case DW_TAG_subprogram:
12172 /* Java constructors will all be named "<init>", so return
12173 the class name when we see this special case. */
12174 if (cu->language == language_java
12175 && DW_STRING (attr) != NULL
12176 && strcmp (DW_STRING (attr), "<init>") == 0)
12177 {
12178 struct dwarf2_cu *spec_cu = cu;
12179 struct die_info *spec_die;
12180
12181 /* GCJ will output '<init>' for Java constructor names.
12182 For this special case, return the name of the parent class. */
12183
12184 /* GCJ may output suprogram DIEs with AT_specification set.
12185 If so, use the name of the specified DIE. */
12186 spec_die = die_specification (die, &spec_cu);
12187 if (spec_die != NULL)
12188 return dwarf2_name (spec_die, spec_cu);
12189
12190 do
12191 {
12192 die = die->parent;
12193 if (die->tag == DW_TAG_class_type)
12194 return dwarf2_name (die, cu);
12195 }
12196 while (die->tag != DW_TAG_compile_unit);
12197 }
907af001
UW
12198 break;
12199
12200 case DW_TAG_class_type:
12201 case DW_TAG_interface_type:
12202 case DW_TAG_structure_type:
12203 case DW_TAG_union_type:
12204 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12205 structures or unions. These were of the form "._%d" in GCC 4.1,
12206 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12207 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12208 if (attr && DW_STRING (attr)
12209 && (strncmp (DW_STRING (attr), "._", 2) == 0
12210 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12211 return NULL;
53832f31
TT
12212
12213 /* GCC might emit a nameless typedef that has a linkage name. See
12214 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12215 if (!attr || DW_STRING (attr) == NULL)
12216 {
df5c6c50 12217 char *demangled = NULL;
53832f31
TT
12218
12219 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12220 if (attr == NULL)
12221 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12222
12223 if (attr == NULL || DW_STRING (attr) == NULL)
12224 return NULL;
12225
df5c6c50
JK
12226 /* Avoid demangling DW_STRING (attr) the second time on a second
12227 call for the same DIE. */
12228 if (!DW_STRING_IS_CANONICAL (attr))
12229 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12230
12231 if (demangled)
12232 {
12233 /* FIXME: we already did this for the partial symbol... */
12234 DW_STRING (attr)
12235 = obsavestring (demangled, strlen (demangled),
12236 &cu->objfile->objfile_obstack);
12237 DW_STRING_IS_CANONICAL (attr) = 1;
12238 xfree (demangled);
12239 }
12240 }
907af001
UW
12241 break;
12242
71c25dea 12243 default:
907af001
UW
12244 break;
12245 }
12246
12247 if (!DW_STRING_IS_CANONICAL (attr))
12248 {
12249 DW_STRING (attr)
12250 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12251 &cu->objfile->objfile_obstack);
12252 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12253 }
907af001 12254 return DW_STRING (attr);
9219021c
DC
12255}
12256
12257/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12258 is none. *EXT_CU is the CU containing DIE on input, and the CU
12259 containing the return value on output. */
9219021c
DC
12260
12261static struct die_info *
f2f0e013 12262dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12263{
12264 struct attribute *attr;
9219021c 12265
f2f0e013 12266 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12267 if (attr == NULL)
12268 return NULL;
12269
f2f0e013 12270 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12271}
12272
c906108c
SS
12273/* Convert a DIE tag into its string name. */
12274
12275static char *
aa1ee363 12276dwarf_tag_name (unsigned tag)
c906108c
SS
12277{
12278 switch (tag)
12279 {
12280 case DW_TAG_padding:
12281 return "DW_TAG_padding";
12282 case DW_TAG_array_type:
12283 return "DW_TAG_array_type";
12284 case DW_TAG_class_type:
12285 return "DW_TAG_class_type";
12286 case DW_TAG_entry_point:
12287 return "DW_TAG_entry_point";
12288 case DW_TAG_enumeration_type:
12289 return "DW_TAG_enumeration_type";
12290 case DW_TAG_formal_parameter:
12291 return "DW_TAG_formal_parameter";
12292 case DW_TAG_imported_declaration:
12293 return "DW_TAG_imported_declaration";
12294 case DW_TAG_label:
12295 return "DW_TAG_label";
12296 case DW_TAG_lexical_block:
12297 return "DW_TAG_lexical_block";
12298 case DW_TAG_member:
12299 return "DW_TAG_member";
12300 case DW_TAG_pointer_type:
12301 return "DW_TAG_pointer_type";
12302 case DW_TAG_reference_type:
12303 return "DW_TAG_reference_type";
12304 case DW_TAG_compile_unit:
12305 return "DW_TAG_compile_unit";
12306 case DW_TAG_string_type:
12307 return "DW_TAG_string_type";
12308 case DW_TAG_structure_type:
12309 return "DW_TAG_structure_type";
12310 case DW_TAG_subroutine_type:
12311 return "DW_TAG_subroutine_type";
12312 case DW_TAG_typedef:
12313 return "DW_TAG_typedef";
12314 case DW_TAG_union_type:
12315 return "DW_TAG_union_type";
12316 case DW_TAG_unspecified_parameters:
12317 return "DW_TAG_unspecified_parameters";
12318 case DW_TAG_variant:
12319 return "DW_TAG_variant";
12320 case DW_TAG_common_block:
12321 return "DW_TAG_common_block";
12322 case DW_TAG_common_inclusion:
12323 return "DW_TAG_common_inclusion";
12324 case DW_TAG_inheritance:
12325 return "DW_TAG_inheritance";
12326 case DW_TAG_inlined_subroutine:
12327 return "DW_TAG_inlined_subroutine";
12328 case DW_TAG_module:
12329 return "DW_TAG_module";
12330 case DW_TAG_ptr_to_member_type:
12331 return "DW_TAG_ptr_to_member_type";
12332 case DW_TAG_set_type:
12333 return "DW_TAG_set_type";
12334 case DW_TAG_subrange_type:
12335 return "DW_TAG_subrange_type";
12336 case DW_TAG_with_stmt:
12337 return "DW_TAG_with_stmt";
12338 case DW_TAG_access_declaration:
12339 return "DW_TAG_access_declaration";
12340 case DW_TAG_base_type:
12341 return "DW_TAG_base_type";
12342 case DW_TAG_catch_block:
12343 return "DW_TAG_catch_block";
12344 case DW_TAG_const_type:
12345 return "DW_TAG_const_type";
12346 case DW_TAG_constant:
12347 return "DW_TAG_constant";
12348 case DW_TAG_enumerator:
12349 return "DW_TAG_enumerator";
12350 case DW_TAG_file_type:
12351 return "DW_TAG_file_type";
12352 case DW_TAG_friend:
12353 return "DW_TAG_friend";
12354 case DW_TAG_namelist:
12355 return "DW_TAG_namelist";
12356 case DW_TAG_namelist_item:
12357 return "DW_TAG_namelist_item";
12358 case DW_TAG_packed_type:
12359 return "DW_TAG_packed_type";
12360 case DW_TAG_subprogram:
12361 return "DW_TAG_subprogram";
12362 case DW_TAG_template_type_param:
12363 return "DW_TAG_template_type_param";
12364 case DW_TAG_template_value_param:
12365 return "DW_TAG_template_value_param";
12366 case DW_TAG_thrown_type:
12367 return "DW_TAG_thrown_type";
12368 case DW_TAG_try_block:
12369 return "DW_TAG_try_block";
12370 case DW_TAG_variant_part:
12371 return "DW_TAG_variant_part";
12372 case DW_TAG_variable:
12373 return "DW_TAG_variable";
12374 case DW_TAG_volatile_type:
12375 return "DW_TAG_volatile_type";
d9fa45fe
DC
12376 case DW_TAG_dwarf_procedure:
12377 return "DW_TAG_dwarf_procedure";
12378 case DW_TAG_restrict_type:
12379 return "DW_TAG_restrict_type";
12380 case DW_TAG_interface_type:
12381 return "DW_TAG_interface_type";
12382 case DW_TAG_namespace:
12383 return "DW_TAG_namespace";
12384 case DW_TAG_imported_module:
12385 return "DW_TAG_imported_module";
12386 case DW_TAG_unspecified_type:
12387 return "DW_TAG_unspecified_type";
12388 case DW_TAG_partial_unit:
12389 return "DW_TAG_partial_unit";
12390 case DW_TAG_imported_unit:
12391 return "DW_TAG_imported_unit";
b7619582
GF
12392 case DW_TAG_condition:
12393 return "DW_TAG_condition";
12394 case DW_TAG_shared_type:
12395 return "DW_TAG_shared_type";
348e048f
DE
12396 case DW_TAG_type_unit:
12397 return "DW_TAG_type_unit";
c906108c
SS
12398 case DW_TAG_MIPS_loop:
12399 return "DW_TAG_MIPS_loop";
b7619582
GF
12400 case DW_TAG_HP_array_descriptor:
12401 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12402 case DW_TAG_format_label:
12403 return "DW_TAG_format_label";
12404 case DW_TAG_function_template:
12405 return "DW_TAG_function_template";
12406 case DW_TAG_class_template:
12407 return "DW_TAG_class_template";
b7619582
GF
12408 case DW_TAG_GNU_BINCL:
12409 return "DW_TAG_GNU_BINCL";
12410 case DW_TAG_GNU_EINCL:
12411 return "DW_TAG_GNU_EINCL";
12412 case DW_TAG_upc_shared_type:
12413 return "DW_TAG_upc_shared_type";
12414 case DW_TAG_upc_strict_type:
12415 return "DW_TAG_upc_strict_type";
12416 case DW_TAG_upc_relaxed_type:
12417 return "DW_TAG_upc_relaxed_type";
12418 case DW_TAG_PGI_kanji_type:
12419 return "DW_TAG_PGI_kanji_type";
12420 case DW_TAG_PGI_interface_block:
12421 return "DW_TAG_PGI_interface_block";
c906108c
SS
12422 default:
12423 return "DW_TAG_<unknown>";
12424 }
12425}
12426
12427/* Convert a DWARF attribute code into its string name. */
12428
12429static char *
aa1ee363 12430dwarf_attr_name (unsigned attr)
c906108c
SS
12431{
12432 switch (attr)
12433 {
12434 case DW_AT_sibling:
12435 return "DW_AT_sibling";
12436 case DW_AT_location:
12437 return "DW_AT_location";
12438 case DW_AT_name:
12439 return "DW_AT_name";
12440 case DW_AT_ordering:
12441 return "DW_AT_ordering";
12442 case DW_AT_subscr_data:
12443 return "DW_AT_subscr_data";
12444 case DW_AT_byte_size:
12445 return "DW_AT_byte_size";
12446 case DW_AT_bit_offset:
12447 return "DW_AT_bit_offset";
12448 case DW_AT_bit_size:
12449 return "DW_AT_bit_size";
12450 case DW_AT_element_list:
12451 return "DW_AT_element_list";
12452 case DW_AT_stmt_list:
12453 return "DW_AT_stmt_list";
12454 case DW_AT_low_pc:
12455 return "DW_AT_low_pc";
12456 case DW_AT_high_pc:
12457 return "DW_AT_high_pc";
12458 case DW_AT_language:
12459 return "DW_AT_language";
12460 case DW_AT_member:
12461 return "DW_AT_member";
12462 case DW_AT_discr:
12463 return "DW_AT_discr";
12464 case DW_AT_discr_value:
12465 return "DW_AT_discr_value";
12466 case DW_AT_visibility:
12467 return "DW_AT_visibility";
12468 case DW_AT_import:
12469 return "DW_AT_import";
12470 case DW_AT_string_length:
12471 return "DW_AT_string_length";
12472 case DW_AT_common_reference:
12473 return "DW_AT_common_reference";
12474 case DW_AT_comp_dir:
12475 return "DW_AT_comp_dir";
12476 case DW_AT_const_value:
12477 return "DW_AT_const_value";
12478 case DW_AT_containing_type:
12479 return "DW_AT_containing_type";
12480 case DW_AT_default_value:
12481 return "DW_AT_default_value";
12482 case DW_AT_inline:
12483 return "DW_AT_inline";
12484 case DW_AT_is_optional:
12485 return "DW_AT_is_optional";
12486 case DW_AT_lower_bound:
12487 return "DW_AT_lower_bound";
12488 case DW_AT_producer:
12489 return "DW_AT_producer";
12490 case DW_AT_prototyped:
12491 return "DW_AT_prototyped";
12492 case DW_AT_return_addr:
12493 return "DW_AT_return_addr";
12494 case DW_AT_start_scope:
12495 return "DW_AT_start_scope";
09fa0d7c
JK
12496 case DW_AT_bit_stride:
12497 return "DW_AT_bit_stride";
c906108c
SS
12498 case DW_AT_upper_bound:
12499 return "DW_AT_upper_bound";
12500 case DW_AT_abstract_origin:
12501 return "DW_AT_abstract_origin";
12502 case DW_AT_accessibility:
12503 return "DW_AT_accessibility";
12504 case DW_AT_address_class:
12505 return "DW_AT_address_class";
12506 case DW_AT_artificial:
12507 return "DW_AT_artificial";
12508 case DW_AT_base_types:
12509 return "DW_AT_base_types";
12510 case DW_AT_calling_convention:
12511 return "DW_AT_calling_convention";
12512 case DW_AT_count:
12513 return "DW_AT_count";
12514 case DW_AT_data_member_location:
12515 return "DW_AT_data_member_location";
12516 case DW_AT_decl_column:
12517 return "DW_AT_decl_column";
12518 case DW_AT_decl_file:
12519 return "DW_AT_decl_file";
12520 case DW_AT_decl_line:
12521 return "DW_AT_decl_line";
12522 case DW_AT_declaration:
12523 return "DW_AT_declaration";
12524 case DW_AT_discr_list:
12525 return "DW_AT_discr_list";
12526 case DW_AT_encoding:
12527 return "DW_AT_encoding";
12528 case DW_AT_external:
12529 return "DW_AT_external";
12530 case DW_AT_frame_base:
12531 return "DW_AT_frame_base";
12532 case DW_AT_friend:
12533 return "DW_AT_friend";
12534 case DW_AT_identifier_case:
12535 return "DW_AT_identifier_case";
12536 case DW_AT_macro_info:
12537 return "DW_AT_macro_info";
12538 case DW_AT_namelist_items:
12539 return "DW_AT_namelist_items";
12540 case DW_AT_priority:
12541 return "DW_AT_priority";
12542 case DW_AT_segment:
12543 return "DW_AT_segment";
12544 case DW_AT_specification:
12545 return "DW_AT_specification";
12546 case DW_AT_static_link:
12547 return "DW_AT_static_link";
12548 case DW_AT_type:
12549 return "DW_AT_type";
12550 case DW_AT_use_location:
12551 return "DW_AT_use_location";
12552 case DW_AT_variable_parameter:
12553 return "DW_AT_variable_parameter";
12554 case DW_AT_virtuality:
12555 return "DW_AT_virtuality";
12556 case DW_AT_vtable_elem_location:
12557 return "DW_AT_vtable_elem_location";
b7619582 12558 /* DWARF 3 values. */
d9fa45fe
DC
12559 case DW_AT_allocated:
12560 return "DW_AT_allocated";
12561 case DW_AT_associated:
12562 return "DW_AT_associated";
12563 case DW_AT_data_location:
12564 return "DW_AT_data_location";
09fa0d7c
JK
12565 case DW_AT_byte_stride:
12566 return "DW_AT_byte_stride";
d9fa45fe
DC
12567 case DW_AT_entry_pc:
12568 return "DW_AT_entry_pc";
12569 case DW_AT_use_UTF8:
12570 return "DW_AT_use_UTF8";
12571 case DW_AT_extension:
12572 return "DW_AT_extension";
12573 case DW_AT_ranges:
12574 return "DW_AT_ranges";
12575 case DW_AT_trampoline:
12576 return "DW_AT_trampoline";
12577 case DW_AT_call_column:
12578 return "DW_AT_call_column";
12579 case DW_AT_call_file:
12580 return "DW_AT_call_file";
12581 case DW_AT_call_line:
12582 return "DW_AT_call_line";
b7619582
GF
12583 case DW_AT_description:
12584 return "DW_AT_description";
12585 case DW_AT_binary_scale:
12586 return "DW_AT_binary_scale";
12587 case DW_AT_decimal_scale:
12588 return "DW_AT_decimal_scale";
12589 case DW_AT_small:
12590 return "DW_AT_small";
12591 case DW_AT_decimal_sign:
12592 return "DW_AT_decimal_sign";
12593 case DW_AT_digit_count:
12594 return "DW_AT_digit_count";
12595 case DW_AT_picture_string:
12596 return "DW_AT_picture_string";
12597 case DW_AT_mutable:
12598 return "DW_AT_mutable";
12599 case DW_AT_threads_scaled:
12600 return "DW_AT_threads_scaled";
12601 case DW_AT_explicit:
12602 return "DW_AT_explicit";
12603 case DW_AT_object_pointer:
12604 return "DW_AT_object_pointer";
12605 case DW_AT_endianity:
12606 return "DW_AT_endianity";
12607 case DW_AT_elemental:
12608 return "DW_AT_elemental";
12609 case DW_AT_pure:
12610 return "DW_AT_pure";
12611 case DW_AT_recursive:
12612 return "DW_AT_recursive";
348e048f
DE
12613 /* DWARF 4 values. */
12614 case DW_AT_signature:
12615 return "DW_AT_signature";
31ef98ae
TT
12616 case DW_AT_linkage_name:
12617 return "DW_AT_linkage_name";
b7619582 12618 /* SGI/MIPS extensions. */
c764a876 12619#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12620 case DW_AT_MIPS_fde:
12621 return "DW_AT_MIPS_fde";
c764a876 12622#endif
c906108c
SS
12623 case DW_AT_MIPS_loop_begin:
12624 return "DW_AT_MIPS_loop_begin";
12625 case DW_AT_MIPS_tail_loop_begin:
12626 return "DW_AT_MIPS_tail_loop_begin";
12627 case DW_AT_MIPS_epilog_begin:
12628 return "DW_AT_MIPS_epilog_begin";
12629 case DW_AT_MIPS_loop_unroll_factor:
12630 return "DW_AT_MIPS_loop_unroll_factor";
12631 case DW_AT_MIPS_software_pipeline_depth:
12632 return "DW_AT_MIPS_software_pipeline_depth";
12633 case DW_AT_MIPS_linkage_name:
12634 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12635 case DW_AT_MIPS_stride:
12636 return "DW_AT_MIPS_stride";
12637 case DW_AT_MIPS_abstract_name:
12638 return "DW_AT_MIPS_abstract_name";
12639 case DW_AT_MIPS_clone_origin:
12640 return "DW_AT_MIPS_clone_origin";
12641 case DW_AT_MIPS_has_inlines:
12642 return "DW_AT_MIPS_has_inlines";
b7619582 12643 /* HP extensions. */
c764a876 12644#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12645 case DW_AT_HP_block_index:
12646 return "DW_AT_HP_block_index";
c764a876 12647#endif
b7619582
GF
12648 case DW_AT_HP_unmodifiable:
12649 return "DW_AT_HP_unmodifiable";
12650 case DW_AT_HP_actuals_stmt_list:
12651 return "DW_AT_HP_actuals_stmt_list";
12652 case DW_AT_HP_proc_per_section:
12653 return "DW_AT_HP_proc_per_section";
12654 case DW_AT_HP_raw_data_ptr:
12655 return "DW_AT_HP_raw_data_ptr";
12656 case DW_AT_HP_pass_by_reference:
12657 return "DW_AT_HP_pass_by_reference";
12658 case DW_AT_HP_opt_level:
12659 return "DW_AT_HP_opt_level";
12660 case DW_AT_HP_prof_version_id:
12661 return "DW_AT_HP_prof_version_id";
12662 case DW_AT_HP_opt_flags:
12663 return "DW_AT_HP_opt_flags";
12664 case DW_AT_HP_cold_region_low_pc:
12665 return "DW_AT_HP_cold_region_low_pc";
12666 case DW_AT_HP_cold_region_high_pc:
12667 return "DW_AT_HP_cold_region_high_pc";
12668 case DW_AT_HP_all_variables_modifiable:
12669 return "DW_AT_HP_all_variables_modifiable";
12670 case DW_AT_HP_linkage_name:
12671 return "DW_AT_HP_linkage_name";
12672 case DW_AT_HP_prof_flags:
12673 return "DW_AT_HP_prof_flags";
12674 /* GNU extensions. */
c906108c
SS
12675 case DW_AT_sf_names:
12676 return "DW_AT_sf_names";
12677 case DW_AT_src_info:
12678 return "DW_AT_src_info";
12679 case DW_AT_mac_info:
12680 return "DW_AT_mac_info";
12681 case DW_AT_src_coords:
12682 return "DW_AT_src_coords";
12683 case DW_AT_body_begin:
12684 return "DW_AT_body_begin";
12685 case DW_AT_body_end:
12686 return "DW_AT_body_end";
f5f8a009
EZ
12687 case DW_AT_GNU_vector:
12688 return "DW_AT_GNU_vector";
2de00c64
DE
12689 case DW_AT_GNU_odr_signature:
12690 return "DW_AT_GNU_odr_signature";
b7619582
GF
12691 /* VMS extensions. */
12692 case DW_AT_VMS_rtnbeg_pd_address:
12693 return "DW_AT_VMS_rtnbeg_pd_address";
12694 /* UPC extension. */
12695 case DW_AT_upc_threads_scaled:
12696 return "DW_AT_upc_threads_scaled";
12697 /* PGI (STMicroelectronics) extensions. */
12698 case DW_AT_PGI_lbase:
12699 return "DW_AT_PGI_lbase";
12700 case DW_AT_PGI_soffset:
12701 return "DW_AT_PGI_soffset";
12702 case DW_AT_PGI_lstride:
12703 return "DW_AT_PGI_lstride";
c906108c
SS
12704 default:
12705 return "DW_AT_<unknown>";
12706 }
12707}
12708
12709/* Convert a DWARF value form code into its string name. */
12710
12711static char *
aa1ee363 12712dwarf_form_name (unsigned form)
c906108c
SS
12713{
12714 switch (form)
12715 {
12716 case DW_FORM_addr:
12717 return "DW_FORM_addr";
12718 case DW_FORM_block2:
12719 return "DW_FORM_block2";
12720 case DW_FORM_block4:
12721 return "DW_FORM_block4";
12722 case DW_FORM_data2:
12723 return "DW_FORM_data2";
12724 case DW_FORM_data4:
12725 return "DW_FORM_data4";
12726 case DW_FORM_data8:
12727 return "DW_FORM_data8";
12728 case DW_FORM_string:
12729 return "DW_FORM_string";
12730 case DW_FORM_block:
12731 return "DW_FORM_block";
12732 case DW_FORM_block1:
12733 return "DW_FORM_block1";
12734 case DW_FORM_data1:
12735 return "DW_FORM_data1";
12736 case DW_FORM_flag:
12737 return "DW_FORM_flag";
12738 case DW_FORM_sdata:
12739 return "DW_FORM_sdata";
12740 case DW_FORM_strp:
12741 return "DW_FORM_strp";
12742 case DW_FORM_udata:
12743 return "DW_FORM_udata";
12744 case DW_FORM_ref_addr:
12745 return "DW_FORM_ref_addr";
12746 case DW_FORM_ref1:
12747 return "DW_FORM_ref1";
12748 case DW_FORM_ref2:
12749 return "DW_FORM_ref2";
12750 case DW_FORM_ref4:
12751 return "DW_FORM_ref4";
12752 case DW_FORM_ref8:
12753 return "DW_FORM_ref8";
12754 case DW_FORM_ref_udata:
12755 return "DW_FORM_ref_udata";
12756 case DW_FORM_indirect:
12757 return "DW_FORM_indirect";
348e048f
DE
12758 case DW_FORM_sec_offset:
12759 return "DW_FORM_sec_offset";
12760 case DW_FORM_exprloc:
12761 return "DW_FORM_exprloc";
12762 case DW_FORM_flag_present:
12763 return "DW_FORM_flag_present";
55f1336d
TT
12764 case DW_FORM_ref_sig8:
12765 return "DW_FORM_ref_sig8";
c906108c
SS
12766 default:
12767 return "DW_FORM_<unknown>";
12768 }
12769}
12770
12771/* Convert a DWARF stack opcode into its string name. */
12772
9eae7c52 12773const char *
b1bfef65 12774dwarf_stack_op_name (unsigned op)
c906108c
SS
12775{
12776 switch (op)
12777 {
12778 case DW_OP_addr:
12779 return "DW_OP_addr";
12780 case DW_OP_deref:
12781 return "DW_OP_deref";
12782 case DW_OP_const1u:
12783 return "DW_OP_const1u";
12784 case DW_OP_const1s:
12785 return "DW_OP_const1s";
12786 case DW_OP_const2u:
12787 return "DW_OP_const2u";
12788 case DW_OP_const2s:
12789 return "DW_OP_const2s";
12790 case DW_OP_const4u:
12791 return "DW_OP_const4u";
12792 case DW_OP_const4s:
12793 return "DW_OP_const4s";
12794 case DW_OP_const8u:
12795 return "DW_OP_const8u";
12796 case DW_OP_const8s:
12797 return "DW_OP_const8s";
12798 case DW_OP_constu:
12799 return "DW_OP_constu";
12800 case DW_OP_consts:
12801 return "DW_OP_consts";
12802 case DW_OP_dup:
12803 return "DW_OP_dup";
12804 case DW_OP_drop:
12805 return "DW_OP_drop";
12806 case DW_OP_over:
12807 return "DW_OP_over";
12808 case DW_OP_pick:
12809 return "DW_OP_pick";
12810 case DW_OP_swap:
12811 return "DW_OP_swap";
12812 case DW_OP_rot:
12813 return "DW_OP_rot";
12814 case DW_OP_xderef:
12815 return "DW_OP_xderef";
12816 case DW_OP_abs:
12817 return "DW_OP_abs";
12818 case DW_OP_and:
12819 return "DW_OP_and";
12820 case DW_OP_div:
12821 return "DW_OP_div";
12822 case DW_OP_minus:
12823 return "DW_OP_minus";
12824 case DW_OP_mod:
12825 return "DW_OP_mod";
12826 case DW_OP_mul:
12827 return "DW_OP_mul";
12828 case DW_OP_neg:
12829 return "DW_OP_neg";
12830 case DW_OP_not:
12831 return "DW_OP_not";
12832 case DW_OP_or:
12833 return "DW_OP_or";
12834 case DW_OP_plus:
12835 return "DW_OP_plus";
12836 case DW_OP_plus_uconst:
12837 return "DW_OP_plus_uconst";
12838 case DW_OP_shl:
12839 return "DW_OP_shl";
12840 case DW_OP_shr:
12841 return "DW_OP_shr";
12842 case DW_OP_shra:
12843 return "DW_OP_shra";
12844 case DW_OP_xor:
12845 return "DW_OP_xor";
12846 case DW_OP_bra:
12847 return "DW_OP_bra";
12848 case DW_OP_eq:
12849 return "DW_OP_eq";
12850 case DW_OP_ge:
12851 return "DW_OP_ge";
12852 case DW_OP_gt:
12853 return "DW_OP_gt";
12854 case DW_OP_le:
12855 return "DW_OP_le";
12856 case DW_OP_lt:
12857 return "DW_OP_lt";
12858 case DW_OP_ne:
12859 return "DW_OP_ne";
12860 case DW_OP_skip:
12861 return "DW_OP_skip";
12862 case DW_OP_lit0:
12863 return "DW_OP_lit0";
12864 case DW_OP_lit1:
12865 return "DW_OP_lit1";
12866 case DW_OP_lit2:
12867 return "DW_OP_lit2";
12868 case DW_OP_lit3:
12869 return "DW_OP_lit3";
12870 case DW_OP_lit4:
12871 return "DW_OP_lit4";
12872 case DW_OP_lit5:
12873 return "DW_OP_lit5";
12874 case DW_OP_lit6:
12875 return "DW_OP_lit6";
12876 case DW_OP_lit7:
12877 return "DW_OP_lit7";
12878 case DW_OP_lit8:
12879 return "DW_OP_lit8";
12880 case DW_OP_lit9:
12881 return "DW_OP_lit9";
12882 case DW_OP_lit10:
12883 return "DW_OP_lit10";
12884 case DW_OP_lit11:
12885 return "DW_OP_lit11";
12886 case DW_OP_lit12:
12887 return "DW_OP_lit12";
12888 case DW_OP_lit13:
12889 return "DW_OP_lit13";
12890 case DW_OP_lit14:
12891 return "DW_OP_lit14";
12892 case DW_OP_lit15:
12893 return "DW_OP_lit15";
12894 case DW_OP_lit16:
12895 return "DW_OP_lit16";
12896 case DW_OP_lit17:
12897 return "DW_OP_lit17";
12898 case DW_OP_lit18:
12899 return "DW_OP_lit18";
12900 case DW_OP_lit19:
12901 return "DW_OP_lit19";
12902 case DW_OP_lit20:
12903 return "DW_OP_lit20";
12904 case DW_OP_lit21:
12905 return "DW_OP_lit21";
12906 case DW_OP_lit22:
12907 return "DW_OP_lit22";
12908 case DW_OP_lit23:
12909 return "DW_OP_lit23";
12910 case DW_OP_lit24:
12911 return "DW_OP_lit24";
12912 case DW_OP_lit25:
12913 return "DW_OP_lit25";
12914 case DW_OP_lit26:
12915 return "DW_OP_lit26";
12916 case DW_OP_lit27:
12917 return "DW_OP_lit27";
12918 case DW_OP_lit28:
12919 return "DW_OP_lit28";
12920 case DW_OP_lit29:
12921 return "DW_OP_lit29";
12922 case DW_OP_lit30:
12923 return "DW_OP_lit30";
12924 case DW_OP_lit31:
12925 return "DW_OP_lit31";
12926 case DW_OP_reg0:
12927 return "DW_OP_reg0";
12928 case DW_OP_reg1:
12929 return "DW_OP_reg1";
12930 case DW_OP_reg2:
12931 return "DW_OP_reg2";
12932 case DW_OP_reg3:
12933 return "DW_OP_reg3";
12934 case DW_OP_reg4:
12935 return "DW_OP_reg4";
12936 case DW_OP_reg5:
12937 return "DW_OP_reg5";
12938 case DW_OP_reg6:
12939 return "DW_OP_reg6";
12940 case DW_OP_reg7:
12941 return "DW_OP_reg7";
12942 case DW_OP_reg8:
12943 return "DW_OP_reg8";
12944 case DW_OP_reg9:
12945 return "DW_OP_reg9";
12946 case DW_OP_reg10:
12947 return "DW_OP_reg10";
12948 case DW_OP_reg11:
12949 return "DW_OP_reg11";
12950 case DW_OP_reg12:
12951 return "DW_OP_reg12";
12952 case DW_OP_reg13:
12953 return "DW_OP_reg13";
12954 case DW_OP_reg14:
12955 return "DW_OP_reg14";
12956 case DW_OP_reg15:
12957 return "DW_OP_reg15";
12958 case DW_OP_reg16:
12959 return "DW_OP_reg16";
12960 case DW_OP_reg17:
12961 return "DW_OP_reg17";
12962 case DW_OP_reg18:
12963 return "DW_OP_reg18";
12964 case DW_OP_reg19:
12965 return "DW_OP_reg19";
12966 case DW_OP_reg20:
12967 return "DW_OP_reg20";
12968 case DW_OP_reg21:
12969 return "DW_OP_reg21";
12970 case DW_OP_reg22:
12971 return "DW_OP_reg22";
12972 case DW_OP_reg23:
12973 return "DW_OP_reg23";
12974 case DW_OP_reg24:
12975 return "DW_OP_reg24";
12976 case DW_OP_reg25:
12977 return "DW_OP_reg25";
12978 case DW_OP_reg26:
12979 return "DW_OP_reg26";
12980 case DW_OP_reg27:
12981 return "DW_OP_reg27";
12982 case DW_OP_reg28:
12983 return "DW_OP_reg28";
12984 case DW_OP_reg29:
12985 return "DW_OP_reg29";
12986 case DW_OP_reg30:
12987 return "DW_OP_reg30";
12988 case DW_OP_reg31:
12989 return "DW_OP_reg31";
12990 case DW_OP_breg0:
12991 return "DW_OP_breg0";
12992 case DW_OP_breg1:
12993 return "DW_OP_breg1";
12994 case DW_OP_breg2:
12995 return "DW_OP_breg2";
12996 case DW_OP_breg3:
12997 return "DW_OP_breg3";
12998 case DW_OP_breg4:
12999 return "DW_OP_breg4";
13000 case DW_OP_breg5:
13001 return "DW_OP_breg5";
13002 case DW_OP_breg6:
13003 return "DW_OP_breg6";
13004 case DW_OP_breg7:
13005 return "DW_OP_breg7";
13006 case DW_OP_breg8:
13007 return "DW_OP_breg8";
13008 case DW_OP_breg9:
13009 return "DW_OP_breg9";
13010 case DW_OP_breg10:
13011 return "DW_OP_breg10";
13012 case DW_OP_breg11:
13013 return "DW_OP_breg11";
13014 case DW_OP_breg12:
13015 return "DW_OP_breg12";
13016 case DW_OP_breg13:
13017 return "DW_OP_breg13";
13018 case DW_OP_breg14:
13019 return "DW_OP_breg14";
13020 case DW_OP_breg15:
13021 return "DW_OP_breg15";
13022 case DW_OP_breg16:
13023 return "DW_OP_breg16";
13024 case DW_OP_breg17:
13025 return "DW_OP_breg17";
13026 case DW_OP_breg18:
13027 return "DW_OP_breg18";
13028 case DW_OP_breg19:
13029 return "DW_OP_breg19";
13030 case DW_OP_breg20:
13031 return "DW_OP_breg20";
13032 case DW_OP_breg21:
13033 return "DW_OP_breg21";
13034 case DW_OP_breg22:
13035 return "DW_OP_breg22";
13036 case DW_OP_breg23:
13037 return "DW_OP_breg23";
13038 case DW_OP_breg24:
13039 return "DW_OP_breg24";
13040 case DW_OP_breg25:
13041 return "DW_OP_breg25";
13042 case DW_OP_breg26:
13043 return "DW_OP_breg26";
13044 case DW_OP_breg27:
13045 return "DW_OP_breg27";
13046 case DW_OP_breg28:
13047 return "DW_OP_breg28";
13048 case DW_OP_breg29:
13049 return "DW_OP_breg29";
13050 case DW_OP_breg30:
13051 return "DW_OP_breg30";
13052 case DW_OP_breg31:
13053 return "DW_OP_breg31";
13054 case DW_OP_regx:
13055 return "DW_OP_regx";
13056 case DW_OP_fbreg:
13057 return "DW_OP_fbreg";
13058 case DW_OP_bregx:
13059 return "DW_OP_bregx";
13060 case DW_OP_piece:
13061 return "DW_OP_piece";
13062 case DW_OP_deref_size:
13063 return "DW_OP_deref_size";
13064 case DW_OP_xderef_size:
13065 return "DW_OP_xderef_size";
13066 case DW_OP_nop:
13067 return "DW_OP_nop";
b7619582 13068 /* DWARF 3 extensions. */
ed348acc
EZ
13069 case DW_OP_push_object_address:
13070 return "DW_OP_push_object_address";
13071 case DW_OP_call2:
13072 return "DW_OP_call2";
13073 case DW_OP_call4:
13074 return "DW_OP_call4";
13075 case DW_OP_call_ref:
13076 return "DW_OP_call_ref";
b7619582
GF
13077 case DW_OP_form_tls_address:
13078 return "DW_OP_form_tls_address";
13079 case DW_OP_call_frame_cfa:
13080 return "DW_OP_call_frame_cfa";
13081 case DW_OP_bit_piece:
13082 return "DW_OP_bit_piece";
9eae7c52
TT
13083 /* DWARF 4 extensions. */
13084 case DW_OP_implicit_value:
13085 return "DW_OP_implicit_value";
13086 case DW_OP_stack_value:
13087 return "DW_OP_stack_value";
13088 /* GNU extensions. */
ed348acc
EZ
13089 case DW_OP_GNU_push_tls_address:
13090 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13091 case DW_OP_GNU_uninit:
13092 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13093 case DW_OP_GNU_implicit_pointer:
13094 return "DW_OP_GNU_implicit_pointer";
c906108c 13095 default:
b1bfef65 13096 return NULL;
c906108c
SS
13097 }
13098}
13099
13100static char *
fba45db2 13101dwarf_bool_name (unsigned mybool)
c906108c
SS
13102{
13103 if (mybool)
13104 return "TRUE";
13105 else
13106 return "FALSE";
13107}
13108
13109/* Convert a DWARF type code into its string name. */
13110
13111static char *
aa1ee363 13112dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13113{
13114 switch (enc)
13115 {
b7619582
GF
13116 case DW_ATE_void:
13117 return "DW_ATE_void";
c906108c
SS
13118 case DW_ATE_address:
13119 return "DW_ATE_address";
13120 case DW_ATE_boolean:
13121 return "DW_ATE_boolean";
13122 case DW_ATE_complex_float:
13123 return "DW_ATE_complex_float";
13124 case DW_ATE_float:
13125 return "DW_ATE_float";
13126 case DW_ATE_signed:
13127 return "DW_ATE_signed";
13128 case DW_ATE_signed_char:
13129 return "DW_ATE_signed_char";
13130 case DW_ATE_unsigned:
13131 return "DW_ATE_unsigned";
13132 case DW_ATE_unsigned_char:
13133 return "DW_ATE_unsigned_char";
b7619582 13134 /* DWARF 3. */
d9fa45fe
DC
13135 case DW_ATE_imaginary_float:
13136 return "DW_ATE_imaginary_float";
b7619582
GF
13137 case DW_ATE_packed_decimal:
13138 return "DW_ATE_packed_decimal";
13139 case DW_ATE_numeric_string:
13140 return "DW_ATE_numeric_string";
13141 case DW_ATE_edited:
13142 return "DW_ATE_edited";
13143 case DW_ATE_signed_fixed:
13144 return "DW_ATE_signed_fixed";
13145 case DW_ATE_unsigned_fixed:
13146 return "DW_ATE_unsigned_fixed";
13147 case DW_ATE_decimal_float:
13148 return "DW_ATE_decimal_float";
75079b2b
TT
13149 /* DWARF 4. */
13150 case DW_ATE_UTF:
13151 return "DW_ATE_UTF";
b7619582
GF
13152 /* HP extensions. */
13153 case DW_ATE_HP_float80:
13154 return "DW_ATE_HP_float80";
13155 case DW_ATE_HP_complex_float80:
13156 return "DW_ATE_HP_complex_float80";
13157 case DW_ATE_HP_float128:
13158 return "DW_ATE_HP_float128";
13159 case DW_ATE_HP_complex_float128:
13160 return "DW_ATE_HP_complex_float128";
13161 case DW_ATE_HP_floathpintel:
13162 return "DW_ATE_HP_floathpintel";
13163 case DW_ATE_HP_imaginary_float80:
13164 return "DW_ATE_HP_imaginary_float80";
13165 case DW_ATE_HP_imaginary_float128:
13166 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13167 default:
13168 return "DW_ATE_<unknown>";
13169 }
13170}
13171
0963b4bd 13172/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13173
13174#if 0
13175static char *
aa1ee363 13176dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13177{
13178 switch (cfi_opc)
13179 {
13180 case DW_CFA_advance_loc:
13181 return "DW_CFA_advance_loc";
13182 case DW_CFA_offset:
13183 return "DW_CFA_offset";
13184 case DW_CFA_restore:
13185 return "DW_CFA_restore";
13186 case DW_CFA_nop:
13187 return "DW_CFA_nop";
13188 case DW_CFA_set_loc:
13189 return "DW_CFA_set_loc";
13190 case DW_CFA_advance_loc1:
13191 return "DW_CFA_advance_loc1";
13192 case DW_CFA_advance_loc2:
13193 return "DW_CFA_advance_loc2";
13194 case DW_CFA_advance_loc4:
13195 return "DW_CFA_advance_loc4";
13196 case DW_CFA_offset_extended:
13197 return "DW_CFA_offset_extended";
13198 case DW_CFA_restore_extended:
13199 return "DW_CFA_restore_extended";
13200 case DW_CFA_undefined:
13201 return "DW_CFA_undefined";
13202 case DW_CFA_same_value:
13203 return "DW_CFA_same_value";
13204 case DW_CFA_register:
13205 return "DW_CFA_register";
13206 case DW_CFA_remember_state:
13207 return "DW_CFA_remember_state";
13208 case DW_CFA_restore_state:
13209 return "DW_CFA_restore_state";
13210 case DW_CFA_def_cfa:
13211 return "DW_CFA_def_cfa";
13212 case DW_CFA_def_cfa_register:
13213 return "DW_CFA_def_cfa_register";
13214 case DW_CFA_def_cfa_offset:
13215 return "DW_CFA_def_cfa_offset";
b7619582 13216 /* DWARF 3. */
985cb1a3
JM
13217 case DW_CFA_def_cfa_expression:
13218 return "DW_CFA_def_cfa_expression";
13219 case DW_CFA_expression:
13220 return "DW_CFA_expression";
13221 case DW_CFA_offset_extended_sf:
13222 return "DW_CFA_offset_extended_sf";
13223 case DW_CFA_def_cfa_sf:
13224 return "DW_CFA_def_cfa_sf";
13225 case DW_CFA_def_cfa_offset_sf:
13226 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13227 case DW_CFA_val_offset:
13228 return "DW_CFA_val_offset";
13229 case DW_CFA_val_offset_sf:
13230 return "DW_CFA_val_offset_sf";
13231 case DW_CFA_val_expression:
13232 return "DW_CFA_val_expression";
13233 /* SGI/MIPS specific. */
c906108c
SS
13234 case DW_CFA_MIPS_advance_loc8:
13235 return "DW_CFA_MIPS_advance_loc8";
b7619582 13236 /* GNU extensions. */
985cb1a3
JM
13237 case DW_CFA_GNU_window_save:
13238 return "DW_CFA_GNU_window_save";
13239 case DW_CFA_GNU_args_size:
13240 return "DW_CFA_GNU_args_size";
13241 case DW_CFA_GNU_negative_offset_extended:
13242 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13243 default:
13244 return "DW_CFA_<unknown>";
13245 }
13246}
13247#endif
13248
f9aca02d 13249static void
d97bc12b 13250dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13251{
13252 unsigned int i;
13253
d97bc12b
DE
13254 print_spaces (indent, f);
13255 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13256 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13257
13258 if (die->parent != NULL)
13259 {
13260 print_spaces (indent, f);
13261 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13262 die->parent->offset);
13263 }
13264
13265 print_spaces (indent, f);
13266 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13267 dwarf_bool_name (die->child != NULL));
c906108c 13268
d97bc12b
DE
13269 print_spaces (indent, f);
13270 fprintf_unfiltered (f, " attributes:\n");
13271
c906108c
SS
13272 for (i = 0; i < die->num_attrs; ++i)
13273 {
d97bc12b
DE
13274 print_spaces (indent, f);
13275 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13276 dwarf_attr_name (die->attrs[i].name),
13277 dwarf_form_name (die->attrs[i].form));
d97bc12b 13278
c906108c
SS
13279 switch (die->attrs[i].form)
13280 {
13281 case DW_FORM_ref_addr:
13282 case DW_FORM_addr:
d97bc12b 13283 fprintf_unfiltered (f, "address: ");
5af949e3 13284 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13285 break;
13286 case DW_FORM_block2:
13287 case DW_FORM_block4:
13288 case DW_FORM_block:
13289 case DW_FORM_block1:
3e43a32a
MS
13290 fprintf_unfiltered (f, "block: size %d",
13291 DW_BLOCK (&die->attrs[i])->size);
c906108c 13292 break;
2dc7f7b3
TT
13293 case DW_FORM_exprloc:
13294 fprintf_unfiltered (f, "expression: size %u",
13295 DW_BLOCK (&die->attrs[i])->size);
13296 break;
10b3939b
DJ
13297 case DW_FORM_ref1:
13298 case DW_FORM_ref2:
13299 case DW_FORM_ref4:
d97bc12b 13300 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13301 (long) (DW_ADDR (&die->attrs[i])));
13302 break;
c906108c
SS
13303 case DW_FORM_data1:
13304 case DW_FORM_data2:
13305 case DW_FORM_data4:
ce5d95e1 13306 case DW_FORM_data8:
c906108c
SS
13307 case DW_FORM_udata:
13308 case DW_FORM_sdata:
43bbcdc2
PH
13309 fprintf_unfiltered (f, "constant: %s",
13310 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13311 break;
2dc7f7b3
TT
13312 case DW_FORM_sec_offset:
13313 fprintf_unfiltered (f, "section offset: %s",
13314 pulongest (DW_UNSND (&die->attrs[i])));
13315 break;
55f1336d 13316 case DW_FORM_ref_sig8:
348e048f
DE
13317 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13318 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13319 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
13320 else
13321 fprintf_unfiltered (f, "signatured type, offset: unknown");
13322 break;
c906108c 13323 case DW_FORM_string:
4bdf3d34 13324 case DW_FORM_strp:
8285870a 13325 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13326 DW_STRING (&die->attrs[i])
8285870a
JK
13327 ? DW_STRING (&die->attrs[i]) : "",
13328 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13329 break;
13330 case DW_FORM_flag:
13331 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13332 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13333 else
d97bc12b 13334 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13335 break;
2dc7f7b3
TT
13336 case DW_FORM_flag_present:
13337 fprintf_unfiltered (f, "flag: TRUE");
13338 break;
a8329558 13339 case DW_FORM_indirect:
0963b4bd
MS
13340 /* The reader will have reduced the indirect form to
13341 the "base form" so this form should not occur. */
3e43a32a
MS
13342 fprintf_unfiltered (f,
13343 "unexpected attribute form: DW_FORM_indirect");
a8329558 13344 break;
c906108c 13345 default:
d97bc12b 13346 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13347 die->attrs[i].form);
d97bc12b 13348 break;
c906108c 13349 }
d97bc12b 13350 fprintf_unfiltered (f, "\n");
c906108c
SS
13351 }
13352}
13353
f9aca02d 13354static void
d97bc12b 13355dump_die_for_error (struct die_info *die)
c906108c 13356{
d97bc12b
DE
13357 dump_die_shallow (gdb_stderr, 0, die);
13358}
13359
13360static void
13361dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13362{
13363 int indent = level * 4;
13364
13365 gdb_assert (die != NULL);
13366
13367 if (level >= max_level)
13368 return;
13369
13370 dump_die_shallow (f, indent, die);
13371
13372 if (die->child != NULL)
c906108c 13373 {
d97bc12b
DE
13374 print_spaces (indent, f);
13375 fprintf_unfiltered (f, " Children:");
13376 if (level + 1 < max_level)
13377 {
13378 fprintf_unfiltered (f, "\n");
13379 dump_die_1 (f, level + 1, max_level, die->child);
13380 }
13381 else
13382 {
3e43a32a
MS
13383 fprintf_unfiltered (f,
13384 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13385 }
13386 }
13387
13388 if (die->sibling != NULL && level > 0)
13389 {
13390 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13391 }
13392}
13393
d97bc12b
DE
13394/* This is called from the pdie macro in gdbinit.in.
13395 It's not static so gcc will keep a copy callable from gdb. */
13396
13397void
13398dump_die (struct die_info *die, int max_level)
13399{
13400 dump_die_1 (gdb_stdlog, 0, max_level, die);
13401}
13402
f9aca02d 13403static void
51545339 13404store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13405{
51545339 13406 void **slot;
c906108c 13407
51545339
DJ
13408 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13409
13410 *slot = die;
c906108c
SS
13411}
13412
93311388
DE
13413static int
13414is_ref_attr (struct attribute *attr)
c906108c 13415{
c906108c
SS
13416 switch (attr->form)
13417 {
13418 case DW_FORM_ref_addr:
c906108c
SS
13419 case DW_FORM_ref1:
13420 case DW_FORM_ref2:
13421 case DW_FORM_ref4:
613e1657 13422 case DW_FORM_ref8:
c906108c 13423 case DW_FORM_ref_udata:
93311388 13424 return 1;
c906108c 13425 default:
93311388 13426 return 0;
c906108c 13427 }
93311388
DE
13428}
13429
13430static unsigned int
13431dwarf2_get_ref_die_offset (struct attribute *attr)
13432{
13433 if (is_ref_attr (attr))
13434 return DW_ADDR (attr);
13435
13436 complaint (&symfile_complaints,
13437 _("unsupported die ref attribute form: '%s'"),
13438 dwarf_form_name (attr->form));
13439 return 0;
c906108c
SS
13440}
13441
43bbcdc2
PH
13442/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13443 * the value held by the attribute is not constant. */
a02abb62 13444
43bbcdc2 13445static LONGEST
a02abb62
JB
13446dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13447{
13448 if (attr->form == DW_FORM_sdata)
13449 return DW_SND (attr);
13450 else if (attr->form == DW_FORM_udata
13451 || attr->form == DW_FORM_data1
13452 || attr->form == DW_FORM_data2
13453 || attr->form == DW_FORM_data4
13454 || attr->form == DW_FORM_data8)
13455 return DW_UNSND (attr);
13456 else
13457 {
3e43a32a
MS
13458 complaint (&symfile_complaints,
13459 _("Attribute value is not a constant (%s)"),
a02abb62
JB
13460 dwarf_form_name (attr->form));
13461 return default_value;
13462 }
13463}
13464
03dd20cc 13465/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
13466 unit and add it to our queue.
13467 The result is non-zero if PER_CU was queued, otherwise the result is zero
13468 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 13469
348e048f 13470static int
03dd20cc
DJ
13471maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13472 struct dwarf2_per_cu_data *per_cu)
13473{
98bfdba5
PA
13474 /* We may arrive here during partial symbol reading, if we need full
13475 DIEs to process an unusual case (e.g. template arguments). Do
13476 not queue PER_CU, just tell our caller to load its DIEs. */
13477 if (dwarf2_per_objfile->reading_partial_symbols)
13478 {
13479 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13480 return 1;
13481 return 0;
13482 }
13483
03dd20cc
DJ
13484 /* Mark the dependence relation so that we don't flush PER_CU
13485 too early. */
13486 dwarf2_add_dependence (this_cu, per_cu);
13487
13488 /* If it's already on the queue, we have nothing to do. */
13489 if (per_cu->queued)
348e048f 13490 return 0;
03dd20cc
DJ
13491
13492 /* If the compilation unit is already loaded, just mark it as
13493 used. */
13494 if (per_cu->cu != NULL)
13495 {
13496 per_cu->cu->last_used = 0;
348e048f 13497 return 0;
03dd20cc
DJ
13498 }
13499
13500 /* Add it to the queue. */
13501 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
13502
13503 return 1;
13504}
13505
13506/* Follow reference or signature attribute ATTR of SRC_DIE.
13507 On entry *REF_CU is the CU of SRC_DIE.
13508 On exit *REF_CU is the CU of the result. */
13509
13510static struct die_info *
13511follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13512 struct dwarf2_cu **ref_cu)
13513{
13514 struct die_info *die;
13515
13516 if (is_ref_attr (attr))
13517 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 13518 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
13519 die = follow_die_sig (src_die, attr, ref_cu);
13520 else
13521 {
13522 dump_die_for_error (src_die);
13523 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13524 (*ref_cu)->objfile->name);
13525 }
13526
13527 return die;
03dd20cc
DJ
13528}
13529
5c631832 13530/* Follow reference OFFSET.
673bfd45
DE
13531 On entry *REF_CU is the CU of the source die referencing OFFSET.
13532 On exit *REF_CU is the CU of the result.
13533 Returns NULL if OFFSET is invalid. */
f504f079 13534
f9aca02d 13535static struct die_info *
5c631832 13536follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13537{
10b3939b 13538 struct die_info temp_die;
f2f0e013 13539 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13540
348e048f
DE
13541 gdb_assert (cu->per_cu != NULL);
13542
98bfdba5
PA
13543 target_cu = cu;
13544
348e048f
DE
13545 if (cu->per_cu->from_debug_types)
13546 {
13547 /* .debug_types CUs cannot reference anything outside their CU.
13548 If they need to, they have to reference a signatured type via
55f1336d 13549 DW_FORM_ref_sig8. */
348e048f 13550 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13551 return NULL;
348e048f
DE
13552 }
13553 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13554 {
13555 struct dwarf2_per_cu_data *per_cu;
9a619af0 13556
45452591 13557 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13558
13559 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13560 if (maybe_queue_comp_unit (cu, per_cu))
13561 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13562
10b3939b
DJ
13563 target_cu = per_cu->cu;
13564 }
98bfdba5
PA
13565 else if (cu->dies == NULL)
13566 {
13567 /* We're loading full DIEs during partial symbol reading. */
13568 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13569 load_full_comp_unit (cu->per_cu, cu->objfile);
13570 }
c906108c 13571
f2f0e013 13572 *ref_cu = target_cu;
51545339 13573 temp_die.offset = offset;
5c631832
JK
13574 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13575}
10b3939b 13576
5c631832
JK
13577/* Follow reference attribute ATTR of SRC_DIE.
13578 On entry *REF_CU is the CU of SRC_DIE.
13579 On exit *REF_CU is the CU of the result. */
13580
13581static struct die_info *
13582follow_die_ref (struct die_info *src_die, struct attribute *attr,
13583 struct dwarf2_cu **ref_cu)
13584{
13585 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13586 struct dwarf2_cu *cu = *ref_cu;
13587 struct die_info *die;
13588
13589 die = follow_die_offset (offset, ref_cu);
13590 if (!die)
13591 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13592 "at 0x%x [in module %s]"),
13593 offset, src_die->offset, cu->objfile->name);
348e048f 13594
5c631832
JK
13595 return die;
13596}
13597
13598/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13599 value is intended for DW_OP_call*. */
13600
13601struct dwarf2_locexpr_baton
13602dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
13603 struct dwarf2_per_cu_data *per_cu,
13604 CORE_ADDR (*get_frame_pc) (void *baton),
13605 void *baton)
5c631832
JK
13606{
13607 struct dwarf2_cu *cu = per_cu->cu;
13608 struct die_info *die;
13609 struct attribute *attr;
13610 struct dwarf2_locexpr_baton retval;
13611
8cf6f0b1
TT
13612 dw2_setup (per_cu->objfile);
13613
5c631832
JK
13614 die = follow_die_offset (offset, &cu);
13615 if (!die)
13616 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13617 offset, per_cu->cu->objfile->name);
13618
13619 attr = dwarf2_attr (die, DW_AT_location, cu);
13620 if (!attr)
13621 {
13622 /* DWARF: "If there is no such attribute, then there is no effect.". */
13623
13624 retval.data = NULL;
13625 retval.size = 0;
13626 }
8cf6f0b1
TT
13627 else if (attr_form_is_section_offset (attr))
13628 {
13629 struct dwarf2_loclist_baton loclist_baton;
13630 CORE_ADDR pc = (*get_frame_pc) (baton);
13631 size_t size;
13632
13633 fill_in_loclist_baton (cu, &loclist_baton, attr);
13634
13635 retval.data = dwarf2_find_location_expression (&loclist_baton,
13636 &size, pc);
13637 retval.size = size;
13638 }
5c631832
JK
13639 else
13640 {
13641 if (!attr_form_is_block (attr))
13642 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13643 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13644 offset, per_cu->cu->objfile->name);
13645
13646 retval.data = DW_BLOCK (attr)->data;
13647 retval.size = DW_BLOCK (attr)->size;
13648 }
13649 retval.per_cu = cu->per_cu;
13650 return retval;
348e048f
DE
13651}
13652
13653/* Follow the signature attribute ATTR in SRC_DIE.
13654 On entry *REF_CU is the CU of SRC_DIE.
13655 On exit *REF_CU is the CU of the result. */
13656
13657static struct die_info *
13658follow_die_sig (struct die_info *src_die, struct attribute *attr,
13659 struct dwarf2_cu **ref_cu)
13660{
13661 struct objfile *objfile = (*ref_cu)->objfile;
13662 struct die_info temp_die;
13663 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13664 struct dwarf2_cu *sig_cu;
13665 struct die_info *die;
13666
13667 /* sig_type will be NULL if the signatured type is missing from
13668 the debug info. */
13669 if (sig_type == NULL)
13670 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13671 "at 0x%x [in module %s]"),
13672 src_die->offset, objfile->name);
13673
13674 /* If necessary, add it to the queue and load its DIEs. */
13675
13676 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13677 read_signatured_type (objfile, sig_type);
13678
13679 gdb_assert (sig_type->per_cu.cu != NULL);
13680
13681 sig_cu = sig_type->per_cu.cu;
13682 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13683 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13684 if (die)
13685 {
13686 *ref_cu = sig_cu;
13687 return die;
13688 }
13689
3e43a32a
MS
13690 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13691 "from DIE at 0x%x [in module %s]"),
348e048f
DE
13692 sig_type->type_offset, src_die->offset, objfile->name);
13693}
13694
13695/* Given an offset of a signatured type, return its signatured_type. */
13696
13697static struct signatured_type *
13698lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13699{
13700 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13701 unsigned int length, initial_length_size;
13702 unsigned int sig_offset;
13703 struct signatured_type find_entry, *type_sig;
13704
13705 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13706 sig_offset = (initial_length_size
13707 + 2 /*version*/
13708 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13709 + 1 /*address_size*/);
13710 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13711 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13712
13713 /* This is only used to lookup previously recorded types.
13714 If we didn't find it, it's our bug. */
13715 gdb_assert (type_sig != NULL);
13716 gdb_assert (offset == type_sig->offset);
13717
13718 return type_sig;
13719}
13720
13721/* Read in signatured type at OFFSET and build its CU and die(s). */
13722
13723static void
13724read_signatured_type_at_offset (struct objfile *objfile,
13725 unsigned int offset)
13726{
13727 struct signatured_type *type_sig;
13728
be391dca
TT
13729 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13730
348e048f
DE
13731 /* We have the section offset, but we need the signature to do the
13732 hash table lookup. */
13733 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13734
13735 gdb_assert (type_sig->per_cu.cu == NULL);
13736
13737 read_signatured_type (objfile, type_sig);
13738
13739 gdb_assert (type_sig->per_cu.cu != NULL);
13740}
13741
13742/* Read in a signatured type and build its CU and DIEs. */
13743
13744static void
13745read_signatured_type (struct objfile *objfile,
13746 struct signatured_type *type_sig)
13747{
1fd400ff 13748 gdb_byte *types_ptr;
348e048f
DE
13749 struct die_reader_specs reader_specs;
13750 struct dwarf2_cu *cu;
13751 ULONGEST signature;
13752 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13753
1fd400ff
TT
13754 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13755 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13756
348e048f
DE
13757 gdb_assert (type_sig->per_cu.cu == NULL);
13758
9816fde3
JK
13759 cu = xmalloc (sizeof (*cu));
13760 init_one_comp_unit (cu, objfile);
13761
348e048f
DE
13762 type_sig->per_cu.cu = cu;
13763 cu->per_cu = &type_sig->per_cu;
13764
13765 /* If an error occurs while loading, release our storage. */
13766 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13767
13768 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13769 types_ptr, objfile->obfd);
13770 gdb_assert (signature == type_sig->signature);
13771
13772 cu->die_hash
13773 = htab_create_alloc_ex (cu->header.length / 12,
13774 die_hash,
13775 die_eq,
13776 NULL,
13777 &cu->comp_unit_obstack,
13778 hashtab_obstack_allocate,
13779 dummy_obstack_deallocate);
13780
13781 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13782 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13783
13784 init_cu_die_reader (&reader_specs, cu);
13785
13786 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13787 NULL /*parent*/);
13788
13789 /* We try not to read any attributes in this function, because not
13790 all objfiles needed for references have been loaded yet, and symbol
13791 table processing isn't initialized. But we have to set the CU language,
13792 or we won't be able to build types correctly. */
9816fde3 13793 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
13794
13795 do_cleanups (back_to);
13796
13797 /* We've successfully allocated this compilation unit. Let our caller
13798 clean it up when finished with it. */
13799 discard_cleanups (free_cu_cleanup);
13800
13801 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13802 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13803}
13804
c906108c
SS
13805/* Decode simple location descriptions.
13806 Given a pointer to a dwarf block that defines a location, compute
13807 the location and return the value.
13808
4cecd739
DJ
13809 NOTE drow/2003-11-18: This function is called in two situations
13810 now: for the address of static or global variables (partial symbols
13811 only) and for offsets into structures which are expected to be
13812 (more or less) constant. The partial symbol case should go away,
13813 and only the constant case should remain. That will let this
13814 function complain more accurately. A few special modes are allowed
13815 without complaint for global variables (for instance, global
13816 register values and thread-local values).
c906108c
SS
13817
13818 A location description containing no operations indicates that the
4cecd739 13819 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13820 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13821 callers will only want a very basic result and this can become a
13822 complaint.
c906108c 13823
d53d4ac5 13824 Note that stack[0] is unused except as a default error return. */
c906108c
SS
13825
13826static CORE_ADDR
e7c27a73 13827decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13828{
e7c27a73 13829 struct objfile *objfile = cu->objfile;
c906108c
SS
13830 int i;
13831 int size = blk->size;
fe1b8b76 13832 gdb_byte *data = blk->data;
c906108c
SS
13833 CORE_ADDR stack[64];
13834 int stacki;
13835 unsigned int bytes_read, unsnd;
fe1b8b76 13836 gdb_byte op;
c906108c
SS
13837
13838 i = 0;
13839 stacki = 0;
13840 stack[stacki] = 0;
d53d4ac5 13841 stack[++stacki] = 0;
c906108c
SS
13842
13843 while (i < size)
13844 {
c906108c
SS
13845 op = data[i++];
13846 switch (op)
13847 {
f1bea926
JM
13848 case DW_OP_lit0:
13849 case DW_OP_lit1:
13850 case DW_OP_lit2:
13851 case DW_OP_lit3:
13852 case DW_OP_lit4:
13853 case DW_OP_lit5:
13854 case DW_OP_lit6:
13855 case DW_OP_lit7:
13856 case DW_OP_lit8:
13857 case DW_OP_lit9:
13858 case DW_OP_lit10:
13859 case DW_OP_lit11:
13860 case DW_OP_lit12:
13861 case DW_OP_lit13:
13862 case DW_OP_lit14:
13863 case DW_OP_lit15:
13864 case DW_OP_lit16:
13865 case DW_OP_lit17:
13866 case DW_OP_lit18:
13867 case DW_OP_lit19:
13868 case DW_OP_lit20:
13869 case DW_OP_lit21:
13870 case DW_OP_lit22:
13871 case DW_OP_lit23:
13872 case DW_OP_lit24:
13873 case DW_OP_lit25:
13874 case DW_OP_lit26:
13875 case DW_OP_lit27:
13876 case DW_OP_lit28:
13877 case DW_OP_lit29:
13878 case DW_OP_lit30:
13879 case DW_OP_lit31:
13880 stack[++stacki] = op - DW_OP_lit0;
13881 break;
13882
c906108c
SS
13883 case DW_OP_reg0:
13884 case DW_OP_reg1:
13885 case DW_OP_reg2:
13886 case DW_OP_reg3:
13887 case DW_OP_reg4:
13888 case DW_OP_reg5:
13889 case DW_OP_reg6:
13890 case DW_OP_reg7:
13891 case DW_OP_reg8:
13892 case DW_OP_reg9:
13893 case DW_OP_reg10:
13894 case DW_OP_reg11:
13895 case DW_OP_reg12:
13896 case DW_OP_reg13:
13897 case DW_OP_reg14:
13898 case DW_OP_reg15:
13899 case DW_OP_reg16:
13900 case DW_OP_reg17:
13901 case DW_OP_reg18:
13902 case DW_OP_reg19:
13903 case DW_OP_reg20:
13904 case DW_OP_reg21:
13905 case DW_OP_reg22:
13906 case DW_OP_reg23:
13907 case DW_OP_reg24:
13908 case DW_OP_reg25:
13909 case DW_OP_reg26:
13910 case DW_OP_reg27:
13911 case DW_OP_reg28:
13912 case DW_OP_reg29:
13913 case DW_OP_reg30:
13914 case DW_OP_reg31:
c906108c 13915 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13916 if (i < size)
13917 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13918 break;
13919
13920 case DW_OP_regx:
c906108c
SS
13921 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13922 i += bytes_read;
c906108c 13923 stack[++stacki] = unsnd;
4cecd739
DJ
13924 if (i < size)
13925 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13926 break;
13927
13928 case DW_OP_addr:
107d2387 13929 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13930 cu, &bytes_read);
107d2387 13931 i += bytes_read;
c906108c
SS
13932 break;
13933
13934 case DW_OP_const1u:
13935 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13936 i += 1;
13937 break;
13938
13939 case DW_OP_const1s:
13940 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13941 i += 1;
13942 break;
13943
13944 case DW_OP_const2u:
13945 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13946 i += 2;
13947 break;
13948
13949 case DW_OP_const2s:
13950 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13951 i += 2;
13952 break;
13953
13954 case DW_OP_const4u:
13955 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13956 i += 4;
13957 break;
13958
13959 case DW_OP_const4s:
13960 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13961 i += 4;
13962 break;
13963
13964 case DW_OP_constu:
13965 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13966 &bytes_read);
c906108c
SS
13967 i += bytes_read;
13968 break;
13969
13970 case DW_OP_consts:
13971 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13972 i += bytes_read;
13973 break;
13974
f1bea926
JM
13975 case DW_OP_dup:
13976 stack[stacki + 1] = stack[stacki];
13977 stacki++;
13978 break;
13979
c906108c
SS
13980 case DW_OP_plus:
13981 stack[stacki - 1] += stack[stacki];
13982 stacki--;
13983 break;
13984
13985 case DW_OP_plus_uconst:
3e43a32a
MS
13986 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
13987 &bytes_read);
c906108c
SS
13988 i += bytes_read;
13989 break;
13990
13991 case DW_OP_minus:
f1bea926 13992 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13993 stacki--;
13994 break;
13995
7a292a7a 13996 case DW_OP_deref:
7a292a7a 13997 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13998 this using GDB's address_class enum. This is valid for partial
13999 global symbols, although the variable's address will be bogus
14000 in the psymtab. */
7a292a7a 14001 if (i < size)
4d3c2250 14002 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
14003 break;
14004
9d774e44 14005 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
14006 /* The top of the stack has the offset from the beginning
14007 of the thread control block at which the variable is located. */
14008 /* Nothing should follow this operator, so the top of stack would
14009 be returned. */
4cecd739
DJ
14010 /* This is valid for partial global symbols, but the variable's
14011 address will be bogus in the psymtab. */
9d774e44 14012 if (i < size)
4d3c2250 14013 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
14014 break;
14015
42be36b3
CT
14016 case DW_OP_GNU_uninit:
14017 break;
14018
c906108c 14019 default:
b1bfef65
TT
14020 {
14021 const char *name = dwarf_stack_op_name (op);
14022
14023 if (name)
14024 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14025 name);
14026 else
14027 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14028 op);
14029 }
14030
c906108c
SS
14031 return (stack[stacki]);
14032 }
d53d4ac5
TT
14033
14034 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14035 outside of the allocated space. Also enforce minimum>0. */
14036 if (stacki >= ARRAY_SIZE (stack) - 1)
14037 {
14038 complaint (&symfile_complaints,
14039 _("location description stack overflow"));
14040 return 0;
14041 }
14042
14043 if (stacki <= 0)
14044 {
14045 complaint (&symfile_complaints,
14046 _("location description stack underflow"));
14047 return 0;
14048 }
c906108c
SS
14049 }
14050 return (stack[stacki]);
14051}
14052
14053/* memory allocation interface */
14054
c906108c 14055static struct dwarf_block *
7b5a2f43 14056dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14057{
14058 struct dwarf_block *blk;
14059
14060 blk = (struct dwarf_block *)
7b5a2f43 14061 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14062 return (blk);
14063}
14064
14065static struct abbrev_info *
f3dd6933 14066dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14067{
14068 struct abbrev_info *abbrev;
14069
f3dd6933
DJ
14070 abbrev = (struct abbrev_info *)
14071 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14072 memset (abbrev, 0, sizeof (struct abbrev_info));
14073 return (abbrev);
14074}
14075
14076static struct die_info *
b60c80d6 14077dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14078{
14079 struct die_info *die;
b60c80d6
DJ
14080 size_t size = sizeof (struct die_info);
14081
14082 if (num_attrs > 1)
14083 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14084
b60c80d6 14085 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14086 memset (die, 0, sizeof (struct die_info));
14087 return (die);
14088}
2e276125
JB
14089
14090\f
14091/* Macro support. */
14092
2e276125
JB
14093/* Return the full name of file number I in *LH's file name table.
14094 Use COMP_DIR as the name of the current directory of the
14095 compilation. The result is allocated using xmalloc; the caller is
14096 responsible for freeing it. */
14097static char *
14098file_full_name (int file, struct line_header *lh, const char *comp_dir)
14099{
6a83a1e6
EZ
14100 /* Is the file number a valid index into the line header's file name
14101 table? Remember that file numbers start with one, not zero. */
14102 if (1 <= file && file <= lh->num_file_names)
14103 {
14104 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14105
6a83a1e6
EZ
14106 if (IS_ABSOLUTE_PATH (fe->name))
14107 return xstrdup (fe->name);
14108 else
14109 {
14110 const char *dir;
14111 int dir_len;
14112 char *full_name;
14113
14114 if (fe->dir_index)
14115 dir = lh->include_dirs[fe->dir_index - 1];
14116 else
14117 dir = comp_dir;
14118
14119 if (dir)
14120 {
14121 dir_len = strlen (dir);
14122 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14123 strcpy (full_name, dir);
14124 full_name[dir_len] = '/';
14125 strcpy (full_name + dir_len + 1, fe->name);
14126 return full_name;
14127 }
14128 else
14129 return xstrdup (fe->name);
14130 }
14131 }
2e276125
JB
14132 else
14133 {
6a83a1e6
EZ
14134 /* The compiler produced a bogus file number. We can at least
14135 record the macro definitions made in the file, even if we
14136 won't be able to find the file by name. */
14137 char fake_name[80];
9a619af0 14138
6a83a1e6 14139 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14140
6e70227d 14141 complaint (&symfile_complaints,
6a83a1e6
EZ
14142 _("bad file number in macro information (%d)"),
14143 file);
2e276125 14144
6a83a1e6 14145 return xstrdup (fake_name);
2e276125
JB
14146 }
14147}
14148
14149
14150static struct macro_source_file *
14151macro_start_file (int file, int line,
14152 struct macro_source_file *current_file,
14153 const char *comp_dir,
14154 struct line_header *lh, struct objfile *objfile)
14155{
14156 /* The full name of this source file. */
14157 char *full_name = file_full_name (file, lh, comp_dir);
14158
14159 /* We don't create a macro table for this compilation unit
14160 at all until we actually get a filename. */
14161 if (! pending_macros)
4a146b47 14162 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14163 objfile->macro_cache);
2e276125
JB
14164
14165 if (! current_file)
14166 /* If we have no current file, then this must be the start_file
14167 directive for the compilation unit's main source file. */
14168 current_file = macro_set_main (pending_macros, full_name);
14169 else
14170 current_file = macro_include (current_file, line, full_name);
14171
14172 xfree (full_name);
6e70227d 14173
2e276125
JB
14174 return current_file;
14175}
14176
14177
14178/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14179 followed by a null byte. */
14180static char *
14181copy_string (const char *buf, int len)
14182{
14183 char *s = xmalloc (len + 1);
9a619af0 14184
2e276125
JB
14185 memcpy (s, buf, len);
14186 s[len] = '\0';
2e276125
JB
14187 return s;
14188}
14189
14190
14191static const char *
14192consume_improper_spaces (const char *p, const char *body)
14193{
14194 if (*p == ' ')
14195 {
4d3c2250 14196 complaint (&symfile_complaints,
3e43a32a
MS
14197 _("macro definition contains spaces "
14198 "in formal argument list:\n`%s'"),
4d3c2250 14199 body);
2e276125
JB
14200
14201 while (*p == ' ')
14202 p++;
14203 }
14204
14205 return p;
14206}
14207
14208
14209static void
14210parse_macro_definition (struct macro_source_file *file, int line,
14211 const char *body)
14212{
14213 const char *p;
14214
14215 /* The body string takes one of two forms. For object-like macro
14216 definitions, it should be:
14217
14218 <macro name> " " <definition>
14219
14220 For function-like macro definitions, it should be:
14221
14222 <macro name> "() " <definition>
14223 or
14224 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14225
14226 Spaces may appear only where explicitly indicated, and in the
14227 <definition>.
14228
14229 The Dwarf 2 spec says that an object-like macro's name is always
14230 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14231 the space when the macro's definition is the empty string.
2e276125
JB
14232
14233 The Dwarf 2 spec says that there should be no spaces between the
14234 formal arguments in a function-like macro's formal argument list,
14235 but versions of GCC around March 2002 include spaces after the
14236 commas. */
14237
14238
14239 /* Find the extent of the macro name. The macro name is terminated
14240 by either a space or null character (for an object-like macro) or
14241 an opening paren (for a function-like macro). */
14242 for (p = body; *p; p++)
14243 if (*p == ' ' || *p == '(')
14244 break;
14245
14246 if (*p == ' ' || *p == '\0')
14247 {
14248 /* It's an object-like macro. */
14249 int name_len = p - body;
14250 char *name = copy_string (body, name_len);
14251 const char *replacement;
14252
14253 if (*p == ' ')
14254 replacement = body + name_len + 1;
14255 else
14256 {
4d3c2250 14257 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14258 replacement = body + name_len;
14259 }
6e70227d 14260
2e276125
JB
14261 macro_define_object (file, line, name, replacement);
14262
14263 xfree (name);
14264 }
14265 else if (*p == '(')
14266 {
14267 /* It's a function-like macro. */
14268 char *name = copy_string (body, p - body);
14269 int argc = 0;
14270 int argv_size = 1;
14271 char **argv = xmalloc (argv_size * sizeof (*argv));
14272
14273 p++;
14274
14275 p = consume_improper_spaces (p, body);
14276
14277 /* Parse the formal argument list. */
14278 while (*p && *p != ')')
14279 {
14280 /* Find the extent of the current argument name. */
14281 const char *arg_start = p;
14282
14283 while (*p && *p != ',' && *p != ')' && *p != ' ')
14284 p++;
14285
14286 if (! *p || p == arg_start)
4d3c2250 14287 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14288 else
14289 {
14290 /* Make sure argv has room for the new argument. */
14291 if (argc >= argv_size)
14292 {
14293 argv_size *= 2;
14294 argv = xrealloc (argv, argv_size * sizeof (*argv));
14295 }
14296
14297 argv[argc++] = copy_string (arg_start, p - arg_start);
14298 }
14299
14300 p = consume_improper_spaces (p, body);
14301
14302 /* Consume the comma, if present. */
14303 if (*p == ',')
14304 {
14305 p++;
14306
14307 p = consume_improper_spaces (p, body);
14308 }
14309 }
14310
14311 if (*p == ')')
14312 {
14313 p++;
14314
14315 if (*p == ' ')
14316 /* Perfectly formed definition, no complaints. */
14317 macro_define_function (file, line, name,
6e70227d 14318 argc, (const char **) argv,
2e276125
JB
14319 p + 1);
14320 else if (*p == '\0')
14321 {
14322 /* Complain, but do define it. */
4d3c2250 14323 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14324 macro_define_function (file, line, name,
6e70227d 14325 argc, (const char **) argv,
2e276125
JB
14326 p);
14327 }
14328 else
14329 /* Just complain. */
4d3c2250 14330 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14331 }
14332 else
14333 /* Just complain. */
4d3c2250 14334 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14335
14336 xfree (name);
14337 {
14338 int i;
14339
14340 for (i = 0; i < argc; i++)
14341 xfree (argv[i]);
14342 }
14343 xfree (argv);
14344 }
14345 else
4d3c2250 14346 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14347}
14348
14349
14350static void
14351dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14352 char *comp_dir, bfd *abfd,
e7c27a73 14353 struct dwarf2_cu *cu)
2e276125 14354{
fe1b8b76 14355 gdb_byte *mac_ptr, *mac_end;
2e276125 14356 struct macro_source_file *current_file = 0;
757a13d0
JK
14357 enum dwarf_macinfo_record_type macinfo_type;
14358 int at_commandline;
2e276125 14359
be391dca
TT
14360 dwarf2_read_section (dwarf2_per_objfile->objfile,
14361 &dwarf2_per_objfile->macinfo);
dce234bc 14362 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 14363 {
e2e0b3e5 14364 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
14365 return;
14366 }
14367
757a13d0
JK
14368 /* First pass: Find the name of the base filename.
14369 This filename is needed in order to process all macros whose definition
14370 (or undefinition) comes from the command line. These macros are defined
14371 before the first DW_MACINFO_start_file entry, and yet still need to be
14372 associated to the base file.
14373
14374 To determine the base file name, we scan the macro definitions until we
14375 reach the first DW_MACINFO_start_file entry. We then initialize
14376 CURRENT_FILE accordingly so that any macro definition found before the
14377 first DW_MACINFO_start_file can still be associated to the base file. */
14378
dce234bc
PP
14379 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14380 mac_end = dwarf2_per_objfile->macinfo.buffer
14381 + dwarf2_per_objfile->macinfo.size;
2e276125 14382
757a13d0 14383 do
2e276125 14384 {
2e276125
JB
14385 /* Do we at least have room for a macinfo type byte? */
14386 if (mac_ptr >= mac_end)
14387 {
757a13d0 14388 /* Complaint is printed during the second pass as GDB will probably
3e43a32a
MS
14389 stop the first pass earlier upon finding
14390 DW_MACINFO_start_file. */
757a13d0 14391 break;
2e276125
JB
14392 }
14393
14394 macinfo_type = read_1_byte (abfd, mac_ptr);
14395 mac_ptr++;
14396
14397 switch (macinfo_type)
14398 {
14399 /* A zero macinfo type indicates the end of the macro
14400 information. */
14401 case 0:
757a13d0
JK
14402 break;
14403
14404 case DW_MACINFO_define:
14405 case DW_MACINFO_undef:
14406 /* Only skip the data by MAC_PTR. */
14407 {
14408 unsigned int bytes_read;
14409
14410 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14411 mac_ptr += bytes_read;
9b1c24c8 14412 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14413 mac_ptr += bytes_read;
14414 }
14415 break;
14416
14417 case DW_MACINFO_start_file:
14418 {
14419 unsigned int bytes_read;
14420 int line, file;
14421
14422 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14423 mac_ptr += bytes_read;
14424 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14425 mac_ptr += bytes_read;
14426
3e43a32a
MS
14427 current_file = macro_start_file (file, line, current_file,
14428 comp_dir, lh, cu->objfile);
757a13d0
JK
14429 }
14430 break;
14431
14432 case DW_MACINFO_end_file:
14433 /* No data to skip by MAC_PTR. */
14434 break;
14435
14436 case DW_MACINFO_vendor_ext:
14437 /* Only skip the data by MAC_PTR. */
14438 {
14439 unsigned int bytes_read;
14440
14441 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14442 mac_ptr += bytes_read;
9b1c24c8 14443 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14444 mac_ptr += bytes_read;
14445 }
14446 break;
14447
14448 default:
14449 break;
14450 }
14451 } while (macinfo_type != 0 && current_file == NULL);
14452
14453 /* Second pass: Process all entries.
14454
14455 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14456 command-line macro definitions/undefinitions. This flag is unset when we
14457 reach the first DW_MACINFO_start_file entry. */
14458
dce234bc 14459 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
14460
14461 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14462 GDB is still reading the definitions from command line. First
14463 DW_MACINFO_start_file will need to be ignored as it was already executed
14464 to create CURRENT_FILE for the main source holding also the command line
14465 definitions. On first met DW_MACINFO_start_file this flag is reset to
14466 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14467
14468 at_commandline = 1;
14469
14470 do
14471 {
14472 /* Do we at least have room for a macinfo type byte? */
14473 if (mac_ptr >= mac_end)
14474 {
14475 dwarf2_macros_too_long_complaint ();
14476 break;
14477 }
14478
14479 macinfo_type = read_1_byte (abfd, mac_ptr);
14480 mac_ptr++;
14481
14482 switch (macinfo_type)
14483 {
14484 /* A zero macinfo type indicates the end of the macro
14485 information. */
14486 case 0:
14487 break;
2e276125
JB
14488
14489 case DW_MACINFO_define:
14490 case DW_MACINFO_undef:
14491 {
891d2f0b 14492 unsigned int bytes_read;
2e276125
JB
14493 int line;
14494 char *body;
14495
14496 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14497 mac_ptr += bytes_read;
9b1c24c8 14498 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14499 mac_ptr += bytes_read;
14500
14501 if (! current_file)
757a13d0
JK
14502 {
14503 /* DWARF violation as no main source is present. */
14504 complaint (&symfile_complaints,
14505 _("debug info with no main source gives macro %s "
14506 "on line %d: %s"),
6e70227d
DE
14507 macinfo_type == DW_MACINFO_define ?
14508 _("definition") :
905e0470
PM
14509 macinfo_type == DW_MACINFO_undef ?
14510 _("undefinition") :
14511 _("something-or-other"), line, body);
757a13d0
JK
14512 break;
14513 }
3e43a32a
MS
14514 if ((line == 0 && !at_commandline)
14515 || (line != 0 && at_commandline))
4d3c2250 14516 complaint (&symfile_complaints,
757a13d0
JK
14517 _("debug info gives %s macro %s with %s line %d: %s"),
14518 at_commandline ? _("command-line") : _("in-file"),
905e0470 14519 macinfo_type == DW_MACINFO_define ?
6e70227d 14520 _("definition") :
905e0470
PM
14521 macinfo_type == DW_MACINFO_undef ?
14522 _("undefinition") :
14523 _("something-or-other"),
757a13d0
JK
14524 line == 0 ? _("zero") : _("non-zero"), line, body);
14525
14526 if (macinfo_type == DW_MACINFO_define)
14527 parse_macro_definition (current_file, line, body);
14528 else if (macinfo_type == DW_MACINFO_undef)
14529 macro_undef (current_file, line, body);
2e276125
JB
14530 }
14531 break;
14532
14533 case DW_MACINFO_start_file:
14534 {
891d2f0b 14535 unsigned int bytes_read;
2e276125
JB
14536 int line, file;
14537
14538 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14539 mac_ptr += bytes_read;
14540 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14541 mac_ptr += bytes_read;
14542
3e43a32a
MS
14543 if ((line == 0 && !at_commandline)
14544 || (line != 0 && at_commandline))
757a13d0
JK
14545 complaint (&symfile_complaints,
14546 _("debug info gives source %d included "
14547 "from %s at %s line %d"),
14548 file, at_commandline ? _("command-line") : _("file"),
14549 line == 0 ? _("zero") : _("non-zero"), line);
14550
14551 if (at_commandline)
14552 {
14553 /* This DW_MACINFO_start_file was executed in the pass one. */
14554 at_commandline = 0;
14555 }
14556 else
14557 current_file = macro_start_file (file, line,
14558 current_file, comp_dir,
14559 lh, cu->objfile);
2e276125
JB
14560 }
14561 break;
14562
14563 case DW_MACINFO_end_file:
14564 if (! current_file)
4d3c2250 14565 complaint (&symfile_complaints,
3e43a32a
MS
14566 _("macro debug info has an unmatched "
14567 "`close_file' directive"));
2e276125
JB
14568 else
14569 {
14570 current_file = current_file->included_by;
14571 if (! current_file)
14572 {
14573 enum dwarf_macinfo_record_type next_type;
14574
14575 /* GCC circa March 2002 doesn't produce the zero
14576 type byte marking the end of the compilation
14577 unit. Complain if it's not there, but exit no
14578 matter what. */
14579
14580 /* Do we at least have room for a macinfo type byte? */
14581 if (mac_ptr >= mac_end)
14582 {
4d3c2250 14583 dwarf2_macros_too_long_complaint ();
2e276125
JB
14584 return;
14585 }
14586
14587 /* We don't increment mac_ptr here, so this is just
14588 a look-ahead. */
14589 next_type = read_1_byte (abfd, mac_ptr);
14590 if (next_type != 0)
4d3c2250 14591 complaint (&symfile_complaints,
3e43a32a
MS
14592 _("no terminating 0-type entry for "
14593 "macros in `.debug_macinfo' section"));
2e276125
JB
14594
14595 return;
14596 }
14597 }
14598 break;
14599
14600 case DW_MACINFO_vendor_ext:
14601 {
891d2f0b 14602 unsigned int bytes_read;
2e276125 14603 int constant;
2e276125
JB
14604
14605 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14606 mac_ptr += bytes_read;
e8e80198 14607 read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14608 mac_ptr += bytes_read;
14609
14610 /* We don't recognize any vendor extensions. */
14611 }
14612 break;
14613 }
757a13d0 14614 } while (macinfo_type != 0);
2e276125 14615}
8e19ed76
PS
14616
14617/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 14618 if so return true else false. */
8e19ed76
PS
14619static int
14620attr_form_is_block (struct attribute *attr)
14621{
14622 return (attr == NULL ? 0 :
14623 attr->form == DW_FORM_block1
14624 || attr->form == DW_FORM_block2
14625 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14626 || attr->form == DW_FORM_block
14627 || attr->form == DW_FORM_exprloc);
8e19ed76 14628}
4c2df51b 14629
c6a0999f
JB
14630/* Return non-zero if ATTR's value is a section offset --- classes
14631 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14632 You may use DW_UNSND (attr) to retrieve such offsets.
14633
14634 Section 7.5.4, "Attribute Encodings", explains that no attribute
14635 may have a value that belongs to more than one of these classes; it
14636 would be ambiguous if we did, because we use the same forms for all
14637 of them. */
3690dd37
JB
14638static int
14639attr_form_is_section_offset (struct attribute *attr)
14640{
14641 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14642 || attr->form == DW_FORM_data8
14643 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14644}
14645
14646
14647/* Return non-zero if ATTR's value falls in the 'constant' class, or
14648 zero otherwise. When this function returns true, you can apply
14649 dwarf2_get_attr_constant_value to it.
14650
14651 However, note that for some attributes you must check
14652 attr_form_is_section_offset before using this test. DW_FORM_data4
14653 and DW_FORM_data8 are members of both the constant class, and of
14654 the classes that contain offsets into other debug sections
14655 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14656 that, if an attribute's can be either a constant or one of the
14657 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14658 taken as section offsets, not constants. */
14659static int
14660attr_form_is_constant (struct attribute *attr)
14661{
14662 switch (attr->form)
14663 {
14664 case DW_FORM_sdata:
14665 case DW_FORM_udata:
14666 case DW_FORM_data1:
14667 case DW_FORM_data2:
14668 case DW_FORM_data4:
14669 case DW_FORM_data8:
14670 return 1;
14671 default:
14672 return 0;
14673 }
14674}
14675
8cf6f0b1
TT
14676/* A helper function that fills in a dwarf2_loclist_baton. */
14677
14678static void
14679fill_in_loclist_baton (struct dwarf2_cu *cu,
14680 struct dwarf2_loclist_baton *baton,
14681 struct attribute *attr)
14682{
14683 dwarf2_read_section (dwarf2_per_objfile->objfile,
14684 &dwarf2_per_objfile->loc);
14685
14686 baton->per_cu = cu->per_cu;
14687 gdb_assert (baton->per_cu);
14688 /* We don't know how long the location list is, but make sure we
14689 don't run off the edge of the section. */
14690 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14691 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14692 baton->base_address = cu->base_address;
14693}
14694
4c2df51b
DJ
14695static void
14696dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14697 struct dwarf2_cu *cu)
4c2df51b 14698{
3690dd37 14699 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14700 /* ".debug_loc" may not exist at all, or the offset may be outside
14701 the section. If so, fall through to the complaint in the
14702 other branch. */
9e0ac564
TT
14703 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14704 &dwarf2_per_objfile->loc))
4c2df51b 14705 {
0d53c4c4 14706 struct dwarf2_loclist_baton *baton;
4c2df51b 14707
4a146b47 14708 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14709 sizeof (struct dwarf2_loclist_baton));
4c2df51b 14710
8cf6f0b1 14711 fill_in_loclist_baton (cu, baton, attr);
be391dca 14712
d00adf39 14713 if (cu->base_known == 0)
0d53c4c4 14714 complaint (&symfile_complaints,
3e43a32a
MS
14715 _("Location list used without "
14716 "specifying the CU base address."));
4c2df51b 14717
768a979c 14718 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14719 SYMBOL_LOCATION_BATON (sym) = baton;
14720 }
14721 else
14722 {
14723 struct dwarf2_locexpr_baton *baton;
14724
4a146b47 14725 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14726 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14727 baton->per_cu = cu->per_cu;
14728 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14729
14730 if (attr_form_is_block (attr))
14731 {
14732 /* Note that we're just copying the block's data pointer
14733 here, not the actual data. We're still pointing into the
6502dd73
DJ
14734 info_buffer for SYM's objfile; right now we never release
14735 that buffer, but when we do clean up properly this may
14736 need to change. */
0d53c4c4
DJ
14737 baton->size = DW_BLOCK (attr)->size;
14738 baton->data = DW_BLOCK (attr)->data;
14739 }
14740 else
14741 {
14742 dwarf2_invalid_attrib_class_complaint ("location description",
14743 SYMBOL_NATURAL_NAME (sym));
14744 baton->size = 0;
14745 baton->data = NULL;
14746 }
6e70227d 14747
768a979c 14748 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14749 SYMBOL_LOCATION_BATON (sym) = baton;
14750 }
4c2df51b 14751}
6502dd73 14752
9aa1f1e3
TT
14753/* Return the OBJFILE associated with the compilation unit CU. If CU
14754 came from a separate debuginfo file, then the master objfile is
14755 returned. */
ae0d2f24
UW
14756
14757struct objfile *
14758dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14759{
9291a0cd 14760 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14761
14762 /* Return the master objfile, so that we can report and look up the
14763 correct file containing this variable. */
14764 if (objfile->separate_debug_objfile_backlink)
14765 objfile = objfile->separate_debug_objfile_backlink;
14766
14767 return objfile;
14768}
14769
14770/* Return the address size given in the compilation unit header for CU. */
14771
14772CORE_ADDR
14773dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14774{
14775 if (per_cu->cu)
14776 return per_cu->cu->header.addr_size;
14777 else
14778 {
14779 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14780 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14781 struct dwarf2_per_objfile *per_objfile
14782 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14783 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14784 struct comp_unit_head cu_header;
9a619af0 14785
ae0d2f24
UW
14786 memset (&cu_header, 0, sizeof cu_header);
14787 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14788 return cu_header.addr_size;
14789 }
14790}
14791
9eae7c52
TT
14792/* Return the offset size given in the compilation unit header for CU. */
14793
14794int
14795dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14796{
14797 if (per_cu->cu)
14798 return per_cu->cu->header.offset_size;
14799 else
14800 {
14801 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14802 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14803 struct dwarf2_per_objfile *per_objfile
14804 = objfile_data (objfile, dwarf2_objfile_data_key);
14805 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14806 struct comp_unit_head cu_header;
14807
14808 memset (&cu_header, 0, sizeof cu_header);
14809 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14810 return cu_header.offset_size;
14811 }
14812}
14813
9aa1f1e3
TT
14814/* Return the text offset of the CU. The returned offset comes from
14815 this CU's objfile. If this objfile came from a separate debuginfo
14816 file, then the offset may be different from the corresponding
14817 offset in the parent objfile. */
14818
14819CORE_ADDR
14820dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14821{
bb3fa9d0 14822 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14823
14824 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14825}
14826
348e048f
DE
14827/* Locate the .debug_info compilation unit from CU's objfile which contains
14828 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14829
14830static struct dwarf2_per_cu_data *
c764a876 14831dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14832 struct objfile *objfile)
14833{
14834 struct dwarf2_per_cu_data *this_cu;
14835 int low, high;
14836
ae038cb0
DJ
14837 low = 0;
14838 high = dwarf2_per_objfile->n_comp_units - 1;
14839 while (high > low)
14840 {
14841 int mid = low + (high - low) / 2;
9a619af0 14842
ae038cb0
DJ
14843 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14844 high = mid;
14845 else
14846 low = mid + 1;
14847 }
14848 gdb_assert (low == high);
14849 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14850 {
10b3939b 14851 if (low == 0)
8a3fe4f8
AC
14852 error (_("Dwarf Error: could not find partial DIE containing "
14853 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14854 (long) offset, bfd_get_filename (objfile->obfd));
14855
ae038cb0
DJ
14856 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14857 return dwarf2_per_objfile->all_comp_units[low-1];
14858 }
14859 else
14860 {
14861 this_cu = dwarf2_per_objfile->all_comp_units[low];
14862 if (low == dwarf2_per_objfile->n_comp_units - 1
14863 && offset >= this_cu->offset + this_cu->length)
c764a876 14864 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14865 gdb_assert (offset < this_cu->offset + this_cu->length);
14866 return this_cu;
14867 }
14868}
14869
10b3939b
DJ
14870/* Locate the compilation unit from OBJFILE which is located at exactly
14871 OFFSET. Raises an error on failure. */
14872
ae038cb0 14873static struct dwarf2_per_cu_data *
c764a876 14874dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14875{
14876 struct dwarf2_per_cu_data *this_cu;
9a619af0 14877
ae038cb0
DJ
14878 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14879 if (this_cu->offset != offset)
c764a876 14880 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14881 return this_cu;
14882}
14883
9816fde3 14884/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 14885
9816fde3
JK
14886static void
14887init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 14888{
9816fde3 14889 memset (cu, 0, sizeof (*cu));
93311388
DE
14890 cu->objfile = objfile;
14891 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
14892}
14893
14894/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14895
14896static void
14897prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14898{
14899 struct attribute *attr;
14900
14901 /* Set the language we're debugging. */
14902 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14903 if (attr)
14904 set_cu_language (DW_UNSND (attr), cu);
14905 else
9cded63f
TT
14906 {
14907 cu->language = language_minimal;
14908 cu->language_defn = language_def (cu->language);
14909 }
93311388
DE
14910}
14911
ae038cb0
DJ
14912/* Release one cached compilation unit, CU. We unlink it from the tree
14913 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14914 the caller is responsible for that.
14915 NOTE: DATA is a void * because this function is also used as a
14916 cleanup routine. */
ae038cb0
DJ
14917
14918static void
14919free_one_comp_unit (void *data)
14920{
14921 struct dwarf2_cu *cu = data;
14922
14923 if (cu->per_cu != NULL)
14924 cu->per_cu->cu = NULL;
14925 cu->per_cu = NULL;
14926
14927 obstack_free (&cu->comp_unit_obstack, NULL);
14928
14929 xfree (cu);
14930}
14931
72bf9492 14932/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14933 when we're finished with it. We can't free the pointer itself, but be
14934 sure to unlink it from the cache. Also release any associated storage
14935 and perform cache maintenance.
72bf9492
DJ
14936
14937 Only used during partial symbol parsing. */
14938
14939static void
14940free_stack_comp_unit (void *data)
14941{
14942 struct dwarf2_cu *cu = data;
14943
14944 obstack_free (&cu->comp_unit_obstack, NULL);
14945 cu->partial_dies = NULL;
ae038cb0
DJ
14946
14947 if (cu->per_cu != NULL)
14948 {
14949 /* This compilation unit is on the stack in our caller, so we
14950 should not xfree it. Just unlink it. */
14951 cu->per_cu->cu = NULL;
14952 cu->per_cu = NULL;
14953
14954 /* If we had a per-cu pointer, then we may have other compilation
14955 units loaded, so age them now. */
14956 age_cached_comp_units ();
14957 }
14958}
14959
14960/* Free all cached compilation units. */
14961
14962static void
14963free_cached_comp_units (void *data)
14964{
14965 struct dwarf2_per_cu_data *per_cu, **last_chain;
14966
14967 per_cu = dwarf2_per_objfile->read_in_chain;
14968 last_chain = &dwarf2_per_objfile->read_in_chain;
14969 while (per_cu != NULL)
14970 {
14971 struct dwarf2_per_cu_data *next_cu;
14972
14973 next_cu = per_cu->cu->read_in_chain;
14974
14975 free_one_comp_unit (per_cu->cu);
14976 *last_chain = next_cu;
14977
14978 per_cu = next_cu;
14979 }
14980}
14981
14982/* Increase the age counter on each cached compilation unit, and free
14983 any that are too old. */
14984
14985static void
14986age_cached_comp_units (void)
14987{
14988 struct dwarf2_per_cu_data *per_cu, **last_chain;
14989
14990 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14991 per_cu = dwarf2_per_objfile->read_in_chain;
14992 while (per_cu != NULL)
14993 {
14994 per_cu->cu->last_used ++;
14995 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14996 dwarf2_mark (per_cu->cu);
14997 per_cu = per_cu->cu->read_in_chain;
14998 }
14999
15000 per_cu = dwarf2_per_objfile->read_in_chain;
15001 last_chain = &dwarf2_per_objfile->read_in_chain;
15002 while (per_cu != NULL)
15003 {
15004 struct dwarf2_per_cu_data *next_cu;
15005
15006 next_cu = per_cu->cu->read_in_chain;
15007
15008 if (!per_cu->cu->mark)
15009 {
15010 free_one_comp_unit (per_cu->cu);
15011 *last_chain = next_cu;
15012 }
15013 else
15014 last_chain = &per_cu->cu->read_in_chain;
15015
15016 per_cu = next_cu;
15017 }
15018}
15019
15020/* Remove a single compilation unit from the cache. */
15021
15022static void
15023free_one_cached_comp_unit (void *target_cu)
15024{
15025 struct dwarf2_per_cu_data *per_cu, **last_chain;
15026
15027 per_cu = dwarf2_per_objfile->read_in_chain;
15028 last_chain = &dwarf2_per_objfile->read_in_chain;
15029 while (per_cu != NULL)
15030 {
15031 struct dwarf2_per_cu_data *next_cu;
15032
15033 next_cu = per_cu->cu->read_in_chain;
15034
15035 if (per_cu->cu == target_cu)
15036 {
15037 free_one_comp_unit (per_cu->cu);
15038 *last_chain = next_cu;
15039 break;
15040 }
15041 else
15042 last_chain = &per_cu->cu->read_in_chain;
15043
15044 per_cu = next_cu;
15045 }
15046}
15047
fe3e1990
DJ
15048/* Release all extra memory associated with OBJFILE. */
15049
15050void
15051dwarf2_free_objfile (struct objfile *objfile)
15052{
15053 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15054
15055 if (dwarf2_per_objfile == NULL)
15056 return;
15057
15058 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15059 free_cached_comp_units (NULL);
15060
7b9f3c50
DE
15061 if (dwarf2_per_objfile->quick_file_names_table)
15062 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 15063
fe3e1990
DJ
15064 /* Everything else should be on the objfile obstack. */
15065}
15066
1c379e20
DJ
15067/* A pair of DIE offset and GDB type pointer. We store these
15068 in a hash table separate from the DIEs, and preserve them
15069 when the DIEs are flushed out of cache. */
15070
15071struct dwarf2_offset_and_type
15072{
15073 unsigned int offset;
15074 struct type *type;
15075};
15076
15077/* Hash function for a dwarf2_offset_and_type. */
15078
15079static hashval_t
15080offset_and_type_hash (const void *item)
15081{
15082 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 15083
1c379e20
DJ
15084 return ofs->offset;
15085}
15086
15087/* Equality function for a dwarf2_offset_and_type. */
15088
15089static int
15090offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15091{
15092 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15093 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 15094
1c379e20
DJ
15095 return ofs_lhs->offset == ofs_rhs->offset;
15096}
15097
15098/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
15099 table if necessary. For convenience, return TYPE.
15100
15101 The DIEs reading must have careful ordering to:
15102 * Not cause infite loops trying to read in DIEs as a prerequisite for
15103 reading current DIE.
15104 * Not trying to dereference contents of still incompletely read in types
15105 while reading in other DIEs.
15106 * Enable referencing still incompletely read in types just by a pointer to
15107 the type without accessing its fields.
15108
15109 Therefore caller should follow these rules:
15110 * Try to fetch any prerequisite types we may need to build this DIE type
15111 before building the type and calling set_die_type.
e71ec853 15112 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
15113 possible before fetching more types to complete the current type.
15114 * Make the type as complete as possible before fetching more types. */
1c379e20 15115
f792889a 15116static struct type *
1c379e20
DJ
15117set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15118{
15119 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
15120 struct objfile *objfile = cu->objfile;
15121 htab_t *type_hash_ptr;
1c379e20 15122
b4ba55a1
JB
15123 /* For Ada types, make sure that the gnat-specific data is always
15124 initialized (if not already set). There are a few types where
15125 we should not be doing so, because the type-specific area is
15126 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15127 where the type-specific area is used to store the floatformat).
15128 But this is not a problem, because the gnat-specific information
15129 is actually not needed for these types. */
15130 if (need_gnat_info (cu)
15131 && TYPE_CODE (type) != TYPE_CODE_FUNC
15132 && TYPE_CODE (type) != TYPE_CODE_FLT
15133 && !HAVE_GNAT_AUX_INFO (type))
15134 INIT_GNAT_SPECIFIC (type);
15135
673bfd45
DE
15136 if (cu->per_cu->from_debug_types)
15137 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15138 else
15139 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15140
15141 if (*type_hash_ptr == NULL)
f792889a 15142 {
673bfd45
DE
15143 *type_hash_ptr
15144 = htab_create_alloc_ex (127,
f792889a
DJ
15145 offset_and_type_hash,
15146 offset_and_type_eq,
15147 NULL,
673bfd45 15148 &objfile->objfile_obstack,
f792889a
DJ
15149 hashtab_obstack_allocate,
15150 dummy_obstack_deallocate);
f792889a 15151 }
1c379e20
DJ
15152
15153 ofs.offset = die->offset;
15154 ofs.type = type;
15155 slot = (struct dwarf2_offset_and_type **)
673bfd45 15156 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
15157 if (*slot)
15158 complaint (&symfile_complaints,
15159 _("A problem internal to GDB: DIE 0x%x has type already set"),
15160 die->offset);
673bfd45 15161 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 15162 **slot = ofs;
f792889a 15163 return type;
1c379e20
DJ
15164}
15165
673bfd45
DE
15166/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15167 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
15168
15169static struct type *
673bfd45
DE
15170get_die_type_at_offset (unsigned int offset,
15171 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
15172{
15173 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 15174 htab_t type_hash;
f792889a 15175
673bfd45
DE
15176 if (per_cu->from_debug_types)
15177 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15178 else
15179 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
15180 if (type_hash == NULL)
15181 return NULL;
1c379e20 15182
673bfd45 15183 ofs.offset = offset;
1c379e20
DJ
15184 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15185 if (slot)
15186 return slot->type;
15187 else
15188 return NULL;
15189}
15190
673bfd45
DE
15191/* Look up the type for DIE in the appropriate type_hash table,
15192 or return NULL if DIE does not have a saved type. */
15193
15194static struct type *
15195get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15196{
15197 return get_die_type_at_offset (die->offset, cu->per_cu);
15198}
15199
10b3939b
DJ
15200/* Add a dependence relationship from CU to REF_PER_CU. */
15201
15202static void
15203dwarf2_add_dependence (struct dwarf2_cu *cu,
15204 struct dwarf2_per_cu_data *ref_per_cu)
15205{
15206 void **slot;
15207
15208 if (cu->dependencies == NULL)
15209 cu->dependencies
15210 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15211 NULL, &cu->comp_unit_obstack,
15212 hashtab_obstack_allocate,
15213 dummy_obstack_deallocate);
15214
15215 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15216 if (*slot == NULL)
15217 *slot = ref_per_cu;
15218}
1c379e20 15219
f504f079
DE
15220/* Subroutine of dwarf2_mark to pass to htab_traverse.
15221 Set the mark field in every compilation unit in the
ae038cb0
DJ
15222 cache that we must keep because we are keeping CU. */
15223
10b3939b
DJ
15224static int
15225dwarf2_mark_helper (void **slot, void *data)
15226{
15227 struct dwarf2_per_cu_data *per_cu;
15228
15229 per_cu = (struct dwarf2_per_cu_data *) *slot;
15230 if (per_cu->cu->mark)
15231 return 1;
15232 per_cu->cu->mark = 1;
15233
15234 if (per_cu->cu->dependencies != NULL)
15235 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15236
15237 return 1;
15238}
15239
f504f079
DE
15240/* Set the mark field in CU and in every other compilation unit in the
15241 cache that we must keep because we are keeping CU. */
15242
ae038cb0
DJ
15243static void
15244dwarf2_mark (struct dwarf2_cu *cu)
15245{
15246 if (cu->mark)
15247 return;
15248 cu->mark = 1;
10b3939b
DJ
15249 if (cu->dependencies != NULL)
15250 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
15251}
15252
15253static void
15254dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15255{
15256 while (per_cu)
15257 {
15258 per_cu->cu->mark = 0;
15259 per_cu = per_cu->cu->read_in_chain;
15260 }
72bf9492
DJ
15261}
15262
72bf9492
DJ
15263/* Trivial hash function for partial_die_info: the hash value of a DIE
15264 is its offset in .debug_info for this objfile. */
15265
15266static hashval_t
15267partial_die_hash (const void *item)
15268{
15269 const struct partial_die_info *part_die = item;
9a619af0 15270
72bf9492
DJ
15271 return part_die->offset;
15272}
15273
15274/* Trivial comparison function for partial_die_info structures: two DIEs
15275 are equal if they have the same offset. */
15276
15277static int
15278partial_die_eq (const void *item_lhs, const void *item_rhs)
15279{
15280 const struct partial_die_info *part_die_lhs = item_lhs;
15281 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 15282
72bf9492
DJ
15283 return part_die_lhs->offset == part_die_rhs->offset;
15284}
15285
ae038cb0
DJ
15286static struct cmd_list_element *set_dwarf2_cmdlist;
15287static struct cmd_list_element *show_dwarf2_cmdlist;
15288
15289static void
15290set_dwarf2_cmd (char *args, int from_tty)
15291{
15292 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15293}
15294
15295static void
15296show_dwarf2_cmd (char *args, int from_tty)
6e70227d 15297{
ae038cb0
DJ
15298 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15299}
15300
dce234bc
PP
15301/* If section described by INFO was mmapped, munmap it now. */
15302
15303static void
15304munmap_section_buffer (struct dwarf2_section_info *info)
15305{
15306 if (info->was_mmapped)
15307 {
15308#ifdef HAVE_MMAP
15309 intptr_t begin = (intptr_t) info->buffer;
15310 intptr_t map_begin = begin & ~(pagesize - 1);
15311 size_t map_length = info->size + begin - map_begin;
9a619af0 15312
dce234bc
PP
15313 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15314#else
15315 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 15316 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
15317#endif
15318 }
15319}
15320
15321/* munmap debug sections for OBJFILE, if necessary. */
15322
15323static void
c1bd65d0 15324dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
15325{
15326 struct dwarf2_per_objfile *data = d;
9a619af0 15327
16be1145
DE
15328 /* This is sorted according to the order they're defined in to make it easier
15329 to keep in sync. */
dce234bc
PP
15330 munmap_section_buffer (&data->info);
15331 munmap_section_buffer (&data->abbrev);
15332 munmap_section_buffer (&data->line);
16be1145 15333 munmap_section_buffer (&data->loc);
dce234bc 15334 munmap_section_buffer (&data->macinfo);
16be1145 15335 munmap_section_buffer (&data->str);
dce234bc 15336 munmap_section_buffer (&data->ranges);
16be1145 15337 munmap_section_buffer (&data->types);
dce234bc
PP
15338 munmap_section_buffer (&data->frame);
15339 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
15340 munmap_section_buffer (&data->gdb_index);
15341}
15342
15343\f
ae2de4f8 15344/* The "save gdb-index" command. */
9291a0cd
TT
15345
15346/* The contents of the hash table we create when building the string
15347 table. */
15348struct strtab_entry
15349{
15350 offset_type offset;
15351 const char *str;
15352};
15353
559a7a62
JK
15354/* Hash function for a strtab_entry.
15355
15356 Function is used only during write_hash_table so no index format backward
15357 compatibility is needed. */
b89be57b 15358
9291a0cd
TT
15359static hashval_t
15360hash_strtab_entry (const void *e)
15361{
15362 const struct strtab_entry *entry = e;
559a7a62 15363 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
15364}
15365
15366/* Equality function for a strtab_entry. */
b89be57b 15367
9291a0cd
TT
15368static int
15369eq_strtab_entry (const void *a, const void *b)
15370{
15371 const struct strtab_entry *ea = a;
15372 const struct strtab_entry *eb = b;
15373 return !strcmp (ea->str, eb->str);
15374}
15375
15376/* Create a strtab_entry hash table. */
b89be57b 15377
9291a0cd
TT
15378static htab_t
15379create_strtab (void)
15380{
15381 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15382 xfree, xcalloc, xfree);
15383}
15384
15385/* Add a string to the constant pool. Return the string's offset in
15386 host order. */
b89be57b 15387
9291a0cd
TT
15388static offset_type
15389add_string (htab_t table, struct obstack *cpool, const char *str)
15390{
15391 void **slot;
15392 struct strtab_entry entry;
15393 struct strtab_entry *result;
15394
15395 entry.str = str;
15396 slot = htab_find_slot (table, &entry, INSERT);
15397 if (*slot)
15398 result = *slot;
15399 else
15400 {
15401 result = XNEW (struct strtab_entry);
15402 result->offset = obstack_object_size (cpool);
15403 result->str = str;
15404 obstack_grow_str0 (cpool, str);
15405 *slot = result;
15406 }
15407 return result->offset;
15408}
15409
15410/* An entry in the symbol table. */
15411struct symtab_index_entry
15412{
15413 /* The name of the symbol. */
15414 const char *name;
15415 /* The offset of the name in the constant pool. */
15416 offset_type index_offset;
15417 /* A sorted vector of the indices of all the CUs that hold an object
15418 of this name. */
15419 VEC (offset_type) *cu_indices;
15420};
15421
15422/* The symbol table. This is a power-of-2-sized hash table. */
15423struct mapped_symtab
15424{
15425 offset_type n_elements;
15426 offset_type size;
15427 struct symtab_index_entry **data;
15428};
15429
15430/* Hash function for a symtab_index_entry. */
b89be57b 15431
9291a0cd
TT
15432static hashval_t
15433hash_symtab_entry (const void *e)
15434{
15435 const struct symtab_index_entry *entry = e;
15436 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15437 sizeof (offset_type) * VEC_length (offset_type,
15438 entry->cu_indices),
15439 0);
15440}
15441
15442/* Equality function for a symtab_index_entry. */
b89be57b 15443
9291a0cd
TT
15444static int
15445eq_symtab_entry (const void *a, const void *b)
15446{
15447 const struct symtab_index_entry *ea = a;
15448 const struct symtab_index_entry *eb = b;
15449 int len = VEC_length (offset_type, ea->cu_indices);
15450 if (len != VEC_length (offset_type, eb->cu_indices))
15451 return 0;
15452 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15453 VEC_address (offset_type, eb->cu_indices),
15454 sizeof (offset_type) * len);
15455}
15456
15457/* Destroy a symtab_index_entry. */
b89be57b 15458
9291a0cd
TT
15459static void
15460delete_symtab_entry (void *p)
15461{
15462 struct symtab_index_entry *entry = p;
15463 VEC_free (offset_type, entry->cu_indices);
15464 xfree (entry);
15465}
15466
15467/* Create a hash table holding symtab_index_entry objects. */
b89be57b 15468
9291a0cd 15469static htab_t
3876f04e 15470create_symbol_hash_table (void)
9291a0cd
TT
15471{
15472 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15473 delete_symtab_entry, xcalloc, xfree);
15474}
15475
15476/* Create a new mapped symtab object. */
b89be57b 15477
9291a0cd
TT
15478static struct mapped_symtab *
15479create_mapped_symtab (void)
15480{
15481 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15482 symtab->n_elements = 0;
15483 symtab->size = 1024;
15484 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15485 return symtab;
15486}
15487
15488/* Destroy a mapped_symtab. */
b89be57b 15489
9291a0cd
TT
15490static void
15491cleanup_mapped_symtab (void *p)
15492{
15493 struct mapped_symtab *symtab = p;
15494 /* The contents of the array are freed when the other hash table is
15495 destroyed. */
15496 xfree (symtab->data);
15497 xfree (symtab);
15498}
15499
15500/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
15501 the slot.
15502
15503 Function is used only during write_hash_table so no index format backward
15504 compatibility is needed. */
b89be57b 15505
9291a0cd
TT
15506static struct symtab_index_entry **
15507find_slot (struct mapped_symtab *symtab, const char *name)
15508{
559a7a62 15509 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
15510
15511 index = hash & (symtab->size - 1);
15512 step = ((hash * 17) & (symtab->size - 1)) | 1;
15513
15514 for (;;)
15515 {
15516 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15517 return &symtab->data[index];
15518 index = (index + step) & (symtab->size - 1);
15519 }
15520}
15521
15522/* Expand SYMTAB's hash table. */
b89be57b 15523
9291a0cd
TT
15524static void
15525hash_expand (struct mapped_symtab *symtab)
15526{
15527 offset_type old_size = symtab->size;
15528 offset_type i;
15529 struct symtab_index_entry **old_entries = symtab->data;
15530
15531 symtab->size *= 2;
15532 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15533
15534 for (i = 0; i < old_size; ++i)
15535 {
15536 if (old_entries[i])
15537 {
15538 struct symtab_index_entry **slot = find_slot (symtab,
15539 old_entries[i]->name);
15540 *slot = old_entries[i];
15541 }
15542 }
15543
15544 xfree (old_entries);
15545}
15546
15547/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15548 is the index of the CU in which the symbol appears. */
b89be57b 15549
9291a0cd
TT
15550static void
15551add_index_entry (struct mapped_symtab *symtab, const char *name,
15552 offset_type cu_index)
15553{
15554 struct symtab_index_entry **slot;
15555
15556 ++symtab->n_elements;
15557 if (4 * symtab->n_elements / 3 >= symtab->size)
15558 hash_expand (symtab);
15559
15560 slot = find_slot (symtab, name);
15561 if (!*slot)
15562 {
15563 *slot = XNEW (struct symtab_index_entry);
15564 (*slot)->name = name;
15565 (*slot)->cu_indices = NULL;
15566 }
15567 /* Don't push an index twice. Due to how we add entries we only
15568 have to check the last one. */
15569 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 15570 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
15571 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15572}
15573
15574/* Add a vector of indices to the constant pool. */
b89be57b 15575
9291a0cd 15576static offset_type
3876f04e 15577add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15578 struct symtab_index_entry *entry)
15579{
15580 void **slot;
15581
3876f04e 15582 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15583 if (!*slot)
15584 {
15585 offset_type len = VEC_length (offset_type, entry->cu_indices);
15586 offset_type val = MAYBE_SWAP (len);
15587 offset_type iter;
15588 int i;
15589
15590 *slot = entry;
15591 entry->index_offset = obstack_object_size (cpool);
15592
15593 obstack_grow (cpool, &val, sizeof (val));
15594 for (i = 0;
15595 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15596 ++i)
15597 {
15598 val = MAYBE_SWAP (iter);
15599 obstack_grow (cpool, &val, sizeof (val));
15600 }
15601 }
15602 else
15603 {
15604 struct symtab_index_entry *old_entry = *slot;
15605 entry->index_offset = old_entry->index_offset;
15606 entry = old_entry;
15607 }
15608 return entry->index_offset;
15609}
15610
15611/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15612 constant pool entries going into the obstack CPOOL. */
b89be57b 15613
9291a0cd
TT
15614static void
15615write_hash_table (struct mapped_symtab *symtab,
15616 struct obstack *output, struct obstack *cpool)
15617{
15618 offset_type i;
3876f04e 15619 htab_t symbol_hash_table;
9291a0cd
TT
15620 htab_t str_table;
15621
3876f04e 15622 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15623 str_table = create_strtab ();
3876f04e 15624
9291a0cd
TT
15625 /* We add all the index vectors to the constant pool first, to
15626 ensure alignment is ok. */
15627 for (i = 0; i < symtab->size; ++i)
15628 {
15629 if (symtab->data[i])
3876f04e 15630 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15631 }
15632
15633 /* Now write out the hash table. */
15634 for (i = 0; i < symtab->size; ++i)
15635 {
15636 offset_type str_off, vec_off;
15637
15638 if (symtab->data[i])
15639 {
15640 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15641 vec_off = symtab->data[i]->index_offset;
15642 }
15643 else
15644 {
15645 /* While 0 is a valid constant pool index, it is not valid
15646 to have 0 for both offsets. */
15647 str_off = 0;
15648 vec_off = 0;
15649 }
15650
15651 str_off = MAYBE_SWAP (str_off);
15652 vec_off = MAYBE_SWAP (vec_off);
15653
15654 obstack_grow (output, &str_off, sizeof (str_off));
15655 obstack_grow (output, &vec_off, sizeof (vec_off));
15656 }
15657
15658 htab_delete (str_table);
3876f04e 15659 htab_delete (symbol_hash_table);
9291a0cd
TT
15660}
15661
0a5429f6
DE
15662/* Struct to map psymtab to CU index in the index file. */
15663struct psymtab_cu_index_map
15664{
15665 struct partial_symtab *psymtab;
15666 unsigned int cu_index;
15667};
15668
15669static hashval_t
15670hash_psymtab_cu_index (const void *item)
15671{
15672 const struct psymtab_cu_index_map *map = item;
15673
15674 return htab_hash_pointer (map->psymtab);
15675}
15676
15677static int
15678eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15679{
15680 const struct psymtab_cu_index_map *lhs = item_lhs;
15681 const struct psymtab_cu_index_map *rhs = item_rhs;
15682
15683 return lhs->psymtab == rhs->psymtab;
15684}
15685
15686/* Helper struct for building the address table. */
15687struct addrmap_index_data
15688{
15689 struct objfile *objfile;
15690 struct obstack *addr_obstack;
15691 htab_t cu_index_htab;
15692
15693 /* Non-zero if the previous_* fields are valid.
15694 We can't write an entry until we see the next entry (since it is only then
15695 that we know the end of the entry). */
15696 int previous_valid;
15697 /* Index of the CU in the table of all CUs in the index file. */
15698 unsigned int previous_cu_index;
0963b4bd 15699 /* Start address of the CU. */
0a5429f6
DE
15700 CORE_ADDR previous_cu_start;
15701};
15702
15703/* Write an address entry to OBSTACK. */
b89be57b 15704
9291a0cd 15705static void
0a5429f6
DE
15706add_address_entry (struct objfile *objfile, struct obstack *obstack,
15707 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 15708{
0a5429f6 15709 offset_type cu_index_to_write;
9291a0cd
TT
15710 char addr[8];
15711 CORE_ADDR baseaddr;
15712
15713 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15714
0a5429f6
DE
15715 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15716 obstack_grow (obstack, addr, 8);
15717 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15718 obstack_grow (obstack, addr, 8);
15719 cu_index_to_write = MAYBE_SWAP (cu_index);
15720 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15721}
15722
15723/* Worker function for traversing an addrmap to build the address table. */
15724
15725static int
15726add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15727{
15728 struct addrmap_index_data *data = datap;
15729 struct partial_symtab *pst = obj;
15730 offset_type cu_index;
15731 void **slot;
15732
15733 if (data->previous_valid)
15734 add_address_entry (data->objfile, data->addr_obstack,
15735 data->previous_cu_start, start_addr,
15736 data->previous_cu_index);
15737
15738 data->previous_cu_start = start_addr;
15739 if (pst != NULL)
15740 {
15741 struct psymtab_cu_index_map find_map, *map;
15742 find_map.psymtab = pst;
15743 map = htab_find (data->cu_index_htab, &find_map);
15744 gdb_assert (map != NULL);
15745 data->previous_cu_index = map->cu_index;
15746 data->previous_valid = 1;
15747 }
15748 else
15749 data->previous_valid = 0;
15750
15751 return 0;
15752}
15753
15754/* Write OBJFILE's address map to OBSTACK.
15755 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15756 in the index file. */
15757
15758static void
15759write_address_map (struct objfile *objfile, struct obstack *obstack,
15760 htab_t cu_index_htab)
15761{
15762 struct addrmap_index_data addrmap_index_data;
15763
15764 /* When writing the address table, we have to cope with the fact that
15765 the addrmap iterator only provides the start of a region; we have to
15766 wait until the next invocation to get the start of the next region. */
15767
15768 addrmap_index_data.objfile = objfile;
15769 addrmap_index_data.addr_obstack = obstack;
15770 addrmap_index_data.cu_index_htab = cu_index_htab;
15771 addrmap_index_data.previous_valid = 0;
15772
15773 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15774 &addrmap_index_data);
15775
15776 /* It's highly unlikely the last entry (end address = 0xff...ff)
15777 is valid, but we should still handle it.
15778 The end address is recorded as the start of the next region, but that
15779 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15780 anyway. */
15781 if (addrmap_index_data.previous_valid)
15782 add_address_entry (objfile, obstack,
15783 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15784 addrmap_index_data.previous_cu_index);
9291a0cd
TT
15785}
15786
15787/* Add a list of partial symbols to SYMTAB. */
b89be57b 15788
9291a0cd
TT
15789static void
15790write_psymbols (struct mapped_symtab *symtab,
987d643c 15791 htab_t psyms_seen,
9291a0cd
TT
15792 struct partial_symbol **psymp,
15793 int count,
987d643c
TT
15794 offset_type cu_index,
15795 int is_static)
9291a0cd
TT
15796{
15797 for (; count-- > 0; ++psymp)
15798 {
987d643c
TT
15799 void **slot, *lookup;
15800
9291a0cd
TT
15801 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15802 error (_("Ada is not currently supported by the index"));
987d643c
TT
15803
15804 /* We only want to add a given psymbol once. However, we also
15805 want to account for whether it is global or static. So, we
15806 may add it twice, using slightly different values. */
15807 if (is_static)
15808 {
15809 uintptr_t val = 1 | (uintptr_t) *psymp;
15810
15811 lookup = (void *) val;
15812 }
15813 else
15814 lookup = *psymp;
15815
15816 /* Only add a given psymbol once. */
15817 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15818 if (!*slot)
15819 {
15820 *slot = lookup;
15821 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15822 }
9291a0cd
TT
15823 }
15824}
15825
15826/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15827 exception if there is an error. */
b89be57b 15828
9291a0cd
TT
15829static void
15830write_obstack (FILE *file, struct obstack *obstack)
15831{
15832 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15833 file)
15834 != obstack_object_size (obstack))
15835 error (_("couldn't data write to file"));
15836}
15837
15838/* Unlink a file if the argument is not NULL. */
b89be57b 15839
9291a0cd
TT
15840static void
15841unlink_if_set (void *p)
15842{
15843 char **filename = p;
15844 if (*filename)
15845 unlink (*filename);
15846}
15847
1fd400ff
TT
15848/* A helper struct used when iterating over debug_types. */
15849struct signatured_type_index_data
15850{
15851 struct objfile *objfile;
15852 struct mapped_symtab *symtab;
15853 struct obstack *types_list;
987d643c 15854 htab_t psyms_seen;
1fd400ff
TT
15855 int cu_index;
15856};
15857
15858/* A helper function that writes a single signatured_type to an
15859 obstack. */
b89be57b 15860
1fd400ff
TT
15861static int
15862write_one_signatured_type (void **slot, void *d)
15863{
15864 struct signatured_type_index_data *info = d;
15865 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15866 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15867 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15868 gdb_byte val[8];
15869
15870 write_psymbols (info->symtab,
987d643c 15871 info->psyms_seen,
3e43a32a
MS
15872 info->objfile->global_psymbols.list
15873 + psymtab->globals_offset,
987d643c
TT
15874 psymtab->n_global_syms, info->cu_index,
15875 0);
1fd400ff 15876 write_psymbols (info->symtab,
987d643c 15877 info->psyms_seen,
3e43a32a
MS
15878 info->objfile->static_psymbols.list
15879 + psymtab->statics_offset,
987d643c
TT
15880 psymtab->n_static_syms, info->cu_index,
15881 1);
1fd400ff
TT
15882
15883 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15884 obstack_grow (info->types_list, val, 8);
15885 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15886 obstack_grow (info->types_list, val, 8);
15887 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15888 obstack_grow (info->types_list, val, 8);
15889
15890 ++info->cu_index;
15891
15892 return 1;
15893}
15894
987d643c
TT
15895/* A cleanup function for an htab_t. */
15896
15897static void
15898cleanup_htab (void *arg)
15899{
15900 htab_delete (arg);
15901}
15902
9291a0cd 15903/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 15904
9291a0cd
TT
15905static void
15906write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15907{
15908 struct cleanup *cleanup;
15909 char *filename, *cleanup_filename;
1fd400ff
TT
15910 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15911 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15912 int i;
15913 FILE *out_file;
15914 struct mapped_symtab *symtab;
15915 offset_type val, size_of_contents, total_len;
15916 struct stat st;
15917 char buf[8];
987d643c 15918 htab_t psyms_seen;
0a5429f6
DE
15919 htab_t cu_index_htab;
15920 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 15921
b4f2f049 15922 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 15923 return;
b4f2f049 15924
9291a0cd
TT
15925 if (dwarf2_per_objfile->using_index)
15926 error (_("Cannot use an index to create the index"));
15927
15928 if (stat (objfile->name, &st) < 0)
7e17e088 15929 perror_with_name (objfile->name);
9291a0cd
TT
15930
15931 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15932 INDEX_SUFFIX, (char *) NULL);
15933 cleanup = make_cleanup (xfree, filename);
15934
15935 out_file = fopen (filename, "wb");
15936 if (!out_file)
15937 error (_("Can't open `%s' for writing"), filename);
15938
15939 cleanup_filename = filename;
15940 make_cleanup (unlink_if_set, &cleanup_filename);
15941
15942 symtab = create_mapped_symtab ();
15943 make_cleanup (cleanup_mapped_symtab, symtab);
15944
15945 obstack_init (&addr_obstack);
15946 make_cleanup_obstack_free (&addr_obstack);
15947
15948 obstack_init (&cu_list);
15949 make_cleanup_obstack_free (&cu_list);
15950
1fd400ff
TT
15951 obstack_init (&types_cu_list);
15952 make_cleanup_obstack_free (&types_cu_list);
15953
987d643c
TT
15954 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15955 NULL, xcalloc, xfree);
15956 make_cleanup (cleanup_htab, psyms_seen);
15957
0a5429f6
DE
15958 /* While we're scanning CU's create a table that maps a psymtab pointer
15959 (which is what addrmap records) to its index (which is what is recorded
15960 in the index file). This will later be needed to write the address
15961 table. */
15962 cu_index_htab = htab_create_alloc (100,
15963 hash_psymtab_cu_index,
15964 eq_psymtab_cu_index,
15965 NULL, xcalloc, xfree);
15966 make_cleanup (cleanup_htab, cu_index_htab);
15967 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15968 xmalloc (sizeof (struct psymtab_cu_index_map)
15969 * dwarf2_per_objfile->n_comp_units);
15970 make_cleanup (xfree, psymtab_cu_index_map);
15971
15972 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
15973 work here. Also, the debug_types entries do not appear in
15974 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15975 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15976 {
3e43a32a
MS
15977 struct dwarf2_per_cu_data *per_cu
15978 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 15979 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 15980 gdb_byte val[8];
0a5429f6
DE
15981 struct psymtab_cu_index_map *map;
15982 void **slot;
9291a0cd
TT
15983
15984 write_psymbols (symtab,
987d643c 15985 psyms_seen,
9291a0cd 15986 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15987 psymtab->n_global_syms, i,
15988 0);
9291a0cd 15989 write_psymbols (symtab,
987d643c 15990 psyms_seen,
9291a0cd 15991 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15992 psymtab->n_static_syms, i,
15993 1);
9291a0cd 15994
0a5429f6
DE
15995 map = &psymtab_cu_index_map[i];
15996 map->psymtab = psymtab;
15997 map->cu_index = i;
15998 slot = htab_find_slot (cu_index_htab, map, INSERT);
15999 gdb_assert (slot != NULL);
16000 gdb_assert (*slot == NULL);
16001 *slot = map;
9291a0cd 16002
e254ef6a 16003 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 16004 obstack_grow (&cu_list, val, 8);
e254ef6a 16005 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
16006 obstack_grow (&cu_list, val, 8);
16007 }
16008
0a5429f6
DE
16009 /* Dump the address map. */
16010 write_address_map (objfile, &addr_obstack, cu_index_htab);
16011
1fd400ff
TT
16012 /* Write out the .debug_type entries, if any. */
16013 if (dwarf2_per_objfile->signatured_types)
16014 {
16015 struct signatured_type_index_data sig_data;
16016
16017 sig_data.objfile = objfile;
16018 sig_data.symtab = symtab;
16019 sig_data.types_list = &types_cu_list;
987d643c 16020 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
16021 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16022 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16023 write_one_signatured_type, &sig_data);
16024 }
16025
9291a0cd
TT
16026 obstack_init (&constant_pool);
16027 make_cleanup_obstack_free (&constant_pool);
16028 obstack_init (&symtab_obstack);
16029 make_cleanup_obstack_free (&symtab_obstack);
16030 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16031
16032 obstack_init (&contents);
16033 make_cleanup_obstack_free (&contents);
1fd400ff 16034 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
16035 total_len = size_of_contents;
16036
16037 /* The version number. */
559a7a62 16038 val = MAYBE_SWAP (5);
9291a0cd
TT
16039 obstack_grow (&contents, &val, sizeof (val));
16040
16041 /* The offset of the CU list from the start of the file. */
16042 val = MAYBE_SWAP (total_len);
16043 obstack_grow (&contents, &val, sizeof (val));
16044 total_len += obstack_object_size (&cu_list);
16045
1fd400ff
TT
16046 /* The offset of the types CU list from the start of the file. */
16047 val = MAYBE_SWAP (total_len);
16048 obstack_grow (&contents, &val, sizeof (val));
16049 total_len += obstack_object_size (&types_cu_list);
16050
9291a0cd
TT
16051 /* The offset of the address table from the start of the file. */
16052 val = MAYBE_SWAP (total_len);
16053 obstack_grow (&contents, &val, sizeof (val));
16054 total_len += obstack_object_size (&addr_obstack);
16055
16056 /* The offset of the symbol table from the start of the file. */
16057 val = MAYBE_SWAP (total_len);
16058 obstack_grow (&contents, &val, sizeof (val));
16059 total_len += obstack_object_size (&symtab_obstack);
16060
16061 /* The offset of the constant pool from the start of the file. */
16062 val = MAYBE_SWAP (total_len);
16063 obstack_grow (&contents, &val, sizeof (val));
16064 total_len += obstack_object_size (&constant_pool);
16065
16066 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16067
16068 write_obstack (out_file, &contents);
16069 write_obstack (out_file, &cu_list);
1fd400ff 16070 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
16071 write_obstack (out_file, &addr_obstack);
16072 write_obstack (out_file, &symtab_obstack);
16073 write_obstack (out_file, &constant_pool);
16074
16075 fclose (out_file);
16076
16077 /* We want to keep the file, so we set cleanup_filename to NULL
16078 here. See unlink_if_set. */
16079 cleanup_filename = NULL;
16080
16081 do_cleanups (cleanup);
16082}
16083
90476074
TT
16084/* Implementation of the `save gdb-index' command.
16085
16086 Note that the file format used by this command is documented in the
16087 GDB manual. Any changes here must be documented there. */
11570e71 16088
9291a0cd
TT
16089static void
16090save_gdb_index_command (char *arg, int from_tty)
16091{
16092 struct objfile *objfile;
16093
16094 if (!arg || !*arg)
96d19272 16095 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
16096
16097 ALL_OBJFILES (objfile)
16098 {
16099 struct stat st;
16100
16101 /* If the objfile does not correspond to an actual file, skip it. */
16102 if (stat (objfile->name, &st) < 0)
16103 continue;
16104
16105 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16106 if (dwarf2_per_objfile)
16107 {
16108 volatile struct gdb_exception except;
16109
16110 TRY_CATCH (except, RETURN_MASK_ERROR)
16111 {
16112 write_psymtabs_to_index (objfile, arg);
16113 }
16114 if (except.reason < 0)
16115 exception_fprintf (gdb_stderr, except,
16116 _("Error while writing index for `%s': "),
16117 objfile->name);
16118 }
16119 }
dce234bc
PP
16120}
16121
9291a0cd
TT
16122\f
16123
9eae7c52
TT
16124int dwarf2_always_disassemble;
16125
16126static void
16127show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16128 struct cmd_list_element *c, const char *value)
16129{
3e43a32a
MS
16130 fprintf_filtered (file,
16131 _("Whether to always disassemble "
16132 "DWARF expressions is %s.\n"),
9eae7c52
TT
16133 value);
16134}
16135
6502dd73
DJ
16136void _initialize_dwarf2_read (void);
16137
16138void
16139_initialize_dwarf2_read (void)
16140{
96d19272
JK
16141 struct cmd_list_element *c;
16142
dce234bc 16143 dwarf2_objfile_data_key
c1bd65d0 16144 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 16145
1bedd215
AC
16146 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16147Set DWARF 2 specific variables.\n\
16148Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16149 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16150 0/*allow-unknown*/, &maintenance_set_cmdlist);
16151
1bedd215
AC
16152 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16153Show DWARF 2 specific variables\n\
16154Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16155 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16156 0/*allow-unknown*/, &maintenance_show_cmdlist);
16157
16158 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
16159 &dwarf2_max_cache_age, _("\
16160Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16161Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16162A higher limit means that cached compilation units will be stored\n\
16163in memory longer, and more total memory will be used. Zero disables\n\
16164caching, which can slow down startup."),
2c5b56ce 16165 NULL,
920d2a44 16166 show_dwarf2_max_cache_age,
2c5b56ce 16167 &set_dwarf2_cmdlist,
ae038cb0 16168 &show_dwarf2_cmdlist);
d97bc12b 16169
9eae7c52
TT
16170 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16171 &dwarf2_always_disassemble, _("\
16172Set whether `info address' always disassembles DWARF expressions."), _("\
16173Show whether `info address' always disassembles DWARF expressions."), _("\
16174When enabled, DWARF expressions are always printed in an assembly-like\n\
16175syntax. When disabled, expressions will be printed in a more\n\
16176conversational style, when possible."),
16177 NULL,
16178 show_dwarf2_always_disassemble,
16179 &set_dwarf2_cmdlist,
16180 &show_dwarf2_cmdlist);
16181
d97bc12b
DE
16182 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16183Set debugging of the dwarf2 DIE reader."), _("\
16184Show debugging of the dwarf2 DIE reader."), _("\
16185When enabled (non-zero), DIEs are dumped after they are read in.\n\
16186The value is the maximum depth to print."),
16187 NULL,
16188 NULL,
16189 &setdebuglist, &showdebuglist);
9291a0cd 16190
96d19272 16191 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 16192 _("\
fc1a9d6e 16193Save a gdb-index file.\n\
11570e71 16194Usage: save gdb-index DIRECTORY"),
96d19272
JK
16195 &save_cmdlist);
16196 set_cmd_completer (c, filename_completer);
6502dd73 16197}
This page took 2.725631 seconds and 4 git commands to generate.